comparison man/lispref/searching.texi @ 444:576fb035e263 r21-2-37

Import from CVS: tag r21-2-37
author cvs
date Mon, 13 Aug 2007 11:36:19 +0200
parents abe6d1db359e
children 1ccc32a20af4
comparison
equal deleted inserted replaced
443:a8296e22da4e 444:576fb035e263
1 @c -*-texinfo-*- 1 @c -*-texinfo-*-
2 @c This is part of the XEmacs Lisp Reference Manual. 2 @c This is part of the XEmacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file lispref.texi for copying conditions. 4 @c See the file lispref.texi for copying conditions.
5 @setfilename ../../info/searching.info 5 @setfilename ../../info/searching.info
6 @node Searching and Matching, Syntax Tables, Text, Top 6 @node Searching and Matching, Syntax Tables, Text, Top
7 @chapter Searching and Matching 7 @chapter Searching and Matching
8 @cindex searching 8 @cindex searching
33 @cindex string search 33 @cindex string search
34 34
35 These are the primitive functions for searching through the text in a 35 These are the primitive functions for searching through the text in a
36 buffer. They are meant for use in programs, but you may call them 36 buffer. They are meant for use in programs, but you may call them
37 interactively. If you do so, they prompt for the search string; 37 interactively. If you do so, they prompt for the search string;
38 @var{limit} and @var{noerror} are set to @code{nil}, and @var{repeat} 38 @var{limit} and @var{noerror} are set to @code{nil}, and @var{count}
39 is set to 1. 39 is set to 1.
40 40
41 @deffn Command search-forward string &optional limit noerror repeat 41 @deffn Command search-forward string &optional limit noerror count buffer
42 This function searches forward from point for an exact match for 42 This function searches forward from point for an exact match for
43 @var{string}. If successful, it sets point to the end of the occurrence 43 @var{string}. If successful, it sets point to the end of the occurrence
44 found, and returns the new value of point. If no match is found, the 44 found, and returns the new value of point. If no match is found, the
45 value and side effects depend on @var{noerror} (see below). 45 value and side effects depend on @var{noerror} (see below).
46 @c Emacs 19 feature
47 46
48 In the following example, point is initially at the beginning of the 47 In the following example, point is initially at the beginning of the
49 line. Then @code{(search-forward "fox")} moves point after the last 48 line. Then @code{(search-forward "fox")} moves point after the last
50 letter of @samp{fox}: 49 letter of @samp{fox}:
51 50
79 @code{nil} nor @code{t}, then @code{search-forward} moves point to the 78 @code{nil} nor @code{t}, then @code{search-forward} moves point to the
80 upper bound and returns @code{nil}. (It would be more consistent now 79 upper bound and returns @code{nil}. (It would be more consistent now
81 to return the new position of point in that case, but some programs 80 to return the new position of point in that case, but some programs
82 may depend on a value of @code{nil}.) 81 may depend on a value of @code{nil}.)
83 82
84 If @var{repeat} is supplied (it must be a positive number), then the 83 If @var{count} is supplied (it must be an integer), then the search is
85 search is repeated that many times (each time starting at the end of the 84 repeated that many times (each time starting at the end of the previous
86 previous time's match). If these successive searches succeed, the 85 time's match). If @var{count} is negative, the search direction is
87 function succeeds, moving point and returning its new value. Otherwise 86 backward. If the successive searches succeed, the function succeeds,
88 the search fails. 87 moving point and returning its new value. Otherwise the search fails.
88
89 @var{buffer} is the buffer to search in, and defaults to the current buffer.
89 @end deffn 90 @end deffn
90 91
91 @deffn Command search-backward string &optional limit noerror repeat 92 @deffn Command search-backward string &optional limit noerror count buffer
92 This function searches backward from point for @var{string}. It is 93 This function searches backward from point for @var{string}. It is
93 just like @code{search-forward} except that it searches backwards and 94 just like @code{search-forward} except that it searches backwards and
94 leaves point at the beginning of the match. 95 leaves point at the beginning of the match.
95 @end deffn 96 @end deffn
96 97
97 @deffn Command word-search-forward string &optional limit noerror repeat 98 @deffn Command word-search-forward string &optional limit noerror count buffer
98 @cindex word search 99 @cindex word search
99 This function searches forward from point for a ``word'' match for 100 This function searches forward from point for a ``word'' match for
100 @var{string}. If it finds a match, it sets point to the end of the 101 @var{string}. If it finds a match, it sets point to the end of the
101 match found, and returns the new value of point. 102 match found, and returns the new value of point.
102 @c Emacs 19 feature
103 103
104 Word matching regards @var{string} as a sequence of words, disregarding 104 Word matching regards @var{string} as a sequence of words, disregarding
105 punctuation that separates them. It searches the buffer for the same 105 punctuation that separates them. It searches the buffer for the same
106 sequence of words. Each word must be distinct in the buffer (searching 106 sequence of words. Each word must be distinct in the buffer (searching
107 for the word @samp{ball} does not match the word @samp{balls}), but the 107 for the word @samp{ball} does not match the word @samp{balls}), but the
138 an error if the search fails. If @var{noerror} is @code{t}, then it 138 an error if the search fails. If @var{noerror} is @code{t}, then it
139 returns @code{nil} instead of signaling an error. If @var{noerror} is 139 returns @code{nil} instead of signaling an error. If @var{noerror} is
140 neither @code{nil} nor @code{t}, it moves point to @var{limit} (or the 140 neither @code{nil} nor @code{t}, it moves point to @var{limit} (or the
141 end of the buffer) and returns @code{nil}. 141 end of the buffer) and returns @code{nil}.
142 142
143 If @var{repeat} is non-@code{nil}, then the search is repeated that many 143 If @var{count} is non-@code{nil}, then the search is repeated that many
144 times. Point is positioned at the end of the last match. 144 times. Point is positioned at the end of the last match.
145
146 @var{buffer} is the buffer to search in, and defaults to the current buffer.
145 @end deffn 147 @end deffn
146 148
147 @deffn Command word-search-backward string &optional limit noerror repeat 149 @deffn Command word-search-backward string &optional limit noerror count buffer
148 This function searches backward from point for a word match to 150 This function searches backward from point for a word match to
149 @var{string}. This function is just like @code{word-search-forward} 151 @var{string}. This function is just like @code{word-search-forward}
150 except that it searches backward and normally leaves point at the 152 except that it searches backward and normally leaves point at the
151 beginning of the match. 153 beginning of the match.
152 @end deffn 154 @end deffn
583 @node Regexp Example 585 @node Regexp Example
584 @subsection Complex Regexp Example 586 @subsection Complex Regexp Example
585 587
586 Here is a complicated regexp, used by XEmacs to recognize the end of a 588 Here is a complicated regexp, used by XEmacs to recognize the end of a
587 sentence together with any whitespace that follows. It is the value of 589 sentence together with any whitespace that follows. It is the value of
588 the variable @code{sentence-end}. 590 the variable @code{sentence-end}.
589 591
590 First, we show the regexp as a string in Lisp syntax to distinguish 592 First, we show the regexp as a string in Lisp syntax to distinguish
591 spaces from tab characters. The string constant begins and ends with a 593 spaces from tab characters. The string constant begins and ends with a
592 double-quote. @samp{\"} stands for a double-quote as part of the 594 double-quote. @samp{\"} stands for a double-quote as part of the
593 string, @samp{\\} for a backslash as part of the string, @samp{\t} for a 595 string, @samp{\\} for a backslash as part of the string, @samp{\t} for a
602 604
603 @example 605 @example
604 @group 606 @group
605 sentence-end 607 sentence-end
606 @result{} 608 @result{}
607 "[.?!][]\"')@}]*\\($\\| $\\| \\| \\)[ 609 "[.?!][]\"')@}]*\\($\\| $\\| \\| \\)[
608 ]*" 610 ]*"
609 @end group 611 @end group
610 @end example 612 @end example
611 613
612 @noindent 614 @noindent
653 @cite{The XEmacs Reference Manual}. @xref{Regexp Search, , Regular Expression 655 @cite{The XEmacs Reference Manual}. @xref{Regexp Search, , Regular Expression
654 Search, emacs, The XEmacs Reference Manual}. Here we describe only the search 656 Search, emacs, The XEmacs Reference Manual}. Here we describe only the search
655 functions useful in programs. The principal one is 657 functions useful in programs. The principal one is
656 @code{re-search-forward}. 658 @code{re-search-forward}.
657 659
658 @deffn Command re-search-forward regexp &optional limit noerror repeat 660 @deffn Command re-search-forward regexp &optional limit noerror count buffer
659 This function searches forward in the current buffer for a string of 661 This function searches forward in the current buffer for a string of
660 text that is matched by the regular expression @var{regexp}. The 662 text that is matched by the regular expression @var{regexp}. The
661 function skips over any amount of text that is not matched by 663 function skips over any amount of text that is not matched by
662 @var{regexp}, and leaves point at the end of the first match found. 664 @var{regexp}, and leaves point at the end of the first match found.
663 It returns the new value of point. 665 It returns the new value of point.
672 @code{re-search-forward} does nothing and returns @code{nil}. If 674 @code{re-search-forward} does nothing and returns @code{nil}. If
673 @var{noerror} is neither @code{nil} nor @code{t}, then 675 @var{noerror} is neither @code{nil} nor @code{t}, then
674 @code{re-search-forward} moves point to @var{limit} (or the end of the 676 @code{re-search-forward} moves point to @var{limit} (or the end of the
675 buffer) and returns @code{nil}. 677 buffer) and returns @code{nil}.
676 678
677 If @var{repeat} is supplied (it must be a positive number), then the 679 If @var{count} is supplied (it must be a positive number), then the
678 search is repeated that many times (each time starting at the end of the 680 search is repeated that many times (each time starting at the end of the
679 previous time's match). If these successive searches succeed, the 681 previous time's match). If these successive searches succeed, the
680 function succeeds, moving point and returning its new value. Otherwise 682 function succeeds, moving point and returning its new value. Otherwise
681 the search fails. 683 the search fails.
682 684
702 ---------- Buffer: foo ---------- 704 ---------- Buffer: foo ----------
703 @end group 705 @end group
704 @end example 706 @end example
705 @end deffn 707 @end deffn
706 708
707 @deffn Command re-search-backward regexp &optional limit noerror repeat 709 @deffn Command re-search-backward regexp &optional limit noerror count buffer
708 This function searches backward in the current buffer for a string of 710 This function searches backward in the current buffer for a string of
709 text that is matched by the regular expression @var{regexp}, leaving 711 text that is matched by the regular expression @var{regexp}, leaving
710 point at the beginning of the first text found. 712 point at the beginning of the first text found.
711 713
712 This function is analogous to @code{re-search-forward}, but they are not 714 This function is analogous to @code{re-search-forward}, but they are not
721 A true mirror-image of @code{re-search-forward} would require a special 723 A true mirror-image of @code{re-search-forward} would require a special
722 feature for matching regexps from end to beginning. It's not worth the 724 feature for matching regexps from end to beginning. It's not worth the
723 trouble of implementing that. 725 trouble of implementing that.
724 @end deffn 726 @end deffn
725 727
726 @defun string-match regexp string &optional start 728 @defun string-match regexp string &optional start buffer
727 This function returns the index of the start of the first match for 729 This function returns the index of the start of the first match for
728 the regular expression @var{regexp} in @var{string}, or @code{nil} if 730 the regular expression @var{regexp} in @var{string}, or @code{nil} if
729 there is no match. If @var{start} is non-@code{nil}, the search starts 731 there is no match. If @var{start} is non-@code{nil}, the search starts
730 at that index in @var{string}. 732 at that index in @var{string}.
733
734
735 Optional arg @var{buffer} controls how case folding is done (according
736 to the value of @code{case-fold-search} in @var{buffer} and
737 @var{buffer}'s case tables) and defaults to the current buffer.
731 738
732 For example, 739 For example,
733 740
734 @example 741 @example
735 @group 742 @group
799 components are separated with the characters specified with 806 components are separated with the characters specified with
800 @code{path-separator}. Under Unix, @code{path-separator} will normally 807 @code{path-separator}. Under Unix, @code{path-separator} will normally
801 be @samp{:}, while under Windows, it will be @samp{;}. 808 be @samp{:}, while under Windows, it will be @samp{;}.
802 @end defun 809 @end defun
803 810
804 @defun looking-at regexp 811 @defun looking-at regexp &optional buffer
805 This function determines whether the text in the current buffer directly 812 This function determines whether the text in the current buffer directly
806 following point matches the regular expression @var{regexp}. ``Directly 813 following point matches the regular expression @var{regexp}. ``Directly
807 following'' means precisely that: the search is ``anchored'' and it can 814 following'' means precisely that: the search is ``anchored'' and it can
808 succeed only starting with the first character following point. The 815 succeed only starting with the first character following point. The
809 result is @code{t} if so, @code{nil} otherwise. 816 result is @code{t} if so, @code{nil} otherwise.
844 functions only when you really need the longest match. 851 functions only when you really need the longest match.
845 852
846 In Emacs versions prior to 19.29, these functions did not exist, and 853 In Emacs versions prior to 19.29, these functions did not exist, and
847 the functions described above implemented full POSIX backtracking. 854 the functions described above implemented full POSIX backtracking.
848 855
849 @defun posix-search-forward regexp &optional limit noerror repeat 856 @deffn Command posix-search-forward regexp &optional limit noerror count buffer
850 This is like @code{re-search-forward} except that it performs the full 857 This is like @code{re-search-forward} except that it performs the full
851 backtracking specified by the POSIX standard for regular expression 858 backtracking specified by the POSIX standard for regular expression
852 matching. 859 matching.
853 @end defun 860 @end deffn
854 861
855 @defun posix-search-backward regexp &optional limit noerror repeat 862 @deffn Command posix-search-backward regexp &optional limit noerror count buffer
856 This is like @code{re-search-backward} except that it performs the full 863 This is like @code{re-search-backward} except that it performs the full
857 backtracking specified by the POSIX standard for regular expression 864 backtracking specified by the POSIX standard for regular expression
858 matching. 865 matching.
859 @end defun 866 @end deffn
860 867
861 @defun posix-looking-at regexp 868 @defun posix-looking-at regexp &optional buffer
862 This is like @code{looking-at} except that it performs the full 869 This is like @code{looking-at} except that it performs the full
863 backtracking specified by the POSIX standard for regular expression 870 backtracking specified by the POSIX standard for regular expression
864 matching. 871 matching.
865 @end defun 872 @end defun
866 873
867 @defun posix-string-match regexp string &optional start 874 @defun posix-string-match regexp string &optional start buffer
868 This is like @code{string-match} except that it performs the full 875 This is like @code{string-match} except that it performs the full
869 backtracking specified by the POSIX standard for regular expression 876 backtracking specified by the POSIX standard for regular expression
870 matching. 877 matching.
878
879 Optional arg @var{buffer} controls how case folding is done (according
880 to the value of @code{case-fold-search} in @var{buffer} and
881 @var{buffer}'s case tables) and defaults to the current buffer.
871 @end defun 882 @end defun
872 883
873 @ignore 884 @ignore
874 @deffn Command delete-matching-lines regexp 885 @deffn Command delete-matching-lines regexp
875 This function is identical to @code{delete-non-matching-lines}, save 886 This function is identical to @code{delete-non-matching-lines}, save
898 @end group 909 @end group
899 @end example 910 @end example
900 @end deffn 911 @end deffn
901 912
902 @deffn Command flush-lines regexp 913 @deffn Command flush-lines regexp
903 This function is the same as @code{delete-matching-lines}. 914 This function is an alias of @code{delete-matching-lines}.
904 @end deffn 915 @end deffn
905 916
906 @defun delete-non-matching-lines regexp 917 @deffn Command delete-non-matching-lines regexp
907 This function deletes all lines following point which don't 918 This function deletes all lines following point which don't
908 contain a match for the regular expression @var{regexp}. 919 contain a match for the regular expression @var{regexp}.
909 @end defun 920 @end deffn
910 921
911 @deffn Command keep-lines regexp 922 @deffn Command keep-lines regexp
912 This function is the same as @code{delete-non-matching-lines}. 923 This function is the same as @code{delete-non-matching-lines}.
913 @end deffn 924 @end deffn
914 925
915 @deffn Command how-many regexp 926 @deffn Command count-matches regexp
916 This function counts the number of matches for @var{regexp} there are in 927 This function counts the number of matches for @var{regexp} there are in
917 the current buffer following point. It prints this number in 928 the current buffer following point. It prints this number in
918 the echo area, returning the string printed. 929 the echo area, returning the string printed.
919 @end deffn 930 @end deffn
920 931
921 @deffn Command count-matches regexp 932 @deffn Command how-many regexp
922 This function is a synonym of @code{how-many}. 933 This function is an alias of @code{count-matches}.
923 @end deffn 934 @end deffn
924 935
925 @deffn Command list-matching-lines regexp nlines 936 @deffn Command list-matching-lines regexp &optional nlines
926 This function is a synonym of @code{occur}. 937 This function is a synonym of @code{occur}.
927 Show all lines following point containing a match for @var{regexp}. 938 Show all lines following point containing a match for @var{regexp}.
928 Display each line with @var{nlines} lines before and after, 939 Display each line with @var{nlines} lines before and after,
929 or @code{-}@var{nlines} before if @var{nlines} is negative. 940 or @code{-}@var{nlines} before if @var{nlines} is negative.
930 @var{nlines} defaults to @code{list-matching-lines-default-context-lines}. 941 @var{nlines} defaults to @code{list-matching-lines-default-context-lines}.
1122 1133
1123 @example 1134 @example
1124 @group 1135 @group
1125 (string-match "\\(qu\\)\\(ick\\)" 1136 (string-match "\\(qu\\)\\(ick\\)"
1126 "The quick fox jumped quickly.") 1137 "The quick fox jumped quickly.")
1127 ;0123456789 1138 ;0123456789
1128 @result{} 4 1139 @result{} 4
1129 @end group 1140 @end group
1130 1141
1131 @group 1142 @group
1132 (match-string 0 "The quick fox jumped quickly.") 1143 (match-string 0 "The quick fox jumped quickly.")
1189 1200
1190 This function replaces the text matched by the last search with 1201 This function replaces the text matched by the last search with
1191 @var{replacement}. 1202 @var{replacement}.
1192 1203
1193 @cindex case in replacements 1204 @cindex case in replacements
1194 @defun replace-match replacement &optional fixedcase literal string 1205 @defun replace-match replacement &optional fixedcase literal string strbuffer
1195 This function replaces the text in the buffer (or in @var{string}) that 1206 This function replaces the text in the buffer (or in @var{string}) that
1196 was matched by the last search. It replaces that text with 1207 was matched by the last search. It replaces that text with
1197 @var{replacement}. 1208 @var{replacement}.
1198 1209
1199 If you did the last search in a buffer, you should specify @code{nil} 1210 If you did the last search in a buffer, you should specify @code{nil}
1202 and returns @code{t}. 1213 and returns @code{t}.
1203 1214
1204 If you did the search in a string, pass the same string as @var{string}. 1215 If you did the search in a string, pass the same string as @var{string}.
1205 Then @code{replace-match} does the replacement by constructing and 1216 Then @code{replace-match} does the replacement by constructing and
1206 returning a new string. 1217 returning a new string.
1218
1219 If the fourth argument @var{string} is a string, fifth argument
1220 @var{strbuffer} specifies the buffer to be used for syntax-table and
1221 case-table lookup and defaults to the current buffer. When @var{string}
1222 is not a string, the buffer that the match occurred in has automatically
1223 been remembered and you do not need to specify it.
1207 1224
1208 If @var{fixedcase} is non-@code{nil}, then the case of the replacement 1225 If @var{fixedcase} is non-@code{nil}, then the case of the replacement
1209 text is not changed; otherwise, the replacement text is converted to a 1226 text is not changed; otherwise, the replacement text is converted to a
1210 different case depending upon the capitalization of the text to be 1227 different case depending upon the capitalization of the text to be
1211 replaced. If the original text is all upper case, the replacement text 1228 replaced. If the original text is all upper case, the replacement text
1214 If the original text contains just one word, and that word is a capital 1231 If the original text contains just one word, and that word is a capital
1215 letter, @code{replace-match} considers this a capitalized first word 1232 letter, @code{replace-match} considers this a capitalized first word
1216 rather than all upper case. 1233 rather than all upper case.
1217 1234
1218 If @code{case-replace} is @code{nil}, then case conversion is not done, 1235 If @code{case-replace} is @code{nil}, then case conversion is not done,
1219 regardless of the value of @var{fixed-case}. @xref{Searching and Case}. 1236 regardless of the value of @var{fixedcase}. @xref{Searching and Case}.
1220 1237
1221 If @var{literal} is non-@code{nil}, then @var{replacement} is inserted 1238 If @var{literal} is non-@code{nil}, then @var{replacement} is inserted
1222 exactly as it is, the only alterations being case changes as needed. 1239 exactly as it is, the only alterations being case changes as needed.
1223 If it is @code{nil} (the default), then the character @samp{\} is treated 1240 If it is @code{nil} (the default), then the character @samp{\} is treated
1224 specially. If a @samp{\} appears in @var{replacement}, then it must be 1241 specially. If a @samp{\} appears in @var{replacement}, then it must be
1245 @subsection Accessing the Entire Match Data 1262 @subsection Accessing the Entire Match Data
1246 1263
1247 The functions @code{match-data} and @code{set-match-data} read or 1264 The functions @code{match-data} and @code{set-match-data} read or
1248 write the entire match data, all at once. 1265 write the entire match data, all at once.
1249 1266
1250 @defun match-data 1267 @defun match-data &optional integers reuse
1251 This function returns a newly constructed list containing all the 1268 This function returns a newly constructed list containing all the
1252 information on what text the last search matched. Element zero is the 1269 information on what text the last search matched. Element zero is the
1253 position of the beginning of the match for the whole expression; element 1270 position of the beginning of the match for the whole expression; element
1254 one is the position of the end of the match for the expression. The 1271 one is the position of the end of the match for the expression. The
1255 next two elements are the positions of the beginning and end of the 1272 next two elements are the positions of the beginning and end of the
1270 @end tex 1287 @end tex
1271 corresponds to @code{(match-end @var{n})}. 1288 corresponds to @code{(match-end @var{n})}.
1272 1289
1273 All the elements are markers or @code{nil} if matching was done on a 1290 All the elements are markers or @code{nil} if matching was done on a
1274 buffer, and all are integers or @code{nil} if matching was done on a 1291 buffer, and all are integers or @code{nil} if matching was done on a
1275 string with @code{string-match}. (In Emacs 18 and earlier versions, 1292 string with @code{string-match}. However, if the optional first
1276 markers were used even for matching on a string, except in the case 1293 argument @var{integers} is non-@code{nil}, always use integers (rather
1277 of the integer 0.) 1294 than markers) to represent buffer positions.
1295
1296 If the optional second argument @var{reuse} is a list, reuse it as part
1297 of the value. If @var{reuse} is long enough to hold all the values, and if
1298 @var{integers} is non-@code{nil}, no new lisp objects are created.
1278 1299
1279 As always, there must be no possibility of intervening searches between 1300 As always, there must be no possibility of intervening searches between
1280 the call to a search function and the call to @code{match-data} that is 1301 the call to a search function and the call to @code{match-data} that is
1281 intended to access the match data for that search. 1302 intended to access the match data for that search.
1282 1303
1322 @end group 1343 @end group
1323 @end example 1344 @end example
1324 1345
1325 You can save and restore the match data with @code{save-match-data}: 1346 You can save and restore the match data with @code{save-match-data}:
1326 1347
1327 @defmac save-match-data body@dots{} 1348 @defspec save-match-data body@dots{}
1328 This special form executes @var{body}, saving and restoring the match 1349 This special form executes @var{body}, saving and restoring the match
1329 data around it. 1350 data around it.
1330 @end defmac 1351 @end defspec
1331 1352
1332 You can use @code{set-match-data} together with @code{match-data} to 1353 You can use @code{set-match-data} together with @code{match-data} to
1333 imitate the effect of the special form @code{save-match-data}. This is 1354 imitate the effect of the special form @code{save-match-data}. This is
1334 useful for writing code that can run in Emacs 18. Here is how: 1355 useful for writing code that can run in Emacs 18. Here is how:
1335 1356