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

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

[nongnu] elpa/anzu 0bdbfcb209 138/288: Improve case when input does not


From: ELPA Syncer
Subject: [nongnu] elpa/anzu 0bdbfcb209 138/288: Improve case when input does not match anything in visiable range(#30)
Date: Thu, 6 Jan 2022 03:58:47 -0500 (EST)

branch: elpa/anzu
commit 0bdbfcb2091bbffbd858a4dafc012a4a13af3c52
Author: Syohei YOSHIDA <syohex@gmail.com>
Commit: Syohei YOSHIDA <syohex@gmail.com>

    Improve case when input does not match anything in visiable range(#30)
---
 anzu.el | 85 ++++++++++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 61 insertions(+), 24 deletions(-)

diff --git a/anzu.el b/anzu.el
index 969db8d357..90fd5f98c4 100644
--- a/anzu.el
+++ b/anzu.el
@@ -131,6 +131,7 @@
 (defvar anzu--cached-count 0)
 (defvar anzu--last-replace-input "")
 (defvar anzu--last-search-state nil)
+(defvar anzu--outside-point nil)
 
 (defun anzu--validate-regexp (regexp)
   (condition-case nil
@@ -304,7 +305,9 @@
     (overlay-put ov 'face 'anzu-replace-highlight)
     (overlay-put ov 'anzu-replace t)))
 
-(defun anzu--count-matched (buf str replace-beg replace-end use-regexp 
overlay-limit)
+;; Return highlighted count
+(defun anzu--count-and-highlight-matched (buf str replace-beg replace-end
+                                          use-regexp overlay-limit)
   (when (not use-regexp)
     (setq str (regexp-quote str)))
   (if (not (anzu--validate-regexp str))
@@ -315,6 +318,7 @@
                (overlay-end (min replace-end overlay-limit)))
           (goto-char overlay-beg)
           (let ((count 0)
+                (overlayed 0)
                 (finish nil)
                 (case-fold-search (anzu--case-fold-search str)))
             (while (and (not finish) (re-search-forward str replace-end t))
@@ -326,15 +330,33 @@
                       (setq finish t)
                     (forward-char 1)))
                 (when (and (>= beg overlay-beg) (<= end overlay-end) (not 
finish))
+                  (cl-incf overlayed)
                   (anzu--add-overlay str beg end))))
-            (setq anzu--cached-count count)))))))
+            (setq anzu--cached-count count)
+            overlayed))))))
+
+(defun anzu--search-outside-visible (buf input beg end use-regexp)
+  (let ((searchfn (if use-regexp 're-search-forward 'search-forward)))
+    (with-selected-window (get-buffer-window buf)
+      (goto-char beg)
+      (when (funcall searchfn input end t)
+        (setq anzu--outside-point (match-beginning 0))
+        (let ((overlay-limit (anzu--overlay-limit)))
+          (anzu--count-and-highlight-matched buf input beg end use-regexp 
overlay-limit))))))
 
 (defun anzu--check-minibuffer-input (buf beg end use-regexp overlay-limit)
   (let* ((content (minibuffer-contents))
-         (matched (if (string= content "")
-                      (setq anzu--cached-count 0)
-                    (anzu--count-matched buf content beg end use-regexp 
overlay-limit))))
-    (setq anzu--total-matched matched)
+         (empty-p (string= content ""))
+         (overlayed (if empty-p
+                        (setq anzu--cached-count 0)
+                      (anzu--count-and-highlight-matched buf content beg end 
use-regexp overlay-limit))))
+    (when anzu--outside-point
+      (setq anzu--outside-point nil)
+      (with-selected-window (get-buffer-window buf)
+        (goto-char beg)))
+    (when (and (not empty-p) (zerop overlayed))
+      (anzu--search-outside-visible buf content beg end use-regexp))
+    (setq anzu--total-matched anzu--cached-count)
     (force-mode-line-update)))
 
 (defun anzu--clear-overlays (buf beg end)
@@ -345,7 +367,7 @@
 
 (defun anzu--read-from-string (prompt beg end use-regexp overlay-limit)
   (let ((curbuf (current-buffer))
-        (timer nil))
+        timer is-input)
     (unwind-protect
         (minibuffer-with-setup-hook
             #'(lambda ()
@@ -358,12 +380,15 @@
                                                          (minibuffer-window))
                                  (anzu--check-minibuffer-input
                                   curbuf beg end use-regexp overlay-limit))))))
-          (read-from-minibuffer (format "%s: " prompt)
-                                nil nil nil
-                                query-replace-from-history-variable nil t))
+          (prog1 (read-from-minibuffer (format "%s: " prompt)
+                                       nil nil nil
+                                       query-replace-from-history-variable nil 
t)
+            (setq is-input t)))
       (when timer
         (cancel-timer timer)
-        (setq timer nil)))))
+        (setq timer nil)
+        (unless is-input
+          (goto-char beg))))))
 
 (defun anzu--query-validate-from-regexp (from)
   (when (string-match "\\(?:\\`\\|[^\\]\\)\\(?:\\\\\\\\\\)*\\(\\\\[nt]\\)" 
from)
@@ -437,12 +462,22 @@
               (setq replace-evaled content))
             (overlay-put ov 'after-string (anzu--propertize-to-string 
replace-evaled))))))))
 
+(defsubst anzu--outside-overlay-limit (orig-beg orig-limit)
+  (save-excursion
+    (goto-char (+ anzu--outside-point (- orig-limit orig-beg)))
+    (line-end-position)))
+
 (defun anzu--read-to-string (from prompt beg end use-regexp overlay-limit)
   (let ((curbuf (current-buffer))
-        (timer nil)
+        (orig-beg beg)
         (to-prompt (format "%s %s with: " prompt (query-replace-descr from)))
-        (history-add-new-input nil))
+        (history-add-new-input nil)
+        timer is-input)
     (setq anzu--last-replace-input "")
+    (when anzu--outside-point
+      (setq beg anzu--outside-point
+            overlay-limit (anzu--outside-overlay-limit orig-beg overlay-limit)
+            anzu--outside-point nil))
     (unwind-protect
         (minibuffer-with-setup-hook
             #'(lambda ()
@@ -454,20 +489,22 @@
                                                          (minibuffer-window))
                                  (anzu--append-replaced-string
                                   curbuf beg end use-regexp overlay-limit))))))
-          (read-from-minibuffer to-prompt
-                                nil nil nil
-                                query-replace-from-history-variable nil t))
+          (prog1 (read-from-minibuffer to-prompt
+                                       nil nil nil
+                                       query-replace-from-history-variable nil 
t)
+            (setq is-input t)))
       (when timer
         (cancel-timer timer)
-        (setq timer nil)))))
+        (setq timer nil)
+        (unless is-input
+          (goto-char orig-beg))))))
 
 (defun anzu--query-replace-read-to (from prompt beg end use-regexp 
overlay-limit)
   (query-replace-compile-replacement
-   (save-excursion
-     (let ((to (anzu--read-to-string from prompt beg end use-regexp 
overlay-limit)))
-       (add-to-history query-replace-to-history-variable to nil t)
-       (setq query-replace-defaults (cons from to))
-       to))
+   (let ((to (anzu--read-to-string from prompt beg end use-regexp 
overlay-limit)))
+     (add-to-history query-replace-to-history-variable to nil t)
+     (setq query-replace-defaults (cons from to))
+     to)
    use-regexp))
 
 (defun anzu--overlay-limit ()
@@ -481,8 +518,8 @@
     (unless symbol
       (error "No symbol at cursor!!"))
     (let ((symbol-regexp (concat "\\_<" (regexp-quote symbol) "\\_>")))
-      (setq anzu--total-matched
-            (anzu--count-matched buf symbol-regexp beg end t overlay-limit))
+      (anzu--count-and-highlight-matched buf symbol-regexp beg end t 
overlay-limit)
+      (setq anzu--total-matched anzu--cached-count)
       (force-mode-line-update)
       symbol-regexp)))
 



reply via email to

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