aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/replace.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/replace.el')
-rw-r--r--lisp/replace.el193
1 files changed, 102 insertions, 91 deletions
diff --git a/lisp/replace.el b/lisp/replace.el
index 001f7d1a78d..e714015fccf 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -62,6 +62,10 @@ no default value.")
62(defvar query-replace-interactive nil 62(defvar query-replace-interactive nil
63 "Non-nil means `query-replace' uses the last search string. 63 "Non-nil means `query-replace' uses the last search string.
64That becomes the \"string to replace\".") 64That becomes the \"string to replace\".")
65(make-obsolete-variable 'query-replace-interactive
66 "use `M-n' to pull the last incremental search string
67to the minibuffer that reads the string to replace, or invoke replacements
68from Isearch by using a key sequence like `C-s C-s M-%'." "24.3")
65 69
66(defcustom query-replace-from-history-variable 'query-replace-history 70(defcustom query-replace-from-history-variable 'query-replace-history
67 "History list to use for the FROM argument of `query-replace' commands. 71 "History list to use for the FROM argument of `query-replace' commands.
@@ -128,20 +132,22 @@ wants to replace FROM with TO."
128 (if query-replace-interactive 132 (if query-replace-interactive
129 (car (if regexp-flag regexp-search-ring search-ring)) 133 (car (if regexp-flag regexp-search-ring search-ring))
130 (let* ((history-add-new-input nil) 134 (let* ((history-add-new-input nil)
135 (prompt
136 (if query-replace-defaults
137 (format "%s (default %s -> %s): " prompt
138 (query-replace-descr (car query-replace-defaults))
139 (query-replace-descr (cdr query-replace-defaults)))
140 (format "%s: " prompt)))
131 (from 141 (from
132 ;; The save-excursion here is in case the user marks and copies 142 ;; The save-excursion here is in case the user marks and copies
133 ;; a region in order to specify the minibuffer input. 143 ;; a region in order to specify the minibuffer input.
134 ;; That should not clobber the region for the query-replace itself. 144 ;; That should not clobber the region for the query-replace itself.
135 (save-excursion 145 (save-excursion
136 (read-from-minibuffer 146 (if regexp-flag
137 (if query-replace-defaults 147 (read-regexp prompt nil query-replace-from-history-variable)
138 (format "%s (default %s -> %s): " prompt 148 (read-from-minibuffer
139 (query-replace-descr (car query-replace-defaults)) 149 prompt nil nil nil query-replace-from-history-variable
140 (query-replace-descr (cdr query-replace-defaults))) 150 (car (if regexp-flag regexp-search-ring search-ring)) t)))))
141 (format "%s: " prompt))
142 nil nil nil
143 query-replace-from-history-variable
144 nil t))))
145 (if (and (zerop (length from)) query-replace-defaults) 151 (if (and (zerop (length from)) query-replace-defaults)
146 (cons (car query-replace-defaults) 152 (cons (car query-replace-defaults)
147 (query-replace-compile-replacement 153 (query-replace-compile-replacement
@@ -230,9 +236,11 @@ what to do with it. For directions, type \\[help-command] at that time.
230In Transient Mark mode, if the mark is active, operate on the contents 236In Transient Mark mode, if the mark is active, operate on the contents
231of the region. Otherwise, operate from point to the end of the buffer. 237of the region. Otherwise, operate from point to the end of the buffer.
232 238
233If `query-replace-interactive' is non-nil, the last incremental search 239Use \\<minibuffer-local-map>\\[next-history-element] \
234string is used as FROM-STRING--you don't have to specify it with the 240to pull the last incremental search string to the minibuffer
235minibuffer. 241that reads FROM-STRING, or invoke replacements from
242incremental search with a key sequence like `C-s C-s M-%'
243to use its current search string as the string to replace.
236 244
237Matching is independent of case if `case-fold-search' is non-nil and 245Matching is independent of case if `case-fold-search' is non-nil and
238FROM-STRING has no uppercase letters. Replacement transfers the case 246FROM-STRING has no uppercase letters. Replacement transfers the case
@@ -278,9 +286,11 @@ what to do with it. For directions, type \\[help-command] at that time.
278In Transient Mark mode, if the mark is active, operate on the contents 286In Transient Mark mode, if the mark is active, operate on the contents
279of the region. Otherwise, operate from point to the end of the buffer. 287of the region. Otherwise, operate from point to the end of the buffer.
280 288
281If `query-replace-interactive' is non-nil, the last incremental search 289Use \\<minibuffer-local-map>\\[next-history-element] \
282regexp is used as REGEXP--you don't have to specify it with the 290to pull the last incremental search regexp to the minibuffer
283minibuffer. 291that reads REGEXP, or invoke replacements from
292incremental search with a key sequence like `C-M-s C-M-s C-M-%'
293to use its current search regexp as the regexp to replace.
284 294
285Matching is independent of case if `case-fold-search' is non-nil and 295Matching is independent of case if `case-fold-search' is non-nil and
286REGEXP has no uppercase letters. Replacement transfers the case 296REGEXP has no uppercase letters. Replacement transfers the case
@@ -363,9 +373,9 @@ In interactive use, `\\#' in itself stands for `replace-count'.
363In Transient Mark mode, if the mark is active, operate on the contents 373In Transient Mark mode, if the mark is active, operate on the contents
364of the region. Otherwise, operate from point to the end of the buffer. 374of the region. Otherwise, operate from point to the end of the buffer.
365 375
366If `query-replace-interactive' is non-nil, the last incremental search 376Use \\<minibuffer-local-map>\\[next-history-element] \
367regexp is used as REGEXP--you don't have to specify it with the 377to pull the last incremental search regexp to the minibuffer
368minibuffer. 378that reads REGEXP.
369 379
370Preserves case in each replacement if `case-replace' and `case-fold-search' 380Preserves case in each replacement if `case-replace' and `case-fold-search'
371are non-nil and REGEXP has no uppercase letters. 381are non-nil and REGEXP has no uppercase letters.
@@ -377,35 +387,33 @@ regexp in `search-whitespace-regexp'.
377Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 387Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
378only matches that are surrounded by word boundaries. 388only matches that are surrounded by word boundaries.
379Fourth and fifth arg START and END specify the region to operate on." 389Fourth and fifth arg START and END specify the region to operate on."
390 (declare (obsolete "use the `\\,' feature of `query-replace-regexp'
391for interactive calls, and `search-forward-regexp'/`replace-match'
392for Lisp calls." "22.1"))
380 (interactive 393 (interactive
381 (progn 394 (progn
382 (barf-if-buffer-read-only) 395 (barf-if-buffer-read-only)
383 (let* ((from 396 (let* ((from
384 ;; Let-bind the history var to disable the "foo -> bar" default. 397 ;; Let-bind the history var to disable the "foo -> bar"
385 ;; Maybe we shouldn't disable this default, but for now I'll 398 ;; default. Maybe we shouldn't disable this default, but
386 ;; leave it off. --Stef 399 ;; for now I'll leave it off. --Stef
387 (let ((query-replace-to-history-variable nil)) 400 (let ((query-replace-to-history-variable nil))
388 (query-replace-read-from "Query replace regexp" t))) 401 (query-replace-read-from "Query replace regexp" t)))
389 (to (list (read-from-minibuffer 402 (to (list (read-from-minibuffer
390 (format "Query replace regexp %s with eval: " 403 (format "Query replace regexp %s with eval: "
391 (query-replace-descr from)) 404 (query-replace-descr from))
392 nil nil t query-replace-to-history-variable from t)))) 405 nil nil t query-replace-to-history-variable from t))))
393 ;; We make TO a list because replace-match-string-symbols requires one, 406 ;; We make TO a list because replace-match-string-symbols requires one,
394 ;; and the user might enter a single token. 407 ;; and the user might enter a single token.
395 (replace-match-string-symbols to) 408 (replace-match-string-symbols to)
396 (list from (car to) current-prefix-arg 409 (list from (car to) current-prefix-arg
397 (if (and transient-mark-mode mark-active) 410 (if (and transient-mark-mode mark-active)
398 (region-beginning)) 411 (region-beginning))
399 (if (and transient-mark-mode mark-active) 412 (if (and transient-mark-mode mark-active)
400 (region-end)))))) 413 (region-end))))))
401 (perform-replace regexp (cons 'replace-eval-replacement to-expr) 414 (perform-replace regexp (cons 'replace-eval-replacement to-expr)
402 t 'literal delimited nil nil start end)) 415 t 'literal delimited nil nil start end))
403 416
404(make-obsolete 'query-replace-regexp-eval
405 "for interactive use, use the special `\\,' feature of
406`query-replace-regexp' instead. Non-interactively, a loop
407using `search-forward-regexp' and `replace-match' is preferred." "22.1")
408
409(defun map-query-replace-regexp (regexp to-strings &optional n start end) 417(defun map-query-replace-regexp (regexp to-strings &optional n start end)
410 "Replace some matches for REGEXP with various strings, in rotation. 418 "Replace some matches for REGEXP with various strings, in rotation.
411The second argument TO-STRINGS contains the replacement strings, separated 419The second argument TO-STRINGS contains the replacement strings, separated
@@ -418,19 +426,16 @@ of the region. Otherwise, operate from point to the end of the buffer.
418 426
419Non-interactively, TO-STRINGS may be a list of replacement strings. 427Non-interactively, TO-STRINGS may be a list of replacement strings.
420 428
421If `query-replace-interactive' is non-nil, the last incremental search 429Use \\<minibuffer-local-map>\\[next-history-element] \
422regexp is used as REGEXP--you don't have to specify it with the minibuffer. 430to pull the last incremental search regexp to the minibuffer
431that reads REGEXP.
423 432
424A prefix argument N says to use each replacement string N times 433A prefix argument N says to use each replacement string N times
425before rotating to the next. 434before rotating to the next.
426Fourth and fifth arg START and END specify the region to operate on." 435Fourth and fifth arg START and END specify the region to operate on."
427 (interactive 436 (interactive
428 (let* ((from (if query-replace-interactive 437 (let* ((from (read-regexp "Map query replace (regexp): " nil
429 (car regexp-search-ring) 438 query-replace-from-history-variable))
430 (read-from-minibuffer "Map query replace (regexp): "
431 nil nil nil
432 query-replace-from-history-variable
433 nil t)))
434 (to (read-from-minibuffer 439 (to (read-from-minibuffer
435 (format "Query replace %s with (space-separated strings): " 440 (format "Query replace %s with (space-separated strings): "
436 (query-replace-descr from)) 441 (query-replace-descr from))
@@ -476,9 +481,9 @@ Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
476only matches surrounded by word boundaries. 481only matches surrounded by word boundaries.
477Fourth and fifth arg START and END specify the region to operate on. 482Fourth and fifth arg START and END specify the region to operate on.
478 483
479If `query-replace-interactive' is non-nil, the last incremental search 484Use \\<minibuffer-local-map>\\[next-history-element] \
480string is used as FROM-STRING--you don't have to specify it with the 485to pull the last incremental search string to the minibuffer
481minibuffer. 486that reads FROM-STRING.
482 487
483This function is usually the wrong thing to use in a Lisp program. 488This function is usually the wrong thing to use in a Lisp program.
484What you probably want is a loop like this: 489What you probably want is a loop like this:
@@ -541,8 +546,9 @@ When using those Lisp features interactively in the replacement
541text, TO-STRING is actually made a list instead of a string. 546text, TO-STRING is actually made a list instead of a string.
542Use \\[repeat-complex-command] after this command for details. 547Use \\[repeat-complex-command] after this command for details.
543 548
544If `query-replace-interactive' is non-nil, the last incremental search 549Use \\<minibuffer-local-map>\\[next-history-element] \
545regexp is used as REGEXP--you don't have to specify it with the minibuffer. 550to pull the last incremental search regexp to the minibuffer
551that reads REGEXP.
546 552
547This function is usually the wrong thing to use in a Lisp program. 553This function is usually the wrong thing to use in a Lisp program.
548What you probably want is a loop like this: 554What you probably want is a loop like this:
@@ -574,38 +580,47 @@ of `history-length', which see.")
574(defvar occur-collect-regexp-history '("\\1") 580(defvar occur-collect-regexp-history '("\\1")
575 "History of regexp for occur's collect operation") 581 "History of regexp for occur's collect operation")
576 582
577(defun read-regexp (prompt &optional default-value) 583(defun read-regexp (prompt &optional defaults history)
578 "Read regexp as a string using the regexp history and some useful defaults. 584 "Read and return a regular expression as a string.
579Prompt for a regular expression with PROMPT (without a colon and 585When PROMPT doesn't end with a colon and space, it adds a final \": \".
580space) in the minibuffer. The optional argument DEFAULT-VALUE 586If DEFAULTS is non-nil, it displays the first default in the prompt.
581provides the value to display in the minibuffer prompt that is 587
582returned if the user just types RET. 588Non-nil optional arg DEFAULTS is a string or a list of strings that
583Values available via M-n are the string at point, the last isearch 589are prepended to a list of standard default values, which include the
584regexp, the last isearch string, and the last replacement regexp." 590string at point, the last isearch regexp, the last isearch string, and
585 (let* ((defaults 591the last replacement regexp.
586 (list (regexp-quote 592
587 (or (funcall (or find-tag-default-function 593Non-nil HISTORY is a symbol to use for the history list.
588 (get major-mode 'find-tag-default-function) 594If HISTORY is nil, `regexp-history' is used."
589 'find-tag-default)) 595 (let* ((default (if (consp defaults) (car defaults) defaults))
590 "")) 596 (defaults
591 (car regexp-search-ring) 597 (append
592 (regexp-quote (or (car search-ring) "")) 598 (if (listp defaults) defaults (list defaults))
593 (car (symbol-value 599 (list (regexp-quote
594 query-replace-from-history-variable)))) 600 (or (funcall (or find-tag-default-function
601 (get major-mode 'find-tag-default-function)
602 'find-tag-default))
603 ""))
604 (car regexp-search-ring)
605 (regexp-quote (or (car search-ring) ""))
606 (car (symbol-value
607 query-replace-from-history-variable)))))
595 (defaults (delete-dups (delq nil (delete "" defaults)))) 608 (defaults (delete-dups (delq nil (delete "" defaults))))
596 ;; Don't add automatically the car of defaults for empty input 609 ;; Do not automatically add default to the history for empty input.
597 (history-add-new-input nil) 610 (history-add-new-input nil)
598 (input 611 (input (read-from-minibuffer
599 (read-from-minibuffer 612 (cond ((string-match-p ":[ \t]*\\'" prompt)
600 (if default-value 613 prompt)
601 (format "%s (default %s): " prompt 614 (default
602 (query-replace-descr default-value)) 615 (format "%s (default %s): " prompt
603 (format "%s: " prompt)) 616 (query-replace-descr default)))
604 nil nil nil 'regexp-history defaults t))) 617 (t
618 (format "%s: " prompt)))
619 nil nil nil (or history 'regexp-history) defaults t)))
605 (if (equal input "") 620 (if (equal input "")
606 (or default-value input) 621 (or default input)
607 (prog1 input 622 (prog1 input
608 (add-to-history 'regexp-history input))))) 623 (add-to-history (or history 'regexp-history) input)))))
609 624
610 625
611(defalias 'delete-non-matching-lines 'keep-lines) 626(defalias 'delete-non-matching-lines 'keep-lines)
@@ -1130,9 +1145,9 @@ which means to discard all text properties."
1130 "\\&" 1145 "\\&"
1131 ;; Get the regexp for collection pattern. 1146 ;; Get the regexp for collection pattern.
1132 (let ((default (car occur-collect-regexp-history))) 1147 (let ((default (car occur-collect-regexp-history)))
1133 (read-string 1148 (read-regexp
1134 (format "Regexp to collect (default %s): " default) 1149 (format "Regexp to collect (default %s): " default)
1135 nil 'occur-collect-regexp-history default))) 1150 default 'occur-collect-regexp-history)))
1136 ;; Otherwise normal occur takes numerical prefix argument. 1151 ;; Otherwise normal occur takes numerical prefix argument.
1137 (when current-prefix-arg 1152 (when current-prefix-arg
1138 (prefix-numeric-value current-prefix-arg)))))) 1153 (prefix-numeric-value current-prefix-arg))))))
@@ -1219,14 +1234,10 @@ See also `multi-occur'."
1219 (cons 1234 (cons
1220 (let* ((default (car regexp-history)) 1235 (let* ((default (car regexp-history))
1221 (input 1236 (input
1222 (read-from-minibuffer 1237 (read-regexp
1223 (if current-prefix-arg 1238 (if current-prefix-arg
1224 "List lines in buffers whose names match regexp: " 1239 "List lines in buffers whose names match regexp: "
1225 "List lines in buffers whose filenames match regexp: ") 1240 "List lines in buffers whose filenames match regexp: "))))
1226 nil
1227 nil
1228 nil
1229 'regexp-history)))
1230 (if (equal input "") 1241 (if (equal input "")
1231 default 1242 default
1232 input)) 1243 input))