about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArun Isaac2022-06-20 12:52:59 +0530
committerArun Isaac2022-06-20 13:03:30 +0530
commit2cd30a0fdaf57ed910479df597e147faed1d9127 (patch)
treeb58427f8d0fd664e7cf19aead2f7513a7bd0bd67
parent1cf8f23fea19a9c055066325fd20a549d8ffc9e3 (diff)
downloadthogai-2cd30a0fdaf57ed910479df597e147faed1d9127.tar.gz
thogai-2cd30a0fdaf57ed910479df597e147faed1d9127.tar.lz
thogai-2cd30a0fdaf57ed910479df597e147faed1d9127.zip
Implement stroke suggestions.
* thogai.el (thogai-suggest): New function.
(thogai-process-stroke): Use thogai-suggest.
-rw-r--r--thogai.el39
1 files changed, 38 insertions, 1 deletions
diff --git a/thogai.el b/thogai.el
index af392a8..efcc506 100644
--- a/thogai.el
+++ b/thogai.el
@@ -480,6 +480,42 @@ STROKES is injected first."
       (ring-insert stroke-ring stroke)
       (thogai-process-stroke stroke-ring))))
 
+(defun thogai-suggest ()
+  "Suggest strokes for current word.
+
+Suggest strokes for current word in the *thogai-suggestions*
+buffer.  Suggestions are sorted shortest first."
+  ;; TODO: Don't cheat by picking up the current word. The current
+  ;; word may be a multi-word phrase.
+  (let ((word (current-word))
+        (suggestions-buffer (get-buffer-create "*thogai-suggestions*")))
+    (when word
+      (with-current-buffer suggestions-buffer
+        ;; Explicitly go to end of buffer, just in case someone messed
+        ;; up the point.
+        (goto-char (point-max))
+        (insert word)
+        (insert "\n")
+        ;; TODO: Suggest orthography-aware suffixes. For example,
+        ;; suggest "SUFBGS/-S" for the word "suffixes", even though
+        ;; "SUFBGS/-S" does not occur in the dictionary.
+        (dolist (stroke (sort (thogai-reverse-lookup word)
+                              (lambda (strokes1 strokes2)
+                                ;; Sort by number of strokes, falling back
+                                ;; to string length.
+                                (let ((count1 (length (split-string strokes1 "/")))
+                                      (count2 (length (split-string strokes2 "/"))))
+                                  (if (= count1 count2)
+                                      (< (length strokes1)
+                                         (length strokes2))
+                                    (< count1 count2))))))
+          (insert (concat "\t" stroke "\n")))
+        (insert "\n")
+        ;; Scroll to the end of windows showing the suggestions
+        ;; buffer.
+        (dolist (window (get-buffer-window-list suggestions-buffer nil t))
+          (set-window-point window (point-max)))))))
+
 (defun thogai-process-stroke (stroke-ring)
   "Process the most recent stroke.
 
@@ -508,7 +544,8 @@ current state, and should typically be `thogai-stroke-ring'."
         (let ((translation (thogai-lookup (string-join matched-strokes "/"))))
           (cond
            ((stringp translation)
-            (thogai-insert-translation translation))
+            (thogai-insert-translation translation)
+            (thogai-suggest))
            ((commandp translation)
             (call-interactively translation))))))))