aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2004-06-25 21:28:14 +0000
committerRichard M. Stallman2004-06-25 21:28:14 +0000
commita745ff66c6901590b190b09663edc3ab3839fe51 (patch)
tree962c392bf933ecc007f3a76f6d9a956b4b638d64
parentbc071f64e5a169e5dbcf7f05509a8bfac607f639 (diff)
downloademacs-a745ff66c6901590b190b09663edc3ab3839fe51.tar.gz
emacs-a745ff66c6901590b190b09663edc3ab3839fe51.zip
(Regexp Replace): Rewrite description of \# \, and \?.
-rw-r--r--man/ChangeLog4
-rw-r--r--man/search.texi69
2 files changed, 44 insertions, 29 deletions
diff --git a/man/ChangeLog b/man/ChangeLog
index a3fbdf4d2e2..7d61928a29b 100644
--- a/man/ChangeLog
+++ b/man/ChangeLog
@@ -1,3 +1,7 @@
12004-06-25 Richard M. Stallman <rms@gnu.org>
2
3 * search.texi (Regexp Replace): Rewrite description of \# \, and \?.
4
12004-06-25 David Kastrup <dak@gnu.org> 52004-06-25 David Kastrup <dak@gnu.org>
2 6
3 * search.texi (Regexp Replace): Some typo corrections and 7 * search.texi (Regexp Replace): Some typo corrections and
diff --git a/man/search.texi b/man/search.texi
index 6eafedf7749..4577ac4b33e 100644
--- a/man/search.texi
+++ b/man/search.texi
@@ -993,13 +993,15 @@ in your text.
993single string. The similar command @kbd{M-x replace-regexp} replaces 993single string. The similar command @kbd{M-x replace-regexp} replaces
994any match for a specified pattern. 994any match for a specified pattern.
995 995
996 In @code{replace-regexp}, the @var{newstring} need not be constant: it 996 In @code{replace-regexp}, the @var{newstring} need not be constant:
997can refer to all or part of what is matched by the @var{regexp}. 997it can refer to all or part of what is matched by the @var{regexp}.
998@samp{\&} in @var{newstring} stands for the entire match being replaced. 998@samp{\&} in @var{newstring} stands for the entire match being
999@samp{\@var{d}} in @var{newstring}, where @var{d} is a digit, stands for 999replaced. @samp{\@var{d}} in @var{newstring}, where @var{d} is a
1000whatever matched the @var{d}th parenthesized grouping in @var{regexp}. 1000digit, stands for whatever matched the @var{d}th parenthesized
1001To include a @samp{\} in the text to replace with, you must enter 1001grouping in @var{regexp}. @samp{\#} refers to the count of
1002@samp{\\}. For example, 1002replacements already made in this command, as a decimal number. In
1003the first replacement, @samp{\#} stands for @samp{0}; in the second,
1004for @samp{1}; and so on. For example,
1003 1005
1004@example 1006@example
1005M-x replace-regexp @key{RET} c[ad]+r @key{RET} \&-safe @key{RET} 1007M-x replace-regexp @key{RET} c[ad]+r @key{RET} \&-safe @key{RET}
@@ -1014,17 +1016,26 @@ M-x replace-regexp @key{RET} \(c[ad]+r\)-safe @key{RET} \1 @key{RET}
1014@end example 1016@end example
1015 1017
1016@noindent 1018@noindent
1017performs the inverse transformation. 1019performs the inverse transformation. To include a @samp{\} in the
1018 1020text to replace with, you must enter @samp{\\}.
1019 You can also use arbitrary Lisp expressions evaluated at replacement 1021
1020time by placing @samp{\,} before them in the replacement string. Inside 1022 You can also use Lisp expressions to calculate parts of the
1021of those expressions, the symbols @samp{\&} and @samp{\@var{n}} refer to 1023replacement string. To do this, write @samp{\,} followed by the
1022match and submatch strings like described above (though @var{n} may 1024expression in the replacement string. Each replacement calculates the
1023exceed 9 here, and you get @code{nil} if nothing matches). @samp{\#&} 1025value of the expression, which ought to be a string, and uses it in
1024and @samp{\#@var{n}} refer to those strings converted to numbers. 1026the replacement string in place of the expression itself. If the
1025@samp{\#} is short for @samp{replace-count}, the number of already 1027expression is a symbol, one space in the replacement string after the
1026completed replacements. This particular shorthand can also be used 1028symbol name counts as part of the symbol name, so the value replaces
1027outside of @samp{\,}. 1029them both.
1030
1031 Inside such an expression, @samp{\&} and @samp{\@var{n}} used as
1032subexpressions refer respectively to the entire match as a string, and
1033to a submatch as a string. @var{n} may exceed 9 here, and the value
1034of @samp{\@var{n}} is @code{nil} if subexpression @var{n} did not
1035match. You can also use @samp{\#&} and @samp{\#@var{n}} refer to
1036those matches converted to numbers (this is valid when the match or
1037submatch has the form of a number). @samp{\#} stands for the number
1038of already-completed replacements.
1028 1039
1029 Repeating our example to exchange @samp{x} and @samp{y}, we can thus 1040 Repeating our example to exchange @samp{x} and @samp{y}, we can thus
1030do it also this way: 1041do it also this way:
@@ -1034,21 +1045,21 @@ M-x replace-regexp @key{RET} \(x\)\|y @key{RET}
1034\,(if \1 "y" "x") @key{RET} 1045\,(if \1 "y" "x") @key{RET}
1035@end example 1046@end example
1036 1047
1037 One function that comes handy in Lisp replacements is @samp{format} 1048 The @code{format} function (@pxref{Formatting Strings,,,elisp, GNU
1038(@pxref{Formatting Strings,,,elisp, GNU Emacs Lisp Reference Manual}). 1049Emacs Lisp Reference Manual}) comes in handy for computing replacement
1039For example, to add consecutively numbered strings like @samp{ABC00042} 1050strings for @samp{\,}. For example, to add consecutively numbered
1040to columns 73 @w{to 80} (unless they are already occupied), you can use 1051strings like @samp{ABC00042} to columns 73 @w{to 80} (unless they are
1052already occupied), you can use
1041 1053
1042@example 1054@example
1043M-x replace-regexp @key{RET} ^.\@{0,72\@}$ @key{RET} 1055M-x replace-regexp @key{RET} ^.\@{0,72\@}$ @key{RET}
1044\,(format "%-72sABC%05d" \& \#) @key{RET} 1056\,(format "%-72sABC%05d" \& \#) @key{RET}
1045@end example 1057@end example
1046 1058
1047 Another feature you can use in the replacement string of Regexp 1059 If you want to enter part of the replacement string by hand each
1048commands is @samp{\?}. In that case you will be allowed to edit the 1060time, use @samp{\?} in the replacement string. Each replacement will
1049replacement string at the given position before the replacement gets 1061enter a recursive edit, with point at the position where the @samp{\?}
1050performed. Lisp style replacements are performed before @samp{\?} gets 1062was. For example,
1051executed. For example,
1052 1063
1053@example 1064@example
1054M-x replace-regexp @key{RET} \footnote@{ @key{RET} 1065M-x replace-regexp @key{RET} \footnote@{ @key{RET}
@@ -1058,8 +1069,8 @@ M-x replace-regexp @key{RET} \footnote@{ @key{RET}
1058@noindent 1069@noindent
1059will add labels starting with @samp{\label@{fn:0@}} to occurences of 1070will add labels starting with @samp{\label@{fn:0@}} to occurences of
1060@samp{\footnote@{}, but letting you edit each replacement before 1071@samp{\footnote@{}, but letting you edit each replacement before
1061performing it. If you want labels starting at 1, use @samp{\,(1+ \#)} 1072performing it. To number the labels starting at 1, use @samp{\,(1+
1062instead of @samp{\#}. 1073\#)} instead of @samp{\#}.
1063 1074
1064@node Replacement and Case, Query Replace, Regexp Replace, Replace 1075@node Replacement and Case, Query Replace, Regexp Replace, Replace
1065@subsection Replace Commands and Case 1076@subsection Replace Commands and Case