annotate man/xemacs/search.texi @ 5686:c6b1500299a7

recenter-top-bottom synced from GNU and new default for C-l Partial implementation of recenter-top-bottom. GNU has support for scroll-margin that is not in XEmacs so that is left out. lisp/ChangeLog: 2012-09-18 Mats Lidell <matsl@xemacs.org> * window-xemacs.el (recenter-positions): New defcustom. (recenter-top-bottom): New command. (recenter-last-op): New defvar. * replace.el (perform-replace): Let-bind recenter-last-op to nil. For def=recenter, replace `recenter' with `recenter-top-bottom' that is called with `this-command' and `last-command' let-bound to `recenter-top-bottom'. When the last `def' was not `recenter', set `recenter-last-op' to nil. * keydefs.el (global-map): Make recenter-top-bottom new default for C-l. etc/ChangeLog: 2012-09-18 Mats Lidell <matsl@xemacs.org> * TUTORIAL: Updated due to recenter-top-bottom man/ChangeLog: 2012-09-18 Mats Lidell <matsl@xemacs.org> * lispref/windows.texi (Vertical Scrolling): Added recenter-top-bottom and recenter-positions * xemacs/display.texi (Display): Rearranged and added documentation due to new function recenter-top-bottom.
author Mats Lidell <mats.lidell@cag.se>
date Tue, 18 Sep 2012 08:58:28 +0200
parents abe6d1db359e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 @node Search, Fixit, Display, Top
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 @chapter Searching and Replacement
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 @cindex searching
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 Like other editors, Emacs has commands for searching for occurrences of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 a string. The principal search command is unusual in that it is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 @dfn{incremental}: it begins to search before you have finished typing the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 search string. There are also non-incremental search commands more like
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 those of other editors.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 Besides the usual @code{replace-string} command that finds all
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 occurrences of one string and replaces them with another, Emacs has a fancy
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 replacement command called @code{query-replace} which asks interactively
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 which occurrences to replace.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 @menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 * Incremental Search:: Search happens as you type the string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 * Non-Incremental Search:: Specify entire string and then search.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 * Word Search:: Search for sequence of words.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 * Regexp Search:: Search for match for a regexp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 * Regexps:: Syntax of regular expressions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 * Search Case:: To ignore case while searching, or not.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 * Replace:: Search, and replace some or all matches.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 * Other Repeating Search:: Operating on all matches for some regexp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 @end menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 @node Incremental Search, Non-Incremental Search, Search, Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 @section Incremental Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 An incremental search begins searching as soon as you type the first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 character of the search string. As you type in the search string, Emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 shows you where the string (as you have typed it so far) is found.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 When you have typed enough characters to identify the place you want, you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 can stop. Depending on what you do next, you may or may not need to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 terminate the search explicitly with a @key{RET}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 @c WideCommands
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 @table @kbd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 @item C-s
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 Incremental search forward (@code{isearch-forward}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 @item C-r
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 Incremental search backward (@code{isearch-backward}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 @kindex C-s
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 @kindex C-r
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 @findex isearch-forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 @findex isearch-backward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 @kbd{C-s} starts an incremental search. @kbd{C-s} reads characters from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 the keyboard and positions the cursor at the first occurrence of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 characters that you have typed. If you type @kbd{C-s} and then @kbd{F},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 the cursor moves right after the first @samp{F}. Type an @kbd{O}, and see
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 the cursor move to after the first @samp{FO}. After another @kbd{O}, the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 cursor is after the first @samp{FOO} after the place where you started the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 search. Meanwhile, the search string @samp{FOO} has been echoed in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 echo area.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 The echo area display ends with three dots when actual searching is going
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 on. When search is waiting for more input, the three dots are removed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (On slow terminals, the three dots are not displayed.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 If you make a mistake in typing the search string, you can erase
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 characters with @key{DEL}. Each @key{DEL} cancels the last character of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 search string. This does not happen until Emacs is ready to read another
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 input character; first it must either find, or fail to find, the character
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 you want to erase. If you do not want to wait for this to happen, use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 @kbd{C-g} as described below.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 When you are satisfied with the place you have reached, you can type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 @key{RET} (or @key{C-m}), which stops searching, leaving the cursor where
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 the search brought it. Any command not specially meaningful in searches also
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 stops the search and is then executed. Thus, typing @kbd{C-a} exits the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 search and then moves to the beginning of the line. @key{RET} is necessary
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 only if the next command you want to type is a printing character,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 @key{DEL}, @key{ESC}, or another control character that is special
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 within searches (@kbd{C-q}, @kbd{C-w}, @kbd{C-r}, @kbd{C-s}, or @kbd{C-y}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 Sometimes you search for @samp{FOO} and find it, but were actually
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
80 looking for a different occurrence of it. To move to the next occurrence
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 of the search string, type another @kbd{C-s}. Do this as often as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 necessary. If you overshoot, you can cancel some @kbd{C-s}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 characters with @key{DEL}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 After you exit a search, you can search for the same string again by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 typing just @kbd{C-s C-s}: the first @kbd{C-s} is the key that invokes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 incremental search, and the second @kbd{C-s} means ``search again''.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 If the specified string is not found at all, the echo area displays
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 the text @samp{Failing I-Search}. The cursor is after the place where
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 Emacs found as much of your string as it could. Thus, if you search for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 @samp{FOOT}, and there is no @samp{FOOT}, the cursor may be after the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 @samp{FOO} in @samp{FOOL}. At this point there are several things you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 can do. If you mistyped the search string, correct it. If you like the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 place you have found, you can type @key{RET} or some other Emacs command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 to ``accept what the search offered''. Or you can type @kbd{C-g}, which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 removes from the search string the characters that could not be found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (the @samp{T} in @samp{FOOT}), leaving those that were found (the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 @samp{FOO} in @samp{FOOT}). A second @kbd{C-g} at that point cancels
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 the search entirely, returning point to where it was when the search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 started.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 If a search is failing and you ask to repeat it by typing another
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 @kbd{C-s}, it starts again from the beginning of the buffer. Repeating
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 a failing backward search with @kbd{C-r} starts again from the end. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 is called @dfn{wrapping around}. @samp{Wrapped} appears in the search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 prompt once this has happened.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 @cindex quitting (in search)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 The @kbd{C-g} ``quit'' character does special things during searches;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 just what it does depends on the status of the search. If the search has
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 found what you specified and is waiting for input, @kbd{C-g} cancels the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 entire search. The cursor moves back to where you started the search. If
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 @kbd{C-g} is typed when there are characters in the search string that have
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 not been found---because Emacs is still searching for them, or because it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 has failed to find them---then the search string characters which have not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 been found are discarded from the search string. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 search is now successful and waiting for more input, so a second @kbd{C-g}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 cancels the entire search.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 To search for a control character such as @kbd{C-s} or @key{DEL} or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 @key{ESC}, you must quote it by typing @kbd{C-q} first. This function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 of @kbd{C-q} is analogous to its meaning as an Emacs command: it causes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 the following character to be treated the way a graphic character would
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 normally be treated in the same context.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 To search backwards, you can use @kbd{C-r} instead of @kbd{C-s} to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 start the search; @kbd{C-r} is the key that runs the command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (@code{isearch-backward}) to search backward. You can also use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 @kbd{C-r} to change from searching forward to searching backwards. Do
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 this if a search fails because the place you started was too far down in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 file. Repeated @kbd{C-r} keeps looking for more occurrences backwards.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 @kbd{C-s} starts going forward again. You can cancel @kbd{C-r} in a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 search with @key{DEL}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 The characters @kbd{C-y} and @kbd{C-w} can be used in incremental search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 to grab text from the buffer into the search string. This makes it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 convenient to search for another occurrence of text at point. @kbd{C-w}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 copies the word after point as part of the search string, advancing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 point over that word. Another @kbd{C-s} to repeat the search will then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 search for a string including that word. @kbd{C-y} is similar to @kbd{C-w}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 but copies the rest of the current line into the search string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 The characters @kbd{M-p} and @kbd{M-n} can be used in an incremental
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 search to recall things which you have searched for in the past. A
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 list of the last 16 things you have searched for is retained, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 @kbd{M-p} and @kbd{M-n} let you cycle through that ring.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 The character @kbd{M-@key{TAB}} does completion on the elements in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 the search history ring. For example, if you know that you have
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 recently searched for the string @code{POTATOE}, you could type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 @kbd{C-s P O M-@key{TAB}}. If you had searched for other strings
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 beginning with @code{PO} then you would be shown a list of them, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 would need to type more to select one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 You can change any of the special characters in incremental search via
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 the normal keybinding mechanism: simply add a binding to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 @code{isearch-mode-map}. For example, to make the character
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 @kbd{C-b} mean ``search backwards'' while in isearch-mode, do this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (define-key isearch-mode-map "\C-b" 'isearch-repeat-backward)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 These are the default bindings of isearch-mode:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 @findex isearch-delete-char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 @findex isearch-exit
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 @findex isearch-quote-char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 @findex isearch-repeat-forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 @findex isearch-repeat-backward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 @findex isearch-yank-line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 @findex isearch-yank-word
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 @findex isearch-abort
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 @findex isearch-ring-retreat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 @findex isearch-ring-advance
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 @findex isearch-complete
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 @kindex DEL (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 @kindex RET (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 @kindex C-q (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 @kindex C-s (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 @kindex C-r (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 @kindex C-y (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 @kindex C-w (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 @kindex C-g (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 @kindex M-p (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 @kindex M-n (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 @kindex M-TAB (isearch-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 @table @kbd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 @item DEL
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 Delete a character from the incremental search string (@code{isearch-delete-char}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 @item RET
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 Exit incremental search (@code{isearch-exit}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 @item C-q
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 Quote special characters for incremental search (@code{isearch-quote-char}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 @item C-s
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 Repeat incremental search forward (@code{isearch-repeat-forward}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 @item C-r
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 Repeat incremental search backward (@code{isearch-repeat-backward}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 @item C-y
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 Pull rest of line from buffer into search string (@code{isearch-yank-line}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 @item C-w
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 Pull next word from buffer into search string (@code{isearch-yank-word}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 @item C-g
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 Cancels input back to what has been found successfully, or aborts the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 isearch (@code{isearch-abort}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 @item M-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 Recall the previous element in the isearch history ring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (@code{isearch-ring-retreat}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 @item M-n
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 Recall the next element in the isearch history ring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (@code{isearch-ring-advance}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 @item M-@key{TAB}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 Do completion on the elements in the isearch history ring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (@code{isearch-complete}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 Any other character which is normally inserted into a buffer when typed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 is automatically added to the search string in isearch-mode.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 @subsection Slow Terminal Incremental Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 Incremental search on a slow terminal uses a modified style of display
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 that is designed to take less time. Instead of redisplaying the buffer at
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 each place the search gets to, it creates a new single-line window and uses
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 that to display the line the search has found. The single-line window
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 appears as soon as point gets outside of the text that is already
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 on the screen.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 When the search is terminated, the single-line window is removed. Only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 at this time the window in which the search was done is redisplayed to show
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 its new value of point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 The three dots at the end of the search string, normally used to indicate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 that searching is going on, are not displayed in slow style display.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 @vindex search-slow-speed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 The slow terminal style of display is used when the terminal baud rate is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 less than or equal to the value of the variable @code{search-slow-speed},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 initially 1200.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 @vindex search-slow-window-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 The number of lines to use in slow terminal search display is controlled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 by the variable @code{search-slow-window-lines}. Its normal value is 1.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 @node Non-Incremental Search, Word Search, Incremental Search, Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 @section Non-Incremental Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 @cindex non-incremental search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 Emacs also has conventional non-incremental search commands, which require
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 you type the entire search string before searching begins.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 @table @kbd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 @item C-s @key{RET} @var{string} @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 Search for @var{string}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 @item C-r @key{RET} @var{string} @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 Search backward for @var{string}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 To do a non-incremental search, first type @kbd{C-s @key{RET}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (or @kbd{C-s C-m}). This enters the minibuffer to read the search string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 Terminate the string with @key{RET} to start the search. If the string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 is not found, the search command gets an error.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 By default, @kbd{C-s} invokes incremental search, but if you give it an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 empty argument, which would otherwise be useless, it invokes non-incremental
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 search. Therefore, @kbd{C-s @key{RET}} invokes non-incremental search.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 @kbd{C-r @key{RET}} also works this way.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 @findex search-forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 @findex search-backward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 Forward and backward non-incremental searches are implemented by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 commands @code{search-forward} and @code{search-backward}. You can bind
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 these commands to keys. The reason that incremental
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 search is programmed to invoke them as well is that @kbd{C-s @key{RET}}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 is the traditional sequence of characters used in Emacs to invoke
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 non-incremental search.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 Non-incremental searches performed using @kbd{C-s @key{RET}} do
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 not call @code{search-forward} right away. They first check
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 if the next character is @kbd{C-w}, which requests a word search.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 @ifinfo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 @xref{Word Search}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 @end ifinfo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 @node Word Search, Regexp Search, Non-Incremental Search, Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 @section Word Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 @cindex word search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 Word search looks for a sequence of words without regard to how the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 words are separated. More precisely, you type a string of many words,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 using single spaces to separate them, and the string is found even if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 there are multiple spaces, newlines or other punctuation between the words.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 Word search is useful in editing documents formatted by text formatters.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 If you edit while looking at the printed, formatted version, you can't tell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 where the line breaks are in the source file. Word search, allows you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 to search without having to know the line breaks.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 @table @kbd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 @item C-s @key{RET} C-w @var{words} @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 Search for @var{words}, ignoring differences in punctuation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 @item C-r @key{RET} C-w @var{words} @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 Search backward for @var{words}, ignoring differences in punctuation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 Word search is a special case of non-incremental search. It is invoked
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 with @kbd{C-s @key{RET} C-w} followed by the search string, which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 must always be terminated with another @key{RET}. Being non-incremental, this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 search does not start until the argument is terminated. It works by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 constructing a regular expression and searching for that. @xref{Regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 Search}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 You can do a backward word search with @kbd{C-r @key{RET} C-w}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 @findex word-search-forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 @findex word-search-backward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 Forward and backward word searches are implemented by the commands
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 @code{word-search-forward} and @code{word-search-backward}. You can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 bind these commands to keys. The reason that incremental
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 search is programmed to invoke them as well is that @kbd{C-s @key{RET} C-w}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 is the traditional Emacs sequence of keys for word search.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 @node Regexp Search, Regexps, Word Search, Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 @section Regular Expression Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 @cindex regular expression
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 @cindex regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 A @dfn{regular expression} (@dfn{regexp}, for short) is a pattern that
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
333 denotes a (possibly infinite) set of strings. Searching for matches
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 for a regexp is a powerful operation that editors on Unix systems have
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
335 traditionally offered.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
336
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
337 To gain a thorough understanding of regular expressions and how to use
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
338 them to best advantage, we recommend that you study @cite{Mastering
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
339 Regular Expressions, by Jeffrey E.F. Friedl, O'Reilly and Associates,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
340 1997}. (It's known as the "Hip Owls" book, because of the picture on its
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
341 cover.) You might also read the manuals to @ref{(gawk)Top},
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
342 @ref{(ed)Top}, @cite{sed}, @cite{grep}, @ref{(perl)Top},
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
343 @ref{(regex)Top}, @ref{(rx)Top}, @cite{pcre}, and @ref{(flex)Top}, which
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
344 also make good use of regular expressions.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
345
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
346 The XEmacs regular expression syntax most closely resembles that of
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
347 @cite{ed}, or @cite{grep}, the GNU versions of which all utilize the GNU
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
348 @cite{regex} library. XEmacs' version of @cite{regex} has recently been
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
349 extended with some Perl--like capabilities, described in the next
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
350 section.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
351
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
352 In XEmacs, you can search for the next match for a regexp either
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
353 incrementally or not.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 @kindex M-C-s
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
356 @kindex M-C-r
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 @findex isearch-forward-regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 @findex isearch-backward-regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 Incremental search for a regexp is done by typing @kbd{M-C-s}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 (@code{isearch-forward-regexp}). This command reads a search string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 incrementally just like @kbd{C-s}, but it treats the search string as a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 regexp rather than looking for an exact match against the text in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 buffer. Each time you add text to the search string, you make the regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 longer, and the new regexp is searched for. A reverse regexp search command
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
365 @code{isearch-backward-regexp} also exists, bound to @kbd{M-C-r}.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 All of the control characters that do special things within an ordinary
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 incremental search have the same functionality in incremental regexp search.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 Typing @kbd{C-s} or @kbd{C-r} immediately after starting a search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 retrieves the last incremental search regexp used:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 incremental regexp and non-regexp searches have independent defaults.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 @findex re-search-forward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 @findex re-search-backward
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 Non-incremental search for a regexp is done by the functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 @code{re-search-forward} and @code{re-search-backward}. You can invoke
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 them with @kbd{M-x} or bind them to keys. You can also call
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 @code{re-search-forward} by way of incremental regexp search with
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
379 @kbd{M-C-s @key{RET}}; similarly for @code{re-search-backward} with
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
380 @kbd{M-C-r @key{RET}}.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 @node Regexps, Search Case, Regexp Search, Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 @section Syntax of Regular Expressions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
385 Regular expressions have a syntax in which a few characters are
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
386 special constructs and the rest are @dfn{ordinary}. An ordinary
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
387 character is a simple regular expression that matches that character and
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
388 nothing else. The special characters are @samp{.}, @samp{*}, @samp{+},
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
389 @samp{?}, @samp{[}, @samp{]}, @samp{^}, @samp{$}, and @samp{\}; no new
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
390 special characters will be defined in the future. Any other character
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
391 appearing in a regular expression is ordinary, unless a @samp{\}
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
392 precedes it.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 For example, @samp{f} is not a special character, so it is ordinary, and
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
395 therefore @samp{f} is a regular expression that matches the string
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
396 @samp{f} and no other string. (It does @emph{not} match the string
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
397 @samp{ff}.) Likewise, @samp{o} is a regular expression that matches
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
398 only @samp{o}.@refill
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 Any two regular expressions @var{a} and @var{b} can be concatenated. The
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
401 result is a regular expression that matches a string if @var{a} matches
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 some amount of the beginning of that string and @var{b} matches the rest of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 the string.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
405 As a simple example, we can concatenate the regular expressions @samp{f}
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 and @samp{o} to get the regular expression @samp{fo}, which matches only
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
407 the string @samp{fo}. Still trivial. To do something more powerful, you
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
408 need to use one of the special characters. Here is a list of them:
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
410 @need 1200
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 @table @kbd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 @item .@: @r{(Period)}
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
413 @cindex @samp{.} in regexp
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 is a special character that matches any single character except a newline.
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
415 Using concatenation, we can make regular expressions like @samp{a.b}, which
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
416 matches any three-character string that begins with @samp{a} and ends with
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 @samp{b}.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 @item *
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
420 @cindex @samp{*} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
421 is not a construct by itself; it is a quantifying suffix operator that
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
422 means to repeat the preceding regular expression as many times as
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 possible. In @samp{fo*}, the @samp{*} applies to the @samp{o}, so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 @samp{fo*} matches one @samp{f} followed by any number of @samp{o}s.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 The case of zero @samp{o}s is allowed: @samp{fo*} does match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 @samp{f}.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
428 @samp{*} always applies to the @emph{smallest} possible preceding
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 expression. Thus, @samp{fo*} has a repeating @samp{o}, not a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 repeating @samp{fo}.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
432 The matcher processes a @samp{*} construct by matching, immediately, as
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
433 many repetitions as can be found; it is "greedy". Then it continues
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
434 with the rest of the pattern. If that fails, backtracking occurs,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
435 discarding some of the matches of the @samp{*}-modified construct in
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
436 case that makes it possible to match the rest of the pattern. For
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
437 example, in matching @samp{ca*ar} against the string @samp{caaar}, the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
438 @samp{a*} first tries to match all three @samp{a}s; but the rest of the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
439 pattern is @samp{ar} and there is only @samp{r} left to match, so this
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
440 try fails. The next alternative is for @samp{a*} to match only two
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
441 @samp{a}s. With this choice, the rest of the regexp matches
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
442 successfully.@refill
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
443
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
444 Nested repetition operators can be extremely slow if they specify
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
445 backtracking loops. For example, it could take hours for the regular
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
446 expression @samp{\(x+y*\)*a} to match the sequence
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
447 @samp{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz}. The slowness is because
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
448 Emacs must try each imaginable way of grouping the 35 @samp{x}'s before
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
449 concluding that none of them can work. To make sure your regular
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
450 expressions run fast, check nested repetitions carefully.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 @item +
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
453 @cindex @samp{+} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
454 is a quantifying suffix operator similar to @samp{*} except that the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
455 preceding expression must match at least once. It is also "greedy".
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
456 So, for example, @samp{ca+r} matches the strings @samp{car} and
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
457 @samp{caaaar} but not the string @samp{cr}, whereas @samp{ca*r} matches
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
458 all three strings.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 @item ?
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
461 @cindex @samp{?} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
462 is a quantifying suffix operator similar to @samp{*}, except that the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
463 preceding expression can match either once or not at all. For example,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
464 @samp{ca?r} matches @samp{car} or @samp{cr}, but does not match anything
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
465 else.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
466
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
467 @item *?
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
468 @cindex @samp{*?} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
469 works just like @samp{*}, except that rather than matching the longest
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
470 match, it matches the shortest match. @samp{*?} is known as a
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
471 @dfn{non-greedy} quantifier, a regexp construct borrowed from Perl.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
472 @c Did perl get this from somewhere? What's the real history of *? ?
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
473
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
474 This construct is very useful for when you want to match the text inside
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
475 a pair of delimiters. For instance, @samp{/\*.*?\*/} will match C
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
476 comments in a string. This could not easily be achieved without the use
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
477 of a non-greedy quantifier.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
478
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
479 This construct has not been available prior to XEmacs 20.4. It is not
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
480 available in FSF Emacs.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
481
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
482 @item +?
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
483 @cindex @samp{+?} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
484 is the non-greedy version of @samp{+}.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
485
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
486 @item ??
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
487 @cindex @samp{??} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
488 is the non-greedy version of @samp{?}.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
489
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
490 @item \@{n,m\@}
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
491 @c Note the spacing after the close brace is deliberate.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
492 @cindex @samp{\@{n,m\@} }in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
493 serves as an interval quantifier, analogous to @samp{*} or @samp{+}, but
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
494 specifies that the expression must match at least @var{n} times, but no
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
495 more than @var{m} times. This syntax is supported by most Unix regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
496 utilities, and has been introduced to XEmacs for the version 20.3.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
497
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
498 Unfortunately, the non-greedy version of this quantifier does not exist
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
499 currently, although it does in Perl.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 @item [ @dots{} ]
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
502 @cindex character set (in regexp)
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
503 @cindex @samp{[} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
504 @cindex @samp{]} in regexp
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 @samp{[} begins a @dfn{character set}, which is terminated by a
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
506 @samp{]}. In the simplest case, the characters between the two brackets
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
507 form the set. Thus, @samp{[ad]} matches either one @samp{a} or one
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
508 @samp{d}, and @samp{[ad]*} matches any string composed of just @samp{a}s
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
509 and @samp{d}s (including the empty string), from which it follows that
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
510 @samp{c[ad]*r} matches @samp{cr}, @samp{car}, @samp{cdr},
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 @samp{caddaar}, etc.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
513 The usual regular expression special characters are not special inside a
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
514 character set. A completely different set of special characters exists
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
515 inside character sets: @samp{]}, @samp{-} and @samp{^}.@refill
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
516
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
517 @samp{-} is used for ranges of characters. To write a range, write two
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 characters with a @samp{-} between them. Thus, @samp{[a-z]} matches any
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
519 lower case letter. Ranges may be intermixed freely with individual
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
520 characters, as in @samp{[a-z$%.]}, which matches any lower case letter
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
521 or @samp{$}, @samp{%}, or a period.@refill
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
523 To include a @samp{]} in a character set, make it the first character.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
524 For example, @samp{[]a]} matches @samp{]} or @samp{a}. To include a
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
525 @samp{-}, write @samp{-} as the first character in the set, or put it
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
526 immediately after a range. (You can replace one individual character
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
527 @var{c} with the range @samp{@var{c}-@var{c}} to make a place to put the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
528 @samp{-}.) There is no way to write a set containing just @samp{-} and
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
529 @samp{]}.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
531 To include @samp{^} in a set, put it anywhere but at the beginning of
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
532 the set.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 @item [^ @dots{} ]
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
535 @cindex @samp{^} in regexp
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 @samp{[^} begins a @dfn{complement character set}, which matches any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 character except the ones specified. Thus, @samp{[^a-z0-9A-Z]}
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
538 matches all characters @emph{except} letters and digits.@refill
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 @samp{^} is not special in a character set unless it is the first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 character. The character following the @samp{^} is treated as if it
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
542 were first (thus, @samp{-} and @samp{]} are not special there).
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 Note that a complement character set can match a newline, unless
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 newline is mentioned as one of the characters not to match.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 @item ^
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
548 @cindex @samp{^} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
549 @cindex beginning of line in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
550 is a special character that matches the empty string, but only at the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
551 beginning of a line in the text being matched. Otherwise it fails to
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
552 match anything. Thus, @samp{^foo} matches a @samp{foo} that occurs at
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
553 the beginning of a line.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
554
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
555 When matching a string instead of a buffer, @samp{^} matches at the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
556 beginning of the string or after a newline character @samp{\n}.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 @item $
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
559 @cindex @samp{$} in regexp
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 is similar to @samp{^} but matches only at the end of a line. Thus,
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
561 @samp{x+$} matches a string of one @samp{x} or more at the end of a line.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
562
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
563 When matching a string instead of a buffer, @samp{$} matches at the end
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
564 of the string or before a newline character @samp{\n}.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 @item \
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
567 @cindex @samp{\} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
568 has two functions: it quotes the special characters (including
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 @samp{\}), and it introduces additional special constructs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 Because @samp{\} quotes special characters, @samp{\$} is a regular
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 expression that matches only @samp{$}, and @samp{\[} is a regular
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
573 expression that matches only @samp{[}, and so on.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
574
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
575 @c Removed a paragraph here in lispref about doubling backslashes inside
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
576 @c of Lisp strings.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
577
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
580 @strong{Please note:} For historical compatibility, special characters
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
581 are treated as ordinary ones if they are in contexts where their special
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
582 meanings make no sense. For example, @samp{*foo} treats @samp{*} as
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
583 ordinary since there is no preceding expression on which the @samp{*}
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
584 can act. It is poor practice to depend on this behavior; quote the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
585 special character anyway, regardless of where it appears.@refill
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
587 For the most part, @samp{\} followed by any character matches only
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 that character. However, there are several exceptions: characters
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
589 that, when preceded by @samp{\}, are special constructs. Such
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 characters are always ordinary when encountered on their own. Here
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
591 is a table of @samp{\} constructs:
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 @table @kbd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 @item \|
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
595 @cindex @samp{|} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
596 @cindex regexp alternative
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 specifies an alternative.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 Two regular expressions @var{a} and @var{b} with @samp{\|} in
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
599 between form an expression that matches anything that either @var{a} or
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 @var{b} matches.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 Thus, @samp{foo\|bar} matches either @samp{foo} or @samp{bar}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 but no other string.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 @samp{\|} applies to the largest possible surrounding expressions. Only a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 surrounding @samp{\( @dots{} \)} grouping can limit the grouping power of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 @samp{\|}.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 Full backtracking capability exists to handle multiple uses of @samp{\|}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 @item \( @dots{} \)
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
612 @cindex @samp{(} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
613 @cindex @samp{)} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
614 @cindex regexp grouping
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 is a grouping construct that serves three purposes:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 @enumerate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 @item
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 To enclose a set of @samp{\|} alternatives for other operations.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 Thus, @samp{\(foo\|bar\)x} matches either @samp{foox} or @samp{barx}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 @item
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
623 To enclose an expression for a suffix operator such as @samp{*} to act
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
624 on. Thus, @samp{ba\(na\)*} matches @samp{bananana}, etc., with any
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
625 (zero or more) number of @samp{na} strings.@refill
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 @item
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
628 To record a matched substring for future reference.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 @end enumerate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 This last application is not a consequence of the idea of a
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
632 parenthetical grouping; it is a separate feature that happens to be
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 assigned as a second meaning to the same @samp{\( @dots{} \)} construct
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
634 because there is no conflict in practice between the two meanings.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
635 Here is an explanation of this feature:
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 @item \@var{digit}
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
638 matches the same text that matched the @var{digit}th occurrence of a
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
639 @samp{\( @dots{} \)} construct.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
641 In other words, after the end of a @samp{\( @dots{} \)} construct. the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
642 matcher remembers the beginning and end of the text matched by that
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
643 construct. Then, later on in the regular expression, you can use
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
644 @samp{\} followed by @var{digit} to match that same text, whatever it
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
645 may have been.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
646
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
647 The strings matching the first nine @samp{\( @dots{} \)} constructs
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
648 appearing in a regular expression are assigned numbers 1 through 9 in
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
649 the order that the open parentheses appear in the regular expression.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
650 So you can use @samp{\1} through @samp{\9} to refer to the text matched
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
651 by the corresponding @samp{\( @dots{} \)} constructs.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 For example, @samp{\(.*\)\1} matches any newline-free string that is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 composed of two identical halves. The @samp{\(.*\)} matches the first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 half, which may be anything, but the @samp{\1} that follows must match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 the same exact text.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
658 @item \(?: @dots{} \)
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
659 @cindex @samp{\(?:} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
660 @cindex regexp grouping
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
661 is called a @dfn{shy} grouping operator, and it is used just like
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
662 @samp{\( @dots{} \)}, except that it does not cause the matched
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
663 substring to be recorded for future reference.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
664
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
665 This is useful when you need a lot of grouping @samp{\( @dots{} \)}
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
666 constructs, but only want to remember one or two -- or if you have
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
667 more than nine groupings and need to use backreferences to refer to
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
668 the groupings at the end.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
669
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
670 Using @samp{\(?: @dots{} \)} rather than @samp{\( @dots{} \)} when you
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
671 don't need the captured substrings ought to speed up your programs some,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
672 since it shortens the code path followed by the regular expression
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
673 engine, as well as the amount of memory allocation and string copying it
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
674 must do. The actual performance gain to be observed has not been
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
675 measured or quantified as of this writing.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
676 @c This is used to good advantage by the font-locking code, and by
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
677 @c `regexp-opt.el'.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
678
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
679 The shy grouping operator has been borrowed from Perl, and has not been
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
680 available prior to XEmacs 20.3, nor is it available in FSF Emacs.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
681
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
682 @item \w
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
683 @cindex @samp{\w} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
684 matches any word-constituent character. The editor syntax table
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
685 determines which characters these are. @xref{Syntax}.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
686
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
687 @item \W
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
688 @cindex @samp{\W} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
689 matches any character that is not a word constituent.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
690
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
691 @item \s@var{code}
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
692 @cindex @samp{\s} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
693 matches any character whose syntax is @var{code}. Here @var{code} is a
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
694 character that represents a syntax code: thus, @samp{w} for word
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
695 constituent, @samp{-} for whitespace, @samp{(} for open parenthesis,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
696 etc. @xref{Syntax}, for a list of syntax codes and the characters that
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
697 stand for them.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
698
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
699 @item \S@var{code}
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
700 @cindex @samp{\S} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
701 matches any character whose syntax is not @var{code}.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
702 @end table
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
703
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
704 The following regular expression constructs match the empty string---that is,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
705 they don't use up any characters---but whether they match depends on the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
706 context.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
707
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
708 @table @kbd
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 @item \`
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
710 @cindex @samp{\`} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
711 matches the empty string, but only at the beginning
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
712 of the buffer or string being matched against.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 @item \'
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
715 @cindex @samp{\'} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
716 matches the empty string, but only at the end of
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
717 the buffer or string being matched against.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
718
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
719 @item \=
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
720 @cindex @samp{\=} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
721 matches the empty string, but only at point.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
722 (This construct is not defined when matching against a string.)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 @item \b
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
725 @cindex @samp{\b} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
726 matches the empty string, but only at the beginning or
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 end of a word. Thus, @samp{\bfoo\b} matches any occurrence of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 @samp{foo} as a separate word. @samp{\bballs?\b} matches
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 @samp{ball} or @samp{balls} as a separate word.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 @item \B
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
732 @cindex @samp{\B} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
733 matches the empty string, but @emph{not} at the beginning or
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 end of a word.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 @item \<
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
737 @cindex @samp{\<} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
738 matches the empty string, but only at the beginning of a word.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 @item \>
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
741 @cindex @samp{\>} in regexp
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 0
diff changeset
742 matches the empty string, but only at the end of a word.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 Here is a complicated regexp used by Emacs to recognize the end of a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 sentence together with any whitespace that follows. It is given in Lisp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 syntax to enable you to distinguish the spaces from the tab characters. In
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 Lisp syntax, the string constant begins and ends with a double-quote.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 @samp{\"} stands for a double-quote as part of the regexp, @samp{\\} for a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 backslash as part of the regexp, @samp{\t} for a tab and @samp{\n} for a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 newline.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 "[.?!][]\"')]*\\($\\|\t\\| \\)[ \t\n]*"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 @noindent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 This regexp contains four parts: a character set matching
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 period, @samp{?} or @samp{!}; a character set matching close-brackets,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 quotes or parentheses, repeated any number of times; an alternative in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 backslash-parentheses that matches end-of-line, a tab or two spaces; and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 a character set matching whitespace characters, repeated any number of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 times.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 @node Search Case, Replace, Regexps, Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 @section Searching and Case
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 @vindex case-fold-search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 All searches in Emacs normally ignore the case of the text they
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 are searching through; if you specify searching for @samp{FOO},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 @samp{Foo} and @samp{foo} are also considered a match. Regexps, and in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 particular character sets, are included: @samp{[aB]} matches @samp{a}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 or @samp{A} or @samp{b} or @samp{B}.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 If you want a case-sensitive search, set the variable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 @code{case-fold-search} to @code{nil}. Then all letters must match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 exactly, including case. @code{case-fold-search} is a per-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 variable; altering it affects only the current buffer, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 there is a default value which you can change as well. @xref{Locals}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 You can also use @b{Case Sensitive Search} from the @b{Options} menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781 on your screen.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 @node Replace, Other Repeating Search, Search Case, Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 @section Replacement Commands
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 @cindex replacement
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 @cindex string substitution
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 @cindex global substitution
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 Global search-and-replace operations are not needed as often in Emacs as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 they are in other editors, but they are available. In addition to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 simple @code{replace-string} command which is like that found in most
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 editors, there is a @code{query-replace} command which asks you, for each
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 occurrence of a pattern, whether to replace it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 The replace commands all replace one string (or regexp) with one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 replacement string. It is possible to perform several replacements in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 parallel using the command @code{expand-region-abbrevs}. @xref{Expanding
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 Abbrevs}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 @menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 * Unconditional Replace:: Replacing all matches for a string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 * Regexp Replace:: Replacing all matches for a regexp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 * Replacement and Case:: How replacements preserve case of letters.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 * Query Replace:: How to use querying.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 @end menu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 @node Unconditional Replace, Regexp Replace, Replace, Replace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808 @subsection Unconditional Replacement
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 @findex replace-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 @findex replace-regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 @table @kbd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 @item M-x replace-string @key{RET} @var{string} @key{RET} @var{newstring} @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814 Replace every occurrence of @var{string} with @var{newstring}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 @item M-x replace-regexp @key{RET} @var{regexp} @key{RET} @var{newstring} @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816 Replace every match for @var{regexp} with @var{newstring}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819 To replace every instance of @samp{foo} after point with @samp{bar},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 use the command @kbd{M-x replace-string} with the two arguments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821 @samp{foo} and @samp{bar}. Replacement occurs only after point: if you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822 want to cover the whole buffer you must go to the beginning first. By
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
823 default, all occurrences up to the end of the buffer are replaced. To
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
824 limit replacement to part of the buffer, narrow to that part of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
825 buffer before doing the replacement (@pxref{Narrowing}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
826
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827 When @code{replace-string} exits, point is left at the last occurrence
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
828 replaced. The value of point when the @code{replace-string} command was
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
829 issued is remembered on the mark ring; @kbd{C-u C-@key{SPC}} moves back
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
830 there.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
831
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
832 A numeric argument restricts replacement to matches that are surrounded
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833 by word boundaries.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
834
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
835 @node Regexp Replace, Replacement and Case, Unconditional Replace, Replace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836 @subsection Regexp Replacement
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838 @code{replace-string} replaces exact matches for a single string. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 similar command @code{replace-regexp} replaces any match for a specified
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
840 pattern.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
841
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
842 In @code{replace-regexp}, the @var{newstring} need not be constant. It
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
843 can refer to all or part of what is matched by the @var{regexp}. @samp{\&}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844 in @var{newstring} stands for the entire text being replaced.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845 @samp{\@var{d}} in @var{newstring}, where @var{d} is a digit, stands for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
846 whatever matched the @var{d}'th parenthesized grouping in @var{regexp}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
847 For example,@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
848
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
850 M-x replace-regexp @key{RET} c[ad]+r @key{RET} \&-safe @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
851 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
852
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
853 @noindent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
854 would replace (for example) @samp{cadr} with @samp{cadr-safe} and @samp{cddr}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
855 with @samp{cddr-safe}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
857 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
858 M-x replace-regexp @key{RET} \(c[ad]+r\)-safe @key{RET} \1 @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
859 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861 @noindent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
862 would perform exactly the opposite replacements. To include a @samp{\}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 in the text to replace with, you must give @samp{\\}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
864
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
865 @node Replacement and Case, Query Replace, Regexp Replace, Replace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866 @subsection Replace Commands and Case
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
868 @vindex case-replace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 @vindex case-fold-search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870 If the arguments to a replace command are in lower case, the command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871 preserves case when it makes a replacement. Thus, the following command:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873 @example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874 M-x replace-string @key{RET} foo @key{RET} bar @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875 @end example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
877 @noindent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878 replaces a lower-case @samp{foo} with a lower case @samp{bar}, @samp{FOO}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
879 with @samp{BAR}, and @samp{Foo} with @samp{Bar}. If upper-case letters are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880 used in the second argument, they remain upper-case every time that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
881 argument is inserted. If upper-case letters are used in the first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882 argument, the second argument is always substituted exactly as given, with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883 no case conversion. Likewise, if the variable @code{case-replace} is set
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884 to @code{nil}, replacement is done without case conversion. If
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 @code{case-fold-search} is set to @code{nil}, case is significant in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
886 matching occurrences of @samp{foo} to replace; also, case conversion of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
887 replacement string is not done.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
888
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
889 @node Query Replace,, Replacement and Case, Replace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
890 @subsection Query Replace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
891 @cindex query replace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
892
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
893 @table @kbd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 @item M-% @var{string} @key{RET} @var{newstring} @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895 @itemx M-x query-replace @key{RET} @var{string} @key{RET} @var{newstring} @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 Replace some occurrences of @var{string} with @var{newstring}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 @item M-x query-replace-regexp @key{RET} @var{regexp} @key{RET} @var{newstring} @key{RET}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898 Replace some matches for @var{regexp} with @var{newstring}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
900
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
901 @kindex M-%
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902 @findex query-replace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 If you want to change only some of the occurrences of @samp{foo} to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
904 @samp{bar}, not all of them, you can use @code{query-replace} instead of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905 @kbd{M-%}. This command finds occurrences of @samp{foo} one by one,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 displays each occurrence, and asks you whether to replace it. A numeric
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 argument to @code{query-replace} tells it to consider only occurrences
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 that are bounded by word-delimiter characters.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
910 @findex query-replace-regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
911 Aside from querying, @code{query-replace} works just like
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
912 @code{replace-string}, and @code{query-replace-regexp} works
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
913 just like @code{replace-regexp}.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
914
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
915 The things you can type when you are shown an occurrence of @var{string}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
916 or a match for @var{regexp} are:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
917
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
918 @kindex SPC (query-replace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
919 @kindex DEL (query-replace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
920 @kindex , (query-replace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
921 @kindex ESC (query-replace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
922 @kindex . (query-replace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
923 @kindex ! (query-replace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
924 @kindex ^ (query-replace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
925 @kindex C-r (query-replace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
926 @kindex C-w (query-replace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
927 @kindex C-l (query-replace)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
928
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
929 @c WideCommands
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
930 @table @kbd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
931 @item @key{SPC}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
932 to replace the occurrence with @var{newstring}. This preserves case, just
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
933 like @code{replace-string}, provided @code{case-replace} is non-@code{nil},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
934 as it normally is.@refill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
936 @item @key{DEL}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937 to skip to the next occurrence without replacing this one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
938
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
939 @item , @r{(Comma)}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
940 to replace this occurrence and display the result. You are then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
941 prompted for another input character. However, since the replacement has
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
942 already been made, @key{DEL} and @key{SPC} are equivalent. At this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
943 point, you can type @kbd{C-r} (see below) to alter the replaced text. To
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
944 undo the replacement, you can type @kbd{C-x u}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
945 This exits the @code{query-replace}. If you want to do further
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
946 replacement you must use @kbd{C-x ESC} to restart (@pxref{Repetition}).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
947
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
948 @item @key{ESC}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
949 to exit without doing any more replacements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
950
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
951 @item .@: @r{(Period)}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952 to replace this occurrence and then exit.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
953
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
954 @item !
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
955 to replace all remaining occurrences without asking again.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
956
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
957 @item ^
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
958 to go back to the location of the previous occurrence (or what used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
959 be an occurrence), in case you changed it by mistake. This works by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
960 popping the mark ring. Only one @kbd{^} in a row is allowed, because
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
961 only one previous replacement location is kept during @code{query-replace}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
962
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
963 @item C-r
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
964 to enter a recursive editing level, in case the occurrence needs to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
965 edited rather than just replaced with @var{newstring}. When you are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
966 done, exit the recursive editing level with @kbd{C-M-c} and the next
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
967 occurrence will be displayed. @xref{Recursive Edit}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
968
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
969 @item C-w
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
970 to delete the occurrence, and then enter a recursive editing level as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
971 in @kbd{C-r}. Use the recursive edit to insert text to replace the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
972 deleted occurrence of @var{string}. When done, exit the recursive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
973 editing level with @kbd{C-M-c} and the next occurrence will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
974 displayed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
975
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
976 @item C-l
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
977 to redisplay the screen and then give another answer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
978
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
979 @item C-h
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
980 to display a message summarizing these options, then give another
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
981 answer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
982 @end table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
983
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
984 If you type any other character, Emacs exits the @code{query-replace}, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
985 executes the character as a command. To restart the @code{query-replace},
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
986 use @kbd{C-x @key{ESC}}, which repeats the @code{query-replace} because it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
987 used the minibuffer to read its arguments. @xref{Repetition, C-x ESC}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
988
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
989 @node Other Repeating Search,, Replace, Search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
990 @section Other Search-and-Loop Commands
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
991
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
992 Here are some other commands that find matches for a regular expression.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
993 They all operate from point to the end of the buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
994
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
995 @findex list-matching-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
996 @findex occur
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
997 @findex count-matches
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
998 @findex delete-non-matching-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
999 @findex delete-matching-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1000 @c grosscommands
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1001 @table @kbd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1002 @item M-x occur
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1003 Print each line that follows point and contains a match for the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1004 specified regexp. A numeric argument specifies the number of context
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1005 lines to print before and after each matching line; the default is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1006 none.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1007
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1008 @kindex C-c C-c (Occur mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1009 The buffer @samp{*Occur*} containing the output serves as a menu for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1010 finding occurrences in their original context. Find an occurrence
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1011 as listed in @samp{*Occur*}, position point there, and type @kbd{C-c
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1012 C-c}; this switches to the buffer that was searched and moves point to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1013 the original of the same occurrence.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1014
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1015 @item M-x list-matching-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1016 Synonym for @kbd{M-x occur}.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1017
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1018 @item M-x count-matches
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1019 Print the number of matches following point for the specified regexp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1020
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1021 @item M-x delete-non-matching-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1022 Delete each line that follows point and does not contain a match for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1023 the specified regexp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1024
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1025 @item M-x delete-matching-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1026 Delete each line that follows point and contains a match for the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1027 specified regexp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1028 @end table