Minibuffers
***********

A minibuffer is a special buffer that Emacs 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 (see The Echo Area), but only while it is in use for reading an argument.

Introduction to Minibuffers
===========================

In most ways, a minibuffer is a normal Emacs 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 text in the minibuffer always starts with the prompt string, the text that was specified by the program that is using the minibuffer to tell the user what sort of input to type.  This text is marked read-only so you won't accidentally delete or change it.  It is also marked as a field (see Fields), so that certain motion functions, including beginning-of-line, forward-word, forward-sentence, and forward-paragraph, stop at the boundary between the prompt and the actual text.  (In older Emacs versions, the prompt was displayed using a special mechanism and was not part of the buffer contents.)

The minibuffer's window is normally a single line; it grows automatically if necessary if the contents require more space.  You can explicitly 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.

Use of the minibuffer reads input events, and that alters the values of variables such as this-command and last-command (see Command Loop Info).  Your program should bind them around the code that uses the minibuffer, if you do not want that to change them.

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 or by putting properties of that name on command symbols (see Recursive Mini).

Like other buffers, a minibuffer uses a local keymap (see Keymaps) to specify special key bindings.  The function that invokes the minibuffer also sets up its local map according to the job to be done.  See Text from Minibuffer, for the non-completion minibuffer local maps.  See Completion Commands, for the minibuffer local maps for completion.

When Emacs is running in batch mode, any request to read from the minibuffer actually reads a line from the standard input descriptor that was supplied when Emacs was started.

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.  There are also specialized commands for reading commands, variables, file names, etc. (see Completion).

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 specification.  See Defining Commands.

 -- Function: read-from-minibuffer prompt-string &optional initial-contents keymap read hist default inherit-input-method
     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 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 the user can edit text in the minibuffer.

     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 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 non-nil, the user can access it using next-history-element, usually bound in the minibuffer to M-n.  If READ is non-nil, then DEFAULT is also used as the input to read, if the user enters empty input. (If READ is non-nil and DEFAULT is nil, empty input results in an end-of-file error.)  However, in the usual case (where READ is nil), read-from-minibuffer ignores DEFAULT when the user enters empty input and returns an empty string, "".  In this respect, it is different from all the other minibuffer input functions in this chapter.

     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 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 Minibuffer History.

     If the variable minibuffer-allow-text-properties is non-nil, then the string which is returned includes whatever text properties were present in the minibuffer.  Otherwise all the text properties are stripped when the value is returned.

     If the argument INHERIT-INPUT-METHOD is non-nil, then the minibuffer inherits the current input method (see Input Methods) and the setting of enable-multibyte-characters (see Text Representations) from whichever buffer was current before entering the minibuffer.

     Use of INITIAL-CONTENTS is mostly deprecated; we recommend using a non-nil value only in conjunction with specifying a cons cell for HIST.  See Initial Input.

 -- Function: read-string prompt &optional initial history default inherit-input-method
     This function reads a string from the minibuffer and returns it.  The arguments PROMPT, INITIAL, HISTORY and INHERIT-INPUT-METHOD are used as in read-from-minibuffer. The keymap used is minibuffer-local-map.

     The optional argument DEFAULT is used as in read-from-minibuffer, except that, if non-nil, it also specifies a default value to return if the user enters null input.  As in read-from-minibuffer it should be a string, or nil, which is equivalent to an empty string.

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

          (read-string PROMPT INITIAL HISTORY DEFAULT INHERIT)
          ==
          (let ((value
                 (read-from-minibuffer PROMPT INITIAL nil nil
                                       HISTORY DEFAULT INHERIT)))
            (if (and (equal value "") DEFAULT)
                DEFAULT
              value))

 -- Variable: minibuffer-allow-text-properties
     If this variable is nil, then read-from-minibuffer strips all text properties from the minibuffer input before returning it. This variable also affects read-string.  However, read-no-blanks-input (see below), as well as read-minibuffer and related functions (see Reading Lisp Objects With the Minibuffer), and all functions that do minibuffer input with completion, discard text properties unconditionally, regardless of the value of this variable.

 -- 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
     DOWN
          next-history-element

     M-p
     UP
          previous-history-element

     M-s
          next-matching-history-element

     M-r
          previous-matching-history-element

 -- Function: read-no-blanks-input prompt &optional initial inherit-input-method
     This function reads a string from the minibuffer, but does not allow whitespace characters as part of the input: instead, those characters terminate the input.  The arguments PROMPT, INITIAL, and INHERIT-INPUT-METHOD are used as in read-from-minibuffer.

     This is a simplified interface to the read-from-minibuffer function, and passes the value of the minibuffer-local-ns-map keymap as the KEYMAP argument for that function.  Since the keymap minibuffer-local-ns-map does not rebind C-q, it is possible to put a space into the string, by quoting it.

     This function discards text properties, regardless of the value of minibuffer-allow-text-properties.

          (read-no-blanks-input PROMPT INITIAL)
          ==
          (let (minibuffer-allow-text-properties)
            (read-from-minibuffer PROMPT INITIAL minibuffer-local-ns-map))

 -- Variable: minibuffer-local-ns-map
     This built-in variable is the keymap used as the minibuffer local keymap in the function read-no-blanks-input.  By default, it makes the following bindings, in addition to those of minibuffer-local-map:

     SPC
          exit-minibuffer

     TAB
          exit-minibuffer

     ?
          self-insert-and-exit

Reading Lisp Objects with the Minibuffer
========================================

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

 -- Function: read-minibuffer prompt &optional initial
     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.

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

          (read-minibuffer PROMPT INITIAL)
          ==
          (let (minibuffer-allow-text-properties)
            (read-from-minibuffer PROMPT INITIAL nil t))

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

          (read-minibuffer
           "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: eval-minibuffer prompt &optional initial
     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.

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

          (eval-minibuffer PROMPT INITIAL)
          ==
          (eval (read-minibuffer PROMPT INITIAL))

 -- Function: edit-and-eval-command prompt form
     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 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 nil in this example.

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.

You specify the history list with the optional HIST argument to either read-from-minibuffer or completing-read.  Here are the possible values for it:

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 (a nonnegative integer).

     Specifying 0 for STARTPOS is equivalent to just specifying the symbol VARIABLE.  previous-history-element will display the most recent element of the history list in the minibuffer.  If you specify a positive STARTPOS, the minibuffer history functions behave as if (elt VARIABLE (1- STARTPOS)) were the history element currently shown in the minibuffer.

     For consistency, you should also specify that element of the history as the initial minibuffer contents, using the INITIAL argument to the minibuffer input function (see Initial Input).

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.

Emacs functions that add a new element to a history list can also delete old elements if the list gets too long.  The variable history-length specifies the maximum length for most history lists.  To specify a different maximum length for a particular history list, put the length in the history-length property of the history list symbol.  The variable history-delete-duplicates specifies whether to delete duplicates in history.

 -- Function: add-to-history history-var newelt &optional maxelt keep-all
     This function adds a new element NEWELT, if it isn't the empty string, to the history list stored in the variable HISTORY-VAR, and returns the updated history list.  It limits the list length to the value of MAXELT (if non-nil) or history-length (described below).  The possible values of MAXELT have the same meaning as the values of history-length.

     Normally, add-to-history removes duplicate members from the history list if history-delete-duplicates is non-nil. However, if KEEP-ALL is non-nil, that says not to remove duplicates, and to add NEWELT to the list even if it is empty.

 -- Variable: history-add-new-input
     If the value of this variable is nil, standard functions that read from the minibuffer don't add new elements to the history list. This lets Lisp programs explicitly manage input history by using add-to-history.  By default, history-add-new-input is set to a non-nil value.

 -- Variable: history-length
     The value of this variable specifies the maximum length for all history lists that don't specify their own maximum lengths.  If the value is t, that means there no maximum (don't delete old elements).  The value of history-length property of the history list variable's symbol, if set, overrides this variable for that particular history list.

 -- Variable: history-delete-duplicates
     If the value of this variable is t, that means when adding a new history element, all previous identical elements are deleted.

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: buffer-name-history
     A history list for buffer-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.

Initial Input
=============

Several of the functions for minibuffer input have an argument called INITIAL or INITIAL-CONTENTS.  This is a mostly-deprecated feature for specifying that the minibuffer should start out with certain text, instead of empty as usual.

If INITIAL is a string, the minibuffer starts out containing the text of the string, with point at the end, when the user starts to edit the text.  If the user simply types RET to exit the minibuffer, it will use the initial input string to determine the value to return.

We discourage use of a non-nil value for INITIAL, because initial input is an intrusive interface. History lists and default values provide a much more convenient method to offer useful default inputs to the user.

There is just one situation where you should specify a string for an INITIAL argument.  This is when you specify a cons cell for the HIST or HISTORY argument.  See Minibuffer History.

INITIAL can also be a cons cell of the form (STRING . POSITION).  This means to insert STRING in the minibuffer but put point at POSITION within the string's text.

As a historical accident, POSITION was implemented inconsistently in different functions.  In completing-read, POSITION's value is interpreted as origin-zero; that is, a value of 0 means the beginning of the string, 1 means after the first character, etc.  In read-minibuffer, and the other non-completion minibuffer input functions that support this argument, 1 means the beginning of the string 2 means after the first character, etc.

Use of a cons cell as the value for INITIAL arguments is deprecated in user code.

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 Emacs 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.

Basic Completion Functions
--------------------------

The completion functions try-completion, all-completions and test-completion 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.

If you store a completion alist in a variable, you should mark the variable as ``risky'' with a non-nil risky-local-variable property.

 -- Function: try-completion string collection &optional predicate
     This function returns the longest common substring of all possible completions of STRING in COLLECTION.  The value of COLLECTION must be a list of strings or symbols, an alist, an obarray, a hash table, 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 Association Lists), the permissible completions are the elements of the alist that are either strings, symbols, or conses whose CAR is a string or symbol. Symbols are converted to strings using symbol-name. Other elements of the alist are ignored. (Remember that in Emacs Lisp, the elements of alists do not have to be conses.)  As all elements of the alist can be strings, this case actually includes lists of strings or symbols, even though we usually do not think of such lists as alists.

     If COLLECTION is an obarray (see Creating 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 COLLECTION is a hash table, then the keys that are strings are the possible completions.  Other keys are ignored.

     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 Programmed Completion.

     If the argument PREDICATE is non-nil, then it must be a function of one argument, unless COLLECTION is a hash table, in which case it should be a function of two arguments.  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 string or a cons cell (the CAR of which is a string) from the alist, or a symbol (not a symbol name) from the obarray.  If COLLECTION is a hash table, PREDICATE is called with two arguments, the string key and the associated value.

     In addition, to be acceptable, a completion must also match all the regular expressions in completion-regexp-list.  (Unless COLLECTION is a function, in which case that function has to handle completion-regexp-list itself.)

     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 nospace
     This function returns a list of all possible completions of STRING.  The arguments to this function (aside from NOSPACE) are the same as those of try-completion.  Also, this function uses completion-regexp-list in the same way that try-completion does.  The optional argument NOSPACE only matters if STRING is the empty string.  In that case, if NOSPACE is non-nil, completions that start with a space are ignored.

     If COLLECTION is a function, it is called with three arguments: STRING, PREDICATE and t; then all-completions returns whatever the function returns.  See 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")

 -- Function: test-completion string collection &optional predicate
     This function returns non-nil if STRING is a valid completion possibility specified by COLLECTION and PREDICATE.  The arguments are the same as in try-completion.  For instance, if COLLECTION is a list of strings, this is true if STRING appears in the list and PREDICATE is satisfied.

     test-completion uses completion-regexp-list in the same way that try-completion does.

     If PREDICATE is non-nil and if COLLECTION contains several strings that are equal to each other, as determined by compare-strings according to completion-ignore-case, then PREDICATE should accept either all or none of them. Otherwise, the return value of test-completion is essentially unpredictable.

     If COLLECTION is a function, it is called with three arguments, the values STRING, PREDICATE and lambda; whatever it returns, test-completion returns in turn.

 -- Variable: completion-ignore-case
     If the value of this variable is non-nil, Emacs does not consider case significant in completion.

 -- Variable: completion-regexp-list
     This is a list of regular expressions.  The completion functions only consider a completion acceptable if it matches all regular expressions in this list, with case-fold-search (see Searching and Case) bound to the value of completion-ignore-case.

 -- Macro: lazy-completion-table var fun
     This macro provides a way to initialize the variable VAR as a collection for completion in a lazy way, not computing its actual contents until they are first needed.  You use this macro to produce a value that you store in VAR.  The actual computation of the proper value is done the first time you do completion using VAR. It is done by calling FUN with no arguments.  The value FUN returns becomes the permanent value of VAR.

     Here is an example of use:

          (defvar foo (lazy-completion-table foo make-my-alist))

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 inherit-input-method
     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.

     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. Some of these commands also call test-completion.  Thus, if PREDICATE is non-nil, it should be compatible with COLLECTION and completion-ignore-case.  See Definition of test-completion.

     If REQUIRE-MATCH is nil, the exit commands work regardless of the input in the minibuffer.  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 already in the buffer matches an element of COLLECTION.

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

     The function completing-read 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 Completion Commands.

     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 Minibuffer History.

     The argument INITIAL is mostly deprecated; we recommend using a non-nil value only in conjunction with specifying a cons cell for HIST.  See Initial Input.  For default input, use DEFAULT instead.

     If the argument INHERIT-INPUT-METHOD is non-nil, then the minibuffer inherits the current input method (see Input Methods) and the setting of enable-multibyte-characters (see Text Representations) from whichever buffer was current before entering the minibuffer.

     If the built-in variable completion-ignore-case is non-nil, completion ignores case when comparing the input against the possible matches.  See Basic Completion.  In this mode of operation, PREDICATE must also ignore case, or you will get surprising results.

     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 variables to pass information to the commands that actually do completion. They are described in the following section.

Minibuffer Commands that Do Completion
--------------------------------------

This section describes the keymaps, commands and user options used in the minibuffer to do completion.  The description refers to the situation when Partial Completion mode is disabled (as it is by default).  When enabled, this minor mode uses its own alternatives to some of the commands described below.  See Completion Options (The GNU Emacs Manual), for a short description of Partial Completion mode.

 -- Variable: minibuffer-completion-table
     The value of this variable is the collection 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.

 -- Variable: minibuffer-completion-confirm
     When the value of this variable is non-nil, Emacs asks for confirmation of a completion before exiting the minibuffer. completing-read binds this variable, and the function minibuffer-complete-and-exit checks the value before exiting.

 -- 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 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.

 -- 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 &optional common-substring
     This function displays COMPLETIONS to the stream in standard-output, usually a buffer.  (see Read and Print, 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.  It can also be a list of two strings, which is printed as if the strings were concatenated.  The first of the two strings is the actual completion, the second string serves as annotation.

     The argument COMMON-SUBSTRING is the prefix that is common to all the completions.  With normal Emacs completion, it is usually the same as the string that was completed.  display-completion-list uses this to highlight text in the completion list for better visual feedback.  This is not needed in the minibuffer; for minibuffer completion, you can pass nil.

     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)
              (buffer-string)))

 -- 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.

 -- 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 Definition of minibuffer-local-map).

 -- 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-local-filename-completion-map
     This is like minibuffer-local-completion-map except that it does not bind SPC.  This keymap is used by the function read-file-name.

 -- Variable: minibuffer-local-must-match-filename-map
     This is like minibuffer-local-must-match-map except that it does not bind SPC.  This keymap is used by the function read-file-name.

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 specification.  See 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.

     The argument PROMPT should be a string ending with a colon and a space.  If DEFAULT is non-nil, the function inserts it in PROMPT before the colon to follow the convention for reading from the minibuffer with a default value (see Programming Tips).

     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.  If EXISTING is neither nil nor t, confirmation is required after completion.  (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"

 -- Variable: read-buffer-function
     This variable specifies how to read buffer names.  For example, if you set this variable to iswitchb-read-buffer, all Emacs commands that call read-buffer to read a buffer name will actually use the iswitchb package to read it.

 -- Function: read-command prompt &optional default
     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 Interactive Call.

     The argument DEFAULT 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 (intern ""), that is, a symbol whose name is an empty string.

          (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 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
     This function reads the name of a user variable and returns it as a symbol.

     The argument DEFAULT 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 is nil, that means no default has been specified; then if the user enters null input, the return value is (intern "").

          (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))

See also the functions read-coding-system and read-non-nil-coding-system, in User-Chosen Coding Systems, and read-input-method-name, in Input Methods.

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 predicate
     This function reads a file name in the minibuffer, prompting with PROMPT and providing completion.

     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 function read-file-name uses minibuffer-local-filename-completion-map as the keymap if EXISTING is nil, and uses minibuffer-local-must-match-filename-map if EXISTING is non-nil.  See Completion Commands.

     The argument DIRECTORY specifies the directory to use for completion of relative file names.  It should be an absolute directory name.  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.  Please note: we recommend using DEFAULT rather than INITIAL in most cases.

     If DEFAULT is non-nil, then the function returns DEFAULT if the user exits the minibuffer with the same non-empty contents that read-file-name inserted initially.  The initial minibuffer contents are always non-empty if insert-default-directory is non-nil, as it is by default.  DEFAULT is not checked for validity, regardless of the value of EXISTING.  However, if EXISTING is non-nil, the initial minibuffer contents should be a valid file (or directory) name.  Otherwise read-file-name attempts completion if the user exits without any editing, and does not return DEFAULT.  DEFAULT is also available through the history commands.

     If DEFAULT is nil, read-file-name tries to find a substitute default to use in its place, which it treats in exactly the same way as if it had been specified explicitly.  If DEFAULT is nil, but INITIAL is non-nil, then the default is the absolute file name obtained from DIRECTORY and INITIAL.  If both DEFAULT and INITIAL are nil and the buffer is visiting a file, read-file-name uses the absolute file name of that file as default.  If the buffer is not visiting a file, then there is no default.  In that case, if the user types RET without any editing, read-file-name simply returns the pre-inserted contents of the minibuffer.

     If the user types RET in an empty minibuffer, this function returns an empty string, regardless of the value of EXISTING. This is, for instance, how the user can make the current buffer visit no file using M-x set-visited-file-name.

     If PREDICATE is non-nil, it specifies a function of one argument that decides which file names are acceptable completion possibilities.  A file name is an acceptable value if PREDICATE returns non-nil for it.

     read-file-name does not automatically expand file names.  You must call expand-file-name yourself if an absolute file name is required.

     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".

 -- Variable: read-file-name-function
     If non-nil, this should be a function that accepts the same arguments as read-file-name.  When read-file-name is called, it calls this function with the supplied arguments instead of doing its usual work.

 -- Variable: read-file-name-completion-ignore-case
     If this variable is non-nil, read-file-name ignores case when performing completion.

 -- Function: read-directory-name prompt &optional directory default existing initial
     This function is like read-file-name but allows only directory names as completion possibilities.

     If DEFAULT is nil and INITIAL is non-nil, read-directory-name constructs a substitute default by combining DIRECTORY (or the current buffer's default directory if DIRECTORY is nil) and INITIAL.  If both DEFAULT and INITIAL are nil, this function uses DIRECTORY as substitute default, or the current buffer's default directory if DIRECTORY is nil.

 -- User Option: insert-default-directory
     This variable is used by read-file-name, and thus, indirectly, by most commands reading file names.  (This includes all commands that use the code letters f or F in their interactive form. See Code Characters for interactive.)  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.

     If this variable is nil and the initial minibuffer contents are empty, the user may have to explicitly fetch the next history element to access a default value.  If the variable is non-nil, the initial minibuffer contents are always non-empty and the user can always request a default value by immediately typing RET in an unedited minibuffer.  (See above.)

     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 ----------

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:

  * The string to be completed.

  * The predicate function to filter possible matches, or nil if none.  Your function should call the predicate for each possible match, and ignore the possible match if the predicate returns nil.

  * A flag specifying the type of operation.

There are three flag values for three operations:

  * nil specifies try-completion.  The completion function should return the completion of the specified string, or t if the string is a unique and exact match already, or nil if the string matches no possibility.

     If the string is an exact match for one possibility, but also matches other longer possibilities, the function should return the string, not t.

  * t specifies all-completions.  The completion function should return a list of all possible completions of the specified string.

  * lambda specifies test-completion.  The completion function should return t if the specified string is an exact match for some possibility; nil otherwise.

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 already have other meanings, and it would be unreliable to treat one differently just 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 File Name Completion.

 -- Macro: dynamic-completion-table function
     This macro is a convenient way to write a function that can act as programmed completion function.  The argument FUNCTION should be a function that takes one argument, a string, and returns an alist of possible completions of it.  You can think of dynamic-completion-table as a transducer between that interface and the interface for programmed completion functions.

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.

If either of these functions is called in a command that was invoked using the mouse---more precisely, if last-nonmenu-event (see Command Loop Info) is either nil or a list---then it uses a dialog box or pop-up menu to ask the question.  Otherwise, it uses keyboard input.  You can force use of the mouse or use of keyboard input by binding last-nonmenu-event to a suitable value around the call.

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 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 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: y-or-n-p-with-timeout prompt seconds default-value
     Like y-or-n-p, except that if the user fails to answer within SECONDS seconds, this function stops waiting and returns DEFAULT-VALUE.  It works by setting up a timer; see Timers. The argument SECONDS may be an integer or a floating point number.

 -- 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 ----------

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 no-cursor-in-echo-area
     This function 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.

     Normally, map-y-or-n-p binds cursor-in-echo-area while prompting.  But if NO-CURSOR-IN-ECHO-AREA is non-nil, it does not do that.

     If map-y-or-n-p is called in a command that was invoked using the mouse---more precisely, if last-nonmenu-event (see Command Loop Info) 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.

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.  If DEFAULT is nil, then read-passwd returns the null string in that case.

Minibuffer Commands
===================

This section describes some commands meant for use in the minibuffer.

 -- Command: exit-minibuffer
     This command exits the active minibuffer.  It is normally bound to keys in minibuffer local keymaps.

 -- Command: self-insert-and-exit
     This command exits the active minibuffer after inserting the last character typed on the keyboard (found in last-command-char; see Command Loop Info).

 -- Command: previous-history-element n
     This command replaces the minibuffer contents with the value of the Nth previous (older) history element.

 -- Command: next-history-element n
     This command replaces the minibuffer contents with the value of the Nth more recent history element.

 -- Command: previous-matching-history-element pattern n
     This command replaces the minibuffer contents with the value of the Nth previous (older) history element that matches PATTERN (a regular expression).

 -- Command: next-matching-history-element pattern n
     This command replaces the minibuffer contents with the value of the Nth next (newer) history element that matches PATTERN (a regular expression).

Minibuffer Windows
==================

These functions access and select minibuffer windows and test whether they are active.

 -- Function: active-minibuffer-window
     This function returns the currently active minibuffer window, or nil if none is currently active.

 -- Function: minibuffer-window &optional frame
     This function returns the minibuffer window used for frame FRAME. If FRAME is nil, that stands for the current frame.  Note that the minibuffer window used by a frame need not be part of that frame---a frame that has no minibuffer of its own necessarily uses some other frame's minibuffer window.

 -- Function: set-minibuffer-window window
     This function specifies WINDOW as the minibuffer window to use. This affects where the minibuffer is displayed if you put text in it without invoking the usual minibuffer commands.  It has no effect on the usual minibuffer input functions because they all start by choosing the minibuffer window according to the current frame.

 -- Function: window-minibuffer-p &optional window
     This function returns non-nil if WINDOW is a minibuffer window. WINDOW defaults to the selected window.

It is not correct to determine whether a given window is a minibuffer by comparing it with the result of (minibuffer-window), because there can be more than one minibuffer window if there is more than one frame.

 -- Function: minibuffer-window-active-p window
     This function returns non-nil if WINDOW, assumed to be a minibuffer window, is currently active.

Minibuffer Contents
===================

These functions access the minibuffer prompt and contents.

 -- Function: minibuffer-prompt
     This function returns the prompt string of the currently active minibuffer.  If no minibuffer is active, it returns nil.

 -- Function: minibuffer-prompt-end
     This function returns the current position of the end of the minibuffer prompt, if a minibuffer is current.  Otherwise, it returns the minimum valid buffer position.

 -- Function: minibuffer-prompt-width
     This function returns the current display-width of the minibuffer prompt, if a minibuffer is current.  Otherwise, it returns zero.

 -- Function: minibuffer-contents
     This function returns the editable contents of the minibuffer (that is, everything except the prompt) as a string, if a minibuffer is current.  Otherwise, it returns the entire contents of the current buffer.

 -- Function: minibuffer-contents-no-properties
     This is like minibuffer-contents, except that it does not copy text properties, just the characters themselves.  See Text Properties.

 -- Function: minibuffer-completion-contents
     This is like minibuffer-contents, except that it returns only the contents before point.  That is the part that completion commands operate on.  See Minibuffer Completion.

 -- Function: delete-minibuffer-contents
     This function erases the editable contents of the minibuffer (that is, everything except the prompt), if a minibuffer is current.  Otherwise, it erases the entire current buffer.

Recursive Minibuffers
=====================

These functions and variables deal with recursive minibuffers (see Recursive Editing):

 -- Function: minibuffer-depth
     This function returns the current depth of activations of the minibuffer, a nonnegative integer.  If no minibuffers are active, it returns zero.

 -- User Option: enable-recursive-minibuffers
     If this variable is non-nil, you can invoke commands (such as find-file) that use minibuffers even while the minibuffer window is active.  Such invocation produces a recursive editing level for a new minibuffer.  The outer-level minibuffer is invisible while you are editing the inner one.

     If this variable is nil, you cannot invoke minibuffer commands when the minibuffer window is active, not even if you switch to another window to do it.

If a command name has a property enable-recursive-minibuffers that is non-nil, then the command can use the minibuffer to read arguments even if it is invoked from the minibuffer.  A command can also achieve this by binding enable-recursive-minibuffers to t in the interactive declaration (see Using Interactive). The minibuffer command next-matching-history-element (normally M-s in the minibuffer) does the latter.

Minibuffer Miscellany
=====================

 -- Function: minibufferp &optional buffer-or-name
     This function returns non-nil if BUFFER-OR-NAME is a minibuffer.  If BUFFER-OR-NAME is omitted, it tests the current buffer.

 -- Variable: minibuffer-setup-hook
     This is a normal hook that is run whenever the minibuffer is entered. See Hooks.

 -- Variable: minibuffer-exit-hook
     This is a normal hook that is run whenever the minibuffer is exited. See Hooks.

 -- Variable: minibuffer-help-form
     The current value of this variable is used to rebind help-form locally inside the minibuffer (see Help Functions).

 -- Variable: minibuffer-scroll-window
     If the value of this variable is non-nil, it should be a window object.  When the function scroll-other-window is called in the minibuffer, it scrolls this window.

 -- Function: minibuffer-selected-window
     This function returns the window which was selected when the minibuffer was entered.  If selected window is not a minibuffer window, it returns nil.

 -- User Option: max-mini-window-height
     This variable specifies the maximum height for resizing minibuffer windows.  If a float, it specifies a fraction of the height of the frame.  If an integer, it specifies a number of lines.

 -- Function: minibuffer-message string
     This function displays STRING temporarily at the end of the minibuffer text, for two seconds, or until the next input event arrives, whichever comes first.