]> code.delx.au - gnu-emacs/blobdiff - lispref/minibuf.texi
(Defining Abbrevs): Index no-self-insert.
[gnu-emacs] / lispref / minibuf.texi
index 96a5e1a99ac8406fc0f1e4ccbe77a34d9817b36d..0e455a446e731ae90b00dafaeacabd03d6aa571c 100644 (file)
@@ -1,7 +1,7 @@
 @c -*-texinfo-*-
 @c This is part of the GNU Emacs Lisp Reference Manual.
 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001
-@c   Free Software Foundation, Inc. 
+@c   Free Software Foundation, Inc.
 @c See the file elisp.texi for copying conditions.
 @setfilename ../info/minibuf
 @node Minibuffers, Command Loop, Read and Print, Top
@@ -37,7 +37,7 @@ for reading an argument.
 @emph{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 @w{@samp{
-*Minibuf-@var{number}}}, and it cannot be changed.  Minibuffers are
+*Minibuf-@var{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
@@ -63,6 +63,11 @@ 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 @code{this-command} and @code{last-command}
+(@pxref{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 @dfn{recursive minibuffer}.  The first minibuffer is
 named @w{@samp{ *Minibuf-0*}}.  Recursive minibuffers are named by
@@ -84,7 +89,7 @@ completion commands (@pxref{Completion}).
 
 @item
 @code{minibuffer-local-ns-map} is similar, except that @key{SPC} exits
-just like @key{RET}.  This is used mainly for Mocklisp compatibility.
+just like @key{RET}.
 
 @item
 @code{minibuffer-local-completion-map} is for permissive completion.
@@ -184,12 +189,12 @@ arguments @var{prompt} and @var{initial} are used as in
 @code{read-from-minibuffer}.  The keymap used is
 @code{minibuffer-local-map}.
 
-The optional argument @var{history}, if non-nil, specifies a history
-list and optionally the initial position in the list.  The optional
-argument @var{default} specifies a default value to return if the user
-enters null input; it should be a string.  The optional argument
-@var{inherit-input-method} specifies whether to inherit the current
-buffer's input method.
+The optional argument @var{history}, if non-@code{nil}, specifies a
+history list and optionally the initial position in the list.  The
+optional argument @var{default} specifies a default value to return if
+the user enters null input; it should be a string.  The optional
+argument @var{inherit-input-method} specifies whether to inherit the
+current buffer's input method.
 
 This function is a simplified interface to the
 @code{read-from-minibuffer} function:
@@ -374,7 +379,7 @@ text which is a valid form already:
 @group
 (edit-and-eval-command "Please edit: " '(forward-word 1))
 
-;; @r{After evaluation of the preceding expression,} 
+;; @r{After evaluation of the preceding expression,}
 ;;   @r{the following appears in the minibuffer:}
 @end group
 
@@ -435,6 +440,20 @@ 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
+@code{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 @code{history-length} property of the
+history list symbol.
+
+@defvar 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 @code{t}, that means there no maximum (don't delete old
+elements).
+@end defvar
+
   Here are some of the standard minibuffer history list variables:
 
 @defvar minibuffer-history
@@ -506,22 +525,22 @@ for reading certain kinds of names with completion.
 * High-Level Completion::  Convenient special cases of completion
                              (reading buffer name, file name, etc.)
 * Reading File Names::     Using completion to read file names.
-* Programmed Completion::  Finding the completions for a given file name.
+* Programmed Completion::  Writing your own completion-function.
 @end menu
 
 @node Basic Completion
 @subsection Basic Completion Functions
 
-  The two functions @code{try-completion} and @code{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.
+  The functions @code{try-completion}, @code{all-completions} and
+@code{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.
 
 @defun try-completion string collection &optional predicate
 This function returns the longest common substring of all possible
 completions of @var{string} in @var{collection}.  The value of
-@var{collection} must be an alist, an obarray, or a function that
-implements a virtual set of strings (see below).
+@var{collection} must be a list of strings, an alist, an obarray, or a
+function that implements a virtual set of strings (see below).
 
 Completion compares @var{string} against each of the permissible
 completions specified by @var{collection}; if the beginning of the
@@ -545,13 +564,6 @@ 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 @code{intern}.
 Also, you cannot intern a given symbol in more than one obarray.
 
-If the argument @var{predicate} is non-@code{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 @var{predicate} returns non-@code{nil}.
-The argument given to @var{predicate} is either a cons cell from the alist
-(the @sc{car} of which is a string) or else it is a symbol (@emph{not} a
-symbol name) from the obarray.
-
 You can also use a symbol that is a function as @var{collection}.  Then
 the function is solely responsible for performing completion;
 @code{try-completion} returns whatever this function returns.  The
@@ -560,6 +572,13 @@ and @code{nil}.  (The reason for the third argument is so that the same
 function can be used in @code{all-completions} and do the appropriate
 thing in either case.)  @xref{Programmed Completion}.
 
+If the argument @var{predicate} is non-@code{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 @var{predicate} returns non-@code{nil}.
+The argument given to @var{predicate} is either a string from the
+list, a cons cell from the alist (the @sc{car} of which is a string)
+or a symbol (@emph{not} a symbol name) from the obarray.
+
 In the first of the following examples, the string @samp{foo} is
 matched by three of the alist @sc{car}s.  All of the matches begin with
 the characters @samp{fooba}, so that is the result.  In the second
@@ -568,7 +587,7 @@ is @code{t}.
 
 @smallexample
 @group
-(try-completion 
+(try-completion
  "foo"
  '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
      @result{} "fooba"
@@ -598,14 +617,14 @@ too short).  Both of those begin with the string @samp{foobar}.
 
 @smallexample
 @group
-(defun test (s) 
+(defun test (s)
   (> (length (car s)) 6))
      @result{} test
 @end group
 @group
-(try-completion 
+(try-completion
  "foo"
- '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)) 
+ '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  'test)
      @result{} "foobar"
 @end group
@@ -628,13 +647,13 @@ example for @code{try-completion}:
 
 @smallexample
 @group
-(defun test (s) 
+(defun test (s)
   (> (length (car s)) 6))
      @result{} test
 @end group
 
 @group
-(all-completions  
+(all-completions
  "foo"
  '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  'test)
@@ -643,11 +662,43 @@ example for @code{try-completion}:
 @end smallexample
 @end defun
 
+@defun test-completion string collection &optional predicate
+This function returns non-@code{nil} if @var{string} is a valid
+completion possibility specified by @var{collection} and
+@var{predicate}.  The other arguments are the same as in
+@code{try-completion}.  For instance, if @var{collection} is a list,
+this is true if @var{string} appears in the list and @var{predicate}
+is satisfied.
+
+If @var{collection} is a function, it is called with three arguments,
+the values @var{string}, @var{predicate} and @code{lambda}; whatever
+it returns, @code{test-completion} returns in turn.
+@end defun
+
 @defvar completion-ignore-case
-If the value of this variable is 
-non-@code{nil}, Emacs does not consider case significant in completion.
+If the value of this variable is non-@code{nil}, Emacs does not
+consider case significant in completion.
 @end defvar
 
+@defmac lazy-completion-table var fun &rest args
+This macro provides a way to initialize the variable @var{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{var}.  The actual computation of the
+proper value is done the first time you do completion using @var{var}.
+It is done by calling @var{fun} with the arguments @var{args}.  The
+value @var{fun} returns becomes the permanent value of @var{var}.
+
+Here are two examples of use:
+
+@example
+(defvar foo (lazy-completion-table foo make-my-alist 'global))
+
+(make-local-variable 'bar)
+(setq bar (lazy-completion-table foo make-my-alist 'local)
+@end example
+@end defmac
+
 @node Minibuffer Completion
 @subsection Completion and the Minibuffer
 
@@ -724,7 +775,7 @@ Here's an example of using @code{completing-read}:
 @end group
 
 @group
-;; @r{After evaluation of the preceding expression,} 
+;; @r{After evaluation of the preceding expression,}
 ;;   @r{the following appears in the minibuffer:}
 
 ---------- Buffer: Minibuffer ----------
@@ -909,7 +960,7 @@ only buffer name starting with the given input is
 @example
 (read-buffer "Buffer name? " "foo" t)
 @group
-;; @r{After evaluation of the preceding expression,} 
+;; @r{After evaluation of the preceding expression,}
 ;;   @r{the following prompt appears,}
 ;;   @r{with an empty minibuffer:}
 @end group
@@ -951,13 +1002,13 @@ enters null input, the return value is @code{nil}.
 (read-command "Command name? ")
 
 @group
-;; @r{After evaluation of the preceding expression,} 
+;; @r{After evaluation of the preceding expression,}
 ;;   @r{the following prompt appears with an empty minibuffer:}
 @end group
 
 @group
----------- Buffer: Minibuffer ---------- 
-Command name?  
+---------- Buffer: Minibuffer ----------
+Command name?
 ---------- Buffer: Minibuffer ----------
 @end group
 @end example
@@ -976,7 +1027,7 @@ complete in the set of extant Lisp symbols, and it uses the
 @group
 (read-command @var{prompt})
 @equiv{}
-(intern (completing-read @var{prompt} obarray 
+(intern (completing-read @var{prompt} obarray
                          'commandp t nil))
 @end group
 @end example
@@ -996,8 +1047,8 @@ user enters null input, the return value is @code{nil}.
 @group
 (read-variable "Variable name? ")
 
-;; @r{After evaluation of the preceding expression,} 
-;;   @r{the following prompt appears,} 
+;; @r{After evaluation of the preceding expression,}
+;;   @r{the following prompt appears,}
 ;;   @r{with an empty minibuffer:}
 @end group
 
@@ -1037,7 +1088,7 @@ predicate @code{user-variable-p} instead of @code{commandp}:
 file name.  It provides special features including automatic insertion
 of the default directory.
 
-@defun read-file-name prompt &optional directory default existing initial
+@defun read-file-name prompt &optional directory default existing initial predicate
 This function reads a file name in the minibuffer, prompting with
 @var{prompt} and providing completion.  If @var{default} is
 non-@code{nil}, then the function returns @var{default} if the user just
@@ -1066,13 +1117,18 @@ case, point goes at the beginning of @var{initial}.  The default for
 @var{initial} does, try the command @kbd{C-x C-v}.  @strong{Note:} we
 recommend using @var{default} rather than @var{initial} in most cases.
 
-Here is an example: 
+If @var{predicate} is non-@code{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 @var{predicate}
+returns non-@code{nil} for it.
+
+Here is an example:
 
 @example
 @group
 (read-file-name "The file is ")
 
-;; @r{After evaluation of the preceding expression,} 
+;; @r{After evaluation of the preceding expression,}
 ;;   @r{the following appears in the minibuffer:}
 @end group
 
@@ -1100,6 +1156,11 @@ If the user types @key{RET}, @code{read-file-name} returns the file name
 as the string @code{"/gp/gnu/elisp/manual.texi"}.
 @end defun
 
+@defun read-directory-name prompt &optional directory default existing initial
+This function is like @code{read-file-name} but allows only directory
+names as completion possibilities.
+@end defun
+
 @defopt insert-default-directory
 This variable is used by @code{read-file-name}.  Its value controls
 whether @code{read-file-name} starts by placing the name of the default
@@ -1189,7 +1250,7 @@ should return a list of all possible completions of the specified
 string.
 
 @item
-@code{lambda} specifies a test for an exact match.  The completion
+@code{lambda} specifies @code{test-completion}.  The completion
 function should return @code{t} if the specified string is an exact
 match for some possibility; @code{nil} otherwise.
 @end itemize
@@ -1197,14 +1258,23 @@ match for some possibility; @code{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 @var{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.
+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.
 @xref{File Name Completion}.
 
+@defmac dynamic-completion-table function
+This macro is a convenient way to write a function that can act as
+programmed completion function.  The argument @var{function} should be
+a function that takes one argument, a string, and returns an alist of
+possible completions of it.  You can think of
+@code{dynamic-completion-table} as a transducer between that interface
+and the interface for programmed completion functions.
+@end defmac
+
 @node Yes-or-No Queries
 @section Yes-or-No Queries
 @cindex asking the user questions
@@ -1261,13 +1331,13 @@ invalid.  At the next prompt the user types @kbd{y}.
 @group
 (y-or-n-p "Do you need a lift? ")
 
-;; @r{After evaluation of the preceding expression,} 
+;; @r{After evaluation of the preceding expression,}
 ;;   @r{the following prompt appears in the echo area:}
 @end group
 
 @group
 ---------- Echo area ----------
-Do you need a lift? (y or n) 
+Do you need a lift? (y or n)
 ---------- Echo area ----------
 @end group
 
@@ -1275,7 +1345,7 @@ Do you need a lift? (y or n)
 
 @group
 ---------- Echo area ----------
-Please answer y or n.  Do you need a lift? (y or n) 
+Please answer y or n.  Do you need a lift? (y or n)
 ---------- Echo area ----------
 @end group
 
@@ -1321,14 +1391,14 @@ Here is an example:
 @group
 (yes-or-no-p "Do you really want to remove everything? ")
 
-;; @r{After evaluation of the preceding expression,} 
-;;   @r{the following prompt appears,} 
+;; @r{After evaluation of the preceding expression,}
+;;   @r{the following prompt appears,}
 ;;   @r{with an empty minibuffer:}
 @end group
 
 @group
 ---------- Buffer: minibuffer ----------
-Do you really want to remove everything? (yes or no) 
+Do you really want to remove everything? (yes or no)
 ---------- Buffer: minibuffer ----------
 @end group
 @end smallexample
@@ -1556,6 +1626,11 @@ The current value of this variable is used to rebind @code{help-form}
 locally inside the minibuffer (@pxref{Help Functions}).
 @end defvar
 
+@defun minibufferp &optional buffer
+This function returns non-@code{nil} if @var{buffer} is a minibuffer.
+If @var{buffer} is omitted, it tests the current buffer.
+@end defun
+
 @defun active-minibuffer-window
 This function returns the currently active minibuffer window, or
 @code{nil} if none is currently active.
@@ -1590,6 +1665,12 @@ object.  When the function @code{scroll-other-window} is called in the
 minibuffer, it scrolls this window.
 @end defvar
 
+@defun 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 @code{nil}.
+@end defun
+
 Finally, some functions and variables deal with recursive minibuffers
 (@pxref{Recursive Editing}):
 
@@ -1617,3 +1698,13 @@ that is non-@code{nil}, then the command can use the minibuffer to read
 arguments even if it is invoked from the minibuffer.  The minibuffer
 command @code{next-matching-history-element} (normally @kbd{M-s} in the
 minibuffer) uses this feature.
+
+@defun minibuffer-message string &optional timeout
+This function displays @var{string} temporarily at the end of the
+minibuffer text, for @var{timeout} seconds.  (The default is 2
+seconds.)
+@end defun
+
+@ignore
+   arch-tag: bba7f945-9078-477f-a2ce-18818a6e1218
+@end ignore