1

I normally use my keypad numbers to perform various navigation commands, but one of those commands is goto-line, and when I'm in the minibuffer entering the line number, I want my keypad to run self-insert-command instead of all those other commands.

How can I do this?

1

You can set key bindings for the minibuffer in minibuffer-local-map. You can't bind a keypad key to self-insert-command because it's a function key, it doesn't have a character associated to it. It's possible to re-route function keys to characters, but this takes place at a lower level that isn't depend on the mode. You can get close by making the key invoke a macro.

(define-key minibuffer-local-map [kp-add] "+")
(define-key minibuffer-local-map [kp-subtract] "-")
(define-key minibuffer-local-map [kp-multiply] "*")
(define-key minibuffer-local-map [kp-divide] "/")
(define-key minibuffer-local-map [kp-decimal] ".")
(dotimes (n 10)
  (define-key minibuffer-local-map (vector (make-symbol (format "kp-%d" n))) (format "%d" n)))
(define-key minibuffer-local-map [kp-1] "1")

Alternatively, it is possible to do the translation at a lower level as well. You have to write a custom event translation function and check whether the code is running in a minibuffer inside the translation function.

(defvar event-translate-keypad-characters
  '((kp-0 . ?0)
    (kp-1 . ?1)
    (kp-2 . ?2)
    (kp-3 . ?3)
    (kp-4 . ?4)
    (kp-5 . ?5)
    (kp-6 . ?6)
    (kp-7 . ?7)
    (kp-8 . ?8)
    (kp-9 . ?9)
    (kp-add . ?+)
    (kp-subtract . ?-)
    (kp-multiply . ?*)
    (kp-divide . ?/)
    (kp-decimal . ?.)))
(defun event-translate-keypad-character-in-minibuffer (_ignore-prompt)
  "In a minibuffer, transform a keypad event into the corresponding character.
Outside of a minibuffer, preserve the original event."
  (let* ((current-events (this-command-keys-vector))
     (current-event (aref current-events (1- (length current-events))))
     (translation (and (minibufferp)
               (assq current-event event-translate-keypad-characters))))
    (vector (if translation
        (cdr translation)
          current-event))))
(dolist (translation event-translate-keypad-characters)
  (define-key input-decode-map (vector (car translation))
              'event-translate-keypad-character-in-minibuffer))
  • (There are also other minibuffer keymaps, such as minibuffer-local-completion-map and its children, and the children of minibuffer-local-map.) – Drew Aug 9 '17 at 2:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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