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: Tue, 03 Aug 2021 23:20:29 +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="28234"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: help-gnu-emacs@gnu.org To: Michael Heerdegen Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Aug 03 23:21:23 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 1mB1qj-0006zV-RJ for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 03 Aug 2021 23:21:22 +0200 Original-Received: from localhost ([::1]:33928 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mB1qd-00009M-9D for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 03 Aug 2021 17:21:15 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49212) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mB1q0-00006i-9b for help-gnu-emacs@gnu.org; Tue, 03 Aug 2021 17:20:36 -0400 Original-Received: from mail-oln040092065106.outbound.protection.outlook.com ([40.92.65.106]:41825 helo=EUR01-HE1-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 1mB1py-0002d8-1P for help-gnu-emacs@gnu.org; Tue, 03 Aug 2021 17:20:35 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=avhi3WAH60Meg7t/0d505tBcYwf2kfDJMPPYrOwGtC3LCOdOSsjYsxmEz7dXDgiBaghVzcuXbg3KrODGHxnNMP7uFBjdajMa+3Q8hoQOIyL2Ieqsmv7nStm5kkC1c0bIN+GqX7wNoIs5ij1pHlu2f9PsLju3eu55Kg1J1D7oN2g1Eh4zkLU8Yfm0BhoT5YI/9GFcBvA4LiKb83bj/Hi9HNaCeZPbaWgVVh2uaaRI+9W45oq0n17i1lHMHihpyeA8+BNN6KbOMD8MBd0ubqc+j+FA3WGVpfXU0TUkKIGO4cSWitOUfwGWvLfpEJZbXnzHEZc9Fb1aCu+9F/owcUREWQ== 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=GqXrLrInTYUP6BHTqk/ChKs/8LmM5OCcxScMC+ynbWc=; b=e3m09bkazI+oIKhlGR2PgX5RIJlBnFh8GLHFzEPKtyUKlbyjqxwi3GR6FdYte9Z5n0GvqMpAsGU/eDqZALLoTRInulYI/NeSvuRZ1v+OVcqUB7QjopWKKe5QpYuKBC5hb30s7jy64olPs3rqtUBHyBd/RsNL8PxZu1BwfaKUHQ/seKmKPtFNTpaHNpZGUzRgbCqpRipbcxz9R6Srp/X3zKoTluTeLxXbQgxAu6vaABn6PTxCABhXLi3PTbp55kIWq4GUf4Ug27HnBthFoJiD8MeItZxgS1GL+Ir15MIaaXleUthWesbHuxx75HQylD7ZCM/L6Jq82TigeToVcQfYmQ== 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=GqXrLrInTYUP6BHTqk/ChKs/8LmM5OCcxScMC+ynbWc=; b=VctQokVXkAUy8KfTRWJ4BYP11HZ9BJHSW17iWBjOuf53IqjToh/NKu7pvKNj1ULG81iQVjKD3M+lpANbvr2hvjkSGH8Vs9Dy7RkjpTTL6mqtW+Fa7DuDDTpqOI1MgF6DnQ26HtPeDRODEgESkKlCTQhgTn/0mBC4c76ktZEEGx5cqVINzmTTPxmOLloNTvcChZmCIk2QMqYDUfNuNPmVuuqmBHW5KHTQ4P9tosbY0Um5COl2ubEkhge04MSMRZy4YqDQln1UI4tHl9THfOHq+hhaodOvqtAQbMj1HkAoDAFxfJAyubAFnm5h65b5kX4ur3yZNN5zUTE/xTLDNn4M5A== Original-Received: from DB5EUR01FT038.eop-EUR01.prod.protection.outlook.com (2a01:111:e400:7e1a::50) by DB5EUR01HT224.eop-EUR01.prod.protection.outlook.com (2a01:111:e400:7e1a::354) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18; Tue, 3 Aug 2021 21:20:30 +0000 Original-Received: from AM9PR09MB4977.eurprd09.prod.outlook.com (2a01:111:e400:7e1a::53) by DB5EUR01FT038.mail.protection.outlook.com (2a01:111:e400:7e1a::191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18 via Frontend Transport; Tue, 3 Aug 2021 21:20:30 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:850C9B60A3D8F7613C40E2A9D23D8B3BEA1DAA5B45AF8796AED937D0792D3BBA; UpperCasedChecksum:3AB2630FD9BDC8D8533C7960C9189A8D109373CBCED0061FAAAA0085EEA36B78; SizeAsReceived:7341; Count:45 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.015; Tue, 3 Aug 2021 21:20:30 +0000 X-TMN: [a7Ijs7kjyPSpzdmAEDKXCp5beCqbih6M] X-ClientProxiedBy: AM6PR10CA0042.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:209:80::19) To AM9PR09MB4977.eurprd09.prod.outlook.com (2603:10a6:20b:304::20) X-Microsoft-Original-Message-ID: <87h7g6ql7m.fsf@live.com> X-MS-Exchange-MessageSentRepresentingType: 1 Original-Received: from pascal.homepc (81.232.177.30) by AM6PR10CA0042.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:209:80::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4394.15 via Frontend Transport; Tue, 3 Aug 2021 21:20:29 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 45 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 73860e1c-cd04-4d5f-40dd-08d956c48527 X-MS-TrafficTypeDiagnostic: DB5EUR01HT224: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zBsNMigY64MNyk2/xVPkEcV62en9dzYJmO90F8+rkhIy1dIL2Qkbo6hXypbPWNyKA7y0wwfPRdri5IPDfQSc0rDRumd5RoFynJwEXTYmyJqtdchuk5mEzv4D+mJi6PKiaD64L7aTrbZdGAlkt//6TmUNCxL0nXZ1v4nVJQBcUSJxhrzIC3XEsu8sY7rTFYTm3OYhMrjn+II0bRPlUemvBsMZMLWEeGXQVzF9XC72QE4Gwket7gNbY9nfm6vkZ7TLUM7+diXiSymtVFReuFHFkchUMJi7cEBSQdwWEfL6bPnfwwxQZ97dVaKlxygGcGSDEXbxfQ/WqaJLNiwL4E/Ptr5qGTVGriDYTOZ93L0QNMXu/nL5KGmud0wWWJwKaUYLnwQPmvMGhBhxAF/fgc69TPeNSMQM1LmMlp+Oz0OOUV4VSDOrKjin6Fzu6kcD/DHe X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 3lfZYPRlYp1AE41OXlx1ikn2HezTniYrWZvicoHUIKhQsGKgY3FbeB+IL2sKkqn4V3onM2WEZq7ao8Ts/Klo5baXxxn4P0BPqgQLmIk8TFjfsZxLxUWq985G21O+AyknGE4F2WXc8TyHvEK8ZHLWig== X-OriginatorOrg: live.com X-MS-Exchange-CrossTenant-Network-Message-Id: 73860e1c-cd04-4d5f-40dd-08d956c48527 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2021 21:20:30.0372 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: DB5EUR01FT038.eop-EUR01.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: DB5EUR01HT224 Received-SPF: pass client-ip=40.92.65.106; envelope-from=arthur.miller@live.com; helo=EUR01-HE1-obe.outbound.protection.outlook.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.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, NICE_REPLY_C=-1, 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:132350 Archived-At: Michael Heerdegen writes: > Arthur Miller writes: > >> #+begin_src emacs-lisp >> >> (defmacro with-key-map (mapname &rest body) >> `(let ((map ,mapname)) >> (dolist (def '(,@body)) >> (define-key map >> (if (vectorp (car def)) (car def) >> (read-kbd-macro (car def))) >> (if (or (listp (cdr def)) >> (functionp (cdr def))) >> (cdr def) >> (if (eval `(keymapp ,(cdr def))) >> (eval (cdr def)))))))) >> >> #+end_src > > Unfortunately you didn't show how you use it. What's the purpose of > your macro? Indeed, I didn't, I appologize. > What's the purpose of > your macro? It is just a simple wrapper for a define-key so I save some typing. I have actually rewrote it to not use cons any more but read ordinary list by pairwise elements, but that does not matter, the real work done is still the same. Here is example from my init file (with conses): (with-key-map global ;; Window-buffer operations ("C-" . term-toggle) ("" . term-toggle-eshell) ([f9] . ispell-word) ([S-f10] . next-buffer) ([f10] . previous-buffer) ([f12] . kill-buffer-but-not-some) ([M-f12] . kill-buffer-other-window) ([C-M-f12] . only-current-buffer)) My original macro didn't handle the case when an indirect keymap should be passed to define-key, someone on reddit point it out with example below: (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)) So I rewrote it like this: (defmacro with-key-map (mapname &rest body) `(dolist (def '(,@body)) (define-key ,mapname (if (vectorp (car def)) (car def) (read-kbd-macro (car def))) (if (keymapp (cdr def)) (eval (cdr def)) (cdr def))))) But I get error: Wrong type argument: commandp, pkg-ops-map So I ended with the one I posted which works, but I wonder why, and I don't really like it. I think you are correct about the reason. That is what I think also, so that is why I used eval, because I need the keymap object itself to pass to define-key, at least so I think. Maybe I am wrong there? > Anyway, `macroexpand' or `macroexpand-1' your call to see > what happens. I know. I am aware of macroexpand, I do use it. The above macro does expand to what I wanted, or better to say to what I thought I wanted, but it seems that it is not correct. Most correctly, it does not expand so much, since dolist itself is a macro: (insert (pp (macroexpand-1 '(defmacro with-key-map (mapname &rest body) `(dolist (def '(,@body)) (define-key ,mapname (if (vectorp (car def)) (car def) (read-kbd-macro (car def))) (if (keymapp (cdr def)) (eval (cdr def)) (cdr def))))) ))) (defalias 'with-key-map (cons 'macro #'(lambda (mapname &rest body) `(dolist (def '(,@body)) (define-key ,mapname (if (vectorp (car def)) (car def) (read-kbd-macro (car def))) (if (keymapp (cdr def)) (eval (cdr def)) (cdr def))))))) I have edited spaces for readability. > Note that nothing in BODY is ever evaluated (it's behind a quote). Stuff in dolist gets evaluated, because dolist itself is a macro. The real body of function which is all in do list gets expanded by dolist (or the real interested part by 'while' which seems to be a special form) and then evaled by dolist. So dolist will actually call define-key for me, and that is what seem to happend, because stuff gets defined after I call the macro. I hope I understand correctly what is going on there. Also I have to test for listp and functionp in correct order. When I switch orders then I get errors too, so I am not so happy about that macro att all. As a side note, this isn't really a macro that writes a function or another macro and returns it. I have it partly to save myself typing, and partly to skip overhead of macroexpansion when Emacs start. Byte compiler will expand it when init file is byte compiled. Actually I wrote a program to write my init file which does expansion when it outputs code to init file but it is just another regression. Anyway these are just side notes, the macro works or should work in interpretter too. > My tip when writing a macro (do you really need one btw?): Write an > example call and then the desired expansion down. Only after that write > down the macro implementation that offers exactly that expansion. You > can later do that in your head, but if you skip that step you get all > the surprises and pitfalls that macros are known for. Indeed, I completely agree. Thank you for the answer and for trying to help me. I am really confused what happends there, so I really appreciate if you (or anyone) can make it a bit more clear.