summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuri Linkov <juri@linkov.net>2022-09-04 19:55:59 +0300
committerJuri Linkov <juri@linkov.net>2022-09-04 19:55:59 +0300
commit40cdbf6dc133214c34da40cd7cc097dc39c05c3f (patch)
treeb5ed834d1f8e2b54e9ad7897cf317c42c26de310
parentad6878005dbde428809c1f587555238892dd253d (diff)
downloademacs-40cdbf6dc133214c34da40cd7cc097dc39c05c3f.tar.gz
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.
-rw-r--r--lisp/isearch.el3
-rw-r--r--lisp/replace.el45
-rw-r--r--lisp/simple.el6
-rw-r--r--lisp/textmodes/paragraphs.el6
4 files changed, 38 insertions, 22 deletions
diff --git a/lisp/isearch.el b/lisp/isearch.el
index 9f1fbb14a4a..2ef35438e91 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 2bb9c1b90dc..06cde771b9e 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 2512397b240..60f2ad34528 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 cd726ad4776..c500dc014fb 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