comparison lisp/replace.el @ 56355:ac4a6e4361cf

(query-replace-descr): New fun. (query-replace-read-from, query-replace-read-args): Default to the last from&to. (query-replace-read-to): Quote the `from' string when displaying it. (query-replace-regexp-eval): Immediately check read-only status. Use query-replace-read-from to get the \n checking. Quote the `from' string when displaying it. (map-query-replace-regexp, occur-read-primary-args): Quote the `from' string when displaying it.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Tue, 06 Jul 2004 00:06:05 +0000
parents d073da76f0a5
children a9b695d281d4 6f6e9fe4658b
comparison
equal deleted inserted replaced
56354:000c36fb46cd 56355:ac4a6e4361cf
1 ;;; replace.el --- replace commands for Emacs 1 ;;; replace.el --- replace commands for Emacs
2 2
3 ;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002, 3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996, 1997, 2000, 2001, 2002,
4 ;; 2003, 2004 Free Software Foundation, Inc. 4 ;; 2003, 2004 Free Software Foundation, Inc.
5 5
6 ;; Maintainer: FSF 6 ;; Maintainer: FSF
7 7
8 ;; This file is part of GNU Emacs. 8 ;; This file is part of GNU Emacs.
9 9
62 "*Non-nil means `query-replace' and friends ignore read-only matches." 62 "*Non-nil means `query-replace' and friends ignore read-only matches."
63 :type 'boolean 63 :type 'boolean
64 :group 'matching 64 :group 'matching
65 :version "21.4") 65 :version "21.4")
66 66
67 (defun query-replace-descr (string)
68 (mapconcat 'isearch-text-char-description string ""))
69
67 (defun query-replace-read-from (string regexp-flag) 70 (defun query-replace-read-from (string regexp-flag)
68 "Query and return the `from' argument of a query-replace operation." 71 "Query and return the `from' argument of a query-replace operation.
72 The return value can also be a pair (FROM . TO) indicating that the user
73 wants to replace FROM with TO."
69 (if query-replace-interactive 74 (if query-replace-interactive
70 (car (if regexp-flag regexp-search-ring search-ring)) 75 (car (if regexp-flag regexp-search-ring search-ring))
71 (let* ((from 76 (let* ((lastfrom (car (symbol-value query-replace-from-history-variable)))
77 (lastto (car (symbol-value query-replace-to-history-variable)))
78 (from
72 ;; The save-excursion here is in case the user marks and copies 79 ;; The save-excursion here is in case the user marks and copies
73 ;; a region in order to specify the minibuffer input. 80 ;; a region in order to specify the minibuffer input.
74 ;; That should not clobber the region for the query-replace itself. 81 ;; That should not clobber the region for the query-replace itself.
75 (save-excursion 82 (save-excursion
83 (when (equal lastfrom lastto)
84 ;; Typically, this is because the two histlists are shared.
85 (setq lastfrom (cadr (symbol-value
86 query-replace-from-history-variable))))
76 (read-from-minibuffer 87 (read-from-minibuffer
77 (format "%s: " string) 88 (if (and lastto lastfrom)
89 (format "%s (default %s -> %s): " string
90 (query-replace-descr lastfrom)
91 (query-replace-descr lastto))
92 (format "%s: " string))
78 nil nil nil 93 nil nil nil
79 query-replace-from-history-variable 94 query-replace-from-history-variable
80 nil t)))) 95 nil t))))
81 ;; Warn if user types \n or \t, but don't reject the input. 96 (if (and (zerop (length from)) lastto lastfrom)
82 (and regexp-flag 97 (cons lastfrom lastto)
83 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) 98 ;; Warn if user types \n or \t, but don't reject the input.
84 (let ((match (match-string 3 from))) 99 (and regexp-flag
85 (cond 100 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from)
86 ((string= match "\\n") 101 (let ((match (match-string 3 from)))
87 (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) 102 (cond
88 ((string= match "\\t") 103 ((string= match "\\n")
89 (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) 104 (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
90 (sit-for 2))) 105 ((string= match "\\t")
91 from))) 106 (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
107 (sit-for 2)))
108 from))))
92 109
93 (defun query-replace-read-to (from string regexp-flag) 110 (defun query-replace-read-to (from string regexp-flag)
94 "Query and return the `from' argument of a query-replace operation." 111 "Query and return the `from' argument of a query-replace operation."
95 (let ((to (save-excursion 112 (let ((to (save-excursion
96 (read-from-minibuffer 113 (read-from-minibuffer
97 (format "%s %s with: " string from) 114 (format "%s %s with: " string (query-replace-descr from))
98 nil nil nil 115 nil nil nil
99 query-replace-to-history-variable from t)))) 116 query-replace-to-history-variable from t))))
100 (when (and regexp-flag 117 (when (and regexp-flag
101 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to)) 118 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to))
102 (let (pos list char) 119 (let (pos list char)
135 152
136 (defun query-replace-read-args (string regexp-flag &optional noerror) 153 (defun query-replace-read-args (string regexp-flag &optional noerror)
137 (unless noerror 154 (unless noerror
138 (barf-if-buffer-read-only)) 155 (barf-if-buffer-read-only))
139 (let* ((from (query-replace-read-from string regexp-flag)) 156 (let* ((from (query-replace-read-from string regexp-flag))
140 (to (query-replace-read-to from string regexp-flag))) 157 (to (if (consp from) (prog1 (cdr from) (setq from (car from)))
158 (query-replace-read-to from string regexp-flag))))
141 (list from to current-prefix-arg))) 159 (list from to current-prefix-arg)))
142 160
143 (defun query-replace (from-string to-string &optional delimited start end) 161 (defun query-replace (from-string to-string &optional delimited start end)
144 "Replace some occurrences of FROM-STRING with TO-STRING. 162 "Replace some occurrences of FROM-STRING with TO-STRING.
145 As each match is found, the user must type a character saying 163 As each match is found, the user must type a character saying
267 285
268 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace 286 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
269 only matches that are surrounded by word boundaries. 287 only matches that are surrounded by word boundaries.
270 Fourth and fifth arg START and END specify the region to operate on." 288 Fourth and fifth arg START and END specify the region to operate on."
271 (interactive 289 (interactive
272 (let* ((from (if query-replace-interactive 290 (barf-if-buffer-read-only)
273 (car regexp-search-ring) 291 (let* ((from
274 (read-from-minibuffer "Query replace regexp: " 292 ;; Let-bind the history var to disable the "foo -> bar" default.
275 nil nil nil 293 ;; Maybe we shouldn't disable this default, but for now I'll
276 query-replace-from-history-variable 294 ;; leave it off. --Stef
277 nil t))) 295 (let ((query-replace-to-history-variable nil))
296 (query-replace-read-from "Query replace regexp" t)))
278 (to (list (read-from-minibuffer 297 (to (list (read-from-minibuffer
279 (format "Query replace regexp %s with eval: " from) 298 (format "Query replace regexp %s with eval: "
299 (query-replace-descr from))
280 nil nil t query-replace-to-history-variable from t)))) 300 nil nil t query-replace-to-history-variable from t))))
281 ;; We make TO a list because replace-match-string-symbols requires one, 301 ;; We make TO a list because replace-match-string-symbols requires one,
282 ;; and the user might enter a single token. 302 ;; and the user might enter a single token.
283 (replace-match-string-symbols to) 303 (replace-match-string-symbols to)
284 (list from (car to) current-prefix-arg 304 (list from (car to) current-prefix-arg
315 (read-from-minibuffer "Map query replace (regexp): " 335 (read-from-minibuffer "Map query replace (regexp): "
316 nil nil nil 336 nil nil nil
317 'query-replace-history nil t))) 337 'query-replace-history nil t)))
318 (to (read-from-minibuffer 338 (to (read-from-minibuffer
319 (format "Query replace %s with (space-separated strings): " 339 (format "Query replace %s with (space-separated strings): "
320 from) 340 (query-replace-descr from))
321 nil nil nil 341 nil nil nil
322 'query-replace-history from t))) 342 'query-replace-history from t)))
323 (list from to 343 (list from to
324 (and current-prefix-arg 344 (and current-prefix-arg
325 (prefix-numeric-value current-prefix-arg)) 345 (prefix-numeric-value current-prefix-arg))
758 (list (let* ((default (car regexp-history)) 778 (list (let* ((default (car regexp-history))
759 (input 779 (input
760 (read-from-minibuffer 780 (read-from-minibuffer
761 (if default 781 (if default
762 (format "List lines matching regexp (default `%s'): " 782 (format "List lines matching regexp (default `%s'): "
763 default) 783 (query-replace-descr default))
764 "List lines matching regexp: ") 784 "List lines matching regexp: ")
765 nil 785 nil
766 nil 786 nil
767 nil 787 nil
768 'regexp-history))) 788 'regexp-history)))
1536 (setq replace-overlay (make-overlay start end)) 1556 (setq replace-overlay (make-overlay start end))
1537 (overlay-put replace-overlay 'face 1557 (overlay-put replace-overlay 'face
1538 (if (facep 'query-replace) 1558 (if (facep 'query-replace)
1539 'query-replace 'region))))) 1559 'query-replace 'region)))))
1540 1560
1541 ;;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4 1561 ;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4
1542 ;;; replace.el ends here 1562 ;;; replace.el ends here