all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: Narendra Joshi <narendraj9@gmail.com>
Cc: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: Using variable names in a macro
Date: Sun, 10 Dec 2017 12:28:37 +0100	[thread overview]
Message-ID: <874loyx1ne.fsf@web.de> (raw)
In-Reply-To: <87r2s3aoks.fsf@gmail.com> (Narendra Joshi's message of "Sat, 09 Dec 2017 21:19:39 +0530")

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	other threads:[~2017-12-10 11:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-09 15:49 Using variable names in a macro Narendra Joshi
2017-12-10 11:28 ` Michael Heerdegen [this message]
2017-12-10 14:42   ` Narendra Joshi
2017-12-10 17:32     ` Michael Heerdegen
2017-12-11 14:07       ` Narendra Joshi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874loyx1ne.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=help-gnu-emacs@gnu.org \
    --cc=narendraj9@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.