[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24. Minibuffers

A minibuffer is a special buffer that XEmacs commands use to read arguments more complicated than the single numeric prefix argument. These arguments include file names, buffer names, and command names (as in M-x). The minibuffer is displayed on the bottom line of the frame, in the same place as the echo area, but only while it is in use for reading an argument.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.1 Introduction to Minibuffers

In most ways, a minibuffer is a normal XEmacs buffer. Most operations within a buffer, such as editing commands, work normally in a minibuffer. However, many operations for managing buffers do not apply to minibuffers. The name of a minibuffer always has the form ‘ *Minibuf-number’, and it cannot be changed. Minibuffers are displayed only in special windows used only for minibuffers; these windows always appear at the bottom of a frame. (Sometimes frames have no minibuffer window, and sometimes a special kind of frame contains nothing but a minibuffer window; see Minibuffers and Frames.)

The minibuffer’s window is normally a single line. You can resize it temporarily with the window sizing commands; it reverts to its normal size when the minibuffer is exited. You can resize it permanently by using the window sizing commands in the frame’s other window, when the minibuffer is not active. If the frame contains just a minibuffer, you can change the minibuffer’s size by changing the frame’s size.

If a command uses a minibuffer while there is an active minibuffer, this is called a recursive minibuffer. The first minibuffer is named ‘ *Minibuf-0*’. Recursive minibuffers are named by incrementing the number at the end of the name. (The names begin with a space so that they won’t show up in normal buffer lists.) Of several recursive minibuffers, the innermost (or most recently entered) is the active minibuffer. We usually call this “the” minibuffer. You can permit or forbid recursive minibuffers by setting the variable enable-recursive-minibuffers.

Like other buffers, a minibuffer may use any of several local keymaps (see section Keymaps); these contain various exit commands and in some cases completion commands (see section Completion).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.2 Reading Text Strings with the Minibuffer

Most often, the minibuffer is used to read text as a string. It can also be used to read a Lisp object in textual form. The most basic primitive for minibuffer input is read-from-minibuffer; it can do either one.

In most cases, you should not call minibuffer input functions in the middle of a Lisp function. Instead, do all minibuffer input as part of reading the arguments for a command, in the interactive spec. See section Defining Commands.

Function: read-from-minibuffer prompt-string &optional initial-contents keymap read hist abbrev-table default

This function is the most general way to get input through the minibuffer. By default, it accepts arbitrary text and returns it as a string; however, if read is non-nil, then it uses read to convert the text into a Lisp object (see section Input Functions).

The first thing this function does is to activate a minibuffer and display it with prompt-string as the prompt. This value must be a string.

Then, if initial-contents is a string, read-from-minibuffer inserts it into the minibuffer, leaving point at the end. The minibuffer appears with this text as its contents.

The value of initial-contents may also be a cons cell of the form (string . position). This means to insert string in the minibuffer but put point position characters from the beginning, rather than at the end.

When the user types a command to exit the minibuffer, read-from-minibuffer constructs the return value from the text in the minibuffer. Normally it returns a string containing that text. However, if read is non-nil, read-from-minibuffer reads the text and returns the resulting Lisp object, unevaluated. (See section Input Functions, for information about reading.)

The argument default specifies a default value to make available through the history commands. It should be a string, or nil.

If keymap is non-nil, that keymap is the local keymap to use in the minibuffer. If keymap is omitted or nil, the value of minibuffer-local-map is used as the keymap. Specifying a keymap is the most important way to customize the minibuffer for various applications such as completion.

The argument abbrev-table specifies local-abbrev-table in the minibuffer (see section Standard Abbrev Tables).

The argument hist specifies which history list variable to use for saving the input and for history commands used in the minibuffer. It defaults to minibuffer-history. See section Minibuffer History.

When the user types a command to exit the minibuffer, read-from-minibuffer uses the text in the minibuffer to produce its return value. Normally it simply makes a string containing that text. However, if read is non-nil, read-from-minibuffer reads the text and returns the resulting Lisp object, unevaluated. (See section Input Functions, for information about reading.)

Usage note: The initial-contents argument and the default argument are two alternative features for more or less the same job. It does not make sense to use both features in a single call to read-from-minibuffer. In general, we recommend using default, since this permits the user to insert the default value when it is wanted, but does not burden the user with deleting it from the minibuffer on other occasions. However, if user is supposed to edit default value, initial-contents may be preferred.

Function: read-string prompt &optional initial history default-value

This function reads a string from the minibuffer and returns it. The arguments prompt and initial are used as in read-from-minibuffer. The keymap used is minibuffer-local-map.

The optional argument history, if non-nil, specifies a history list and optionally the initial position in the list. The optional argument default-value specifies a default value to return if the user enters null input; it should be a string.

This function is a simplified interface to the read-from-minibuffer function:

 
(read-string prompt initial history default)
≡
(read-from-minibuffer prompt initial nil nil
                      history nil default)))
Variable: minibuffer-local-map

This is the default local keymap for reading from the minibuffer. By default, it makes the following bindings:

C-j

exit-minibuffer

<RET>

exit-minibuffer

C-g

abort-recursive-edit

M-n

next-history-element

M-p

previous-history-element

M-r

next-matching-history-element

M-s

previous-matching-history-element


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.3 Reading Lisp Objects with the Minibuffer

This section describes functions for reading Lisp objects with the minibuffer.

Function: read-expression prompt &optional initial history default-value

This function reads a Lisp object using the minibuffer, and returns it without evaluating it. The arguments prompt and initial are used as in read-from-minibuffer.

The optional argument history, if non-nil, specifies a history list and optionally the initial position in the list. The optional argument default-value specifies a default value to return if the user enters null input; it should be a string.

This is a simplified interface to the read-from-minibuffer function:

 
(read-expression prompt initial history default-value)
≡
(read-from-minibuffer prompt initial nil t
                      history nil default-value)

Here is an example in which we supply the string "(testing)" as initial input:

 
(read-expression
 "Enter an expression: " (format "%s" '(testing)))

;; Here is how the minibuffer is displayed:
---------- Buffer: Minibuffer ----------
Enter an expression: (testing)∗
---------- Buffer: Minibuffer ----------

The user can type <RET> immediately to use the initial input as a default, or can edit the input.

Function: read-minibuffer prompt &optional initial history default-value

This is a FSF Emacs compatible function. Use read-expression instead.

Function: eval-minibuffer prompt &optional initial history default-value

This function reads a Lisp expression using the minibuffer, evaluates it, then returns the result. The arguments prompt and initial are used as in read-from-minibuffer.

The optional argument history, if non-nil, specifies a history list and optionally the initial position in the list. The optional argument default-value specifies a default value to return if the user enters null input; it should be a string.

This function simply evaluates the result of a call to read-expression:

 
(eval-minibuffer prompt initial)
≡
(eval (read-expression prompt initial))
Function: edit-and-eval-command prompt form &optional history

This function reads a Lisp expression in the minibuffer, and then evaluates it. The difference between this command and eval-minibuffer is that here the initial form is not optional and it is treated as a Lisp object to be converted to printed representation rather than as a string of text. It is printed with prin1, so if it is a string, double-quote characters (‘"’) appear in the initial text. See section Output Functions.

The first thing edit-and-eval-command does is to activate the minibuffer with prompt as the prompt. Then it inserts the printed representation of form in the minibuffer, and lets the user edit it. When the user exits the minibuffer, the edited text is read with read and then evaluated. The resulting value becomes the value of edit-and-eval-command.

In the following example, we offer the user an expression with initial text which is a valid form already:

 
(edit-and-eval-command "Please edit: " '(forward-word 1))

;; After evaluation of the preceding expression,
;;   the following appears in the minibuffer:
---------- Buffer: Minibuffer ----------
Please edit: (forward-word 1)∗
---------- Buffer: Minibuffer ----------

Typing <RET> right away would exit the minibuffer and evaluate the expression, thus moving point forward one word. edit-and-eval-command returns t in this example.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.4 Minibuffer History

A minibuffer history list records previous minibuffer inputs so the user can reuse them conveniently. A history list is actually a symbol, not a list; it is a variable whose value is a list of strings (previous inputs), most recent first.

There are many separate history lists, used for different kinds of inputs. It’s the Lisp programmer’s job to specify the right history list for each use of the minibuffer.

The basic minibuffer input functions read-from-minibuffer and completing-read both accept an optional argument named hist which is how you specify the history list. Here are the possible values:

variable

Use variable (a symbol) as the history list.

(variable . startpos)

Use variable (a symbol) as the history list, and assume that the initial history position is startpos (an integer, counting from zero which specifies the most recent element of the history).

If you specify startpos, then you should also specify that element of the history as the initial minibuffer contents, for consistency.

If you don’t specify hist, then the default history list minibuffer-history is used. For other standard history lists, see below. You can also create your own history list variable; just initialize it to nil before the first use.

Both read-from-minibuffer and completing-read add new elements to the history list automatically, and provide commands to allow the user to reuse items on the list. The only thing your program needs to do to use a history list is to initialize it and to pass its name to the input functions when you wish. But it is safe to modify the list by hand when the minibuffer input functions are not using it.

Here are some of the standard minibuffer history list variables:

Variable: minibuffer-history

The default history list for minibuffer history input.

Variable: query-replace-history

A history list for arguments to query-replace (and similar arguments to other commands).

Variable: file-name-history

A history list for file name arguments.

Variable: regexp-history

A history list for regular expression arguments.

Variable: extended-command-history

A history list for arguments that are names of extended commands.

Variable: shell-command-history

A history list for arguments that are shell commands.

Variable: read-expression-history

A history list for arguments that are Lisp expressions to evaluate.

Variable: Info-minibuffer-history

A history list for Info mode’s minibuffer.

Variable: Manual-page-minibuffer-history

A history list for manual-entry.

There are many other minibuffer history lists, defined by various libraries. An M-x apropos search for ‘history’ should prove fruitful in discovering them.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.5 Completion

Completion is a feature that fills in the rest of a name starting from an abbreviation for it. Completion works by comparing the user’s input against a list of valid names and determining how much of the name is determined uniquely by what the user has typed. For example, when you type C-x b (switch-to-buffer) and then type the first few letters of the name of the buffer to which you wish to switch, and then type <TAB> (minibuffer-complete), Emacs extends the name as far as it can.

Standard XEmacs commands offer completion for names of symbols, files, buffers, and processes; with the functions in this section, you can implement completion for other kinds of names.

The try-completion function is the basic primitive for completion: it returns the longest determined completion of a given initial string, with a given set of strings to match against.

The function completing-read provides a higher-level interface for completion. A call to completing-read specifies how to determine the list of valid names. The function then activates the minibuffer with a local keymap that binds a few keys to commands useful for completion. Other functions provide convenient simple interfaces for reading certain kinds of names with completion.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.5.1 Basic Completion Functions

The two functions try-completion and all-completions have nothing in themselves to do with minibuffers. We describe them in this chapter so as to keep them near the higher-level completion features that do use the minibuffer.

Function: try-completion string collection &optional predicate

This function returns the longest common prefix of all possible completions of string in collection. The value of collection must be an alist, an obarray, or a function that implements a virtual set of strings (see below).

Completion compares string against each of the permissible completions specified by collection; if the beginning of the permissible completion equals string, it matches. If no permissible completions match, try-completion returns nil. If only one permissible completion matches, and the match is exact, then try-completion returns t. Otherwise, the value is the longest initial sequence common to all the permissible completions that match.

If collection is an alist (see section Association Lists), the CARs of the alist elements form the set of permissible completions.

If collection is an obarray (see section Creating and Interning Symbols), the names of all symbols in the obarray form the set of permissible completions. The global variable obarray holds an obarray containing the names of all interned Lisp symbols.

Note that the only valid way to make a new obarray is to create it empty and then add symbols to it one by one using intern. Also, you cannot intern a given symbol in more than one obarray.

If the argument predicate is non-nil, then it must be a function of one argument. It is used to test each possible match, and the match is accepted only if predicate returns non-nil. The argument given to predicate is either a cons cell from the alist (the CAR of which is a string) or else it is a symbol (not a symbol name) from the obarray.

You can also use a symbol that is a function as collection. Then the function is solely responsible for performing completion; try-completion returns whatever this function returns. The function is called with three arguments: string, predicate and nil. (The reason for the third argument is so that the same function can be used in all-completions and do the appropriate thing in either case.) See section Programmed Completion.

In the first of the following examples, the string ‘foo’ is matched by three of the alist CARs. All of the matches begin with the characters ‘fooba’, so that is the result. In the second example, there is only one possible match, and it is exact, so the value is t.

 
(try-completion
 "foo"
 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
     ⇒ "fooba"
(try-completion "foo" '(("barfoo" 2) ("foo" 3)))
     ⇒ t

In the following example, numerous symbols begin with the characters ‘forw’, and all of them begin with the word ‘forward’. In most of the symbols, this is followed with a ‘-’, but not in all, so no more than ‘forward’ can be completed.

 
(try-completion "forw" obarray)
     ⇒ "forward"

Finally, in the following example, only two of the three possible matches pass the predicate test (the string ‘foobaz’ is too short). Both of those begin with the string ‘foobar’.

 
(defun test (s)
  (> (length (car s)) 6))
     ⇒ test
(try-completion
 "foo"
 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
 'test)
     ⇒ "foobar"
Function: all-completions string collection &optional predicate

This function returns a list of all possible completions of string. The arguments to this function are the same as those of try-completion.

If collection is a function, it is called with three arguments: string, predicate and t; then all-completions returns whatever the function returns. See section Programmed Completion.

Here is an example, using the function test shown in the example for try-completion:

 
(defun test (s)
  (> (length (car s)) 6))
     ⇒ test
(all-completions
 "foo"
 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
 'test)
     ⇒ ("foobar1" "foobar2")
Variable: completion-ignore-case

If the value of this variable is non-nil, XEmacs does not consider case significant in completion.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.5.2 Completion and the Minibuffer

This section describes the basic interface for reading from the minibuffer with completion.

Function: completing-read prompt collection &optional predicate require-match initial hist default

This function reads a string in the minibuffer, assisting the user by providing completion. It activates the minibuffer with prompt prompt, which must be a string. If initial is non-nil, completing-read inserts it into the minibuffer as part of the input. Then it allows the user to edit the input, providing several commands to attempt completion.

The actual completion is done by passing collection and predicate to the function try-completion. This happens in certain commands bound in the local keymaps used for completion.

If require-match is t, the usual minibuffer exit commands won’t exit unless the input completes to an element of collection. If require-match is neither nil nor t, then the exit commands won’t exit unless the input typed is itself an element of collection. If require-match is nil, the exit commands work regardless of the input in the minibuffer.

However, empty input is always permitted, regardless of the value of require-match; in that case, completing-read returns default. The value of default (if non-nil) is also available to the user through the history commands.

The user can exit with null input by typing <RET> with an empty minibuffer. Then completing-read returns "". This is how the user requests whatever default the command uses for the value being read. The user can return using <RET> in this way regardless of the value of require-match, and regardless of whether the empty string is included in collection.

The function completing-read works by calling read-expression. It uses minibuffer-local-completion-map as the keymap if require-match is nil, and uses minibuffer-local-must-match-map if require-match is non-nil. See section Minibuffer Commands That Do Completion.

The argument hist specifies which history list variable to use for saving the input and for minibuffer history commands. It defaults to minibuffer-history. See section Minibuffer History.

Completion ignores case when comparing the input against the possible matches, if the built-in variable completion-ignore-case is non-nil. See section Basic Completion Functions.

Here’s an example of using completing-read:

 
(completing-read
 "Complete a foo: "
 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
 nil t "fo")
;; After evaluation of the preceding expression,
;;   the following appears in the minibuffer:

---------- Buffer: Minibuffer ----------
Complete a foo: fo∗
---------- Buffer: Minibuffer ----------

If the user then types <DEL> <DEL> b <RET>, completing-read returns barfoo.

The completing-read function binds three variables to pass information to the commands that actually do completion. These variables are minibuffer-completion-table, minibuffer-completion-predicate and minibuffer-completion-confirm. For more information about them, see Minibuffer Commands That Do Completion.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.5.3 Minibuffer Commands That Do Completion

This section describes the keymaps, commands and user options used in the minibuffer to do completion.

Variable: minibuffer-local-completion-map

completing-read uses this value as the local keymap when an exact match of one of the completions is not required. By default, this keymap makes the following bindings:

?

minibuffer-completion-help

<SPC>

minibuffer-complete-word

<TAB>

minibuffer-complete

with other characters bound as in minibuffer-local-map (see section Reading Text Strings with the Minibuffer).

Variable: minibuffer-local-must-match-map

completing-read uses this value as the local keymap when an exact match of one of the completions is required. Therefore, no keys are bound to exit-minibuffer, the command that exits the minibuffer unconditionally. By default, this keymap makes the following bindings:

?

minibuffer-completion-help

<SPC>

minibuffer-complete-word

<TAB>

minibuffer-complete

C-j

minibuffer-complete-and-exit

<RET>

minibuffer-complete-and-exit

with other characters bound as in minibuffer-local-map.

Variable: minibuffer-completion-table

The value of this variable is the alist or obarray used for completion in the minibuffer. This is the global variable that contains what completing-read passes to try-completion. It is used by minibuffer completion commands such as minibuffer-complete-word.

Variable: minibuffer-completion-predicate

This variable’s value is the predicate that completing-read passes to try-completion. The variable is also used by the other minibuffer completion functions.

Command: minibuffer-complete-word

This function completes the minibuffer contents by at most a single word. Even if the minibuffer contents have only one completion, minibuffer-complete-word does not add any characters beyond the first character that is not a word constituent. See section Syntax Tables.

Command: minibuffer-complete

This function completes the minibuffer contents as far as possible.

Command: minibuffer-complete-and-exit

This function completes the minibuffer contents, and exits if confirmation is not required, i.e., if minibuffer-completion-confirm is nil. If confirmation is required, it is given by repeating this command immediately—the command is programmed to work without confirmation when run twice in succession.

Variable: minibuffer-completion-confirm

When the value of this variable is non-nil, XEmacs asks for confirmation of a completion before exiting the minibuffer. The function minibuffer-complete-and-exit checks the value of this variable before it exits.

Command: minibuffer-completion-help

This function creates a list of the possible completions of the current minibuffer contents. It works by calling all-completions using the value of the variable minibuffer-completion-table as the collection argument, and the value of minibuffer-completion-predicate as the predicate argument. The list of completions is displayed as text in a buffer named ‘*Completions*’.

Function: display-completion-list completions &rest cl-keys

This function displays completions to the stream in standard-output, usually a buffer. (See section Reading and Printing Lisp Objects, for more information about streams.) The argument completions is normally a list of completions just returned by all-completions, but it does not have to be. Each element may be a symbol or a string, either of which is simply printed, or a list of two strings, which is printed as if the strings were concatenated.

This function is called by minibuffer-completion-help. The most common way to use it is together with with-output-to-temp-buffer, like this:

 
(with-output-to-temp-buffer "*Completions*"
  (display-completion-list
    (all-completions (buffer-string) my-alist)))
User Option: completion-auto-help

If this variable is non-nil, the completion commands automatically display a list of possible completions whenever nothing can be completed because the next character is not uniquely determined.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.5.4 High-Level Completion Functions

This section describes the higher-level convenient functions for reading certain sorts of names with completion.

In most cases, you should not call these functions in the middle of a Lisp function. When possible, do all minibuffer input as part of reading the arguments for a command, in the interactive spec. See section Defining Commands.

Function: read-buffer prompt &optional default existing

This function reads the name of a buffer and returns it as a string. The argument default is the default name to use, the value to return if the user exits with an empty minibuffer. If non-nil, it should be a string or a buffer. It is mentioned in the prompt, but is not inserted in the minibuffer as initial input.

If existing is non-nil, then the name specified must be that of an existing buffer. The usual commands to exit the minibuffer do not exit if the text is not valid, and <RET> does completion to attempt to find a valid name. (However, default is not checked for validity; it is returned, whatever it is, if the user exits with the minibuffer empty.)

In the following example, the user enters ‘minibuffer.t’, and then types <RET>. The argument existing is t, and the only buffer name starting with the given input is ‘minibuffer.texi’, so that name is the value.

 
(read-buffer "Buffer name? " "foo" t)
;; After evaluation of the preceding expression,
;;   the following prompt appears,
;;   with an empty minibuffer:
---------- Buffer: Minibuffer ----------
Buffer name? (default foo) ∗
---------- Buffer: Minibuffer ----------
;; The user types minibuffer.t <RET>.
     ⇒ "minibuffer.texi"
Function: read-command prompt &optional default-value

This function reads the name of a command and returns it as a Lisp symbol. The argument prompt is used as in read-from-minibuffer. Recall that a command is anything for which commandp returns t, and a command name is a symbol for which commandp returns t. See section Interactive Call.

The argument default-value specifies what to return if the user enters null input. It can be a symbol or a string; if it is a string, read-command interns it before returning it. If default is nil, that means no default has been specified; then if the user enters null input, the return value is nil.

 
(read-command "Command name? ")

;; After evaluation of the preceding expression,
;;   the following prompt appears with an empty minibuffer:
---------- Buffer: Minibuffer ----------
Command name?
---------- Buffer: Minibuffer ----------

If the user types forward-c <RET>, then this function returns forward-char.

The read-command function is a simplified interface to the function completing-read. It uses the variable obarray so as to complete in the set of extant Lisp symbols, and it uses the commandp predicate so as to accept only command names:

 
(read-command prompt)
≡
(intern (completing-read prompt obarray
                         'commandp t nil))
Function: read-variable prompt &optional default-value

This function reads the name of a user variable and returns it as a symbol.

The argument default-value specifies what to return if the user enters null input. It can be a symbol or a string; if it is a string, read-variable interns it before returning it. If default-value is nil, that means no default has been specified; then if the user enters null input, the return value is nil.

 
(read-variable "Variable name? ")

;; After evaluation of the preceding expression,
;;   the following prompt appears,
;;   with an empty minibuffer:
---------- Buffer: Minibuffer ----------
Variable name? ∗
---------- Buffer: Minibuffer ----------

If the user then types fill-p <RET>, read-variable returns fill-prefix.

This function is similar to read-command, but uses the predicate user-variable-p instead of commandp:

 
(read-variable prompt)
≡
(intern
 (completing-read prompt obarray
                  'user-variable-p t nil))

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.5.5 Reading File Names

Here is another high-level completion function, designed for reading a file name. It provides special features including automatic insertion of the default directory.

Function: read-file-name prompt &optional directory default existing initial history

This function reads a file name in the minibuffer, prompting with prompt and providing completion. If default is non-nil, then the function returns default if the user just types <RET>. default is not checked for validity; it is returned, whatever it is, if the user exits with the minibuffer empty.

If existing is non-nil, then the user must specify the name of an existing file; <RET> performs completion to make the name valid if possible, and then refuses to exit if it is not valid. If the value of existing is neither nil nor t, then <RET> also requires confirmation after completion. If existing is nil, then the name of a nonexistent file is acceptable.

The argument directory specifies the directory to use for completion of relative file names. If insert-default-directory is non-nil, directory is also inserted in the minibuffer as initial input. It defaults to the current buffer’s value of default-directory.

If you specify initial, that is an initial file name to insert in the buffer (after directory, if that is inserted). In this case, point goes at the beginning of initial. The default for initial is nil—don’t insert any file name. To see what initial does, try the command C-x C-v.

Here is an example:

 
(read-file-name "The file is ")

;; After evaluation of the preceding expression,
;;   the following appears in the minibuffer:
---------- Buffer: Minibuffer ----------
The file is /gp/gnu/elisp/∗
---------- Buffer: Minibuffer ----------

Typing manual <TAB> results in the following:

 
---------- Buffer: Minibuffer ----------
The file is /gp/gnu/elisp/manual.texi∗
---------- Buffer: Minibuffer ----------

If the user types <RET>, read-file-name returns the file name as the string "/gp/gnu/elisp/manual.texi".

User Option: insert-default-directory

This variable is used by read-file-name. Its value controls whether read-file-name starts by placing the name of the default directory in the minibuffer, plus the initial file name if any. If the value of this variable is nil, then read-file-name does not place any initial input in the minibuffer (unless you specify initial input with the initial argument). In that case, the default directory is still used for completion of relative file names, but is not displayed.

For example:

 
;; Here the minibuffer starts out with the default directory.
(let ((insert-default-directory t))
  (read-file-name "The file is "))
---------- Buffer: Minibuffer ----------
The file is ~lewis/manual/∗
---------- Buffer: Minibuffer ----------
;; Here the minibuffer is empty and only the prompt
;;   appears on its line.
(let ((insert-default-directory nil))
  (read-file-name "The file is "))
---------- Buffer: Minibuffer ----------
The file is ∗
---------- Buffer: Minibuffer ----------

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.5.6 Programmed Completion

Sometimes it is not possible to create an alist or an obarray containing all the intended possible completions. In such a case, you can supply your own function to compute the completion of a given string. This is called programmed completion.

To use this feature, pass a symbol with a function definition as the collection argument to completing-read. The function completing-read arranges to pass your completion function along to try-completion and all-completions, which will then let your function do all the work.

The completion function should accept three arguments:

There are three flag values for three operations:

It would be consistent and clean for completion functions to allow lambda expressions (lists that are functions) as well as function symbols as collection, but this is impossible. Lists as completion tables are already assigned another meaning—as alists. It would be unreliable to fail to handle an alist normally because it is also a possible function. So you must arrange for any function you wish to use for completion to be encapsulated in a symbol.

Emacs uses programmed completion when completing file names. See section File Name Completion.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.6 Yes-or-No Queries

This section describes functions used to ask the user a yes-or-no question. The function y-or-n-p can be answered with a single character; it is useful for questions where an inadvertent wrong answer will not have serious consequences. yes-or-no-p is suitable for more momentous questions, since it requires three or four characters to answer. Variations of these functions can be used to ask a yes-or-no question using a dialog box, or optionally using one.

If either of these functions is called in a command that was invoked using the mouse, then it uses a dialog box or pop-up menu to ask the question. Otherwise, it uses keyboard input.

Strictly speaking, yes-or-no-p uses the minibuffer and y-or-n-p does not; but it seems best to describe them together.

Function: y-or-n-p prompt

This function asks the user a question, expecting input in the echo area. It returns t if the user types y, nil if the user types n. This function also accepts <SPC> to mean yes and <DEL> to mean no. It accepts C-] to mean “quit”, like C-g, because the question might look like a minibuffer and for that reason the user might try to use C-] to get out. The answer is a single character, with no <RET> needed to terminate it. Upper and lower case are equivalent.

“Asking the question” means printing prompt in the echo area, followed by the string ‘(y or n) ’. If the input is not one of the expected answers (y, n, <SPC>, <DEL>, or something that quits), the function responds ‘Please answer y or n.’, and repeats the request.

This function does not actually use the minibuffer, since it does not allow editing of the answer. It actually uses the echo area (see section The Echo Area), which uses the same screen space as the minibuffer. The cursor moves to the echo area while the question is being asked.

The answers and their meanings, even ‘y’ and ‘n’, are not hardwired. The keymap query-replace-map specifies them. See section Search and Replace.

In the following example, the user first types q, which is invalid. At the next prompt the user types y.

 
(y-or-n-p "Do you need a lift? ")

;; After evaluation of the preceding expression,
;;   the following prompt appears in the echo area:
---------- Echo area ----------
Do you need a lift? (y or n)
---------- Echo area ----------
;; If the user then types q, the following appears:

---------- Echo area ----------
Please answer y or n.  Do you need a lift? (y or n)
---------- Echo area ----------
;; When the user types a valid answer,
;;   it is displayed after the question:

---------- Echo area ----------
Do you need a lift? (y or n) y
---------- Echo area ----------

We show successive lines of echo area messages, but only one actually appears on the screen at a time.

Function: yes-or-no-p prompt

This function asks the user a question, expecting input in the minibuffer. It returns t if the user enters ‘yes’, nil if the user types ‘no’. The user must type <RET> to finalize the response. Upper and lower case are equivalent.

yes-or-no-p starts by displaying prompt in the echo area, followed by ‘(yes or no) ’. The user must type one of the expected responses; otherwise, the function responds ‘Please answer yes or no.’, waits about two seconds and repeats the request.

yes-or-no-p requires more work from the user than y-or-n-p and is appropriate for more crucial decisions.

Here is an example:

 
(yes-or-no-p "Do you really want to remove everything? ")

;; After evaluation of the preceding expression,
;;   the following prompt appears,
;;   with an empty minibuffer:
---------- Buffer: minibuffer ----------
Do you really want to remove everything? (yes or no)
---------- Buffer: minibuffer ----------

If the user first types y <RET>, which is invalid because this function demands the entire word ‘yes’, it responds by displaying these prompts, with a brief pause between them:

 
---------- Buffer: minibuffer ----------
Please answer yes or no.
Do you really want to remove everything? (yes or no)
---------- Buffer: minibuffer ----------
Function: yes-or-no-p-dialog-box prompt

This function asks the user a “y or n” question with a popup dialog box. It returns t if the answer is “yes”. prompt is the string to display to ask the question.

The following functions ask a question either in the minibuffer or a dialog box, depending on whether the last user event (which presumably invoked this command) was a keyboard or mouse event. When XEmacs is running on a window system, the functions y-or-n-p and yes-or-no-p are replaced with the following functions, so that menu items bring up dialog boxes instead of minibuffer questions.

Function: y-or-n-p-maybe-dialog-box prompt

This function asks user a “y or n” question, using either a dialog box or the minibuffer, as appropriate.

Function: yes-or-no-p-maybe-dialog-box prompt

This function asks user a “yes or no” question, using either a dialog box or the minibuffer, as appropriate.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.7 Asking Multiple Y-or-N Questions

When you have a series of similar questions to ask, such as “Do you want to save this buffer” for each buffer in turn, you should use map-y-or-n-p to ask the collection of questions, rather than asking each question individually. This gives the user certain convenient facilities such as the ability to answer the whole series at once.

Function: map-y-or-n-p prompter actor list &optional help action-alist

This function, new in Emacs 19, asks the user a series of questions, reading a single-character answer in the echo area for each one.

The value of list specifies the objects to ask questions about. It should be either a list of objects or a generator function. If it is a function, it should expect no arguments, and should return either the next object to ask about, or nil meaning stop asking questions.

The argument prompter specifies how to ask each question. If prompter is a string, the question text is computed like this:

 
(format prompter object)

where object is the next object to ask about (as obtained from list).

If not a string, prompter should be a function of one argument (the next object to ask about) and should return the question text. If the value is a string, that is the question to ask the user. The function can also return t meaning do act on this object (and don’t ask the user), or nil meaning ignore this object (and don’t ask the user).

The argument actor says how to act on the answers that the user gives. It should be a function of one argument, and it is called with each object that the user says yes for. Its argument is always an object obtained from list.

If the argument help is given, it should be a list of this form:

 
(singular plural action)

where singular is a string containing a singular noun that describes the objects conceptually being acted on, plural is the corresponding plural noun, and action is a transitive verb describing what actor does.

If you don’t specify help, the default is ("object" "objects" "act on").

Each time a question is asked, the user may enter y, Y, or <SPC> to act on that object; n, N, or <DEL> to skip that object; ! to act on all following objects; <ESC> or q to exit (skip all following objects); . (period) to act on the current object and then exit; or C-h to get help. These are the same answers that query-replace accepts. The keymap query-replace-map defines their meaning for map-y-or-n-p as well as for query-replace; see Search and Replace.

You can use action-alist to specify additional possible answers and what they mean. It is an alist of elements of the form (char function help), each of which defines one additional answer. In this element, char is a character (the answer); function is a function of one argument (an object from list); help is a string.

When the user responds with char, map-y-or-n-p calls function. If it returns non-nil, the object is considered “acted upon”, and map-y-or-n-p advances to the next object in list. If it returns nil, the prompt is repeated for the same object.

If map-y-or-n-p is called in a command that was invoked using the mouse—more precisely, if last-nonmenu-event (see section Information from the Command Loop) is either nil or a list—then it uses a dialog box or pop-up menu to ask the question. In this case, it does not use keyboard input or the echo area. You can force use of the mouse or use of keyboard input by binding last-nonmenu-event to a suitable value around the call.

The return value of map-y-or-n-p is the number of objects acted on.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

24.8 Reading a Password

To read a password to pass to another program, you can use the function read-passwd.

Function: read-passwd prompt &optional confirm default

This function reads a password, prompting with prompt. It does not echo the password as the user types it; instead, it echoes ‘.’ for each character in the password.

The optional argument confirm, if non-nil, says to read the password twice and insist it must be the same both times. If it isn’t the same, the user has to type it over and over until the last two times match.

The optional argument default specifies the default password to return if the user enters empty input. It is translated to ‘.’ and inserted in the minibuffer. If default is nil, then read-passwd returns the null string in that case.

User Option: passwd-invert-frame-when-keyboard-grabbed

If non-nil, swap the foreground and background colors of all faces while reading a password. Default values is t, unless feature infodock is provided.

User Option: passwd-echo

This specifies the character echoed when typing a password. When nil, nothing is echoed.


This is not implemented in XEmacs because it is a kludge. If you want to explicitly set the value of enable-recursive-minibuffers in this fashion, just use an evaluated interactive spec and bind enable-recursive-minibuffers while reading from the minibuffer. See the definition of next-matching-history-element in ‘lisp/minibuf.el’.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Aidan Kehoe on December 27, 2016 using texi2html 1.82.