emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* FYI: with-org-today-date macro, helps with testing
@ 2017-07-29  6:11 Adam Porter
  2017-07-29 14:43 ` Kyle Meyer
  0 siblings, 1 reply; 6+ messages in thread
From: Adam Porter @ 2017-07-29  6:11 UTC (permalink / raw)
  To: emacs-orgmode

Hi friends,

Just wanted to drop this here.  I was testing some agenda-related code,
and got tired of having to keep moving the dates forward on my test data
as days passed in real life, so after digging into the agenda code, I
came up with this macro that makes testing much easier:

#+BEGIN_SRC elisp
  (defmacro with-org-today-date (date &rest body)
    "Run BODY with the `org-today' function set to return simply DATE.
  DATE should be a date-time string (both date and time must be included)."
    (declare (indent defun))
    `(let ((day (date-to-day ,date))
           (orig (symbol-function 'org-today)))
       (unwind-protect
           (progn
             (fset 'org-today (lambda () day))
             ,@body)
         (fset 'org-today orig))))
#+END_SRC

You can use it like this:

#+BEGIN_SRC elisp
  (with-org-today-date "2017-07-05 00:00"
    (let ((org-agenda-files (list "~/test.org"))
          (org-agenda-span 'day))
      (org-agenda-list nil)))
#+END_SRC

Hope someone finds this useful.

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

* Re: FYI: with-org-today-date macro, helps with testing
  2017-07-29  6:11 FYI: with-org-today-date macro, helps with testing Adam Porter
@ 2017-07-29 14:43 ` Kyle Meyer
  2017-07-29 19:44   ` Adam Porter
  0 siblings, 1 reply; 6+ messages in thread
From: Kyle Meyer @ 2017-07-29 14:43 UTC (permalink / raw)
  To: Adam Porter, emacs-orgmode

Adam Porter <adam@alphapapa.net> writes:

> #+BEGIN_SRC elisp
>   (defmacro with-org-today-date (date &rest body)
>     "Run BODY with the `org-today' function set to return simply DATE.
>   DATE should be a date-time string (both date and time must be included)."
>     (declare (indent defun))
>     `(let ((day (date-to-day ,date))
>            (orig (symbol-function 'org-today)))
>        (unwind-protect
>            (progn
>              (fset 'org-today (lambda () day))
>              ,@body)
>          (fset 'org-today orig))))
> #+END_SRC

You should be able to simplify the macro body to

    `(cl-letf (((symbol-function 'org-today) (lambda () (date-to-day ,date))))
       ,@body)
-- 
Kyle

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

* Re: FYI: with-org-today-date macro, helps with testing
  2017-07-29 14:43 ` Kyle Meyer
@ 2017-07-29 19:44   ` Adam Porter
  2017-07-29 20:26     ` Kyle Meyer
  0 siblings, 1 reply; 6+ messages in thread
From: Adam Porter @ 2017-07-29 19:44 UTC (permalink / raw)
  To: emacs-orgmode

Kyle Meyer <kyle@kyleam.com> writes:

> Adam Porter <adam@alphapapa.net> writes:
>
>> #+BEGIN_SRC elisp
>>   (defmacro with-org-today-date (date &rest body)
>>     "Run BODY with the `org-today' function set to return simply DATE.
>>   DATE should be a date-time string (both date and time must be included)."
>>     (declare (indent defun))
>>     `(let ((day (date-to-day ,date))
>>            (orig (symbol-function 'org-today)))
>>        (unwind-protect
>>            (progn
>>              (fset 'org-today (lambda () day))
>>              ,@body)
>>          (fset 'org-today orig))))
>> #+END_SRC
>
> You should be able to simplify the macro body to
>
>     `(cl-letf (((symbol-function 'org-today) (lambda () (date-to-day ,date))))
>        ,@body)

Hi Kyle,

I tried that, but it didn't work, so I had to come up with this uglier
one.  I couldn't figure out why, but with cl-letf, the redefined
function didn't seem to persist deeper in the tree of function calls,
only to the first level.  Maybe I did something wrong, but all I know
now is that at least this works.  :)

Thanks.

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

* Re: FYI: with-org-today-date macro, helps with testing
  2017-07-29 19:44   ` Adam Porter
@ 2017-07-29 20:26     ` Kyle Meyer
  2017-07-29 23:03       ` Adam Porter
  0 siblings, 1 reply; 6+ messages in thread
From: Kyle Meyer @ 2017-07-29 20:26 UTC (permalink / raw)
  To: Adam Porter, emacs-orgmode

Adam Porter <adam@alphapapa.net> writes:

> Kyle Meyer <kyle@kyleam.com> writes:

[...]

>> You should be able to simplify the macro body to
>>
>>     `(cl-letf (((symbol-function 'org-today) (lambda () (date-to-day ,date))))
>>        ,@body)
>
> Hi Kyle,
>
> I tried that, but it didn't work, so I had to come up with this uglier
> one.  I couldn't figure out why, but with cl-letf, the redefined
> function didn't seem to persist deeper in the tree of function calls,
> only to the first level.  Maybe I did something wrong, but all I know
> now is that at least this works.  :)

On my end,

  (defmacro with-org-today-date (date &rest body)
    `(cl-letf (((symbol-function 'org-today) (lambda () (date-to-day ,date))))
       ,@body))
  
  (with-org-today-date "2017-07-05 00:00"
    (let ((org-agenda-files (list "~/test.org"))
          (org-agenda-span 'day))
      (org-agenda-list nil)))

produces the same result as your original macro.

-- 
Kyle

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

* Re: FYI: with-org-today-date macro, helps with testing
  2017-07-29 20:26     ` Kyle Meyer
@ 2017-07-29 23:03       ` Adam Porter
  2018-10-23 20:56         ` Samuel Wales
  0 siblings, 1 reply; 6+ messages in thread
From: Adam Porter @ 2017-07-29 23:03 UTC (permalink / raw)
  To: emacs-orgmode

Thanks, maybe I was up too late to be messing with macros.  :)

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

* Re: FYI: with-org-today-date macro, helps with testing
  2017-07-29 23:03       ` Adam Porter
@ 2018-10-23 20:56         ` Samuel Wales
  0 siblings, 0 replies; 6+ messages in thread
From: Samuel Wales @ 2018-10-23 20:56 UTC (permalink / raw)
  To: Adam Porter; +Cc: emacs-orgmode

i duly saved this macro, didn't think to look for it, and used datefudge(1).

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

end of thread, other threads:[~2018-10-23 20:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-29  6:11 FYI: with-org-today-date macro, helps with testing Adam Porter
2017-07-29 14:43 ` Kyle Meyer
2017-07-29 19:44   ` Adam Porter
2017-07-29 20:26     ` Kyle Meyer
2017-07-29 23:03       ` Adam Porter
2018-10-23 20:56         ` Samuel Wales

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).