1 |
;;; replace.el --- replace commands for Emacs |
;;; replace.el --- replace commands for Emacs |
2 |
|
|
3 |
;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002, |
;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996, 1997, 2000, 2001, 2002, |
4 |
;; 2003, 2004 Free Software Foundation, Inc. |
;; 2003, 2004 Free Software Foundation, Inc. |
5 |
|
|
6 |
;; Maintainer: FSF |
;; Maintainer: FSF |
7 |
|
|
64 |
:group 'matching |
:group 'matching |
65 |
:version "21.4") |
:version "21.4") |
66 |
|
|
67 |
|
(defun query-replace-descr (string) |
68 |
|
(mapconcat 'isearch-text-char-description string "")) |
69 |
|
|
70 |
(defun query-replace-read-from (string regexp-flag) |
(defun query-replace-read-from (string regexp-flag) |
71 |
"Query and return the `from' argument of a query-replace operation." |
"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." |
74 |
(if query-replace-interactive |
(if query-replace-interactive |
75 |
(car (if regexp-flag regexp-search-ring search-ring)) |
(car (if regexp-flag regexp-search-ring search-ring)) |
76 |
(let* ((from |
(let* ((lastfrom (car (symbol-value query-replace-from-history-variable))) |
77 |
|
(lastto (car (symbol-value query-replace-to-history-variable))) |
78 |
|
(from |
79 |
;; The save-excursion here is in case the user marks and copies |
;; The save-excursion here is in case the user marks and copies |
80 |
;; a region in order to specify the minibuffer input. |
;; a region in order to specify the minibuffer input. |
81 |
;; That should not clobber the region for the query-replace itself. |
;; That should not clobber the region for the query-replace itself. |
82 |
(save-excursion |
(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)))) |
87 |
(read-from-minibuffer |
(read-from-minibuffer |
88 |
(format "%s: " string) |
(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)) |
93 |
nil nil nil |
nil nil nil |
94 |
query-replace-from-history-variable |
query-replace-from-history-variable |
95 |
nil t)))) |
nil t)))) |
96 |
;; Warn if user types \n or \t, but don't reject the input. |
(if (and (zerop (length from)) lastto lastfrom) |
97 |
(and regexp-flag |
(cons lastfrom lastto) |
98 |
(string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) |
;; Warn if user types \n or \t, but don't reject the input. |
99 |
(let ((match (match-string 3 from))) |
(and regexp-flag |
100 |
(cond |
(string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from) |
101 |
((string= match "\\n") |
(let ((match (match-string 3 from))) |
102 |
(message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) |
(cond |
103 |
((string= match "\\t") |
((string= match "\\n") |
104 |
(message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) |
(message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) |
105 |
(sit-for 2))) |
((string= match "\\t") |
106 |
from))) |
(message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) |
107 |
|
(sit-for 2))) |
108 |
|
from)))) |
109 |
|
|
110 |
(defun query-replace-read-to (from string regexp-flag) |
(defun query-replace-read-to (from string regexp-flag) |
111 |
"Query and return the `from' argument of a query-replace operation." |
"Query and return the `from' argument of a query-replace operation." |
112 |
(let ((to (save-excursion |
(let ((to (save-excursion |
113 |
(read-from-minibuffer |
(read-from-minibuffer |
114 |
(format "%s %s with: " string from) |
(format "%s %s with: " string (query-replace-descr from)) |
115 |
nil nil nil |
nil nil nil |
116 |
query-replace-to-history-variable from t)))) |
query-replace-to-history-variable from t)))) |
117 |
(when (and regexp-flag |
(when (and regexp-flag |
154 |
(unless noerror |
(unless noerror |
155 |
(barf-if-buffer-read-only)) |
(barf-if-buffer-read-only)) |
156 |
(let* ((from (query-replace-read-from string regexp-flag)) |
(let* ((from (query-replace-read-from string regexp-flag)) |
157 |
(to (query-replace-read-to from string regexp-flag))) |
(to (if (consp from) (prog1 (cdr from) (setq from (car from))) |
158 |
|
(query-replace-read-to from string regexp-flag)))) |
159 |
(list from to current-prefix-arg))) |
(list from to current-prefix-arg))) |
160 |
|
|
161 |
(defun query-replace (from-string to-string &optional delimited start end) |
(defun query-replace (from-string to-string &optional delimited start end) |
287 |
only matches that are surrounded by word boundaries. |
only matches that are surrounded by word boundaries. |
288 |
Fourth and fifth arg START and END specify the region to operate on." |
Fourth and fifth arg START and END specify the region to operate on." |
289 |
(interactive |
(interactive |
290 |
(let* ((from (if query-replace-interactive |
(barf-if-buffer-read-only) |
291 |
(car regexp-search-ring) |
(let* ((from |
292 |
(read-from-minibuffer "Query replace regexp: " |
;; Let-bind the history var to disable the "foo -> bar" default. |
293 |
nil nil nil |
;; Maybe we shouldn't disable this default, but for now I'll |
294 |
query-replace-from-history-variable |
;; leave it off. --Stef |
295 |
nil t))) |
(let ((query-replace-to-history-variable nil)) |
296 |
|
(query-replace-read-from "Query replace regexp" t))) |
297 |
(to (list (read-from-minibuffer |
(to (list (read-from-minibuffer |
298 |
(format "Query replace regexp %s with eval: " from) |
(format "Query replace regexp %s with eval: " |
299 |
|
(query-replace-descr from)) |
300 |
nil nil t query-replace-to-history-variable from t)))) |
nil nil t query-replace-to-history-variable from t)))) |
301 |
;; We make TO a list because replace-match-string-symbols requires one, |
;; We make TO a list because replace-match-string-symbols requires one, |
302 |
;; and the user might enter a single token. |
;; and the user might enter a single token. |
337 |
'query-replace-history nil t))) |
'query-replace-history nil t))) |
338 |
(to (read-from-minibuffer |
(to (read-from-minibuffer |
339 |
(format "Query replace %s with (space-separated strings): " |
(format "Query replace %s with (space-separated strings): " |
340 |
from) |
(query-replace-descr from)) |
341 |
nil nil nil |
nil nil nil |
342 |
'query-replace-history from t))) |
'query-replace-history from t))) |
343 |
(list from to |
(list from to |
780 |
(read-from-minibuffer |
(read-from-minibuffer |
781 |
(if default |
(if default |
782 |
(format "List lines matching regexp (default `%s'): " |
(format "List lines matching regexp (default `%s'): " |
783 |
default) |
(query-replace-descr default)) |
784 |
"List lines matching regexp: ") |
"List lines matching regexp: ") |
785 |
nil |
nil |
786 |
nil |
nil |
1558 |
(if (facep 'query-replace) |
(if (facep 'query-replace) |
1559 |
'query-replace 'region))))) |
'query-replace 'region))))) |
1560 |
|
|
1561 |
;;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4 |
;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4 |
1562 |
;;; replace.el ends here |
;;; replace.el ends here |