help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Eval keymapp in a macros


From: Arthur Miller
Subject: Re: Eval keymapp in a macros
Date: Thu, 05 Aug 2021 08:03:42 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Michael Heerdegen <michael_heerdegen@web.de> writes:

> Arthur Miller <arthur.miller@live.com> writes:
>
>> > Your `keymapp' fix is an emergency solution but it's not perfect:
>> > that test happens at compile time.  If the keymap is not defined at
>> > compile time your compiled code will be inappropriate.
>
> I see now that I was wrong here - the test is performed at run-time.

I discovered that yesterday too. I tried the new macro on my init file,
and it found several left-over bindings after some packages I don't
use any more.

>> I agree, but do I wish to pass name of undefined keymap to define-key?
>
> Not at run-time: when the `define-key' call is evaluated, the symbol
> must be bound, else you would get an error.

Of course, every symbold has to bound when evaled. This is a bit special
use case. I am always ensuring it is evaled in context where everythign
is defined.The use case for my init file is that I pass those stuff
always in 'with-eval-after-load' for some package. So it is always
symbols should be always defined otherwise I have bigger problem then
misspelled syntax. I also expand this myself so I just just write
define-key calls to my init file. However I also have this macro defined
for interpretter, so I can test and eval stuff while I hack my init file.

> But some way or the other something has to decide how to interpret a
> symbol.  If you want to use the same notation for the cases, you need to
> use some kind of heuristic: in theory a symbol might name a function and
> might be bound to a keymap at the same time.

Yes, it's 'lisp-2' ... I like that feature. I know a lot of people
prefer scheme version, but I think it is handy and also more efficient
with 2 namespaces.

> 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.

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.

>                    You could also check whether the `symbol-name' ends
> with "...-map".

I could, there is not guarantee, since it is just a convention, but yes
I agree, I could have done that.

Anyway, thanks  you for the help and feedback. I do have some better
understanding after this.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]