0

I want to send keybindings to meow-leader-define-key but it only takes them in dotted pair notation, which is a string followed by a function, but I can't figure out how to convert a string and a function into dotted pair notation in the function.

I would like something like this:

(cons "hi"  (lambda ()
              (message "ok..." )))

To result in this:

("hi" . (lambda ()
          (message "ok..." )))

I would like to return the code below like this with a dot:

'("z z" .
  (lambda nil
    (interactive)
    (message "zz")))

Here is the full code, the problem I am trying to solve is in pop newlist.

(setq my/modal 'meow)

(defmacro my/def-key (state map &rest rest)
  (let* ((newlist
          (pcase my/modal
            ('evil rest)
            ('meow (mapcar (lambda (x)
                             (let ((KBD (car (cdr x))))
                               (if (and (stringp KBD)
                                        (string-match-p "^<leader" KBD))
                                   (setcar (cdr x)
                                           (replace-regexp-in-string "<leader>" "" KBD))
                                 x))) rest))))
         (mlist (remq nil
                      (mapcar (lambda
                                (x) (when (and (cdr newlist)
                                               (car newlist))
                                      `'(,(pop newlist)
                                         ,(pop newlist)))) newlist)))
         ;; works
         ;;`(,@(cons 1 2 )) )) newlist)))
         )
    (pcase my/modal
      ('evil
       `(evil-define-key ,state ,map ,@newlist )) 
      ('meow
       `(meow-leader-define-key ,@mlist ))
      (_
       (display-warning :wrong-variable
                        "Wrong variable set for my/modal")))))

(macroexpand-1 '(my/def-key 'normal global-map
                            (kbd "<leader>z z")
                            (lambda ()
                              (interactive)
                              (message "zz"))
                            (kbd "<leader>z f")
                            (lambda ()
                              (interactive)
                              (message "zf"))))
7
  • 1
    Can you please describe what problem you are trying to solve in words? As it is, we would have to wade through your code to guess what you are trying to do and then figure out how to fix it. Also, why are you using macros? Why can't you do what you want to do with functions?
    – NickD
    Mar 26 at 16:27
  • 1
    It helps, I think. I edited your question to add this information - which you should have done in the first place: comments are mostly for us to ask clarification from you, but your response should be to edit the question to clarify it, not to add the clarification in a comment. Ideally, the question should stand on its own, even if all the comments are deleted.
    – NickD
    Mar 26 at 18:55
  • 1
    @NickD Sorry about that, more info next time. I think a macro was not the best approach, dolist might work better. However, I'm still puzzled as to how to do that. Thanks.
    – ritchie
    Mar 26 at 20:50
  • 1
    "decided to use a macro to learn more about them" -- a good question is only as complicated as necessary. You should always try to reduce a problem to the simplest test case with which you can reproduce the issue, and then post about that.
    – phils
    Mar 26 at 21:53
  • 2
    "I would like something like... to result in..." -- it does. The printed representation just doesn't display in that syntax. Perhaps you are getting confused by the structural equivalence of (foo . (bar)) and (foo bar) ? If not... what is the actual problem?
    – phils
    Mar 26 at 22:00

1 Answer 1

0

I problem was that I forgot to add interactive 🫤.

(let ((key "z o")
     (func 'find-file)
     (func2 '(lambda ()(interactive)(message "hello" ))))
  (meow-leader-define-key (cons key func2)))
3
  • `,(cons key func) is the same thing as (cons key func). I.e. if you are 'unquoting' the entire form, the backquote+unquote is redundant.
    – phils
    Mar 29 at 2:57
  • n.b. ((lambda ()(message "hello" ))) is a form which, if evaluated, returns a string "hello". It is defining and then calling a function (and message returns its message). If you want to obtain the function object you would use only (lambda ()(message "hello")) (and preferably not quoted, although quoted function forms can still be called).
    – phils
    Mar 29 at 3:02
  • You're right, I updated the answer. The problem was that I forgot to add interactive 🫤.
    – ritchie
    Apr 3 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.