forked from emacs-mirror/emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.texi
3701 lines (3079 loc) · 142 KB
/
commands.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
@c Copyright (C) 1990--1995, 1998--1999, 2001--2021 Free Software
@c Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@node Command Loop
@chapter Command Loop
@cindex editor command loop
@cindex command loop
When you run Emacs, it enters the @dfn{editor command loop} almost
immediately. This loop reads key sequences, executes their definitions,
and displays the results. In this chapter, we describe how these things
are done, and the subroutines that allow Lisp programs to do them.
@menu
* Command Overview:: How the command loop reads commands.
* Defining Commands:: Specifying how a function should read arguments.
* Interactive Call:: Calling a command, so that it will read arguments.
* Distinguish Interactive:: Making a command distinguish interactive calls.
* Command Loop Info:: Variables set by the command loop for you to examine.
* Adjusting Point:: Adjustment of point after a command.
* Input Events:: What input looks like when you read it.
* Reading Input:: How to read input events from the keyboard or mouse.
* Special Events:: Events processed immediately and individually.
* Waiting:: Waiting for user input or elapsed time.
* Quitting:: How @kbd{C-g} works. How to catch or defer quitting.
* Prefix Command Arguments:: How the commands to set prefix args work.
* Recursive Editing:: Entering a recursive edit,
and why you usually shouldn't.
* Disabling Commands:: How the command loop handles disabled commands.
* Command History:: How the command history is set up, and how accessed.
* Keyboard Macros:: How keyboard macros are implemented.
@end menu
@node Command Overview
@section Command Loop Overview
The first thing the command loop must do is read a key sequence,
which is a sequence of input events that translates into a command.
It does this by calling the function @code{read-key-sequence}. Lisp
programs can also call this function (@pxref{Key Sequence Input}).
They can also read input at a lower level with @code{read-key} or
@code{read-event} (@pxref{Reading One Event}), or discard pending
input with @code{discard-input} (@pxref{Event Input Misc}).
The key sequence is translated into a command through the currently
active keymaps. @xref{Key Lookup}, for information on how this is done.
The result should be a keyboard macro or an interactively callable
function. If the key is @kbd{M-x}, then it reads the name of another
command, which it then calls. This is done by the command
@code{execute-extended-command} (@pxref{Interactive Call}).
Prior to executing the command, Emacs runs @code{undo-boundary} to
create an undo boundary. @xref{Maintaining Undo}.
To execute a command, Emacs first reads its arguments by calling
@code{command-execute} (@pxref{Interactive Call}). For commands
written in Lisp, the @code{interactive} specification says how to read
the arguments. This may use the prefix argument (@pxref{Prefix
Command Arguments}) or may read with prompting in the minibuffer
(@pxref{Minibuffers}). For example, the command @code{find-file} has
an @code{interactive} specification which says to read a file name
using the minibuffer. The function body of @code{find-file} does not
use the minibuffer, so if you call @code{find-file} as a function from
Lisp code, you must supply the file name string as an ordinary Lisp
function argument.
If the command is a keyboard macro (i.e., a string or vector),
Emacs executes it using @code{execute-kbd-macro} (@pxref{Keyboard
Macros}).
@defvar pre-command-hook
This normal hook is run by the editor command loop before it executes
each command. At that time, @code{this-command} contains the command
that is about to run, and @code{last-command} describes the previous
command. @xref{Command Loop Info}.
@end defvar
@defvar post-command-hook
This normal hook is run by the editor command loop after it executes
each command (including commands terminated prematurely by quitting or
by errors). At that time, @code{this-command} refers to the command
that just ran, and @code{last-command} refers to the command before
that.
This hook is also run when Emacs first enters the command loop (at
which point @code{this-command} and @code{last-command} are both
@code{nil}).
@end defvar
Quitting is suppressed while running @code{pre-command-hook} and
@code{post-command-hook}. If an error happens while executing one of
these hooks, it does not terminate execution of the hook; instead
the error is silenced and the function in which the error occurred
is removed from the hook.
A request coming into the Emacs server (@pxref{Emacs Server,,,
emacs, The GNU Emacs Manual}) runs these two hooks just as a keyboard
command does.
@node Defining Commands
@section Defining Commands
@cindex defining commands
@cindex commands, defining
@cindex functions, making them interactive
@cindex interactive function
The special form @code{interactive} turns a Lisp function into a
command. The @code{interactive} form must be located at top-level in
the function body, usually as the first form in the body; this applies
to both lambda expressions (@pxref{Lambda Expressions}) and
@code{defun} forms (@pxref{Defining Functions}). This form does
nothing during the actual execution of the function; its presence
serves as a flag, telling the Emacs command loop that the function can
be called interactively. The argument of the @code{interactive} form
specifies how the arguments for an interactive call should be read.
@cindex @code{interactive-form} property
Alternatively, an @code{interactive} form may be specified in a
function symbol's @code{interactive-form} property. A non-@code{nil}
value for this property takes precedence over any @code{interactive}
form in the function body itself. This feature is seldom used.
@anchor{The interactive-only property}
@cindex @code{interactive-only} property
Sometimes, a function is only intended to be called interactively,
never directly from Lisp. In that case, give the function a
non-@code{nil} @code{interactive-only} property, either directly
or via @code{declare} (@pxref{Declare Form}). This causes the
byte compiler to warn if the command is called from Lisp. The output
of @code{describe-function} will include similar information.
The value of the property can be: a string, which the byte-compiler
will use directly in its warning (it should end with a period, and not
start with a capital, e.g., @code{"use (system-name) instead."}); @code{t}; any
other symbol, which should be an alternative function to use in Lisp
code.
Generic functions (@pxref{Generic Functions}) cannot be turned into
commands by adding the @code{interactive} form to them.
@menu
* Using Interactive:: General rules for @code{interactive}.
* Interactive Codes:: The standard letter-codes for reading arguments
in various ways.
* Interactive Examples:: Examples of how to read interactive arguments.
* Generic Commands:: Select among command alternatives.
@end menu
@node Using Interactive
@subsection Using @code{interactive}
@cindex arguments, interactive entry
@cindex interactive spec, using
This section describes how to write the @code{interactive} form that
makes a Lisp function an interactively-callable command, and how to
examine a command's @code{interactive} form.
@defspec interactive arg-descriptor
This special form declares that a function is a command, and that it
may therefore be called interactively (via @kbd{M-x} or by entering a
key sequence bound to it). The argument @var{arg-descriptor} declares
how to compute the arguments to the command when the command is called
interactively.
A command may be called from Lisp programs like any other function, but
then the caller supplies the arguments and @var{arg-descriptor} has no
effect.
@cindex @code{interactive-form}, symbol property
The @code{interactive} form must be located at top-level in the
function body, or in the function symbol's @code{interactive-form}
property (@pxref{Symbol Properties}). It has its effect because the
command loop looks for it before calling the function
(@pxref{Interactive Call}). Once the function is called, all its body
forms are executed; at this time, if the @code{interactive} form
occurs within the body, the form simply returns @code{nil} without
even evaluating its argument.
By convention, you should put the @code{interactive} form in the
function body, as the first top-level form. If there is an
@code{interactive} form in both the @code{interactive-form} symbol
property and the function body, the former takes precedence. The
@code{interactive-form} symbol property can be used to add an
interactive form to an existing function, or change how its arguments
are processed interactively, without redefining the function.
@end defspec
There are three possibilities for the argument @var{arg-descriptor}:
@itemize @bullet
@item
It may be omitted or @code{nil}; then the command is called with no
arguments. This leads quickly to an error if the command requires one
or more arguments.
@item
It may be a string; its contents are a sequence of elements separated
by newlines, one for each argument@footnote{Some elements actually
supply two arguments.}. Each element consists of a code character
(@pxref{Interactive Codes}) optionally followed by a prompt (which
some code characters use and some ignore). Here is an example:
@smallexample
(interactive "P\nbFrobnicate buffer: ")
@end smallexample
@noindent
The code letter @samp{P} sets the command's first argument to the raw
command prefix (@pxref{Prefix Command Arguments}). @samp{bFrobnicate
buffer: } prompts the user with @samp{Frobnicate buffer: } to enter
the name of an existing buffer, which becomes the second and final
argument.
The prompt string can use @samp{%} to include previous argument values
(starting with the first argument) in the prompt. This is done using
@code{format-message} (@pxref{Formatting Strings}). For example, here is how
you could read the name of an existing buffer followed by a new name to
give to that buffer:
@smallexample
@group
(interactive "bBuffer to rename: \nsRename buffer %s to: ")
@end group
@end smallexample
@cindex @samp{*} in @code{interactive}
@cindex read-only buffers in interactive
If @samp{*} appears at the beginning of the string, then an error is
signaled if the buffer is read-only.
@cindex @samp{@@} in @code{interactive}
If @samp{@@} appears at the beginning of the string, and if the key
sequence used to invoke the command includes any mouse events, then
the window associated with the first of those events is selected
before the command is run.
@cindex @samp{^} in @code{interactive}
@cindex shift-selection, and @code{interactive} spec
If @samp{^} appears at the beginning of the string, and if the command
was invoked through @dfn{shift-translation}, set the mark and activate
the region temporarily, or extend an already active region, before the
command is run. If the command was invoked without shift-translation,
and the region is temporarily active, deactivate the region before the
command is run. Shift-translation is controlled on the user level by
@code{shift-select-mode}; see @ref{Shift Selection,,, emacs, The GNU
Emacs Manual}.
You can use @samp{*}, @samp{@@}, and @code{^} together; the order does
not matter. Actual reading of arguments is controlled by the rest of
the prompt string (starting with the first character that is not
@samp{*}, @samp{@@}, or @samp{^}).
@item
It may be a Lisp expression that is not a string; then it should be a
form that is evaluated to get a list of arguments to pass to the
command. Usually this form will call various functions to read input
from the user, most often through the minibuffer (@pxref{Minibuffers})
or directly from the keyboard (@pxref{Reading Input}).
Providing point or the mark as an argument value is also common, but
if you do this @emph{and} read input (whether using the minibuffer or
not), be sure to get the integer values of point or the mark after
reading. The current buffer may be receiving subprocess output; if
subprocess output arrives while the command is waiting for input, it
could relocate point and the mark.
Here's an example of what @emph{not} to do:
@smallexample
(interactive
(list (region-beginning) (region-end)
(read-string "Foo: " nil 'my-history)))
@end smallexample
@noindent
Here's how to avoid the problem, by examining point and the mark after
reading the keyboard input:
@smallexample
(interactive
(let ((string (read-string "Foo: " nil 'my-history)))
(list (region-beginning) (region-end) string)))
@end smallexample
@strong{Warning:} the argument values should not include any data
types that can't be printed and then read. Some facilities save
@code{command-history} in a file to be read in the subsequent
sessions; if a command's arguments contain a data type that prints
using @samp{#<@dots{}>} syntax, those facilities won't work.
There are, however, a few exceptions: it is ok to use a limited set of
expressions such as @code{(point)}, @code{(mark)},
@code{(region-beginning)}, and @code{(region-end)}, because Emacs
recognizes them specially and puts the expression (rather than its
value) into the command history. To see whether the expression you
wrote is one of these exceptions, run the command, then examine
@code{(car command-history)}.
@end itemize
@cindex examining the @code{interactive} form
@defun interactive-form function
This function returns the @code{interactive} form of @var{function}.
If @var{function} is an interactively callable function
(@pxref{Interactive Call}), the value is the command's
@code{interactive} form @code{(interactive @var{spec})}, which
specifies how to compute its arguments. Otherwise, the value is
@code{nil}. If @var{function} is a symbol, its function definition is
used.
@end defun
@node Interactive Codes
@subsection Code Characters for @code{interactive}
@cindex interactive code description
@cindex description for interactive codes
@cindex codes, interactive, description of
@cindex characters for interactive codes
The code character descriptions below contain a number of key words,
defined here as follows:
@table @b
@item Completion
@cindex interactive completion
Provide completion. @key{TAB}, @key{SPC}, and @key{RET} perform name
completion because the argument is read using @code{completing-read}
(@pxref{Completion}). @kbd{?} displays a list of possible completions.
@item Existing
Require the name of an existing object. An invalid name is not
accepted; the commands to exit the minibuffer do not exit if the current
input is not valid.
@item Default
@cindex default argument string
A default value of some sort is used if the user enters no text in the
minibuffer. The default depends on the code character.
@item No I/O
This code letter computes an argument without reading any input.
Therefore, it does not use a prompt string, and any prompt string you
supply is ignored.
Even though the code letter doesn't use a prompt string, you must follow
it with a newline if it is not the last code character in the string.
@item Prompt
A prompt immediately follows the code character. The prompt ends either
with the end of the string or with a newline.
@item Special
This code character is meaningful only at the beginning of the
interactive string, and it does not look for a prompt or a newline.
It is a single, isolated character.
@end table
@cindex reading interactive arguments
Here are the code character descriptions for use with @code{interactive}:
@table @samp
@item *
Signal an error if the current buffer is read-only. Special.
@item @@
Select the window mentioned in the first mouse event in the key
sequence that invoked this command. Special.
@item ^
If the command was invoked through shift-translation, set the mark and
activate the region temporarily, or extend an already active region,
before the command is run. If the command was invoked without
shift-translation, and the region is temporarily active, deactivate
the region before the command is run. Special.
@item a
A function name (i.e., a symbol satisfying @code{fboundp}). Existing,
Completion, Prompt.
@item b
The name of an existing buffer. By default, uses the name of the
current buffer (@pxref{Buffers}). Existing, Completion, Default,
Prompt.
@item B
A buffer name. The buffer need not exist. By default, uses the name of
a recently used buffer other than the current buffer. Completion,
Default, Prompt.
@item c
A character. The cursor does not move into the echo area. Prompt.
@item C
A command name (i.e., a symbol satisfying @code{commandp}). Existing,
Completion, Prompt.
@item d
@cindex position argument
The position of point, as an integer (@pxref{Point}). No I/O.
@item D
A directory. The default is the current default directory of the
current buffer, @code{default-directory} (@pxref{File Name Expansion}).
Existing, Completion, Default, Prompt.
@item e
The first or next non-keyboard event in the key sequence that invoked
the command. More precisely, @samp{e} gets events that are lists, so
you can look at the data in the lists. @xref{Input Events}. No I/O.
You use @samp{e} for mouse events and for special system events
(@pxref{Misc Events}). The event list that the command receives
depends on the event. @xref{Input Events}, which describes the forms
of the list for each event in the corresponding subsections.
You can use @samp{e} more than once in a single command's interactive
specification. If the key sequence that invoked the command has
@var{n} events that are lists, the @var{n}th @samp{e} provides the
@var{n}th such event. Events that are not lists, such as function keys
and @acronym{ASCII} characters, do not count where @samp{e} is concerned.
@item f
A file name of an existing file (@pxref{File Names}). The default
directory is @code{default-directory}. Existing, Completion, Default,
Prompt.
@item F
A file name. The file need not exist. Completion, Default, Prompt.
@item G
A file name. The file need not exist. If the user enters just a
directory name, then the value is just that directory name, with no
file name within the directory added. Completion, Default, Prompt.
@item i
An irrelevant argument. This code always supplies @code{nil} as
the argument's value. No I/O.
@item k
A key sequence (@pxref{Key Sequences}). This keeps reading events
until a command (or undefined command) is found in the current key
maps. The key sequence argument is represented as a string or vector.
The cursor does not move into the echo area. Prompt.
If @samp{k} reads a key sequence that ends with a down-event, it also
reads and discards the following up-event. You can get access to that
up-event with the @samp{U} code character.
This kind of input is used by commands such as @code{describe-key} and
@code{global-set-key}.
@item K
A key sequence, whose definition you intend to change. This works like
@samp{k}, except that it suppresses, for the last input event in the key
sequence, the conversions that are normally used (when necessary) to
convert an undefined key into a defined one.
@item m
@cindex marker argument
The position of the mark, as an integer. No I/O.
@item M
Arbitrary text, read in the minibuffer using the current buffer's input
method, and returned as a string (@pxref{Input Methods,,, emacs, The GNU
Emacs Manual}). Prompt.
@item n
A number, read with the minibuffer. If the input is not a number, the
user has to try again. @samp{n} never uses the prefix argument.
Prompt.
@item N
The numeric prefix argument; but if there is no prefix argument, read
a number as with @kbd{n}. The value is always a number. @xref{Prefix
Command Arguments}. Prompt.
@item p
@cindex numeric prefix argument usage
The numeric prefix argument. (Note that this @samp{p} is lower case.)
No I/O.
@item P
@cindex raw prefix argument usage
The raw prefix argument. (Note that this @samp{P} is upper case.) No
I/O.
@item r
@cindex region argument
Point and the mark, as two numeric arguments, smallest first. This is
the only code letter that specifies two successive arguments rather than
one. This will signal an error if the mark is not set in the buffer
which is current when the command is invoked. No I/O.
@item s
Arbitrary text, read in the minibuffer and returned as a string
(@pxref{Text from Minibuffer}). Terminate the input with either
@kbd{C-j} or @key{RET}. (@kbd{C-q} may be used to include either of
these characters in the input.) Prompt.
@item S
An interned symbol whose name is read in the minibuffer. Terminate
the input with either @kbd{C-j} or @key{RET}. Other characters that
normally terminate a symbol (e.g., whitespace, parentheses and
brackets) do not do so here. Prompt.
@item U
A key sequence or @code{nil}. Can be used after a @samp{k} or
@samp{K} argument to get the up-event that was discarded (if any)
after @samp{k} or @samp{K} read a down-event. If no up-event has been
discarded, @samp{U} provides @code{nil} as the argument. No I/O.
@item v
A variable declared to be a user option (i.e., satisfying the
predicate @code{custom-variable-p}). This reads the variable using
@code{read-variable}. @xref{Definition of read-variable}. Existing,
Completion, Prompt.
@item x
A Lisp object, specified with its read syntax, terminated with a
@kbd{C-j} or @key{RET}. The object is not evaluated. @xref{Object from
Minibuffer}. Prompt.
@item X
@cindex evaluated expression argument
A Lisp form's value. @samp{X} reads as @samp{x} does, then evaluates
the form so that its value becomes the argument for the command.
Prompt.
@item z
A coding system name (a symbol). If the user enters null input, the
argument value is @code{nil}. @xref{Coding Systems}. Completion,
Existing, Prompt.
@item Z
A coding system name (a symbol)---but only if this command has a prefix
argument. With no prefix argument, @samp{Z} provides @code{nil} as the
argument value. Completion, Existing, Prompt.
@end table
@node Interactive Examples
@subsection Examples of Using @code{interactive}
@cindex examples of using @code{interactive}
@cindex @code{interactive}, examples of using
Here are some examples of @code{interactive}:
@example
@group
(defun foo1 () ; @r{@code{foo1} takes no arguments,}
(interactive) ; @r{just moves forward two words.}
(forward-word 2))
@result{} foo1
@end group
@group
(defun foo2 (n) ; @r{@code{foo2} takes one argument,}
(interactive "^p") ; @r{which is the numeric prefix.}
; @r{under @code{shift-select-mode},}
; @r{will activate or extend region.}
(forward-word (* 2 n)))
@result{} foo2
@end group
@group
(defun foo3 (n) ; @r{@code{foo3} takes one argument,}
(interactive "nCount:") ; @r{which is read with the Minibuffer.}
(forward-word (* 2 n)))
@result{} foo3
@end group
@group
(defun three-b (b1 b2 b3)
"Select three existing buffers.
Put them into three windows, selecting the last one."
@end group
(interactive "bBuffer1:\nbBuffer2:\nbBuffer3:")
(delete-other-windows)
(split-window (selected-window) 8)
(switch-to-buffer b1)
(other-window 1)
(split-window (selected-window) 8)
(switch-to-buffer b2)
(other-window 1)
(switch-to-buffer b3))
@result{} three-b
@group
(three-b "*scratch*" "declarations.texi" "*mail*")
@result{} nil
@end group
@end example
@node Generic Commands
@subsection Select among Command Alternatives
@cindex generic commands
@cindex alternatives, defining
The macro @code{define-alternatives} can be used to define
@dfn{generic commands}. These are interactive functions whose
implementation can be selected from several alternatives, as a matter
of user preference.
@defmac define-alternatives command &rest customizations
Define the new command @var{command}, a symbol.
When a user runs @kbd{M-x @var{command} @key{RET}} for the first time,
Emacs prompts for which real form of the command to use, and records
the selection by way of a custom variable. Using a prefix argument
repeats this process of choosing an alternative.
The variable @code{@var{command}-alternatives} should contain an alist
with alternative implementations of @var{command}.
Until this variable is set, @code{define-alternatives} has no effect.
If @var{customizations} is non-@code{nil}, it should consist of
alternating @code{defcustom} keywords (typically @code{:group} and
@code{:version}) and values to add to the declaration of
@code{@var{command}-alternatives}.
@end defmac
@node Interactive Call
@section Interactive Call
@cindex interactive call
After the command loop has translated a key sequence into a command,
it invokes that command using the function @code{command-execute}. If
the command is a function, @code{command-execute} calls
@code{call-interactively}, which reads the arguments and calls the
command. You can also call these functions yourself.
Note that the term ``command'', in this context, refers to an
interactively callable function (or function-like object), or a
keyboard macro. It does not refer to the key sequence used to invoke
a command (@pxref{Keymaps}).
@defun commandp object &optional for-call-interactively
This function returns @code{t} if @var{object} is a command.
Otherwise, it returns @code{nil}.
Commands include strings and vectors (which are treated as keyboard
macros), lambda expressions that contain a top-level
@code{interactive} form (@pxref{Using Interactive}), byte-code
function objects made from such lambda expressions, autoload objects
that are declared as interactive (non-@code{nil} fourth argument to
@code{autoload}), and some primitive functions. Also, a symbol is
considered a command if it has a non-@code{nil}
@code{interactive-form} property, or if its function definition
satisfies @code{commandp}.
If @var{for-call-interactively} is non-@code{nil}, then
@code{commandp} returns @code{t} only for objects that
@code{call-interactively} could call---thus, not for keyboard macros.
See @code{documentation} in @ref{Accessing Documentation}, for a
realistic example of using @code{commandp}.
@end defun
@defun call-interactively command &optional record-flag keys
This function calls the interactively callable function @var{command},
providing arguments according to its interactive calling specifications.
It returns whatever @var{command} returns.
If, for instance, you have a function with the following signature:
@example
(defun foo (begin end)
(interactive "r")
...)
@end example
then saying
@example
(call-interactively 'foo)
@end example
will call @code{foo} with the region (@code{point} and @code{mark}) as
the arguments.
An error is signaled if @var{command} is not a function or if it
cannot be called interactively (i.e., is not a command). Note that
keyboard macros (strings and vectors) are not accepted, even though
they are considered commands, because they are not functions. If
@var{command} is a symbol, then @code{call-interactively} uses its
function definition.
@cindex record command history
If @var{record-flag} is non-@code{nil}, then this command and its
arguments are unconditionally added to the list @code{command-history}.
Otherwise, the command is added only if it uses the minibuffer to read
an argument. @xref{Command History}.
The argument @var{keys}, if given, should be a vector which specifies
the sequence of events to supply if the command inquires which events
were used to invoke it. If @var{keys} is omitted or @code{nil}, the
default is the return value of @code{this-command-keys-vector}.
@xref{Definition of this-command-keys-vector}.
@end defun
@defun funcall-interactively function &rest arguments
This function works like @code{funcall} (@pxref{Calling Functions}),
but it makes the call look like an interactive invocation: a call to
@code{called-interactively-p} inside @var{function} will return
@code{t}. If @var{function} is not a command, it is called without
signaling an error.
@end defun
@defun command-execute command &optional record-flag keys special
@cindex keyboard macro execution
This function executes @var{command}. The argument @var{command} must
satisfy the @code{commandp} predicate; i.e., it must be an interactively
callable function or a keyboard macro.
A string or vector as @var{command} is executed with
@code{execute-kbd-macro}. A function is passed to
@code{call-interactively} (see above), along with the
@var{record-flag} and @var{keys} arguments.
If @var{command} is a symbol, its function definition is used in its
place. A symbol with an @code{autoload} definition counts as a
command if it was declared to stand for an interactively callable
function. Such a definition is handled by loading the specified
library and then rechecking the definition of the symbol.
The argument @var{special}, if given, means to ignore the prefix
argument and not clear it. This is used for executing special events
(@pxref{Special Events}).
@end defun
@deffn Command execute-extended-command prefix-argument
@cindex read command name
This function reads a command name from the minibuffer using
@code{completing-read} (@pxref{Completion}). Then it uses
@code{command-execute} to call the specified command. Whatever that
command returns becomes the value of @code{execute-extended-command}.
@cindex execute with prefix argument
If the command asks for a prefix argument, it receives the value
@var{prefix-argument}. If @code{execute-extended-command} is called
interactively, the current raw prefix argument is used for
@var{prefix-argument}, and thus passed on to whatever command is run.
@c !!! Should this be @kindex?
@cindex @kbd{M-x}
@code{execute-extended-command} is the normal definition of @kbd{M-x},
so it uses the string @w{@samp{M-x }} as a prompt. (It would be better
to take the prompt from the events used to invoke
@code{execute-extended-command}, but that is painful to implement.) A
description of the value of the prefix argument, if any, also becomes
part of the prompt.
@example
@group
(execute-extended-command 3)
---------- Buffer: Minibuffer ----------
3 M-x forward-word @key{RET}
---------- Buffer: Minibuffer ----------
@result{} t
@end group
@end example
@end deffn
@node Distinguish Interactive
@section Distinguish Interactive Calls
@cindex distinguish interactive calls
@cindex is this call interactive
Sometimes a command should display additional visual feedback (such
as an informative message in the echo area) for interactive calls
only. There are three ways to do this. The recommended way to test
whether the function was called using @code{call-interactively} is to
give it an optional argument @code{print-message} and use the
@code{interactive} spec to make it non-@code{nil} in interactive
calls. Here's an example:
@example
(defun foo (&optional print-message)
(interactive "p")
(when print-message
(message "foo")))
@end example
@noindent
We use @code{"p"} because the numeric prefix argument is never
@code{nil}. Defined in this way, the function does display the
message when called from a keyboard macro.
The above method with the additional argument is usually best,
because it allows callers to say ``treat this call as interactive''.
But you can also do the job by testing @code{called-interactively-p}.
@defun called-interactively-p kind
This function returns @code{t} when the calling function was called
using @code{call-interactively}.
The argument @var{kind} should be either the symbol @code{interactive}
or the symbol @code{any}. If it is @code{interactive}, then
@code{called-interactively-p} returns @code{t} only if the call was
made directly by the user---e.g., if the user typed a key sequence
bound to the calling function, but @emph{not} if the user ran a
keyboard macro that called the function (@pxref{Keyboard Macros}). If
@var{kind} is @code{any}, @code{called-interactively-p} returns
@code{t} for any kind of interactive call, including keyboard macros.
If in doubt, use @code{any}; the only known proper use of
@code{interactive} is if you need to decide whether to display a
helpful message while a function is running.
A function is never considered to be called interactively if it was
called via Lisp evaluation (or with @code{apply} or @code{funcall}).
@end defun
@noindent
Here is an example of using @code{called-interactively-p}:
@example
@group
(defun foo ()
(interactive)
(when (called-interactively-p 'any)
(message "Interactive!")
'foo-called-interactively))
@end group
@group
;; @r{Type @kbd{M-x foo}.}
@print{} Interactive!
@end group
@group
(foo)
@result{} nil
@end group
@end example
@noindent
Here is another example that contrasts direct and indirect calls to
@code{called-interactively-p}.
@example
@group
(defun bar ()
(interactive)
(message "%s" (list (foo) (called-interactively-p 'any))))
@end group
@group
;; @r{Type @kbd{M-x bar}.}
@print{} (nil t)
@end group
@end example
@node Command Loop Info
@section Information from the Command Loop
@cindex command loop variables
The editor command loop sets several Lisp variables to keep status
records for itself and for commands that are run. With the exception of
@code{this-command} and @code{last-command} it's generally a bad idea to
change any of these variables in a Lisp program.
@defvar last-command
This variable records the name of the previous command executed by the
command loop (the one before the current command). Normally the value
is a symbol with a function definition, but this is not guaranteed.
The value is copied from @code{this-command} when a command returns to
the command loop, except when the command has specified a prefix
argument for the following command.
This variable is always local to the current terminal and cannot be
buffer-local. @xref{Multiple Terminals}.
@end defvar
@defvar real-last-command
This variable is set up by Emacs just like @code{last-command},
but never altered by Lisp programs.
@end defvar
@defvar last-repeatable-command
This variable stores the most recently executed command that was not
part of an input event. This is the command @code{repeat} will try to
repeat, @xref{Repeating,,, emacs, The GNU Emacs Manual}.
@end defvar
@defvar this-command
@cindex current command
This variable records the name of the command now being executed by
the editor command loop. Like @code{last-command}, it is normally a symbol
with a function definition.
The command loop sets this variable just before running a command, and
copies its value into @code{last-command} when the command finishes
(unless the command specified a prefix argument for the following
command).
@cindex kill command repetition
Some commands set this variable during their execution, as a flag for
whatever command runs next. In particular, the functions for killing text
set @code{this-command} to @code{kill-region} so that any kill commands
immediately following will know to append the killed text to the
previous kill.
@end defvar
If you do not want a particular command to be recognized as the previous
command in the case where it got an error, you must code that command to
prevent this. One way is to set @code{this-command} to @code{t} at the
beginning of the command, and set @code{this-command} back to its proper
value at the end, like this:
@example
(defun foo (args@dots{})
(interactive @dots{})
(let ((old-this-command this-command))
(setq this-command t)
@r{@dots{}do the work@dots{}}
(setq this-command old-this-command)))
@end example
@noindent
We do not bind @code{this-command} with @code{let} because that would
restore the old value in case of error---a feature of @code{let} which
in this case does precisely what we want to avoid.
@defvar this-original-command
This has the same value as @code{this-command} except when command
remapping occurs (@pxref{Remapping Commands}). In that case,
@code{this-command} gives the command actually run (the result of
remapping), and @code{this-original-command} gives the command that
was specified to run but remapped into another command.
@end defvar
@defvar current-minibuffer-command
This has the same value as @code{this-command}, but is bound
recursively when entering a minibuffer. This variable can be used
from minibuffer hooks and the like to determine what command opened
the current minibuffer session.
@end defvar
@defun this-command-keys
This function returns a string or vector containing the key sequence
that invoked the present command. Any events read by the command
using @code{read-event} without a timeout get tacked on to the end.
However, if the command has called @code{read-key-sequence}, it
returns the last read key sequence. @xref{Key Sequence Input}. The
value is a string if all events in the sequence were characters that
fit in a string. @xref{Input Events}.
@example
@group
(this-command-keys)
;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
@result{} "^X^E"
@end group
@end example
@end defun
@defun this-command-keys-vector
@anchor{Definition of this-command-keys-vector}
Like @code{this-command-keys}, except that it always returns the events
in a vector, so you don't need to deal with the complexities of storing
input events in a string (@pxref{Strings of Events}).
@end defun
@defun clear-this-command-keys &optional keep-record
This function empties out the table of events for
@code{this-command-keys} to return. Unless @var{keep-record} is
non-@code{nil}, it also empties the records that the function
@code{recent-keys} (@pxref{Recording Input}) will subsequently return.
This is useful after reading a password, to prevent the password from
echoing inadvertently as part of the next command in certain cases.
@end defun
@defvar last-nonmenu-event
This variable holds the last input event read as part of a key sequence,
not counting events resulting from mouse menus.
One use of this variable is for telling @code{x-popup-menu} where to pop
up a menu. It is also used internally by @code{y-or-n-p}
(@pxref{Yes-or-No Queries}).
@end defvar
@defvar last-command-event
This variable is set to the last input event that was read by the
command loop as part of a command. The principal use of this variable
is in @code{self-insert-command}, which uses it to decide which
character to insert.
@example
@group
last-command-event
;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
@result{} 5
@end group
@end example
@noindent
The value is 5 because that is the @acronym{ASCII} code for @kbd{C-e}.
@end defvar
@defvar last-event-frame