aboutsummaryrefslogtreecommitdiffstats
path: root/lispref
diff options
context:
space:
mode:
authorRichard M. Stallman2005-08-29 08:44:20 +0000
committerRichard M. Stallman2005-08-29 08:44:20 +0000
commit99543e8b33e5914918a8caa9b1c5b5cf5400c57b (patch)
tree0047f0ade76a9cb16f6aa9f651c87965ddc59f09 /lispref
parentf215a1b4dd7d8fa4d13084b288c1b4859b008c7f (diff)
downloademacs-99543e8b33e5914918a8caa9b1c5b5cf5400c57b.tar.gz
emacs-99543e8b33e5914918a8caa9b1c5b5cf5400c57b.zip
(Searching and Matching): Move node.
Rearrange contents and add overall explanation. (Searching and Case): Move node. (Searching and Matching): Update menu.
Diffstat (limited to 'lispref')
-rw-r--r--lispref/searching.texi458
1 files changed, 196 insertions, 262 deletions
diff --git a/lispref/searching.texi b/lispref/searching.texi
index e702469a14b..84de54984ae 100644
--- a/lispref/searching.texi
+++ b/lispref/searching.texi
@@ -16,13 +16,13 @@ portions of it.
16 16
17@menu 17@menu
18* String Search:: Search for an exact match. 18* String Search:: Search for an exact match.
19* Searching and Case:: Case-independent or case-significant searching.
19* Regular Expressions:: Describing classes of strings. 20* Regular Expressions:: Describing classes of strings.
20* Regexp Search:: Searching for a match for a regexp. 21* Regexp Search:: Searching for a match for a regexp.
21* POSIX Regexps:: Searching POSIX-style for the longest match. 22* POSIX Regexps:: Searching POSIX-style for the longest match.
22* Search and Replace:: Internals of @code{query-replace}.
23* Match Data:: Finding out which part of the text matched, 23* Match Data:: Finding out which part of the text matched,
24 after a string or regexp search. 24 after a string or regexp search.
25* Searching and Case:: Case-independent or case-significant searching. 25* Search and Replace:: Commands that loop, searching and replacing.
26* Standard Regexps:: Useful regexps for finding sentences, pages,... 26* Standard Regexps:: Useful regexps for finding sentences, pages,...
27@end menu 27@end menu
28 28
@@ -157,6 +157,53 @@ except that it searches backward and normally leaves point at the
157beginning of the match. 157beginning of the match.
158@end deffn 158@end deffn
159 159
160@node Searching and Case
161@section Searching and Case
162@cindex searching and case
163
164 By default, searches in Emacs ignore the case of the text they are
165searching through; if you specify searching for @samp{FOO}, then
166@samp{Foo} or @samp{foo} is also considered a match. This applies to
167regular expressions, too; thus, @samp{[aB]} would match @samp{a} or
168@samp{A} or @samp{b} or @samp{B}.
169
170 If you do not want this feature, set the variable
171@code{case-fold-search} to @code{nil}. Then all letters must match
172exactly, including case. This is a buffer-local variable; altering the
173variable affects only the current buffer. (@xref{Intro to
174Buffer-Local}.) Alternatively, you may change the value of
175@code{default-case-fold-search}, which is the default value of
176@code{case-fold-search} for buffers that do not override it.
177
178 Note that the user-level incremental search feature handles case
179distinctions differently. When given a lower case letter, it looks for
180a match of either case, but when given an upper case letter, it looks
181for an upper case letter only. But this has nothing to do with the
182searching functions used in Lisp code.
183
184@defopt case-replace
185This variable determines whether the higher level replacement
186functions should preserve case. If the variable is @code{nil}, that
187means to use the replacement text verbatim. A non-@code{nil} value
188means to convert the case of the replacement text according to the
189text being replaced.
190
191This variable is used by passing it as an argument to the function
192@code{replace-match}. @xref{Replacing Match}.
193@end defopt
194
195@defopt case-fold-search
196This buffer-local variable determines whether searches should ignore
197case. If the variable is @code{nil} they do not ignore case; otherwise
198they do ignore case.
199@end defopt
200
201@defvar default-case-fold-search
202The value of this variable is the default value for
203@code{case-fold-search} in buffers that do not override it. This is the
204same as @code{(default-value 'case-fold-search)}.
205@end defvar
206
160@node Regular Expressions 207@node Regular Expressions
161@section Regular Expressions 208@section Regular Expressions
162@cindex regular expression 209@cindex regular expression
@@ -1070,231 +1117,15 @@ backtracking specified by the POSIX standard for regular expression
1070matching. 1117matching.
1071@end defun 1118@end defun
1072 1119
1073@ignore
1074@deffn Command delete-matching-lines regexp
1075This function is identical to @code{delete-non-matching-lines}, save
1076that it deletes what @code{delete-non-matching-lines} keeps.
1077
1078In the example below, point is located on the first line of text.
1079
1080@example
1081@group
1082---------- Buffer: foo ----------
1083We hold these truths
1084to be self-evident,
1085that all men are created
1086equal, and that they are
1087---------- Buffer: foo ----------
1088@end group
1089
1090@group
1091(delete-matching-lines "the")
1092 @result{} nil
1093
1094---------- Buffer: foo ----------
1095to be self-evident,
1096that all men are created
1097---------- Buffer: foo ----------
1098@end group
1099@end example
1100@end deffn
1101
1102@deffn Command flush-lines regexp
1103This function is the same as @code{delete-matching-lines}.
1104@end deffn
1105
1106@defun delete-non-matching-lines regexp
1107This function deletes all lines following point which don't
1108contain a match for the regular expression @var{regexp}.
1109@end defun
1110
1111@deffn Command keep-lines regexp
1112This function is the same as @code{delete-non-matching-lines}.
1113@end deffn
1114
1115@deffn Command how-many regexp
1116This function counts the number of matches for @var{regexp} there are in
1117the current buffer following point. It prints this number in
1118the echo area, returning the string printed.
1119@end deffn
1120
1121@deffn Command count-matches regexp
1122This function is a synonym of @code{how-many}.
1123@end deffn
1124
1125@deffn Command list-matching-lines regexp &optional nlines
1126This function is a synonym of @code{occur}.
1127Show all lines following point containing a match for @var{regexp}.
1128Display each line with @var{nlines} lines before and after,
1129or @code{-}@var{nlines} before if @var{nlines} is negative.
1130@var{nlines} defaults to @code{list-matching-lines-default-context-lines}.
1131Interactively it is the prefix arg.
1132
1133The lines are shown in a buffer named @samp{*Occur*}.
1134It serves as a menu to find any of the occurrences in this buffer.
1135@kbd{C-h m} (@code{describe-mode}) in that buffer gives help.
1136@end deffn
1137
1138@defopt list-matching-lines-default-context-lines
1139Default value is 0.
1140Default number of context lines to include around a @code{list-matching-lines}
1141match. A negative number means to include that many lines before the match.
1142A positive number means to include that many lines both before and after.
1143@end defopt
1144@end ignore
1145
1146@node Search and Replace
1147@section Search and Replace
1148@cindex replacement
1149
1150@defun replace-regexp-in-string regexp rep string &optional fixedcase literal subexp start
1151This function copies @var{string} and searches it for matches for
1152@var{regexp}, and replaces them with @var{rep}. It returns the
1153modified copy. If @var{start} is non-@code{nil}, the search for
1154matches starts at that index in @var{string}, so matches starting
1155before that index are not changed.
1156
1157This function uses @code{replace-match} to do the replacement, and it
1158passes the optional arguments @var{fixedcase}, @var{literal} and
1159@var{subexp} along to @code{replace-match}.
1160
1161Instead of a string, @var{rep} can be a function. In that case,
1162@code{replace-regexp-in-string} calls @var{rep} for each match,
1163passing the text of the match as its sole argument. It collects the
1164value @var{rep} returns and passes that to @code{replace-match} as the
1165replacement string. The match-data at this point are the result
1166of matching @var{regexp} against a substring of @var{string}.
1167@end defun
1168
1169@defun perform-replace from-string replacements query-flag regexp-flag delimited-flag &optional repeat-count map start end
1170This function is the guts of @code{query-replace} and related
1171commands. It searches for occurrences of @var{from-string} in the
1172text between positions @var{start} and @var{end} and replaces some or
1173all of them. If @var{start} is @code{nil} (or omitted), point is used
1174instead, and the end of the buffer's accessible portion is used for
1175@var{end}.
1176
1177If @var{query-flag} is @code{nil}, it replaces all
1178occurrences; otherwise, it asks the user what to do about each one.
1179
1180If @var{regexp-flag} is non-@code{nil}, then @var{from-string} is
1181considered a regular expression; otherwise, it must match literally. If
1182@var{delimited-flag} is non-@code{nil}, then only replacements
1183surrounded by word boundaries are considered.
1184
1185The argument @var{replacements} specifies what to replace occurrences
1186with. If it is a string, that string is used. It can also be a list of
1187strings, to be used in cyclic order.
1188
1189If @var{replacements} is a cons cell, @code{(@var{function}
1190. @var{data})}, this means to call @var{function} after each match to
1191get the replacement text. This function is called with two arguments:
1192@var{data}, and the number of replacements already made.
1193
1194If @var{repeat-count} is non-@code{nil}, it should be an integer. Then
1195it specifies how many times to use each of the strings in the
1196@var{replacements} list before advancing cyclically to the next one.
1197
1198If @var{from-string} contains upper-case letters, then
1199@code{perform-replace} binds @code{case-fold-search} to @code{nil}, and
1200it uses the @code{replacements} without altering the case of them.
1201
1202Normally, the keymap @code{query-replace-map} defines the possible user
1203responses for queries. The argument @var{map}, if non-@code{nil}, is a
1204keymap to use instead of @code{query-replace-map}.
1205
1206@strong{Usage note:} Do not use this function in your own programs
1207unless you want to do something very similar to what
1208@code{query-replace} does, including setting the mark and possibly
1209querying the user. For most purposes a simple loop like, for
1210instance:
1211
1212@example
1213(while (re-search-forward "foo[ \t]+bar" nil t)
1214 (replace-match "foobar"))
1215@end example
1216
1217@noindent
1218is preferable. It runs faster and avoids side effects, such as
1219setting the mark. @xref{Replacing Match,, Replacing the Text that
1220Matched}, for a description of @code{replace-match}.
1221@end defun
1222
1223@defvar query-replace-map
1224This variable holds a special keymap that defines the valid user
1225responses for @code{query-replace} and related functions, as well as
1226@code{y-or-n-p} and @code{map-y-or-n-p}. It is unusual in two ways:
1227
1228@itemize @bullet
1229@item
1230The ``key bindings'' are not commands, just symbols that are meaningful
1231to the functions that use this map.
1232
1233@item
1234Prefix keys are not supported; each key binding must be for a
1235single-event key sequence. This is because the functions don't use
1236@code{read-key-sequence} to get the input; instead, they read a single
1237event and look it up ``by hand.''
1238@end itemize
1239@end defvar
1240
1241Here are the meaningful ``bindings'' for @code{query-replace-map}.
1242Several of them are meaningful only for @code{query-replace} and
1243friends.
1244
1245@table @code
1246@item act
1247Do take the action being considered---in other words, ``yes.''
1248
1249@item skip
1250Do not take action for this question---in other words, ``no.''
1251
1252@item exit
1253Answer this question ``no,'' and give up on the entire series of
1254questions, assuming that the answers will be ``no.''
1255
1256@item act-and-exit
1257Answer this question ``yes,'' and give up on the entire series of
1258questions, assuming that subsequent answers will be ``no.''
1259
1260@item act-and-show
1261Answer this question ``yes,'' but show the results---don't advance yet
1262to the next question.
1263
1264@item automatic
1265Answer this question and all subsequent questions in the series with
1266``yes,'' without further user interaction.
1267
1268@item backup
1269Move back to the previous place that a question was asked about.
1270
1271@item edit
1272Enter a recursive edit to deal with this question---instead of any
1273other action that would normally be taken.
1274
1275@item delete-and-edit
1276Delete the text being considered, then enter a recursive edit to replace
1277it.
1278
1279@item recenter
1280Redisplay and center the window, then ask the same question again.
1281
1282@item quit
1283Perform a quit right away. Only @code{y-or-n-p} and related functions
1284use this answer.
1285
1286@item help
1287Display some help, then ask again.
1288@end table
1289
1290@node Match Data 1120@node Match Data
1291@section The Match Data 1121@section The Match Data
1292@cindex match data 1122@cindex match data
1293 1123
1294 Emacs keeps track of the start and end positions of the segments of 1124 Emacs keeps track of the start and end positions of the segments of
1295text found during a search. This means, for example, that you can 1125text found during a search; this is called the @dfn{match data}.
1296search for a complex pattern, such as a date in an Rmail message, and 1126Thanks to the match data, you can search for a complex pattern, such
1297then extract parts of the match under control of the pattern. 1127as a date in a mail message, and then extract parts of the match under
1128control of the pattern.
1298 1129
1299 Because the match data normally describe the most recent search only, 1130 Because the match data normally describe the most recent search only,
1300you must be careful not to do another search inadvertently between the 1131you must be careful not to do another search inadvertently between the
@@ -1313,8 +1144,8 @@ match data around it, to prevent it from being overwritten.
1313@node Replacing Match 1144@node Replacing Match
1314@subsection Replacing the Text that Matched 1145@subsection Replacing the Text that Matched
1315 1146
1316 This function replaces the text matched by the last search with 1147 This function replaces all or part of the text matched by the last
1317@var{replacement}. 1148search. It works by means of the match data.
1318 1149
1319@cindex case in replacements 1150@cindex case in replacements
1320@defun replace-match replacement &optional fixedcase literal string subexp 1151@defun replace-match replacement &optional fixedcase literal string subexp
@@ -1661,53 +1492,156 @@ associated with it still exists.
1661@end smallexample 1492@end smallexample
1662@end ignore 1493@end ignore
1663 1494
1664@node Searching and Case 1495@node Search and Replace
1665@section Searching and Case 1496@section Search and Replace
1666@cindex searching and case 1497@cindex replacement
1667 1498
1668 By default, searches in Emacs ignore the case of the text they are 1499 If you want to find all matches for a regexp in part of the buffer,
1669searching through; if you specify searching for @samp{FOO}, then 1500and replace them, the best way is to write an explicit loop using
1670@samp{Foo} or @samp{foo} is also considered a match. This applies to 1501@code{re-search-forward} and @code{replace-match}, like this:
1671regular expressions, too; thus, @samp{[aB]} would match @samp{a} or
1672@samp{A} or @samp{b} or @samp{B}.
1673 1502
1674 If you do not want this feature, set the variable 1503@example
1675@code{case-fold-search} to @code{nil}. Then all letters must match 1504(while (re-search-forward "foo[ \t]+bar" nil t)
1676exactly, including case. This is a buffer-local variable; altering the 1505 (replace-match "foobar"))
1677variable affects only the current buffer. (@xref{Intro to 1506@end example
1678Buffer-Local}.) Alternatively, you may change the value of
1679@code{default-case-fold-search}, which is the default value of
1680@code{case-fold-search} for buffers that do not override it.
1681 1507
1682 Note that the user-level incremental search feature handles case 1508@noindent
1683distinctions differently. When given a lower case letter, it looks for 1509@xref{Replacing Match,, Replacing the Text that Matched}, for a
1684a match of either case, but when given an upper case letter, it looks 1510description of @code{replace-match}.
1685for an upper case letter only. But this has nothing to do with the
1686searching functions used in Lisp code.
1687 1511
1688@defopt case-replace 1512 However, replacing matches in a string is more complex, especially
1689This variable determines whether the higher level replacement 1513if you want to do it efficiently. So Emacs provides a function to do
1690functions should preserve case. If the variable is @code{nil}, that 1514this.
1691means to use the replacement text verbatim. A non-@code{nil} value
1692means to convert the case of the replacement text according to the
1693text being replaced.
1694 1515
1695This variable is used by passing it as an argument to the function 1516@defun replace-regexp-in-string regexp rep string &optional fixedcase literal subexp start
1696@code{replace-match}. @xref{Replacing Match}. 1517This function copies @var{string} and searches it for matches for
1697@end defopt 1518@var{regexp}, and replaces them with @var{rep}. It returns the
1519modified copy. If @var{start} is non-@code{nil}, the search for
1520matches starts at that index in @var{string}, so matches starting
1521before that index are not changed.
1698 1522
1699@defopt case-fold-search 1523This function uses @code{replace-match} to do the replacement, and it
1700This buffer-local variable determines whether searches should ignore 1524passes the optional arguments @var{fixedcase}, @var{literal} and
1701case. If the variable is @code{nil} they do not ignore case; otherwise 1525@var{subexp} along to @code{replace-match}.
1702they do ignore case.
1703@end defopt
1704 1526
1705@defvar default-case-fold-search 1527Instead of a string, @var{rep} can be a function. In that case,
1706The value of this variable is the default value for 1528@code{replace-regexp-in-string} calls @var{rep} for each match,
1707@code{case-fold-search} in buffers that do not override it. This is the 1529passing the text of the match as its sole argument. It collects the
1708same as @code{(default-value 'case-fold-search)}. 1530value @var{rep} returns and passes that to @code{replace-match} as the
1531replacement string. The match-data at this point are the result
1532of matching @var{regexp} against a substring of @var{string}.
1533@end defun
1534
1535 If you want to write a command along the lines of @code{query-replace},
1536you can use @code{perform-replace} to do the work.
1537
1538@defun perform-replace from-string replacements query-flag regexp-flag delimited-flag &optional repeat-count map start end
1539This function is the guts of @code{query-replace} and related
1540commands. It searches for occurrences of @var{from-string} in the
1541text between positions @var{start} and @var{end} and replaces some or
1542all of them. If @var{start} is @code{nil} (or omitted), point is used
1543instead, and the end of the buffer's accessible portion is used for
1544@var{end}.
1545
1546If @var{query-flag} is @code{nil}, it replaces all
1547occurrences; otherwise, it asks the user what to do about each one.
1548
1549If @var{regexp-flag} is non-@code{nil}, then @var{from-string} is
1550considered a regular expression; otherwise, it must match literally. If
1551@var{delimited-flag} is non-@code{nil}, then only replacements
1552surrounded by word boundaries are considered.
1553
1554The argument @var{replacements} specifies what to replace occurrences
1555with. If it is a string, that string is used. It can also be a list of
1556strings, to be used in cyclic order.
1557
1558If @var{replacements} is a cons cell, @code{(@var{function}
1559. @var{data})}, this means to call @var{function} after each match to
1560get the replacement text. This function is called with two arguments:
1561@var{data}, and the number of replacements already made.
1562
1563If @var{repeat-count} is non-@code{nil}, it should be an integer. Then
1564it specifies how many times to use each of the strings in the
1565@var{replacements} list before advancing cyclically to the next one.
1566
1567If @var{from-string} contains upper-case letters, then
1568@code{perform-replace} binds @code{case-fold-search} to @code{nil}, and
1569it uses the @code{replacements} without altering the case of them.
1570
1571Normally, the keymap @code{query-replace-map} defines the possible
1572user responses for queries. The argument @var{map}, if
1573non-@code{nil}, specifies a keymap to use instead of
1574@code{query-replace-map}.
1575@end defun
1576
1577@defvar query-replace-map
1578This variable holds a special keymap that defines the valid user
1579responses for @code{perform-replace} and the commands that use it, as
1580well as @code{y-or-n-p} and @code{map-y-or-n-p}. This map is unusual
1581in two ways:
1582
1583@itemize @bullet
1584@item
1585The ``key bindings'' are not commands, just symbols that are meaningful
1586to the functions that use this map.
1587
1588@item
1589Prefix keys are not supported; each key binding must be for a
1590single-event key sequence. This is because the functions don't use
1591@code{read-key-sequence} to get the input; instead, they read a single
1592event and look it up ``by hand.''
1593@end itemize
1709@end defvar 1594@end defvar
1710 1595
1596Here are the meaningful ``bindings'' for @code{query-replace-map}.
1597Several of them are meaningful only for @code{query-replace} and
1598friends.
1599
1600@table @code
1601@item act
1602Do take the action being considered---in other words, ``yes.''
1603
1604@item skip
1605Do not take action for this question---in other words, ``no.''
1606
1607@item exit
1608Answer this question ``no,'' and give up on the entire series of
1609questions, assuming that the answers will be ``no.''
1610
1611@item act-and-exit
1612Answer this question ``yes,'' and give up on the entire series of
1613questions, assuming that subsequent answers will be ``no.''
1614
1615@item act-and-show
1616Answer this question ``yes,'' but show the results---don't advance yet
1617to the next question.
1618
1619@item automatic
1620Answer this question and all subsequent questions in the series with
1621``yes,'' without further user interaction.
1622
1623@item backup
1624Move back to the previous place that a question was asked about.
1625
1626@item edit
1627Enter a recursive edit to deal with this question---instead of any
1628other action that would normally be taken.
1629
1630@item delete-and-edit
1631Delete the text being considered, then enter a recursive edit to replace
1632it.
1633
1634@item recenter
1635Redisplay and center the window, then ask the same question again.
1636
1637@item quit
1638Perform a quit right away. Only @code{y-or-n-p} and related functions
1639use this answer.
1640
1641@item help
1642Display some help, then ask again.
1643@end table
1644
1711@node Standard Regexps 1645@node Standard Regexps
1712@section Standard Regular Expressions Used in Editing 1646@section Standard Regular Expressions Used in Editing
1713@cindex regexps used standardly in editing 1647@cindex regexps used standardly in editing