1018 |
|
|
1019 |
You can also use arbitrary Lisp expressions evaluated at replacement |
You can also use arbitrary Lisp expressions evaluated at replacement |
1020 |
time by placing @samp{\,} before them in the replacement string. Inside |
time by placing @samp{\,} before them in the replacement string. Inside |
1021 |
of those expressions, the symbols @samp{\&} and @samp{\@var{d}} refer to |
of those expressions, the symbols @samp{\&} and @samp{\@var{n}} refer to |
1022 |
match and submatch strings like described above (a submatch not matching |
match and submatch strings like described above (though @var{n} may |
1023 |
anything will be @samp{nil}), and @samp{\&#} and @samp{\@var{d}#} to |
exceed 9 here, and you get @code{nil} if nothing matches). @samp{\#&} |
1024 |
those strings converted to numbers. @samp{\#} is short for |
and @samp{\#@var{n}} refer to those strings converted to numbers. |
1025 |
@samp{replace-count}, the number of already completed replacements. |
@samp{\#} is short for @samp{replace-count}, the number of already |
1026 |
This particular shorthand can also be used outside of @samp{\,}. |
completed replacements. This particular shorthand can also be used |
1027 |
|
outside of @samp{\,}. |
1028 |
|
|
1029 |
Repeating our example to exchange @samp{x} and @samp{y}, we can thus |
Repeating our example to exchange @samp{x} and @samp{y}, we can thus |
1030 |
do it also this way: |
do it also this way: |
1034 |
\,(if \1 "y" "x") @key{RET} |
\,(if \1 "y" "x") @key{RET} |
1035 |
@end example |
@end example |
1036 |
|
|
1037 |
|
One function that comes handy in Lisp replacements is @samp{format} |
1038 |
|
(@pxref{Formatting Strings,,,elisp, GNU Emacs Lisp Reference Manual}). |
1039 |
|
For example, to add consecutively numbered strings like @samp{ABC00042} |
1040 |
|
to columns 73 @w{to 80} (unless they are already occupied), you can use |
1041 |
|
|
1042 |
|
@example |
1043 |
|
M-x replace-regexp @key{RET} ^.\@{0,72\@}$ @key{RET} |
1044 |
|
\,(format "%-72sABC%05d" \& \#) @key{RET} |
1045 |
|
@end example |
1046 |
|
|
1047 |
Another feature you can use in the replacement string of Regexp |
Another feature you can use in the replacement string of Regexp |
1048 |
commands is @samp{\?}. In that case you will be allowed to edit the |
commands is @samp{\?}. In that case you will be allowed to edit the |
1049 |
replacement string at the given position before the replacement gets |
replacement string at the given position before the replacement gets |
1050 |
performed. Lisp style replacements have already been done before |
performed. Lisp style replacements are performed before @samp{\?} gets |
1051 |
@samp{\?} is executed. For example, |
executed. For example, |
1052 |
|
|
1053 |
@example |
@example |
1054 |
M-x replace-regexp @key{RET} \footnote@{ @key{RET} |
M-x replace-regexp @key{RET} \footnote@{ @key{RET} |
1061 |
performing it. If you want labels starting at 1, use @samp{\,(1+ \#)} |
performing it. If you want labels starting at 1, use @samp{\,(1+ \#)} |
1062 |
instead of @samp{\#}. |
instead of @samp{\#}. |
1063 |
|
|
|
As another example, to add consecutively numbered strings like |
|
|
@samp{ABC00042} to column 73 to~80 (unless they are already occupied), |
|
|
you can use |
|
|
|
|
|
@example |
|
|
M-x replace-regexp @key{RET} ^.\@{0,72\@}$ @key{RET} |
|
|
\,(format "%-72sABC%05d" \& \#) @key{RET} |
|
|
@end example |
|
|
|
|
1064 |
@node Replacement and Case, Query Replace, Regexp Replace, Replace |
@node Replacement and Case, Query Replace, Regexp Replace, Replace |
1065 |
@subsection Replace Commands and Case |
@subsection Replace Commands and Case |
1066 |
|
|