aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/replace.el
diff options
context:
space:
mode:
authorRichard M. Stallman1994-09-20 04:26:12 +0000
committerRichard M. Stallman1994-09-20 04:26:12 +0000
commit151270f331b4d9f3581861aecb8ce43685cf6a81 (patch)
treed13b5e4bc351dd48c068fbf7f980bdaceb73b177 /lisp/replace.el
parent53ea491aa51386be2ce042efcfee47880dcc66f0 (diff)
downloademacs-151270f331b4d9f3581861aecb8ce43685cf6a81.tar.gz
emacs-151270f331b4d9f3581861aecb8ce43685cf6a81.zip
(query-replace-interactive): New user option.
(query-replace-read-args): Obey that option--fetch from search-ring or regexp-search-ring. New arg regexp-flag. (query-replace, query-replace-regexp, replace-string) (replace-regexp): Pass new arg to query-replace-read-args. (map-query-replace-regexp): Obey query-replace-interactive.
Diffstat (limited to 'lisp/replace.el')
-rw-r--r--lisp/replace.el48
1 files changed, 37 insertions, 11 deletions
diff --git a/lisp/replace.el b/lisp/replace.el
index 7c6b8ea257d..01bf44155e3 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -30,11 +30,17 @@
30 30
31(defvar query-replace-history nil) 31(defvar query-replace-history nil)
32 32
33(defun query-replace-read-args (string) 33(defvar query-replace-interactive nil
34 "Non-nil means `query-replace' uses the last search string.
35That becomes the \"string to replace\".")
36
37(defun query-replace-read-args (string regexp-flag)
34 (let (from to) 38 (let (from to)
35 (setq from (read-from-minibuffer (format "%s: " string) 39 (if query-replace-interactive
36 nil nil nil 40 (setq from (car (if regexp-flag regexp-search-ring search-ring)))
37 'query-replace-history)) 41 (setq from (read-from-minibuffer (format "%s: " string)
42 nil nil nil
43 'query-replace-history)))
38 (setq to (read-from-minibuffer (format "%s %s with: " string from) 44 (setq to (read-from-minibuffer (format "%s %s with: " string from)
39 nil nil nil 45 nil nil nil
40 'query-replace-history)) 46 'query-replace-history))
@@ -45,13 +51,17 @@
45As each match is found, the user must type a character saying 51As each match is found, the user must type a character saying
46what to do with it. For directions, type \\[help-command] at that time. 52what to do with it. For directions, type \\[help-command] at that time.
47 53
54If `query-replace-interactive' is non-nil, the last incremental search
55string is used as FROM-STRING--you don't have to specify it with the
56minibuffer.
57
48Preserves case in each replacement if `case-replace' and `case-fold-search' 58Preserves case in each replacement if `case-replace' and `case-fold-search'
49are non-nil and FROM-STRING has no uppercase letters. 59are non-nil and FROM-STRING has no uppercase letters.
50Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 60Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
51only matches surrounded by word boundaries. 61only matches surrounded by word boundaries.
52 62
53To customize possible responses, change the \"bindings\" in `query-replace-map'." 63To customize possible responses, change the \"bindings\" in `query-replace-map'."
54 (interactive (query-replace-read-args "Query replace")) 64 (interactive (query-replace-read-args "Query replace" nil))
55 (perform-replace from-string to-string t nil arg) 65 (perform-replace from-string to-string t nil arg)
56 (or unread-command-events (message "Done"))) 66 (or unread-command-events (message "Done")))
57(define-key esc-map "%" 'query-replace) 67(define-key esc-map "%" 'query-replace)
@@ -61,6 +71,10 @@ To customize possible responses, change the \"bindings\" in `query-replace-map'.
61As each match is found, the user must type a character saying 71As each match is found, the user must type a character saying
62what to do with it. For directions, type \\[help-command] at that time. 72what to do with it. For directions, type \\[help-command] at that time.
63 73
74If `query-replace-interactive' is non-nil, the last incremental search
75regexp is used as REGEXP--you don't have to specify it with the
76minibuffer.
77
64Preserves case in each replacement if `case-replace' and `case-fold-search' 78Preserves case in each replacement if `case-replace' and `case-fold-search'
65are non-nil and REGEXP has no uppercase letters. 79are non-nil and REGEXP has no uppercase letters.
66Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 80Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
@@ -68,7 +82,7 @@ only matches surrounded by word boundaries.
68In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, 82In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
69and `\\=\\N' (where N is a digit) stands for 83and `\\=\\N' (where N is a digit) stands for
70 whatever what matched the Nth `\\(...\\)' in REGEXP." 84 whatever what matched the Nth `\\(...\\)' in REGEXP."
71 (interactive (query-replace-read-args "Query replace regexp")) 85 (interactive (query-replace-read-args "Query replace regexp" t))
72 (perform-replace regexp to-string t t arg) 86 (perform-replace regexp to-string t t arg)
73 (or unread-command-events (message "Done"))) 87 (or unread-command-events (message "Done")))
74 88
@@ -81,13 +95,18 @@ wrapping around from the last such string to the first.
81 95
82Non-interactively, TO-STRINGS may be a list of replacement strings. 96Non-interactively, TO-STRINGS may be a list of replacement strings.
83 97
98If `query-replace-interactive' is non-nil, the last incremental search
99regexp is used as REGEXP--you don't have to specify it with the minibuffer.
100
84A prefix argument N says to use each replacement string N times 101A prefix argument N says to use each replacement string N times
85before rotating to the next." 102before rotating to the next."
86 (interactive 103 (interactive
87 (let (from to) 104 (let (from to)
88 (setq from (read-from-minibuffer "Map query replace (regexp): " 105 (setq from (if query-replace-interactive
89 nil nil nil 106 (car regexp-search-ring)
90 'query-replace-history)) 107 (read-from-minibuffer "Map query replace (regexp): "
108 nil nil nil
109 'query-replace-history)))
91 (setq to (read-from-minibuffer 110 (setq to (read-from-minibuffer
92 (format "Query replace %s with (space-separated strings): " 111 (format "Query replace %s with (space-separated strings): "
93 from) 112 from)
@@ -117,12 +136,16 @@ are non-nil and FROM-STRING has no uppercase letters.
117Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 136Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
118only matches surrounded by word boundaries. 137only matches surrounded by word boundaries.
119 138
139If `query-replace-interactive' is non-nil, the last incremental search
140string is used as FROM-STRING--you don't have to specify it with the
141minibuffer.
142
120This function is usually the wrong thing to use in a Lisp program. 143This function is usually the wrong thing to use in a Lisp program.
121What you probably want is a loop like this: 144What you probably want is a loop like this:
122 (while (search-forward FROM-STRING nil t) 145 (while (search-forward FROM-STRING nil t)
123 (replace-match TO-STRING nil t)) 146 (replace-match TO-STRING nil t))
124which will run faster and will not set the mark or print anything." 147which will run faster and will not set the mark or print anything."
125 (interactive (query-replace-read-args "Replace string")) 148 (interactive (query-replace-read-args "Replace string" nil))
126 (perform-replace from-string to-string nil nil delimited) 149 (perform-replace from-string to-string nil nil delimited)
127 (or unread-command-events (message "Done"))) 150 (or unread-command-events (message "Done")))
128 151
@@ -136,12 +159,15 @@ In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
136and `\\=\\N' (where N is a digit) stands for 159and `\\=\\N' (where N is a digit) stands for
137 whatever what matched the Nth `\\(...\\)' in REGEXP. 160 whatever what matched the Nth `\\(...\\)' in REGEXP.
138 161
162If `query-replace-interactive' is non-nil, the last incremental search
163regexp is used as REGEXP--you don't have to specify it with the minibuffer.
164
139This function is usually the wrong thing to use in a Lisp program. 165This function is usually the wrong thing to use in a Lisp program.
140What you probably want is a loop like this: 166What you probably want is a loop like this:
141 (while (re-search-forward REGEXP nil t) 167 (while (re-search-forward REGEXP nil t)
142 (replace-match TO-STRING nil nil)) 168 (replace-match TO-STRING nil nil))
143which will run faster and will not set the mark or print anything." 169which will run faster and will not set the mark or print anything."
144 (interactive (query-replace-read-args "Replace regexp")) 170 (interactive (query-replace-read-args "Replace regexp" t))
145 (perform-replace regexp to-string nil t delimited) 171 (perform-replace regexp to-string nil t delimited)
146 (or unread-command-events (message "Done"))) 172 (or unread-command-events (message "Done")))
147 173