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, 12 Aug 2021 22:28:59 +0200 Message-ID: References: <87bl6fy4cf.fsf@web.de> <87r1faf4fy.fsf@web.de> <871r78ydbb.fsf@web.de> <87zgtvns7x.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="40044"; 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 Thu Aug 12 22:29:29 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 1mEHKS-000AEq-Ng for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 12 Aug 2021 22:29:29 +0200 Original-Received: from localhost ([::1]:56340 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mEHKR-0002Yp-Hm for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 12 Aug 2021 16:29:27 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44574) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mEHK4-0002YO-G7 for help-gnu-emacs@gnu.org; Thu, 12 Aug 2021 16:29:04 -0400 Original-Received: from mail-am7eur06olkn2016.outbound.protection.outlook.com ([40.92.16.16]:59105 helo=EUR06-AM7-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 1mEHK2-0007Qc-Qs for help-gnu-emacs@gnu.org; Thu, 12 Aug 2021 16:29:04 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MbOQlH3kwxX3lhIZosI2/t8Z3m7TAF0OkgP5gHe7TcX/vSOoHpVwBO2hvDiy/IIfArmrwKfXdTHfWQGk3/RneOcRw2eMQgi3N9lbJqoghsBLDwSed/CY+LNI5/D1p39AUf+QttPwdAQjQSOXxq2S/e9SYVK8Pu/2QvDxL4ca0hC+3kOpcknmTld90WvUmogk/ULZXXj6HYbSWoH3SeOV6bw3olRapX+X8On9KkjL+Ua1uhaekVHd+J5NLS0023/9Lhss38BNgk4ML1zx/yzd582TVhJuuQpUvEWXc5g9crCUZ2dbiq50ohHVk11d/m94oq6mtEaB+W/2qOvnjXhVZg== 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=8bM+IyPNXKBpHTTxgt2FttTfwKcrDMuUp8TecibDe8M=; b=QwO5FmElOfTAcWa8Z8MA7AD2xnxyevY+tk9qsrmMihdjNCu8lemFGi8vpk9HVSYVMx0bPEsrhrnFVWfXDUQmpdmMrVTOAwjB/1Xq+PEckXRvLqMFIQG+rCvgZxoGdc7CnESQMHtLdlfauaUWZ6xJfCQ/aST17Pdh0lDvrDMwBH/25ECikQI4fCCWn/5/isZAqXJJMYW78lnUCKATlqSWF396evcDODUOvl5L4SCQ+bIYq0epSMZ59YqOUX2vnvL5H86aVjM09Y4oiocfEGB3AtsaNYwTcEltwbMYhmcN6fpp3YO1cF4oWiDUhzeEA1gqoPPZJjLDd/kc8g5ZY5Jqpg== 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=8bM+IyPNXKBpHTTxgt2FttTfwKcrDMuUp8TecibDe8M=; b=CxhpnS4uusKpa+9rChSUIdDKZbtBMpkzIOVib8lz2A7QPZfaeVBpeulTrARgLsjGhN4rpiO6sHw5eD0FXYjbpJUxm8BG+5Y5sZS3gS7p6eedJIWBDzAFRN3809YUsTONF14LDNSvdgkOoclvU9D/4Smtmy7eNOVUc8UtZRbicmMZJXV7zBxz+0bEyfxnhUEq1RqdSbwmieWkzVQgHXULL13rOquElWeSB1v+959S3adJ/67ejX6zmvp+5QYMoE4tdGMQd1gHfKSbEZ7Daxov+A2rT2RUPGNvc9EVnz6Dx7VgCRZYQyoNXn3bkFfFF9/xD1Kb8VZezdUayIntimN0KA== Original-Received: from VI1EUR06FT057.eop-eur06.prod.protection.outlook.com (2a01:111:e400:fc37::51) by VI1EUR06HT219.eop-eur06.prod.protection.outlook.com (2a01:111:e400:fc37::378) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.16; Thu, 12 Aug 2021 20:29:00 +0000 Original-Received: from AM9PR09MB4977.eurprd09.prod.outlook.com (2a01:111:e400:fc37::49) by VI1EUR06FT057.mail.protection.outlook.com (2a01:111:e400:fc37::165) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.16 via Frontend Transport; Thu, 12 Aug 2021 20:29:00 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:A103109477AB163C29D1A6407269101B6A9CECD22211BEBC41264C780309389E; UpperCasedChecksum:D0B6CF1C89DA4981F49DC4750A5F374DA3F44FFDB0A6D1B5B930019B21227770; SizeAsReceived:7790; 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.4415.018; Thu, 12 Aug 2021 20:29:00 +0000 In-Reply-To: <87zgtvns7x.fsf@web.de> (Michael Heerdegen's message of "Fri, 06 Aug 2021 05:54:10 +0200") X-TMN: [SJCawrvOVq8w1zWhjc5R8s4ZVMhaedkg] X-ClientProxiedBy: AS8PR04CA0042.eurprd04.prod.outlook.com (2603:10a6:20b:312::17) To AM9PR09MB4977.eurprd09.prod.outlook.com (2603:10a6:20b:304::20) X-Microsoft-Original-Message-ID: <87tujutnjo.fsf@live.com> X-MS-Exchange-MessageSentRepresentingType: 1 Original-Received: from pascal.homepc (81.232.177.30) by AS8PR04CA0042.eurprd04.prod.outlook.com (2603:10a6:20b:312::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.14 via Frontend Transport; Thu, 12 Aug 2021 20:28:59 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 46 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 60405ccf-6632-4302-abf3-08d95dcfd113 X-MS-TrafficTypeDiagnostic: VI1EUR06HT219: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: knAb9WU5Rh+TlV8QID1s1tFAPovYvqFs8pdXQXskOSKx3HGDJEXxcnaT6lE8tS/220D3mvSzAGPiYu7zHL1LRtDYypsP8vj0wpWPvLFKJOmX7efPysaN02sp0KdaZ94vS5v+bPanMsLGclENgW8MpfaiuPj98BnAxTRkDOVf7d/pYvQSjE2JVoyX2/req4I1dxZJ63C4jl66kHNnhLtCmr+hHfyD9vosua1EMHSzNPWTfIkUGcVlFeFn6hGSzCzM1SPrItYB3NlYbgeWGLaG+136rP/AU5Af45ZQk5AcJ4IH/DXK/SPzNRBoW2BLjQ3t96YV1z5bmTTPojHpIwdZZWvk92lvy9IgBGGN4fEkqOUguUIHQsXDFrCL7ZtGvNl7iMYRpv+wjTbNyWKVzPRvc6kCbr+sUEvT4zGEyvvkqnHWdHFFcs/6A7la48Cl4S6z X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: GVcTtWG01dolddXpfSdRlAgXWAkElfj2idR0zbkPepAES3PXX4cKn9+M4yLb27jy4fNAK2leGBW9gqYbNoHcLApRIFEl53NHxrDivIItnF1nZhDenXX6ELmMeOjIyFfrW4+FwxmuDol9yLJwu7jrWw== X-OriginatorOrg: live.com X-MS-Exchange-CrossTenant-Network-Message-Id: 60405ccf-6632-4302-abf3-08d95dcfd113 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Aug 2021 20:28:59.9983 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: VI1EUR06FT057.eop-eur06.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: VI1EUR06HT219 Received-SPF: pass client-ip=40.92.16.16; envelope-from=arthur.miller@live.com; helo=EUR06-AM7-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:132489 Archived-At: Michael Heerdegen writes: Sorry for the little bit late answer. I didn't had time to look at this immidiately, and then it just was left. > Arthur Miller writes: > >> > And instead of `eval' better use `bound-and-true-p' - you know that you >> > look at a symbol. >> >> Thanks. I can remove at least the eval in test with bound-and-true-p, >> but I don't think I can remove the second eval, since I have to get >> object the symbol is representing. > > And exactly this is what `bound-and-true-p' returns. But that doesn't Aha, I did a little bit of a noob thing there, and didn't checked the docs. Just expected return to be a boolean and forgott that in elisp, actually lots of stuff return the object itself, which is handy (like `or', or `and' etc). > matter if you are actually having a problem like this: > >> However I am getting false positives from keymapp, it accepts anything >> seems like: >> >> (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 `(bound-and-true-p ,(cdr def)) >> (if `(keymapp ,(cdr def)) >> (eval (cdr def)) >> (cdr def)))))) >> >> Instead I have to use to test for listp and functionp first: >> >> (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 `(bound-and-true-p ,(cdr def)) >> (if (or (listp (cdr def)) >> (functionp (cdr def))) >> (cdr def) >> (if `(keymapp ,(cdr def)) >> (eval (cdr def)))))))) >> >> And I can also remove last if, and just leave eval, keymapp does not >> seems to cull anything out. > > I think you have a problem here with these nested backquotes, not > everything you think gets evaluated. Have a look at the expansion > (`macroexpand'). > >> Anyway, thanks you for the help and feedback. I do have some better >> understanding after this. > > An honest opinion: Maybe the advice to start with a function is the best > one so far. Else you will be wasting time for nothing. Don't write > macro code and look what happens when you call it and then try to fix > what seems to be wrong. You'll get crazy and not learn very much. That > approach doesn't work for most human brains. Yes, that is a very good advice, if anyone else than me is reading this in the future. > One cool trick for learning: make the macro expander a named function. > > I'm using your first definition from above - it is equivalent to > > #+begin_src emacs-lisp > (defmacro with-key-map (mapname &rest body) > (apply #'my-with-key-map-expander mapname body)) > > (defun my-with-key-map-expander (mapname &rest body) > `(dolist (def '(,@body)) > (define-key ,mapname > (if (vectorp (car def)) (car def) > (read-kbd-macro (car def))) > (if `(bound-and-true-p ,(cdr def)) > (if (or (listp (cdr def)) > (functionp (cdr def))) > (cdr def) > (if `(keymapp ,(cdr def)) > (eval (cdr def)))))))) > #+end_src > > While this is semantically exactly the same and doesn't make much of a > difference you can now better study what the expander does: how it > transforms code (or forms) to code (to be evaluated). > > E.g. now > > (with-key-map global-map ([f1] . make-frame-command)) > > will internally use an ordinary function call: > > (my-with-key-map-expander 'global-map '([f1] . emacs-list-mode-map)) > > to generate the expansion. You can use the debugger, study the return > value etc. Why does the code it returns not work as expected? Would > you write the code like that? > > To repeat: this is actually working in the wrong direction. Better > start from the expected expansion, and then write an expander function > that generates that expansion. Indeed, and I agree. That was something like that I meant when I answered in some earlier mail to Stefan. Anyway It all start with simple 6 lines of very simple macro which worked out straightforward, so I was all the time just "hacking" a single case it didn't worked for. But yes, in the end, it is not very good approach. Thank you for the help.