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

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

[elpa] externals/jinx 5702e733eb 2/9: Refactoring


From: ELPA Syncer
Subject: [elpa] externals/jinx 5702e733eb 2/9: Refactoring
Date: Mon, 11 Sep 2023 12:58:18 -0400 (EDT)

branch: externals/jinx
commit 5702e733ebc76d9d22fcbefd0ffbb617dc821391
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    Refactoring
---
 jinx.el | 160 ++++++++++++++++++++++++++++++++++------------------------------
 1 file changed, 84 insertions(+), 76 deletions(-)

diff --git a/jinx.el b/jinx.el
index 8c0b162925..8f9d39cf19 100644
--- a/jinx.el
+++ b/jinx.el
@@ -586,6 +586,13 @@ If CHECK is non-nil, always check first."
           (setq mod-file (expand-file-name mod-name))))
       (module-load mod-file))))
 
+(defmacro jinx--correct-guard (&rest body)
+  "Guard BODY during correction loop."
+  `(cl-letf (((symbol-function #'jinx--timer-handler) #'ignore) ;; Inhibit
+             (repeat-mode nil)) ;; No repeating of jinx-next and jinx-previous
+     (unless jinx-mode (jinx-mode 1))
+     ,@body))
+
 (defun jinx--correct-highlight (overlay fun)
   "Highlight and show OVERLAY during FUN."
   (declare (indent 1))
@@ -711,30 +718,32 @@ The word will be associated with GROUP and get a prefix 
key."
                    (annotation-function . jinx--correct-annotation))
       (complete-with-action action suggestions str pred))))
 
-(defun jinx--correct (overlay info)
+(defun jinx--correct-overlay (overlay info)
   "Correct word at OVERLAY, maybe show prompt INFO."
-  (let* ((word (buffer-substring-no-properties
-                (overlay-start overlay) (overlay-end overlay)))
-         (choice
-          (jinx--correct-highlight overlay
-            (lambda ()
-              (when (or (< (point) (window-start)) (> (point) (window-end nil 
t)))
-                (recenter))
-              (minibuffer-with-setup-hook
-                  #'jinx--correct-setup
-                (or (completing-read
-                     (format "Correct ‘%s’%s: " word (or info ""))
-                     (jinx--correct-table
-                      (jinx--correct-suggestions word))
-                     nil nil nil t word)
-                    word)))))
-         (len (length choice)))
-    (pcase (and (> len 0) (assq (aref choice 0) jinx--save-keys))
-      (`(,key . ,fun)
-       (funcall fun 'save key (if (> len 1) (substring choice 1) word))
-       (jinx--recheck-overlays))
-      ((guard (not (equal choice word)))
-       (jinx--correct-replace overlay choice)))))
+  (catch 'jinx--goto
+    (let* ((word (buffer-substring-no-properties
+                  (overlay-start overlay) (overlay-end overlay)))
+           (choice
+            (jinx--correct-highlight overlay
+              (lambda ()
+                (when (or (< (point) (window-start)) (> (point) (window-end 
nil t)))
+                  (recenter))
+                (minibuffer-with-setup-hook
+                    #'jinx--correct-setup
+                  (or (completing-read
+                       (format "Correct ‘%s’%s: " word (or info ""))
+                       (jinx--correct-table
+                        (jinx--correct-suggestions word))
+                       nil nil nil t word)
+                      word)))))
+           (len (length choice)))
+      (pcase (and (> len 0) (assq (aref choice 0) jinx--save-keys))
+        (`(,key . ,fun)
+         (funcall fun 'save key (if (> len 1) (substring choice 1) word))
+         (jinx--recheck-overlays))
+        ((guard (not (equal choice word)))
+         (jinx--correct-replace overlay choice)))
+      nil)))
 
 (defun jinx--correct-replace (overlay word)
   "Replace OVERLAY with WORD."
@@ -760,9 +769,8 @@ The word will be associated with GROUP and get a prefix 
key."
   (modify-syntax-entry ?’ "w" jinx--syntax-table)
   (modify-syntax-entry ?. "." jinx--syntax-table))
 
-(defun jinx--bounds-of-word-at-point ()
-  "Return bounds of word at point as a cons cell.
-Using `jinx--syntax-table'."
+(defun jinx--bounds-of-word ()
+  "Return bounds of word at point using `jinx--syntax-table'."
   (save-excursion
     (save-match-data
       (with-syntax-table jinx--syntax-table
@@ -854,62 +862,62 @@ With prefix argument GLOBAL change the languages 
globally."
   (jinx--cleanup))
 
 ;;;###autoload
-(defun jinx-correct (&optional all)
-  "Correct nearest misspelled word.
-If prefix argument ALL non-nil correct all misspellings."
+(defun jinx-correct-all ()
+  "Correct all misspelled words in the buffer."
+  (interactive "*")
+  (jinx--correct-guard
+   (let* ((overlays (jinx--force-overlays (point-min) (point-max) :check t))
+          (count (length overlays))
+          (idx 0))
+     (push-mark)
+     (while (when-let ((ov (nth idx overlays)))
+              (if-let (((overlay-buffer ov))
+                       (skip (jinx--correct-overlay ov (format " (%d of %d)" 
(1+ idx) count))))
+                  (setq idx (mod (+ idx skip) count))
+                (cl-incf idx)))))))
+
+;;;###autoload
+(defun jinx-correct-nearest ()
+  "Correct nearest misspelled word."
+  (interactive "*")
+  (jinx--correct-guard
+   (let* ((old-point (point-marker))
+          (overlays (jinx--force-overlays (window-start) (window-end) :visible 
t))
+          (count (length overlays))
+          (idx 0))
+     (unwind-protect
+         (while (when-let ((ov (nth idx overlays)))
+                  (if (overlay-buffer ov)
+                      (when-let ((skip (jinx--correct-overlay ov nil)))
+                        (setq idx (mod (+ idx skip) count)))
+                    (cl-incf idx)))) ;; Skip deleted overlay
+       (goto-char old-point)))))
+
+;;;###autoload
+(defun jinx-correct (&optional arg)
+  "Correct nearest misspelled word or all misspellings.
+If prefix ARG is non-nil, all misspellings are corrected in a loop."
   (interactive "*P")
-  (unless jinx-mode (jinx-mode 1))
-  (cl-letf* (((symbol-function #'jinx--timer-handler) #'ignore) ;; Inhibit
-             (repeat-mode nil) ;; No repeating of jinx-next and jinx-previous
-             (old-point (point-marker))
-             (overlays
-              (if all
-                  (jinx--force-overlays (point-min) (point-max) :check t)
-                (jinx--force-overlays (window-start) (window-end) :visible t)))
-             (count (length overlays))
-             (idx 0))
-    (when all
-      (push-mark))
-    (unwind-protect
-        (while (when-let ((ov (nth idx overlays)))
-                 (if (not (overlay-buffer ov))
-                     (cl-incf idx) ;; Skip deleted overlay
-                   (let ((skip
-                          (catch 'jinx--goto
-                            (jinx--correct
-                             ov (and all (format " (%d of %d)" (1+ idx) 
count))))))
-                     (cond
-                      ((integerp skip) (setq idx (mod (+ idx skip) count)))
-                      (all (cl-incf idx)))))))
-      (unless all
-        (goto-char old-point)))))
+  (if arg
+      (jinx-correct-all)
+    (jinx-correct-nearest)))
 
 ;;;###autoload
 (defun jinx-correct-at-point (&optional beg end)
-  "Correct a word between BEG and END, defaults to the one at the cursor.
-Suggest corrections even if it's not misspelled."
+  "Correct a word between BEG and END, by default the word under point.
+Suggest corrections even if the word is not misspelled."
   (interactive)
   (unless (and beg end)
-    (setf (cons beg end) (jinx--bounds-of-word-at-point)))
-  (unless jinx-mode
-    (jinx-mode 1))
-  (cl-letf* (((symbol-function #'jinx--timer-handler) #'ignore) ;; Inhibit
-             (repeat-mode nil) ;; No repeating of jinx-next and jinx-previous
-             (old-point (point-marker)))
-    (unwind-protect
-        (while (let ((skip
-                      (catch 'jinx--goto
-                        (if (and beg end)
-                            (let ((ov (make-overlay beg end)))
-                              (jinx--correct ov nil))
-                          (user-error "No word at point")))))
-                 ;; use jinx-next/previous to move among words
-                 (cond
-                  ((integerp skip)
-                   (forward-to-word skip)
-                   (setf (cons beg end) (jinx--bounds-of-word-at-point))
-                   t))))
-      (goto-char old-point))))
+    (setf (cons beg end) (or (jinx--bounds-of-word)
+                             (user-error "No word at point"))))
+  (jinx--correct-guard
+   (let ((old-point (point-marker)))
+     (unwind-protect
+         (while (when-let ((skip (jinx--correct-overlay (make-overlay beg end) 
nil)))
+                  (forward-to-word skip)
+                  (setf (cons beg end) (jinx--bounds-of-word))
+                  beg))
+       (goto-char old-point)))))
 
 (defun jinx-correct-select ()
   "Quick selection key for corrections."



reply via email to

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