branch: externals/gnosis
commit 3f76dbd71ffc40b6a8502f3470a675413310fe2b
Author: Thanos Apollo <[email protected]>
Commit: Thanos Apollo <[email protected]>

    [Feature] Adjust input-method depending on detected script.
    
    * New custom: gnosis-script-input-method-alist
    Switch to input-method for script detected automatically.
    
    * New gnosis--read-string-with-input-method
    read-string wrapper that adjusts input-method for script.
    
    Add support for this feature in gnosis-review and gnosis-monkeytype.
---
 gnosis-monkeytype.el |  9 ++++++++-
 gnosis-review.el     |  6 ++++--
 gnosis.el            | 24 ++++++++++++++++++++++++
 3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/gnosis-monkeytype.el b/gnosis-monkeytype.el
index 10741f871b..255f5c68e8 100644
--- a/gnosis-monkeytype.el
+++ b/gnosis-monkeytype.el
@@ -31,6 +31,8 @@
 
 (require 'gnosis-utils)
 
+(defvar gnosis-script-input-method-alist)
+
 
 (defface gnosis-monkeytype-face-dimmed
   '((((class color) (background light)) :foreground "grey50")
@@ -131,7 +133,12 @@ Optionally, highlight MISTAKES."
        (switch-to-buffer (get-buffer-create gnosis-monkeytype-buffer-name))
        (goto-char (point-min))
        (add-hook 'after-change-functions #'gnosis-monkeytype--handler nil t)
-       (recursive-edit)
+       (let ((method (alist-get (gnosis-utils-detect-script text)
+                                gnosis-script-input-method-alist)))
+         (when method (activate-input-method method))
+         (unwind-protect
+             (recursive-edit)
+           (when method (deactivate-input-method))))
        (setq gnosis-monkeytype-wpm-result
              (gnosis-monkeytype--calculate-wpm text-formatted start-time))))))
 
diff --git a/gnosis-review.el b/gnosis-review.el
index 99e68cedf7..4318fd08d5 100644
--- a/gnosis-review.el
+++ b/gnosis-review.el
@@ -362,7 +362,7 @@ SUCCESS is a boolean value, t for success, nil for failure."
     (gnosis-display-image keimenon)
     (gnosis-display-keimenon (gnosis-org-format-string keimenon))
     (gnosis-display-hint hypothesis)
-    (let* ((user-input (read-string "Answer: "))
+    (let* ((user-input (gnosis--read-string-with-input-method "Answer: " 
answer))
           (success (gnosis-compare-strings answer user-input)))
       (gnosis-display-basic-answer answer success user-input)
       (gnosis-display-parathema parathema)
@@ -376,7 +376,9 @@ CLOZES is a list of possible correct answers.
 
 Returns a cons; ='(position . user-input) if correct,
 ='(nil . user-input) if incorrect."
-  (let* ((user-input (or user-input (read-string "Answer: ")))
+  (let* ((user-input (or user-input
+                         (gnosis--read-string-with-input-method
+                          "Answer: " (car clozes))))
          (position (cl-position user-input clozes :test 
#'gnosis-compare-strings)))
     (cons position user-input)))
 
diff --git a/gnosis.el b/gnosis.el
index cb4ba991c4..73b44feadb 100644
--- a/gnosis.el
+++ b/gnosis.el
@@ -128,6 +128,16 @@ When non-nil, center content during review sessions.
 When nil, content will be displayed left-aligned instead of centered."
   :type 'boolean)
 
+(defcustom gnosis-script-input-method-alist
+  '((greek . "greek")
+    (cyrillic . "cyrillic-translit"))
+  "Alist mapping script symbols to input method names.
+Each element is (SCRIPT . INPUT-METHOD) where SCRIPT is a symbol
+from `char-script-table' and INPUT-METHOD is a string suitable for
+`activate-input-method'."
+  :type '(alist :key-type symbol :value-type string)
+  :group 'gnosis)
+
 (defvar-local gnosis-center-content t
   "Buffer-local variable controlling content centering.
 
@@ -730,6 +740,20 @@ This function should be used in combination with
         (<= (string-distance normalized-str1 normalized-str2) 
gnosis-string-difference)
       (string= normalized-str1 normalized-str2))))
 
+(defun gnosis--read-string-with-input-method (prompt answer)
+  "Read string with PROMPT, activating input method matching ANSWER's script.
+Activates the input method in the current buffer so `read-string' with
+INHERIT-INPUT-METHOD propagates it into the minibuffer.  Restores the
+previous state on exit."
+  (let ((method (alist-get (gnosis-utils-detect-script answer)
+                           gnosis-script-input-method-alist)))
+    (if (not method)
+        (read-string prompt)
+      (activate-input-method method)
+      (unwind-protect
+          (read-string prompt nil nil nil t)
+        (deactivate-input-method)))))
+
 (defun gnosis-get-tags--unique ()
   "Return a list of unique strings for tags in `gnosis-db'."
   (cl-loop for tags in (apply 'append

Reply via email to