emacs-diffs
[Top][All Lists]
Advanced

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

master 40cdbf6dc1: Use use-region-beginning/end in replacement commands


From: Juri Linkov
Subject: master 40cdbf6dc1: Use use-region-beginning/end in replacement commands (bug#45607)
Date: Sun, 4 Sep 2022 12:56:54 -0400 (EDT)

branch: master
commit 40cdbf6dc133214c34da40cd7cc097dc39c05c3f
Author: Juri Linkov <juri@linkov.net>
Commit: Juri Linkov <juri@linkov.net>

    Use use-region-beginning/end in replacement commands (bug#45607)
    
    * lisp/isearch.el (isearch-query-replace): Use use-region-beginning
    and use-region-end.
    
    * lisp/textmodes/paragraphs.el (repunctuate-sentences):
    * lisp/replace.el (query-replace, query-replace-regexp)
    (map-query-replace-regexp, replace-string, replace-regexp):
    Add 'interactive-args' to 'declare' and use use-region-beginning,
    use-region-end, use-region-noncontiguous-p.
    
    * lisp/simple.el (use-region-noncontiguous-p): New function.
    (region-noncontiguous-p): Return more meaningful value.
---
 lisp/isearch.el              |  3 +--
 lisp/replace.el              | 45 ++++++++++++++++++++++++++++----------------
 lisp/simple.el               |  6 +++++-
 lisp/textmodes/paragraphs.el |  6 +++---
 4 files changed, 38 insertions(+), 22 deletions(-)

diff --git a/lisp/isearch.el b/lisp/isearch.el
index 9f1fbb14a4..2ef35438e9 100644
--- a/lisp/isearch.el
+++ b/lisp/isearch.el
@@ -2389,8 +2389,7 @@ type \\[help-command] at that time."
              (if (use-region-p) " in region" ""))
       isearch-regexp)
      t isearch-regexp (or delimited isearch-regexp-function) nil nil
-     (if (use-region-p) (region-beginning))
-     (if (use-region-p) (region-end))
+     (use-region-beginning) (use-region-end)
      backward))
   (and isearch-recursive-edit (exit-recursive-edit)))
 
diff --git a/lisp/replace.el b/lisp/replace.el
index 2bb9c1b90d..06cde771b9 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -448,6 +448,10 @@ Arguments FROM-STRING, TO-STRING, DELIMITED, START, END, 
BACKWARD, and
 REGION-NONCONTIGUOUS-P are passed to `perform-replace' (which see).
 
 To customize possible responses, change the bindings in `query-replace-map'."
+  (declare (interactive-args
+           (start (use-region-beginning))
+           (end (use-region-end))
+           (region-noncontiguous-p (use-region-noncontiguous-p))))
   (interactive
    (let ((common
          (query-replace-read-args
@@ -461,10 +465,9 @@ To customize possible responses, change the bindings in 
`query-replace-map'."
           ;; These are done separately here
           ;; so that command-history will record these expressions
           ;; rather than the values they had this time.
-          (if (use-region-p) (region-beginning))
-          (if (use-region-p) (region-end))
+          (use-region-beginning) (use-region-end)
           (nth 3 common)
-          (if (use-region-p) (region-noncontiguous-p)))))
+          (use-region-noncontiguous-p))))
   (perform-replace from-string to-string t nil delimited nil nil start end 
backward region-noncontiguous-p))
 
 (define-key esc-map "%" 'query-replace)
@@ -541,6 +544,10 @@ Use \\[repeat-complex-command] after this command for 
details.
 
 Arguments REGEXP, TO-STRING, DELIMITED, START, END, BACKWARD, and
 REGION-NONCONTIGUOUS-P are passed to `perform-replace' (which see)."
+  (declare (interactive-args
+           (start (use-region-beginning))
+           (end (use-region-end))
+           (region-noncontiguous-p (use-region-noncontiguous-p))))
   (interactive
    (let ((common
          (query-replace-read-args
@@ -555,10 +562,9 @@ REGION-NONCONTIGUOUS-P are passed to `perform-replace' 
(which see)."
           ;; These are done separately here
           ;; so that command-history will record these expressions
           ;; rather than the values they had this time.
-          (if (use-region-p) (region-beginning))
-          (if (use-region-p) (region-end))
+          (use-region-beginning) (use-region-end)
           (nth 3 common)
-          (if (use-region-p) (region-noncontiguous-p)))))
+          (use-region-noncontiguous-p))))
   (perform-replace regexp to-string t t delimited nil nil start end backward 
region-noncontiguous-p))
 
 (define-key esc-map [?\C-%] 'query-replace-regexp)
@@ -592,6 +598,10 @@ Fourth and fifth arg START and END specify the region to 
operate on.
 
 Arguments REGEXP, START, END, and REGION-NONCONTIGUOUS-P are passed to
 `perform-replace' (which see)."
+  (declare (interactive-args
+           (start (use-region-beginning))
+           (end (use-region-end))
+           (region-noncontiguous-p (use-region-noncontiguous-p))))
   (interactive
    (let* ((from (read-regexp "Map query replace (regexp): " nil
                             query-replace-from-history-variable))
@@ -603,9 +613,8 @@ Arguments REGEXP, START, END, and REGION-NONCONTIGUOUS-P 
are passed to
      (list from to
           (and current-prefix-arg
                (prefix-numeric-value current-prefix-arg))
-          (if (use-region-p) (region-beginning))
-          (if (use-region-p) (region-end))
-          (if (use-region-p) (region-noncontiguous-p)))))
+          (use-region-beginning) (use-region-end)
+          (use-region-noncontiguous-p))))
   (let (replacements)
     (if (listp to-strings)
        (setq replacements to-strings)
@@ -665,9 +674,10 @@ which will run faster and will not set the mark or print 
anything.
 and TO-STRING is also null.)"
   (declare (interactive-only
            "use `search-forward' and `replace-match' instead.")
-           (interactive-args
+          (interactive-args
            (start (use-region-beginning))
-           (end (use-region-end))))
+           (end (use-region-end))
+           (region-noncontiguous-p (use-region-noncontiguous-p))))
   (interactive
    (let ((common
          (query-replace-read-args
@@ -681,7 +691,7 @@ and TO-STRING is also null.)"
      (list (nth 0 common) (nth 1 common) (nth 2 common)
           (use-region-beginning) (use-region-end)
           (nth 3 common)
-          (if (use-region-p) (region-noncontiguous-p)))))
+          (use-region-noncontiguous-p))))
   (perform-replace from-string to-string nil nil delimited nil nil start end 
backward region-noncontiguous-p))
 
 (defun replace-regexp (regexp to-string &optional delimited start end backward 
region-noncontiguous-p)
@@ -746,7 +756,11 @@ What you probably want is a loop like this:
     (replace-match TO-STRING nil nil))
 which will run faster and will not set the mark or print anything."
   (declare (interactive-only
-           "use `re-search-forward' and `replace-match' instead."))
+           "use `re-search-forward' and `replace-match' instead.")
+          (interactive-args
+           (start (use-region-beginning))
+           (end (use-region-end))
+           (region-noncontiguous-p (use-region-noncontiguous-p))))
   (interactive
    (let ((common
          (query-replace-read-args
@@ -758,10 +772,9 @@ which will run faster and will not set the mark or print 
anything."
                   (if (use-region-p) " in region" ""))
           t)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
-          (if (use-region-p) (region-beginning))
-          (if (use-region-p) (region-end))
+          (use-region-beginning) (use-region-end)
           (nth 3 common)
-          (if (use-region-p) (region-noncontiguous-p)))))
+          (use-region-noncontiguous-p))))
   (perform-replace regexp to-string nil t delimited nil nil start end backward 
region-noncontiguous-p))
 
 
diff --git a/lisp/simple.el b/lisp/simple.el
index 2512397b24..60f2ad3452 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -6878,6 +6878,10 @@ point otherwise."
   "Return the end of the region if `use-region-p'."
   (and (use-region-p) (region-end)))
 
+(defun use-region-noncontiguous-p ()
+  "Return non-nil for a non-contiguous region if `use-region-p'."
+  (and (use-region-p) (region-noncontiguous-p)))
+
 (defun use-region-p ()
   "Return t if the region is active and it is appropriate to act on it.
 This is used by commands that act specially on the region under
@@ -6922,7 +6926,7 @@ see `region-noncontiguous-p' and 
`extract-rectangle-bounds'."
   "Return non-nil if the region contains several pieces.
 An example is a rectangular region handled as a list of
 separate contiguous regions for each line."
-  (cdr (region-bounds)))
+  (let ((bounds (region-bounds))) (and (cdr bounds) bounds)))
 
 (defun redisplay--unhighlight-overlay-function (rol)
   "If ROL is an overlay, call `delete-overlay'."
diff --git a/lisp/textmodes/paragraphs.el b/lisp/textmodes/paragraphs.el
index cd726ad477..c500dc014f 100644
--- a/lisp/textmodes/paragraphs.el
+++ b/lisp/textmodes/paragraphs.el
@@ -514,9 +514,9 @@ Second and third arg START and END specify the region to 
operate on.
 If optional argument NO-QUERY is non-nil, make changes without asking
 for confirmation.  You can use `repunctuate-sentences-filter' to add
 filters to skip occurrences of spaces that don't need to be replaced."
-  (interactive (list nil
-                     (if (use-region-p) (region-beginning))
-                     (if (use-region-p) (region-end))))
+  (declare (interactive-args (start (use-region-beginning))
+                             (end (use-region-end))))
+  (interactive (list nil (use-region-beginning) (use-region-end)))
   (let ((regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +")
         (to-string "\\1\\2\\3  "))
     (if no-query



reply via email to

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