(M21670): Unify GB, CNS and JIS.
[chise/xemacs-chise.git-] / info / lispref.info-16
1 This is ../info/lispref.info, produced by makeinfo version 4.0 from
2 lispref/lispref.texi.
3
4 INFO-DIR-SECTION XEmacs Editor
5 START-INFO-DIR-ENTRY
6 * Lispref: (lispref).           XEmacs Lisp Reference Manual.
7 END-INFO-DIR-ENTRY
8
9    Edition History:
10
11    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
12 Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
13 Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
14 XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
15 GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
16 Programmer's Manual (for 19.13) Third Edition, July 1995 XEmacs Lisp
17 Reference Manual (for 19.14 and 20.0) v3.1, March 1996 XEmacs Lisp
18 Reference Manual (for 19.15 and 20.1, 20.2, 20.3) v3.2, April, May,
19 November 1997 XEmacs Lisp Reference Manual (for 21.0) v3.3, April 1998
20
21    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
22 Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
23 Copyright (C) 1995, 1996 Ben Wing.
24
25    Permission is granted to make and distribute verbatim copies of this
26 manual provided the copyright notice and this permission notice are
27 preserved on all copies.
28
29    Permission is granted to copy and distribute modified versions of
30 this manual under the conditions for verbatim copying, provided that the
31 entire resulting derived work is distributed under the terms of a
32 permission notice identical to this one.
33
34    Permission is granted to copy and distribute translations of this
35 manual into another language, under the above conditions for modified
36 versions, except that this permission notice may be stated in a
37 translation approved by the Foundation.
38
39    Permission is granted to copy and distribute modified versions of
40 this manual under the conditions for verbatim copying, provided also
41 that the section entitled "GNU General Public License" is included
42 exactly as in the original, and provided that the entire resulting
43 derived work is distributed under the terms of a permission notice
44 identical to this one.
45
46    Permission is granted to copy and distribute translations of this
47 manual into another language, under the above conditions for modified
48 versions, except that the section entitled "GNU General Public License"
49 may be included in a translation approved by the Free Software
50 Foundation instead of in the original English.
51
52 \1f
53 File: lispref.info,  Node: High-Level Completion,  Next: Reading File Names,  Prev: Completion Commands,  Up: Completion
54
55 High-Level Completion  Functions
56 --------------------------------
57
58    This section describes the higher-level convenient functions for
59 reading certain sorts of names with completion.
60
61    In most cases, you should not call these functions in the middle of a
62 Lisp function.  When possible, do all minibuffer input as part of
63 reading the arguments for a command, in the `interactive' spec.  *Note
64 Defining Commands::.
65
66  - Function: read-buffer prompt &optional default existing
67      This function reads the name of a buffer and returns it as a
68      string.  The argument DEFAULT is the default name to use, the
69      value to return if the user exits with an empty minibuffer.  If
70      non-`nil', it should be a string or a buffer.  It is mentioned in
71      the prompt, but is not inserted in the minibuffer as initial input.
72
73      If EXISTING is non-`nil', then the name specified must be that of
74      an existing buffer.  The usual commands to exit the minibuffer do
75      not exit if the text is not valid, and <RET> does completion to
76      attempt to find a valid name.  (However, DEFAULT is not checked
77      for validity; it is returned, whatever it is, if the user exits
78      with the minibuffer empty.)
79
80      In the following example, the user enters `minibuffer.t', and then
81      types <RET>.  The argument EXISTING is `t', and the only buffer
82      name starting with the given input is `minibuffer.texi', so that
83      name is the value.
84
85           (read-buffer "Buffer name? " "foo" t)
86           ;; After evaluation of the preceding expression,
87           ;;   the following prompt appears,
88           ;;   with an empty minibuffer:
89           
90           ---------- Buffer: Minibuffer ----------
91           Buffer name? (default foo) -!-
92           ---------- Buffer: Minibuffer ----------
93           
94           ;; The user types `minibuffer.t <RET>'.
95                => "minibuffer.texi"
96
97  - Function: read-command prompt
98      This function reads the name of a command and returns it as a Lisp
99      symbol.  The argument PROMPT is used as in `read-from-minibuffer'.
100      Recall that a command is anything for which `commandp' returns
101      `t', and a command name is a symbol for which `commandp' returns
102      `t'.  *Note Interactive Call::.
103
104           (read-command "Command name? ")
105           
106           ;; After evaluation of the preceding expression,
107           ;;   the following prompt appears with an empty minibuffer:
108           
109           ---------- Buffer: Minibuffer ----------
110           Command name?
111           ---------- Buffer: Minibuffer ----------
112
113      If the user types `forward-c <RET>', then this function returns
114      `forward-char'.
115
116      The `read-command' function is a simplified interface to the
117      function `completing-read'.  It uses the variable `obarray' so as
118      to complete in the set of extant Lisp symbols, and it uses the
119      `commandp' predicate so as to accept only command names:
120
121           (read-command PROMPT)
122           ==
123           (intern (completing-read PROMPT obarray
124                                    'commandp t nil))
125
126  - Function: read-variable prompt
127      This function reads the name of a user variable and returns it as a
128      symbol.
129
130           (read-variable "Variable name? ")
131           
132           ;; After evaluation of the preceding expression,
133           ;;   the following prompt appears,
134           ;;   with an empty minibuffer:
135           
136           ---------- Buffer: Minibuffer ----------
137           Variable name? -!-
138           ---------- Buffer: Minibuffer ----------
139
140      If the user then types `fill-p <RET>', `read-variable' returns
141      `fill-prefix'.
142
143      This function is similar to `read-command', but uses the predicate
144      `user-variable-p' instead of `commandp':
145
146           (read-variable PROMPT)
147           ==
148           (intern
149            (completing-read PROMPT obarray
150                             'user-variable-p t nil))
151
152 \1f
153 File: lispref.info,  Node: Reading File Names,  Next: Programmed Completion,  Prev: High-Level Completion,  Up: Completion
154
155 Reading File Names
156 ------------------
157
158    Here is another high-level completion function, designed for reading
159 a file name.  It provides special features including automatic insertion
160 of the default directory.
161
162  - Function: read-file-name prompt &optional directory default existing
163           initial
164      This function reads a file name in the minibuffer, prompting with
165      PROMPT and providing completion.  If DEFAULT is non-`nil', then
166      the function returns DEFAULT if the user just types <RET>.
167      DEFAULT is not checked for validity; it is returned, whatever it
168      is, if the user exits with the minibuffer empty.
169
170      If EXISTING is non-`nil', then the user must specify the name of
171      an existing file; <RET> performs completion to make the name valid
172      if possible, and then refuses to exit if it is not valid.  If the
173      value of EXISTING is neither `nil' nor `t', then <RET> also
174      requires confirmation after completion.  If EXISTING is `nil',
175      then the name of a nonexistent file is acceptable.
176
177      The argument DIRECTORY specifies the directory to use for
178      completion of relative file names.  If `insert-default-directory'
179      is non-`nil', DIRECTORY is also inserted in the minibuffer as
180      initial input.  It defaults to the current buffer's value of
181      `default-directory'.
182
183      If you specify INITIAL, that is an initial file name to insert in
184      the buffer (after DIRECTORY, if that is inserted).  In this case,
185      point goes at the beginning of INITIAL.  The default for INITIAL
186      is `nil'--don't insert any file name.  To see what INITIAL does,
187      try the command `C-x C-v'.
188
189      Here is an example:
190
191           (read-file-name "The file is ")
192           
193           ;; After evaluation of the preceding expression,
194           ;;   the following appears in the minibuffer:
195           
196           ---------- Buffer: Minibuffer ----------
197           The file is /gp/gnu/elisp/-!-
198           ---------- Buffer: Minibuffer ----------
199
200      Typing `manual <TAB>' results in the following:
201
202           ---------- Buffer: Minibuffer ----------
203           The file is /gp/gnu/elisp/manual.texi-!-
204           ---------- Buffer: Minibuffer ----------
205
206      If the user types <RET>, `read-file-name' returns the file name as
207      the string `"/gp/gnu/elisp/manual.texi"'.
208
209  - User Option: insert-default-directory
210      This variable is used by `read-file-name'.  Its value controls
211      whether `read-file-name' starts by placing the name of the default
212      directory in the minibuffer, plus the initial file name if any.
213      If the value of this variable is `nil', then `read-file-name' does
214      not place any initial input in the minibuffer (unless you specify
215      initial input with the INITIAL argument).  In that case, the
216      default directory is still used for completion of relative file
217      names, but is not displayed.
218
219      For example:
220
221           ;; Here the minibuffer starts out with the default directory.
222           (let ((insert-default-directory t))
223             (read-file-name "The file is "))
224           
225           ---------- Buffer: Minibuffer ----------
226           The file is ~lewis/manual/-!-
227           ---------- Buffer: Minibuffer ----------
228           
229           ;; Here the minibuffer is empty and only the prompt
230           ;;   appears on its line.
231           (let ((insert-default-directory nil))
232             (read-file-name "The file is "))
233           
234           ---------- Buffer: Minibuffer ----------
235           The file is -!-
236           ---------- Buffer: Minibuffer ----------
237
238 \1f
239 File: lispref.info,  Node: Programmed Completion,  Prev: Reading File Names,  Up: Completion
240
241 Programmed Completion
242 ---------------------
243
244    Sometimes it is not possible to create an alist or an obarray
245 containing all the intended possible completions.  In such a case, you
246 can supply your own function to compute the completion of a given
247 string.  This is called "programmed completion".
248
249    To use this feature, pass a symbol with a function definition as the
250 COLLECTION argument to `completing-read'.  The function
251 `completing-read' arranges to pass your completion function along to
252 `try-completion' and `all-completions', which will then let your
253 function do all the work.
254
255    The completion function should accept three arguments:
256
257    * The string to be completed.
258
259    * The predicate function to filter possible matches, or `nil' if
260      none.  Your function should call the predicate for each possible
261      match, and ignore the possible match if the predicate returns
262      `nil'.
263
264    * A flag specifying the type of operation.
265
266    There are three flag values for three operations:
267
268    * `nil' specifies `try-completion'.  The completion function should
269      return the completion of the specified string, or `t' if the
270      string is a unique and exact match already, or `nil' if the string
271      matches no possibility.
272
273      If the string is an exact match for one possibility, but also
274      matches other longer possibilities, the function should return the
275      string, not `t'.
276
277    * `t' specifies `all-completions'.  The completion function should
278      return a list of all possible completions of the specified string.
279
280    * `lambda' specifies a test for an exact match.  The completion
281      function should return `t' if the specified string is an exact
282      match for some possibility; `nil' otherwise.
283
284    It would be consistent and clean for completion functions to allow
285 lambda expressions (lists that are functions) as well as function
286 symbols as COLLECTION, but this is impossible.  Lists as completion
287 tables are already assigned another meaning--as alists.  It would be
288 unreliable to fail to handle an alist normally because it is also a
289 possible function.  So you must arrange for any function you wish to
290 use for completion to be encapsulated in a symbol.
291
292    Emacs uses programmed completion when completing file names.  *Note
293 File Name Completion::.
294
295 \1f
296 File: lispref.info,  Node: Yes-or-No Queries,  Next: Multiple Queries,  Prev: Completion,  Up: Minibuffers
297
298 Yes-or-No Queries
299 =================
300
301    This section describes functions used to ask the user a yes-or-no
302 question.  The function `y-or-n-p' can be answered with a single
303 character; it is useful for questions where an inadvertent wrong answer
304 will not have serious consequences.  `yes-or-no-p' is suitable for more
305 momentous questions, since it requires three or four characters to
306 answer.  Variations of these functions can be used to ask a yes-or-no
307 question using a dialog box, or optionally using one.
308
309    If either of these functions is called in a command that was invoked
310 using the mouse, then it uses a dialog box or pop-up menu to ask the
311 question.  Otherwise, it uses keyboard input.
312
313    Strictly speaking, `yes-or-no-p' uses the minibuffer and `y-or-n-p'
314 does not; but it seems best to describe them together.
315
316  - Function: y-or-n-p prompt
317      This function asks the user a question, expecting input in the echo
318      area.  It returns `t' if the user types `y', `nil' if the user
319      types `n'.  This function also accepts <SPC> to mean yes and <DEL>
320      to mean no.  It accepts `C-]' to mean "quit", like `C-g', because
321      the question might look like a minibuffer and for that reason the
322      user might try to use `C-]' to get out.  The answer is a single
323      character, with no <RET> needed to terminate it.  Upper and lower
324      case are equivalent.
325
326      "Asking the question" means printing PROMPT in the echo area,
327      followed by the string `(y or n) '.  If the input is not one of
328      the expected answers (`y', `n', `<SPC>', `<DEL>', or something
329      that quits), the function responds `Please answer y or n.', and
330      repeats the request.
331
332      This function does not actually use the minibuffer, since it does
333      not allow editing of the answer.  It actually uses the echo area
334      (*note The Echo Area::), which uses the same screen space as the
335      minibuffer.  The cursor moves to the echo area while the question
336      is being asked.
337
338      The answers and their meanings, even `y' and `n', are not
339      hardwired.  The keymap `query-replace-map' specifies them.  *Note
340      Search and Replace::.
341
342      In the following example, the user first types `q', which is
343      invalid.  At the next prompt the user types `y'.
344
345           (y-or-n-p "Do you need a lift? ")
346           
347           ;; After evaluation of the preceding expression,
348           ;;   the following prompt appears in the echo area:
349           
350           ---------- Echo area ----------
351           Do you need a lift? (y or n)
352           ---------- Echo area ----------
353           
354           ;; If the user then types `q', the following appears:
355           
356           ---------- Echo area ----------
357           Please answer y or n.  Do you need a lift? (y or n)
358           ---------- Echo area ----------
359           
360           ;; When the user types a valid answer,
361           ;;   it is displayed after the question:
362           
363           ---------- Echo area ----------
364           Do you need a lift? (y or n) y
365           ---------- Echo area ----------
366
367      We show successive lines of echo area messages, but only one
368      actually appears on the screen at a time.
369
370  - Function: yes-or-no-p prompt
371      This function asks the user a question, expecting input in the
372      minibuffer.  It returns `t' if the user enters `yes', `nil' if the
373      user types `no'.  The user must type <RET> to finalize the
374      response.  Upper and lower case are equivalent.
375
376      `yes-or-no-p' starts by displaying PROMPT in the echo area,
377      followed by `(yes or no) '.  The user must type one of the
378      expected responses; otherwise, the function responds `Please answer
379      yes or no.', waits about two seconds and repeats the request.
380
381      `yes-or-no-p' requires more work from the user than `y-or-n-p' and
382      is appropriate for more crucial decisions.
383
384      Here is an example:
385
386           (yes-or-no-p "Do you really want to remove everything? ")
387           
388           ;; After evaluation of the preceding expression,
389           ;;   the following prompt appears,
390           ;;   with an empty minibuffer:
391           
392           ---------- Buffer: minibuffer ----------
393           Do you really want to remove everything? (yes or no)
394           ---------- Buffer: minibuffer ----------
395
396      If the user first types `y <RET>', which is invalid because this
397      function demands the entire word `yes', it responds by displaying
398      these prompts, with a brief pause between them:
399
400           ---------- Buffer: minibuffer ----------
401           Please answer yes or no.
402           Do you really want to remove everything? (yes or no)
403           ---------- Buffer: minibuffer ----------
404
405  - Function: yes-or-no-p-dialog-box prompt
406      This function asks the user a "y or n" question with a popup dialog
407      box.  It returns `t' if the answer is "yes".  PROMPT is the string
408      to display to ask the question.
409
410    The following functions ask a question either in the minibuffer or a
411 dialog box, depending on whether the last user event (which presumably
412 invoked this command) was a keyboard or mouse event.  When XEmacs is
413 running on a window system, the functions `y-or-n-p' and `yes-or-no-p'
414 are replaced with the following functions, so that menu items bring up
415 dialog boxes instead of minibuffer questions.
416
417  - Function: y-or-n-p-maybe-dialog-box prompt
418      This function asks user a "y or n" question, using either a dialog
419      box or the minibuffer, as appropriate.
420
421  - Function: yes-or-no-p-maybe-dialog-box prompt
422      This function asks user a "yes or no" question, using either a
423      dialog box or the minibuffer, as appropriate.
424
425 \1f
426 File: lispref.info,  Node: Multiple Queries,  Next: Reading a Password,  Prev: Yes-or-No Queries,  Up: Minibuffers
427
428 Asking Multiple Y-or-N Questions
429 ================================
430
431    When you have a series of similar questions to ask, such as "Do you
432 want to save this buffer" for each buffer in turn, you should use
433 `map-y-or-n-p' to ask the collection of questions, rather than asking
434 each question individually.  This gives the user certain convenient
435 facilities such as the ability to answer the whole series at once.
436
437  - Function: map-y-or-n-p prompter actor list &optional help
438           action-alist
439      This function, new in Emacs 19, asks the user a series of
440      questions, reading a single-character answer in the echo area for
441      each one.
442
443      The value of LIST specifies the objects to ask questions about.
444      It should be either a list of objects or a generator function.  If
445      it is a function, it should expect no arguments, and should return
446      either the next object to ask about, or `nil' meaning stop asking
447      questions.
448
449      The argument PROMPTER specifies how to ask each question.  If
450      PROMPTER is a string, the question text is computed like this:
451
452           (format PROMPTER OBJECT)
453
454      where OBJECT is the next object to ask about (as obtained from
455      LIST).
456
457      If not a string, PROMPTER should be a function of one argument
458      (the next object to ask about) and should return the question
459      text.  If the value is a string, that is the question to ask the
460      user.  The function can also return `t' meaning do act on this
461      object (and don't ask the user), or `nil' meaning ignore this
462      object (and don't ask the user).
463
464      The argument ACTOR says how to act on the answers that the user
465      gives.  It should be a function of one argument, and it is called
466      with each object that the user says yes for.  Its argument is
467      always an object obtained from LIST.
468
469      If the argument HELP is given, it should be a list of this form:
470
471           (SINGULAR PLURAL ACTION)
472
473      where SINGULAR is a string containing a singular noun that
474      describes the objects conceptually being acted on, PLURAL is the
475      corresponding plural noun, and ACTION is a transitive verb
476      describing what ACTOR does.
477
478      If you don't specify HELP, the default is `("object" "objects"
479      "act on")'.
480
481      Each time a question is asked, the user may enter `y', `Y', or
482      <SPC> to act on that object; `n', `N', or <DEL> to skip that
483      object; `!' to act on all following objects; <ESC> or `q' to exit
484      (skip all following objects); `.' (period) to act on the current
485      object and then exit; or `C-h' to get help.  These are the same
486      answers that `query-replace' accepts.  The keymap
487      `query-replace-map' defines their meaning for `map-y-or-n-p' as
488      well as for `query-replace'; see *Note Search and Replace::.
489
490      You can use ACTION-ALIST to specify additional possible answers
491      and what they mean.  It is an alist of elements of the form `(CHAR
492      FUNCTION HELP)', each of which defines one additional answer.  In
493      this element, CHAR is a character (the answer); FUNCTION is a
494      function of one argument (an object from LIST); HELP is a string.
495
496      When the user responds with CHAR, `map-y-or-n-p' calls FUNCTION.
497      If it returns non-`nil', the object is considered "acted upon",
498      and `map-y-or-n-p' advances to the next object in LIST.  If it
499      returns `nil', the prompt is repeated for the same object.
500
501      If `map-y-or-n-p' is called in a command that was invoked using the
502      mouse--more precisely, if `last-nonmenu-event' (*note Command Loop
503      Info::) is either `nil' or a list--then it uses a dialog box or
504      pop-up menu to ask the question.  In this case, it does not use
505      keyboard input or the echo area.  You can force use of the mouse
506      or use of keyboard input by binding `last-nonmenu-event' to a
507      suitable value around the call.
508
509      The return value of `map-y-or-n-p' is the number of objects acted
510      on.
511
512 \1f
513 File: lispref.info,  Node: Reading a Password,  Next: Minibuffer Misc,  Prev: Multiple Queries,  Up: Minibuffers
514
515 Reading a Password
516 ==================
517
518    To read a password to pass to another program, you can use the
519 function `read-passwd'.
520
521  - Function: read-passwd prompt &optional confirm default
522      This function reads a password, prompting with PROMPT.  It does
523      not echo the password as the user types it; instead, it echoes `.'
524      for each character in the password.
525
526      The optional argument CONFIRM, if non-`nil', says to read the
527      password twice and insist it must be the same both times.  If it
528      isn't the same, the user has to type it over and over until the
529      last two times match.
530
531      The optional argument DEFAULT specifies the default password to
532      return if the user enters empty input.  It is translated to `.'
533      and inserted in the minibuffer. If DEFAULT is `nil', then
534      `read-passwd' returns the null string in that case.
535
536  - User Option: passwd-invert-frame-when-keyboard-grabbed
537      If non-nil swap the foreground and background colors of all faces
538      while reading a password.  Default values is `t' unless feature
539      `infodock' is provided.
540
541  - User Option: passwd-echo
542      This specifies the character echoed when typing a password.  When
543      nil, nothing is echoed.
544
545 \1f
546 File: lispref.info,  Node: Minibuffer Misc,  Prev: Reading a Password,  Up: Minibuffers
547
548 Minibuffer Miscellany
549 =====================
550
551    This section describes some basic functions and variables related to
552 minibuffers.
553
554  - Command: exit-minibuffer
555      This command exits the active minibuffer.  It is normally bound to
556      keys in minibuffer local keymaps.
557
558  - Command: self-insert-and-exit
559      This command exits the active minibuffer after inserting the last
560      character typed on the keyboard (found in `last-command-char';
561      *note Command Loop Info::).
562
563  - Command: previous-history-element n
564      This command replaces the minibuffer contents with the value of the
565      Nth previous (older) history element.
566
567  - Command: next-history-element n
568      This command replaces the minibuffer contents with the value of the
569      Nth more recent history element.
570
571  - Command: previous-matching-history-element pattern
572      This command replaces the minibuffer contents with the value of the
573      previous (older) history element that matches PATTERN (a regular
574      expression).
575
576  - Command: next-matching-history-element pattern
577      This command replaces the minibuffer contents with the value of
578      the next (newer) history element that matches PATTERN (a regular
579      expression).
580
581  - Function: minibuffer-prompt
582      This function returns the prompt string of the currently active
583      minibuffer.  If no minibuffer is active, it returns `nil'.
584
585  - Function: minibuffer-prompt-width
586      This function returns the display width of the prompt string of the
587      currently active minibuffer.  If no minibuffer is active, it
588      returns 0.
589
590  - Variable: minibuffer-setup-hook
591      This is a normal hook that is run whenever the minibuffer is
592      entered.  *Note Hooks::.
593
594  - Variable: minibuffer-exit-hook
595      This is a normal hook that is run whenever the minibuffer is
596      exited.  *Note Hooks::.
597
598  - Variable: minibuffer-help-form
599      The current value of this variable is used to rebind `help-form'
600      locally inside the minibuffer (*note Help Functions::).
601
602  - Function: active-minibuffer-window
603      This function returns the currently active minibuffer window, or
604      `nil' if none is currently active.
605
606  - Function: minibuffer-window &optional frame
607      This function returns the minibuffer window used for frame FRAME.
608      If FRAME is `nil', that stands for the current frame.  Note that
609      the minibuffer window used by a frame need not be part of that
610      frame--a frame that has no minibuffer of its own necessarily uses
611      some other frame's minibuffer window.
612
613  - Function: window-minibuffer-p window
614      This function returns non-`nil' if WINDOW is a minibuffer window.
615
616    It is not correct to determine whether a given window is a
617 minibuffer by comparing it with the result of `(minibuffer-window)',
618 because there can be more than one minibuffer window if there is more
619 than one frame.
620
621  - Function: minibuffer-window-active-p window
622      This function returns non-`nil' if WINDOW, assumed to be a
623      minibuffer window, is currently active.
624
625  - Variable: minibuffer-scroll-window
626      If the value of this variable is non-`nil', it should be a window
627      object.  When the function `scroll-other-window' is called in the
628      minibuffer, it scrolls this window.
629
630    Finally, some functions and variables deal with recursive minibuffers
631 (*note Recursive Editing::):
632
633  - Function: minibuffer-depth
634      This function returns the current depth of activations of the
635      minibuffer, a nonnegative integer.  If no minibuffers are active,
636      it returns zero.
637
638  - User Option: enable-recursive-minibuffers
639      If this variable is non-`nil', you can invoke commands (such as
640      `find-file') that use minibuffers even while the minibuffer window
641      is active.  Such invocation produces a recursive editing level for
642      a new minibuffer.  The outer-level minibuffer is invisible while
643      you are editing the inner one.
644
645      This variable only affects invoking the minibuffer while the
646      minibuffer window is selected.   If you switch windows while in the
647      minibuffer, you can always invoke minibuffer commands while some
648      other window is selected.
649
650    In FSF Emacs 19, if a command name has a property
651 `enable-recursive-minibuffers' that is non-`nil', then the command can
652 use the minibuffer to read arguments even if it is invoked from the
653 minibuffer.  The minibuffer command `next-matching-history-element'
654 (normally `M-s' in the minibuffer) uses this feature.
655
656    This is not implemented in XEmacs because it is a kludge.  If you
657 want to explicitly set the value of `enable-recursive-minibuffers' in
658 this fashion, just use an evaluated interactive spec and bind
659 `enable-recursive-minibuffers' while reading from the minibuffer.  See
660 the definition of `next-matching-history-element' in `lisp/minibuf.el'.
661
662 \1f
663 File: lispref.info,  Node: Command Loop,  Next: Keymaps,  Prev: Minibuffers,  Up: Top
664
665 Command Loop
666 ************
667
668    When you run XEmacs, it enters the "editor command loop" almost
669 immediately.  This loop reads events, executes their definitions, and
670 displays the results.  In this chapter, we describe how these things
671 are done, and the subroutines that allow Lisp programs to do them.
672
673 * Menu:
674
675 * Command Overview::    How the command loop reads commands.
676 * Defining Commands::   Specifying how a function should read arguments.
677 * Interactive Call::    Calling a command, so that it will read arguments.
678 * Command Loop Info::   Variables set by the command loop for you to examine.
679 * Events::              What input looks like when you read it.
680 * Reading Input::       How to read input events from the keyboard or mouse.
681 * Waiting::             Waiting for user input or elapsed time.
682 * Quitting::            How C-g works.  How to catch or defer quitting.
683 * Prefix Command Arguments::    How the commands to set prefix args work.
684 * Recursive Editing::   Entering a recursive edit,
685                           and why you usually shouldn't.
686 * Disabling Commands::  How the command loop handles disabled commands.
687 * Command History::     How the command history is set up, and how accessed.
688 * Keyboard Macros::     How keyboard macros are implemented.
689
690 \1f
691 File: lispref.info,  Node: Command Overview,  Next: Defining Commands,  Up: Command Loop
692
693 Command Loop Overview
694 =====================
695
696    The command loop in XEmacs is a standard event loop, reading events
697 one at a time with `next-event' and handling them with
698 `dispatch-event'.  An event is typically a single user action, such as
699 a keypress, mouse movement, or menu selection; but they can also be
700 notifications from the window system, informing XEmacs that (for
701 example) part of its window was just uncovered and needs to be redrawn.
702 *Note Events::.  Pending events are held in a first-in, first-out list
703 called the "event queue": events are read from the head of the list,
704 and newly arriving events are added to the tail.  In this way, events
705 are always processed in the order in which they arrive.
706
707    `dispatch-event' does most of the work of handling user actions.
708 The first thing it must do is put the events together into a key
709 sequence, which is a sequence of events that translates into a command.
710 It does this by consulting the active keymaps, which specify what the
711 valid key sequences are and how to translate them into commands.  *Note
712 Key Lookup::, for information on how this is done.  The result of the
713 translation should be a keyboard macro or an interactively callable
714 function.  If the key is `M-x', then it reads the name of another
715 command, which it then calls.  This is done by the command
716 `execute-extended-command' (*note Interactive Call::).
717
718    To execute a command requires first reading the arguments for it.
719 This is done by calling `command-execute' (*note Interactive Call::).
720 For commands written in Lisp, the `interactive' specification says how
721 to read the arguments.  This may use the prefix argument (*note Prefix
722 Command Arguments::) or may read with prompting in the minibuffer
723 (*note Minibuffers::).  For example, the command `find-file' has an
724 `interactive' specification which says to read a file name using the
725 minibuffer.  The command's function body does not use the minibuffer;
726 if you call this command from Lisp code as a function, you must supply
727 the file name string as an ordinary Lisp function argument.
728
729    If the command is a string or vector (i.e., a keyboard macro) then
730 `execute-kbd-macro' is used to execute it.  You can call this function
731 yourself (*note Keyboard Macros::).
732
733    To terminate the execution of a running command, type `C-g'.  This
734 character causes "quitting" (*note Quitting::).
735
736  - Variable: pre-command-hook
737      The editor command loop runs this normal hook before each command.
738      At that time, `this-command' contains the command that is about to
739      run, and `last-command' describes the previous command.  *Note
740      Hooks::.
741
742  - Variable: post-command-hook
743      The editor command loop runs this normal hook after each command.
744      (In FSF Emacs, it is also run when the command loop is entered, or
745      reentered after an error or quit.)  At that time, `this-command'
746      describes the command that just ran, and `last-command' describes
747      the command before that.  *Note Hooks::.
748
749    Quitting is suppressed while running `pre-command-hook' and
750 `post-command-hook'.  If an error happens while executing one of these
751 hooks, it terminates execution of the hook, but that is all it does.
752
753 \1f
754 File: lispref.info,  Node: Defining Commands,  Next: Interactive Call,  Prev: Command Overview,  Up: Command Loop
755
756 Defining Commands
757 =================
758
759    A Lisp function becomes a command when its body contains, at top
760 level, a form that calls the special form `interactive'.  This form
761 does nothing when actually executed, but its presence serves as a flag
762 to indicate that interactive calling is permitted.  Its argument
763 controls the reading of arguments for an interactive call.
764
765 * Menu:
766
767 * Using Interactive::     General rules for `interactive'.
768 * Interactive Codes::     The standard letter-codes for reading arguments
769                              in various ways.
770 * Interactive Examples::  Examples of how to read interactive arguments.
771
772 \1f
773 File: lispref.info,  Node: Using Interactive,  Next: Interactive Codes,  Up: Defining Commands
774
775 Using `interactive'
776 -------------------
777
778    This section describes how to write the `interactive' form that
779 makes a Lisp function an interactively-callable command.
780
781  - Special Form: interactive arg-descriptor
782      This special form declares that the function in which it appears
783      is a command, and that it may therefore be called interactively
784      (via `M-x' or by entering a key sequence bound to it).  The
785      argument ARG-DESCRIPTOR declares how to compute the arguments to
786      the command when the command is called interactively.
787
788      A command may be called from Lisp programs like any other
789      function, but then the caller supplies the arguments and
790      ARG-DESCRIPTOR has no effect.
791
792      The `interactive' form has its effect because the command loop
793      (actually, its subroutine `call-interactively') scans through the
794      function definition looking for it, before calling the function.
795      Once the function is called, all its body forms including the
796      `interactive' form are executed, but at this time `interactive'
797      simply returns `nil' without even evaluating its argument.
798
799    There are three possibilities for the argument ARG-DESCRIPTOR:
800
801    * It may be omitted or `nil'; then the command is called with no
802      arguments.  This leads quickly to an error if the command requires
803      one or more arguments.
804
805    * It may be a Lisp expression that is not a string; then it should
806      be a form that is evaluated to get a list of arguments to pass to
807      the command.
808
809      If this expression reads keyboard input (this includes using the
810      minibuffer), keep in mind that the integer value of point or the
811      mark before reading input may be incorrect after reading input.
812      This is because the current buffer may be receiving subprocess
813      output; if subprocess output arrives while the command is waiting
814      for input, it could relocate point and the mark.
815
816      Here's an example of what _not_ to do:
817
818           (interactive
819            (list (region-beginning) (region-end)
820                  (read-string "Foo: " nil 'my-history)))
821
822      Here's how to avoid the problem, by examining point and the mark
823      only after reading the keyboard input:
824
825           (interactive
826            (let ((string (read-string "Foo: " nil 'my-history)))
827              (list (region-beginning) (region-end) string)))
828
829    * It may be a string; then its contents should consist of a code
830      character followed by a prompt (which some code characters use and
831      some ignore).  The prompt ends either with the end of the string
832      or with a newline.  Here is a simple example:
833
834           (interactive "bFrobnicate buffer: ")
835
836      The code letter `b' says to read the name of an existing buffer,
837      with completion.  The buffer name is the sole argument passed to
838      the command.  The rest of the string is a prompt.
839
840      If there is a newline character in the string, it terminates the
841      prompt.  If the string does not end there, then the rest of the
842      string should contain another code character and prompt,
843      specifying another argument.  You can specify any number of
844      arguments in this way.
845
846      The prompt string can use `%' to include previous argument values
847      (starting with the first argument) in the prompt.  This is done
848      using `format' (*note Formatting Strings::).  For example, here is
849      how you could read the name of an existing buffer followed by a
850      new name to give to that buffer:
851
852           (interactive "bBuffer to rename: \nsRename buffer %s to: ")
853
854      If the first character in the string is `*', then an error is
855      signaled if the buffer is read-only.
856
857      If the first character in the string is `@', and if the key
858      sequence used to invoke the command includes any mouse events, then
859      the window associated with the first of those events is selected
860      before the command is run.
861
862      If the first character in the string is `_', then this command will
863      not cause the region to be deactivated when it completes; that is,
864      `zmacs-region-stays' will be set to `t' when the command exits
865      successfully.
866
867      You can use `*', `@', and `_' together; the order does not matter.
868      Actual reading of arguments is controlled by the rest of the
869      prompt string (starting with the first character that is not `*',
870      `@', or `_').
871
872  - Function: function-interactive function
873      This function retrieves the interactive specification of FUNCTION,
874      which may be any funcallable object.  The specification will be
875      returned as the list of the symbol `interactive' and the specs.  If
876      FUNCTION is not interactive, `nil' will be returned.
877
878 \1f
879 File: lispref.info,  Node: Interactive Codes,  Next: Interactive Examples,  Prev: Using Interactive,  Up: Defining Commands
880
881 Code Characters for `interactive'
882 ---------------------------------
883
884    The code character descriptions below contain a number of key words,
885 defined here as follows:
886
887 Completion
888      Provide completion.  <TAB>, <SPC>, and <RET> perform name
889      completion because the argument is read using `completing-read'
890      (*note Completion::).  `?' displays a list of possible completions.
891
892 Existing
893      Require the name of an existing object.  An invalid name is not
894      accepted; the commands to exit the minibuffer do not exit if the
895      current input is not valid.
896
897 Default
898      A default value of some sort is used if the user enters no text in
899      the minibuffer.  The default depends on the code character.
900
901 No I/O
902      This code letter computes an argument without reading any input.
903      Therefore, it does not use a prompt string, and any prompt string
904      you supply is ignored.
905
906      Even though the code letter doesn't use a prompt string, you must
907      follow it with a newline if it is not the last code character in
908      the string.
909
910 Prompt
911      A prompt immediately follows the code character.  The prompt ends
912      either with the end of the string or with a newline.
913
914 Special
915      This code character is meaningful only at the beginning of the
916      interactive string, and it does not look for a prompt or a newline.
917      It is a single, isolated character.
918
919    Here are the code character descriptions for use with `interactive':
920
921 `*'
922      Signal an error if the current buffer is read-only.  Special.
923
924 `@'
925      Select the window mentioned in the first mouse event in the key
926      sequence that invoked this command.  Special.
927
928 `_'
929      Do not cause the region to be deactivated when this command
930      completes.  Special.
931
932 `a'
933      A function name (i.e., a symbol satisfying `fboundp').  Existing,
934      Completion, Prompt.
935
936 `b'
937      The name of an existing buffer.  By default, uses the name of the
938      current buffer (*note Buffers::).  Existing, Completion, Default,
939      Prompt.
940
941 `B'
942      A buffer name.  The buffer need not exist.  By default, uses the
943      name of a recently used buffer other than the current buffer.
944      Completion, Default, Prompt.
945
946 `c'
947      A character.  The cursor does not move into the echo area.  Prompt.
948
949 `C'
950      A command name (i.e., a symbol satisfying `commandp').  Existing,
951      Completion, Prompt.
952
953 `d'
954      The position of point, as an integer (*note Point::).  No I/O.
955
956 `D'
957      A directory name.  The default is the current default directory of
958      the current buffer, `default-directory' (*note System
959      Environment::).  Existing, Completion, Default, Prompt.
960
961 `e'
962      The last mouse-button or misc-user event in the key sequence that
963      invoked the command.  No I/O.
964
965      You can use `e' more than once in a single command's interactive
966      specification.  If the key sequence that invoked the command has N
967      mouse-button or misc-user events, the Nth `e' provides the Nth
968      such event.
969
970 `f'
971      A file name of an existing file (*note File Names::).  The default
972      directory is `default-directory'.  Existing, Completion, Default,
973      Prompt.
974
975 `F'
976      A file name.  The file need not exist.  Completion, Default,
977      Prompt.
978
979 `k'
980      A key sequence (*note Keymap Terminology::).  This keeps reading
981      events until a command (or undefined command) is found in the
982      current key maps.  The key sequence argument is represented as a
983      vector of events.  The cursor does not move into the echo area.
984      Prompt.
985
986      This kind of input is used by commands such as `describe-key' and
987      `global-set-key'.
988
989 `K'
990      A key sequence, whose definition you intend to change.  This works
991      like `k', except that it suppresses, for the last input event in
992      the key sequence, the conversions that are normally used (when
993      necessary) to convert an undefined key into a defined one.
994
995 `m'
996      The position of the mark, as an integer.  No I/O.
997
998 `n'
999      A number read with the minibuffer.  If the input is not a number,
1000      the user is asked to try again.  The prefix argument, if any, is
1001      not used.  Prompt.
1002
1003 `N'
1004      The raw prefix argument.  If the prefix argument is `nil', then
1005      read a number as with `n'.  Requires a number.  *Note Prefix
1006      Command Arguments::.  Prompt.
1007
1008 `p'
1009      The numeric prefix argument.  (Note that this `p' is lower case.)
1010      No I/O.
1011
1012 `P'
1013      The raw prefix argument.  (Note that this `P' is upper case.)  No
1014      I/O.
1015
1016 `r'
1017      Point and the mark, as two numeric arguments, smallest first.
1018      This is the only code letter that specifies two successive
1019      arguments rather than one.  No I/O.
1020
1021 `s'
1022      Arbitrary text, read in the minibuffer and returned as a string
1023      (*note Text from Minibuffer::).  Terminate the input with either
1024      <LFD> or <RET>.  (`C-q' may be used to include either of these
1025      characters in the input.)  Prompt.
1026
1027 `S'
1028      An interned symbol whose name is read in the minibuffer.  Any
1029      whitespace character terminates the input.  (Use `C-q' to include
1030      whitespace in the string.)  Other characters that normally
1031      terminate a symbol (e.g., parentheses and brackets) do not do so
1032      here.  Prompt.
1033
1034 `v'
1035      A variable declared to be a user option (i.e., satisfying the
1036      predicate `user-variable-p').  *Note High-Level Completion::.
1037      Existing, Completion, Prompt.
1038
1039 `x'
1040      A Lisp object, specified with its read syntax, terminated with a
1041      <LFD> or <RET>.  The object is not evaluated.  *Note Object from
1042      Minibuffer::.  Prompt.
1043
1044 `X'
1045      A Lisp form is read as with `x', but then evaluated so that its
1046      value becomes the argument for the command.  Prompt.
1047
1048 \1f
1049 File: lispref.info,  Node: Interactive Examples,  Prev: Interactive Codes,  Up: Defining Commands
1050
1051 Examples of Using `interactive'
1052 -------------------------------
1053
1054    Here are some examples of `interactive':
1055
1056      (defun foo1 ()              ; `foo1' takes no arguments,
1057          (interactive)           ;   just moves forward two words.
1058          (forward-word 2))
1059           => foo1
1060      
1061      (defun foo2 (n)             ; `foo2' takes one argument,
1062          (interactive "p")       ;   which is the numeric prefix.
1063          (forward-word (* 2 n)))
1064           => foo2
1065      
1066      (defun foo3 (n)             ; `foo3' takes one argument,
1067          (interactive "nCount:") ;   which is read with the Minibuffer.
1068          (forward-word (* 2 n)))
1069           => foo3
1070      
1071      (defun three-b (b1 b2 b3)
1072        "Select three existing buffers.
1073      Put them into three windows, selecting the last one."
1074          (interactive "bBuffer1:\nbBuffer2:\nbBuffer3:")
1075          (delete-other-windows)
1076          (split-window (selected-window) 8)
1077          (switch-to-buffer b1)
1078          (other-window 1)
1079          (split-window (selected-window) 8)
1080          (switch-to-buffer b2)
1081          (other-window 1)
1082          (switch-to-buffer b3))
1083           => three-b
1084      (three-b "*scratch*" "declarations.texi" "*mail*")
1085           => nil
1086
1087 \1f
1088 File: lispref.info,  Node: Interactive Call,  Next: Command Loop Info,  Prev: Defining Commands,  Up: Command Loop
1089
1090 Interactive Call
1091 ================
1092
1093    After the command loop has translated a key sequence into a
1094 definition, it invokes that definition using the function
1095 `command-execute'.  If the definition is a function that is a command,
1096 `command-execute' calls `call-interactively', which reads the arguments
1097 and calls the command.  You can also call these functions yourself.
1098
1099  - Function: commandp object
1100      Returns `t' if OBJECT is suitable for calling interactively; that
1101      is, if OBJECT is a command.  Otherwise, returns `nil'.
1102
1103      The interactively callable objects include strings and vectors
1104      (treated as keyboard macros), lambda expressions that contain a
1105      top-level call to `interactive', compiled-function objects made
1106      from such lambda expressions, autoload objects that are declared
1107      as interactive (non-`nil' fourth argument to `autoload'), and some
1108      of the primitive functions.
1109
1110      A symbol is `commandp' if its function definition is `commandp'.
1111
1112      Keys and keymaps are not commands.  Rather, they are used to look
1113      up commands (*note Keymaps::).
1114
1115      See `documentation' in *Note Accessing Documentation::, for a
1116      realistic example of using `commandp'.
1117
1118  - Function: call-interactively command &optional record-flag
1119      This function calls the interactively callable function COMMAND,
1120      reading arguments according to its interactive calling
1121      specifications.  An error is signaled if COMMAND is not a function
1122      or if it cannot be called interactively (i.e., is not a command).
1123      Note that keyboard macros (strings and vectors) are not accepted,
1124      even though they are considered commands, because they are not
1125      functions.
1126
1127      If RECORD-FLAG is the symbol `lambda', the interactive calling
1128      arguments for `command' are read and returned as a list, but the
1129      function is not called on them.
1130
1131      If RECORD-FLAG is `t', then this command and its arguments are
1132      unconditionally added to the list `command-history'.  Otherwise,
1133      the command is added only if it uses the minibuffer to read an
1134      argument.  *Note Command History::.
1135
1136  - Function: command-execute command &optional record-flag
1137      This function executes COMMAND as an editing command.  The
1138      argument COMMAND must satisfy the `commandp' predicate; i.e., it
1139      must be an interactively callable function or a keyboard macro.
1140
1141      A string or vector as COMMAND is executed with
1142      `execute-kbd-macro'.  A function is passed to
1143      `call-interactively', along with the optional RECORD-FLAG.
1144
1145      A symbol is handled by using its function definition in its place.
1146      A symbol with an `autoload' definition counts as a command if it
1147      was declared to stand for an interactively callable function.
1148      Such a definition is handled by loading the specified library and
1149      then rechecking the definition of the symbol.
1150
1151  - Command: execute-extended-command prefix-argument
1152      This function reads a command name from the minibuffer using
1153      `completing-read' (*note Completion::).  Then it uses
1154      `command-execute' to call the specified command.  Whatever that
1155      command returns becomes the value of `execute-extended-command'.
1156
1157      If the command asks for a prefix argument, it receives the value
1158      PREFIX-ARGUMENT.  If `execute-extended-command' is called
1159      interactively, the current raw prefix argument is used for
1160      PREFIX-ARGUMENT, and thus passed on to whatever command is run.
1161
1162      `execute-extended-command' is the normal definition of `M-x', so
1163      it uses the string `M-x ' as a prompt.  (It would be better to
1164      take the prompt from the events used to invoke
1165      `execute-extended-command', but that is painful to implement.)  A
1166      description of the value of the prefix argument, if any, also
1167      becomes part of the prompt.
1168
1169           (execute-extended-command 1)
1170           ---------- Buffer: Minibuffer ----------
1171           1 M-x forward-word RET
1172           ---------- Buffer: Minibuffer ----------
1173                => t
1174
1175  - Function: interactive-p
1176      This function returns `t' if the containing function (the one that
1177      called `interactive-p') was called interactively, with the function
1178      `call-interactively'.  (It makes no difference whether
1179      `call-interactively' was called from Lisp or directly from the
1180      editor command loop.)  If the containing function was called by
1181      Lisp evaluation (or with `apply' or `funcall'), then it was not
1182      called interactively.
1183
1184      The most common use of `interactive-p' is for deciding whether to
1185      print an informative message.  As a special exception,
1186      `interactive-p' returns `nil' whenever a keyboard macro is being
1187      run.  This is to suppress the informative messages and speed
1188      execution of the macro.
1189
1190      For example:
1191
1192           (defun foo ()
1193             (interactive)
1194             (and (interactive-p)
1195                  (message "foo")))
1196                => foo
1197           
1198           (defun bar ()
1199             (interactive)
1200             (setq foobar (list (foo) (interactive-p))))
1201                => bar
1202           
1203           ;; Type `M-x foo'.
1204                -| foo
1205           
1206           ;; Type `M-x bar'.
1207           ;; This does not print anything.
1208           
1209           foobar
1210                => (nil t)
1211