diff options
| author | Richard M. Stallman | 2004-06-25 21:28:14 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 2004-06-25 21:28:14 +0000 |
| commit | a745ff66c6901590b190b09663edc3ab3839fe51 (patch) | |
| tree | 962c392bf933ecc007f3a76f6d9a956b4b638d64 | |
| parent | bc071f64e5a169e5dbcf7f05509a8bfac607f639 (diff) | |
| download | emacs-a745ff66c6901590b190b09663edc3ab3839fe51.tar.gz emacs-a745ff66c6901590b190b09663edc3ab3839fe51.zip | |
(Regexp Replace): Rewrite description of \# \, and \?.
| -rw-r--r-- | man/ChangeLog | 4 | ||||
| -rw-r--r-- | man/search.texi | 69 |
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 @@ | |||
| 1 | 2004-06-25 Richard M. Stallman <rms@gnu.org> | ||
| 2 | |||
| 3 | * search.texi (Regexp Replace): Rewrite description of \# \, and \?. | ||
| 4 | |||
| 1 | 2004-06-25 David Kastrup <dak@gnu.org> | 5 | 2004-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. | |||
| 993 | single string. The similar command @kbd{M-x replace-regexp} replaces | 993 | single string. The similar command @kbd{M-x replace-regexp} replaces |
| 994 | any match for a specified pattern. | 994 | any 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: |
| 997 | can refer to all or part of what is matched by the @var{regexp}. | 997 | it 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 | 999 | replaced. @samp{\@var{d}} in @var{newstring}, where @var{d} is a |
| 1000 | whatever matched the @var{d}th parenthesized grouping in @var{regexp}. | 1000 | digit, stands for whatever matched the @var{d}th parenthesized |
| 1001 | To include a @samp{\} in the text to replace with, you must enter | 1001 | grouping in @var{regexp}. @samp{\#} refers to the count of |
| 1002 | @samp{\\}. For example, | 1002 | replacements already made in this command, as a decimal number. In |
| 1003 | the first replacement, @samp{\#} stands for @samp{0}; in the second, | ||
| 1004 | for @samp{1}; and so on. For example, | ||
| 1003 | 1005 | ||
| 1004 | @example | 1006 | @example |
| 1005 | M-x replace-regexp @key{RET} c[ad]+r @key{RET} \&-safe @key{RET} | 1007 | M-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 |
| 1017 | performs the inverse transformation. | 1019 | performs the inverse transformation. To include a @samp{\} in the |
| 1018 | 1020 | text to replace with, you must enter @samp{\\}. | |
| 1019 | You can also use arbitrary Lisp expressions evaluated at replacement | 1021 | |
| 1020 | time by placing @samp{\,} before them in the replacement string. Inside | 1022 | You can also use Lisp expressions to calculate parts of the |
| 1021 | of those expressions, the symbols @samp{\&} and @samp{\@var{n}} refer to | 1023 | replacement string. To do this, write @samp{\,} followed by the |
| 1022 | match and submatch strings like described above (though @var{n} may | 1024 | expression in the replacement string. Each replacement calculates the |
| 1023 | exceed 9 here, and you get @code{nil} if nothing matches). @samp{\#&} | 1025 | value of the expression, which ought to be a string, and uses it in |
| 1024 | and @samp{\#@var{n}} refer to those strings converted to numbers. | 1026 | the replacement string in place of the expression itself. If the |
| 1025 | @samp{\#} is short for @samp{replace-count}, the number of already | 1027 | expression is a symbol, one space in the replacement string after the |
| 1026 | completed replacements. This particular shorthand can also be used | 1028 | symbol name counts as part of the symbol name, so the value replaces |
| 1027 | outside of @samp{\,}. | 1029 | them both. |
| 1030 | |||
| 1031 | Inside such an expression, @samp{\&} and @samp{\@var{n}} used as | ||
| 1032 | subexpressions refer respectively to the entire match as a string, and | ||
| 1033 | to a submatch as a string. @var{n} may exceed 9 here, and the value | ||
| 1034 | of @samp{\@var{n}} is @code{nil} if subexpression @var{n} did not | ||
| 1035 | match. You can also use @samp{\#&} and @samp{\#@var{n}} refer to | ||
| 1036 | those matches converted to numbers (this is valid when the match or | ||
| 1037 | submatch has the form of a number). @samp{\#} stands for the number | ||
| 1038 | of 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 |
| 1030 | do it also this way: | 1041 | do 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}). | 1049 | Emacs Lisp Reference Manual}) comes in handy for computing replacement |
| 1039 | For example, to add consecutively numbered strings like @samp{ABC00042} | 1050 | strings for @samp{\,}. For example, to add consecutively numbered |
| 1040 | to columns 73 @w{to 80} (unless they are already occupied), you can use | 1051 | strings like @samp{ABC00042} to columns 73 @w{to 80} (unless they are |
| 1052 | already occupied), you can use | ||
| 1041 | 1053 | ||
| 1042 | @example | 1054 | @example |
| 1043 | M-x replace-regexp @key{RET} ^.\@{0,72\@}$ @key{RET} | 1055 | M-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 |
| 1048 | commands is @samp{\?}. In that case you will be allowed to edit the | 1060 | time, use @samp{\?} in the replacement string. Each replacement will |
| 1049 | replacement string at the given position before the replacement gets | 1061 | enter a recursive edit, with point at the position where the @samp{\?} |
| 1050 | performed. Lisp style replacements are performed before @samp{\?} gets | 1062 | was. For example, |
| 1051 | executed. For example, | ||
| 1052 | 1063 | ||
| 1053 | @example | 1064 | @example |
| 1054 | M-x replace-regexp @key{RET} \footnote@{ @key{RET} | 1065 | M-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 |
| 1059 | will add labels starting with @samp{\label@{fn:0@}} to occurences of | 1070 | will 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 |
| 1061 | performing it. If you want labels starting at 1, use @samp{\,(1+ \#)} | 1072 | performing it. To number the labels starting at 1, use @samp{\,(1+ |
| 1062 | instead 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 |