aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1994-05-01 09:17:11 +0000
committerRichard M. Stallman1994-05-01 09:17:11 +0000
commit87b2d5ff029e93b27ab4887608788fd3d2b8c7e7 (patch)
tree91e94898df59821a1134e69a0fd9d7212d4b52fe
parent4fe1de1272b657e687889dbf1f47d954b1d14278 (diff)
downloademacs-87b2d5ff029e93b27ab4887608788fd3d2b8c7e7.tar.gz
emacs-87b2d5ff029e93b27ab4887608788fd3d2b8c7e7.zip
*** empty log message ***
-rw-r--r--lispref/edebug.texi330
-rw-r--r--lispref/keymaps.texi823
2 files changed, 524 insertions, 629 deletions
diff --git a/lispref/edebug.texi b/lispref/edebug.texi
index c36ac42768e..991ea0a5391 100644
--- a/lispref/edebug.texi
+++ b/lispref/edebug.texi
@@ -7,7 +7,7 @@
7 7
8@c , Bugs and Todo List, Top, Top 8@c , Bugs and Todo List, Top, Top
9 9
10@node Edebug, Bugs and Todo List, Top, Top 10@node Edebug,, Compilation Errors, Debugging
11@section Edebug 11@section Edebug
12@cindex Edebug mode 12@cindex Edebug mode
13 13
@@ -152,12 +152,7 @@ display a list of all Edebug commands.
152 152
153 In order to use Edebug to debug Lisp code, you must first 153 In order to use Edebug to debug Lisp code, you must first
154@dfn{instrument} the code. Instrumenting code inserts additional code 154@dfn{instrument} the code. Instrumenting code inserts additional code
155into it, code which invokes Edebug at the proper places. 155into it, to invoke Edebug at the proper places.
156
157 Once a function is instrumented, any call to the function activates
158Edebug. This may or may not stop execution, depending on the Edebug
159execution mode in use. Some Edebug modes only update the display to
160indicate the progress of the evaluation without stopping execution.
161 156
162@kindex C-M-x 157@kindex C-M-x
163@findex eval-defun (Edebug) 158@findex eval-defun (Edebug)
@@ -217,7 +212,7 @@ must tell it; @xref{Instrumenting Macro Calls}, for details.
217@findex eval-expression (Edebug) 212@findex eval-expression (Edebug)
218 To remove instrumentation from a definition, simply reevaluate its 213 To remove instrumentation from a definition, simply reevaluate its
219definition in a way that does not instrument. There are two ways of 214definition in a way that does not instrument. There are two ways of
220evaluating forms without instrumenting them: from a file with 215evaluating forms that never instrument them: from a file with
221@code{load}, and from the minibuffer with @code{eval-expression} 216@code{load}, and from the minibuffer with @code{eval-expression}
222(@kbd{M-ESC}). 217(@kbd{M-ESC}).
223 218
@@ -233,7 +228,7 @@ inside of Edebug.
233@cindex Edebug execution modes 228@cindex Edebug execution modes
234Edebug supports several execution modes for running the program you are 229Edebug supports several execution modes for running the program you are
235debugging. We call these alternatives @dfn{Edebug execution modes}; do 230debugging. We call these alternatives @dfn{Edebug execution modes}; do
236not confuse them with major or minor modes. The current Edebug mode 231not confuse them with major or minor modes. The current Edebug execution mode
237determines how far Edebug continues execution before stopping---whether 232determines how far Edebug continues execution before stopping---whether
238it stops at each stop point, or continues to the next breakpoint, for 233it stops at each stop point, or continues to the next breakpoint, for
239example---and how much Edebug displays the progress of the evaluation 234example---and how much Edebug displays the progress of the evaluation
@@ -281,13 +276,13 @@ can still stop the program by typing @kbd{S}, or any editing command.
281@end table 276@end table
282 277
283In general, the execution modes earlier in the above list run the 278In general, the execution modes earlier in the above list run the
284program more slowly or stop sooner. 279program more slowly or stop sooner than the modes later in the list.
285 280
286While executing or tracing, you can interrupt the execution by typing 281While executing or tracing, you can interrupt the execution by typing
287any Edebug command. Edebug stops the program at the next stop point and 282any Edebug command. Edebug stops the program at the next stop point and
288then executes the command that you typed. For example, typing @kbd{t} 283then executes the command you typed. For example, typing @kbd{t} during
289during execution switches to trace mode at the next stop point. You can 284execution switches to trace mode at the next stop point. You can use
290use @kbd{S} to stop execution without doing anything else. 285@kbd{S} to stop execution without doing anything else.
291 286
292If your function happens to read input, a character you type intending 287If your function happens to read input, a character you type intending
293to interrupt execution may be read by the function instead. You can 288to interrupt execution may be read by the function instead. You can
@@ -362,17 +357,17 @@ move point there, and then type @kbd{f}.
362 357
363The @kbd{o} command continues ``out of'' an expression. It places a 358The @kbd{o} command continues ``out of'' an expression. It places a
364temporary breakpoint at the end of the sexp containing point. If the 359temporary breakpoint at the end of the sexp containing point. If the
365containing sexp is a function definition itself, it continues until just 360containing sexp is a function definition itself, @kbd{o} continues until
366before the last sexp in the definition. If that is where you are now, 361just before the last sexp in the definition. If that is where you are
367it returns from the function and then stops. In other words, this 362now, it returns from the function and then stops. In other words, this
368command does not exit the currently executing function unless you are 363command does not exit the currently executing function unless you are
369positioned after the last sexp. 364positioned after the last sexp.
370 365
371The @kbd{i} command steps into the function or macro called by the list 366The @kbd{i} command steps into the function or macro called by the list
372form after point. Note that the form need not be the one about to be 367form after point, and stops at its first stop point. Note that the form
373evaluated. But if the form is a function call about to be evaluated, 368need not be the one about to be evaluated. But if the form is a
374remember to use this command before any of the arguments are evaluated, 369function call about to be evaluated, remember to use this command before
375since otherwise it will be too late. 370any of the arguments are evaluated, since otherwise it will be too late.
376 371
377The @kbd{i} command instruments the function or macro it's supposed to 372The @kbd{i} command instruments the function or macro it's supposed to
378step into, if it isn't instrumented already. This is convenient, but keep 373step into, if it isn't instrumented already. This is convenient, but keep
@@ -418,7 +413,7 @@ The backtrace buffer is killed automatically when you continue
418execution. 413execution.
419@end table 414@end table
420 415
421>From the Edebug recursive edit, you may invoke commands that activate 416From the Edebug recursive edit, you may invoke commands that activate
422Edebug again recursively. Any time Edebug is active, you can quit to 417Edebug again recursively. Any time Edebug is active, you can quit to
423the top level with @kbd{q} or abort one recursive edit level with 418the top level with @kbd{q} or abort one recursive edit level with
424@kbd{C-]}. You can display a backtrace of all the 419@kbd{C-]}. You can display a backtrace of all the
@@ -485,9 +480,10 @@ Edebug always stops or pauses at a breakpoint except when the Edebug
485mode is Go-nonstop. In that mode, it ignores breakpoints entirely. 480mode is Go-nonstop. In that mode, it ignores breakpoints entirely.
486 481
487To find out where your breakpoints are, use the @kbd{B} command, which 482To find out where your breakpoints are, use the @kbd{B} command, which
488moves point to the next breakpoint in the definition following point, or 483moves point to the next breakpoint following point, within the same
489to the first breakpoint if there are no following breakpoints. This 484function, or to the first breakpoint if there are no following
490command does not continue execution---it just moves point in the buffer. 485breakpoints. This command does not continue execution---it just moves
486point in the buffer.
491 487
492@menu 488@menu
493* Global Break Condition:: Breaking on an event. 489* Global Break Condition:: Breaking on an event.
@@ -561,31 +557,24 @@ occurred. For an unbound variable error, the last known stop point
561might be quite distant from the offending variable reference. In that 557might be quite distant from the offending variable reference. In that
562case you might want to display a full backtrace (@pxref{Edebug Misc}). 558case you might want to display a full backtrace (@pxref{Edebug Misc}).
563 559
560@c Edebug should be changed for the following: -- dan
564If you change @code{debug-on-error} or @code{debug-on-quit} while 561If you change @code{debug-on-error} or @code{debug-on-quit} while
565Edebug is active, these changes will be forgotten when Edebug becomes 562Edebug is active, these changes will be forgotten when Edebug becomes
566inactive. Furthermore, during Edebug's recursive edit, these variables 563inactive. Furthermore, during Edebug's recursive edit, these variables
567are bound to the values they had outside of Edebug. 564are bound to the values they had outside of Edebug.
568 565
569@ignore @c I don't want to document something that works only partly -- rms.
570Edebug can also trap signals even if they are handled. If
571@code{debug-on-error} is a list of signal names, Edebug will stop when
572any of these errors are signaled. Edebug shows you the last known stop
573point just as for unhandled errors. After you continue execution, the
574error is signaled again (but without being caught by Edebug). Edebug
575can only trap errors that are handled if they are signaled in Lisp code
576(not subroutines) since it does so by temporarily replacing the
577@code{signal} function.
578@end ignore
579
580@node Edebug Views 566@node Edebug Views
581@subsection Edebug Views 567@subsection Edebug Views
582 568
583These Edebug commands let you view aspects of the buffer and window 569These Edebug commands let you view aspects of the buffer and window
584status that obtained before entry to Edebug. 570status that obtained before entry to Edebug. The outside window
571configuration is the collection of windows and contents that were in
572effect outside of Edebug.
585 573
586@table @kbd 574@table @kbd
587@item v 575@item v
588View the outside window configuration (@code{edebug-view-outside}). 576Temporarily view the outside window configuration
577(@code{edebug-view-outside}).
589 578
590@item p 579@item p
591Temporarily display the outside current buffer with point at its outside 580Temporarily display the outside current buffer with point at its outside
@@ -599,18 +588,13 @@ displaying the same buffer, that window will be used instead to display
599the current definition in the future. 588the current definition in the future.
600 589
601@item W 590@item W
602Forget the saved outside window configuration---so that the current 591@c Its function is not simply to forget the saved configuration -- dan
603window configuration will remain unchanged when you next exit Edebug (by 592Toggle whether Edebug saves and restores the outside window
604continuing the program). Also toggle the @code{edebug-save-windows} 593configuration (@code{edebug-toggle-save-windows}).
605variable. 594
606@ignore @c This text is implementation-oriented and doesn't emphasize 595With a prefix argument, @code{W} only toggles saving and restoring of
607 what users really want to know. 596the selected window. To specify a window that is not displaying the
608Toggle the @code{edebug-save-windows} variable which indicates whether 597source code buffer, you must use @kbd{C-x X W} from the global keymap.
609the outside window configuration is saved and restored
610(@code{edebug-toggle-save-windows}). Also, each time it is toggled on,
611make the outside window configuration the same as the current window
612configuration.
613@end ignore
614@end table 598@end table
615 599
616You can view the outside window configuration with @kbd{v} or just 600You can view the outside window configuration with @kbd{v} or just
@@ -618,18 +602,13 @@ bounce to the point in the current buffer with @kbd{p}, even if
618it is not normally displayed. After moving point, you may wish to jump 602it is not normally displayed. After moving point, you may wish to jump
619back to the stop point with @kbd{w} from a source code buffer. 603back to the stop point with @kbd{w} from a source code buffer.
620 604
621@ignore I don't understand this -- rms 605Each time you use @kbd{W} to turn saving @emph{off}, Edebug forgets the
622If you type @kbd{W} twice, Edebug continues saving and restoring an 606saved outside window configuration---so that even if you turn saving
623outside window configuration, but updates it to match the current 607back @emph{on}, the current window configuration remains unchanged when
624configuration. You can use this to add another buffer to be displayed 608you next exit Edebug (by continuing the program). However, the
625whenever Edebug is active. However, the automatic redisplay of 609automatic redisplay of @samp{*edebug*} and @samp{*edebug-trace*} may
626@samp{*edebug*} and @samp{*edebug-trace*} may conflict with the buffers 610conflict with the buffers you wish to see unless you have enough windows
627you wish to see unless you have enough windows open. 611open.
628
629With a prefix argument, @code{W} only toggles saving and restoring of
630the selected window. To specify a window that is not displaying the
631source code buffer, you must use @kbd{C-x X W} from the global keymap.
632@end ignore
633 612
634@node Edebug Eval 613@node Edebug Eval
635@subsection Evaluation 614@subsection Evaluation
@@ -690,7 +669,7 @@ Evaluate the expression before point, in the context outside of Edebug
690(@code{edebug-eval-last-sexp}). 669(@code{edebug-eval-last-sexp}).
691 670
692@item C-c C-u 671@item C-c C-u
693Build a new evaluation list from contents of the buffer 672Build a new evaluation list from the contents of the buffer
694(@code{edebug-update-eval-list}). 673(@code{edebug-update-eval-list}).
695 674
696@item C-c C-d 675@item C-c C-d
@@ -717,17 +696,18 @@ more Lisp expressions. Groups are separated by comment lines.
717 696
718The command @kbd{C-c C-u} (@code{edebug-update-eval-list}) rebuilds the 697The command @kbd{C-c C-u} (@code{edebug-update-eval-list}) rebuilds the
719evaluation list, scanning the buffer and using the first expression of 698evaluation list, scanning the buffer and using the first expression of
720each group. 699each group. (The idea is that the second expression of the group is the
700value previously computed and displayed.)
721 701
722Be careful not to add expressions that execute instrumented code since 702Be careful not to add expressions that execute instrumented code since
723that would cause an infinite loop. 703that would cause an infinite loop.
724@c There ought to be a way to fix this. 704@c There ought to be a way to fix this.
725 705
726Redisplaying the evaluation list works by inserting each expression in 706Each entry to Edebug redisplays the evaluation list by inserting each
727the buffer, followed by its current value. It also inserts comment 707expression in the buffer, followed by its current value. It also
728lines so that each expression becomes its own group. Thus, if you type 708inserts comment lines so that each expression becomes its own group.
729@kbd{C-c C-u} again without changing the buffer text, the evaluation 709Thus, if you type @kbd{C-c C-u} again without changing the buffer text,
730list is effectively unchanged. 710the evaluation list is effectively unchanged.
731 711
732If an error occurs during an evaluation from the evaluation list, the 712If an error occurs during an evaluation from the evaluation list, the
733error message is displayed in a string as if it were the result. 713error message is displayed in a string as if it were the result.
@@ -817,7 +797,7 @@ for details.
817@subsection Trace Buffer 797@subsection Trace Buffer
818@cindex trace buffer 798@cindex trace buffer
819 799
820 Edebug can record an execution trace in a buffer named 800 Edebug can record an execution trace, storing it in a buffer named
821@samp{*edebug-trace*}. This is a log of function calls and returns, 801@samp{*edebug-trace*}. This is a log of function calls and returns,
822showing the function names and their arguments and values. To enable 802showing the function names and their arguments and values. To enable
823trace recording, set @code{edebug-trace} to a non-@code{nil} value. 803trace recording, set @code{edebug-trace} to a non-@code{nil} value.
@@ -851,7 +831,7 @@ to put in the trace buffer. All the arguments are evaluated.
851@defun edebug-trace format-string &rest format-args 831@defun edebug-trace format-string &rest format-args
852This function inserts text in the trace buffer. It computes the text 832This function inserts text in the trace buffer. It computes the text
853with @code{(apply 'format @var{format-string} @var{format-args})}. 833with @code{(apply 'format @var{format-string} @var{format-args})}.
854It also inserts a newline to separate entries. 834It also appends a newline to separate entries.
855@end defun 835@end defun
856 836
857 @code{edebug-tracing} and @code{edebug-trace} insert lines in the trace 837 @code{edebug-tracing} and @code{edebug-trace} insert lines in the trace
@@ -860,11 +840,6 @@ buffer even if Edebug is not active.
860 Adding text to the trace buffer also scrolls its window to show the 840 Adding text to the trace buffer also scrolls its window to show the
861last lines inserted. 841last lines inserted.
862 842
863@ignore @c too vague
864There may be some display problems if you use
865tracing along with the evaluation list.
866@end ignore
867
868@node Coverage Testing 843@node Coverage Testing
869@subsection Coverage Testing 844@subsection Coverage Testing
870 845
@@ -883,9 +858,9 @@ displays both the frequency data and the coverage data (if recorded).
883This command displays the frequency count data for each line of the 858This command displays the frequency count data for each line of the
884current definition. 859current definition.
885 860
886The frequency counts appear comment lines after each line of code, and 861The frequency counts appear as comment lines after each line of code, and
887you can undo all insertions with one @code{undo} command. The counts 862you can undo all insertions with one @code{undo} command. The counts
888are appear under the @kbd{(} before an expression or the @kbd{)} after 863appear under the @kbd{(} before an expression or the @kbd{)} after
889an expression, or on the last character of a symbol. Values do not appear if 864an expression, or on the last character of a symbol. Values do not appear if
890they are equal to the previous count on the same line. 865they are equal to the previous count on the same line.
891 866
@@ -913,10 +888,10 @@ the breakpoint is reached, the frequency data looks like this:
913;# 0 888;# 0
914@end example 889@end example
915 890
916The comment lines show that @code{fac} has been called 6 times. The 891The comment lines show that @code{fac} was called 6 times. The
917first @code{if} statement has returned 5 times with the same result each 892first @code{if} statement returned 5 times with the same result each
918time; the same is true of the condition on the second @code{if}. 893time; the same is true of the condition on the second @code{if}.
919The recursive call of @code{fac} has not returned at all. 894The recursive call of @code{fac} did not return at all.
920 895
921 896
922@node The Outside Context 897@node The Outside Context
@@ -929,10 +904,6 @@ buffer, by temporarily restoring the outside context. This section
929explains precisely what context Edebug restores, and how Edebug fails to 904explains precisely what context Edebug restores, and how Edebug fails to
930be completely transparent. 905be completely transparent.
931 906
932@c This can be fixed and should be
933The same mechanism that avoids masking certain variable's outside values
934also currently makes it impossible to set these variables within Edebug.
935
936@menu 907@menu
937* Checking Whether to Stop:: When Edebug decides what to do. 908* Checking Whether to Stop:: When Edebug decides what to do.
938* Edebug Display Update:: When Edebug updates the display. 909* Edebug Display Update:: When Edebug updates the display.
@@ -942,8 +913,9 @@ also currently makes it impossible to set these variables within Edebug.
942@node Checking Whether to Stop 913@node Checking Whether to Stop
943@subsubsection Checking Whether to Stop 914@subsubsection Checking Whether to Stop
944 915
945Whenever Edebug is entered just to think about whether to take some 916Whenever Edebug is entered, it needs to save and restore certain data
946action, it needs to save and restore certain data. 917before even deciding whether to make trace information or stop the
918program.
947 919
948@itemize @bullet 920@itemize @bullet
949@item 921@item
@@ -962,11 +934,12 @@ Edebug is active, @code{executing-macro} is bound to
962@node Edebug Display Update 934@node Edebug Display Update
963@subsubsection Edebug Display Update 935@subsubsection Edebug Display Update
964 936
937@c This paragraph is not filled, because LaLiberte's conversion script
938@c needs an xref to be on just one line.
965When Edebug needs to display something (e.g., in trace mode), it saves 939When Edebug needs to display something (e.g., in trace mode), it saves
966the current window configuration from ``outside'' Edebug (@pxref{Window 940the current window configuration from ``outside'' Edebug
967Configurations,,, elisp, GNU Emacs Lisp Reference Manual}). When 941(@pxref{Window Configurations}). When you exit Edebug (by continuing
968you exit Edebug (by continuing the program), it restores the previous 942the program), it restores the previous window configuration.
969window configuration.
970 943
971Emacs redisplays only when it pauses. Usually, when you continue 944Emacs redisplays only when it pauses. Usually, when you continue
972execution, the program comes back into Edebug at a breakpoint or after 945execution, the program comes back into Edebug at a breakpoint or after
@@ -1000,7 +973,6 @@ The window start and horizontal scrolling of the source code buffer are
1000not restored, however, so that the display remains coherent within Edebug. 973not restored, however, so that the display remains coherent within Edebug.
1001 974
1002@item 975@item
1003@vindex edebug-save-displayed-buffer-points
1004The value of point in each displayed buffer is saved and restored if 976The value of point in each displayed buffer is saved and restored if
1005@code{edebug-save-displayed-buffer-points} is non-@code{nil}. 977@code{edebug-save-displayed-buffer-points} is non-@code{nil}.
1006 978
@@ -1035,6 +1007,11 @@ The key sequence returned by @code{this-command-keys} is changed by
1035executing commands within Edebug and there is no way to reset 1007executing commands within Edebug and there is no way to reset
1036the key sequence from Lisp. 1008the key sequence from Lisp.
1037 1009
1010Edebug cannot save and restore the value of
1011@code{unread-command-events}. Entering Edebug while this variable has a
1012nontrivial value can interfere with execution of the program you are
1013debugging.
1014
1038@item 1015@item
1039Complex commands executed while in Edebug are added to the variable 1016Complex commands executed while in Edebug are added to the variable
1040@code{command-history}. In rare cases this can alter execution. 1017@code{command-history}. In rare cases this can alter execution.
@@ -1064,7 +1041,7 @@ way to tell which subexpressions of the macro call are forms to be
1064evaluated. (Evaluation may occur explicitly in the macro body, or when 1041evaluated. (Evaluation may occur explicitly in the macro body, or when
1065the resulting expansion is evaluated, or any time later.) You must 1042the resulting expansion is evaluated, or any time later.) You must
1066explain the format of calls to each macro to enable Edebug to handle it. 1043explain the format of calls to each macro to enable Edebug to handle it.
1067To do this, use @code{def-edebug-form-spec} to define the format of 1044To do this, use @code{def-edebug-spec} to define the format of
1068calls to a given macro. 1045calls to a given macro.
1069 1046
1070@deffn Macro def-edebug-spec macro specification 1047@deffn Macro def-edebug-spec macro specification
@@ -1114,7 +1091,6 @@ described in the following sections.
1114@menu 1091@menu
1115* Specification List:: How to specify complex patterns of evaluation. 1092* Specification List:: How to specify complex patterns of evaluation.
1116* Backtracking:: What Edebug does when matching fails. 1093* Backtracking:: What Edebug does when matching fails.
1117@c * Debugging Backquote:: Debugging Backquote
1118* Specification Examples:: To help understand specifications. 1094* Specification Examples:: To help understand specifications.
1119@end menu 1095@end menu
1120 1096
@@ -1150,7 +1126,9 @@ their meanings:
1150 1126
1151@table @code 1127@table @code
1152@item sexp 1128@item sexp
1153A single unevaluated Lisp object object. 1129A single Lisp object, not unevaluated.
1130@c "unevaluated expression" is not meaningful, because
1131@c an expression is a Lisp object intended for evaluation.
1154 1132
1155@item form 1133@item form
1156A single evaluated expression, which is instrumented. 1134A single evaluated expression, which is instrumented.
@@ -1246,16 +1224,8 @@ Otherwise, the symbol should be a predicate. The predicate is called
1246with the argument and the specification fails if the predicate returns 1224with the argument and the specification fails if the predicate returns
1247@code{nil}. In either case, that argument is not instrumented. 1225@code{nil}. In either case, that argument is not instrumented.
1248 1226
1249@findex keywordp
1250@findex lambda-list-keywordp
1251Some suitable predicates include @code{symbolp}, @code{integerp}, 1227Some suitable predicates include @code{symbolp}, @code{integerp},
1252@code{stringp}, @code{vectorp}, and @code{atom}. 1228@code{stringp}, @code{vectorp}, and @code{atom}.
1253@ignore
1254, @code{keywordp}, and
1255@code{lambda-list-keywordp}. The last two, defined in @file{edebug.el},
1256test whether the argument is a symbol starting with @samp{@code{:}} and
1257@samp{@code{&}} respectively.
1258@end ignore
1259 1229
1260@item [@var{elements}@dots{}] 1230@item [@var{elements}@dots{}]
1261@cindex [@dots{}] (Edebug) 1231@cindex [@dots{}] (Edebug)
@@ -1267,12 +1237,6 @@ The argument should be a symbol named @var{string}. This specification
1267is equivalent to the quoted symbol, @code{'@var{symbol}}, where the name 1237is equivalent to the quoted symbol, @code{'@var{symbol}}, where the name
1268of @var{symbol} is the @var{string}, but the string form is preferred. 1238of @var{symbol} is the @var{string}, but the string form is preferred.
1269 1239
1270@ignore
1271@item '@var{symbol} @r{or} (quote @var{symbol})
1272The argument should be the symbol @var{symbol}. But use a string
1273specification instead.
1274@end ignore
1275
1276@item (vector @var{elements}@dots{}) 1240@item (vector @var{elements}@dots{})
1277The argument should be a vector whose elements must match the 1241The argument should be a vector whose elements must match the
1278@var{elements} in the specification. See the backquote example below. 1242@var{elements} in the specification. See the backquote example below.
@@ -1291,9 +1255,9 @@ This is useful in recursive specifications such as in the backquote
1291example below. Also see the description of a @code{nil} specification 1255example below. Also see the description of a @code{nil} specification
1292above for terminating such recursion. 1256above for terminating such recursion.
1293 1257
1294Note that a sublist specification of the form @code{(specs . nil)} 1258Note that a sublist specification written as @code{(specs . nil)}
1295means the same as @code{(specs)}, and @code{(specs . 1259is equivalent to @code{(specs)}, and @code{(specs .
1296(sublist-elements@dots{}))} means the same as @code{(specs 1260(sublist-elements@dots{}))} is equivalent to @code{(specs
1297sublist-elements@dots{})}. 1261sublist-elements@dots{})}.
1298@end table 1262@end table
1299 1263
@@ -1319,12 +1283,11 @@ than once.
1319@item arg 1283@item arg
1320The argument, a symbol, is the name of an argument of the defining form. 1284The argument, a symbol, is the name of an argument of the defining form.
1321However, lambda list keywords (symbols starting with @samp{@code{&}}) 1285However, lambda list keywords (symbols starting with @samp{@code{&}})
1322are not allowed. See @code{lambda-list} and the example below. 1286are not allowed.
1323 1287
1324@item lambda-list 1288@item lambda-list
1325@cindex lambda-list (Edebug) 1289@cindex lambda-list (Edebug)
1326This matches a lambda list---the argument list of a lambda expression. 1290This matches a lambda list---the argument list of a lambda expression.
1327The argument should be a list of symbols.
1328 1291
1329@item def-body 1292@item def-body
1330The argument is the body of code in a definition. This is like 1293The argument is the body of code in a definition. This is like
@@ -1380,94 +1343,6 @@ Third, backtracking may be explicitly disabled by using the
1380@code{gate} specification. This is useful when you know that 1343@code{gate} specification. This is useful when you know that
1381no higher alternatives may apply. 1344no higher alternatives may apply.
1382 1345
1383@ignore
1384@node Debugging Backquote
1385@subsubsection Debugging Backquote
1386
1387@findex ` (Edebug)
1388@cindex backquote (Edebug)
1389Backquote (@kbd{`}) is a macro that results in an expression that may or
1390may not be evaluated. It is often used to simplify the definition of a
1391macro to return an expression to be evaluated, but Edebug cannot know
1392whether the resyult of backquote will be used in any other way.
1393
1394The forms inside unquotes (@code{,} and @code{,@@}) are evaluated, and
1395Edebug instruments them.
1396
1397Edebug supports nested backquotes, but there is a limit on the support
1398of quotes inside of backquotes. Forms quoted with @code{'} are not
1399normally evaluated, but if the quoted form appears immediately within
1400@code{,} and @code{,@@} forms, Edebug treats this as a backquoted form
1401at the next higher level (even if there is not a next higher level; this
1402is difficult to fix).
1403
1404@findex edebug-`
1405If the backquoted forms are code to be evaluated, you can have Edebug
1406instrument them by using @code{edebug-`} instead of the regular
1407@code{`}. Unquoting forms can be used inside @code{edebug-`} anywhere a
1408form is normally allowed. But @code{(, @var{form})} may be used in two
1409other places specially recognized by Edebug: wherever a predicate
1410specification would match, and at the head of a list form where the
1411function name normally appears. The @var{form} inside a spliced
1412unquote, @code{(,@@ @var{form})}, will be instrumented, but the unquote
1413form itself will not be instrumented since this would interfere with the
1414splicing.
1415
1416There is one other complication with using @code{edebug-`}. If the
1417@code{edebug-`} call is in a macro and the macro may be called from code
1418that is also instrumented, and if unquoted forms contain any macro
1419arguments bound to instrumented forms, then you should modify the
1420specification for the macro as follows: the specifications for those
1421arguments must use @code{def-form} instead of @code{form}. (This is to
1422reestablish the Edebugging context for those external forms.)
1423
1424For example, the @code{for} macro (@pxref{Problems with Macros,,, elisp,
1425Emacs Lisp Reference Manual}) is shown here but with @code{edebug-`}
1426substituted for regular @code{`}.
1427
1428@example
1429(defmacro inc (var)
1430 (list 'setq var (list '1+ var)))
1431
1432(defmacro for (var from init to final do &rest body)
1433 (let ((tempvar (make-symbol "max")))
1434 (edebug-` (let (((, var) (, init))
1435 ((, tempvar) (, final)))
1436 (while (<= (, var) (, tempvar))
1437 (,@ body)
1438 (inc (, var)))))))
1439@end example
1440
1441Here is the corresponding modified Edebug specification and a
1442call of the macro:
1443
1444@example
1445(def-edebug-spec for
1446 (symbolp "from" def-form "to" def-form "do" &rest def-form))
1447
1448(let ((n 5))
1449 (for i from n to (* n (+ n 1)) do
1450 (message "%s" i)))
1451@end example
1452
1453After instrumenting the @code{for} macro and the macro call, Edebug
1454first steps to the beginning of the macro call, then into the macro
1455body, then through each of the unquoted expressions in the backquote
1456showing the expressions that will be embedded. Then when the macro
1457expansion is evaluated, Edebug will step through the @code{let} form and
1458each time it gets to an unquoted form, it will jump back to an argument
1459of the macro call to step through that expression. Finally stepping
1460will continue after the macro call. Even more convoluted execution
1461paths may result when using anonymous functions.
1462
1463@vindex edebug-unwrap-results
1464When the result of an expression is an instrumented expression, it is
1465difficult to see the expression inside the instrumentation. So
1466you may want to set the option @code{edebug-unwrap-results} to a
1467non-@code{nil} value while debugging such expressions, but it would slow
1468Edebug down to always do this.
1469
1470@end ignore
1471@node Specification Examples 1346@node Specification Examples
1472@subsubsection Specification Examples 1347@subsubsection Specification Examples
1473 1348
@@ -1492,11 +1367,11 @@ specifications. It is necessary to handle interactive forms specially
1492since an expression argument it is actually evaluated outside of the 1367since an expression argument it is actually evaluated outside of the
1493function body. 1368function body.
1494 1369
1495@example 1370@smallexample
1496(def-edebug-spec defmacro defun) ; @r{Indirect ref to @code{defun} spec} 1371(def-edebug-spec defmacro defun) ; @r{Indirect ref to @code{defun} spec.}
1497(def-edebug-spec defun 1372(def-edebug-spec defun
1498 (&define name lambda-list 1373 (&define name lambda-list
1499 [&optional stringp] ; @r{Match the doc string, if present.} 1374 [&optional stringp] ; @r{Match the doc string, if present.}
1500 [&optional ("interactive" interactive)] 1375 [&optional ("interactive" interactive)]
1501 def-body)) 1376 def-body))
1502 1377
@@ -1508,7 +1383,7 @@ function body.
1508 1383
1509(def-edebug-spec interactive 1384(def-edebug-spec interactive
1510 (&optional &or stringp def-form)) ; @r{Notice: @code{def-form}} 1385 (&optional &or stringp def-form)) ; @r{Notice: @code{def-form}}
1511@end example 1386@end smallexample
1512 1387
1513The specification for backquote below illustrates how to match 1388The specification for backquote below illustrates how to match
1514dotted lists and use @code{nil} to terminate recursion. It also 1389dotted lists and use @code{nil} to terminate recursion. It also
@@ -1516,15 +1391,15 @@ illustrates how components of a vector may be matched. (The actual
1516specification defined by Edebug does not support dotted lists because 1391specification defined by Edebug does not support dotted lists because
1517doing so causes very deep recursion that could fail.) 1392doing so causes very deep recursion that could fail.)
1518 1393
1519@example 1394@smallexample
1520(def-edebug-spec ` (backquote-form)) ;; alias just for clarity 1395(def-edebug-spec ` (backquote-form)) ; @r{Alias just for clarity.}
1521 1396
1522(def-edebug-spec backquote-form 1397(def-edebug-spec backquote-form
1523 (&or ([&or "," ",@@"] &or ("quote" backquote-form) form) 1398 (&or ([&or "," ",@@"] &or ("quote" backquote-form) form)
1524 (backquote-form . [&or nil backquote-form]) 1399 (backquote-form . [&or nil backquote-form])
1525 (vector &rest backquote-form) 1400 (vector &rest backquote-form)
1526 sexp)) 1401 sexp))
1527@end example 1402@end smallexample
1528 1403
1529 1404
1530@node Edebug Options 1405@node Edebug Options
@@ -1544,18 +1419,21 @@ but only when you also use Edebug.
1544@defopt edebug-all-defs 1419@defopt edebug-all-defs
1545If this is non-@code{nil}, normal evaluation of defining forms such as 1420If this is non-@code{nil}, normal evaluation of defining forms such as
1546@code{defun} and @code{defmacro} instruments them for Edebug. This 1421@code{defun} and @code{defmacro} instruments them for Edebug. This
1547applies to @code{eval-defun}, @code{eval-region}, and 1422applies to @code{eval-defun}, @code{eval-region}, @code{eval-buffer},
1548@code{eval-current-buffer}. @xref{Instrumenting}. 1423and @code{eval-current-buffer}.
1424
1425Use the command @kbd{M-x edebug-all-defs} to toggle the value of this
1426option. @xref{Instrumenting}.
1549@end defopt 1427@end defopt
1550 1428
1551@defopt edebug-all-forms 1429@defopt edebug-all-forms
1552If this is non-@code{nil}, the commands @code{eval-defun}, @code{eval-region}, 1430If this is non-@code{nil}, the commands @code{eval-defun},
1553and @code{eval-current-buffer} instrument all forms, even those that 1431@code{eval-region}, @code{eval-buffer}, and @code{eval-current-buffer}
1554don't define anything. 1432instrument all forms, even those that don't define anything.
1433This doesn't apply to loading or evaluations in the minibuffer.
1555 1434
1556Use the command @kbd{M-x edebug-all-forms} to toggle the value of this 1435Use the command @kbd{M-x edebug-all-forms} to toggle the value of this
1557option. 1436option. @xref{Instrumenting}.
1558@xref{Instrumenting}.
1559@end defopt 1437@end defopt
1560 1438
1561@defopt edebug-save-windows 1439@defopt edebug-save-windows
@@ -1572,12 +1450,13 @@ interactively. @xref{Edebug Display Update}.
1572@end defopt 1450@end defopt
1573 1451
1574@defopt edebug-save-displayed-buffer-points 1452@defopt edebug-save-displayed-buffer-points
1575If non-@code{nil}, Edebug saves and restores point in all buffers. 1453If this is non-@code{nil}, Edebug saves and restores point in all
1454displayed buffers.
1576 1455
1577Saving and restoring point in other buffers is necessary if you are 1456Saving and restoring point in other buffers is necessary if you are
1578debugging code that changes the point of a buffer which is displayed in 1457debugging code that changes the point of a buffer which is displayed in
1579a non-selected window. If Edebug or the user then selects the window, 1458a non-selected window. If Edebug or the user then selects the window,
1580the buffer's point will change to the window's point. 1459point in that buffer will move to the window's value of point.
1581 1460
1582Saving and restoring point in all buffers is expensive, since it 1461Saving and restoring point in all buffers is expensive, since it
1583requires selecting each window twice, so enable this only if you need 1462requires selecting each window twice, so enable this only if you need
@@ -1603,8 +1482,7 @@ function entry or exit per line, indented by the recursion level.
1603 1482
1604The default value is @code{nil}. 1483The default value is @code{nil}.
1605 1484
1606Also see @code{edebug-tracing}. 1485Also see @code{edebug-tracing}, in @xref{Trace Buffer}.
1607@xref{Tracing}.
1608@end defopt 1486@end defopt
1609 1487
1610@defopt edebug-test-coverage 1488@defopt edebug-test-coverage
@@ -1657,20 +1535,12 @@ Errors}.
1657 1535
1658 If you change the values of @code{edebug-on-error} or 1536 If you change the values of @code{edebug-on-error} or
1659@code{edebug-on-quit} while Edebug is active, their values won't be used 1537@code{edebug-on-quit} while Edebug is active, their values won't be used
1660until the @emph{next} time Edebug is invoked at a deeper command level. 1538until the @emph{next} time Edebug is invoked via a new command.
1661 1539@c Not necessarily a deeper command level.
1662@ignore 1540@c A new command is not precisely true, but that is close enough -- dan
1663@defopt edebug-unwrap-results
1664Non-@code{nil} if Edebug should unwrap results of expressions. This is
1665useful when debugging macros where the results of expressions are
1666instrumented expressions. But don't do this when results might be
1667circular, or an infinite loop will result. @xref{Debugging Backquote}.
1668@end defopt
1669@end ignore
1670 1541
1671@defopt edebug-global-break-condition 1542@defopt edebug-global-break-condition
1672If non-@code{nil}, an expression to test for at every stop point. 1543If non-@code{nil}, an expression to test for at every stop point.
1673If the result is non-nil, then break. Errors are ignored. 1544If the result is non-nil, then break. Errors are ignored.
1674@xref{Global Break Condition}. 1545@xref{Global Break Condition}.
1675@end defopt 1546@end defopt
1676
diff --git a/lispref/keymaps.texi b/lispref/keymaps.texi
index 298397e55ac..02bada65bce 100644
--- a/lispref/keymaps.texi
+++ b/lispref/keymaps.texi
@@ -21,7 +21,6 @@ is found. The whole process is called @dfn{key lookup}.
21* Inheritance and Keymaps:: How one keymap can inherit the bindings 21* Inheritance and Keymaps:: How one keymap can inherit the bindings
22 of another keymap. 22 of another keymap.
23* Prefix Keys:: Defining a key with a keymap as its definition. 23* Prefix Keys:: Defining a key with a keymap as its definition.
24* Menu Keymaps:: A keymap can define a menu.
25* Active Keymaps:: Each buffer has a local keymap 24* Active Keymaps:: Each buffer has a local keymap
26 to override the standard (global) bindings. 25 to override the standard (global) bindings.
27 A minor mode can also override them. 26 A minor mode can also override them.
@@ -30,6 +29,7 @@ is found. The whole process is called @dfn{key lookup}.
30* Changing Key Bindings:: Redefining a key in a keymap. 29* Changing Key Bindings:: Redefining a key in a keymap.
31* Key Binding Commands:: Interactive interfaces for redefining keys. 30* Key Binding Commands:: Interactive interfaces for redefining keys.
32* Scanning Keymaps:: Looking through all keymaps, for printing help. 31* Scanning Keymaps:: Looking through all keymaps, for printing help.
32* Menu Keymaps:: A keymap can define a menu.
33@end menu 33@end menu
34 34
35@node Keymap Terminology 35@node Keymap Terminology
@@ -62,7 +62,7 @@ used up.
62 62
63 If the binding of a key sequence is a keymap, we call the key sequence 63 If the binding of a key sequence is a keymap, we call the key sequence
64a @dfn{prefix key}. Otherwise, we call it a @dfn{complete key} (because 64a @dfn{prefix key}. Otherwise, we call it a @dfn{complete key} (because
65no more characters can be added to it). If the binding is @code{nil}, 65no more events can be added to it). If the binding is @code{nil},
66we call the key @dfn{undefined}. Examples of prefix keys are @kbd{C-c}, 66we call the key @dfn{undefined}. Examples of prefix keys are @kbd{C-c},
67@kbd{C-x}, and @kbd{C-x 4}. Examples of defined complete keys are 67@kbd{C-x}, and @kbd{C-x 4}. Examples of defined complete keys are
68@kbd{X}, @key{RET}, and @kbd{C-x 4 C-f}. Examples of undefined complete 68@kbd{X}, @key{RET}, and @kbd{C-x 4 C-f}. Examples of undefined complete
@@ -87,7 +87,7 @@ prefix keys for its well-formedness.
87use for finding key bindings. These are the @dfn{global map}, which is 87use for finding key bindings. These are the @dfn{global map}, which is
88shared by all buffers; the @dfn{local keymap}, which is usually 88shared by all buffers; the @dfn{local keymap}, which is usually
89associated with a specific major mode; and zero or more @dfn{minor mode 89associated with a specific major mode; and zero or more @dfn{minor mode
90keymaps} which belong to currently enabled minor modes. (Not all minor 90keymaps}, which belong to currently enabled minor modes. (Not all minor
91modes have keymaps.) The local keymap bindings shadow (i.e., take 91modes have keymaps.) The local keymap bindings shadow (i.e., take
92precedence over) the corresponding global bindings. The minor mode 92precedence over) the corresponding global bindings. The minor mode
93keymaps shadow both local and global keymaps. @xref{Active Keymaps}, 93keymaps shadow both local and global keymaps. @xref{Active Keymaps},
@@ -105,11 +105,13 @@ remaining elements of the list define the key bindings of the keymap.
105Use the function @code{keymapp} (see below) to test whether an object is 105Use the function @code{keymapp} (see below) to test whether an object is
106a keymap. 106a keymap.
107 107
108 An ordinary element is a cons cell of the form @code{(@var{type} .@: 108 Each ordinary binding applies to events of a particular @dfn{event
109@var{binding})}. This specifies one binding which applies to events of 109type}, which is always a character or a symbol. @xref{Classifying
110type @var{type}. Each ordinary binding applies to events of a 110Events}.
111particular @dfn{event type}, which is always a character or a symbol. 111
112@xref{Classifying Events}. 112 An ordinary element of a keymap is a cons cell of the form
113@code{(@var{type} .@: @var{binding})}. This specifies one binding, for
114events of type @var{type}.
113 115
114@cindex default key binding 116@cindex default key binding
115@c Emacs 19 feature 117@c Emacs 19 feature
@@ -122,7 +124,7 @@ keymap.
122 124
123 If an element of a keymap is a vector, the vector counts as bindings 125 If an element of a keymap is a vector, the vector counts as bindings
124for all the @sc{ASCII} characters; vector element @var{n} is the binding 126for all the @sc{ASCII} characters; vector element @var{n} is the binding
125for the character with code @var{n}. This is a more compact way to 127for the character with code @var{n}. This is a compact way to
126record lots of bindings. A keymap with such a vector is called a 128record lots of bindings. A keymap with such a vector is called a
127@dfn{full keymap}. Other keymaps are called @dfn{sparse keymaps}. 129@dfn{full keymap}. Other keymaps are called @dfn{sparse keymaps}.
128 130
@@ -184,7 +186,7 @@ lisp-mode-map
184 186
185@defun keymapp object 187@defun keymapp object
186This function returns @code{t} if @var{object} is a keymap, @code{nil} 188This function returns @code{t} if @var{object} is a keymap, @code{nil}
187otherwise. Practically speaking, this function tests for a list whose 189otherwise. More precisely, this function tests for a list whose
188@sc{car} is @code{keymap}. 190@sc{car} is @code{keymap}.
189 191
190@example 192@example
@@ -206,8 +208,8 @@ otherwise. Practically speaking, this function tests for a list whose
206 Here we describe the functions for creating keymaps. 208 Here we describe the functions for creating keymaps.
207 209
208@defun make-keymap &optional prompt 210@defun make-keymap &optional prompt
209This function creates and returns a new full keymap (i.e., one which 211This function creates and returns a new full keymap (i.e., one
210contains a vector of length 128 for defining all the @sc{ASCII} 212containing a vector of length 128 for defining all the @sc{ASCII}
211characters). The new keymap initially binds all @sc{ASCII} characters 213characters). The new keymap initially binds all @sc{ASCII} characters
212to @code{nil}, and does not bind any other kind of event. 214to @code{nil}, and does not bind any other kind of event.
213 215
@@ -237,7 +239,7 @@ specifies a prompt string, as in @code{make-keymap}.
237@end defun 239@end defun
238 240
239@defun copy-keymap keymap 241@defun copy-keymap keymap
240This function returns a copy of @var{keymap}. Any keymaps which 242This function returns a copy of @var{keymap}. Any keymaps that
241appear directly as bindings in @var{keymap} are also copied recursively, 243appear directly as bindings in @var{keymap} are also copied recursively,
242and so on to any number of levels. However, recursive copying does not 244and so on to any number of levels. However, recursive copying does not
243take place when the definition of a character is a symbol whose function 245take place when the definition of a character is a symbol whose function
@@ -303,9 +305,9 @@ from @code{text-mode-map}:
303@section Prefix Keys 305@section Prefix Keys
304@cindex prefix key 306@cindex prefix key
305 307
306 A @dfn{prefix key} has an associated keymap which defines what to do 308 A @dfn{prefix key} has an associated keymap that defines what to do
307with key sequences that start with the prefix key. For example, 309with key sequences that start with the prefix key. For example,
308@kbd{C-x} is a prefix key, and it uses a keymap which is also stored in 310@kbd{C-x} is a prefix key, and it uses a keymap that is also stored in
309the variable @code{ctl-x-map}. Here is a list of the standard prefix 311the variable @code{ctl-x-map}. Here is a list of the standard prefix
310keys of Emacs and their keymaps: 312keys of Emacs and their keymaps:
311 313
@@ -333,9 +335,8 @@ describes the main use of the @kbd{C-c} prefix key.
333@cindex @kbd{C-x} 335@cindex @kbd{C-x}
334@vindex ctl-x-map 336@vindex ctl-x-map
335@findex Control-X-prefix 337@findex Control-X-prefix
336@code{ctl-x-map} is the variable name for the map used for events 338@code{ctl-x-map} is the map used for events that follow @kbd{C-x}. This
337that follow @kbd{C-x}. This map is also the function definition of 339map is also the function definition of @code{Control-X-prefix}.
338@code{Control-X-prefix}.
339 340
340@item 341@item
341@cindex @kbd{C-x 4} 342@cindex @kbd{C-x 4}
@@ -346,7 +347,7 @@ that follow @kbd{C-x}. This map is also the function definition of
346@item 347@item
347@cindex @kbd{C-x 5} 348@cindex @kbd{C-x 5}
348@vindex ctl-x-5-map 349@vindex ctl-x-5-map
349@code{ctl-x-5-map} used is for events that follow @kbd{C-x 5}. 350@code{ctl-x-5-map} is used for events that follow @kbd{C-x 5}.
350 351
351@c Emacs 19 feature 352@c Emacs 19 feature
352@item 353@item
@@ -365,9 +366,9 @@ the symbol @code{Control-X-prefix}, whose function definition is the
365keymap for @kbd{C-x} commands. (The same keymap is also the value of 366keymap for @kbd{C-x} commands. (The same keymap is also the value of
366@code{ctl-x-map}.) 367@code{ctl-x-map}.)
367 368
368 Prefix key definitions of this sort can appear in any active keymap. 369 Prefix key definitions can appear in any active keymap. The
369The definitions of @kbd{C-c}, @kbd{C-x}, @kbd{C-h} and @key{ESC} as 370definitions of @kbd{C-c}, @kbd{C-x}, @kbd{C-h} and @key{ESC} as prefix
370prefix keys appear in the global map, so these prefix keys are always 371keys appear in the global map, so these prefix keys are always
371available. Major and minor modes can redefine a key as a prefix by 372available. Major and minor modes can redefine a key as a prefix by
372putting a prefix key definition for it in the local map or the minor 373putting a prefix key definition for it in the local map or the minor
373mode's map. @xref{Active Keymaps}. 374mode's map. @xref{Active Keymaps}.
@@ -405,313 +406,16 @@ active keymap.
405 406
406@defun define-prefix-command symbol 407@defun define-prefix-command symbol
407@cindex prefix command 408@cindex prefix command
408 This function defines @var{symbol} as a prefix command: it creates a 409This function defines @var{symbol} as a prefix command: it creates a
409full keymap and stores it as @var{symbol}'s function definition. 410full keymap and stores it as @var{symbol}'s function definition.
410Storing the symbol as the binding of a key makes the key a prefix key 411Storing the symbol as the binding of a key makes the key a prefix key
411which has a name. It also sets @var{symbol} as a variable, to have the 412that has a name. The function also sets @var{symbol} as a variable, to
412keymap as its value. The function returns @var{symbol}. 413have the keymap as its value. It returns @var{symbol}.
413 414
414 In Emacs version 18, only the function definition of @var{symbol} was 415 In Emacs version 18, only the function definition of @var{symbol} was
415set, not the value as a variable. 416set, not the value as a variable.
416@end defun 417@end defun
417 418
418@node Menu Keymaps
419@section Menu Keymaps
420@cindex menu keymaps
421
422@c Emacs 19 feature
423A keymap can define a menu as well as bindings for keyboard keys and
424mouse button. Menus are usually actuated with the mouse, but they can
425work with the keyboard also.
426
427@menu
428* Defining Menus:: How to make a keymap that defines a menu.
429* Mouse Menus:: How users actuate the menu with the mouse.
430* Keyboard Menus:: How they actuate it with the keyboard.
431* Menu Example:: Making a simple menu.
432* Menu Bar:: How to customize the menu bar.
433* Modifying Menus:: How to add new items to a menu.
434@end menu
435
436@node Defining Menus
437@subsection Defining Menus
438@cindex defining menus
439@cindex menu prompt string
440@cindex prompt string (of menu)
441
442A keymap is suitable for menu use if it has an @dfn{overall prompt
443string}, which is a string that appears as an element of the keymap.
444(@xref{Format of Keymaps}.) The string should describe the purpose of
445the menu. The easiest way to construct a keymap with a prompt string is
446to specify the string as an argument when you call @code{make-keymap} or
447@code{make-sparse-keymap} (@pxref{Creating Keymaps}).
448
449The individual bindings in the menu keymap should have item
450strings; these strings become the items displayed in the menu. A
451binding with a item string looks like this:
452
453@example
454(@var{string} . @var{real-binding})
455@end example
456
457The item string for a binding should be short---one or two words. It
458should describe the action of the command it corresponds to.
459
460As far as @code{define-key} is concerned, @var{string} is part of the
461event's binding. However, @code{lookup-key} returns just
462@var{real-binding}, and only @var{real-binding} is used for executing
463the key.
464
465You can also supply a second string, called the help string, as follows:
466
467@example
468(@var{string} @var{help-string} . @var{real-binding})
469@end example
470
471Currently Emacs does not actually use @var{help-string}; it knows only
472how to ignore @var{help-string} in order to extract @var{real-binding}.
473In the future we hope to make @var{help-string} serve as extended
474documentation for the menu item, available on request.
475
476If @var{real-binding} is @code{nil}, then @var{string} appears in the
477menu but cannot be selected.
478
479If @var{real-binding} is a symbol, and has a non-@code{nil}
480@code{menu-enable} property, that property is an expression which
481controls whether the menu item is enabled. Every time the keymap is
482used to display a menu, Emacs evaluates the expression, and it enables
483the menu item only if the expression's value is non-@code{nil}. When a
484menu item is disabled, it is displayed in a ``fuzzy'' fashion, and
485cannot be selected with the mouse.
486
487The order of items in the menu is the same as the order of bindings in
488the keymap. Since @code{define-key} puts new bindings at the front, you
489should define the menu items starting at the bottom of the menu and
490moving to the top, if you care about the order. When you add an item to
491an existing menu, you can specify its position in the menu using
492@code{define-key-after} (@pxref{Modifying Menus}).
493
494You've probably noticed that menu items show the equivalent keyboard key
495sequence (if any) to invoke the same command. To save time on
496recalculation, menu display caches this information in a sublist in the
497binding, like this:
498
499@c This line is not too long--rms.
500@example
501(@var{string} @r{[}@var{help-string}@r{]} (@var{key-binding-data}) . @var{real-binding})
502@end example
503
504Don't put these sublists in the menu item yourself; menu display
505calculates them automatically. Don't add keyboard equivalents to the
506item string yourself, for that is redundant.
507
508@node Mouse Menus
509@subsection Menus and the Mouse
510
511The way to make a menu keymap produce a menu is to make it the
512definition of a prefix key.
513
514If the prefix key ends with a mouse event, Emacs handles the menu keymap
515by popping up a visible menu, so that the user can select a choice with
516the mouse. When the user clicks on a menu item, the event generated is
517whatever character or symbol has the binding which brought about that
518menu item. (A menu item may generate a series of events if the menu has
519multiple levels or comes from the menu bar.)
520
521It's often best to use a button-down event to trigger the menu. Then
522the user can select a menu item by releasing the button.
523
524A single keymap can appear as multiple menu panes, if you explicitly
525arrange for this. The way to do this is to make a keymap for each pane,
526then create a binding for each of those maps in the main keymap of the
527menu. Give each of these bindings a item string that starts with
528@samp{@@}. The rest of the item string becomes the name of the pane.
529See the file @file{lisp/mouse.el} for an example of this. Any ordinary
530bindings with @samp{@@}-less item strings are grouped into one pane,
531which appears along with the other panes explicitly created for the
532submaps.
533
534X toolkit menus don't have panes; instead, they can have submenus.
535Every nested keymap becomes a submenu, whether the item string starts
536with @samp{@@} or not. In a toolkit version of Emacs, The only thing
537special about @samp{@@} at the beginning of an item string is that the
538@samp{@@} doesn't appear in the menu item.
539
540You can also get multiple panes from separate keymaps. The full
541definition of a prefix key always comes from merging the definitions
542supplied by the various active keymaps (minor mode, local, and
543global). When more than one of these keymaps is a menu, each of them
544makes a separate pane or panes. @xref{Active Keymaps}.
545
546In toolkit versions of Emacs, menus don't have panes, so submenus are
547used to represent the separate keymaps. Each keymap's contribution
548becomes one submenu.
549
550A Lisp program can explicitly pop up a menu and receive the user's
551choice. You can use keymaps for this also. @xref{Pop-Up Menus}.
552
553@node Keyboard Menus
554@subsection Menus and the Keyboard
555
556When a prefix key ending with a keyboard event (a character or function
557key) has a definition that is a menu keymap, the user can use the
558keyboard to choose a menu item.
559
560Emacs displays the menu alternatives (the item strings of the
561bindings) in the echo area. If they don't all fit at once, the user can
562type @key{SPC} to see the next line of alternatives. Successive uses of
563@key{SPC} eventually get to the end of the menu and then cycle around to
564the beginning.
565
566When the user has found the desired alternative from the menu, he or she
567should type the corresponding character---the one whose binding is that
568alternative.
569
570In a menu intended for keyboard use, each menu item must clearly
571indicate what character to type. The best convention to use is to make
572the character the first letter of the item string. That is something
573users will understand without being told.
574
575This way of using menus in an Emacs-like editor was inspired by the
576Hierarkey system.
577
578@defvar menu-prompt-more-char
579This variable specifies the character to use to ask to see
580the next line of a menu. Its initial value is 32, the code
581for @key{SPC}.
582@end defvar
583
584@node Menu Example
585@subsection Menu Example
586
587 Here is a simple example of how to set up a menu for mouse use.
588
589@example
590(defvar my-menu-map
591 (make-sparse-keymap "Key Commands <==> Functions"))
592(fset 'help-for-keys my-menu-map)
593
594(define-key my-menu-map [bindings]
595 '("List all keystroke commands" . describe-bindings))
596(define-key my-menu-map [key]
597 '("Describe key briefly" . describe-key-briefly))
598(define-key my-menu-map [key-verbose]
599 '("Describe key verbose" . describe-key))
600(define-key my-menu-map [function]
601 '("Describe Lisp function" . describe-function))
602(define-key my-menu-map [where-is]
603 '("Where is this command" . where-is))
604
605(define-key global-map [C-S-down-mouse-1] 'help-for-keys)
606@end example
607
608 The symbols used in the key sequences bound in the menu are fictitious
609``function keys''; they don't appear on the keyboard, but that doesn't
610stop you from using them in the menu. Their names were chosen to be
611mnemonic, because they show up in the output of @code{where-is} and
612@code{apropos} to identify the corresponding menu items.
613
614 However, if you want the menu to be usable from the keyboard as well,
615you must bind real @sc{ASCII} characters as well as fictitious function
616keys.
617
618@node Menu Bar
619@subsection The Menu Bar
620@cindex menu bar
621
622 Most window systems allow each frame to have a @dfn{menu bar}---a
623permanently displayed menu stretching horizontally across the top of the
624frame. The items of the menu bar are the subcommands of the fake
625``function key'' @code{menu-bar}, as defined by all the active keymaps.
626
627 To add an item to the menu bar, invent a fake ``function key'' of your
628own (let's call it @var{key}), and make a binding for the key sequence
629@code{[menu-bar @var{key}]}. Most often, the binding is a menu keymap,
630so that pressing a button on the menu bar item leads to another menu.
631
632 When more than one active keymap defines the same fake function key
633for the menu bar, the item appears just once. If the user clicks on
634that menu bar item, it brings up a single, combined submenu containing
635all the subcommands of that item---the global subcommands, the local
636subcommands, and the minor mode subcommands, all together.
637
638 In order for a frame to display a menu bar, its @code{menu-bar-lines}
639parameter must be greater than zero. Emacs uses just one line for the
640menu bar itself; if you specify more than one line, the other lines
641serve to separate the menu bar from the windows in the frame. We
642recommend you try 1 or 2 as the value of @code{menu-bar-lines}. @xref{X
643Frame Parameters}.
644
645 Here's an example of setting up a menu bar item:
646
647@smallexample
648(modify-frame-parameters (selected-frame) '((menu-bar-lines . 2)))
649
650;; @r{Make a menu keymap (with a prompt string)}
651;; @r{and make it the menu bar item's definition.}
652(define-key global-map [menu-bar words]
653 (cons "Words" (make-sparse-keymap "Words")))
654
655@group
656;; @r{Define specific subcommands in the item's menu.}
657(define-key global-map
658 [menu-bar words forward]
659 '("Forward word" . forward-word))
660@end group
661@group
662(define-key global-map
663 [menu-bar words backward]
664 '("Backward word" . backward-word))
665@end group
666@end smallexample
667
668 A local keymap can cancel a menu bar item made by the global keymap by
669rebinding the same fake function key with @code{undefined} as the
670binding. For example, this is how Dired suppresses the @samp{Edit} menu
671bar item:
672
673@example
674(define-key dired-mode-map [menu-bar edit] 'undefined)
675@end example
676
677@noindent
678@code{edit} is the fake function key used by the global map for the
679@samp{Edit} menu bar item. The main reason to suppress a global
680menu bar item is to regain space for mode-specific items.
681
682@defvar menu-bar-final-items
683Normally the menu bar shows global items followed by items defined by the
684local maps.
685
686This variable holds a list of fake function keys for items to display at
687the end of the menu bar rather than in normal sequence. The default
688value is @code{(help)}; thus, the @samp{Help} menu item normally appears
689at the end of the menu bar, following local menu items.
690@end defvar
691
692@node Modifying Menus
693@subsection Modifying Menus
694
695 When you insert a new item in an existing menu, you probably want to
696put it in a particular place among the menu's existing items. If you
697use @code{define-key} to add the item, it normally goes at the front of
698the menu. To put it elsewhere, use @code{define-key-after}:
699
700@defun define-key-after map key binding after
701Define a binding in @var{map} for @var{key}, with value @var{binding},
702just like @code{define-key}, but position the binding in @var{map} after
703the binding for the key @var{after}. For example,
704
705@example
706(define-key-after my-menu [drink]
707 '("Drink" . drink-command) [eat])
708@end example
709
710@noindent
711makes a binding for the fake function key @key{drink} and puts it
712right after the binding for @key{eat}.
713@end defun
714
715@node Active Keymaps 419@node Active Keymaps
716@section Active Keymaps 420@section Active Keymaps
717@cindex active keymap 421@cindex active keymap
@@ -756,10 +460,9 @@ map.
756 460
757@cindex major mode keymap 461@cindex major mode keymap
758 Since every buffer that uses the same major mode normally uses the 462 Since every buffer that uses the same major mode normally uses the
759very same local keymap, it may appear as if the keymap is local to the 463same local keymap, you can think of the keymap as local to the mode. A
760mode. A change to the local keymap of a buffer (using 464change to the local keymap of a buffer (using @code{local-set-key}, for
761@code{local-set-key}, for example) will be seen also in the other 465example) is seen also in the other buffers that share that keymap.
762buffers that share that keymap.
763 466
764 The local keymaps that are used for Lisp mode, C mode, and several 467 The local keymaps that are used for Lisp mode, C mode, and several
765other major modes exist even if they have not yet been used. These 468other major modes exist even if they have not yet been used. These
@@ -774,7 +477,7 @@ and exit commands. @xref{Intro to Minibuffers}.
774 @xref{Standard Keymaps}, for a list of standard keymaps. 477 @xref{Standard Keymaps}, for a list of standard keymaps.
775 478
776@defvar global-map 479@defvar global-map
777 This variable contains the default global keymap that maps Emacs 480This variable contains the default global keymap that maps Emacs
778keyboard input to commands. The global keymap is normally this keymap. 481keyboard input to commands. The global keymap is normally this keymap.
779The default global keymap is a full keymap that binds 482The default global keymap is a full keymap that binds
780@code{self-insert-command} to all of the printing characters. 483@code{self-insert-command} to all of the printing characters.
@@ -785,7 +488,7 @@ out with.
785@end defvar 488@end defvar
786 489
787@defun current-global-map 490@defun current-global-map
788 This function returns the current global keymap. This is the 491This function returns the current global keymap. This is the
789same as the value of @code{global-map} unless you change one or the 492same as the value of @code{global-map} unless you change one or the
790other. 493other.
791 494
@@ -799,7 +502,7 @@ other.
799@end defun 502@end defun
800 503
801@defun current-local-map 504@defun current-local-map
802 This function returns the current buffer's local keymap, or @code{nil} 505This function returns the current buffer's local keymap, or @code{nil}
803if it has none. In the following example, the keymap for the 506if it has none. In the following example, the keymap for the
804@samp{*scratch*} buffer (using Lisp Interaction mode) is a sparse keymap 507@samp{*scratch*} buffer (using Lisp Interaction mode) is a sparse keymap
805in which the entry for @key{ESC}, @sc{ASCII} code 27, is another sparse 508in which the entry for @key{ESC}, @sc{ASCII} code 27, is another sparse
@@ -826,14 +529,14 @@ This function returns a list of the keymaps of currently enabled minor modes.
826@end defun 529@end defun
827 530
828@defun use-global-map keymap 531@defun use-global-map keymap
829 This function makes @var{keymap} the new current global keymap. It 532This function makes @var{keymap} the new current global keymap. It
830returns @code{nil}. 533returns @code{nil}.
831 534
832 It is very unusual to change the global keymap. 535It is very unusual to change the global keymap.
833@end defun 536@end defun
834 537
835@defun use-local-map keymap 538@defun use-local-map keymap
836 This function makes @var{keymap} the new local keymap of the current 539This function makes @var{keymap} the new local keymap of the current
837buffer. If @var{keymap} is @code{nil}, then the buffer has no local 540buffer. If @var{keymap} is @code{nil}, then the buffer has no local
838keymap. @code{use-local-map} returns @code{nil}. Most major mode 541keymap. @code{use-local-map} returns @code{nil}. Most major mode
839commands use this function. 542commands use this function.
@@ -850,7 +553,7 @@ like this:
850@end example 553@end example
851 554
852The keymap @var{keymap} is active whenever @var{variable} has a 555The keymap @var{keymap} is active whenever @var{variable} has a
853non-@code{nil} value. Typically @var{variable} is the variable which 556non-@code{nil} value. Typically @var{variable} is the variable that
854enables or disables a minor mode. @xref{Keymaps and Minor Modes}. 557enables or disables a minor mode. @xref{Keymaps and Minor Modes}.
855 558
856Note that elements of @code{minor-mode-map-alist} do not have the same 559Note that elements of @code{minor-mode-map-alist} do not have the same
@@ -873,10 +576,10 @@ minor modes.
873@end defvar 576@end defvar
874 577
875@defvar overriding-local-map 578@defvar overriding-local-map
876If non-@code{nil}, a keymap to use instead of the buffer's local keymap 579If non-@code{nil}, this variable holds a keymap to use instead of the
877and instead of all the minor mode keymaps. This keymap, if any, 580buffer's local keymap and instead of all the minor mode keymaps. This
878overrides all other maps that would have been active, except for the 581keymap, if any, overrides all other maps that would have been active,
879current global map. 582except for the current global map.
880@end defvar 583@end defvar
881 584
882@node Key Lookup 585@node Key Lookup
@@ -888,7 +591,7 @@ current global map.
888sequence from a given keymap. Actual execution of the binding is not 591sequence from a given keymap. Actual execution of the binding is not
889part of key lookup. 592part of key lookup.
890 593
891 Key lookup uses just the event types of each event in the key 594 Key lookup uses just the event type of each event in the key
892sequence; the rest of the event is ignored. In fact, a key sequence 595sequence; the rest of the event is ignored. In fact, a key sequence
893used for key lookup may designate mouse events with just their types 596used for key lookup may designate mouse events with just their types
894(symbols) instead of with entire mouse events (lists). @xref{Input 597(symbols) instead of with entire mouse events (lists). @xref{Input
@@ -930,7 +633,7 @@ event of the key sequence is looked up in @var{keymap}.
930@item @var{command} 633@item @var{command}
931@cindex command in keymap 634@cindex command in keymap
932The events used so far in the lookup form a complete key, 635The events used so far in the lookup form a complete key,
933and @var{command} is its binding. 636and @var{command} is its binding. @xref{What Is a Function}.
934 637
935@item @var{string} 638@item @var{string}
936@itemx @var{vector} 639@itemx @var{vector}
@@ -975,16 +678,16 @@ binding of @kbd{Meta-@key{SPC}}, whatever that may be.''
975@cindex symbol in keymap 678@cindex symbol in keymap
976The function definition of @var{symbol} is used in place of 679The function definition of @var{symbol} is used in place of
977@var{symbol}. If that too is a symbol, then this process is repeated, 680@var{symbol}. If that too is a symbol, then this process is repeated,
978any number of times. Ultimately this should lead to an object which is 681any number of times. Ultimately this should lead to an object that is
979a keymap, a command or a keyboard macro. A list is allowed if it is a 682a keymap, a command or a keyboard macro. A list is allowed if it is a
980keymap or a command, but indirect entries are not understood when found 683keymap or a command, but indirect entries are not understood when found
981via symbols. 684via symbols.
982 685
983Note that keymaps and keyboard macros (strings and vectors) are not 686Note that keymaps and keyboard macros (strings and vectors) are not
984valid functions, so a symbol with a keymap, string or vector as its 687valid functions, so a symbol with a keymap, string, or vector as its
985function definition is also invalid as a function. It is, however, 688function definition is invalid as a function. It is, however, valid as
986valid as a key binding. If the definition is a keyboard macro, then the 689a key binding. If the definition is a keyboard macro, then the symbol
987symbol is also valid as an argument to @code{command-execute} 690is also valid as an argument to @code{command-execute}
988(@pxref{Interactive Call}). 691(@pxref{Interactive Call}).
989 692
990@cindex @code{undefined} in keymap 693@cindex @code{undefined} in keymap
@@ -1007,7 +710,7 @@ binding is not executable as a command.
1007@end table 710@end table
1008 711
1009 In short, a keymap entry may be a keymap, a command, a keyboard macro, 712 In short, a keymap entry may be a keymap, a command, a keyboard macro,
1010a symbol which leads to one of them, or an indirection or @code{nil}. 713a symbol that leads to one of them, or an indirection or @code{nil}.
1011Here is an example of a sparse keymap with two characters bound to 714Here is an example of a sparse keymap with two characters bound to
1012commands and one bound to another keymap. This map is the normal value 715commands and one bound to another keymap. This map is the normal value
1013of @code{emacs-lisp-mode-map}. Note that 9 is the code for @key{TAB}, 716of @code{emacs-lisp-mode-map}. Note that 9 is the code for @key{TAB},
@@ -1039,7 +742,8 @@ If @var{accept-defaults} is non-@code{nil}, then @code{lookup-key}
1039considers default bindings as well as bindings for the specific events 742considers default bindings as well as bindings for the specific events
1040in @var{key}. Otherwise, @code{lookup-key} reports only bindings for 743in @var{key}. Otherwise, @code{lookup-key} reports only bindings for
1041the specific sequence @var{key}, ignoring default bindings except when 744the specific sequence @var{key}, ignoring default bindings except when
1042an element of @var{key} is @code{t}. 745you explicitly ask about them. (To do this, supply @code{t} as an
746element of @var{key}; see @ref{Format of Keymaps}.)
1043 747
1044All the other functions described in this chapter that look up keys use 748All the other functions described in this chapter that look up keys use
1045@code{lookup-key}. 749@code{lookup-key}.
@@ -1090,7 +794,7 @@ keymaps, trying all the active keymaps. The result is @code{nil} if
1090 794
1091@c Emacs 19 feature 795@c Emacs 19 feature
1092The argument @var{accept-defaults} controls checking for default 796The argument @var{accept-defaults} controls checking for default
1093bindings, as in @code{lookup-key}. 797bindings, as in @code{lookup-key} (above).
1094 798
1095An error is signaled if @var{key} is not a string or a vector. 799An error is signaled if @var{key} is not a string or a vector.
1096 800
@@ -1124,8 +828,8 @@ as in @code{lookup-key} (above).
1124@defun minor-mode-key-binding key &optional accept-defaults 828@defun minor-mode-key-binding key &optional accept-defaults
1125This function returns a list of all the active minor mode bindings of 829This function returns a list of all the active minor mode bindings of
1126@var{key}. More precisely, it returns an alist of pairs 830@var{key}. More precisely, it returns an alist of pairs
1127@code{(@var{modename} . @var{binding})}, where @var{modename} is the the 831@code{(@var{modename} . @var{binding})}, where @var{modename} is the
1128variable which enables the minor mode, and @var{binding} is @var{key}'s 832variable that enables the minor mode, and @var{binding} is @var{key}'s
1129binding in that mode. If @var{key} has no minor-mode bindings, the 833binding in that mode. If @var{key} has no minor-mode bindings, the
1130value is @code{nil}. 834value is @code{nil}.
1131 835
@@ -1187,48 +891,27 @@ meta-prefix-char ; @r{The default value.}
1187@cindex rebinding 891@cindex rebinding
1188 892
1189 The way to rebind a key is to change its entry in a keymap. If you 893 The way to rebind a key is to change its entry in a keymap. If you
1190change the global keymap, the change is effective in all buffers (except 894change a binding in the global keymap, the change is effective in all
1191those that override the global binding with a local one). If you change 895buffers (though it has no direct effect in buffers that shadow the
1192the current buffer's local map, that usually affects all buffers using 896global binding with a local one). If you change the current buffer's
1193the same major mode. The @code{global-set-key} and @code{local-set-key} 897local map, that usually affects all buffers using the same major mode.
1194functions are convenient interfaces for these operations. Or you can 898The @code{global-set-key} and @code{local-set-key} functions are
1195use @code{define-key} and specify explicitly which map to change. 899convenient interfaces for these operations (@pxref{Key Binding
1196 900Commands}). You can also use @code{define-key}, a more general
1197 People often use @code{global-set-key} in their @file{.emacs} file for 901function; then you must specify explicitly the map to change.
1198simple customization. For example,
1199
1200@smallexample
1201(global-set-key "\C-x\C-\\" 'next-line)
1202@end smallexample
1203
1204@noindent
1205or
1206
1207@smallexample
1208(global-set-key [?\C-x ?\C-\\] 'next-line)
1209@end smallexample
1210
1211@noindent
1212redefines @kbd{C-x C-\} to move down a line.
1213
1214@smallexample
1215(global-set-key [M-mouse-1] 'mouse-set-point)
1216@end smallexample
1217
1218@noindent
1219redefines the first (leftmost) mouse button, typed with the Meta key, to
1220set point where you click.
1221 902
1222@cindex meta character key constants 903@cindex meta character key constants
1223@cindex control character key constants 904@cindex control character key constants
1224 In writing the key sequence to rebind, it is useful to use the special 905 In writing the key sequence to rebind, it is good to use the special
1225escape sequences for control and meta characters (@pxref{String Type}). 906escape sequences for control and meta characters (@pxref{String Type}).
1226The syntax @samp{\C-} means that the following character is a control 907The syntax @samp{\C-} means that the following character is a control
1227character and @samp{\M-} means that the following character is a meta 908character and @samp{\M-} means that the following character is a meta
1228character. Thus, the string @code{"\M-x"} is read as containing a 909character. Thus, the string @code{"\M-x"} is read as containing a
1229single @kbd{M-x}, @code{"\C-f"} is read as containing a single 910single @kbd{M-x}, @code{"\C-f"} is read as containing a single
1230@kbd{C-f}, and @code{"\M-\C-x"} and @code{"\C-\M-x"} are both read as 911@kbd{C-f}, and @code{"\M-\C-x"} and @code{"\C-\M-x"} are both read as
1231containing a single @kbd{C-M-x}. 912containing a single @kbd{C-M-x}. You can also use this escape syntax in
913vectors, as well as others that aren't allowed in strings; one example
914is @samp{[?\C-\H-x home]}. @xref{Character Type}.
1232 915
1233 For the functions below, an error is signaled if @var{keymap} is not a 916 For the functions below, an error is signaled if @var{keymap} is not a
1234keymap or if @var{key} is not a string or vector representing a key 917keymap or if @var{key} is not a string or vector representing a key
@@ -1236,7 +919,7 @@ sequence. You can use event types (symbols) as shorthand for events
1236that are lists. 919that are lists.
1237 920
1238@defun define-key keymap key binding 921@defun define-key keymap key binding
1239 This function sets the binding for @var{key} in @var{keymap}. (If 922This function sets the binding for @var{key} in @var{keymap}. (If
1240@var{key} is more than one event long, the change is actually made 923@var{key} is more than one event long, the change is actually made
1241in another keymap reached from @var{keymap}.) The argument 924in another keymap reached from @var{keymap}.) The argument
1242@var{binding} can be any Lisp object, but only certain types are 925@var{binding} can be any Lisp object, but only certain types are
@@ -1245,7 +928,7 @@ The value returned by @code{define-key} is @var{binding}.
1245 928
1246@cindex invalid prefix key error 929@cindex invalid prefix key error
1247@cindex key sequence error 930@cindex key sequence error
1248 Every prefix of @var{key} must be a prefix key (i.e., bound to a 931Every prefix of @var{key} must be a prefix key (i.e., bound to a
1249keymap) or undefined; otherwise an error is signaled. 932keymap) or undefined; otherwise an error is signaled.
1250 933
1251If some prefix of @var{key} is undefined, then @code{define-key} defines 934If some prefix of @var{key} is undefined, then @code{define-key} defines
@@ -1253,7 +936,8 @@ it as a prefix key so that the rest of @var{key} may be defined as
1253specified. 936specified.
1254@end defun 937@end defun
1255 938
1256 This example creates a sparse keymap and makes a number of bindings: 939 Here is an example that creates a sparse keymap and makes a number of
940bindings in it:
1257 941
1258@smallexample 942@smallexample
1259@group 943@group
@@ -1425,8 +1109,33 @@ Dired mode is set up:
1425 This section describes some convenient interactive interfaces for 1109 This section describes some convenient interactive interfaces for
1426changing key bindings. They work by calling @code{define-key}. 1110changing key bindings. They work by calling @code{define-key}.
1427 1111
1112 People often use @code{global-set-key} in their @file{.emacs} file for
1113simple customization. For example,
1114
1115@smallexample
1116(global-set-key "\C-x\C-\\" 'next-line)
1117@end smallexample
1118
1119@noindent
1120or
1121
1122@smallexample
1123(global-set-key [?\C-x ?\C-\\] 'next-line)
1124@end smallexample
1125
1126@noindent
1127redefines @kbd{C-x C-\} to move down a line.
1128
1129@smallexample
1130(global-set-key [M-mouse-1] 'mouse-set-point)
1131@end smallexample
1132
1133@noindent
1134redefines the first (leftmost) mouse button, typed with the Meta key, to
1135set point where you click.
1136
1428@deffn Command global-set-key key definition 1137@deffn Command global-set-key key definition
1429 This function sets the binding of @var{key} in the current global map 1138This function sets the binding of @var{key} in the current global map
1430to @var{definition}. 1139to @var{definition}.
1431 1140
1432@smallexample 1141@smallexample
@@ -1440,11 +1149,11 @@ to @var{definition}.
1440 1149
1441@deffn Command global-unset-key key 1150@deffn Command global-unset-key key
1442@cindex unbinding keys 1151@cindex unbinding keys
1443 This function removes the binding of @var{key} from the current 1152This function removes the binding of @var{key} from the current
1444global map. 1153global map.
1445 1154
1446One use of this function is in preparation for defining a longer key 1155One use of this function is in preparation for defining a longer key
1447which uses it implicitly as a prefix---which would not be allowed if 1156that uses @var{key} as a prefix---which would not be allowed if
1448@var{key} has a non-prefix binding. For example: 1157@var{key} has a non-prefix binding. For example:
1449 1158
1450@smallexample 1159@smallexample
@@ -1470,7 +1179,7 @@ This function is implemented simply using @code{define-key}:
1470@end deffn 1179@end deffn
1471 1180
1472@deffn Command local-set-key key definition 1181@deffn Command local-set-key key definition
1473 This function sets the binding of @var{key} in the current local 1182This function sets the binding of @var{key} in the current local
1474keymap to @var{definition}. 1183keymap to @var{definition}.
1475 1184
1476@smallexample 1185@smallexample
@@ -1483,7 +1192,7 @@ keymap to @var{definition}.
1483@end deffn 1192@end deffn
1484 1193
1485@deffn Command local-unset-key key 1194@deffn Command local-unset-key key
1486 This function removes the binding of @var{key} from the current 1195This function removes the binding of @var{key} from the current
1487local map. 1196local map.
1488 1197
1489@smallexample 1198@smallexample
@@ -1543,9 +1252,10 @@ definition is the sparse keymap @code{(keymap (83 .@: center-paragraph)
1543 1252
1544In the following example, @kbd{C-h} is a prefix key that uses a sparse 1253In the following example, @kbd{C-h} is a prefix key that uses a sparse
1545keymap starting with @code{(keymap (118 . describe-variable)@dots{})}. 1254keymap starting with @code{(keymap (118 . describe-variable)@dots{})}.
1546Another prefix, @kbd{C-x 4}, uses a keymap which happens to be 1255Another prefix, @kbd{C-x 4}, uses a keymap which is also the value of
1547@code{ctl-x-4-map}. The event @code{mode-line} is one of several dummy 1256the variable @code{ctl-x-4-map}. The event @code{mode-line} is one of
1548events used as prefixes for mouse actions in special parts of a window. 1257several dummy events used as prefixes for mouse actions in special parts
1258of a window.
1549 1259
1550@smallexample 1260@smallexample
1551@group 1261@group
@@ -1601,8 +1311,8 @@ entirely of @sc{ASCII} characters (or meta variants of @sc{ASCII}
1601characters) are preferred to all other key sequences. 1311characters) are preferred to all other key sequences.
1602 1312
1603If @var{noindirect} is non-@code{nil}, @code{where-is-internal} doesn't 1313If @var{noindirect} is non-@code{nil}, @code{where-is-internal} doesn't
1604follow indirections to other keymaps or slots. This makes it possible 1314follow indirect keymap bindings. This makes it possible to search for
1605to search for an indirect definition itself. 1315an indirect definition itself.
1606 1316
1607This function is used by @code{where-is} (@pxref{Help, , Help, emacs, 1317This function is used by @code{where-is} (@pxref{Help, , Help, emacs,
1608The GNU Emacs Manual}). 1318The GNU Emacs Manual}).
@@ -1616,10 +1326,13 @@ The GNU Emacs Manual}).
1616@end defun 1326@end defun
1617 1327
1618@deffn Command describe-bindings prefix 1328@deffn Command describe-bindings prefix
1619This function creates a listing of all defined keys, and their 1329This function creates a listing of all defined keys and their
1620definitions. It writes the listing in a buffer named @samp{*Help*} and 1330definitions. It writes the listing in a buffer named @samp{*Help*} and
1621displays it in a window. 1331displays it in a window.
1622 1332
1333If @var{prefix} is non-@code{nil}, it should be a prefix key; then the
1334listing includes only keys that start with @var{prefix}.
1335
1623The listing describes meta characters as @key{ESC} followed by the 1336The listing describes meta characters as @key{ESC} followed by the
1624corresponding non-meta character. 1337corresponding non-meta character.
1625 1338
@@ -1632,7 +1345,319 @@ For example, in the default global map, the characters @samp{@key{SPC}
1632@kbd{~} is @sc{ASCII} 126, and the characters between them include all 1345@kbd{~} is @sc{ASCII} 126, and the characters between them include all
1633the normal printing characters, (e.g., letters, digits, punctuation, 1346the normal printing characters, (e.g., letters, digits, punctuation,
1634etc.@:); all these characters are bound to @code{self-insert-command}. 1347etc.@:); all these characters are bound to @code{self-insert-command}.
1635
1636If @var{prefix} is non-@code{nil}, it should be a prefix key; then the
1637listing includes only keys that start with @var{prefix}.
1638@end deffn 1348@end deffn
1349
1350@node Menu Keymaps
1351@section Menu Keymaps
1352@cindex menu keymaps
1353
1354@c Emacs 19 feature
1355A keymap can define a menu as well as bindings for keyboard keys and
1356mouse button. Menus are usually actuated with the mouse, but they can
1357work with the keyboard also.
1358
1359@menu
1360* Defining Menus:: How to make a keymap that defines a menu.
1361* Mouse Menus:: How users actuate the menu with the mouse.
1362* Keyboard Menus:: How they actuate it with the keyboard.
1363* Menu Example:: Making a simple menu.
1364* Menu Bar:: How to customize the menu bar.
1365* Modifying Menus:: How to add new items to a menu.
1366@end menu
1367
1368@node Defining Menus
1369@subsection Defining Menus
1370@cindex defining menus
1371@cindex menu prompt string
1372@cindex prompt string (of menu)
1373
1374A keymap is suitable for menu use if it has an @dfn{overall prompt
1375string}, which is a string that appears as an element of the keymap.
1376(@xref{Format of Keymaps}.) The string should describe the purpose of
1377the menu. The easiest way to construct a keymap with a prompt string is
1378to specify the string as an argument when you call @code{make-keymap} or
1379@code{make-sparse-keymap} (@pxref{Creating Keymaps}).
1380
1381The individual bindings in the menu keymap should have item
1382strings; these strings become the items displayed in the menu. A
1383binding with an item string looks like this:
1384
1385@example
1386(@var{string} . @var{real-binding})
1387@end example
1388
1389The item string for a binding should be short---one or two words. It
1390should describe the action of the command it corresponds to.
1391
1392As far as @code{define-key} is concerned, @var{string} is part of the
1393event's binding. However, @code{lookup-key} returns just
1394@var{real-binding}, and only @var{real-binding} is used for executing
1395the key.
1396
1397You can also supply a second string, called the help string, as follows:
1398
1399@example
1400(@var{string} @var{help-string} . @var{real-binding})
1401@end example
1402
1403Currently Emacs does not actually use @var{help-string}; it knows only
1404how to ignore @var{help-string} in order to extract @var{real-binding}.
1405In the future we hope to make @var{help-string} serve as extended
1406documentation for the menu item, available on request.
1407
1408If @var{real-binding} is @code{nil}, then @var{string} appears in the
1409menu but cannot be selected.
1410
1411If @var{real-binding} is a symbol and has a non-@code{nil}
1412@code{menu-enable} property, that property is an expression that
1413controls whether the menu item is enabled. Every time the keymap is
1414used to display a menu, Emacs evaluates the expression, and it enables
1415the menu item only if the expression's value is non-@code{nil}. When a
1416menu item is disabled, it is displayed in a ``fuzzy'' fashion, and
1417cannot be selected with the mouse.
1418
1419The order of items in the menu is the same as the order of bindings in
1420the keymap. Since @code{define-key} puts new bindings at the front, you
1421should define the menu items starting at the bottom of the menu and
1422moving to the top, if you care about the order. When you add an item to
1423an existing menu, you can specify its position in the menu using
1424@code{define-key-after} (@pxref{Modifying Menus}).
1425
1426You've probably noticed that menu items show the equivalent keyboard key
1427sequence (if any) to invoke the same command. To save time on
1428recalculation, menu display caches this information in a sublist in the
1429binding, like this:
1430
1431@c This line is not too long--rms.
1432@example
1433(@var{string} @r{[}@var{help-string}@r{]} (@var{key-binding-data}) . @var{real-binding})
1434@end example
1435
1436Don't put these sublists in the menu item yourself; menu display
1437calculates them automatically. Don't add keyboard equivalents to the
1438item strings in a mouse menu, since that is redundant.
1439
1440@node Mouse Menus
1441@subsection Menus and the Mouse
1442
1443The way to make a menu keymap produce a menu is to make it the
1444definition of a prefix key.
1445
1446If the prefix key ends with a mouse event, Emacs handles the menu keymap
1447by popping up a visible menu, so that the user can select a choice with
1448the mouse. When the user clicks on a menu item, the event generated is
1449whatever character or symbol has the binding that brought about that
1450menu item. (A menu item may generate a series of events if the menu has
1451multiple levels or comes from the menu bar.)
1452
1453It's often best to use a button-down event to trigger the menu. Then
1454the user can select a menu item by releasing the button.
1455
1456A single keymap can appear as multiple menu panes, if you explicitly
1457arrange for this. The way to do this is to make a keymap for each pane,
1458then create a binding for each of those maps in the main keymap of the
1459menu. Give each of these bindings an item string that starts with
1460@samp{@@}. The rest of the item string becomes the name of the pane.
1461See the file @file{lisp/mouse.el} for an example of this. Any ordinary
1462bindings with @samp{@@}-less item strings are grouped into one pane,
1463which appears along with the other panes explicitly created for the
1464submaps.
1465
1466X toolkit menus don't have panes; instead, they can have submenus.
1467Every nested keymap becomes a submenu, whether the item string starts
1468with @samp{@@} or not. In a toolkit version of Emacs, the only thing
1469special about @samp{@@} at the beginning of an item string is that the
1470@samp{@@} doesn't appear in the menu item.
1471
1472You can also get multiple panes from separate keymaps. The full
1473definition of a prefix key always comes from merging the definitions
1474supplied by the various active keymaps (minor mode, local, and
1475global). When more than one of these keymaps is a menu, each of them
1476makes a separate pane or panes. @xref{Active Keymaps}.
1477
1478In toolkit versions of Emacs, menus don't have panes, so submenus are
1479used to represent the separate keymaps. Each keymap's contribution
1480becomes one submenu.
1481
1482A Lisp program can explicitly pop up a menu and receive the user's
1483choice. You can use keymaps for this also. @xref{Pop-Up Menus}.
1484
1485@node Keyboard Menus
1486@subsection Menus and the Keyboard
1487
1488When a prefix key ending with a keyboard event (a character or function
1489key) has a definition that is a menu keymap, the user can use the
1490keyboard to choose a menu item.
1491
1492Emacs displays the menu alternatives (the item strings of the bindings)
1493in the echo area. If they don't all fit at once, the user can type
1494@key{SPC} to see the next line of alternatives. Successive uses of
1495@key{SPC} eventually get to the end of the menu and then cycle around to
1496the beginning. (The variable @code{menu-prompt-more-char} specifies
1497which character is used for this; @key{SPC} is the default.)
1498
1499When the user has found the desired alternative from the menu, he or she
1500should type the corresponding character---the one whose binding is that
1501alternative.
1502
1503In a menu intended for keyboard use, each menu item must clearly
1504indicate what character to type. The best convention to use is to make
1505the character the first letter of the item string. That is something
1506users will understand without being told.
1507
1508This way of using menus in an Emacs-like editor was inspired by the
1509Hierarkey system.
1510
1511@defvar menu-prompt-more-char
1512This variable specifies the character to use to ask to see
1513the next line of a menu. Its initial value is 32, the code
1514for @key{SPC}.
1515@end defvar
1516
1517@node Menu Example
1518@subsection Menu Example
1519
1520 Here is a simple example of how to set up a menu for mouse use.
1521
1522@example
1523(defvar my-menu-map
1524 (make-sparse-keymap "Key Commands <==> Functions"))
1525(fset 'help-for-keys my-menu-map)
1526
1527(define-key my-menu-map [bindings]
1528 '("List all keystroke commands" . describe-bindings))
1529(define-key my-menu-map [key]
1530 '("Describe key briefly" . describe-key-briefly))
1531(define-key my-menu-map [key-verbose]
1532 '("Describe key verbose" . describe-key))
1533(define-key my-menu-map [function]
1534 '("Describe Lisp function" . describe-function))
1535(define-key my-menu-map [where-is]
1536 '("Where is this command" . where-is))
1537
1538(define-key global-map [C-S-down-mouse-1] 'help-for-keys)
1539@end example
1540
1541 The symbols used in the key sequences bound in the menu are fictitious
1542``function keys''; they don't appear on the keyboard, but that doesn't
1543stop you from using them in the menu. Their names were chosen to be
1544mnemonic, because they show up in the output of @code{where-is} and
1545@code{apropos} to identify the corresponding menu items.
1546
1547 However, if you want the menu to be usable from the keyboard as well,
1548you must bind real @sc{ASCII} characters as well as fictitious function
1549keys.
1550
1551@node Menu Bar
1552@subsection The Menu Bar
1553@cindex menu bar
1554
1555 Most window systems allow each frame to have a @dfn{menu bar}---a
1556permanently displayed menu stretching horizontally across the top of the
1557frame. The items of the menu bar are the subcommands of the fake
1558``function key'' @code{menu-bar}, as defined by all the active keymaps.
1559
1560 To add an item to the menu bar, invent a fake ``function key'' of your
1561own (let's call it @var{key}), and make a binding for the key sequence
1562@code{[menu-bar @var{key}]}. Most often, the binding is a menu keymap,
1563so that pressing a button on the menu bar item leads to another menu.
1564
1565 When more than one active keymap defines the same fake function key
1566for the menu bar, the item appears just once. If the user clicks on
1567that menu bar item, it brings up a single, combined submenu containing
1568all the subcommands of that item---the global subcommands, the local
1569subcommands, and the minor mode subcommands, all together.
1570
1571 In order for a frame to display a menu bar, its @code{menu-bar-lines}
1572parameter must be greater than zero. Emacs uses just one line for the
1573menu bar itself; if you specify more than one line, the other lines
1574serve to separate the menu bar from the windows in the frame. We
1575recommend you try 1 or 2 as the value of @code{menu-bar-lines}. @xref{X
1576Frame Parameters}.
1577
1578 Here's an example of setting up a menu bar item:
1579
1580@example
1581@group
1582(modify-frame-parameters (selected-frame)
1583 '((menu-bar-lines . 2)))
1584@end group
1585
1586@group
1587;; @r{Make a menu keymap (with a prompt string)}
1588;; @r{and make it the menu bar item's definition.}
1589(define-key global-map [menu-bar words]
1590 (cons "Words" (make-sparse-keymap "Words")))
1591@end group
1592
1593@group
1594;; @r{Define specific subcommands in the item's menu.}
1595(define-key global-map
1596 [menu-bar words forward]
1597 '("Forward word" . forward-word))
1598@end group
1599@group
1600(define-key global-map
1601 [menu-bar words backward]
1602 '("Backward word" . backward-word))
1603@end group
1604@end example
1605
1606 A local keymap can cancel a menu bar item made by the global keymap by
1607rebinding the same fake function key with @code{undefined} as the
1608binding. For example, this is how Dired suppresses the @samp{Edit} menu
1609bar item:
1610
1611@example
1612(define-key dired-mode-map [menu-bar edit] 'undefined)
1613@end example
1614
1615@noindent
1616@code{edit} is the fake function key used by the global map for the
1617@samp{Edit} menu bar item. The main reason to suppress a global
1618menu bar item is to regain space for mode-specific items.
1619
1620@defvar menu-bar-final-items
1621Normally the menu bar shows global items followed by items defined by the
1622local maps.
1623
1624This variable holds a list of fake function keys for items to display at
1625the end of the menu bar rather than in normal sequence. The default
1626value is @code{(help)}; thus, the @samp{Help} menu item normally appears
1627at the end of the menu bar, following local menu items.
1628@end defvar
1629
1630@node Modifying Menus
1631@subsection Modifying Menus
1632
1633 When you insert a new item in an existing menu, you probably want to
1634put it in a particular place among the menu's existing items. If you
1635use @code{define-key} to add the item, it normally goes at the front of
1636the menu. To put it elsewhere, use @code{define-key-after}:
1637
1638@defun define-key-after map key binding after
1639Define a binding in @var{map} for @var{key}, with value @var{binding},
1640just like @code{define-key}, but position the binding in @var{map} after
1641the binding for the event @var{after}. For example,
1642
1643@example
1644(define-key-after my-menu [drink]
1645 '("Drink" . drink-command) 'eat)
1646@end example
1647
1648@noindent
1649makes a binding for the fake function key @key{drink} and puts it
1650right after the binding for @key{eat}.
1651
1652Here is how to insert an item called @samp{Work} in the @samp{Signals}
1653menu of Shell mode, after the item @code{break}:
1654
1655@example
1656(define-key-after
1657 (lookup-key shell-mode-map [menu-bar signals])
1658 [work] '("Work" . work-command) 'break)
1659@end example
1660
1661Note that @var{key} is a sequence containing just one event type, but
1662@var{after} is just an event type (not a sequence).
1663@end defun