0

Is it possible to have multiple keymap entries set up to call the same interactive function, but with different arguments associated with each mapping?

For example, suppose I have this function ...

    (defun interactive-func (&optional arg) ;; or perhaps another arg specification
      (interactive) ;; or perhaps with one or more parameters passed
      (message "arg=%s" arg)
      (sit-for 1)
      )

... and suppose I have a keymap defined something like the following:

    (let ((map (make-keymap)))
      (suppress-keymap map)
      (define-key map "a" 'interactive-func) ;; with "abcdefg" as the argument
      (define-key map "b" 'interactive-func) ;; with "x y z" as the argument
      (define-key map "c" 'interactive-func) ;; with (list "1" "2" "3") as the argument
      ;;; etc. ;;;
      )

Is something like this possible? Or do I have to always use different functions with different key mappings if I want all of them to trigger different behaviors?

1
  • Dynamically providing interactive argument values is often taken care of by the interactive spec of the command. Remember that you can use (interactive FORM) where FORM is any lisp expression which returns a list of arguments. (interactive (list ...)) is common, but you can do anything. So if your command can figure out the arguments it needs from other context, you could just do that.
    – phils
    Commented Mar 6 at 23:34

1 Answer 1

1

I tried:

(defun interactive-func (&rest args)
  (message "%s" args))
  
(defvar my-keymap (make-keymap) 
  "Keymap")

(define-key my-keymap (kbd "C-c a") (lambda () (interactive)
                      (interactive-func "a")))

(define-key my-keymap (kbd "C-c b") (lambda () (interactive)
                      (interactive-func "a" "b")))

(define-key my-keymap (kbd "C-c c") (lambda ()  (interactive)
                      (interactive-func (list "a" "b" "c"))))

(use-local-map my-keymap)

I don't understand how you would define your local-map.

1
  • I didn't realize that I could define the keymap action with a lambda. I copied and pasted similar code (without using 'interactive-func) from a portion of a much longer emacs module, and then I changed the actions to 'interactive-func for my question here. That's why the keymap is shown as being defined in the let clause ... which I realize isn't going to be available to the caller in the example I gave. But I wasn't worried about the keymap definition itself, only about the action definitions, and your response provided the answer that I've been looking for. Thank you very much!
    – HippoMan
    Commented Mar 7 at 1:28

Your Answer

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

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