From mboxrd@z Thu Jan 1 00:00:00 1970 From: Adam Porter Subject: Re: FYI: with-org-today-date macro, helps with testing Date: Sat, 29 Jul 2017 14:44:48 -0500 Message-ID: <877eyrvxhr.fsf@alphapapa.net> References: <87fudfepqo.fsf@alphapapa.net> <87lgn7palq.fsf@kyleam.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:43226) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dbXft-0007Hj-1p for emacs-orgmode@gnu.org; Sat, 29 Jul 2017 15:45:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dbXfp-0001rr-LV for emacs-orgmode@gnu.org; Sat, 29 Jul 2017 15:45:21 -0400 Received: from [195.159.176.226] (port=58007 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dbXfp-0001qG-Ek for emacs-orgmode@gnu.org; Sat, 29 Jul 2017 15:45:17 -0400 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1dbXfc-0000oc-N9 for emacs-orgmode@gnu.org; Sat, 29 Jul 2017 21:45:04 +0200 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: emacs-orgmode@gnu.org Kyle Meyer writes: > Adam Porter 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.