From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.help Subject: Re: Eval keymapp in a macros Date: Fri, 06 Aug 2021 05:54:10 +0200 Message-ID: <87zgtvns7x.fsf@web.de> References: <87bl6fy4cf.fsf@web.de> <87r1faf4fy.fsf@web.de> <871r78ydbb.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="22125"; 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: Arthur Miller Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Aug 06 05:54:39 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 1mBqwR-0005ej-Gj for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 06 Aug 2021 05:54:39 +0200 Original-Received: from localhost ([::1]:40050 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mBqwP-000833-UH for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 05 Aug 2021 23:54:37 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47034) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mBqw4-00082d-DI for help-gnu-emacs@gnu.org; Thu, 05 Aug 2021 23:54:16 -0400 Original-Received: from mout.web.de ([217.72.192.78]:55281) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mBqw2-0004So-Lg for help-gnu-emacs@gnu.org; Thu, 05 Aug 2021 23:54:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1628222051; bh=VFKsZ0JfTZSkxqTKEed7+NRIO7nmEXTyHk8et617Aec=; h=X-UI-Sender-Class:From:To:Cc:Subject:References:Date:In-Reply-To; b=fAjXeGS+INxutZI/EeUyHKxu52IXh/3RomZiTyk+WudAuP16hD9SihXlCBWtlNaX5 C7uMROCfEdTwpFkLi8vpjF9y7hlHbmUr3mwXBtHnzsL7fd62ckCJQhG9cHBaCR6GZE v4fWOqXx+awdPjhUOf/y27iwIAuyUuSCMEmBsJTk= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Original-Received: from drachen.dragon ([88.66.201.45]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MzTLO-1n6xnf1dHU-00vMkP; Fri, 06 Aug 2021 05:54:11 +0200 In-Reply-To: (Arthur Miller's message of "Thu, 05 Aug 2021 08:03:42 +0200") X-Provags-ID: V03:K1:FDL9CKb4yDbiQHUHemH+huruGtcgTIFkn7KWfDnmaahHhNzCtRH ub+cPi/YY8ugJMwx4dn++8WFZ4EnBnrPAUolsUhtucIsNUfFy2Vf3MLQWmAycau5vD4NGfc CSElnGWhZVAxPfOZ4h2CE3u4FCovpMD75/zBuZ+GMzIjyMXGN+wtJl09dYTOpN1crlzjS2H jz17K8H6zWth9hhY7M+qw== X-UI-Out-Filterresults: notjunk:1;V03:K0:92E1xG4e1oU=:aMcJBlVi3zcOrJUFATm3Ft soUXYijT7iH2kAac0lbuyyQi+QOxEvDg8KqqpJ7JkH6EAlNYutVBYdr2gyOq0dZtS8HcjDu6Q M40+oH1SyS4Xl2FGgejIXcCf9LKE0zDjm3u7T3ku3AyExAPZxjuai2K8ICBOBGtL1DOQFiG1B PxrtqfvlO3KQvy76iAnukI5vRdyMtdqz6D8/j5mKCdmcCCKCThJPD+holzcDHIJ/h0N1leNOu vCSwH1p+W8Wvg5E6jpBvF5kAW+d+39WxLE4dVfQ0rjgOecscb3hj0yDlbQB+tRZsoEvu6iYkZ dS76CJR4/0+DLWkRJr0Fh6Z01FinPxKPpVqGRDzu+Gzg4Jj8H8l/WfYQZM+zpZKPby9y6YOnj SA1M0E1J4MrbIyCt8j8totO2nvVWWWobjiGqBEMca6WJhskBVaWaiOxoh20VxbWgl+BcDMVXo mFL1n6C9tEIfWmQwQhwbnI4BV78UieQUz3XbkWFFQcfSuxlHQZgg5NoTNRNEO4+KgWPPfslWl qQVGX+PZqEu87K/kdnORKiVmrgnloC8ZbxVbQdG3Wuz9z2JWshZOfIubkzuM6BVM7+NoMDgbl vteM7rtpQwHUfLfss79yZuSnxBBCdL9IK+Toe4C9Ffcynr6tbg/HcBuXch+B5MlOVrb3gkYAM tZjffDx1DrlajwnFAOui92gHnc2ATDd0NZCEYs8GzTbjiHMmVuQCsMA0EuuSWwTS1d3htITDZ nX6oDEZAs0q132htFZvcbFNsXUaf9LR+6J/+j9iuK0nLPejgvfK5E9hGHQfM56RVy5V/KTzB Received-SPF: pass client-ip=217.72.192.78; envelope-from=michael_heerdegen@web.de; helo=mout.web.de X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, 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:132373 Archived-At: 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 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. 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. Michael.