all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Using variable names in a macro
@ 2017-12-09 15:49 Narendra Joshi
  2017-12-10 11:28 ` Michael Heerdegen
  0 siblings, 1 reply; 5+ messages in thread
From: Narendra Joshi @ 2017-12-09 15:49 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

I want to refactor the following macro. I use it to do things at times
when I am idle for some time and when I come back to the keyboard
eventually. For example, showing bitcoin prices when I have been idle
for some time but removing them from the mode line once I am back as it
takes a lot of space. I think there is a lot of repetition but I am not
sure how to abstract things out into variables. It would have been
simpler if an anonymous function could refer itself.

```
(defmacro do-when-idle (f g interval)
  "Call F when idle for INTERVAL seconds and then G when there is activity."
  `(progn
     (defun ,(intern (concat "do-when-idle-"
                             (sha1 (format "%s-%s" f g)))) ()
       (funcall ,g)
       (remove-hook 'post-command-hook
                    #',(intern (concat "do-when-idle-"
                                       (sha1 (format "%s-%s" f g)))))
       (run-with-idle-timer ,interval
                            nil
                            (lambda ()
                              (funcall ,f)
                              (add-hook 'post-command-hook
                                        #',(intern (concat "do-when-idle-"
                                                           (sha1 (format "%s-%s" f g))))))))
     (run-with-idle-timer ,interval
                          nil
                          (lambda ()
                            (funcall ,f)
                            (add-hook 'post-command-hook
                                      #',(intern (concat "do-when-idle-"
                                                         (sha1 (format "%s-%s" f g)))))))))
```

Best,
-- 
Narendra Joshi



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

end of thread, other threads:[~2017-12-11 14:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-09 15:49 Using variable names in a macro Narendra Joshi
2017-12-10 11:28 ` Michael Heerdegen
2017-12-10 14:42   ` Narendra Joshi
2017-12-10 17:32     ` Michael Heerdegen
2017-12-11 14:07       ` Narendra Joshi

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.