aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/replace.el
diff options
context:
space:
mode:
authorMiles Bader2004-06-28 07:56:49 +0000
committerMiles Bader2004-06-28 07:56:49 +0000
commit327719ee8a3fcdb36ed6acaf6d8cb5fbdf0bd801 (patch)
tree21de188e13b5e41a79bb50040933072ae0235217 /lisp/replace.el
parent852f73b7fa7b71910282eacb6263b3ecfd4ee783 (diff)
parent376de73927383d6062483db10b8a82448505f52b (diff)
downloademacs-327719ee8a3fcdb36ed6acaf6d8cb5fbdf0bd801.tar.gz
emacs-327719ee8a3fcdb36ed6acaf6d8cb5fbdf0bd801.zip
Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-15
Merge from emacs--cvs-trunk--0 Patches applied: * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-218 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-220 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-221 Restore deleted tagline in etc/TUTORIAL.ru * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-222 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-228 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-229 Remove TeX output files from the archive * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-230 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-247 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-248 src/lisp.h (CYCLE_CHECK): Macro moved from xfaces.c * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-249 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-256 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-258 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-263 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-264 Update from CVS: lispref/display.texi: emacs -> Emacs. * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-265 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-274 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-275 Update from CVS: man/makefile.w32-in: Revert last change * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-276 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-295 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-296 Allow restarting an existing debugger session that's exited * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-297 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-299 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-300 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-327 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-328 Update from CVS: src/.gdbinit (xsymbol): Fix last change. * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-329 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-344 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-345 Tweak source regexps so that building in place won't cause problems * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-346 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-351 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-352 Update from CVS: lisp/flymake.el: New file. * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-353 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-361 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-362 Support " [...]" style defaults in minibuffer-electric-default-mode * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-363 (read-number): Use canonical format for default in prompt. * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-364 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-367 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-368 Improve display-supports-face-attributes-p on non-ttys * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-369 Rewrite face-differs-from-default-p * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-370 Move `display-supports-face-attributes-p' entirely into C code * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-371 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-372 Simplify face-differs-from-default-p; don't consider :stipple. * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-373 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-374 (tty_supports_face_attributes_p): Ensure attributes differ from default * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-375 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-376 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-377 (Fdisplay_supports_face_attributes_p): Work around bootstrapping problem * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-378 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-380 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-381 Face merging cleanups * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-382 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-384 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-385 src/xfaces.c (push_named_merge_point): Return 0 if a cycle is detected * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-386 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-395 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-396 Tweak arch tagging to make build/install-in-place less annoying * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-397 Work around vc-arch problems when building eshell * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-398 Tweak permissions * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-399 Tweak directory permissions * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-400 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-401 More build-in-place tweaking of arch tagging * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-402 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-403 Yet more build-in-place tweaking of arch tagging * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-404 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-409 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-410 Make sure image types are initialized for lookup too * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-411 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-416 Update from CVS
Diffstat (limited to 'lisp/replace.el')
-rw-r--r--lisp/replace.el137
1 files changed, 111 insertions, 26 deletions
diff --git a/lisp/replace.el b/lisp/replace.el
index 2d26cb5cc66..89f55c2829e 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -1,7 +1,7 @@
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, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002,
4;; Free Software Foundation, Inc. 4;; 2003, 2004 Free Software Foundation, Inc.
5 5
6;; Maintainer: FSF 6;; Maintainer: FSF
7 7
@@ -36,9 +36,11 @@
36 36
37(defvar query-replace-history nil) 37(defvar query-replace-history nil)
38 38
39(defvar query-replace-interactive nil 39(defcustom query-replace-interactive nil
40 "Non-nil means `query-replace' uses the last search string. 40 "Non-nil means `query-replace' uses the last search string.
41That becomes the \"string to replace\".") 41That becomes the \"string to replace\"."
42 :type 'boolean
43 :group 'matching)
42 44
43(defcustom query-replace-from-history-variable 'query-replace-history 45(defcustom query-replace-from-history-variable 'query-replace-history
44 "History list to use for the FROM argument of `query-replace' commands. 46 "History list to use for the FROM argument of `query-replace' commands.
@@ -79,14 +81,15 @@ strings or patterns."
79 query-replace-from-history-variable 81 query-replace-from-history-variable
80 nil t))) 82 nil t)))
81 ;; Warn if user types \n or \t, but don't reject the input. 83 ;; Warn if user types \n or \t, but don't reject the input.
82 (if (string-match "\\\\[nt]" from) 84 (and regexp-flag
83 (let ((match (match-string 0 from))) 85 (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from)
84 (cond 86 (let ((match (match-string 3 from)))
85 ((string= match "\\n") 87 (cond
86 (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead")) 88 ((string= match "\\n")
87 ((string= match "\\t") 89 (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
88 (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB"))) 90 ((string= match "\\t")
89 (sit-for 2)))) 91 (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
92 (sit-for 2))))
90 93
91 (save-excursion 94 (save-excursion
92 (setq to (read-from-minibuffer (format "%s %s with: " string from) 95 (setq to (read-from-minibuffer (format "%s %s with: " string from)
@@ -159,20 +162,62 @@ Fourth and fifth arg START and END specify the region to operate on.
159 162
160In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP, 163In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
161and `\\=\\N' (where N is a digit) stands for 164and `\\=\\N' (where N is a digit) stands for
162 whatever what matched the Nth `\\(...\\)' in REGEXP." 165whatever what matched the Nth `\\(...\\)' in REGEXP.
166
167When this function is called interactively, the replacement text
168can also contain `\\,' followed by a Lisp expression. The escaped
169shorthands for `query-replace-regexp-eval' are also valid
170here: within the Lisp expression, you can use `\\&' for the whole
171match string, `\\N' for partial matches, `\\#&' and `\\#N' for
172the respective numeric values, and `\\#' for `replace-count'.
173
174If your Lisp expression is an identifier and the next
175letter in the replacement string would be interpreted as part of it,
176you can wrap it with an expression like `\\,(or \\#)'. Incidentally,
177for this particular case you may also enter `\\#' in the replacement
178text directly.
179
180When you use `\\,' or `\\#' in the replacement, TO-STRING actually
181becomes a list with expanded shorthands.
182Use \\[repeat-complex-command] after this command to see details."
163 (interactive 183 (interactive
164 (let ((common 184 (let ((common
165 (query-replace-read-args "Query replace regexp" t))) 185 (query-replace-read-args "Query replace regexp" t)))
166 (list (nth 0 common) (nth 1 common) (nth 2 common) 186 (list
167 ;; These are done separately here 187 (nth 0 common)
168 ;; so that command-history will record these expressions 188 (if (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]"
169 ;; rather than the values they had this time. 189 (nth 1 common))
170 (if (and transient-mark-mode mark-active) 190 (let ((to-string (nth 1 common)) pos to-expr char prompt)
171 (region-beginning)) 191 (while (string-match
172 (if (and transient-mark-mode mark-active) 192 "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]"
173 (region-end))))) 193 to-string)
174 194 (setq pos (match-end 0))
195 (push (substring to-string 0 (- pos 2)) to-expr)
196 (setq char (aref to-string (1- pos))
197 to-string (substring to-string pos))
198 (cond ((eq char ?\#)
199 (push '(number-to-string replace-count) to-expr))
200 ((eq char ?\,)
201 (setq pos (read-from-string to-string))
202 (push `(replace-quote ,(car pos)) to-expr)
203 (setq to-string (substring to-string (cdr pos))))))
204 (setq to-expr (nreverse (delete "" (cons to-string to-expr))))
205 (replace-match-string-symbols to-expr)
206 (cons 'replace-eval-replacement
207 (if (> (length to-expr) 1)
208 (cons 'concat to-expr)
209 (car to-expr))))
210 (nth 1 common))
211 (nth 2 common)
212 ;; These are done separately here
213 ;; so that command-history will record these expressions
214 ;; rather than the values they had this time.
215 (if (and transient-mark-mode mark-active)
216 (region-beginning))
217 (if (and transient-mark-mode mark-active)
218 (region-end)))))
175 (perform-replace regexp to-string t t delimited nil nil start end)) 219 (perform-replace regexp to-string t t delimited nil nil start end))
220
176(define-key esc-map [?\C-%] 'query-replace-regexp) 221(define-key esc-map [?\C-%] 'query-replace-regexp)
177 222
178(defun query-replace-regexp-eval (regexp to-expr &optional delimited start end) 223(defun query-replace-regexp-eval (regexp to-expr &optional delimited start end)
@@ -189,6 +234,7 @@ For convenience, when entering TO-EXPR interactively, you can use `\\&' or
189`\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where 234`\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where
190N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP. 235N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP.
191Use `\\#&' or `\\#N' if you want a number instead of a string. 236Use `\\#&' or `\\#N' if you want a number instead of a string.
237In interactive use, `\\#' in itself stands for `replace-count'.
192 238
193In Transient Mark mode, if the mark is active, operate on the contents 239In Transient Mark mode, if the mark is active, operate on the contents
194of the region. Otherwise, operate from point to the end of the buffer. 240of the region. Otherwise, operate from point to the end of the buffer.
@@ -538,6 +584,7 @@ Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
538 (set (make-local-variable 'revert-buffer-function) 'occur-revert-function) 584 (set (make-local-variable 'revert-buffer-function) 'occur-revert-function)
539 (make-local-variable 'occur-revert-arguments) 585 (make-local-variable 'occur-revert-arguments)
540 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t) 586 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
587 (setq next-error-function 'occur-next-error)
541 (run-hooks 'occur-mode-hook)) 588 (run-hooks 'occur-mode-hook))
542 589
543(defun occur-revert-function (ignore1 ignore2) 590(defun occur-revert-function (ignore1 ignore2)
@@ -614,6 +661,21 @@ Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
614 "Move to the Nth (default 1) previous match in an Occur mode buffer." 661 "Move to the Nth (default 1) previous match in an Occur mode buffer."
615 (interactive "p") 662 (interactive "p")
616 (occur-find-match n #'previous-single-property-change "No earlier matches")) 663 (occur-find-match n #'previous-single-property-change "No earlier matches"))
664
665(defun occur-next-error (&optional argp reset)
666 "Move to the Nth (default 1) next match in an Occur mode buffer.
667Compatibility function for \\[next-error] invocations."
668 (interactive "p")
669 (when reset
670 (occur-find-match 0 #'next-single-property-change "No first match"))
671 (occur-find-match
672 (prefix-numeric-value argp)
673 (if (> 0 (prefix-numeric-value argp))
674 #'previous-single-property-change
675 #'next-single-property-change)
676 "No more matches")
677 (occur-mode-goto-occurrence))
678
617 679
618(defcustom list-matching-lines-default-context-lines 0 680(defcustom list-matching-lines-default-context-lines 0
619 "*Default number of context lines included around `list-matching-lines' matches. 681 "*Default number of context lines included around `list-matching-lines' matches.
@@ -800,7 +862,9 @@ See also `multi-occur'."
800 (setq occur-revert-arguments (list regexp nlines bufs) 862 (setq occur-revert-arguments (list regexp nlines bufs)
801 buffer-read-only t) 863 buffer-read-only t)
802 (if (> count 0) 864 (if (> count 0)
803 (display-buffer occur-buf) 865 (progn
866 (display-buffer occur-buf)
867 (setq next-error-last-buffer occur-buf))
804 (kill-buffer occur-buf))) 868 (kill-buffer occur-buf)))
805 (run-hooks 'occur-hook)))) 869 (run-hooks 'occur-hook))))
806 870
@@ -992,6 +1056,7 @@ N (match-string N) (where N is a string of digits)
992#N (string-to-number (match-string N)) 1056#N (string-to-number (match-string N))
993& (match-string 0) 1057& (match-string 0)
994#& (string-to-number (match-string 0)) 1058#& (string-to-number (match-string 0))
1059# replace-count
995 1060
996Note that these symbols must be preceeded by a backslash in order to 1061Note that these symbols must be preceeded by a backslash in order to
997type them." 1062type them."
@@ -1011,7 +1076,9 @@ type them."
1011 ((string= "&" name) 1076 ((string= "&" name)
1012 (setcar n '(match-string 0))) 1077 (setcar n '(match-string 0)))
1013 ((string= "#&" name) 1078 ((string= "#&" name)
1014 (setcar n '(string-to-number (match-string 0)))))))) 1079 (setcar n '(string-to-number (match-string 0))))
1080 ((string= "#" name)
1081 (setcar n 'replace-count))))))
1015 (setq n (cdr n)))) 1082 (setq n (cdr n))))
1016 1083
1017(defun replace-eval-replacement (expression replace-count) 1084(defun replace-eval-replacement (expression replace-count)
@@ -1020,6 +1087,21 @@ type them."
1020 replacement 1087 replacement
1021 (prin1-to-string replacement t)))) 1088 (prin1-to-string replacement t))))
1022 1089
1090(defun replace-quote (replacement)
1091 "Quote a replacement string.
1092This just doubles all backslashes in REPLACEMENT and
1093returns the resulting string. If REPLACEMENT is not
1094a string, it is first passed through `prin1-to-string'
1095with the `noescape' argument set.
1096
1097`match-data' is preserved across the call."
1098 (save-match-data
1099 (replace-regexp-in-string "\\\\" "\\\\"
1100 (if (stringp replacement)
1101 replacement
1102 (prin1-to-string replacement t))
1103 t t)))
1104
1023(defun replace-loop-through-replacements (data replace-count) 1105(defun replace-loop-through-replacements (data replace-count)
1024 ;; DATA is a vector contaning the following values: 1106 ;; DATA is a vector contaning the following values:
1025 ;; 0 next-rotate-count 1107 ;; 0 next-rotate-count
@@ -1112,7 +1194,7 @@ make, or the user didn't cancel the call."
1112 (unwind-protect 1194 (unwind-protect
1113 ;; Loop finding occurrences that perhaps should be replaced. 1195 ;; Loop finding occurrences that perhaps should be replaced.
1114 (while (and keep-going 1196 (while (and keep-going
1115 (not (eobp)) 1197 (not (or (eobp) (and limit (>= (point) limit))))
1116 ;; Use the next match if it is already known; 1198 ;; Use the next match if it is already known;
1117 ;; otherwise, search for a match after moving forward 1199 ;; otherwise, search for a match after moving forward
1118 ;; one char if progress is required. 1200 ;; one char if progress is required.
@@ -1128,7 +1210,10 @@ make, or the user didn't cancel the call."
1128 ;; character too far at the end, 1210 ;; character too far at the end,
1129 ;; but this is undone after the 1211 ;; but this is undone after the
1130 ;; while-loop. 1212 ;; while-loop.
1131 (progn (forward-char 1) (not (eobp)))) 1213 (progn
1214 (forward-char 1)
1215 (not (or (eobp)
1216 (and limit (>= (point) limit))))))
1132 (funcall search-function search-string limit t) 1217 (funcall search-function search-string limit t)
1133 ;; For speed, use only integers and 1218 ;; For speed, use only integers and
1134 ;; reuse the list used last time. 1219 ;; reuse the list used last time.