help-gnu-emacs
[Top][All Lists]
Advanced

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

Using variable names in a macro


From: Narendra Joshi
Subject: Using variable names in a macro
Date: Sat, 09 Dec 2017 21:19:39 +0530

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



reply via email to

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