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

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

Re: Using variable names in a macro


From: Michael Heerdegen
Subject: Re: Using variable names in a macro
Date: Sun, 10 Dec 2017 12:28:37 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Narendra Joshi <narendraj9@gmail.com> writes:

> ```
> (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)))))))))
> ```

Hmm, with some trivial simplifications of the list building, this could
look like:

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

It would be better, though, to replace the run-idle-timer-form from with
a lambda, to avoid the code duplication.

Actually, `do-when-idle' doesn't even have to be a macro.  You can make
it a defun when you define the NAME with defalias.  Bind it to a closure
so that you can refer to all of the stuff with variables bound around
it.


Michael.



reply via email to

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