all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Arthur Miller <arthur.miller@live.com>
To: Michael Heerdegen <michael_heerdegen@web.de>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Eval keymapp in a macros
Date: Thu, 12 Aug 2021 22:28:59 +0200	[thread overview]
Message-ID: <AM9PR09MB49771FC22BD96C925883DC1096F99@AM9PR09MB4977.eurprd09.prod.outlook.com> (raw)
In-Reply-To: <87zgtvns7x.fsf@web.de> (Michael Heerdegen's message of "Fri, 06 Aug 2021 05:54:10 +0200")

Michael Heerdegen <michael_heerdegen@web.de> 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 <arthur.miller@live.com> 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.



  reply	other threads:[~2021-08-12 20:28 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-02 12:16 Eval keymapp in a macros Arthur Miller
2021-08-02 20:33 ` Michael Heerdegen
2021-08-02 20:53   ` [External] : " Drew Adams
2021-08-03 21:20   ` Arthur Miller
2021-08-04  0:18     ` Michael Heerdegen
2021-08-04 10:52       ` Arthur Miller
2021-08-04 23:56         ` Michael Heerdegen
2021-08-05  6:03           ` Arthur Miller
2021-08-06  3:54             ` Michael Heerdegen
2021-08-12 20:28               ` Arthur Miller [this message]
2021-08-04  4:54     ` Yuri Khan
2021-08-04  9:38       ` Arthur Miller
2021-08-04 15:37         ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-08-05  6:12           ` Arthur Miller
2021-08-05 13:53             ` Stefan Monnier
2021-08-05 16:04               ` Arthur Miller
2021-08-05 16:34                 ` Stefan Monnier
2021-08-06  4:17                   ` Michael Heerdegen
2021-08-12 20:21                     ` Arthur Miller
2021-08-05  0:03         ` Michael Heerdegen
2021-08-05  6:15           ` Arthur Miller
2021-08-06  3:18             ` Michael Heerdegen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AM9PR09MB49771FC22BD96C925883DC1096F99@AM9PR09MB4977.eurprd09.prod.outlook.com \
    --to=arthur.miller@live.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=michael_heerdegen@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.