emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 80c0dec 4/5: [org-translate] Allow ogt-insert-glossary-tra


From: Eric Abrahamsen
Subject: [elpa] master 80c0dec 4/5: [org-translate] Allow ogt-insert-glossary-translation to prompt
Date: Sat, 19 Sep 2020 17:21:37 -0400 (EDT)

branch: master
commit 80c0dece59e77efdbcbcdccf3808e5c5e6dcc018
Author: Eric Abrahamsen <eric@ericabrahamsen.net>
Commit: Eric Abrahamsen <eric@ericabrahamsen.net>

    [org-translate] Allow ogt-insert-glossary-translation to prompt
    
    *
    packages/org-translate/org-translate.el (ogt-insert-glossary-translation):
    With a prefix arg, prompt for a term to insert, rather than guessing.
---
 packages/org-translate/org-translate.el | 77 +++++++++++++++++++++------------
 1 file changed, 50 insertions(+), 27 deletions(-)

diff --git a/packages/org-translate/org-translate.el 
b/packages/org-translate/org-translate.el
index e882b4f..1c31915 100644
--- a/packages/org-translate/org-translate.el
+++ b/packages/org-translate/org-translate.el
@@ -105,7 +105,8 @@
 ;; In the translation text, use "C-M-;"
 ;; (`ogt-insert-glossary-translation') to add a translation.  The mode
 ;; will attempt to guess which term you're adding, and suggest
-;; previous translations for that term.
+;; previous translations for that term.  If you don't want it to
+;; guess, use a prefix argument to be prompted.
 
 ;; Bookmarks
 
@@ -674,33 +675,55 @@ prompt the user for STRING."
       (push string (alist-get 'source (gethash id ogt-glossary-table)))))
   (message "Added %s as a glossary term" string))
 
-(defun ogt-insert-glossary-translation ()
-  "Insert a likely translation of the next glossary item."
-  (interactive)
-  (let ((terms-this-segment 1)
-       glossary-id glossary-translation orig this-translation)
+(defun ogt-insert-glossary-translation (prompt)
+  "Insert a likely translation of the next glossary term.
+Guesses the glossary term to insert based on how many terms have
+already been translated in this segment.  Alternately, give a
+prefix arg to be prompted for the term to enter."
+  (interactive "P")
+  (let* ((orig (when prompt
+                (completing-read
+                 "Add translation of: "
+                 (mapcan (lambda (v)
+                           (copy-sequence (alist-get 'source v)))
+                         (hash-table-values ogt-glossary-table))
+                 nil t)))
+        (glossary-id (when orig
+                       (catch 'found
+                         (maphash
+                          (lambda (k v)
+                            (when (member orig (alist-get 'source v))
+                              (throw 'found k)))
+                          ogt-glossary-table))))
+        glossary-translation this-translation)
     (ogt-update-source-location)
-    (save-excursion
-      (while (re-search-backward "\\[\\[trans:"
-                                (save-excursion
-                                  (re-search-backward
-                                   (string ogt-segmentation-character) nil t)
-                                  (point))
-                                t)
-       (cl-incf terms-this-segment))
-      (with-selected-window ogt-source-window
-       (goto-char ogt-probable-source-location)
-       (while (null (zerop terms-this-segment))
-         (re-search-forward org-link-any-re nil t)
-         (when (string-prefix-p "trans:" (match-string 2))
-           (cl-decf terms-this-segment)))
-       (setq orig (match-string-no-properties 3)
-             glossary-id (string-remove-prefix
-                          "trans:" (match-string 2))
-             glossary-translation
-             (alist-get 'translation
-                        (gethash glossary-id ogt-glossary-table)))))
-    (setq this-translation
+    ;; If we didn't prompt, attempt to guess which glossary term
+    ;; should be translated next by counting how many we've already
+    ;; done this segment.
+    (unless (and orig glossary-id)
+      (let ((terms-this-segment 1))
+       (save-excursion
+         (while (re-search-backward
+                 "\\[\\[trans:"
+                 (save-excursion
+                   (re-search-backward
+                    (string ogt-segmentation-character) nil t)
+                   (point))
+                 t)
+           (cl-incf terms-this-segment)))
+       (with-selected-window ogt-source-window
+         (goto-char ogt-probable-source-location)
+         (while (null (zerop terms-this-segment))
+           (re-search-forward org-link-any-re nil t)
+           (when (string-prefix-p "trans:" (match-string 2))
+             (cl-decf terms-this-segment)))
+         (setq orig (match-string-no-properties 3)
+               glossary-id (string-remove-prefix
+                            "trans:" (match-string 2))))))
+    (setq glossary-translation
+         (alist-get 'translation
+                    (gethash glossary-id ogt-glossary-table))
+         this-translation
          (completing-read (format "Translation of %s: " orig)
                           glossary-translation))
     (cl-pushnew



reply via email to

[Prev in Thread] Current Thread [Next in Thread]