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

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

[nongnu] elpa/anzu d4dcd76f47 190/288: Merge pull request #48 from syohe


From: ELPA Syncer
Subject: [nongnu] elpa/anzu d4dcd76f47 190/288: Merge pull request #48 from syohex/improve-history
Date: Thu, 6 Jan 2022 03:58:51 -0500 (EST)

branch: elpa/anzu
commit d4dcd76f47ee1a9073626aaa268b10279a980e76
Merge: 57bad64f1e 1f7ac26191
Author: Syohei YOSHIDA <syohex@gmail.com>
Commit: Syohei YOSHIDA <syohex@gmail.com>

    Merge pull request #48 from syohex/improve-history
    
    Improve history
---
 anzu.el | 95 ++++++++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 58 insertions(+), 37 deletions(-)

diff --git a/anzu.el b/anzu.el
index 8e33bfee9a..e44f4ce221 100644
--- a/anzu.el
+++ b/anzu.el
@@ -130,6 +130,8 @@
 (defvar anzu--last-search-state nil)
 (defvar anzu--last-replaced-count nil)
 (defvar anzu--outside-point nil)
+(defvar anzu--history nil)
+(defvar anzu--query-defaults nil)
 
 (defun anzu--validate-regexp (regexp)
   (condition-case nil
@@ -151,7 +153,7 @@
 
 (defun anzu--transform-input (str)
   (cond ((eq isearch-word 'isearch-symbol-regexp)
-         (setq str (concat "\\_<" str "\\_>")))
+         (setq str (isearch-symbol-regexp str)))
         ((anzu--word-search-p)
          (setq str (regexp-quote str)))
         (t str)))
@@ -221,9 +223,6 @@
 
 (defconst anzu--mode-line-format '(:eval (anzu--update-mode-line)))
 
-(defsubst anzu--emacs25-p ()
-  (>= emacs-major-version 25))
-
 (defsubst anzu--mode-line-not-set-p ()
   (and (listp mode-line-format)
        (member anzu--mode-line-format mode-line-format)))
@@ -300,20 +299,13 @@
           (if use-regexp " regexp" "")
           (if use-region " in region" ""))  )
 
-(defsubst anzu--retrive-from-at-history ()
-  (let ((retrieve-func (if (anzu--emacs25-p) 'caar 'car)))
-    (query-replace-descr (funcall retrieve-func query-replace-defaults))))
-
-(defsubst anzu--retrive-to-at-history ()
-  (let ((retrieve-func (if (anzu--emacs25-p) 'cdar 'cdr)))
-    (query-replace-descr (funcall retrieve-func query-replace-defaults))))
-
 (defun anzu--query-prompt (use-region use-regexp at-cursor)
   (let ((prompt (anzu--query-prompt-base use-region use-regexp)))
-    (if (and query-replace-defaults (not at-cursor))
-        (format "%s (default %s -> %s) " prompt
-                (anzu--retrive-from-at-history)
-                (anzu--retrive-to-at-history))
+    (if (and anzu--query-defaults (not at-cursor))
+        (format "%s (default %s -> %s) "
+                prompt
+                (query-replace-descr (caar anzu--query-defaults))
+                (query-replace-descr (cdar anzu--query-defaults)))
       prompt)))
 
 (defvar anzu--replaced-markers nil)
@@ -376,19 +368,37 @@
             (anzu--count-and-highlight-matched buf input beg end use-regexp
                                                overlay-limit nil)))))))
 
+(defconst anzu--from-to-separator
+  (propertize
+   (or (ignore-errors
+        (if (char-displayable-p ?\u2192) " \u2192 " " -> "))
+       " -> ")
+   'face 'minibuffer-prompt))
+
+(defsubst anzu--separator ()
+  (propertize "\0" 'display anzu--from-to-separator 'separator t))
+
 (defun anzu--check-minibuffer-input (buf beg end use-regexp overlay-limit)
   (let* ((content (minibuffer-contents))
-         (empty-p (string= content ""))
+         (to (when (and (string-match (anzu--separator) content)
+                        (get-text-property (match-beginning 0) 'separator 
content))
+               (substring-no-properties content (match-end 0))))
+         (from (or (and to (substring-no-properties content 0 (match-beginning 
0)))
+                   content))
+         (empty-p (string= from ""))
          (overlayed (if empty-p
                         (setq anzu--cached-count 0)
-                      (anzu--count-and-highlight-matched buf content beg end 
use-regexp
+                      (anzu--count-and-highlight-matched buf from beg end 
use-regexp
                                                          overlay-limit nil))))
     (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))
+      (anzu--search-outside-visible buf from beg end use-regexp))
+    (when to
+      (setq anzu--last-replace-input "")
+      (anzu--append-replaced-string to buf beg end use-regexp overlay-limit 
from))
     (setq anzu--total-matched anzu--cached-count)
     (force-mode-line-update)))
 
@@ -398,9 +408,19 @@
       (when (overlay-get ov 'anzu-replace)
         (delete-overlay ov)))))
 
+(defun anzu--transform-from-to-history ()
+  (let ((separator (anzu--separator)))
+    (append (mapcar (lambda (from-to)
+                      (concat (query-replace-descr (car from-to))
+                              separator
+                              (query-replace-descr (cdr from-to))))
+                    anzu--query-defaults)
+            (symbol-value query-replace-from-history-variable))))
+
 (defun anzu--read-from-string (prompt beg end use-regexp overlay-limit)
   (let ((curbuf (current-buffer))
         (blink-matching-paren nil)
+        (anzu--history (anzu--transform-from-to-history))
         timer is-input)
     (unwind-protect
         (minibuffer-with-setup-hook
@@ -415,8 +435,7 @@
                                  (anzu--check-minibuffer-input
                                   curbuf beg end use-regexp overlay-limit))))))
           (prog1 (read-from-minibuffer (format "%s: " prompt)
-                                       nil nil nil
-                                       query-replace-from-history-variable nil 
t)
+                                       nil nil nil 'anzu--history nil t)
             (setq is-input t)))
       (when timer
         (cancel-timer timer)
@@ -437,12 +456,12 @@
 (defun anzu--query-from-string (prompt beg end use-regexp overlay-limit)
   (let* ((from (anzu--read-from-string prompt beg end use-regexp 
overlay-limit))
          (is-empty (string= from "")))
-    (when (and (not is-empty) (not query-replace-defaults))
+    (when (and (not is-empty) (not anzu--query-defaults))
       (setq anzu--last-replaced-count anzu--total-matched))
-    (if (and is-empty query-replace-defaults)
-        (cons (anzu--retrive-from-at-history)
+    (if (and is-empty anzu--query-defaults)
+        (cons (query-replace-descr (caar anzu--query-defaults))
               (query-replace-compile-replacement
-               (anzu--retrive-to-at-history) use-regexp))
+               (query-replace-descr (cdar anzu--query-defaults)) use-regexp))
       (add-to-history query-replace-from-history-variable from nil t)
       (when use-regexp
         (anzu--query-validate-from-regexp from))
@@ -494,9 +513,8 @@
     (when (string-match str from)
       (replace-match replaced (not case-fold-search) nil str))))
 
-(defun anzu--append-replaced-string (buf beg end use-regexp overlay-limit from)
-  (let ((content (minibuffer-contents))
-        (replacements 0))
+(defun anzu--append-replaced-string (content buf beg end use-regexp 
overlay-limit from)
+  (let ((replacements 0))
     (unless (string= content anzu--last-replace-input)
       (setq anzu--last-replace-input content)
       (with-current-buffer buf
@@ -537,6 +555,7 @@
                                (with-selected-window (or 
(active-minibuffer-window)
                                                          (minibuffer-window))
                                  (anzu--append-replaced-string
+                                  (minibuffer-contents)
                                   curbuf beg end use-regexp overlay-limit 
from))))))
           (prog1 (read-from-minibuffer to-prompt
                                        nil nil nil
@@ -552,9 +571,7 @@
   (query-replace-compile-replacement
    (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)
-     (if (anzu--emacs25-p)
-         (add-to-history 'query-replace-defaults (cons from to) nil t)
-       (setq query-replace-defaults (cons from to)))
+     (add-to-history 'anzu--query-defaults (cons from to) nil t)
      to)
    use-regexp))
 
@@ -677,12 +694,16 @@
                            (setq delimited nil)
                            (anzu--query-from-at-cursor curbuf beg end 
overlay-limit))
                        (anzu--query-from-string prompt beg end use-regexp 
overlay-limit)))
-               (to (if (consp from)
-                       (prog1 (cdr from)
-                         (setq from (car from)
-                               anzu--total-matched anzu--last-replaced-count))
-                     (anzu--query-replace-read-to
-                      from prompt beg end use-regexp overlay-limit))))
+               (to (cond ((string-match "\0" from)
+                          (prog1 (substring-no-properties from (match-end 0))
+                            (setq from (substring-no-properties from 0 
(match-beginning 0)))))
+                         ((consp from)
+                          (prog1 (cdr from)
+                            (setq from (car from)
+                                  anzu--total-matched 
anzu--last-replaced-count)))
+                         (t
+                          (anzu--query-replace-read-to
+                           from prompt beg end use-regexp overlay-limit)))))
           (anzu--clear-overlays curbuf beg end)
           (anzu--set-replaced-markers from beg end use-regexp)
           (setq anzu--state 'replace anzu--current-position 0



reply via email to

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