Re: Headline generation as in diary?

2020-10-28 Thread Michael Heerdegen
I  wrote:

> With diary, I can use arbitrary Lisp expressions to generate dates, and
> those expressions can also return a string to use as non-nil return
> value to generate non-constant headlines including certain numbers for
> example. [...]

My solution so far: I've set up my `org-capture-templates' so that when
I hit T (instead of t) I am prompted for time+headline and optionally a
number of days for a reminder, and, for example, when I enter "21:00
Test" and a "2" I get an entry like

| ** TODO Special entry: "21:00 Test"
| %%(diary-remind
|'(and
|  (dg-it-is "[2020-10-31 Sa]")
|  "TODO 21:00 Test")
|2)

`dg-it-is' is a function that tests whether "it is" the given date
specified as an inactive org time stamp.  The nice thing about this is
that org recognizes the stamp and I can click on it with the mouse to
change it etc.

And I can freely edit the whole expression.  The entry gets listed in
the agenda correctly (also the reminder) and I get two appt.el
appointments (or rather, my substitution for it).

Cons: The headline is ignored, I use a dummy headline.  I don't have
emacs-lisp bindings to edit the expression in the Capture or Org buffer,
so if I want something more complicated I probably need to use *scratch*
as intermediate step  (I tried to use a source block, but then the %%()
expression is not recognized any more, and the time stamps are not
clickable any more).

And for whatever reason, the agenda doesn't fontify the word "TODO".

If anyone has ideas for improvements, I'm curious!


Regards,

Michael.



Re: Headline generation as in diary?

2020-09-03 Thread Michael Heerdegen
Ihor Radchenko  writes:

> > I didn't know that eval specs support multi-line sexps, but seems that
> > works, so I can indeed use this.  Thanks for the suggestion.
>
> Hmm. According to manual, it should support multi-line string. Not sure
> about sexps.

AFAIR it didn't work in the past.  I guess it has been fixed in master
not long ago.

Michael.



Re: Headline generation as in diary?

2020-09-03 Thread Ihor Radchenko
> I didn't know that eval specs support multi-line sexps, but seems that
> works, so I can indeed use this.  Thanks for the suggestion.

Hmm. According to manual, it should support multi-line string. Not sure
about sexps.

In the worst case when you absolutely want multi-line sexp to be really
multi-line and that is also not possible in file-local eval, you can
define your sexp in a named src block. Then, you can execute the named
block using file-local eval statement calling the following function:

(defun org-link-babel-follow (name  return-info)
  "Run src block NAME.
The NAME is parsed as in #+CALL: specification.
The src block should be in the same org file."
  (let* ((call (with-temp-buffer
 (interactive)
 (org-mode)
 (insert "#+CALL: " (format "%s" (org-link-unescape name)) "\n")
 (beginning-of-buffer)
 (org-element-babel-call-parser (point-max) (list (point-min)))
 ))
 (info (org-babel-lob-get-info call)))
(if return-info
info
  (flet ((org-babel-insert-result (result  result-params info hash 
lang) nil))
(org-babel-execute-src-block nil info)

Michael Heerdegen  writes:

> Ihor Radchenko  writes:
>
>> I do not think there is support of multi-line planning everywhere.
>
> I see.
>
>> You can always use file-local definition at the beginning or end of your
>> org file. Below is an example of local definition at the end of an org
>> file.
>>
>> # Local Variables:
>> # eval: (defun your-function () (sexp))
>
> I didn't know that eval specs support multi-line sexps, but seems that
> works, so I can indeed use this.  Thanks for the suggestion.
>
> Michael.



Re: Headline generation as in diary?

2020-09-03 Thread Michael Heerdegen
Ihor Radchenko  writes:

> I do not think there is support of multi-line planning everywhere.

I see.

> You can always use file-local definition at the beginning or end of your
> org file. Below is an example of local definition at the end of an org
> file.
>
> # Local Variables:
> # eval: (defun your-function () (sexp))

I didn't know that eval specs support multi-line sexps, but seems that
works, so I can indeed use this.  Thanks for the suggestion.

Michael.



Re: Headline generation as in diary?

2020-09-03 Thread Ihor Radchenko
> Multi-line sexps in time stamps work would just be nice to have.  Would
> it be hard to achieve?  I mean, since multi-line %%(...) entries already
> work...

The problem is that org-mode assumes that planning line must be a single
line and a lot of internal logic hard-code this assumption.

For example, below is a fragment of org-get-property-block:

 ;; Move point to its position according to its positional rules.
 (cond ((org-before-first-heading-p)
(while (and (org-at-comment-p) (bolp)) (forward-line)))
   (t (forward-line)
  (when (looking-at-p org-planning-line-re) (forward-line

The last line clearly assumes that planning line is always a single
line. The same assumption is made in many other places in org-mode
internals. Though one may write a patch to make multi-line sexps work in
org-mode, there is also a problem with external tools working with org.
I do not think there is support of multi-line planning everywhere.

> I think this is inconvenient.  Ok, only a bit.  But it would be nicer if
> I could specify my dates in only one file.  Jumping around in my org
> file + my emacs init file is what I used to do.

You can always use file-local definition at the beginning or end of your
org file. Below is an example of local definition at the end of an org
file. 

# Local Variables:
# eval: (defun your-function () (sexp))

Best,
Ihor

Michael Heerdegen  writes:

> Ihor Radchenko  writes:
>
>> > When dealing with complicated date rules it can likely happen that a
>> > diary sexp doesn't fit into one line.
>>
>> Diary sexp can be a user-defined function. If your sexp needs to span
>> multiple lines, it is probably worth defining a function and simply
>> using <%%(your-function)> as a timestamp.
>
> I think this is inconvenient.  Ok, only a bit.  But it would be nicer if
> I could specify my dates in only one file.  Jumping around in my org
> file + my emacs init file is what I used to do.
>
> Multi-line sexps in time stamps work would just be nice to have.  Would
> it be hard to achieve?  I mean, since multi-line %%(...) entries already
> work...
>
>
> Thanks,
>
> Michael.



Re: Headline generation as in diary?

2020-09-03 Thread Ihor Radchenko
> It can? Thatʼs not documented, as far as I can tell.

It is literally called "diary sexp". You can use any elisp sexp there,
like (let ...) or (or ...) or (function-name ...)

Robert Pluim  writes:

>> On Thu, 03 Sep 2020 21:35:54 +0800, Ihor Radchenko  
>> said:
>
> >> When dealing with complicated date rules it can likely happen that a
> >> diary sexp doesn't fit into one line.
>
> Ihor> Diary sexp can be a user-defined function. If your sexp needs to 
> span
> Ihor> multiple lines, it is probably worth defining a function and simply
> Ihor> using <%%(your-function)> as a timestamp.
>
> It can? Thatʼs not documented, as far as I can tell.
>
> Robert



Re: Headline generation as in diary?

2020-09-03 Thread Robert Pluim
> On Thu, 03 Sep 2020 16:46:48 +0200, Michael Heerdegen 
>  said:

Michael> Robert Pluim  writes:
>> It can? Thatʼs not documented, as far as I can tell.

Michael> As a user, I read "Diary-style expression entries" in the org 
manual as
Michael> including my own defined diary sexps - excluding them would be a
Michael> surprise that should be documented (no, I don't want that to 
happen!).

Ah, I wasnʼt aware that "diary-style expression entries" and "lisp
expression with %% prepended" were the same thing.

And itʼs not likely to go away: org just runs those expressions
through `eval'.

Robert



Re: Headline generation as in diary?

2020-09-03 Thread Michael Heerdegen
Robert Pluim  writes:

> It can? Thatʼs not documented, as far as I can tell.

As a user, I read "Diary-style expression entries" in the org manual as
including my own defined diary sexps - excluding them would be a
surprise that should be documented (no, I don't want that to happen!).

Michael.



Re: Headline generation as in diary?

2020-09-03 Thread Robert Pluim
> On Thu, 03 Sep 2020 21:35:54 +0800, Ihor Radchenko  
> said:

>> When dealing with complicated date rules it can likely happen that a
>> diary sexp doesn't fit into one line.

Ihor> Diary sexp can be a user-defined function. If your sexp needs to span
Ihor> multiple lines, it is probably worth defining a function and simply
Ihor> using <%%(your-function)> as a timestamp.

It can? Thatʼs not documented, as far as I can tell.

Robert



Re: Headline generation as in diary?

2020-09-03 Thread Michael Heerdegen
Ihor Radchenko  writes:

> > When dealing with complicated date rules it can likely happen that a
> > diary sexp doesn't fit into one line.
>
> Diary sexp can be a user-defined function. If your sexp needs to span
> multiple lines, it is probably worth defining a function and simply
> using <%%(your-function)> as a timestamp.

I think this is inconvenient.  Ok, only a bit.  But it would be nicer if
I could specify my dates in only one file.  Jumping around in my org
file + my emacs init file is what I used to do.

Multi-line sexps in time stamps work would just be nice to have.  Would
it be hard to achieve?  I mean, since multi-line %%(...) entries already
work...


Thanks,

Michael.



Re: Headline generation as in diary?

2020-09-03 Thread Ihor Radchenko
> When dealing with complicated date rules it can likely happen that a
> diary sexp doesn't fit into one line.

Diary sexp can be a user-defined function. If your sexp needs to span
multiple lines, it is probably worth defining a function and simply
using <%%(your-function)> as a timestamp.

Best,
Ihor

Michael Heerdegen  writes:

> Robert Pluim  writes:
>
>> I can push my change to org, but Iʼm not a regular org contributor, so
>> Iʼd prefer to hear from one of the maintainers first.
>
> Sorry - I meant, I see no reason to touch the existing code.  No need to
> change anything for what I want.
>
>> Michael> This doesn't work:
>>
>> Michael> | ** APPT 17:00 Test
>> Michael> | SCHEDULED: <%%(and (or (diary-date 03 09 2020)
>> Michael> |(diary-date 04 09 2020)))>
>
>> I think thatʼs just a consequence of timestamps not being allowed to
>> span multiple lines in org, unlike diary.
>
> This is bad.  Why is that?  Can it be changed?
>
> When dealing with complicated date rules it can likely happen that a
> diary sexp doesn't fit into one line.
>
> Regards,
>
> Michael.



Re: Headline generation as in diary?

2020-09-03 Thread Michael Heerdegen
Robert Pluim  writes:

> I can push my change to org, but Iʼm not a regular org contributor, so
> Iʼd prefer to hear from one of the maintainers first.

Sorry - I meant, I see no reason to touch the existing code.  No need to
change anything for what I want.

> Michael> This doesn't work:
>
> Michael> | ** APPT 17:00 Test
> Michael> | SCHEDULED: <%%(and (or (diary-date 03 09 2020)
> Michael> |(diary-date 04 09 2020)))>

> I think thatʼs just a consequence of timestamps not being allowed to
> span multiple lines in org, unlike diary.

This is bad.  Why is that?  Can it be changed?

When dealing with complicated date rules it can likely happen that a
diary sexp doesn't fit into one line.

Regards,

Michael.



Re: Headline generation as in diary?

2020-09-02 Thread Michael Heerdegen
Robert Pluim  writes:

> OK. Thatʼs as far as my org-hacking knowledge goes, so perhaps someone
> else here has an idea of the right way to invoke "tell me what heading
> Iʼm in, as a string".

The situation is actually like this: the empty string issue doesn't
happen with time stamps <%%(...)>, this always uses the correct
headline.  Seems %%() entries are expected to return a string OTOH since
the headline seems to be ignored.  I guess this is reasonable.  Dunno if
there is something to change here at all in the code.

Ok, the other thing: broken time stamps with line breaks:

> You've lost me. Can you show me an example?

The difference here is only a line break:

This doesn't work:

| ** APPT 17:00 Test
| SCHEDULED: <%%(and (or (diary-date 03 09 2020)
|(diary-date 04 09 2020)))>

But this works:

| ** APPT 17:00 Test
| SCHEDULED: <%%(and (or (diary-date 03 09 2020) (diary-date 04 09 2020)))>


Michael.



Re: Headline generation as in diary?

2020-09-02 Thread Robert Pluim
> On Wed, 02 Sep 2020 15:45:36 +0200, Michael Heerdegen 
>  said:

Michael> Robert Pluim  writes:
>> How about:
>> 
>> diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
>> index 78fe13303..9049b3a42 100644
>> --- a/lisp/org-agenda.el
>> +++ b/lisp/org-agenda.el
>> @@ -5772,7 +5772,7 @@ displayed in agenda view."
>> r (replace-match "" nil nil r)))
>> (if (string-match "\\S-" r)
>> (setq txt r)
>> -  (setq txt "SEXP entry returned empty string"))
>> +  (setq txt (org-no-properties (org-get-heading t t t t
>> (setq txt (org-agenda-format-item extra txt level category tags 'time))
>> (org-add-props txt props 'org-marker marker
>> 'date date 'todo-state todo-state

Michael> Thanks for looking at this.

Michael> Ok - That gives me a headline in the agenda, but a wrong one (more 
or
Michael> less, a random headline in the same file, at a seemingly unrelated
Michael> location).

OK. Thatʼs as far as my org-hacking knowledge goes, so perhaps someone
else here has an idea of the right way to invoke "tell me what heading
Iʼm in, as a string".

>> >> **  Just before midnight on a few days 23:00-24:00
>> >> <%%(org-block 2020 8 31 2020 9 2)>
>> >> <%%(org-block 2020 9 10 2020 9 12)>
>> 
Michael> But it seems those time stamps are not allowed to span
Michael> multiple lines
Michael> (at least I don't get it work).  Seems org doesn't
Michael> recognize them as
Michael> such?
>> 
>> Itʼs working fine for me in org-9.3.6. Note that I have my default
>> agenda view set to a fortnight, and those dates span two different
>> weeks.

Michael> I didn't mean time spans.  What doesn't work for me are time 
_stamps_
Michael> that span multiple text lines.

You've lost me. Can you show me an example?

Robert



Re: Headline generation as in diary?

2020-09-02 Thread Michael Heerdegen
Robert Pluim  writes:

> How about:
>
> diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
> index 78fe13303..9049b3a42 100644
> --- a/lisp/org-agenda.el
> +++ b/lisp/org-agenda.el
> @@ -5772,7 +5772,7 @@ displayed in agenda view."
>   r (replace-match "" nil nil r)))
>   (if (string-match "\\S-" r)
>   (setq txt r)
> -   (setq txt "SEXP entry returned empty string"))
> +   (setq txt (org-no-properties (org-get-heading t t t t
>   (setq txt (org-agenda-format-item extra txt level category tags 
> 'time))
>   (org-add-props txt props 'org-marker marker
>  'date date 'todo-state todo-state

Thanks for looking at this.

Ok - That gives me a headline in the agenda, but a wrong one (more or
less, a random headline in the same file, at a seemingly unrelated
location).

> (Iʼm guessing yet-another-config-variable is unnecessary)

I think so toom yes.

> >> You can use them in timestamps as well, which is useful for
> >> weird time
> >> periods
> >> 
> >> **  Just before midnight on a few days 23:00-24:00
> >> <%%(org-block 2020 8 31 2020 9 2)>
> >> <%%(org-block 2020 9 10 2020 9 12)>
>
> Michael> But it seems those time stamps are not allowed to span
> Michael> multiple lines
> Michael> (at least I don't get it work).  Seems org doesn't
> Michael> recognize them as
> Michael> such?
>
> Itʼs working fine for me in org-9.3.6. Note that I have my default
> agenda view set to a fortnight, and those dates span two different
> weeks.

I didn't mean time spans.  What doesn't work for me are time _stamps_
that span multiple text lines.


Thanks,

Michael.



Re: Headline generation as in diary?

2020-09-02 Thread Robert Pluim
> On Tue, 01 Sep 2020 23:56:55 +0200, Michael Heerdegen 
>  said:

Michael> Robert Pluim  writes:
>> Itʼs triggered by the %%(, I believe, but not in headlines.

Michael> Yes, I see that in the code.

Michael> But actually I seem to have the opposite problem: I can't make the
Michael> original headline appear in the agenda.  When the sexp doesn't 
return a
Michael> string the agenda will contain a line saying "SEXP entry returned 
empty
Michael> string" instead of showing the headline.

It seems `org-anniversary' supports %d for year and thatʼs about it,
which is not surprising, since itʼs just a wrapper around
`diary-anniversary'.

How about:

diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index 78fe13303..9049b3a42 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -5772,7 +5772,7 @@ displayed in agenda view."
r (replace-match "" nil nil r)))
(if (string-match "\\S-" r)
(setq txt r)
- (setq txt "SEXP entry returned empty string"))
+ (setq txt (org-no-properties (org-get-heading t t t t
(setq txt (org-agenda-format-item extra txt level category tags 
'time))
(org-add-props txt props 'org-marker marker
   'date date 'todo-state todo-state

(Iʼm guessing yet-another-config-variable is unnecessary)

>> You can use them in timestamps as well, which is useful for weird time
>> periods
>> 
>> **  Just before midnight on a few days 23:00-24:00
>> <%%(org-block 2020 8 31 2020 9 2)>
>> <%%(org-block 2020 9 10 2020 9 12)>

Michael> But it seems those time stamps are not allowed to span multiple 
lines
Michael> (at least I don't get it work).  Seems org doesn't recognize them 
as
Michael> such?

Itʼs working fine for me in org-9.3.6. Note that I have my default
agenda view set to a fortnight, and those dates span two different
weeks.

Robert



Re: Headline generation as in diary?

2020-09-01 Thread Michael Heerdegen
Robert Pluim  writes:

> Itʼs triggered by the %%(, I believe, but not in headlines.

Yes, I see that in the code.

But actually I seem to have the opposite problem: I can't make the
original headline appear in the agenda.  When the sexp doesn't return a
string the agenda will contain a line saying "SEXP entry returned empty
string" instead of showing the headline.

> You can use them in timestamps as well, which is useful for weird time
> periods
>
> **  Just before midnight on a few days 23:00-24:00
> <%%(org-block 2020 8 31 2020 9 2)>
> <%%(org-block 2020 9 10 2020 9 12)>

But it seems those time stamps are not allowed to span multiple lines
(at least I don't get it work).  Seems org doesn't recognize them as
such?


Michael.



Re: Headline generation as in diary?

2020-09-01 Thread Robert Horn


Eric S Fraga writes:

> On Tuesday,  1 Sep 2020 at 18:10, Robert Pluim wrote:
> * Birthdays
>
> and then a number of %%(diary-anniversary ...) entries all under this
> headline.
>

Note that you can also use %%(org-anniversary ...) with slightly
different dependencies.

I also use modifications of diary to insert sunrise and sunset.

But I think that these %% functiotns only show up as part of agenda processing.

-- 
Robert Horn
rjh...@alum.mit.edu



Re: Headline generation as in diary?

2020-09-01 Thread Eric S Fraga
On Tuesday,  1 Sep 2020 at 18:10, Robert Pluim wrote:
>> On Tue, 01 Sep 2020 17:18:42 +0200, Michael Heerdegen 
>>  said:
> Michael> Where in an entry do you specify such specifications?
>
> Below the headline, after the properties.

Yes, exactly.  I have one headline which says something along the lines
of:

* Birthdays

and then a number of %%(diary-anniversary ...) entries all under this
headline.

I could have done these with a +1y repeat but, like Michael, like the %d
capability of diary entries.

I use org-recur for most other repeat items and
org-clone-subtree-with-time-shift for setting up repeats with end dates
(e.g. lectures in a term).

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.3.7-725-g7bc18e



Re: Headline generation as in diary?

2020-09-01 Thread Robert Pluim
> On Tue, 01 Sep 2020 17:18:42 +0200, Michael Heerdegen 
>  said:

Michael> Eric S Fraga  writes:
>> No, not necessarily.  I have entries like this:
>> 
>> %%(diary-anniversary 1981 03 17) Somebody's birthday (%d years)
>> 
>> and the agenda view shows "Somebody's birthday (19 years)"; the actual
>> heading for this entry is ignored.

Michael> Where in an entry do you specify such specifications?

Below the headline, after the properties.

>> However, I don't know if other diary- functions work the same way.

They do, although there are org- versions of most (all?) of them that
you should use, since they consistently use ISO8601 date order,
unlike the diary functions. The manual is a bit lacking in this area,
I think.

Michael> Yes, the interesting part then is: when does org behave like this, 
and
Michael> can this behavior be forced?

Itʼs triggered by the %%(, I believe, but not in headlines. You can
use them in timestamps as well, which is useful for weird time periods

**  Just before midnight on a few days 23:00-24:00
<%%(org-block 2020 8 31 2020 9 2)>
<%%(org-block 2020 9 10 2020 9 12)>

Robert



Re: Headline generation as in diary?

2020-09-01 Thread Michael Heerdegen
Michael Heerdegen  writes:

> > [1]  https://github.com/m-cat/org-recur
>
> Thanks for the hint, I'll have a look.

Nice to have definitely.

I want to stick to diary sexp expressions for some complicated dates.  I
have already extended the diary language to fit my needs to specify the
dates of garbage collection here.  That looks like this:

#+begin_src emacs-lisp
 (dg-let-date (dg-tomorrow)
   (dg-except-for*
(and (dg-friday-p) (cl-evenp (dg-iso-week)))
(cl-some #'dg-holiday-p (dg-the-days-between (dg-days-before 2)))
+1))
#+end_src

It means: warn me one day before garbage collection happens, and that
is: every Friday in any week with even week number, unless it has been a
holiday up to two days before that, then they collect one day later.

I guess I always want Lisp to specify something like this.

Michael.



Re: Headline generation as in diary?

2020-09-01 Thread Michael Heerdegen
Eric S Fraga  writes:

> No, not necessarily.  I have entries like this:
>
> %%(diary-anniversary 1981 03 17) Somebody's birthday (%d years)
>
> and the agenda view shows "Somebody's birthday (19 years)"; the actual
> heading for this entry is ignored.

Where in an entry do you specify such specifications?

> However, I don't know if other diary- functions work the same way.

Yes, the interesting part then is: when does org behave like this, and
can this behavior be forced?

> > Or do I miss something?  How to other people deal with things
> > like...garbage collection?
>
> I use the org-recur [1] package for this.  Very nice for repeating
> events.

> Footnotes:
> [1]  https://github.com/m-cat/org-recur

Thanks for the hint, I'll have a look.


Michael.



Re: Headline generation as in diary?

2020-09-01 Thread Eric S Fraga
On Tuesday,  1 Sep 2020 at 16:34, Michael Heerdegen wrote:
> I would like to use my org file to specify those things instead of
> maintaining a second file (the diary file).  But AFAIK the dynamically
> created headline part is tricky: what appears in the agenda is always
> the org headline as it appears in the file, right?

No, not necessarily.  I have entries like this:

%%(diary-anniversary 1981 03 17) Somebody's birthday (%d years)

and the agenda view shows "Somebody's birthday (19 years)"; the actual
heading for this entry is ignored.  In fact, I have a large number of
such entries all in the same headline.

However, I don't know if other diary- functions work the same way.

> Or do I miss something?  How to other people deal with things
> like...garbage collection?

I use the org-recur [1] package for this.  Very nice for repeating
events.

HTH,
eric


Footnotes:
[1]  https://github.com/m-cat/org-recur

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.3.7-725-g7bc18e