From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Narendra Joshi Newsgroups: gmane.emacs.help Subject: Re: Using variable names in a macro Date: Sun, 10 Dec 2017 20:12:18 +0530 Message-ID: <87k1xuod9x.fsf@gmail.com> References: <87r2s3aoks.fsf@gmail.com> <874loyx1ne.fsf@web.de> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1512916980 30433 195.159.176.226 (10 Dec 2017 14:43:00 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 10 Dec 2017 14:43:00 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.90 (gnu/linux) Cc: help-gnu-emacs To: Michael Heerdegen Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Dec 10 15:42:55 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 1eO2og-0007eU-W9 for geh-help-gnu-emacs@m.gmane.org; Sun, 10 Dec 2017 15:42:55 +0100 Original-Received: from localhost ([::1]:44896 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eO2oo-0001Gw-3f for geh-help-gnu-emacs@m.gmane.org; Sun, 10 Dec 2017 09:43:02 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52846) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eO2oJ-0001EX-6R for help-gnu-emacs@gnu.org; Sun, 10 Dec 2017 09:42:32 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eO2oE-0003PG-9n for help-gnu-emacs@gnu.org; Sun, 10 Dec 2017 09:42:31 -0500 Original-Received: from mail-pl0-x234.google.com ([2607:f8b0:400e:c01::234]:34871) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eO2oE-0003OX-3Z for help-gnu-emacs@gnu.org; Sun, 10 Dec 2017 09:42:26 -0500 Original-Received: by mail-pl0-x234.google.com with SMTP id b96so3071431pli.2 for ; Sun, 10 Dec 2017 06:42:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=eTrM7Hq4hpCqZJJSCYjY2SHXp05pM9pWeQHvPaxuFvw=; b=F3hhgC2RFCQVDY3DnZPmVCrW17GoYPXWx1hrDyaCMPdGxv8E0plWrTmlCEMVz1HAO2 fO553RZJEwMLqm4frnRc09uHBfdJ37gav1W8YzjZM+YGaqt5IGEiBkirtYopOPn9kPEv Xp0q0NFebpec7JPYOIVf6UwH7dEvdHwHJX3OsyXGzWOU4L7ukW8D382c0UoveE8t1nkF RdOLupY09yvodTmieT3X6gXwInwE7Q83OLnsQgKV7kAOhlRjPVU4Am1l9AA7R4otu9tK JnKIFhUv1ZQhA2ejroEb2eCgySJHqu0tAs85QPhlbj3DYBGXUvlxjdp26Z6lb13wlX9G 3Wsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=eTrM7Hq4hpCqZJJSCYjY2SHXp05pM9pWeQHvPaxuFvw=; b=Fy8fzK7W0Jyhcgy/WSLRNlR3QKqYCjVLqen6V4zu0mIBQEXTbI7gXrBkjBOpiX+MKZ +n6TPL+FMT03vIonG7UKKlo8o1CUIMnmzUwLdenzZ5U+faldrjmJdBlj/lw1ZcWVYSiB PHdop9+Y3oHhkB2P3cIqC5G+7f38/0XwE3+j0PrOn6S/HjuMzqzDyqOB3Di8EGXScUF+ dA8U0Ow8EUlivw78vSi35ibkzjNW93PcAF39uL4DPme3W8v9xcBIGDiKS+xu6pcgVFfO PW7aEVVKutfTrEd6x98ZM2VnEdAftpdL8awLn+SnGZgqDzx2w4UjmsFiNiwYQVbeVdEk BPyA== X-Gm-Message-State: AJaThX68PYEfLQcCi8TS1MtQ7SFREARmiBcCg3XRAjGInJ+f2qPbs3YD +YbKgjJuLTtd1Ni0dUBTVXU+ X-Google-Smtp-Source: AGs4zMYcPBfv66FBIV4pnGk3CyFtJ9wANzs+CzzFTtU7kOP5SkPvkVXoQb642gObEfK76czkd8mczg== X-Received: by 10.84.172.195 with SMTP id n61mr35138650plb.78.1512916943821; Sun, 10 Dec 2017 06:42:23 -0800 (PST) Original-Received: from vicarie ([1.23.149.57]) by smtp.gmail.com with ESMTPSA id w24sm19499987pgm.54.2017.12.10.06.42.21 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 10 Dec 2017 06:42:22 -0800 (PST) In-Reply-To: <874loyx1ne.fsf@web.de> (Michael Heerdegen's message of "Sun, 10 Dec 2017 12:28:37 +0100") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c01::234 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:115324 Archived-At: Michael Heerdegen writes: > 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 This is a lot cleaner. Thanks! :) > 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. Can you share an example please? :) -- Narendra Joshi