4

I use different input methods in different buffers. I would like to have M-x and generally any minibuffer input automatically use the input method of the buffer I am currently working in. How can I do this?

Right now, I have the following:

(add-hook 'coq-mode-hook (lambda () (set-input-method "math")))
(add-hook 'minibuffer-setup-hook (lambda () (set-input-method "math")))

So, I automatically get the math input method when working on Coq files, but I always have the math input method in the minibuffer. I'd rather only use math in the minibuffer if I am currently editing a Coq file.

1 Answer 1

2

Most commands which read from the minibuffer, such as completing-read, take an optional inherit-input-method argument which does exactly what you want. Nevertheless, in my experience this argument is rarely used in general Elisp code.

Instead, we must inspect the input method of the buffer from which the minibuffer was invoked ourselves:

(defun my-inherit-input-method ()
  "Inherit input method from `minibuffer-selected-window'."
  (let* ((win (minibuffer-selected-window))
         (buf (and win (window-buffer win))))
    (when buf
      (activate-input-method (buffer-local-value 'current-input-method buf)))))

(add-hook 'minibuffer-setup-hook #'my-inherit-input-method)
1
  • +1 for mentioning optional arg INHERIT-INPUT-METHOD for read-from-minibuffer and completing-read (but not for read-file-name).
    – Drew
    Jan 24, 2018 at 19:13

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.