Add an input method indicator to minibuffer in Emacs

Posted on January 14, 2023

Do you notice that some Emacs’s built-in LEIM input methods have special display enhancements when used inside minibuffer?

Normally the activation status of the input method is displayed in the buffer modeline. But there is no such indication mechanism for minibuffer inputs. Some input methods provided by quail.el such as japanese uses post-command-hook to append input method activation information to the end of minibuffer like this:

M-x user-inputed-string [Aあ]

While limited to a few built-in input methods, it’s really a convenient feature! Unfortunately the implementation is quite buggy and doesn’t work well with completion frameworks like icomplete that update the minibuffer dynamically.

With only 14 lines of elisp code we can add this feature to any input method and it works with completion frameworks by utilizing overlays.

(defvar-local input-method-minibuffer-guidance--ov nil
  "Overlay showing the active input method")
(defun input-method-minibuffer-activate-guidance ()
  (when (minibufferp)
    (unless input-method-minibuffer-guidance--ov
      (setq input-method-minibuffer-guidance--ov (make-overlay (point-min) (point-min) nil nil t)))
    (overlay-put input-method-minibuffer-guidance--ov 'after-string
                 (format "[%s] " current-input-method-title))
    ))
(defun input-method-minibuffer-deactivate-guidance ()
  (when (minibufferp)
    (overlay-put input-method-minibuffer-guidance--ov 'after-string "[-] ")))
(add-hook 'input-method-activate-hook #'input-method-minibuffer-activate-guidance)
(add-hook 'input-method-deactivate-hook #'input-method-minibuffer-deactivate-guidance)

This is what it looks like with mozc.

[[Mozc]] M-x user-inputed-string