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

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

[elpa] master d060eea 313/399: swiper.el (swiper-query-replace): Add que


From: Oleh Krehel
Subject: [elpa] master d060eea 313/399: swiper.el (swiper-query-replace): Add query-replace-compile-replacement
Date: Sat, 20 Jul 2019 14:57:47 -0400 (EDT)

branch: master
commit d060eea466de6478291b4adf4f23db1da10e0473
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    swiper.el (swiper-query-replace): Add query-replace-compile-replacement
    
    Fixes #2096
---
 swiper.el | 47 ++++++++++++++++++++++++++++++++++-------------
 1 file changed, 34 insertions(+), 13 deletions(-)

diff --git a/swiper.el b/swiper.el
index 9990664..620416c 100644
--- a/swiper.el
+++ b/swiper.el
@@ -137,8 +137,19 @@
 (defvar swiper--query-replace-overlays nil)
 
 (defun swiper--query-replace-updatefn ()
-  (dolist (ov swiper--query-replace-overlays)
-    (overlay-put ov 'after-string (propertize ivy-text 'face 'error))))
+  (let ((lisp (ignore-errors (nth 2 (query-replace-compile-replacement 
ivy-text t)))))
+    (dolist (ov swiper--query-replace-overlays)
+      (when lisp
+        (dolist (x (overlay-get ov 'matches))
+          (setq lisp (cl-subst (cadr x) (car x) lisp :test #'equal)))
+        (setq lisp (ignore-errors (eval lisp))))
+      (overlay-put
+       ov 'after-string
+       (propertize
+        (if (stringp lisp)
+            lisp
+          ivy-text)
+        'face 'error)))))
 
 (defun swiper--query-replace-cleanup ()
   (while swiper--query-replace-overlays
@@ -151,8 +162,15 @@
       (save-excursion
         (goto-char (window-start))
         (while (re-search-forward re end t)
-          (push (make-overlay (1- (match-end 0)) (match-end 0))
-                swiper--query-replace-overlays))))))
+          (let ((ov (make-overlay (1- (match-end 0)) (match-end 0)))
+                (md (match-data)))
+            (overlay-put
+             ov 'matches
+             (mapcar
+              (lambda (x)
+                (list `(match-string ,x) (match-string x)))
+              (number-sequence 0 (1- (/ (length md) 2)))))
+            (push ov swiper--query-replace-overlays)))))))
 
 (defun swiper-query-replace ()
   "Start `query-replace' with string to replace from last search string."
@@ -166,15 +184,18 @@
          (unwind-protect
               (let* ((enable-recursive-minibuffers t)
                      (from (ivy--regex ivy-text))
-                     (to (minibuffer-with-setup-hook
-                             (lambda ()
-                               (setq minibuffer-default
-                                     (if (string-match 
"\\`\\\\_<\\(.*\\)\\\\_>\\'" ivy-text)
-                                         (match-string 1 ivy-text)
-                                       ivy-text)))
-                           (ivy-read
-                            (format "Query replace %s with: " from) nil
-                            :update-fn #'swiper--query-replace-updatefn))))
+                     (to
+                      (query-replace-compile-replacement
+                       (minibuffer-with-setup-hook
+                           (lambda ()
+                             (setq minibuffer-default
+                                   (if (string-match 
"\\`\\\\_<\\(.*\\)\\\\_>\\'" ivy-text)
+                                       (match-string 1 ivy-text)
+                                     ivy-text)))
+                         (ivy-read
+                          (format "Query replace %s with: " from) nil
+                          :update-fn #'swiper--query-replace-updatefn))
+                       t)))
                 (swiper--cleanup)
                 (ivy-exit-with-action
                  (lambda (_)



reply via email to

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