[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 18:32:52 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) |
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.