aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim F. Storm2003-08-17 22:26:42 +0000
committerKim F. Storm2003-08-17 22:26:42 +0000
commita0d0adaf6ab3e109b04d540d55c925e37b970b37 (patch)
tree57840a0ed09b39dd559dc6335a3eb99956d402ee
parentbaf2630dd7d96cba636d428c302a9907a0c99ace (diff)
downloademacs-a0d0adaf6ab3e109b04d540d55c925e37b970b37.tar.gz
emacs-a0d0adaf6ab3e109b04d540d55c925e37b970b37.zip
(Customization): Add xref to Keyboard Macros chapter.
(Keyboard Macros): Move to new kmacro.texi file.
-rw-r--r--man/custom.texi233
1 files changed, 5 insertions, 228 deletions
diff --git a/man/custom.texi b/man/custom.texi
index 4a89f8f086b..9cd3f0baee6 100644
--- a/man/custom.texi
+++ b/man/custom.texi
@@ -19,15 +19,17 @@ between sessions unless you save the customization in a file such as
19customizations for future sessions, this actually works by editing 19customizations for future sessions, this actually works by editing
20@file{.emacs} for you. 20@file{.emacs} for you.
21 21
22 Another means of customization is the keyboard macro, which is a
23sequence of keystrokes to be replayed with a single command.
24@xref{Keyboard Macros}, for full instruction how to record, manage, and
25replay sequences of keys.
26
22@menu 27@menu
23* Minor Modes:: Each minor mode is one feature you can turn on 28* Minor Modes:: Each minor mode is one feature you can turn on
24 independently of any others. 29 independently of any others.
25* Variables:: Many Emacs commands examine Emacs variables 30* Variables:: Many Emacs commands examine Emacs variables
26 to decide what to do; by setting variables, 31 to decide what to do; by setting variables,
27 you can control their functioning. 32 you can control their functioning.
28* Keyboard Macros:: A keyboard macro records a sequence of
29 keystrokes to be replayed with a single
30 command.
31* Key Bindings:: The keymaps say what command each key runs. 33* Key Bindings:: The keymaps say what command each key runs.
32 By changing them, you can "redefine keys". 34 By changing them, you can "redefine keys".
33* Keyboard Translations:: 35* Keyboard Translations::
@@ -1058,231 +1060,6 @@ value are @code{t}, @code{nil}, and anything else, just as for
1058neither @code{t} nor @code{nil}, so normally Emacs does ask for 1060neither @code{t} nor @code{nil}, so normally Emacs does ask for
1059confirmation about file settings for these variables. 1061confirmation about file settings for these variables.
1060 1062
1061@node Keyboard Macros
1062@section Keyboard Macros
1063
1064@cindex defining keyboard macros
1065@cindex keyboard macro
1066 A @dfn{keyboard macro} is a command defined by the user to stand for
1067another sequence of keys. For example, if you discover that you are
1068about to type @kbd{C-n C-d} forty times, you can speed your work by
1069defining a keyboard macro to do @kbd{C-n C-d} and calling it with a
1070repeat count of forty.
1071
1072@table @kbd
1073@item C-x (
1074Start defining a keyboard macro (@code{start-kbd-macro}).
1075@item C-x )
1076End the definition of a keyboard macro (@code{end-kbd-macro}).
1077@item C-x e
1078Execute the most recent keyboard macro (@code{call-last-kbd-macro}).
1079@item C-u C-x (
1080Re-execute last keyboard macro, then add more keys to its definition.
1081@item C-x q
1082When this point is reached during macro execution, ask for confirmation
1083(@code{kbd-macro-query}).
1084@item M-x name-last-kbd-macro
1085Give a command name (for the duration of the session) to the most
1086recently defined keyboard macro.
1087@item M-x insert-kbd-macro
1088Insert in the buffer a keyboard macro's definition, as Lisp code.
1089@item C-x C-k
1090Edit a previously defined keyboard macro (@code{edit-kbd-macro}).
1091@item M-x apply-macro-to-region-lines
1092Run the last keyboard macro on each complete line in the region.
1093@end table
1094
1095 Keyboard macros differ from ordinary Emacs commands in that they are
1096written in the Emacs command language rather than in Lisp. This makes it
1097easier for the novice to write them, and makes them more convenient as
1098temporary hacks. However, the Emacs command language is not powerful
1099enough as a programming language to be useful for writing anything
1100intelligent or general. For such things, Lisp must be used.
1101
1102 You define a keyboard macro while executing the commands which are the
1103definition. Put differently, as you define a keyboard macro, the
1104definition is being executed for the first time. This way, you can see
1105what the effects of your commands are, so that you don't have to figure
1106them out in your head. When you are finished, the keyboard macro is
1107defined and also has been, in effect, executed once. You can then do the
1108whole thing over again by invoking the macro.
1109
1110@menu
1111* Basic Kbd Macro:: Defining and running keyboard macros.
1112* Save Kbd Macro:: Giving keyboard macros names; saving them in files.
1113* Kbd Macro Query:: Making keyboard macros do different things each time.
1114@end menu
1115
1116@node Basic Kbd Macro
1117@subsection Basic Use
1118
1119@kindex C-x (
1120@kindex C-x )
1121@kindex C-x e
1122@findex start-kbd-macro
1123@findex end-kbd-macro
1124@findex call-last-kbd-macro
1125 To start defining a keyboard macro, type the @kbd{C-x (} command
1126(@code{start-kbd-macro}). From then on, your keys continue to be
1127executed, but also become part of the definition of the macro. @samp{Def}
1128appears in the mode line to remind you of what is going on. When you are
1129finished, the @kbd{C-x )} command (@code{end-kbd-macro}) terminates the
1130definition (without becoming part of it!). For example,
1131
1132@example
1133C-x ( M-f foo C-x )
1134@end example
1135
1136@noindent
1137defines a macro to move forward a word and then insert @samp{foo}.
1138
1139 The macro thus defined can be invoked again with the @kbd{C-x e}
1140command (@code{call-last-kbd-macro}), which may be given a repeat count
1141as a numeric argument to execute the macro many times. @kbd{C-x )} can
1142also be given a repeat count as an argument, in which case it repeats
1143the macro that many times right after defining it, but defining the
1144macro counts as the first repetition (since it is executed as you define
1145it). Therefore, giving @kbd{C-x )} an argument of 4 executes the macro
1146immediately 3 additional times. An argument of zero to @kbd{C-x e} or
1147@kbd{C-x )} means repeat the macro indefinitely (until it gets an error
1148or you type @kbd{C-g} or, on MS-DOS, @kbd{C-@key{BREAK}}).
1149
1150 If you wish to repeat an operation at regularly spaced places in the
1151text, define a macro and include as part of the macro the commands to move
1152to the next place you want to use it. For example, if you want to change
1153each line, you should position point at the start of a line, and define a
1154macro to change that line and leave point at the start of the next line.
1155Then repeating the macro will operate on successive lines.
1156
1157 When a command reads an argument with the minibuffer, your
1158minibuffer input becomes part of the macro along with the command. So
1159when you replay the macro, the command gets the same argument as
1160when you entered the macro. For example,
1161
1162@example
1163C-x ( C-a C-@key{SPC} C-n M-w C-x b f o o @key{RET} C-y C-x b @key{RET} C-x )
1164@end example
1165
1166@noindent
1167defines a macro that copies the current line into the buffer
1168@samp{foo}, then returns to the original buffer.
1169
1170 You can use function keys in a keyboard macro, just like keyboard
1171keys. You can even use mouse events, but be careful about that: when
1172the macro replays the mouse event, it uses the original mouse position
1173of that event, the position that the mouse had while you were defining
1174the macro. The effect of this may be hard to predict. (Using the
1175current mouse position would be even less predictable.)
1176
1177 One thing that doesn't always work well in a keyboard macro is the
1178command @kbd{C-M-c} (@code{exit-recursive-edit}). When this command
1179exits a recursive edit that started within the macro, it works as you'd
1180expect. But if it exits a recursive edit that started before you
1181invoked the keyboard macro, it also necessarily exits the keyboard macro
1182as part of the process.
1183
1184 After you have terminated the definition of a keyboard macro, you can add
1185to the end of its definition by typing @kbd{C-u C-x (}. This is equivalent
1186to plain @kbd{C-x (} followed by retyping the whole definition so far. As
1187a consequence it re-executes the macro as previously defined.
1188
1189@findex edit-kbd-macro
1190@kindex C-x C-k
1191 You can edit a keyboard macro already defined by typing @kbd{C-x C-k}
1192(@code{edit-kbd-macro}). Follow that with the keyboard input that you
1193would use to invoke the macro---@kbd{C-x e} or @kbd{M-x @var{name}} or
1194some other key sequence. This formats the macro definition in a buffer
1195and enters a specialized major mode for editing it. Type @kbd{C-h m}
1196once in that buffer to display details of how to edit the macro. When
1197you are finished editing, type @kbd{C-c C-c}.
1198
1199@findex apply-macro-to-region-lines
1200 The command @kbd{M-x apply-macro-to-region-lines} repeats the last
1201defined keyboard macro on each complete line within the current region.
1202It does this line by line, by moving point to the beginning of the line
1203and then executing the macro.
1204
1205@node Save Kbd Macro
1206@subsection Naming and Saving Keyboard Macros
1207
1208@cindex saving keyboard macros
1209@findex name-last-kbd-macro
1210 If you wish to save a keyboard macro for longer than until you define the
1211next one, you must give it a name using @kbd{M-x name-last-kbd-macro}.
1212This reads a name as an argument using the minibuffer and defines that name
1213to execute the macro. The macro name is a Lisp symbol, and defining it in
1214this way makes it a valid command name for calling with @kbd{M-x} or for
1215binding a key to with @code{global-set-key} (@pxref{Keymaps}). If you
1216specify a name that has a prior definition other than another keyboard
1217macro, an error message is shown and nothing is changed.
1218
1219@findex insert-kbd-macro
1220 Once a macro has a command name, you can save its definition in a file.
1221Then it can be used in another editing session. First, visit the file
1222you want to save the definition in. Then use this command:
1223
1224@example
1225M-x insert-kbd-macro @key{RET} @var{macroname} @key{RET}
1226@end example
1227
1228@noindent
1229This inserts some Lisp code that, when executed later, will define the
1230same macro with the same definition it has now. (You need not
1231understand Lisp code to do this, because @code{insert-kbd-macro} writes
1232the Lisp code for you.) Then save the file. You can load the file
1233later with @code{load-file} (@pxref{Lisp Libraries}). If the file you
1234save in is your init file @file{~/.emacs} (@pxref{Init File}) then the
1235macro will be defined each time you run Emacs.
1236
1237 If you give @code{insert-kbd-macro} a numeric argument, it makes
1238additional Lisp code to record the keys (if any) that you have bound to the
1239keyboard macro, so that the macro will be reassigned the same keys when you
1240load the file.
1241
1242@node Kbd Macro Query
1243@subsection Executing Macros with Variations
1244
1245@kindex C-x q
1246@findex kbd-macro-query
1247 Using @kbd{C-x q} (@code{kbd-macro-query}), you can get an effect
1248similar to that of @code{query-replace}, where the macro asks you each
1249time around whether to make a change. While defining the macro,
1250type @kbd{C-x q} at the point where you want the query to occur. During
1251macro definition, the @kbd{C-x q} does nothing, but when you run the
1252macro later, @kbd{C-x q} asks you interactively whether to continue.
1253
1254 The valid responses when @kbd{C-x q} asks are @key{SPC} (or @kbd{y}),
1255@key{DEL} (or @kbd{n}), @key{RET} (or @kbd{q}), @kbd{C-l} and @kbd{C-r}.
1256The answers are the same as in @code{query-replace}, though not all of
1257the @code{query-replace} options are meaningful.
1258
1259 These responses include @key{SPC} to continue, and @key{DEL} to skip
1260the remainder of this repetition of the macro and start right away with
1261the next repetition. @key{RET} means to skip the remainder of this
1262repetition and cancel further repetitions. @kbd{C-l} redraws the screen
1263and asks you again for a character to say what to do.
1264
1265 @kbd{C-r} enters a recursive editing level, in which you can perform
1266editing which is not part of the macro. When you exit the recursive
1267edit using @kbd{C-M-c}, you are asked again how to continue with the
1268keyboard macro. If you type a @key{SPC} at this time, the rest of the
1269macro definition is executed. It is up to you to leave point and the
1270text in a state such that the rest of the macro will do what you
1271want.@refill
1272
1273 @kbd{C-u C-x q}, which is @kbd{C-x q} with a numeric argument,
1274performs a completely different function. It enters a recursive edit
1275reading input from the keyboard, both when you type it during the
1276definition of the macro, and when it is executed from the macro. During
1277definition, the editing you do inside the recursive edit does not become
1278part of the macro. During macro execution, the recursive edit gives you
1279a chance to do some particularized editing on each repetition.
1280@xref{Recursive Edit}.
1281
1282 Another way to vary the behavior of a keyboard macro is to use a
1283register as a counter, incrementing it on each repetition of the macro.
1284@xref{RegNumbers}.
1285
1286@node Key Bindings 1063@node Key Bindings
1287@section Customizing Key Bindings 1064@section Customizing Key Bindings
1288@cindex key bindings 1065@cindex key bindings