emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] dynamic date arithmetic in a macro or otherwise (simulating a "date


From: Matt Price
Subject: [O] dynamic date arithmetic in a macro or otherwise (simulating a "date counter")?
Date: Tue, 22 Aug 2017 19:09:38 -0400



On Thu, Aug 17, 2017 at 3:25 PM, Matt Price <address@hidden> 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

Attachment: 0001-org-macro-Implement-the-n-macro.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]