From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sacha Chua Subject: Re: dynamic date arithmetic in a macro or otherwise (simulating a "date counter")? Date: Tue, 22 Aug 2017 20:14:11 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a113cd8788331470557609725" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:47436) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dkJJJ-0008V9-NK for emacs-orgmode@gnu.org; Tue, 22 Aug 2017 20:14:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dkJJI-0004r0-81 for emacs-orgmode@gnu.org; Tue, 22 Aug 2017 20:14:17 -0400 Received: from mail-oi0-x22b.google.com ([2607:f8b0:4003:c06::22b]:33953) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dkJJH-0004qN-Vw for emacs-orgmode@gnu.org; Tue, 22 Aug 2017 20:14:16 -0400 Received: by mail-oi0-x22b.google.com with SMTP id j144so2573572oib.1 for ; Tue, 22 Aug 2017 17:14:14 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Matt Price Cc: emacs list --001a113cd8788331470557609725 Content-Type: text/plain; charset="UTF-8" org-clone-subtree-with-time-shift might be a good starting point, too. It could be interesting to be able to replace dates within text and priorities. Good luck! On Aug 22, 2017 7:10 PM, "Matt Price" wrote: > > > On Thu, Aug 17, 2017 at 3:25 PM, Matt Price wrote: > >> I'd love to be able generate dates dynamically using the {{{n}}} org >> macro, or some other mechanism. I don't immediately see how that would be >> possible but maybe someone can guide me. I'd want to do something >> equivalent to this pseudo-elisp: >> >> (let ((base-date 2017-09-05)) >> (+ base-date (* 7 {{{n}}})) >> >> I haven't looked into how date objects are parsed in org-mode, though, so >> I have no idea how hard it would be to actually implement something like >> this. >> > > I made some progress on this (but not much). > > I defined a simple macro that adds times together and creates a timestamp > that org can read -- in fact, this one adds some text as well: > > #+MACRO: w (eval (format-time-string "Week {{{n(week)}}} (<%Y-%m-%d %a>)" > (time-add (encode-time 0 0 0 17 9 2017) (days-to-time (* 7 1)))) ) > > This does everything that I want *except* dynamically adding time to the > previous macro call. I tried defining a dynamic version: > > #+MACRO: w (eval (format-time-string "Week {{{n(week)}}} (<%Y-%m-%d %a>)" > (time-add (encode-time 0 0 0 17 9 2017) (days-to-time (* 7 $1)))) )) > > And then calling it with > > {{{w({{{n}}})}}} > > But unsurprisingly and appropriately, that didn't work. > > I also took a look at the patch Nicolas used to implement the {{{n}}} > macro (I've reattached it here for convenience only!). It defines > `org-macro--counter-initialize` and calls it from inside > `org-macro-templates-initialize`. I guess I could copy that strategy but > then I'd be maintaining my own copy of org-macro-templates-initialize, > which seems like a terrible idea. > > So, I'm not sure how best to proceed. For my specific use-case, something > like this would be a huge timesaver when multiplied over semesters/years. > But I also wonder whether maybe other people would enjoy being able to do > date arithmetic inside org files (and outside of tables -- I know from > Sacha's 2015 blog post that this is possible inside a table -- > http://sachachua.com/blog/2015/06/using-your-own-emacs- > lisp-functions-in-org-mode-table-calculations-easier-dosage-totals/). If > other people would also use such code, I could try to hack something > together for submission. Generalizable functions will be hard for me to > write because I am sometimes a bit dense. I would love to hear suggestions > from the group... > > >> Thank you everyone! >> > > ^^ this still applies! > Matt > > --001a113cd8788331470557609725 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
org-clone-subtree-with-time-shift might be a good startin= g point, too. It could be interesting to be able to replace dates within te= xt and priorities. Good luck!

On Aug 22, 2017 7:10 PM, "Matt Price" <moptop99@gmail.com> wrote:


On Thu, Aug 17, 2017 at= 3:25 PM, Matt Price <moptop99@gmail.com> wrote:
I'd lo= ve to be able generate dates dynamically using the {{{n}}} org macro, or so= me other mechanism.=C2=A0 I don't immediately see how that would be pos= sible but maybe someone can guide me.=C2=A0 I'd want to do something eq= uivalent to this pseudo-elisp:

(let ((base-date 20= 17-09-05))
=C2=A0 (+ base-date (* 7 {{{n}}}))

I haven't looked into how date objects are parsed in org-mode, th= ough, so I have no idea how hard it would be to actually implement somethin= g like this.=C2=A0=C2=A0

I made= some progress on this (but not much).=C2=A0

= I defined a simple macro that adds times together and creates a timestamp t= hat org can read -- in fact, this one adds some text as well:

#+MACRO: w (eval (format-time-string "Week {{{n(week)= }}} (<%Y-%m-%d %a>)" (time-add (encode-time 0 0 0 17 9 2017) (da= ys-to-time (* 7 1)))) )

This does everything t= hat I want *except* dynamically adding time to the previous macro call.=C2= =A0 I tried defining a dynamic version:

#+MACRO: w= (eval (format-time-string "Week {{{n(week)}}} (<%Y-%m-%d %a>)&q= uot; (time-add (encode-time 0 0 0 17 9 2017) (days-to-time (* 7 $1)))) ))

And then calling it with

<= div>{{{w({{{n}}})}}}

But unsurprisingly and approp= riately, that didn't work.

I also took a l= ook at the patch Nicolas used to implement the {{{n}}} macro (I've reat= tached it here for convenience only!).=C2=A0 It defines `org-macro--counter= -initialize` and calls it from inside `org-macro-templates-initia= lize`.=C2=A0 I guess I could copy that strategy but then I'd be maintai= ning my own copy of org-macro-templates-initialize, which seems like a= terrible idea.=C2=A0

So, I'm not sure ho= w best to proceed. For my specific use-case, something like this would be a= huge timesaver when multiplied over semesters/years. But I also wonder whe= ther maybe other people would enjoy being able to do date arithmetic inside= org files (and outside of tables -- I know from Sacha's 2015 blog post= =C2=A0 that this is possible inside a table -- http://sachachua.com/blog= /2015/06/using-your-own-emacs-lisp-functions-in-org-mode-tab= le-calculations-easier-dosage-totals/).=C2=A0 If other people woul= d also use such code, I could try to hack something together for submission= . Generalizable functions will be hard for me to write because I am sometim= es a bit dense. I would love to hear suggestions from the group...


Thank you everyone!

^^ this still applies!
Matt
=
--001a113cd8788331470557609725--