From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Arthur Miller Newsgroups: gmane.emacs.help Subject: Re: Eval keymapp in a macros Date: Thu, 05 Aug 2021 18:04:30 +0200 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="29968"; 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: Stefan Monnier Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Aug 05 18:15:43 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 1mBg22-0007Vg-0z for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 05 Aug 2021 18:15:42 +0200 Original-Received: from localhost ([::1]:41476 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mBg20-0000Sw-3q for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 05 Aug 2021 12:15:40 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:54686) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mBfrR-0000SS-Hw for help-gnu-emacs@gnu.org; Thu, 05 Aug 2021 12:04:45 -0400 Original-Received: from mail-am6eur05olkn2087.outbound.protection.outlook.com ([40.92.91.87]:59009 helo=EUR05-AM6-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mBfrL-0006Jw-O4 for help-gnu-emacs@gnu.org; Thu, 05 Aug 2021 12:04:45 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fezR/wLyhy8ATyEUJaa4VwZZr/dd8oEEjhdbr7py4EMWo0XHOX4d2gGsenhBgn9OjqsHh+WxXhycBEU3zoJHAZBJGrbRQkQleXPvMuPpT3XcuuG7CFmLg4g59ImzjtRgzD8gB8B92yod/P2n8hqerSBCM04ysgqG9keSoXz2OHW8P9ni2KFoqvyxjid+Du3fL/c826osYZ2qxdn6Clp25HH05Mu7TOyvMF3BwBjkUY4g7djD8XQ+jdZb89DViujFA8eEPKufVmP75Ls2Iei0x5g9P5NHNqxTSN0xvx5jKv5Q63xOE6NtJxsA5+KufymftzcZaM4C/vhCLKbfqSfZHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KsE5DtjmNZkYdh/auk0xa4DnGPb/Po4Pw3/hHGp/H0E=; b=U1Cn3/x5giat0J2ysyUxJGSLGj6xRHPsgW8DX4k6UYJaNPqjJ47j40FpTqh3tLob+UKEwrqUz0jBM0nO4SffcGDsqJbb1KNyagFX99MaMj7tkSHjQjdtAY99cvy3h/fX+7GVSp4+NTvNRZCcVj8ddnocsvEatNi3CaH5Lg63d0/ugkqQtQS7yUxI97qoByQnIWsK3AbW+8Q3/G6E8q7mxMPI9LtvsIkf44/I2IKu4q59AUt/4M3f+7iEpUSOVSqR2VgwO3cxlcGXEpl/ZOItkoyhG4maRzIKv0XoZfe0xAlrGCNZF9hv7QPAIoxG6qT86a+nxTnDM2yejByXS/I9gg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=live.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KsE5DtjmNZkYdh/auk0xa4DnGPb/Po4Pw3/hHGp/H0E=; b=HUAEVPjfq8VNYnBj2QW5BLa5g223CO/HIWt+wCjZPgXmWUdKct3OnvbkDiuU75au2L5seMM9S64C4nYfL5BH1QBGVCj0cUl2xNX/aka7yqB3zS5uhkJjkz0uWsnTpAUmNPpcscO0kLbsHryYuA88iRp5ramhqtw3L7vBV9oKE/A0kX1LKT2UcgISRXJjQt0kZbFvrdPbHY5nXobH92ARrD/JZG0qG3SgwLlGHlVJVFwF2cX11DinKq0PMSZk2UdPxcdMT6y999vvKAhIOR1quIz2iErhIvWOSVHKMAZ+uRTv5jhTw9f0NAWGDYH11W+x07yFf/tup3Iyf440epEZaA== Original-Received: from AM6EUR05FT040.eop-eur05.prod.protection.outlook.com (2a01:111:e400:fc11::4d) by AM6EUR05HT052.eop-eur05.prod.protection.outlook.com (2a01:111:e400:fc11::162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4394.16; Thu, 5 Aug 2021 16:04:31 +0000 Original-Received: from AM9PR09MB4977.eurprd09.prod.outlook.com (2a01:111:e400:fc11::45) by AM6EUR05FT040.mail.protection.outlook.com (2a01:111:e400:fc11::421) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4394.16 via Frontend Transport; Thu, 5 Aug 2021 16:04:31 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:51F11D2C723B7CBAC3C28772062B2E17811BCE9F023524AB34FB1035ACAD573A; UpperCasedChecksum:DDBE56C96CCF28831609DA06A30D1FA40FE354406869A6320156476FD20C8427; SizeAsReceived:7928; Count:46 Original-Received: from AM9PR09MB4977.eurprd09.prod.outlook.com ([fe80::6558:f201:6d1a:3f39]) by AM9PR09MB4977.eurprd09.prod.outlook.com ([fe80::6558:f201:6d1a:3f39%2]) with mapi id 15.20.4394.016; Thu, 5 Aug 2021 16:04:31 +0000 In-Reply-To: (Stefan Monnier's message of "Thu, 05 Aug 2021 09:53:16 -0400") X-TMN: [Lqd03gKqdjlgaFKlzItvTgjUpib5b/vx] X-ClientProxiedBy: AS8P189CA0003.EURP189.PROD.OUTLOOK.COM (2603:10a6:20b:31f::17) To AM9PR09MB4977.eurprd09.prod.outlook.com (2603:10a6:20b:304::20) X-Microsoft-Original-Message-ID: <87zgtvho8h.fsf@live.com> X-MS-Exchange-MessageSentRepresentingType: 1 Original-Received: from pascal.homepc (81.232.177.30) by AS8P189CA0003.EURP189.PROD.OUTLOOK.COM (2603:10a6:20b:31f::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4394.15 via Frontend Transport; Thu, 5 Aug 2021 16:04:31 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 46 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: e786b026-759a-4eda-0ef9-08d9582ab5db X-MS-TrafficTypeDiagnostic: AM6EUR05HT052: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zK1nxPSZFhKKLs+YytSLHKO9NP+vb/AEJKpPS1DZW1ypZxYeu26EoyEag8sV6Nia4+p/11/BXyS2L/4jtY1F3KKW8qUSlnG0STF+MU29JRNmDH6g1vS6FRrGLhYhbLDiWgA82WNEGA3QTQMd05iGNXDgVdv7CmWVlyL2A4OmvXRxi5+y0m2CB00VaEDyfc8lwmUNW/YSHcHFTiCiBRezsXr+6YBSPi7uztvokzOjj9ji1PHgt5e+X9120Rj8ou0KzcsTOBqVj+g44aLaSB9BnJ5qTCpzu5qtkofqq7YKAhAwTyyH2L+NSzEqqNmR2K2w2kHzy9F5FAqXqbfZ0AeSIWr5DXEk/vm1MjXbQaXtsGOKcuUWb7+OHrIRrEHZ/QBIUF/xPe51CxtNDw4p+qdOnECHRsImuLGwcMZsWH7M0SUr1Hb2XGwrws4I4h9zuduY X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: yhHL3TC7+7S4gdHQeF1lOtdktEFUyIqNZk2vh0TWrgy94J2eIMfZejerZwCwdqvGADHn4MGPL4GgJIt4JB0EGWmDANcha/v1BhtY/wR/eXn/Vyl5QcRnEfPtu79f35OaVXvX8JWeiCqzW2c3UNEBgw== X-OriginatorOrg: live.com X-MS-Exchange-CrossTenant-Network-Message-Id: e786b026-759a-4eda-0ef9-08d9582ab5db X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Aug 2021 16:04:31.6431 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: AM6EUR05FT040.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6EUR05HT052 Received-SPF: pass client-ip=40.92.91.87; envelope-from=arthur.miller@live.com; helo=EUR05-AM6-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-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:132370 Archived-At: Stefan Monnier writes: >> (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 yes, I know, that was just me being lazy, that wasn't ment for other people, so I used shorter name for the variable that I would type later on :). > 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. And same reason explains -map obfuscation :). I thought from the beginning it was cool to omit it but after a while realized it looked a bit dumb, so now when I am changing to ordinary list instead of cons, I planned to drop that. I agree it obscures the code with not much of repetitive typing removed. > (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`. When I said I would write it differently, I was thinking in different direction indeed. This is simpler than what I had in mind. However :-) ... if we drop the -map suffix thing, we have: (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 with-key-map (mapname &rest body) `(my--with-key-map ,mapname ',body)) This still does not handle the case with indirect keymap: (setq pkg-ops-map (let ((map (make-sparse-keymap "Packages"))) (with-key-map map ("h" . '("describe" . describe-package)) ("a" . '("autoremove" . package-autoremove)) ("d" . '("delete" . package-delete)) ("i" . '("install" . package-install)) ("s" . '("selected" . package-install-selected-packages)) ("r" . '("refresh" . package-refresh-contents)) ("l" . '("list" . list-packages))) map)) (with-key-map global-map ("C-c p" . pkg-ops-map)) The last line will eval, but at runtime gives error: Wrong type argument: commandp, pkg-ops-map. The "evals" came to fix that case. I tried with keymmapp in my--with-key-map but it still won't work without eval, I think for the reasons as Michael summarized.