From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.help Subject: Re: Using variable names in a macro Date: Sun, 10 Dec 2017 12:28:37 +0100 Message-ID: <874loyx1ne.fsf@web.de> References: <87r2s3aoks.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1512905349 25342 195.159.176.226 (10 Dec 2017 11:29:09 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 10 Dec 2017 11:29:09 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: help-gnu-emacs To: Narendra Joshi Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Dec 10 12:29:06 2017 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eNzn7-0006PK-Pn for geh-help-gnu-emacs@m.gmane.org; Sun, 10 Dec 2017 12:29:06 +0100 Original-Received: from localhost ([::1]:44221 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNznF-0000iF-41 for geh-help-gnu-emacs@m.gmane.org; Sun, 10 Dec 2017 06:29:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38053) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNzmq-0000hx-94 for help-gnu-emacs@gnu.org; Sun, 10 Dec 2017 06:28:49 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eNzmj-0000hK-PB for help-gnu-emacs@gnu.org; Sun, 10 Dec 2017 06:28:48 -0500 Original-Received: from mout.web.de ([212.227.15.4]:55745) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eNzmj-0000bU-FG for help-gnu-emacs@gnu.org; Sun, 10 Dec 2017 06:28:41 -0500 Original-Received: from drachen.dragon ([92.74.165.17]) by smtp.web.de (mrweb002 [213.165.67.108]) with ESMTPSA (Nemesis) id 0LvB6w-1f6mV53Amo-010PHZ; Sun, 10 Dec 2017 12:28:38 +0100 In-Reply-To: <87r2s3aoks.fsf@gmail.com> (Narendra Joshi's message of "Sat, 09 Dec 2017 21:19:39 +0530") X-Provags-ID: V03:K0:pnfhWSZX5wowb3JqfRIXrjcrU8KMCfLgWB5YVl3IJRLXH2jtTrE hXiCg3czhzfiS5huca2XMFAEPDy1fhXWL6Qpt0BK38iMDCSFKOVl2zl7/sQuRA0gbjduN6h LMh2ltqrj7gMvSTcMmLMChgyZlNL3/ro+PO7it0NsGMBxvap+keHQS16+MarlzpnMxipcd8 nniz+FgNGVDhYdGhtU9rA== X-UI-Out-Filterresults: notjunk:1;V01:K0:w6WB8AwoAvM=:ngu2g2q6EIJsgU2JayDNun 2dN1ruNdCFr2JZDgWorwVa+RqtRc9rvjS31iMzKfEyP5TA0hq55NWXpIvom78xRyweMGXg6KC b8YCN9yYEU1TQBF9b+oVsqU1XgicsizNbt1QBHD6MS1zmk34rdoPVV418TwLtqoHtYrFNsZdh OsN6Co6XZxXHSDFqu/aJ7rhdSK9vfuBjXS/igU6uRMaHpQWbS18b6bIquvEjJkGe1+v/rWFjU sl1W+nT1aq0ufyZ9e4ImsLOtCm7HtvOckM/9e2IQKjK/z2NybaT+cu6sCcslLhp5TxdSMtBrh s6/WLGi2lQ8SkTw4heUvkZmLscxL+95D1i6MpSPr1ykdzCEtjcV3w+vsbypsaIJvpNYEDWWpz gX/nAv61jHKYcDppq4zfARXSuso+/eXmL5R0NjNKER/LzEiqn+QeXR9KjuDsX+FLXXbF+NGUP OI8eWrA3zIfaGrO9EANKwbTwU8ax+KhPb3sWDy2DnroD96+VCQsMqcV82avi2EYs6bxjixPDz fCTHEPuNoqdIgdveg7NHztJ5LtUE/vjczfgTDYftJUN3WnHgWBTT6U1tFwghouw9TgN6zWAed 2svjHvpxHk4LQZ4Cc0iyEw5Rb0RfqJ5nG+8f+hAcdQyxikmnb7y/TZ9UwLa3EgSknLakyP2pv i4W/ROgVPfd4bgvjllHnW/xNMtPhms2Wy2LNus9WOAV5haMyJdrIobtx2I/VwU65drmF59kei IPgCcVRNxzVKO6vIaca9YDtVGMGLs0UIsaAVEzQgH42kQl+iT7JygyeExI7ebIrFwAu75vme X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.227.15.4 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:115323 Archived-At: Narendra Joshi 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.