emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] lisp/org-clock.el (org-clock-sum): Rewrite regex using rx
@ 2024-04-11 17:20 Morgan Smith
  2024-04-13 14:49 ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Morgan Smith @ 2024-04-11 17:20 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 839 bytes --]

Hello!

See two attached patches.  All tests pass on my computer.

Every once in a while I feel obligated to go back to org-clock-sum to
try and optimize it.  I have a file with 8 clocktables in it and it
takes forever to update.  This time I decided instead of trying to
optimize, I'm just going to try and understand.

The regex has been altered slightly.

1. Instead of using "[ \t]", I decided to use [[:blank:]].  No real
reason.  I just think it's easier to read and maybe slightly more
correct?

2. For the timestamps, instead of ".*?" (using a non-greedy ".*") I
decided to use "[^]]*" (accept everything except "]").  I did this simply
because I'm not used to using non-greedy regex's.  Maybe this way
performs better?  I didn't test that.

3. I used the variable `org-outline-regexp' but that doesn't actually
change the regex.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-org-clock.el-org-clock-sum-Rewrite-regex-using-.patch --]
[-- Type: text/x-patch, Size: 1689 bytes --]

From 3c3d7abed25cafb2be1096ca079a0e8be907c644 Mon Sep 17 00:00:00 2001
From: Morgan Smith <Morgan.J.Smith@outlook.com>
Date: Thu, 11 Apr 2024 12:23:21 -0400
Subject: [PATCH 1/2] lisp/org-clock.el (org-clock-sum): Rewrite regex using rx

---
 lisp/org-clock.el | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 65a54579a..5ef987ab8 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -2008,9 +2008,23 @@ each headline in the time range with point at the headline.  Headlines for
 which HEADLINE-FILTER returns nil are excluded from the clock summation.
 PROPNAME lets you set a custom text property instead of :org-clock-minutes."
   (with-silent-modifications
-    (let* ((re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
-		       org-clock-string
-		       "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
+    (let* ((re (rx line-start
+                   (or
+                    (group (regexp org-outline-regexp))
+                    (seq (* blank)
+                         (literal org-clock-string)
+                         (* blank)
+                         (or
+                          (seq
+                           (group "[" (* (not "]")) "]")
+                           (+ "-")
+                           (group "[" (* (not "]")) "]"))
+                          (seq
+                           "=>"
+                           (+ blank)
+                           (group (+ digit))
+                           ":"
+                           (group (+ digit))))))))
 	   (lmax 30)
 	   (ltimes (make-vector lmax 0))
 	   (level 0)
-- 
2.41.0


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-Test-clock-times-without-timestamps.patch --]
[-- Type: text/x-patch, Size: 1237 bytes --]

From e5298920568e4c5a34589640f11edfa09a98d0d1 Mon Sep 17 00:00:00 2001
From: Morgan Smith <Morgan.J.Smith@outlook.com>
Date: Thu, 11 Apr 2024 12:51:18 -0400
Subject: [PATCH 2/2] Test clock times without timestamps

* testing/lisp/test-org-clock.el (test-org-clock/clocktable/insert):
Add a clock time that does not include timestamps.
---
 testing/lisp/test-org-clock.el | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/testing/lisp/test-org-clock.el b/testing/lisp/test-org-clock.el
index 44c62e7bc..be8acb529 100644
--- a/testing/lisp/test-org-clock.el
+++ b/testing/lisp/test-org-clock.el
@@ -345,13 +345,12 @@ CLOCK: [2022-11-03 %s 06:00]--[2022-11-03 %s 06:01] =>  0:01
    (equal
     "| Headline     | Time   |
 |--------------+--------|
-| *Total time* | *1:00* |
+| *Total time* | *2:00* |
 |--------------+--------|
-| H1           | 1:00   |"
+| H1           | 2:00   |"
     (org-test-with-temp-text "* H1\n<point>"
-      (insert (org-test-clock-create-clock ". 1:00" ". 2:00"))
-
-      (goto-line 2)
+      (insert (org-test-clock-create-clock ". 1:00" ". 2:00")
+              "CLOCK: => 1:00\n")
       (require 'org-clock)
       (org-dynamic-block-insert-dblock "clocktable")
 
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] lisp/org-clock.el (org-clock-sum): Rewrite regex using rx
  2024-04-11 17:20 [PATCH] lisp/org-clock.el (org-clock-sum): Rewrite regex using rx Morgan Smith
@ 2024-04-13 14:49 ` Ihor Radchenko
  2024-04-13 16:08   ` Morgan Smith
  0 siblings, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2024-04-13 14:49 UTC (permalink / raw)
  To: Morgan Smith; +Cc: emacs-orgmode

Morgan Smith <Morgan.J.Smith@outlook.com> writes:

> See two attached patches.  All tests pass on my computer.
>
> Every once in a while I feel obligated to go back to org-clock-sum to
> try and optimize it.  I have a file with 8 clocktables in it and it
> takes forever to update.  This time I decided instead of trying to
> optimize, I'm just going to try and understand.
>
> The regex has been altered slightly.
>
> 1. Instead of using "[ \t]", I decided to use [[:blank:]].  No real
> reason.  I just think it's easier to read and maybe slightly more
> correct?
>
> 2. For the timestamps, instead of ".*?" (using a non-greedy ".*") I
> decided to use "[^]]*" (accept everything except "]").  I did this simply
> because I'm not used to using non-greedy regex's.  Maybe this way
> performs better?  I didn't test that.
>
> 3. I used the variable `org-outline-regexp' but that doesn't actually
> change the regex.

Thanks for the patch!
I think that a better approach would be re-using the parser constant
`org-element-clock-line-re'. 

> * testing/lisp/test-org-clock.el (test-org-clock/clocktable/insert):
> Add a clock time that does not include timestamps.
> ...
> -
> -      (goto-line 2)
> +      (insert (org-test-clock-create-clock ". 1:00" ". 2:00")
> +              "CLOCK: => 1:00\n")

This is not a valid clock format. Matching such lines is a bug.
See https://list.orgmode.org/orgmode/87wpkkhafc.fsf@saiph.selenimh/

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] lisp/org-clock.el (org-clock-sum): Rewrite regex using rx
  2024-04-13 14:49 ` Ihor Radchenko
@ 2024-04-13 16:08   ` Morgan Smith
  2024-04-13 16:48     ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Morgan Smith @ 2024-04-13 16:08 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

Ihor Radchenko <yantar92@posteo.net> writes:

>> * testing/lisp/test-org-clock.el (test-org-clock/clocktable/insert):
>> Add a clock time that does not include timestamps.
>> ...
>> -
>> -      (goto-line 2)
>> +      (insert (org-test-clock-create-clock ". 1:00" ". 2:00")
>> +              "CLOCK: => 1:00\n")
>
> This is not a valid clock format. Matching such lines is a bug.
> See https://list.orgmode.org/orgmode/87wpkkhafc.fsf@saiph.selenimh/

Let me preface this defense with the fact that I don't like this format
and I don't think we should support it.  Rewriting `org-clock-sum' would
be much easier if we drop support for it.  However, I do believe we
currently support it.

First of all, it currently does work.

Accord to the "Version 4.78" release notes as found on worg, this is
valid.

```
   - You may specify clocking times by hand (i.e. without
     clocking in and out) using this syntax.

     : CLOCK: => 2:00

     Thanks to Scott Jaderholm for this proposal.
```

Also last time I went to rewrite `org-clock-sum' you said
(https://list.orgmode.org/orgmode/87bkg7xbxo.fsf@localhost/):

```
Further, you dropped the

	     ((match-end 4)
	      ;; A naked time.

branch of the code, which accounts for CLOCK: => HH:MM lines that are not clock elements.
```


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] lisp/org-clock.el (org-clock-sum): Rewrite regex using rx
  2024-04-13 16:08   ` Morgan Smith
@ 2024-04-13 16:48     ` Ihor Radchenko
  2024-04-13 17:46       ` Morgan Smith
  2024-04-14 12:53       ` Ihor Radchenko
  0 siblings, 2 replies; 6+ messages in thread
From: Ihor Radchenko @ 2024-04-13 16:48 UTC (permalink / raw)
  To: Morgan Smith; +Cc: emacs-orgmode, Sanel Zukan

Morgan Smith <morgan.j.smith@outlook.com> writes:

>>> -      (goto-line 2)
>>> +      (insert (org-test-clock-create-clock ". 1:00" ". 2:00")
>>> +              "CLOCK: => 1:00\n")
>>
>> This is not a valid clock format. Matching such lines is a bug.
>> See https://list.orgmode.org/orgmode/87wpkkhafc.fsf@saiph.selenimh/
>
> Let me preface this defense with the fact that I don't like this format
> and I don't think we should support it.  Rewriting `org-clock-sum' would
> be much easier if we drop support for it.  However, I do believe we
> currently support it.
>
> First of all, it currently does work.
>
> Accord to the "Version 4.78" release notes as found on worg, this is
> valid.
>
> ```
>    - You may specify clocking times by hand (i.e. without
>      clocking in and out) using this syntax.
>
>      : CLOCK: => 2:00
>
>      Thanks to Scott Jaderholm for this proposal.
> ```

This is convincing. I did not know that this format is explicitly
mentioned in the news.

Our general rule is that we do not drop existing features in Org mode
except extraordinary circumstances:
https://bzg.fr/en/the-software-maintainers-pledge/
Especially when they are documented.

So, in the message I linked, Nicolas (the major Org mode contributor)
was not right. I hence need to fix the parser and update Org syntax
page. This includes fixing `org-element-clock-line-re' to account for
CLOCK: => 1:00 syntax.

Luckily, it does not look like we are going to break the existing
external exporter packages as long as they are using ox.el API -
`org-export-translate' works just fine with missing timestamps.

> Also last time I went to rewrite `org-clock-sum' you said
> (https://list.orgmode.org/orgmode/87bkg7xbxo.fsf@localhost/):
>
> ```
> Further, you dropped the
>
> 	     ((match-end 4)
> 	      ;; A naked time.
>
> branch of the code, which accounts for CLOCK: => HH:MM lines that are not clock elements.
> ```

Yup. Although I did not see Nicolas' message that time. My judgment was
simply based on looking at the code and seeing that CLOCK: => HH:MM
matching was clearly intentional.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] lisp/org-clock.el (org-clock-sum): Rewrite regex using rx
  2024-04-13 16:48     ` Ihor Radchenko
@ 2024-04-13 17:46       ` Morgan Smith
  2024-04-14 12:53       ` Ihor Radchenko
  1 sibling, 0 replies; 6+ messages in thread
From: Morgan Smith @ 2024-04-13 17:46 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode, Sanel Zukan

Ihor Radchenko <yantar92@posteo.net> writes:

> So, in the message I linked, Nicolas (the major Org mode contributor)
> was not right. I hence need to fix the parser and update Org syntax
> page. This includes fixing `org-element-clock-line-re' to account for
> CLOCK: => 1:00 syntax.

Cool.  I guess ping this thread when that's done so I can give you
another version of the patch.  Or if you'd like help with that stuff let
me know.  I'm here to help.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] lisp/org-clock.el (org-clock-sum): Rewrite regex using rx
  2024-04-13 16:48     ` Ihor Radchenko
  2024-04-13 17:46       ` Morgan Smith
@ 2024-04-14 12:53       ` Ihor Radchenko
  1 sibling, 0 replies; 6+ messages in thread
From: Ihor Radchenko @ 2024-04-14 12:53 UTC (permalink / raw)
  To: Morgan Smith; +Cc: emacs-orgmode, Sanel Zukan

Ihor Radchenko <yantar92@posteo.net> writes:

> So, in the message I linked, Nicolas (the major Org mode contributor)
> was not right. I hence need to fix the parser and update Org syntax
> page. This includes fixing `org-element-clock-line-re' to account for
> CLOCK: => 1:00 syntax.

I changed the parser on main.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=17072a469
and updated the syntax ref
https://git.sr.ht/~bzg/worg/commit/1c56837d

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-04-14 13:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-11 17:20 [PATCH] lisp/org-clock.el (org-clock-sum): Rewrite regex using rx Morgan Smith
2024-04-13 14:49 ` Ihor Radchenko
2024-04-13 16:08   ` Morgan Smith
2024-04-13 16:48     ` Ihor Radchenko
2024-04-13 17:46       ` Morgan Smith
2024-04-14 12:53       ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).