Subject: bug#44266: Transient input methods

From: Juri Linkov <[email-masked]>
Date: Tue, 27 Oct 2020 22:43:33 +0200

Tags: patch

[Creating a separate feature request from bug#43866]

>> How about making a new input method for those?  It seems to me that
>> C-x 8 is already too "fat".
>
> That may be useful, but it has a drawback compared with C-x 8.
>
> It is inconvenient to change input methods just for one character and
> then change back.

The following patch addresses such issues of the current input method
implementation by providing a new feature of transient input methods.

A transient input method is enabled temporarily for entering a key sequence,
and is disabled automatically afterwards.

The command that temporarily activates an one-off transient input method
is bound to the mnemonic key 'C-x \'.  A prefix key is used to select
the default transient input method, e.g.:

C-u C-x \ compose RET   - selects the 'compose' input method
                          as the default transient input method;
C-x \ E =   - activates the default transient input method,
              inserts the EURO SIGN character,
              and disables the transient input method;
C-x \ ^ 3   - inserts the character SUPERSCRIPT THREE.

The same with the help of the 'TeX' input method:

C-u C-x \ TeX RET
C-x \ \ e u r o   - inserts the character EURO SIGN

Also it's possible to create a new input method from the existing
'C-x 8' keymap, and set it as the default method, e.g.:

C-u C-x \ iso-transl RET
C-x \ * E   - inserts the character EURO SIGN

diff --git a/lisp/international/mule-cmds.el b/lisp/international/mule-cmds.el
index e3155dfc52..c5a0145163 100644
--- a/lisp/international/mule-cmds.el
+++ b/lisp/international/mule-cmds.el
@@ -55,6 +55,7 @@ mule-keymap
 
 ;; Keep "C-x C-m ..." for mule specific commands.
 (define-key ctl-x-map "\C-m" mule-keymap)
+(define-key ctl-x-map "\\" 'transient-input-method)
 
 (defvar describe-language-environment-map
   (let ((map (make-sparse-keymap "Describe Language Environment")))
@@ -1344,6 +1345,16 @@ default-input-method
                  mule-input-method-string)
   :set-after '(current-language-environment))
 
+(defcustom transient-input-method nil
+  "Default transient input method.
+This is the input method activated automatically by the command
+`transient-input-method' (\\[transient-input-method])."
+  :link  '(custom-manual "(emacs)Input Methods")
+  :group 'mule
+  :type '(choice (const nil)
+                 mule-input-method-string)
+  :set-after '(current-language-environment))
+
 (put 'input-method-function 'permanent-local t)
 
 (defvar input-method-history nil
@@ -1519,6 +1530,35 @@ set-input-method
 (defvar toggle-input-method-active nil
   "Non-nil inside `toggle-input-method'.")
 
+(defun transient-input-method (&optional arg interactive)
+  "Enable transient input method for the current buffer."
+  (interactive "P\np")
+  (when (or arg (not transient-input-method))
+    (let* ((default (or (car input-method-history) default-input-method))
+           (input-method
+            (read-input-method-name
+             (if default "Transient input method (default %s): " "Transient input method: ")
+             default t)))
+      (setq transient-input-method input-method)
+      (when interactive
+        (customize-mark-as-set 'transient-input-method))))
+  (let* ((previous-input-method current-input-method)
+         (history input-method-history)
+         (clearfun (make-symbol "clear-transient-input-method"))
+         (exitfun
+          (lambda ()
+            (deactivate-input-method)
+            (when previous-input-method
+              (activate-input-method previous-input-method))
+            (setq input-method-history history)
+            (remove-hook 'input-method-after-insert-chunk-hook clearfun))))
+    (fset clearfun (lambda () (funcall exitfun)))
+    (add-hook 'input-method-after-insert-chunk-hook clearfun)
+    (when previous-input-method
+      (deactivate-input-method))
+    (activate-input-method transient-input-method)
+    exitfun))
+
 (defun toggle-input-method (&optional arg interactive)
   "Enable or disable multilingual text input method for the current buffer.
 Only one input method can be enabled at any time in a given buffer.