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

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

[nongnu] elpa/iedit 345431294a 294/301: Unify prefix augument handling


From: ELPA Syncer
Subject: [nongnu] elpa/iedit 345431294a 294/301: Unify prefix augument handling
Date: Mon, 10 Jan 2022 22:59:11 -0500 (EST)

branch: elpa/iedit
commit 345431294a9d346e34eec08acff41c8abae369ab
Author: Victor Ren <victorhge@gmail.com>
Commit: Victor Ren <victorhge@gmail.com>

    Unify prefix augument handling
    
    `iedit-mode-from-isearch' can be called with the same prefix argument as
    `iedit-mode'.
---
 iedit-lib.el   |  3 +-
 iedit-tests.el | 45 +++++++++++++++++------------
 iedit.el       | 91 ++++++++++++++++++++++++++++++++--------------------------
 3 files changed, 79 insertions(+), 60 deletions(-)

diff --git a/iedit-lib.el b/iedit-lib.el
index 0818eab9ed..cca5b952e8 100644
--- a/iedit-lib.el
+++ b/iedit-lib.el
@@ -3,7 +3,7 @@
 
 ;; Copyright (C) 2010 - 2019, 2020, 2021 Victor Ren
 
-;; Time-stamp: <2021-08-12 15:17:29 Victor Ren>
+;; Time-stamp: <2021-12-23 18:18:46 Victor Ren>
 ;; Author: Victor Ren <victorhge@gmail.com>
 ;; Keywords: occurrence region simultaneous rectangle refactoring
 ;; Version: 0.9.9.9
@@ -413,6 +413,7 @@ Return the start position of the new occurrence if 
successful."
         ))
     pos))
 
+;; todo: this function is not used yet.
 (defun iedit-add-region-as-occurrence (beg end)
   "Add region as an occurrence.
 The length of the region must the same as other occurrences if
diff --git a/iedit-tests.el b/iedit-tests.el
index f6888650da..d3fc19834b 100644
--- a/iedit-tests.el
+++ b/iedit-tests.el
@@ -2,7 +2,7 @@
 
 ;; Copyright (C) 2010 - 2019, 2020 Victor Ren
 
-;; Time-stamp: <2021-08-12 13:50:27 Victor Ren>
+;; Time-stamp: <2021-12-23 18:19:15 Victor Ren>
 ;; Author: Victor Ren <victorhge@gmail.com>
 ;; Version: 0.9.9.9
 ;; X-URL: https://github.com/victorhge/iedit
@@ -213,24 +213,33 @@ fob")))))
 
 (ert-deftest iedit-mode-start-from-isearch-test ()
   (with-iedit-test-fixture
-"foo
-  foo
-   barfoo
-   foo"
+"a
+(defun foo (foo bar foo)
+\"foo bar foobar\" nil)
+ (defun bar (bar foo bar)
+  \"bar foo barfoo\" nil)
+foo
+ foo"
    (lambda ()
-     (should (= 3 (length iedit-occurrences-overlays)))
-     (should (string= iedit-initial-string-local "foo"))
-     (iedit-mode)
-     (forward-line 2)
-     (isearch-mode t)
-     (isearch-process-search-char ?f)
-     (isearch-process-search-char ?o)
-     (isearch-process-search-char ?o)
-     (call-interactively 'iedit-mode-from-isearch)
-     (should (string= iedit-initial-string-local "foo"))
-     (should (= 4 (length iedit-occurrences-overlays)))
-     (iedit-mode)
-     (should (null iedit-occurrences-overlays)))))
+      (iedit-mode)
+      (emacs-lisp-mode)
+      (goto-char 5)
+      (iedit-mode)
+      (isearch-mode t)
+      (isearch-process-search-char ?f)
+      (isearch-process-search-char ?o)
+      (isearch-process-search-char ?o)
+      (iedit-mode-from-isearch 0)
+      (should (string= iedit-initial-string-local "foo"))
+      (should (= 5 (length iedit-occurrences-overlays)))
+         (iedit-mode)
+         (isearch-mode t)
+      (isearch-process-search-char ?f)
+      (isearch-process-search-char ?o)
+      (isearch-process-search-char ?o)
+      (iedit-mode-from-isearch)
+         (should (= 10 (length iedit-occurrences-overlays)))
+         )))
 
 (ert-deftest iedit-mode-start-from-isearch-regexp-test ()
   (with-iedit-test-fixture
diff --git a/iedit.el b/iedit.el
index 4d4777a17f..6ac2b3713a 100755
--- a/iedit.el
+++ b/iedit.el
@@ -2,7 +2,7 @@
 
 ;; Copyright (C) 2010 - 2019, 2020, 2021 Victor Ren
 
-;; Time-stamp: <2021-08-05 15:18:16 Victor Ren>
+;; Time-stamp: <2021-12-23 18:27:46 Victor Ren>
 ;; Author: Victor Ren <victorhge@gmail.com>
 ;; Keywords: occurrence region simultaneous refactoring
 ;; Version: 0.9.9.9
@@ -118,7 +118,7 @@ isearch-mode-map, esc-map and help-map."
 (defvar iedit-mode nil) ;; Name of the minor mode
 
 (defcustom iedit-auto-narrow nil
-  "If no-nil, the buffer is narrowed temporairily if iedit-mode
+  "If no-nil, the buffer is narrowed temporarily if iedit-mode
 is enabled on current defun."
   :type 'boolean
   :group 'iedit)
@@ -392,11 +392,7 @@ Keymap used within overlays:
   (if iedit-mode
       (iedit-mode-on-action arg)
     (iedit-barf-if-lib-active)
-    (let (occurrence
-          (beg (if (eq major-mode 'occur-edit-mode) ; skip the first occurrence
-                   (next-single-char-property-change 1 'read-only)
-                 (point-min)))
-          (end (point-max)))
+    (let (occurrence beg end)
       ;; Get the occurrence and iedit-occurrence-type-local
       (cond ((and arg
                   (= 4 (prefix-numeric-value arg))
@@ -415,26 +411,7 @@ Keymap used within overlays:
                (unless occurrence
                  (error "No candidate of the occurrence, cannot enable Iedit 
mode"))))
       ;; Get the scope
-      (when arg
-        (cond ((= 0 (prefix-numeric-value arg))
-               (save-excursion
-                ;; Since Emacs 26.1, `mark-defun' marks the next defun if the
-                ;; mark is active.
-                (deactivate-mark t)
-                 (mark-defun)
-                 (setq beg (region-beginning))
-                 (setq end (region-end)))
-              (when (and iedit-auto-narrow (not (buffer-narrowed-p)))
-                (narrow-to-region beg end)
-                (setq iedit-is-narrowed t)))
-              ((and (= 1 (prefix-numeric-value arg))
-                    (not (iedit-region-active)))
-               (let ((region (bounds-of-thing-at-point 'symbol)))
-                 (setq beg (car region))
-                 (setq end (cdr region))))
-              ((iedit-region-active)
-                (setq beg (region-beginning))
-                (setq end (region-end)))))
+         (cl-multiple-value-setq (beg end) (iedit--get-scope arg))
       (setq mark-active nil)
       (run-hooks 'deactivate-mark-hook)
       (setq iedit-initial-string-local occurrence)
@@ -445,31 +422,34 @@ Keymap used within overlays:
 
 (unless (boundp 'isearch-regexp-function)
   (defvaralias 'isearch-regexp-function 'isearch-word))
-(defun iedit-mode-from-isearch (regexp)
+
+(defun iedit-mode-from-isearch (&optional arg)
   "Start Iedit mode using last search string as the regexp."
-  (interactive
-   (let ((regexp (cond
-                 ((functionp isearch-regexp-function)
+  (interactive "P")
+  (or isearch-success
+      (error "No match" ))
+  (let ((regexp (cond
+                                 ((functionp isearch-regexp-function)
                    (funcall isearch-regexp-function isearch-string))
                   (isearch-regexp-function (word-search-regexp isearch-string))
                   (isearch-regexp isearch-string)
-                  (t (regexp-quote isearch-string)))))
-     (list regexp)))
-  (or isearch-success
-      (error "No match" ))
-  (if (or isearch-regexp isearch-regexp-function)
-      nil
-    (setq iedit-initial-string-local isearch-string))
-  (let ((iedit-case-sensitive (not isearch-case-fold-search))
-       result)
+                  (t (regexp-quote isearch-string))))
+               (iedit-case-sensitive (not isearch-case-fold-search))
+               result
+               beg
+               end)
+       (if (or isearch-regexp isearch-regexp-function)
+               nil
+      (setq iedit-initial-string-local isearch-string))
     (isearch-exit)
+       (cl-multiple-value-setq (beg end) (iedit--get-scope arg))
     (setq mark-active nil)
     (run-hooks 'deactivate-mark-hook)
     (when iedit-mode
       (iedit-lib-cleanup))
     (setq result
          (catch 'not-same-length
-           (iedit-start regexp (point-min) (point-max))))
+           (iedit-start regexp beg end)))
     (cond ((not iedit-occurrences-overlays)
            (message "No matches found for %s" regexp)
            (iedit-done))
@@ -477,6 +457,35 @@ Keymap used within overlays:
            (message "Matches are not the same length.")
            (iedit-done)))))
 
+(defun iedit--get-scope (arg)
+  "Return a region according to the prefix argument."
+  (let ((beg (if (eq major-mode 'occur-edit-mode) ; skip the first occurrence
+                                (next-single-char-property-change 1 'read-only)
+               (point-min)))
+               (end (point-max)))
+       (when arg
+         (cond
+          ((= 0 (prefix-numeric-value arg))
+               (save-excursion
+                 ;; Since Emacs 26.1, `mark-defun' marks the next defun if the
+                 ;; mark is active.
+                 (deactivate-mark t)
+          (mark-defun)
+          (setq beg (region-beginning))
+          (setq end (region-end)))
+               (when (and iedit-auto-narrow (not (buffer-narrowed-p)))
+                 (narrow-to-region beg end)
+                 (setq iedit-is-narrowed t)))
+          ((and (= 1 (prefix-numeric-value arg))
+             (not (iedit-region-active)))
+               (let ((region (bounds-of-thing-at-point 'symbol)))
+          (setq beg (car region))
+          (setq end (cdr region))))
+          ((iedit-region-active)
+               (setq beg (region-beginning))
+               (setq end (region-end)))))
+       (list beg end)))
+
 (defun iedit-start (occurrence-regexp beg end)
   "Start Iedit mode for the `occurrence-regexp' in the current buffer."
   (setq iedit-initial-region (list beg end))



reply via email to

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