aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/replace.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/replace.el')
-rw-r--r--lisp/replace.el110
1 files changed, 80 insertions, 30 deletions
diff --git a/lisp/replace.el b/lisp/replace.el
index 3373ee8e512..001f7d1a78d 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -33,6 +33,22 @@
33 :type 'boolean 33 :type 'boolean
34 :group 'matching) 34 :group 'matching)
35 35
36(defcustom replace-lax-whitespace nil
37 "Non-nil means `query-replace' matches a sequence of whitespace chars.
38When you enter a space or spaces in the strings to be replaced,
39it will match any sequence matched by the regexp `search-whitespace-regexp'."
40 :type 'boolean
41 :group 'matching
42 :version "24.3")
43
44(defcustom replace-regexp-lax-whitespace nil
45 "Non-nil means `query-replace-regexp' matches a sequence of whitespace chars.
46When you enter a space or spaces in the regexps to be replaced,
47it will match any sequence matched by the regexp `search-whitespace-regexp'."
48 :type 'boolean
49 :group 'matching
50 :version "24.3")
51
36(defvar query-replace-history nil 52(defvar query-replace-history nil
37 "Default history list for query-replace commands. 53 "Default history list for query-replace commands.
38See `query-replace-from-history-variable' and 54See `query-replace-from-history-variable' and
@@ -226,6 +242,10 @@ letters. \(Transferring the case pattern means that if the old text
226matched is all caps, or capitalized, then its replacement is upcased 242matched is all caps, or capitalized, then its replacement is upcased
227or capitalized.) 243or capitalized.)
228 244
245If `replace-lax-whitespace' is non-nil, a space or spaces in the string
246to be replaced will match a sequence of whitespace chars defined by the
247regexp in `search-whitespace-regexp'.
248
229Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 249Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
230only matches surrounded by word boundaries. 250only matches surrounded by word boundaries.
231Fourth and fifth arg START and END specify the region to operate on. 251Fourth and fifth arg START and END specify the region to operate on.
@@ -270,6 +290,10 @@ pattern of the old text to the new text, if `case-replace' and
270all caps, or capitalized, then its replacement is upcased or 290all caps, or capitalized, then its replacement is upcased or
271capitalized.) 291capitalized.)
272 292
293If `replace-regexp-lax-whitespace' is non-nil, a space or spaces in the regexp
294to be replaced will match a sequence of whitespace chars defined by the
295regexp in `search-whitespace-regexp'.
296
273Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 297Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
274only matches surrounded by word boundaries. 298only matches surrounded by word boundaries.
275Fourth and fifth arg START and END specify the region to operate on. 299Fourth and fifth arg START and END specify the region to operate on.
@@ -346,6 +370,10 @@ minibuffer.
346Preserves case in each replacement if `case-replace' and `case-fold-search' 370Preserves case in each replacement if `case-replace' and `case-fold-search'
347are non-nil and REGEXP has no uppercase letters. 371are non-nil and REGEXP has no uppercase letters.
348 372
373If `replace-regexp-lax-whitespace' is non-nil, a space or spaces in the regexp
374to be replaced will match a sequence of whitespace chars defined by the
375regexp in `search-whitespace-regexp'.
376
349Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 377Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
350only matches that are surrounded by word boundaries. 378only matches that are surrounded by word boundaries.
351Fourth and fifth arg START and END specify the region to operate on." 379Fourth and fifth arg START and END specify the region to operate on."
@@ -437,6 +465,10 @@ are non-nil and FROM-STRING has no uppercase letters.
437\(Preserving case means that if the string matched is all caps, or capitalized, 465\(Preserving case means that if the string matched is all caps, or capitalized,
438then its replacement is upcased or capitalized.) 466then its replacement is upcased or capitalized.)
439 467
468If `replace-lax-whitespace' is non-nil, a space or spaces in the string
469to be replaced will match a sequence of whitespace chars defined by the
470regexp in `search-whitespace-regexp'.
471
440In Transient Mark mode, if the mark is active, operate on the contents 472In Transient Mark mode, if the mark is active, operate on the contents
441of the region. Otherwise, operate from point to the end of the buffer. 473of the region. Otherwise, operate from point to the end of the buffer.
442 474
@@ -475,6 +507,10 @@ and TO-STRING is also null.)"
475Preserve case in each match if `case-replace' and `case-fold-search' 507Preserve case in each match if `case-replace' and `case-fold-search'
476are non-nil and REGEXP has no uppercase letters. 508are non-nil and REGEXP has no uppercase letters.
477 509
510If `replace-regexp-lax-whitespace' is non-nil, a space or spaces in the regexp
511to be replaced will match a sequence of whitespace chars defined by the
512regexp in `search-whitespace-regexp'.
513
478In Transient Mark mode, if the mark is active, operate on the contents 514In Transient Mark mode, if the mark is active, operate on the contents
479of the region. Otherwise, operate from point to the end of the buffer. 515of the region. Otherwise, operate from point to the end of the buffer.
480 516
@@ -1589,14 +1625,28 @@ E to edit the replacement string"
1589 (define-key map "?" 'help) 1625 (define-key map "?" 'help)
1590 (define-key map "\C-g" 'quit) 1626 (define-key map "\C-g" 'quit)
1591 (define-key map "\C-]" 'quit) 1627 (define-key map "\C-]" 'quit)
1592 (define-key map "\e" 'exit-prefix) 1628 (define-key map "\C-v" 'scroll-up)
1629 (define-key map "\M-v" 'scroll-down)
1630 (define-key map [next] 'scroll-up)
1631 (define-key map [prior] 'scroll-down)
1632 (define-key map [?\C-\M-v] 'scroll-other-window)
1633 (define-key map [M-next] 'scroll-other-window)
1634 (define-key map [?\C-\M-\S-v] 'scroll-other-window-down)
1635 (define-key map [M-prior] 'scroll-other-window-down)
1636 ;; Binding ESC would prohibit the M-v binding. Instead, callers
1637 ;; should check for ESC specially.
1638 ;; (define-key map "\e" 'exit-prefix)
1593 (define-key map [escape] 'exit-prefix) 1639 (define-key map [escape] 'exit-prefix)
1594 map) 1640 map)
1595 "Keymap that defines the responses to questions in `query-replace'. 1641 "Keymap of responses to questions posed by commands like `query-replace'.
1596The \"bindings\" in this map are not commands; they are answers. 1642The \"bindings\" in this map are not commands; they are answers.
1597The valid answers include `act', `skip', `act-and-show', 1643The valid answers include `act', `skip', `act-and-show',
1598`exit', `act-and-exit', `edit', `edit-replacement', `delete-and-edit', 1644`act-and-exit', `exit', `exit-prefix', `recenter', `scroll-up',
1599`recenter', `automatic', `backup', `exit-prefix', `quit', and `help'.") 1645`scroll-down', `scroll-other-window', `scroll-other-window-down',
1646`edit', `edit-replacement', `delete-and-edit', `automatic',
1647`backup', `quit', and `help'.
1648
1649This keymap is used by `y-or-n-p' as well as `query-replace'.")
1600 1650
1601(defvar multi-query-replace-map 1651(defvar multi-query-replace-map
1602 (let ((map (make-sparse-keymap))) 1652 (let ((map (make-sparse-keymap)))
@@ -1717,12 +1767,12 @@ passed in. If LITERAL is set, no checking is done, anyway."
1717 (replace-match newtext fixedcase literal) 1767 (replace-match newtext fixedcase literal)
1718 noedit) 1768 noedit)
1719 1769
1720(defvar replace-search-function 'search-forward 1770(defvar replace-search-function nil
1721 "Function to use when searching for strings to replace. 1771 "Function to use when searching for strings to replace.
1722It is used by `query-replace' and `replace-string', and is called 1772It is used by `query-replace' and `replace-string', and is called
1723with three arguments, as if it were `search-forward'.") 1773with three arguments, as if it were `search-forward'.")
1724 1774
1725(defvar replace-re-search-function 're-search-forward 1775(defvar replace-re-search-function nil
1726 "Function to use when searching for regexps to replace. 1776 "Function to use when searching for regexps to replace.
1727It is used by `query-replace-regexp', `replace-regexp', 1777It is used by `query-replace-regexp', `replace-regexp',
1728`query-replace-regexp-eval', and `map-query-replace-regexp'. 1778`query-replace-regexp-eval', and `map-query-replace-regexp'.
@@ -1755,9 +1805,18 @@ make, or the user didn't cancel the call."
1755 (nocasify (not (and case-replace case-fold-search))) 1805 (nocasify (not (and case-replace case-fold-search)))
1756 (literal (or (not regexp-flag) (eq regexp-flag 'literal))) 1806 (literal (or (not regexp-flag) (eq regexp-flag 'literal)))
1757 (search-function 1807 (search-function
1758 (if regexp-flag 1808 (or (if regexp-flag
1759 replace-re-search-function 1809 replace-re-search-function
1760 replace-search-function)) 1810 replace-search-function)
1811 (let ((isearch-regexp regexp-flag)
1812 (isearch-word delimited-flag)
1813 (isearch-lax-whitespace
1814 replace-lax-whitespace)
1815 (isearch-regexp-lax-whitespace
1816 replace-regexp-lax-whitespace)
1817 (isearch-case-fold-search case-fold-search)
1818 (isearch-forward t))
1819 (isearch-search-fun))))
1761 (search-string from-string) 1820 (search-string from-string)
1762 (real-match-data nil) ; The match data for the current match. 1821 (real-match-data nil) ; The match data for the current match.
1763 (next-replacement nil) 1822 (next-replacement nil)
@@ -1811,12 +1870,6 @@ make, or the user didn't cancel the call."
1811 (vector repeat-count repeat-count 1870 (vector repeat-count repeat-count
1812 replacements replacements))))) 1871 replacements replacements)))))
1813 1872
1814 (if delimited-flag
1815 (setq search-function 're-search-forward
1816 search-string (concat "\\b"
1817 (if regexp-flag from-string
1818 (regexp-quote from-string))
1819 "\\b")))
1820 (when query-replace-lazy-highlight 1873 (when query-replace-lazy-highlight
1821 (setq isearch-lazy-highlight-last-string nil)) 1874 (setq isearch-lazy-highlight-last-string nil))
1822 1875
@@ -1898,7 +1951,7 @@ make, or the user didn't cancel the call."
1898 (replace-highlight 1951 (replace-highlight
1899 (nth 0 real-match-data) (nth 1 real-match-data) 1952 (nth 0 real-match-data) (nth 1 real-match-data)
1900 start end search-string 1953 start end search-string
1901 (or delimited-flag regexp-flag) case-fold-search)) 1954 regexp-flag delimited-flag case-fold-search))
1902 (setq noedit 1955 (setq noedit
1903 (replace-match-maybe-edit 1956 (replace-match-maybe-edit
1904 next-replacement nocasify literal 1957 next-replacement nocasify literal
@@ -1917,7 +1970,7 @@ make, or the user didn't cancel the call."
1917 (replace-highlight 1970 (replace-highlight
1918 (match-beginning 0) (match-end 0) 1971 (match-beginning 0) (match-end 0)
1919 start end search-string 1972 start end search-string
1920 (or delimited-flag regexp-flag) case-fold-search) 1973 regexp-flag delimited-flag case-fold-search)
1921 ;; Bind message-log-max so we don't fill up the message log 1974 ;; Bind message-log-max so we don't fill up the message log
1922 ;; with a bunch of identical messages. 1975 ;; with a bunch of identical messages.
1923 (let ((message-log-max nil) 1976 (let ((message-log-max nil)
@@ -2099,15 +2152,11 @@ make, or the user didn't cancel the call."
2099 (if (= replace-count 1) "" "s"))) 2152 (if (= replace-count 1) "" "s")))
2100 (or (and keep-going stack) multi-buffer))) 2153 (or (and keep-going stack) multi-buffer)))
2101 2154
2102(defvar isearch-error)
2103(defvar isearch-forward)
2104(defvar isearch-case-fold-search)
2105(defvar isearch-string)
2106
2107(defvar replace-overlay nil) 2155(defvar replace-overlay nil)
2108 2156
2109(defun replace-highlight (match-beg match-end range-beg range-end 2157(defun replace-highlight (match-beg match-end range-beg range-end
2110 string regexp case-fold) 2158 search-string regexp-flag delimited-flag
2159 case-fold-search)
2111 (if query-replace-highlight 2160 (if query-replace-highlight
2112 (if replace-overlay 2161 (if replace-overlay
2113 (move-overlay replace-overlay match-beg match-end (current-buffer)) 2162 (move-overlay replace-overlay match-beg match-end (current-buffer))
@@ -2115,13 +2164,14 @@ make, or the user didn't cancel the call."
2115 (overlay-put replace-overlay 'priority 1001) ;higher than lazy overlays 2164 (overlay-put replace-overlay 'priority 1001) ;higher than lazy overlays
2116 (overlay-put replace-overlay 'face 'query-replace))) 2165 (overlay-put replace-overlay 'face 'query-replace)))
2117 (if query-replace-lazy-highlight 2166 (if query-replace-lazy-highlight
2118 (let ((isearch-string string) 2167 (let ((isearch-string search-string)
2119 (isearch-regexp regexp) 2168 (isearch-regexp regexp-flag)
2120 ;; Set isearch-word to nil because word-replace is regexp-based, 2169 (isearch-word delimited-flag)
2121 ;; so `isearch-search-fun' should not use `word-search-forward'. 2170 (isearch-lax-whitespace
2122 (isearch-word nil) 2171 replace-lax-whitespace)
2123 (search-whitespace-regexp nil) 2172 (isearch-regexp-lax-whitespace
2124 (isearch-case-fold-search case-fold) 2173 replace-regexp-lax-whitespace)
2174 (isearch-case-fold-search case-fold-search)
2125 (isearch-forward t) 2175 (isearch-forward t)
2126 (isearch-error nil)) 2176 (isearch-error nil))
2127 (isearch-lazy-highlight-new-loop range-beg range-end)))) 2177 (isearch-lazy-highlight-new-loop range-beg range-end))))