From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: Eval keymapp in a macros Date: Thu, 05 Aug 2021 09:53:16 -0400 Message-ID: References: <87bl6fy4cf.fsf@web.de> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30885"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: Stefan Monnier via Users list for the GNU Emacs text editor To: Arthur Miller Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Aug 05 15:55:01 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mBdps-0007nj-9P for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 05 Aug 2021 15:55:00 +0200 Original-Received: from localhost ([::1]:38884 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mBdpq-0002wD-Jg for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 05 Aug 2021 09:54:58 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46396) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mBdoM-0001tJ-A8 for help-gnu-emacs@gnu.org; Thu, 05 Aug 2021 09:53:26 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:22816) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mBdoI-0005WR-4Z for help-gnu-emacs@gnu.org; Thu, 05 Aug 2021 09:53:25 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 2F5D410020E; Thu, 5 Aug 2021 09:53:20 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id B22F91001F4; Thu, 5 Aug 2021 09:53:18 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1628171598; bh=pkS9xBRUDblZhUMMHvBmwO07bZVFsehUchgz76O4cBQ=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=K/VZ4M/+99EOZgdGAU3QsFeMbfdM/nIc8dztWtfs3yyWi951QGIi91crVE/LxN8fG qGMy+oXGqjupeCkZtBK/NGLZLZJwWpWtZWBNhHa5DCg3dlya8kLEx8swzQwfQvIaHb BY8LLfb14t/21OBYKm3T1Dt2ecpWcBpuqD6hntu7O7UZBWtxP3A+g5cmXbTVU4sm/D tjGYZANkkLJRWoeCcw+SFNPbXt7Mu85rQlykOdgColsh4wOTQku261S15sdnc2gkw5 HBGcCXPd6uy4Hcsz48RLK+ljxs75JyY1FEEeylwSFPHwFg072zNeKW6c2LV8GkZyOZ c5EB1bWRYA9PA== Original-Received: from alfajor (unknown [216.154.29.138]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 8810D1201C6; Thu, 5 Aug 2021 09:53:18 -0400 (EDT) In-Reply-To: (Arthur Miller's message of "Thu, 05 Aug 2021 08:12:41 +0200") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:132369 Archived-At: > (defmacro with-key-map (mapname &rest body) > `(let ((map (eval-and-compile (if (string-match-p "-map$" (symbol-name ',mapname)) > (symbol-name ',mapname) > (concat (symbol-name ',mapname) "-map")))) > (defs '(,@body))) > (dolist (def defs) > (define-key (symbol-value (intern map)) > (if (vectorp (car def)) (car def) > (read-kbd-macro (car def))) > (cdr def))))) Your `map` variable above has a misleading name since it won't hold a map but a string (the name of the variable holding the map, so you need the "double indirection" thought `intern` first and `symbol-value` afterwards to get at the map). (defun my--with-key-map (map defs) (dolist (def defs) (let ((key (car def))) (define-key map (if (vectorp key) key (read-kbd-macro key)) (cdr def))))) (defmacro my-with-key-map (mapname &rest body) `(my--with-key-map ,(if (string-match-p "-map\\'" (symbol-name mapname)) mapname (intern (format "%s-map" mapname))) ',body)) Look ma! No `eval-and-compile` and no `symbol-value`. Stefan PS: Personally I'd recommend against the `string-match-p` dance, since all it saves you is the typing of `-map` but in exchange it prevents you from using your macro with keymaps that have a name that doesn't end in `-map` and it "obfuscates" the code a little, preventing things like ElDoc and Xref from understanding what's going on.