unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / 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 18:32:52 +0100	[thread overview]
Message-ID: <87bmj6cwu3.fsf@web.de> (raw)
In-Reply-To: <87k1xuod9x.fsf@gmail.com> (Narendra Joshi's message of "Sun, 10 Dec 2017 20:12:18 +0530")

Narendra Joshi <narendraj9@gmail.com> writes:

> Can you share an example please? :)

If you want to go with anonymous functions, `letrec' is perfect here
(`cl-labels' would work as well and avoid the need to `funcall'):

#+begin_src emacs-lisp
;; -*- lexical-binding: t -*-

(defun my-do-when-idle (f g interval)
  "Call F when idle for INTERVAL seconds and then G when there is activity."
  (letrec ((run-timer-fun (lambda () (run-with-idle-timer
                                 interval nil
                                 (lambda ()
                                   (funcall f)
                                   (add-hook 'post-command-hook activity-fun)))))
           (activity-fun (lambda ()
                           (remove-hook 'post-command-hook activity-fun)
                           (funcall g)
                           (funcall run-timer-fun))))
    (funcall run-timer-fun)))
#+end_src

I've changed the order in the `activity-fun' to run `remove-hook' first,
so that you get a sane behavior when running G gives an error.

A short test:

#+begin_src emacs-lisp
(my-do-when-idle (lambda () (message "%s" (current-time-string)))
                 (lambda () (message "Stopping timer"))
                 5)
#+end_src


Michael.




  reply	other threads:[~2017-12-10 17:32 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
2017-12-10 14:42   ` Narendra Joshi
2017-12-10 17:32     ` Michael Heerdegen [this message]
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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87bmj6cwu3.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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).