minibuf.texi 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990-1995, 1998-1999, 2001-2015 Free Software
  4. @c Foundation, Inc.
  5. @c See the file elisp.texi for copying conditions.
  6. @node Minibuffers
  7. @chapter Minibuffers
  8. @cindex arguments, reading
  9. @cindex complex arguments
  10. @cindex minibuffer
  11. A @dfn{minibuffer} is a special buffer that Emacs commands use to
  12. read arguments more complicated than the single numeric prefix
  13. argument. These arguments include file names, buffer names, and
  14. command names (as in @kbd{M-x}). The minibuffer is displayed on the
  15. bottom line of the frame, in the same place as the echo area
  16. (@pxref{The Echo Area}), but only while it is in use for reading an
  17. argument.
  18. @menu
  19. * Intro to Minibuffers:: Basic information about minibuffers.
  20. * Text from Minibuffer:: How to read a straight text string.
  21. * Object from Minibuffer:: How to read a Lisp object or expression.
  22. * Minibuffer History:: Recording previous minibuffer inputs
  23. so the user can reuse them.
  24. * Initial Input:: Specifying initial contents for the minibuffer.
  25. * Completion:: How to invoke and customize completion.
  26. * Yes-or-No Queries:: Asking a question with a simple answer.
  27. * Multiple Queries:: Asking a series of similar questions.
  28. * Reading a Password:: Reading a password from the terminal.
  29. * Minibuffer Commands:: Commands used as key bindings in minibuffers.
  30. * Minibuffer Windows:: Operating on the special minibuffer windows.
  31. * Minibuffer Contents:: How such commands access the minibuffer text.
  32. * Recursive Mini:: Whether recursive entry to minibuffer is allowed.
  33. * Minibuffer Misc:: Various customization hooks and variables.
  34. @end menu
  35. @node Intro to Minibuffers
  36. @section Introduction to Minibuffers
  37. In most ways, a minibuffer is a normal Emacs buffer. Most operations
  38. @emph{within} a buffer, such as editing commands, work normally in a
  39. minibuffer. However, many operations for managing buffers do not apply
  40. to minibuffers. The name of a minibuffer always has the form @w{@samp{
  41. *Minibuf-@var{number}*}}, and it cannot be changed. Minibuffers are
  42. displayed only in special windows used only for minibuffers; these
  43. windows always appear at the bottom of a frame. (Sometimes frames have
  44. no minibuffer window, and sometimes a special kind of frame contains
  45. nothing but a minibuffer window; see @ref{Minibuffers and Frames}.)
  46. The text in the minibuffer always starts with the @dfn{prompt string},
  47. the text that was specified by the program that is using the minibuffer
  48. to tell the user what sort of input to type. This text is marked
  49. read-only so you won't accidentally delete or change it. It is also
  50. marked as a field (@pxref{Fields}), so that certain motion functions,
  51. including @code{beginning-of-line}, @code{forward-word},
  52. @code{forward-sentence}, and @code{forward-paragraph}, stop at the
  53. boundary between the prompt and the actual text.
  54. @c See http://debbugs.gnu.org/11276
  55. The minibuffer's window is normally a single line; it grows
  56. automatically if the contents require more space. Whilst it is
  57. active, you can explicitly resize it temporarily with the window
  58. sizing commands; it reverts to its normal size when the minibuffer is
  59. exited. When the minibuffer is not active, you can resize it
  60. permanently by using the window sizing commands in the frame's other
  61. window, or dragging the mode line with the mouse. (Due to details of
  62. the current implementation, for this to work @code{resize-mini-windows}
  63. must be @code{nil}.) If the frame contains just a minibuffer, you can
  64. change the minibuffer's size by changing the frame's size.
  65. Use of the minibuffer reads input events, and that alters the values
  66. of variables such as @code{this-command} and @code{last-command}
  67. (@pxref{Command Loop Info}). Your program should bind them around the
  68. code that uses the minibuffer, if you do not want that to change them.
  69. Under some circumstances, a command can use a minibuffer even if
  70. there is an active minibuffer; such a minibuffer is called a
  71. @dfn{recursive minibuffer}. The first minibuffer is named
  72. @w{@samp{ *Minibuf-1*}}. Recursive minibuffers are named by
  73. incrementing the number at the end of the name. (The names begin with
  74. a space so that they won't show up in normal buffer lists.) Of
  75. several recursive minibuffers, the innermost (or most recently
  76. entered) is the active minibuffer. We usually call this ``the''
  77. minibuffer. You can permit or forbid recursive minibuffers by setting
  78. the variable @code{enable-recursive-minibuffers}, or by putting
  79. properties of that name on command symbols (@xref{Recursive Mini}.)
  80. Like other buffers, a minibuffer uses a local keymap
  81. (@pxref{Keymaps}) to specify special key bindings. The function that
  82. invokes the minibuffer also sets up its local map according to the job
  83. to be done. @xref{Text from Minibuffer}, for the non-completion
  84. minibuffer local maps. @xref{Completion Commands}, for the minibuffer
  85. local maps for completion.
  86. @cindex inactive minibuffer
  87. When a minibuffer is inactive, its major mode is
  88. @code{minibuffer-inactive-mode}, with keymap
  89. @code{minibuffer-inactive-mode-map}. This is only really useful if
  90. the minibuffer is in a separate frame. @xref{Minibuffers and Frames}.
  91. When Emacs is running in batch mode, any request to read from the
  92. minibuffer actually reads a line from the standard input descriptor that
  93. was supplied when Emacs was started. This supports only basic input:
  94. none of the special minibuffer features (history, completion, etc.)@:
  95. are available in batch mode.
  96. @node Text from Minibuffer
  97. @section Reading Text Strings with the Minibuffer
  98. @cindex minibuffer input, reading text strings
  99. The most basic primitive for minibuffer input is
  100. @code{read-from-minibuffer}, which can be used to read either a string
  101. or a Lisp object in textual form. The function @code{read-regexp} is
  102. used for reading regular expressions (@pxref{Regular Expressions}),
  103. which are a special kind of string. There are also specialized
  104. functions for reading commands, variables, file names, etc.@:
  105. (@pxref{Completion}).
  106. In most cases, you should not call minibuffer input functions in the
  107. middle of a Lisp function. Instead, do all minibuffer input as part of
  108. reading the arguments for a command, in the @code{interactive}
  109. specification. @xref{Defining Commands}.
  110. @defun read-from-minibuffer prompt &optional initial keymap read history default inherit-input-method
  111. This function is the most general way to get input from the
  112. minibuffer. By default, it accepts arbitrary text and returns it as a
  113. string; however, if @var{read} is non-@code{nil}, then it uses
  114. @code{read} to convert the text into a Lisp object (@pxref{Input
  115. Functions}).
  116. The first thing this function does is to activate a minibuffer and
  117. display it with @var{prompt} (which must be a string) as the
  118. prompt. Then the user can edit text in the minibuffer.
  119. When the user types a command to exit the minibuffer,
  120. @code{read-from-minibuffer} constructs the return value from the text in
  121. the minibuffer. Normally it returns a string containing that text.
  122. However, if @var{read} is non-@code{nil}, @code{read-from-minibuffer}
  123. reads the text and returns the resulting Lisp object, unevaluated.
  124. (@xref{Input Functions}, for information about reading.)
  125. The argument @var{default} specifies default values to make available
  126. through the history commands. It should be a string, a list of
  127. strings, or @code{nil}. The string or strings become the minibuffer's
  128. ``future history'', available to the user with @kbd{M-n}.
  129. If @var{read} is non-@code{nil}, then @var{default} is also used
  130. as the input to @code{read}, if the user enters empty input.
  131. If @var{default} is a list of strings, the first string is used as the input.
  132. If @var{default} is @code{nil}, empty input results in an @code{end-of-file} error.
  133. However, in the usual case (where @var{read} is @code{nil}),
  134. @code{read-from-minibuffer} ignores @var{default} when the user enters
  135. empty input and returns an empty string, @code{""}. In this respect,
  136. it differs from all the other minibuffer input functions in this chapter.
  137. If @var{keymap} is non-@code{nil}, that keymap is the local keymap to
  138. use in the minibuffer. If @var{keymap} is omitted or @code{nil}, the
  139. value of @code{minibuffer-local-map} is used as the keymap. Specifying
  140. a keymap is the most important way to customize the minibuffer for
  141. various applications such as completion.
  142. The argument @var{history} specifies a history list variable to use
  143. for saving the input and for history commands used in the minibuffer.
  144. It defaults to @code{minibuffer-history}. You can optionally specify
  145. a starting position in the history list as well. @xref{Minibuffer History}.
  146. If the variable @code{minibuffer-allow-text-properties} is
  147. non-@code{nil}, then the string that is returned includes whatever text
  148. properties were present in the minibuffer. Otherwise all the text
  149. properties are stripped when the value is returned.
  150. If the argument @var{inherit-input-method} is non-@code{nil}, then the
  151. minibuffer inherits the current input method (@pxref{Input Methods}) and
  152. the setting of @code{enable-multibyte-characters} (@pxref{Text
  153. Representations}) from whichever buffer was current before entering the
  154. minibuffer.
  155. Use of @var{initial} is mostly deprecated; we recommend using
  156. a non-@code{nil} value only in conjunction with specifying a cons cell
  157. for @var{history}. @xref{Initial Input}.
  158. @end defun
  159. @defun read-string prompt &optional initial history default inherit-input-method
  160. This function reads a string from the minibuffer and returns it. The
  161. arguments @var{prompt}, @var{initial}, @var{history} and
  162. @var{inherit-input-method} are used as in @code{read-from-minibuffer}.
  163. The keymap used is @code{minibuffer-local-map}.
  164. The optional argument @var{default} is used as in
  165. @code{read-from-minibuffer}, except that, if non-@code{nil}, it also
  166. specifies a default value to return if the user enters null input. As
  167. in @code{read-from-minibuffer} it should be a string, a list of
  168. strings, or @code{nil}, which is equivalent to an empty string. When
  169. @var{default} is a string, that string is the default value. When it
  170. is a list of strings, the first string is the default value. (All
  171. these strings are available to the user in the ``future minibuffer
  172. history''.)
  173. This function works by calling the
  174. @code{read-from-minibuffer} function:
  175. @smallexample
  176. @group
  177. (read-string @var{prompt} @var{initial} @var{history} @var{default} @var{inherit})
  178. @equiv{}
  179. (let ((value
  180. (read-from-minibuffer @var{prompt} @var{initial} nil nil
  181. @var{history} @var{default} @var{inherit})))
  182. (if (and (equal value "") @var{default})
  183. (if (consp @var{default}) (car @var{default}) @var{default})
  184. value))
  185. @end group
  186. @end smallexample
  187. @end defun
  188. @defun read-regexp prompt &optional defaults history
  189. This function reads a regular expression as a string from the
  190. minibuffer and returns it. If the minibuffer prompt string
  191. @var{prompt} does not end in @samp{:} (followed by optional
  192. whitespace), the function adds @samp{: } to the end, preceded by the
  193. default return value (see below), if that is non-empty.
  194. The optional argument @var{defaults} controls the default value to
  195. return if the user enters null input, and should be one of: a string;
  196. @code{nil}, which is equivalent to an empty string; a list of strings;
  197. or a symbol.
  198. If @var{defaults} is a symbol, @code{read-regexp} consults the value
  199. of the variable @code{read-regexp-defaults-function} (see below), and
  200. if that is non-@code{nil} uses it in preference to @var{defaults}.
  201. The value in this case should be either:
  202. @itemize @minus
  203. @item
  204. @code{regexp-history-last}, which means to use the first element of
  205. the appropriate minibuffer history list (see below).
  206. @item
  207. A function of no arguments, whose return value (which should be
  208. @code{nil}, a string, or a list of strings) becomes the value of
  209. @var{defaults}.
  210. @end itemize
  211. @code{read-regexp} now ensures that the result of processing
  212. @var{defaults} is a list (i.e., if the value is @code{nil} or a
  213. string, it converts it to a list of one element). To this list,
  214. @code{read-regexp} then appends a few potentially useful candidates for
  215. input. These are:
  216. @itemize @minus
  217. @item
  218. The word or symbol at point.
  219. @item
  220. The last regexp used in an incremental search.
  221. @item
  222. The last string used in an incremental search.
  223. @item
  224. The last string or pattern used in query-replace commands.
  225. @end itemize
  226. The function now has a list of regular expressions that it passes to
  227. @code{read-from-minibuffer} to obtain the user's input. The first
  228. element of the list is the default result in case of empty input. All
  229. elements of the list are available to the user as the ``future
  230. minibuffer history list'' (@pxref{Minibuffer History, future list,,
  231. emacs, The GNU Emacs Manual}).
  232. The optional argument @var{history}, if non-@code{nil}, is a symbol
  233. specifying a minibuffer history list to use (@pxref{Minibuffer
  234. History}). If it is omitted or @code{nil}, the history list defaults
  235. to @code{regexp-history}.
  236. @end defun
  237. @defvar read-regexp-defaults-function
  238. The function @code{read-regexp} may use the value of this variable to
  239. determine its list of default regular expressions. If non-@code{nil},
  240. the value of this variable should be either:
  241. @itemize @minus
  242. @item
  243. The symbol @code{regexp-history-last}.
  244. @item
  245. A function of no arguments that returns either @code{nil}, a string,
  246. or a list of strings.
  247. @end itemize
  248. @noindent
  249. See @code{read-regexp} above for details of how these values are used.
  250. @end defvar
  251. @defvar minibuffer-allow-text-properties
  252. If this variable is @code{nil}, then @code{read-from-minibuffer}
  253. and @code{read-string} strip all text properties from the minibuffer
  254. input before returning it. However,
  255. @code{read-no-blanks-input} (see below), as well as
  256. @code{read-minibuffer} and related functions (@pxref{Object from
  257. Minibuffer,, Reading Lisp Objects With the Minibuffer}), and all
  258. functions that do minibuffer input with completion, discard text
  259. properties unconditionally, regardless of the value of this variable.
  260. @end defvar
  261. @defvar minibuffer-local-map
  262. This
  263. @anchor{Definition of minibuffer-local-map}
  264. @c avoid page break at anchor; work around Texinfo deficiency
  265. is the default local keymap for reading from the minibuffer. By
  266. default, it makes the following bindings:
  267. @table @asis
  268. @item @kbd{C-j}
  269. @code{exit-minibuffer}
  270. @item @key{RET}
  271. @code{exit-minibuffer}
  272. @item @kbd{C-g}
  273. @code{abort-recursive-edit}
  274. @item @kbd{M-n}
  275. @itemx @key{DOWN}
  276. @code{next-history-element}
  277. @item @kbd{M-p}
  278. @itemx @key{UP}
  279. @code{previous-history-element}
  280. @item @kbd{M-s}
  281. @code{next-matching-history-element}
  282. @item @kbd{M-r}
  283. @code{previous-matching-history-element}
  284. @ignore
  285. @c Does not seem worth/appropriate mentioning.
  286. @item @kbd{C-@key{TAB}}
  287. @code{file-cache-minibuffer-complete}
  288. @end ignore
  289. @end table
  290. @end defvar
  291. @c In version 18, initial is required
  292. @c Emacs 19 feature
  293. @defun read-no-blanks-input prompt &optional initial inherit-input-method
  294. This function reads a string from the minibuffer, but does not allow
  295. whitespace characters as part of the input: instead, those characters
  296. terminate the input. The arguments @var{prompt}, @var{initial}, and
  297. @var{inherit-input-method} are used as in @code{read-from-minibuffer}.
  298. This is a simplified interface to the @code{read-from-minibuffer}
  299. function, and passes the value of the @code{minibuffer-local-ns-map}
  300. keymap as the @var{keymap} argument for that function. Since the keymap
  301. @code{minibuffer-local-ns-map} does not rebind @kbd{C-q}, it @emph{is}
  302. possible to put a space into the string, by quoting it.
  303. This function discards text properties, regardless of the value of
  304. @code{minibuffer-allow-text-properties}.
  305. @smallexample
  306. @group
  307. (read-no-blanks-input @var{prompt} @var{initial})
  308. @equiv{}
  309. (let (minibuffer-allow-text-properties)
  310. (read-from-minibuffer @var{prompt} @var{initial} minibuffer-local-ns-map))
  311. @end group
  312. @end smallexample
  313. @end defun
  314. @c Slightly unfortunate name, suggesting it might be related to the
  315. @c Nextstep port...
  316. @defvar minibuffer-local-ns-map
  317. This built-in variable is the keymap used as the minibuffer local keymap
  318. in the function @code{read-no-blanks-input}. By default, it makes the
  319. following bindings, in addition to those of @code{minibuffer-local-map}:
  320. @table @asis
  321. @item @key{SPC}
  322. @cindex @key{SPC} in minibuffer
  323. @code{exit-minibuffer}
  324. @item @key{TAB}
  325. @cindex @key{TAB} in minibuffer
  326. @code{exit-minibuffer}
  327. @item @kbd{?}
  328. @cindex @kbd{?} in minibuffer
  329. @code{self-insert-and-exit}
  330. @end table
  331. @end defvar
  332. @node Object from Minibuffer
  333. @section Reading Lisp Objects with the Minibuffer
  334. @cindex minibuffer input, reading lisp objects
  335. This section describes functions for reading Lisp objects with the
  336. minibuffer.
  337. @defun read-minibuffer prompt &optional initial
  338. This function reads a Lisp object using the minibuffer, and returns it
  339. without evaluating it. The arguments @var{prompt} and @var{initial} are
  340. used as in @code{read-from-minibuffer}.
  341. This is a simplified interface to the
  342. @code{read-from-minibuffer} function:
  343. @smallexample
  344. @group
  345. (read-minibuffer @var{prompt} @var{initial})
  346. @equiv{}
  347. (let (minibuffer-allow-text-properties)
  348. (read-from-minibuffer @var{prompt} @var{initial} nil t))
  349. @end group
  350. @end smallexample
  351. Here is an example in which we supply the string @code{"(testing)"} as
  352. initial input:
  353. @smallexample
  354. @group
  355. (read-minibuffer
  356. "Enter an expression: " (format "%s" '(testing)))
  357. ;; @r{Here is how the minibuffer is displayed:}
  358. @end group
  359. @group
  360. ---------- Buffer: Minibuffer ----------
  361. Enter an expression: (testing)@point{}
  362. ---------- Buffer: Minibuffer ----------
  363. @end group
  364. @end smallexample
  365. @noindent
  366. The user can type @key{RET} immediately to use the initial input as a
  367. default, or can edit the input.
  368. @end defun
  369. @defun eval-minibuffer prompt &optional initial
  370. This function reads a Lisp expression using the minibuffer, evaluates
  371. it, then returns the result. The arguments @var{prompt} and
  372. @var{initial} are used as in @code{read-from-minibuffer}.
  373. This function simply evaluates the result of a call to
  374. @code{read-minibuffer}:
  375. @smallexample
  376. @group
  377. (eval-minibuffer @var{prompt} @var{initial})
  378. @equiv{}
  379. (eval (read-minibuffer @var{prompt} @var{initial}))
  380. @end group
  381. @end smallexample
  382. @end defun
  383. @defun edit-and-eval-command prompt form
  384. This function reads a Lisp expression in the minibuffer, evaluates it,
  385. then returns the result. The difference between this command and
  386. @code{eval-minibuffer} is that here the initial @var{form} is not
  387. optional and it is treated as a Lisp object to be converted to printed
  388. representation rather than as a string of text. It is printed with
  389. @code{prin1}, so if it is a string, double-quote characters (@samp{"})
  390. appear in the initial text. @xref{Output Functions}.
  391. In the following example, we offer the user an expression with initial
  392. text that is already a valid form:
  393. @smallexample
  394. @group
  395. (edit-and-eval-command "Please edit: " '(forward-word 1))
  396. ;; @r{After evaluation of the preceding expression,}
  397. ;; @r{the following appears in the minibuffer:}
  398. @end group
  399. @group
  400. ---------- Buffer: Minibuffer ----------
  401. Please edit: (forward-word 1)@point{}
  402. ---------- Buffer: Minibuffer ----------
  403. @end group
  404. @end smallexample
  405. @noindent
  406. Typing @key{RET} right away would exit the minibuffer and evaluate the
  407. expression, thus moving point forward one word.
  408. @end defun
  409. @node Minibuffer History
  410. @section Minibuffer History
  411. @cindex minibuffer history
  412. @cindex history list
  413. A @dfn{minibuffer history list} records previous minibuffer inputs
  414. so the user can reuse them conveniently. It is a variable whose value
  415. is a list of strings (previous inputs), most recent first.
  416. There are many separate minibuffer history lists, used for different
  417. kinds of inputs. It's the Lisp programmer's job to specify the right
  418. history list for each use of the minibuffer.
  419. You specify a minibuffer history list with the optional @var{history}
  420. argument to @code{read-from-minibuffer} or @code{completing-read}.
  421. Here are the possible values for it:
  422. @table @asis
  423. @item @var{variable}
  424. Use @var{variable} (a symbol) as the history list.
  425. @item (@var{variable} . @var{startpos})
  426. Use @var{variable} (a symbol) as the history list, and assume that the
  427. initial history position is @var{startpos} (a nonnegative integer).
  428. Specifying 0 for @var{startpos} is equivalent to just specifying the
  429. symbol @var{variable}. @code{previous-history-element} will display
  430. the most recent element of the history list in the minibuffer. If you
  431. specify a positive @var{startpos}, the minibuffer history functions
  432. behave as if @code{(elt @var{variable} (1- @var{startpos}))} were the
  433. history element currently shown in the minibuffer.
  434. For consistency, you should also specify that element of the history
  435. as the initial minibuffer contents, using the @var{initial} argument
  436. to the minibuffer input function (@pxref{Initial Input}).
  437. @end table
  438. If you don't specify @var{history}, then the default history list
  439. @code{minibuffer-history} is used. For other standard history lists,
  440. see below. You can also create your own history list variable; just
  441. initialize it to @code{nil} before the first use.
  442. Both @code{read-from-minibuffer} and @code{completing-read} add new
  443. elements to the history list automatically, and provide commands to
  444. allow the user to reuse items on the list. The only thing your program
  445. needs to do to use a history list is to initialize it and to pass its
  446. name to the input functions when you wish. But it is safe to modify the
  447. list by hand when the minibuffer input functions are not using it.
  448. Emacs functions that add a new element to a history list can also
  449. delete old elements if the list gets too long. The variable
  450. @code{history-length} specifies the maximum length for most history
  451. lists. To specify a different maximum length for a particular history
  452. list, put the length in the @code{history-length} property of the
  453. history list symbol. The variable @code{history-delete-duplicates}
  454. specifies whether to delete duplicates in history.
  455. @defun add-to-history history-var newelt &optional maxelt keep-all
  456. This function adds a new element @var{newelt}, if it isn't the empty
  457. string, to the history list stored in the variable @var{history-var},
  458. and returns the updated history list. It limits the list length to
  459. the value of @var{maxelt} (if non-@code{nil}) or @code{history-length}
  460. (described below). The possible values of @var{maxelt} have the same
  461. meaning as the values of @code{history-length}.
  462. Normally, @code{add-to-history} removes duplicate members from the
  463. history list if @code{history-delete-duplicates} is non-@code{nil}.
  464. However, if @var{keep-all} is non-@code{nil}, that says not to remove
  465. duplicates, and to add @var{newelt} to the list even if it is empty.
  466. @end defun
  467. @defvar history-add-new-input
  468. If the value of this variable is @code{nil}, standard functions that
  469. read from the minibuffer don't add new elements to the history list.
  470. This lets Lisp programs explicitly manage input history by using
  471. @code{add-to-history}. The default value is @code{t}.
  472. @end defvar
  473. @defopt history-length
  474. The value of this variable specifies the maximum length for all
  475. history lists that don't specify their own maximum lengths. If the
  476. value is @code{t}, that means there is no maximum (don't delete old
  477. elements). If a history list variable's symbol has a non-@code{nil}
  478. @code{history-length} property, it overrides this variable for that
  479. particular history list.
  480. @end defopt
  481. @defopt history-delete-duplicates
  482. If the value of this variable is @code{t}, that means when adding a
  483. new history element, all previous identical elements are deleted.
  484. @end defopt
  485. Here are some of the standard minibuffer history list variables:
  486. @defvar minibuffer-history
  487. The default history list for minibuffer history input.
  488. @end defvar
  489. @defvar query-replace-history
  490. A history list for arguments to @code{query-replace} (and similar
  491. arguments to other commands).
  492. @end defvar
  493. @defvar file-name-history
  494. A history list for file-name arguments.
  495. @end defvar
  496. @defvar buffer-name-history
  497. A history list for buffer-name arguments.
  498. @end defvar
  499. @defvar regexp-history
  500. A history list for regular expression arguments.
  501. @end defvar
  502. @defvar extended-command-history
  503. A history list for arguments that are names of extended commands.
  504. @end defvar
  505. @defvar shell-command-history
  506. A history list for arguments that are shell commands.
  507. @end defvar
  508. @defvar read-expression-history
  509. A history list for arguments that are Lisp expressions to evaluate.
  510. @end defvar
  511. @defvar face-name-history
  512. A history list for arguments that are faces.
  513. @end defvar
  514. @c Less common: coding-system-history, input-method-history,
  515. @c command-history, grep-history, grep-find-history,
  516. @c read-envvar-name-history, setenv-history, yes-or-no-p-history.
  517. @node Initial Input
  518. @section Initial Input
  519. Several of the functions for minibuffer input have an argument called
  520. @var{initial}. This is a mostly-deprecated
  521. feature for specifying that the minibuffer should start out with
  522. certain text, instead of empty as usual.
  523. If @var{initial} is a string, the minibuffer starts out containing the
  524. text of the string, with point at the end, when the user starts to
  525. edit the text. If the user simply types @key{RET} to exit the
  526. minibuffer, it will use the initial input string to determine the
  527. value to return.
  528. @strong{We discourage use of a non-@code{nil} value for
  529. @var{initial}}, because initial input is an intrusive interface.
  530. History lists and default values provide a much more convenient method
  531. to offer useful default inputs to the user.
  532. There is just one situation where you should specify a string for an
  533. @var{initial} argument. This is when you specify a cons cell for the
  534. @var{history} argument. @xref{Minibuffer History}.
  535. @var{initial} can also be a cons cell of the form @code{(@var{string}
  536. . @var{position})}. This means to insert @var{string} in the
  537. minibuffer but put point at @var{position} within the string's text.
  538. As a historical accident, @var{position} was implemented
  539. inconsistently in different functions. In @code{completing-read},
  540. @var{position}'s value is interpreted as origin-zero; that is, a value
  541. of 0 means the beginning of the string, 1 means after the first
  542. character, etc. In @code{read-minibuffer}, and the other
  543. non-completion minibuffer input functions that support this argument,
  544. 1 means the beginning of the string, 2 means after the first character,
  545. etc.
  546. Use of a cons cell as the value for @var{initial} arguments is deprecated.
  547. @node Completion
  548. @section Completion
  549. @cindex completion
  550. @dfn{Completion} is a feature that fills in the rest of a name
  551. starting from an abbreviation for it. Completion works by comparing the
  552. user's input against a list of valid names and determining how much of
  553. the name is determined uniquely by what the user has typed. For
  554. example, when you type @kbd{C-x b} (@code{switch-to-buffer}) and then
  555. @c "This is the sort of English up with which I will not put."
  556. type the first few letters of the name of the buffer to which you wish
  557. to switch, and then type @key{TAB} (@code{minibuffer-complete}), Emacs
  558. extends the name as far as it can.
  559. Standard Emacs commands offer completion for names of symbols, files,
  560. buffers, and processes; with the functions in this section, you can
  561. implement completion for other kinds of names.
  562. The @code{try-completion} function is the basic primitive for
  563. completion: it returns the longest determined completion of a given
  564. initial string, with a given set of strings to match against.
  565. The function @code{completing-read} provides a higher-level interface
  566. for completion. A call to @code{completing-read} specifies how to
  567. determine the list of valid names. The function then activates the
  568. minibuffer with a local keymap that binds a few keys to commands useful
  569. for completion. Other functions provide convenient simple interfaces
  570. for reading certain kinds of names with completion.
  571. @menu
  572. * Basic Completion:: Low-level functions for completing strings.
  573. * Minibuffer Completion:: Invoking the minibuffer with completion.
  574. * Completion Commands:: Minibuffer commands that do completion.
  575. * High-Level Completion:: Convenient special cases of completion
  576. (reading buffer names, variable names, etc.).
  577. * Reading File Names:: Using completion to read file names and
  578. shell commands.
  579. * Completion Variables:: Variables controlling completion behavior.
  580. * Programmed Completion:: Writing your own completion function.
  581. * Completion in Buffers:: Completing text in ordinary buffers.
  582. @end menu
  583. @node Basic Completion
  584. @subsection Basic Completion Functions
  585. The following completion functions have nothing in themselves to do
  586. with minibuffers. We describe them here to keep them near the
  587. higher-level completion features that do use the minibuffer.
  588. @defun try-completion string collection &optional predicate
  589. This function returns the longest common substring of all possible
  590. completions of @var{string} in @var{collection}.
  591. @cindex completion table
  592. @var{collection} is called the @dfn{completion table}. Its value must
  593. be a list of strings or cons cells, an obarray, a hash table, or a
  594. completion function.
  595. @code{try-completion} compares @var{string} against each of the
  596. permissible completions specified by the completion table. If no
  597. permissible completions match, it returns @code{nil}. If there is
  598. just one matching completion, and the match is exact, it returns
  599. @code{t}. Otherwise, it returns the longest initial sequence common
  600. to all possible matching completions.
  601. If @var{collection} is a list, the permissible completions are
  602. specified by the elements of the list, each of which should be either
  603. a string, or a cons cell whose @sc{car} is either a string or a symbol
  604. (a symbol is converted to a string using @code{symbol-name}). If the
  605. list contains elements of any other type, those are ignored.
  606. @cindex obarray in completion
  607. If @var{collection} is an obarray (@pxref{Creating Symbols}), the names
  608. of all symbols in the obarray form the set of permissible completions.
  609. If @var{collection} is a hash table, then the keys that are strings
  610. are the possible completions. Other keys are ignored.
  611. You can also use a function as @var{collection}. Then the function is
  612. solely responsible for performing completion; @code{try-completion}
  613. returns whatever this function returns. The function is called with
  614. three arguments: @var{string}, @var{predicate} and @code{nil} (the
  615. third argument is so that the same function can be used
  616. in @code{all-completions} and do the appropriate thing in either
  617. case). @xref{Programmed Completion}.
  618. If the argument @var{predicate} is non-@code{nil}, then it must be a
  619. function of one argument, unless @var{collection} is a hash table, in
  620. which case it should be a function of two arguments. It is used to
  621. test each possible match, and the match is accepted only if
  622. @var{predicate} returns non-@code{nil}. The argument given to
  623. @var{predicate} is either a string or a cons cell (the @sc{car} of
  624. which is a string) from the alist, or a symbol (@emph{not} a symbol
  625. name) from the obarray. If @var{collection} is a hash table,
  626. @var{predicate} is called with two arguments, the string key and the
  627. associated value.
  628. In addition, to be acceptable, a completion must also match all the
  629. regular expressions in @code{completion-regexp-list}. (Unless
  630. @var{collection} is a function, in which case that function has to
  631. handle @code{completion-regexp-list} itself.)
  632. In the first of the following examples, the string @samp{foo} is
  633. matched by three of the alist @sc{car}s. All of the matches begin with
  634. the characters @samp{fooba}, so that is the result. In the second
  635. example, there is only one possible match, and it is exact, so the
  636. return value is @code{t}.
  637. @smallexample
  638. @group
  639. (try-completion
  640. "foo"
  641. '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
  642. @result{} "fooba"
  643. @end group
  644. @group
  645. (try-completion "foo" '(("barfoo" 2) ("foo" 3)))
  646. @result{} t
  647. @end group
  648. @end smallexample
  649. In the following example, numerous symbols begin with the characters
  650. @samp{forw}, and all of them begin with the word @samp{forward}. In
  651. most of the symbols, this is followed with a @samp{-}, but not in all,
  652. so no more than @samp{forward} can be completed.
  653. @smallexample
  654. @group
  655. (try-completion "forw" obarray)
  656. @result{} "forward"
  657. @end group
  658. @end smallexample
  659. Finally, in the following example, only two of the three possible
  660. matches pass the predicate @code{test} (the string @samp{foobaz} is
  661. too short). Both of those begin with the string @samp{foobar}.
  662. @smallexample
  663. @group
  664. (defun test (s)
  665. (> (length (car s)) 6))
  666. @result{} test
  667. @end group
  668. @group
  669. (try-completion
  670. "foo"
  671. '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  672. 'test)
  673. @result{} "foobar"
  674. @end group
  675. @end smallexample
  676. @end defun
  677. @c Removed obsolete argument nospace.
  678. @defun all-completions string collection &optional predicate
  679. This function returns a list of all possible completions of
  680. @var{string}. The arguments to this function
  681. @c (aside from @var{nospace})
  682. are the same as those of @code{try-completion}, and it
  683. uses @code{completion-regexp-list} in the same way that
  684. @code{try-completion} does.
  685. @ignore
  686. The optional argument @var{nospace} is obsolete. If it is
  687. non-@code{nil}, completions that start with a space are ignored unless
  688. @var{string} starts with a space.
  689. @end ignore
  690. If @var{collection} is a function, it is called with three arguments:
  691. @var{string}, @var{predicate} and @code{t}; then @code{all-completions}
  692. returns whatever the function returns. @xref{Programmed Completion}.
  693. Here is an example, using the function @code{test} shown in the
  694. example for @code{try-completion}:
  695. @smallexample
  696. @group
  697. (defun test (s)
  698. (> (length (car s)) 6))
  699. @result{} test
  700. @end group
  701. @group
  702. (all-completions
  703. "foo"
  704. '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  705. 'test)
  706. @result{} ("foobar1" "foobar2")
  707. @end group
  708. @end smallexample
  709. @end defun
  710. @defun test-completion string collection &optional predicate
  711. @anchor{Definition of test-completion}
  712. This function returns non-@code{nil} if @var{string} is a valid
  713. completion alternative specified by @var{collection} and
  714. @var{predicate}. The arguments are the same as in
  715. @code{try-completion}. For instance, if @var{collection} is a list of
  716. strings, this is true if @var{string} appears in the list and
  717. @var{predicate} is satisfied.
  718. This function uses @code{completion-regexp-list} in the same
  719. way that @code{try-completion} does.
  720. If @var{predicate} is non-@code{nil} and if @var{collection} contains
  721. several strings that are equal to each other, as determined by
  722. @code{compare-strings} according to @code{completion-ignore-case},
  723. then @var{predicate} should accept either all or none of them.
  724. Otherwise, the return value of @code{test-completion} is essentially
  725. unpredictable.
  726. If @var{collection} is a function, it is called with three arguments,
  727. the values @var{string}, @var{predicate} and @code{lambda}; whatever
  728. it returns, @code{test-completion} returns in turn.
  729. @end defun
  730. @defun completion-boundaries string collection predicate suffix
  731. This function returns the boundaries of the field on which @var{collection}
  732. will operate, assuming that @var{string} holds the text before point
  733. and @var{suffix} holds the text after point.
  734. Normally completion operates on the whole string, so for all normal
  735. collections, this will always return @code{(0 . (length
  736. @var{suffix}))}. But more complex completion such as completion on
  737. files is done one field at a time. For example, completion of
  738. @code{"/usr/sh"} will include @code{"/usr/share/"} but not
  739. @code{"/usr/share/doc"} even if @code{"/usr/share/doc"} exists.
  740. Also @code{all-completions} on @code{"/usr/sh"} will not include
  741. @code{"/usr/share/"} but only @code{"share/"}. So if @var{string} is
  742. @code{"/usr/sh"} and @var{suffix} is @code{"e/doc"},
  743. @code{completion-boundaries} will return @code{(5 . 1)} which tells us
  744. that the @var{collection} will only return completion information that
  745. pertains to the area after @code{"/usr/"} and before @code{"/doc"}.
  746. @end defun
  747. If you store a completion alist in a variable, you should mark the
  748. variable as ``risky'' by giving it a non-@code{nil}
  749. @code{risky-local-variable} property. @xref{File Local Variables}.
  750. @defvar completion-ignore-case
  751. If the value of this variable is non-@code{nil}, case is not
  752. considered significant in completion. Within @code{read-file-name},
  753. this variable is overridden by
  754. @code{read-file-name-completion-ignore-case} (@pxref{Reading File
  755. Names}); within @code{read-buffer}, it is overridden by
  756. @code{read-buffer-completion-ignore-case} (@pxref{High-Level
  757. Completion}).
  758. @end defvar
  759. @defvar completion-regexp-list
  760. This is a list of regular expressions. The completion functions only
  761. consider a completion acceptable if it matches all regular expressions
  762. in this list, with @code{case-fold-search} (@pxref{Searching and Case})
  763. bound to the value of @code{completion-ignore-case}.
  764. @end defvar
  765. @defmac lazy-completion-table var fun
  766. This macro provides a way to initialize the variable @var{var} as a
  767. collection for completion in a lazy way, not computing its actual
  768. contents until they are first needed. You use this macro to produce a
  769. value that you store in @var{var}. The actual computation of the
  770. proper value is done the first time you do completion using @var{var}.
  771. It is done by calling @var{fun} with no arguments. The
  772. value @var{fun} returns becomes the permanent value of @var{var}.
  773. Here is an example:
  774. @smallexample
  775. (defvar foo (lazy-completion-table foo make-my-alist))
  776. @end smallexample
  777. @end defmac
  778. @c FIXME? completion-table-with-context?
  779. @findex completion-table-case-fold
  780. @findex completion-table-in-turn
  781. @findex completion-table-merge
  782. @findex completion-table-subvert
  783. @findex completion-table-with-quoting
  784. @findex completion-table-with-predicate
  785. @findex completion-table-with-terminator
  786. @cindex completion table, modifying
  787. @cindex completion tables, combining
  788. There are several functions that take an existing completion table and
  789. return a modified version. @code{completion-table-case-fold} returns
  790. a case-insensitive table. @code{completion-table-in-turn} and
  791. @code{completion-table-merge} combine multiple input tables in
  792. different ways. @code{completion-table-subvert} alters a table to use
  793. a different initial prefix. @code{completion-table-with-quoting}
  794. returns a table suitable for operating on quoted text.
  795. @code{completion-table-with-predicate} filters a table with a
  796. predicate function. @code{completion-table-with-terminator} adds a
  797. terminating string.
  798. @node Minibuffer Completion
  799. @subsection Completion and the Minibuffer
  800. @cindex minibuffer completion
  801. @cindex reading from minibuffer with completion
  802. This section describes the basic interface for reading from the
  803. minibuffer with completion.
  804. @defun completing-read prompt collection &optional predicate require-match initial history default inherit-input-method
  805. This function reads a string in the minibuffer, assisting the user by
  806. providing completion. It activates the minibuffer with prompt
  807. @var{prompt}, which must be a string.
  808. The actual completion is done by passing the completion table
  809. @var{collection} and the completion predicate @var{predicate} to the
  810. function @code{try-completion} (@pxref{Basic Completion}). This
  811. happens in certain commands bound in the local keymaps used for
  812. completion. Some of these commands also call @code{test-completion}.
  813. Thus, if @var{predicate} is non-@code{nil}, it should be compatible
  814. with @var{collection} and @code{completion-ignore-case}.
  815. @xref{Definition of test-completion}.
  816. The value of the optional argument @var{require-match} determines how
  817. the user may exit the minibuffer:
  818. @itemize @bullet
  819. @item
  820. If @code{nil}, the usual minibuffer exit commands work regardless of
  821. the input in the minibuffer.
  822. @item
  823. If @code{t}, the usual minibuffer exit commands won't exit unless the
  824. input completes to an element of @var{collection}.
  825. @item
  826. If @code{confirm}, the user can exit with any input, but is asked for
  827. confirmation if the input is not an element of @var{collection}.
  828. @item
  829. If @code{confirm-after-completion}, the user can exit with any input,
  830. but is asked for confirmation if the preceding command was a
  831. completion command (i.e., one of the commands in
  832. @code{minibuffer-confirm-exit-commands}) and the resulting input is
  833. not an element of @var{collection}. @xref{Completion Commands}.
  834. @item
  835. Any other value of @var{require-match} behaves like @code{t}, except
  836. that the exit commands won't exit if it performs completion.
  837. @end itemize
  838. However, empty input is always permitted, regardless of the value of
  839. @var{require-match}; in that case, @code{completing-read} returns the
  840. first element of @var{default}, if it is a list; @code{""}, if
  841. @var{default} is @code{nil}; or @var{default}. The string or strings
  842. in @var{default} are also available to the user through the history
  843. commands.
  844. The function @code{completing-read} uses
  845. @code{minibuffer-local-completion-map} as the keymap if
  846. @var{require-match} is @code{nil}, and uses
  847. @code{minibuffer-local-must-match-map} if @var{require-match} is
  848. non-@code{nil}. @xref{Completion Commands}.
  849. The argument @var{history} specifies which history list variable to use for
  850. saving the input and for minibuffer history commands. It defaults to
  851. @code{minibuffer-history}. @xref{Minibuffer History}.
  852. The argument @var{initial} is mostly deprecated; we recommend using a
  853. non-@code{nil} value only in conjunction with specifying a cons cell
  854. for @var{history}. @xref{Initial Input}. For default input, use
  855. @var{default} instead.
  856. If the argument @var{inherit-input-method} is non-@code{nil}, then the
  857. minibuffer inherits the current input method (@pxref{Input
  858. Methods}) and the setting of @code{enable-multibyte-characters}
  859. (@pxref{Text Representations}) from whichever buffer was current before
  860. entering the minibuffer.
  861. If the variable @code{completion-ignore-case} is
  862. non-@code{nil}, completion ignores case when comparing the input
  863. against the possible matches. @xref{Basic Completion}. In this mode
  864. of operation, @var{predicate} must also ignore case, or you will get
  865. surprising results.
  866. Here's an example of using @code{completing-read}:
  867. @smallexample
  868. @group
  869. (completing-read
  870. "Complete a foo: "
  871. '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  872. nil t "fo")
  873. @end group
  874. @group
  875. ;; @r{After evaluation of the preceding expression,}
  876. ;; @r{the following appears in the minibuffer:}
  877. ---------- Buffer: Minibuffer ----------
  878. Complete a foo: fo@point{}
  879. ---------- Buffer: Minibuffer ----------
  880. @end group
  881. @end smallexample
  882. @noindent
  883. If the user then types @kbd{@key{DEL} @key{DEL} b @key{RET}},
  884. @code{completing-read} returns @code{barfoo}.
  885. The @code{completing-read} function binds variables to pass
  886. information to the commands that actually do completion.
  887. They are described in the following section.
  888. @end defun
  889. @defvar completing-read-function
  890. The value of this variable must be a function, which is called by
  891. @code{completing-read} to actually do its work. It should accept the
  892. same arguments as @code{completing-read}. This can be bound to a
  893. different function to completely override the normal behavior of
  894. @code{completing-read}.
  895. @end defvar
  896. @node Completion Commands
  897. @subsection Minibuffer Commands that Do Completion
  898. This section describes the keymaps, commands and user options used
  899. in the minibuffer to do completion.
  900. @defvar minibuffer-completion-table
  901. The value of this variable is the completion table used for completion
  902. in the minibuffer. This is the global variable that contains what
  903. @code{completing-read} passes to @code{try-completion}. It is used by
  904. minibuffer completion commands such as
  905. @code{minibuffer-complete-word}.
  906. @end defvar
  907. @defvar minibuffer-completion-predicate
  908. This variable's value is the predicate that @code{completing-read}
  909. passes to @code{try-completion}. The variable is also used by the other
  910. minibuffer completion functions.
  911. @end defvar
  912. @defvar minibuffer-completion-confirm
  913. This variable determines whether Emacs asks for confirmation before
  914. exiting the minibuffer; @code{completing-read} binds this variable,
  915. and the function @code{minibuffer-complete-and-exit} checks the value
  916. before exiting. If the value is @code{nil}, confirmation is not
  917. required. If the value is @code{confirm}, the user may exit with an
  918. input that is not a valid completion alternative, but Emacs asks for
  919. confirmation. If the value is @code{confirm-after-completion}, the
  920. user may exit with an input that is not a valid completion
  921. alternative, but Emacs asks for confirmation if the user submitted the
  922. input right after any of the completion commands in
  923. @code{minibuffer-confirm-exit-commands}.
  924. @end defvar
  925. @defvar minibuffer-confirm-exit-commands
  926. This variable holds a list of commands that cause Emacs to ask for
  927. confirmation before exiting the minibuffer, if the @var{require-match}
  928. argument to @code{completing-read} is @code{confirm-after-completion}.
  929. The confirmation is requested if the user attempts to exit the
  930. minibuffer immediately after calling any command in this list.
  931. @end defvar
  932. @deffn Command minibuffer-complete-word
  933. This function completes the minibuffer contents by at most a single
  934. word. Even if the minibuffer contents have only one completion,
  935. @code{minibuffer-complete-word} does not add any characters beyond the
  936. first character that is not a word constituent. @xref{Syntax Tables}.
  937. @end deffn
  938. @deffn Command minibuffer-complete
  939. This function completes the minibuffer contents as far as possible.
  940. @end deffn
  941. @deffn Command minibuffer-complete-and-exit
  942. This function completes the minibuffer contents, and exits if
  943. confirmation is not required, i.e., if
  944. @code{minibuffer-completion-confirm} is @code{nil}. If confirmation
  945. @emph{is} required, it is given by repeating this command
  946. immediately---the command is programmed to work without confirmation
  947. when run twice in succession.
  948. @end deffn
  949. @deffn Command minibuffer-completion-help
  950. This function creates a list of the possible completions of the
  951. current minibuffer contents. It works by calling @code{all-completions}
  952. using the value of the variable @code{minibuffer-completion-table} as
  953. the @var{collection} argument, and the value of
  954. @code{minibuffer-completion-predicate} as the @var{predicate} argument.
  955. The list of completions is displayed as text in a buffer named
  956. @file{*Completions*}.
  957. @end deffn
  958. @defun display-completion-list completions
  959. This function displays @var{completions} to the stream in
  960. @code{standard-output}, usually a buffer. (@xref{Read and Print}, for more
  961. information about streams.) The argument @var{completions} is normally
  962. a list of completions just returned by @code{all-completions}, but it
  963. does not have to be. Each element may be a symbol or a string, either
  964. of which is simply printed. It can also be a list of two strings,
  965. which is printed as if the strings were concatenated. The first of
  966. the two strings is the actual completion, the second string serves as
  967. annotation.
  968. This function is called by @code{minibuffer-completion-help}. A
  969. common way to use it is together with
  970. @code{with-output-to-temp-buffer}, like this:
  971. @example
  972. (with-output-to-temp-buffer "*Completions*"
  973. (display-completion-list
  974. (all-completions (buffer-string) my-alist)))
  975. @end example
  976. @end defun
  977. @defopt completion-auto-help
  978. If this variable is non-@code{nil}, the completion commands
  979. automatically display a list of possible completions whenever nothing
  980. can be completed because the next character is not uniquely determined.
  981. @end defopt
  982. @defvar minibuffer-local-completion-map
  983. @code{completing-read} uses this value as the local keymap when an
  984. exact match of one of the completions is not required. By default, this
  985. keymap makes the following bindings:
  986. @table @asis
  987. @item @kbd{?}
  988. @code{minibuffer-completion-help}
  989. @item @key{SPC}
  990. @code{minibuffer-complete-word}
  991. @item @key{TAB}
  992. @code{minibuffer-complete}
  993. @end table
  994. @noindent
  995. and uses @code{minibuffer-local-map} as its parent keymap
  996. (@pxref{Definition of minibuffer-local-map}).
  997. @end defvar
  998. @defvar minibuffer-local-must-match-map
  999. @code{completing-read} uses this value as the local keymap when an
  1000. exact match of one of the completions is required. Therefore, no keys
  1001. are bound to @code{exit-minibuffer}, the command that exits the
  1002. minibuffer unconditionally. By default, this keymap makes the following
  1003. bindings:
  1004. @table @asis
  1005. @item @kbd{C-j}
  1006. @code{minibuffer-complete-and-exit}
  1007. @item @key{RET}
  1008. @code{minibuffer-complete-and-exit}
  1009. @end table
  1010. @noindent
  1011. and uses @code{minibuffer-local-completion-map} as its parent keymap.
  1012. @end defvar
  1013. @defvar minibuffer-local-filename-completion-map
  1014. This is a sparse keymap that simply unbinds @key{SPC}; because
  1015. filenames can contain spaces. The function @code{read-file-name}
  1016. combines this keymap with either @code{minibuffer-local-completion-map}
  1017. or @code{minibuffer-local-must-match-map}.
  1018. @end defvar
  1019. @node High-Level Completion
  1020. @subsection High-Level Completion Functions
  1021. This section describes the higher-level convenience functions for
  1022. reading certain sorts of names with completion.
  1023. In most cases, you should not call these functions in the middle of a
  1024. Lisp function. When possible, do all minibuffer input as part of
  1025. reading the arguments for a command, in the @code{interactive}
  1026. specification. @xref{Defining Commands}.
  1027. @defun read-buffer prompt &optional default require-match
  1028. This function reads the name of a buffer and returns it as a string.
  1029. The argument @var{default} is the default name to use, the value to
  1030. return if the user exits with an empty minibuffer. If non-@code{nil},
  1031. it should be a string, a list of strings, or a buffer. If it is
  1032. a list, the default value is the first element of this list. It is
  1033. mentioned in the prompt, but is not inserted in the minibuffer as
  1034. initial input.
  1035. The argument @var{prompt} should be a string ending with a colon and a
  1036. space. If @var{default} is non-@code{nil}, the function inserts it in
  1037. @var{prompt} before the colon to follow the convention for reading from
  1038. the minibuffer with a default value (@pxref{Programming Tips}).
  1039. The optional argument @var{require-match} has the same meaning as in
  1040. @code{completing-read}. @xref{Minibuffer Completion}.
  1041. In the following example, the user enters @samp{minibuffer.t}, and
  1042. then types @key{RET}. The argument @var{require-match} is @code{t},
  1043. and the only buffer name starting with the given input is
  1044. @samp{minibuffer.texi}, so that name is the value.
  1045. @example
  1046. (read-buffer "Buffer name: " "foo" t)
  1047. @group
  1048. ;; @r{After evaluation of the preceding expression,}
  1049. ;; @r{the following prompt appears,}
  1050. ;; @r{with an empty minibuffer:}
  1051. @end group
  1052. @group
  1053. ---------- Buffer: Minibuffer ----------
  1054. Buffer name (default foo): @point{}
  1055. ---------- Buffer: Minibuffer ----------
  1056. @end group
  1057. @group
  1058. ;; @r{The user types @kbd{minibuffer.t @key{RET}}.}
  1059. @result{} "minibuffer.texi"
  1060. @end group
  1061. @end example
  1062. @end defun
  1063. @defopt read-buffer-function
  1064. This variable, if non-@code{nil}, specifies a function for reading
  1065. buffer names. @code{read-buffer} calls this function instead of doing
  1066. its usual work, with the same arguments passed to @code{read-buffer}.
  1067. @end defopt
  1068. @defopt read-buffer-completion-ignore-case
  1069. If this variable is non-@code{nil}, @code{read-buffer} ignores case
  1070. when performing completion.
  1071. @end defopt
  1072. @defun read-command prompt &optional default
  1073. This function reads the name of a command and returns it as a Lisp
  1074. symbol. The argument @var{prompt} is used as in
  1075. @code{read-from-minibuffer}. Recall that a command is anything for
  1076. which @code{commandp} returns @code{t}, and a command name is a symbol
  1077. for which @code{commandp} returns @code{t}. @xref{Interactive Call}.
  1078. The argument @var{default} specifies what to return if the user enters
  1079. null input. It can be a symbol, a string or a list of strings. If it
  1080. is a string, @code{read-command} interns it before returning it.
  1081. If it is a list, @code{read-command} interns the first element of this list.
  1082. If @var{default} is @code{nil}, that means no default has been
  1083. specified; then if the user enters null input, the return value is
  1084. @code{(intern "")}, that is, a symbol whose name is an empty string.
  1085. @example
  1086. (read-command "Command name? ")
  1087. @group
  1088. ;; @r{After evaluation of the preceding expression,}
  1089. ;; @r{the following prompt appears with an empty minibuffer:}
  1090. @end group
  1091. @group
  1092. ---------- Buffer: Minibuffer ----------
  1093. Command name?
  1094. ---------- Buffer: Minibuffer ----------
  1095. @end group
  1096. @end example
  1097. @noindent
  1098. If the user types @kbd{forward-c @key{RET}}, then this function returns
  1099. @code{forward-char}.
  1100. The @code{read-command} function is a simplified interface to
  1101. @code{completing-read}. It uses the variable @code{obarray} so as to
  1102. complete in the set of extant Lisp symbols, and it uses the
  1103. @code{commandp} predicate so as to accept only command names:
  1104. @cindex @code{commandp} example
  1105. @example
  1106. @group
  1107. (read-command @var{prompt})
  1108. @equiv{}
  1109. (intern (completing-read @var{prompt} obarray
  1110. 'commandp t nil))
  1111. @end group
  1112. @end example
  1113. @end defun
  1114. @defun read-variable prompt &optional default
  1115. @anchor{Definition of read-variable}
  1116. This function reads the name of a customizable variable and returns it
  1117. as a symbol. Its arguments have the same form as those of
  1118. @code{read-command}. It behaves just like @code{read-command}, except
  1119. that it uses the predicate @code{custom-variable-p} instead of
  1120. @code{commandp}.
  1121. @end defun
  1122. @deffn Command read-color &optional prompt convert allow-empty display
  1123. This function reads a string that is a color specification, either the
  1124. color's name or an RGB hex value such as @code{#RRRGGGBBB}. It
  1125. prompts with @var{prompt} (default: @code{"Color (name or #RGB triplet):"})
  1126. and provides completion for color names, but not for hex RGB values.
  1127. In addition to names of standard colors, completion candidates include
  1128. the foreground and background colors at point.
  1129. Valid RGB values are described in @ref{Color Names}.
  1130. The function's return value is the string typed by the user in the
  1131. minibuffer. However, when called interactively or if the optional
  1132. argument @var{convert} is non-@code{nil}, it converts any input color
  1133. name into the corresponding RGB value string and instead returns that.
  1134. This function requires a valid color specification to be input.
  1135. Empty color names are allowed when @var{allow-empty} is
  1136. non-@code{nil} and the user enters null input.
  1137. Interactively, or when @var{display} is non-@code{nil}, the return
  1138. value is also displayed in the echo area.
  1139. @end deffn
  1140. See also the functions @code{read-coding-system} and
  1141. @code{read-non-nil-coding-system}, in @ref{User-Chosen Coding Systems},
  1142. and @code{read-input-method-name}, in @ref{Input Methods}.
  1143. @node Reading File Names
  1144. @subsection Reading File Names
  1145. @cindex read file names
  1146. @cindex prompt for file name
  1147. The high-level completion functions @code{read-file-name},
  1148. @code{read-directory-name}, and @code{read-shell-command} are designed
  1149. to read file names, directory names, and shell commands, respectively.
  1150. They provide special features, including automatic insertion of the
  1151. default directory.
  1152. @defun read-file-name prompt &optional directory default require-match initial predicate
  1153. This function reads a file name, prompting with @var{prompt} and
  1154. providing completion.
  1155. As an exception, this function reads a file name using a graphical
  1156. file dialog instead of the minibuffer, if all of the following are
  1157. true:
  1158. @enumerate
  1159. @item
  1160. It is invoked via a mouse command.
  1161. @item
  1162. The selected frame is on a graphical display supporting such dialogs.
  1163. @item
  1164. The variable @code{use-dialog-box} is non-@code{nil}.
  1165. @xref{Dialog Boxes,, Dialog Boxes, emacs, The GNU Emacs Manual}.
  1166. @item
  1167. The @var{directory} argument, described below, does not specify a
  1168. remote file. @xref{Remote Files,, Remote Files, emacs, The GNU Emacs Manual}.
  1169. @end enumerate
  1170. @noindent
  1171. The exact behavior when using a graphical file dialog is
  1172. platform-dependent. Here, we simply document the behavior when using
  1173. the minibuffer.
  1174. @code{read-file-name} does not automatically expand the returned file
  1175. name. You must call @code{expand-file-name} yourself if an absolute
  1176. file name is required.
  1177. The optional argument @var{require-match} has the same meaning as in
  1178. @code{completing-read}. @xref{Minibuffer Completion}.
  1179. The argument @var{directory} specifies the directory to use for
  1180. completing relative file names. It should be an absolute directory
  1181. name. If the variable @code{insert-default-directory} is non-@code{nil},
  1182. @var{directory} is also inserted in the minibuffer as initial input.
  1183. It defaults to the current buffer's value of @code{default-directory}.
  1184. If you specify @var{initial}, that is an initial file name to insert
  1185. in the buffer (after @var{directory}, if that is inserted). In this
  1186. case, point goes at the beginning of @var{initial}. The default for
  1187. @var{initial} is @code{nil}---don't insert any file name. To see what
  1188. @var{initial} does, try the command @kbd{C-x C-v} in a buffer visiting
  1189. a file. @strong{Please note:} we recommend using @var{default} rather
  1190. than @var{initial} in most cases.
  1191. If @var{default} is non-@code{nil}, then the function returns
  1192. @var{default} if the user exits the minibuffer with the same non-empty
  1193. contents that @code{read-file-name} inserted initially. The initial
  1194. minibuffer contents are always non-empty if
  1195. @code{insert-default-directory} is non-@code{nil}, as it is by
  1196. default. @var{default} is not checked for validity, regardless of the
  1197. value of @var{require-match}. However, if @var{require-match} is
  1198. non-@code{nil}, the initial minibuffer contents should be a valid file
  1199. (or directory) name. Otherwise @code{read-file-name} attempts
  1200. completion if the user exits without any editing, and does not return
  1201. @var{default}. @var{default} is also available through the history
  1202. commands.
  1203. If @var{default} is @code{nil}, @code{read-file-name} tries to find a
  1204. substitute default to use in its place, which it treats in exactly the
  1205. same way as if it had been specified explicitly. If @var{default} is
  1206. @code{nil}, but @var{initial} is non-@code{nil}, then the default is
  1207. the absolute file name obtained from @var{directory} and
  1208. @var{initial}. If both @var{default} and @var{initial} are @code{nil}
  1209. and the buffer is visiting a file, @code{read-file-name} uses the
  1210. absolute file name of that file as default. If the buffer is not
  1211. visiting a file, then there is no default. In that case, if the user
  1212. types @key{RET} without any editing, @code{read-file-name} simply
  1213. returns the pre-inserted contents of the minibuffer.
  1214. If the user types @key{RET} in an empty minibuffer, this function
  1215. returns an empty string, regardless of the value of
  1216. @var{require-match}. This is, for instance, how the user can make the
  1217. current buffer visit no file using @kbd{M-x set-visited-file-name}.
  1218. If @var{predicate} is non-@code{nil}, it specifies a function of one
  1219. argument that decides which file names are acceptable completion
  1220. alternatives. A file name is an acceptable value if @var{predicate}
  1221. returns non-@code{nil} for it.
  1222. Here is an example of using @code{read-file-name}:
  1223. @example
  1224. @group
  1225. (read-file-name "The file is ")
  1226. ;; @r{After evaluation of the preceding expression,}
  1227. ;; @r{the following appears in the minibuffer:}
  1228. @end group
  1229. @group
  1230. ---------- Buffer: Minibuffer ----------
  1231. The file is /gp/gnu/elisp/@point{}
  1232. ---------- Buffer: Minibuffer ----------
  1233. @end group
  1234. @end example
  1235. @noindent
  1236. Typing @kbd{manual @key{TAB}} results in the following:
  1237. @example
  1238. @group
  1239. ---------- Buffer: Minibuffer ----------
  1240. The file is /gp/gnu/elisp/manual.texi@point{}
  1241. ---------- Buffer: Minibuffer ----------
  1242. @end group
  1243. @end example
  1244. @c Wordy to avoid overfull hbox in smallbook mode.
  1245. @noindent
  1246. If the user types @key{RET}, @code{read-file-name} returns the file name
  1247. as the string @code{"/gp/gnu/elisp/manual.texi"}.
  1248. @end defun
  1249. @defvar read-file-name-function
  1250. If non-@code{nil}, this should be a function that accepts the same
  1251. arguments as @code{read-file-name}. When @code{read-file-name} is
  1252. called, it calls this function with the supplied arguments instead of
  1253. doing its usual work.
  1254. @end defvar
  1255. @defopt read-file-name-completion-ignore-case
  1256. If this variable is non-@code{nil}, @code{read-file-name} ignores case
  1257. when performing completion.
  1258. @end defopt
  1259. @defun read-directory-name prompt &optional directory default require-match initial
  1260. This function is like @code{read-file-name} but allows only directory
  1261. names as completion alternatives.
  1262. If @var{default} is @code{nil} and @var{initial} is non-@code{nil},
  1263. @code{read-directory-name} constructs a substitute default by
  1264. combining @var{directory} (or the current buffer's default directory
  1265. if @var{directory} is @code{nil}) and @var{initial}. If both
  1266. @var{default} and @var{initial} are @code{nil}, this function uses
  1267. @var{directory} as substitute default, or the current buffer's default
  1268. directory if @var{directory} is @code{nil}.
  1269. @end defun
  1270. @defopt insert-default-directory
  1271. This variable is used by @code{read-file-name}, and thus, indirectly,
  1272. by most commands reading file names. (This includes all commands that
  1273. use the code letters @samp{f} or @samp{F} in their interactive form.
  1274. @xref{Interactive Codes,, Code Characters for interactive}.) Its
  1275. value controls whether @code{read-file-name} starts by placing the
  1276. name of the default directory in the minibuffer, plus the initial file
  1277. name, if any. If the value of this variable is @code{nil}, then
  1278. @code{read-file-name} does not place any initial input in the
  1279. minibuffer (unless you specify initial input with the @var{initial}
  1280. argument). In that case, the default directory is still used for
  1281. completion of relative file names, but is not displayed.
  1282. If this variable is @code{nil} and the initial minibuffer contents are
  1283. empty, the user may have to explicitly fetch the next history element
  1284. to access a default value. If the variable is non-@code{nil}, the
  1285. initial minibuffer contents are always non-empty and the user can
  1286. always request a default value by immediately typing @key{RET} in an
  1287. unedited minibuffer. (See above.)
  1288. For example:
  1289. @example
  1290. @group
  1291. ;; @r{Here the minibuffer starts out with the default directory.}
  1292. (let ((insert-default-directory t))
  1293. (read-file-name "The file is "))
  1294. @end group
  1295. @group
  1296. ---------- Buffer: Minibuffer ----------
  1297. The file is ~lewis/manual/@point{}
  1298. ---------- Buffer: Minibuffer ----------
  1299. @end group
  1300. @group
  1301. ;; @r{Here the minibuffer is empty and only the prompt}
  1302. ;; @r{appears on its line.}
  1303. (let ((insert-default-directory nil))
  1304. (read-file-name "The file is "))
  1305. @end group
  1306. @group
  1307. ---------- Buffer: Minibuffer ----------
  1308. The file is @point{}
  1309. ---------- Buffer: Minibuffer ----------
  1310. @end group
  1311. @end example
  1312. @end defopt
  1313. @defun read-shell-command prompt &optional initial history &rest args
  1314. This function reads a shell command from the minibuffer, prompting
  1315. with @var{prompt} and providing intelligent completion. It completes
  1316. the first word of the command using candidates that are appropriate
  1317. for command names, and the rest of the command words as file names.
  1318. This function uses @code{minibuffer-local-shell-command-map} as the
  1319. keymap for minibuffer input. The @var{history} argument specifies the
  1320. history list to use; if is omitted or @code{nil}, it defaults to
  1321. @code{shell-command-history} (@pxref{Minibuffer History,
  1322. shell-command-history}). The optional argument @var{initial}
  1323. specifies the initial content of the minibuffer (@pxref{Initial
  1324. Input}). The rest of @var{args}, if present, are used as the
  1325. @var{default} and @var{inherit-input-method} arguments in
  1326. @code{read-from-minibuffer} (@pxref{Text from Minibuffer}).
  1327. @end defun
  1328. @defvar minibuffer-local-shell-command-map
  1329. This keymap is used by @code{read-shell-command} for completing
  1330. command and file names that are part of a shell command. It uses
  1331. @code{minibuffer-local-map} as its parent keymap, and binds @key{TAB}
  1332. to @code{completion-at-point}.
  1333. @end defvar
  1334. @node Completion Variables
  1335. @subsection Completion Variables
  1336. Here are some variables that can be used to alter the default
  1337. completion behavior.
  1338. @cindex completion styles
  1339. @defopt completion-styles
  1340. The value of this variable is a list of completion style (symbols) to
  1341. use for performing completion. A @dfn{completion style} is a set of
  1342. rules for generating completions. Each symbol occurring this list
  1343. must have a corresponding entry in @code{completion-styles-alist}.
  1344. @end defopt
  1345. @defvar completion-styles-alist
  1346. This variable stores a list of available completion styles. Each
  1347. element in the list has the form
  1348. @example
  1349. (@var{style} @var{try-completion} @var{all-completions} @var{doc})
  1350. @end example
  1351. @noindent
  1352. Here, @var{style} is the name of the completion style (a symbol),
  1353. which may be used in the @code{completion-styles} variable to refer to
  1354. this style; @var{try-completion} is the function that does the
  1355. completion; @var{all-completions} is the function that lists the
  1356. completions; and @var{doc} is a string describing the completion
  1357. style.
  1358. The @var{try-completion} and @var{all-completions} functions should
  1359. each accept four arguments: @var{string}, @var{collection},
  1360. @var{predicate}, and @var{point}. The @var{string}, @var{collection},
  1361. and @var{predicate} arguments have the same meanings as in
  1362. @code{try-completion} (@pxref{Basic Completion}), and the @var{point}
  1363. argument is the position of point within @var{string}. Each function
  1364. should return a non-@code{nil} value if it performed its job, and
  1365. @code{nil} if it did not (e.g., if there is no way to complete
  1366. @var{string} according to the completion style).
  1367. When the user calls a completion command like
  1368. @code{minibuffer-complete} (@pxref{Completion Commands}), Emacs looks
  1369. for the first style listed in @code{completion-styles} and calls its
  1370. @var{try-completion} function. If this function returns @code{nil},
  1371. Emacs moves to the next listed completion style and calls its
  1372. @var{try-completion} function, and so on until one of the
  1373. @var{try-completion} functions successfully performs completion and
  1374. returns a non-@code{nil} value. A similar procedure is used for
  1375. listing completions, via the @var{all-completions} functions.
  1376. @xref{Completion Styles,,, emacs, The GNU Emacs Manual}, for a
  1377. description of the available completion styles.
  1378. @end defvar
  1379. @defopt completion-category-overrides
  1380. This variable specifies special completion styles and other completion
  1381. behaviors to use when completing certain types of text. Its value
  1382. should be an alist with elements of the form @code{(@var{category}
  1383. . @var{alist})}. @var{category} is a symbol describing what is being
  1384. completed; currently, the @code{buffer}, @code{file}, and
  1385. @code{unicode-name} categories are defined, but others can be defined
  1386. via specialized completion functions (@pxref{Programmed Completion}).
  1387. @var{alist} is an association list describing how completion should
  1388. behave for the corresponding category. The following alist keys are
  1389. supported:
  1390. @table @code
  1391. @item styles
  1392. The value should be a list of completion styles (symbols).
  1393. @item cycle
  1394. The value should be a value for @code{completion-cycle-threshold}
  1395. (@pxref{Completion Options,,, emacs, The GNU Emacs Manual}) for this
  1396. category.
  1397. @end table
  1398. @noindent
  1399. Additional alist entries may be defined in the future.
  1400. @end defopt
  1401. @defvar completion-extra-properties
  1402. This variable is used to specify extra properties of the current
  1403. completion command. It is intended to be let-bound by specialized
  1404. completion commands. Its value should be a list of property and value
  1405. pairs. The following properties are supported:
  1406. @table @code
  1407. @item :annotation-function
  1408. The value should be a function to add annotations in the completions
  1409. buffer. This function must accept one argument, a completion, and
  1410. should either return @code{nil} or a string to be displayed next to
  1411. the completion.
  1412. @item :exit-function
  1413. The value should be a function to run after performing completion.
  1414. The function should accept two arguments, @var{string} and
  1415. @var{status}, where @var{string} is the text to which the field was
  1416. completed, and @var{status} indicates what kind of operation happened:
  1417. @code{finished} if text is now complete, @code{sole} if the text
  1418. cannot be further completed but completion is not finished, or
  1419. @code{exact} if the text is a valid completion but may be further
  1420. completed.
  1421. @end table
  1422. @end defvar
  1423. @node Programmed Completion
  1424. @subsection Programmed Completion
  1425. @cindex programmed completion
  1426. Sometimes it is not possible or convenient to create an alist or
  1427. an obarray containing all the intended possible completions ahead
  1428. of time. In such a case, you can supply your own function to compute
  1429. the completion of a given string. This is called @dfn{programmed
  1430. completion}. Emacs uses programmed completion when completing file
  1431. names (@pxref{File Name Completion}), among many other cases.
  1432. To use this feature, pass a function as the @var{collection}
  1433. argument to @code{completing-read}. The function
  1434. @code{completing-read} arranges to pass your completion function along
  1435. to @code{try-completion}, @code{all-completions}, and other basic
  1436. completion functions, which will then let your function do all
  1437. the work.
  1438. The completion function should accept three arguments:
  1439. @itemize @bullet
  1440. @item
  1441. The string to be completed.
  1442. @item
  1443. A predicate function with which to filter possible matches, or
  1444. @code{nil} if none. The function should call the predicate for each
  1445. possible match, and ignore the match if the predicate returns
  1446. @code{nil}.
  1447. @item
  1448. A flag specifying the type of completion operation to perform. This
  1449. flag may be one of the following values.
  1450. @table @code
  1451. @item nil
  1452. This specifies a @code{try-completion} operation. The function should
  1453. return @code{t} if the specified string is a unique and exact match;
  1454. if there is more than one match, it should return the common substring
  1455. of all matches (if the string is an exact match for one completion
  1456. alternative but also matches other longer alternatives, the return
  1457. value is the string); if there are no matches, it should return
  1458. @code{nil}.
  1459. @item t
  1460. This specifies an @code{all-completions} operation. The function
  1461. should return a list of all possible completions of the specified
  1462. string.
  1463. @item lambda
  1464. This specifies a @code{test-completion} operation. The function
  1465. should return @code{t} if the specified string is an exact match for
  1466. some completion alternative; @code{nil} otherwise.
  1467. @item (boundaries . @var{suffix})
  1468. This specifies a @code{completion-boundaries} operation. The function
  1469. should return @code{(boundaries @var{start} . @var{end})}, where
  1470. @var{start} is the position of the beginning boundary in the specified
  1471. string, and @var{end} is the position of the end boundary in
  1472. @var{suffix}.
  1473. @item metadata
  1474. This specifies a request for information about the state of the
  1475. current completion. The return value should have the form
  1476. @code{(metadata . @var{alist})}, where @var{alist} is an alist whose
  1477. elements are described below.
  1478. @end table
  1479. @noindent
  1480. If the flag has any other value, the completion function should return
  1481. @code{nil}.
  1482. @end itemize
  1483. The following is a list of metadata entries that a completion function
  1484. may return in response to a @code{metadata} flag argument:
  1485. @table @code
  1486. @item category
  1487. The value should be a symbol describing what kind of text the
  1488. completion function is trying to complete. If the symbol matches one
  1489. of the keys in @code{completion-category-overrides}, the usual
  1490. completion behavior is overridden. @xref{Completion Variables}.
  1491. @item annotation-function
  1492. The value should be a function for @dfn{annotating} completions. The
  1493. function should take one argument, @var{string}, which is a possible
  1494. completion. It should return a string, which is displayed after the
  1495. completion @var{string} in the @file{*Completions*} buffer.
  1496. @item display-sort-function
  1497. The value should be a function for sorting completions. The function
  1498. should take one argument, a list of completion strings, and return a
  1499. sorted list of completion strings. It is allowed to alter the input
  1500. list destructively.
  1501. @item cycle-sort-function
  1502. The value should be a function for sorting completions, when
  1503. @code{completion-cycle-threshold} is non-@code{nil} and the user is
  1504. cycling through completion alternatives. @xref{Completion Options,,,
  1505. emacs, The GNU Emacs Manual}. Its argument list and return value are
  1506. the same as for @code{display-sort-function}.
  1507. @end table
  1508. @defun completion-table-dynamic function
  1509. This function is a convenient way to write a function that can act as
  1510. a programmed completion function. The argument @var{function} should be
  1511. a function that takes one argument, a string, and returns an alist of
  1512. possible completions of it. You can think of
  1513. @code{completion-table-dynamic} as a transducer between that interface
  1514. and the interface for programmed completion functions.
  1515. @end defun
  1516. @defun completion-table-with-cache function &optional ignore-case
  1517. This is a wrapper for @code{completion-table-dynamic} that saves the
  1518. last argument-result pair. This means that multiple lookups with the
  1519. same argument only need to call @var{function} once. This can be useful
  1520. when a slow operation is involved, such as calling an external process.
  1521. @end defun
  1522. @node Completion in Buffers
  1523. @subsection Completion in Ordinary Buffers
  1524. @cindex inline completion
  1525. @findex completion-at-point
  1526. Although completion is usually done in the minibuffer, the
  1527. completion facility can also be used on the text in ordinary Emacs
  1528. buffers. In many major modes, in-buffer completion is performed by
  1529. the @kbd{C-M-i} or @kbd{M-@key{TAB}} command, bound to
  1530. @code{completion-at-point}. @xref{Symbol Completion,,, emacs, The GNU
  1531. Emacs Manual}. This command uses the abnormal hook variable
  1532. @code{completion-at-point-functions}:
  1533. @defvar completion-at-point-functions
  1534. The value of this abnormal hook should be a list of functions, which
  1535. are used to compute a completion table for completing the text at
  1536. point. It can be used by major modes to provide mode-specific
  1537. completion tables (@pxref{Major Mode Conventions}).
  1538. When the command @code{completion-at-point} runs, it calls the
  1539. functions in the list one by one, without any argument. Each function
  1540. should return @code{nil} if it is unable to produce a completion table
  1541. for the text at point. Otherwise it should return a list of the form
  1542. @example
  1543. (@var{start} @var{end} @var{collection} . @var{props})
  1544. @end example
  1545. @noindent
  1546. @var{start} and @var{end} delimit the text to complete (which should
  1547. enclose point). @var{collection} is a completion table for completing
  1548. that text, in a form suitable for passing as the second argument to
  1549. @code{try-completion} (@pxref{Basic Completion}); completion
  1550. alternatives will be generated from this completion table in the usual
  1551. way, via the completion styles defined in @code{completion-styles}
  1552. (@pxref{Completion Variables}). @var{props} is a property list for
  1553. additional information; any of the properties in
  1554. @code{completion-extra-properties} are recognized (@pxref{Completion
  1555. Variables}), as well as the following additional ones:
  1556. @table @code
  1557. @item :predicate
  1558. The value should be a predicate that completion candidates need to
  1559. satisfy.
  1560. @item :exclusive
  1561. If the value is @code{no}, then if the completion table fails to match
  1562. the text at point, @code{completion-at-point} moves on to the
  1563. next function in @code{completion-at-point-functions} instead of
  1564. reporting a completion failure.
  1565. @end table
  1566. Supplying a function for @var{collection} is strongly recommended if
  1567. generating the list of completions is an expensive operation. Emacs
  1568. may internally call functions in @code{completion-at-point-functions}
  1569. many times, but care about the value of @var{collection} for only some
  1570. of these calls. By supplying a function for @var{collection}, Emacs
  1571. can defer generating completions until necessary. You can use
  1572. @var{completion-table-dynamic} to create a wrapper function:
  1573. @smallexample
  1574. ;; Avoid this pattern.
  1575. (let ((beg ...) (end ...) (my-completions (my-make-completions)))
  1576. (list beg end my-completions))
  1577. ;; Use this instead.
  1578. (let ((beg ...) (end ...))
  1579. (list beg
  1580. end
  1581. (completion-table-dynamic
  1582. (lambda (_)
  1583. (my-make-completions)))))
  1584. @end smallexample
  1585. A function in @code{completion-at-point-functions} may also return a
  1586. function instead of a list as described above. In that case, that
  1587. returned function is called, with no argument, and it is entirely
  1588. responsible for performing the completion. We discourage this usage;
  1589. it is intended to help convert old code to using
  1590. @code{completion-at-point}.
  1591. The first function in @code{completion-at-point-functions} to return a
  1592. non-@code{nil} value is used by @code{completion-at-point}. The
  1593. remaining functions are not called. The exception to this is when
  1594. there is an @code{:exclusive} specification, as described above.
  1595. @end defvar
  1596. The following function provides a convenient way to perform
  1597. completion on an arbitrary stretch of text in an Emacs buffer:
  1598. @defun completion-in-region start end collection &optional predicate
  1599. This function completes the text in the current buffer between the
  1600. positions @var{start} and @var{end}, using @var{collection}. The
  1601. argument @var{collection} has the same meaning as in
  1602. @code{try-completion} (@pxref{Basic Completion}).
  1603. This function inserts the completion text directly into the current
  1604. buffer. Unlike @code{completing-read} (@pxref{Minibuffer
  1605. Completion}), it does not activate the minibuffer.
  1606. For this function to work, point must be somewhere between @var{start}
  1607. and @var{end}.
  1608. @end defun
  1609. @node Yes-or-No Queries
  1610. @section Yes-or-No Queries
  1611. @cindex asking the user questions
  1612. @cindex querying the user
  1613. @cindex yes-or-no questions
  1614. This section describes functions used to ask the user a yes-or-no
  1615. question. The function @code{y-or-n-p} can be answered with a single
  1616. character; it is useful for questions where an inadvertent wrong answer
  1617. will not have serious consequences. @code{yes-or-no-p} is suitable for
  1618. more momentous questions, since it requires three or four characters to
  1619. answer.
  1620. If either of these functions is called in a command that was invoked
  1621. using the mouse---more precisely, if @code{last-nonmenu-event}
  1622. (@pxref{Command Loop Info}) is either @code{nil} or a list---then it
  1623. uses a dialog box or pop-up menu to ask the question. Otherwise, it
  1624. uses keyboard input. You can force use either of the mouse or of keyboard
  1625. input by binding @code{last-nonmenu-event} to a suitable value around
  1626. the call.
  1627. Strictly speaking, @code{yes-or-no-p} uses the minibuffer and
  1628. @code{y-or-n-p} does not; but it seems best to describe them together.
  1629. @defun y-or-n-p prompt
  1630. This function asks the user a question, expecting input in the echo
  1631. area. It returns @code{t} if the user types @kbd{y}, @code{nil} if the
  1632. user types @kbd{n}. This function also accepts @key{SPC} to mean yes
  1633. and @key{DEL} to mean no. It accepts @kbd{C-]} to mean ``quit'', like
  1634. @kbd{C-g}, because the question might look like a minibuffer and for
  1635. that reason the user might try to use @kbd{C-]} to get out. The answer
  1636. is a single character, with no @key{RET} needed to terminate it. Upper
  1637. and lower case are equivalent.
  1638. ``Asking the question'' means printing @var{prompt} in the echo area,
  1639. followed by the string @w{@samp{(y or n) }}. If the input is not one of
  1640. the expected answers (@kbd{y}, @kbd{n}, @kbd{@key{SPC}},
  1641. @kbd{@key{DEL}}, or something that quits), the function responds
  1642. @samp{Please answer y or n.}, and repeats the request.
  1643. This function does not actually use the minibuffer, since it does not
  1644. allow editing of the answer. It actually uses the echo area (@pxref{The
  1645. Echo Area}), which uses the same screen space as the minibuffer. The
  1646. cursor moves to the echo area while the question is being asked.
  1647. The answers and their meanings, even @samp{y} and @samp{n}, are not
  1648. hardwired, and are specified by the keymap @code{query-replace-map}
  1649. (@pxref{Search and Replace}). In particular, if the user enters the
  1650. special responses @code{recenter}, @code{scroll-up},
  1651. @code{scroll-down}, @code{scroll-other-window}, or
  1652. @code{scroll-other-window-down} (respectively bound to @kbd{C-l},
  1653. @kbd{C-v}, @kbd{M-v}, @kbd{C-M-v} and @kbd{C-M-S-v} in
  1654. @code{query-replace-map}), this function performs the specified window
  1655. recentering or scrolling operation, and poses the question again.
  1656. @noindent
  1657. We show successive lines of echo area messages, but only one actually
  1658. appears on the screen at a time.
  1659. @end defun
  1660. @defun y-or-n-p-with-timeout prompt seconds default
  1661. Like @code{y-or-n-p}, except that if the user fails to answer within
  1662. @var{seconds} seconds, this function stops waiting and returns
  1663. @var{default}. It works by setting up a timer; see @ref{Timers}.
  1664. The argument @var{seconds} should be a number.
  1665. @end defun
  1666. @defun yes-or-no-p prompt
  1667. This function asks the user a question, expecting input in the
  1668. minibuffer. It returns @code{t} if the user enters @samp{yes},
  1669. @code{nil} if the user types @samp{no}. The user must type @key{RET} to
  1670. finalize the response. Upper and lower case are equivalent.
  1671. @code{yes-or-no-p} starts by displaying @var{prompt} in the echo area,
  1672. followed by @w{@samp{(yes or no) }}. The user must type one of the
  1673. expected responses; otherwise, the function responds @samp{Please answer
  1674. yes or no.}, waits about two seconds and repeats the request.
  1675. @code{yes-or-no-p} requires more work from the user than
  1676. @code{y-or-n-p} and is appropriate for more crucial decisions.
  1677. Here is an example:
  1678. @smallexample
  1679. @group
  1680. (yes-or-no-p "Do you really want to remove everything? ")
  1681. ;; @r{After evaluation of the preceding expression,}
  1682. ;; @r{the following prompt appears,}
  1683. ;; @r{with an empty minibuffer:}
  1684. @end group
  1685. @group
  1686. ---------- Buffer: minibuffer ----------
  1687. Do you really want to remove everything? (yes or no)
  1688. ---------- Buffer: minibuffer ----------
  1689. @end group
  1690. @end smallexample
  1691. @noindent
  1692. If the user first types @kbd{y @key{RET}}, which is invalid because this
  1693. function demands the entire word @samp{yes}, it responds by displaying
  1694. these prompts, with a brief pause between them:
  1695. @smallexample
  1696. @group
  1697. ---------- Buffer: minibuffer ----------
  1698. Please answer yes or no.
  1699. Do you really want to remove everything? (yes or no)
  1700. ---------- Buffer: minibuffer ----------
  1701. @end group
  1702. @end smallexample
  1703. @end defun
  1704. @node Multiple Queries
  1705. @section Asking Multiple Y-or-N Questions
  1706. @cindex multiple yes-or-no questions
  1707. When you have a series of similar questions to ask, such as ``Do you
  1708. want to save this buffer'' for each buffer in turn, you should use
  1709. @code{map-y-or-n-p} to ask the collection of questions, rather than
  1710. asking each question individually. This gives the user certain
  1711. convenient facilities such as the ability to answer the whole series at
  1712. once.
  1713. @defun map-y-or-n-p prompter actor list &optional help action-alist no-cursor-in-echo-area
  1714. This function asks the user a series of questions, reading a
  1715. single-character answer in the echo area for each one.
  1716. The value of @var{list} specifies the objects to ask questions about.
  1717. It should be either a list of objects or a generator function. If it is
  1718. a function, it should expect no arguments, and should return either the
  1719. next object to ask about, or @code{nil}, meaning to stop asking questions.
  1720. The argument @var{prompter} specifies how to ask each question. If
  1721. @var{prompter} is a string, the question text is computed like this:
  1722. @example
  1723. (format @var{prompter} @var{object})
  1724. @end example
  1725. @noindent
  1726. where @var{object} is the next object to ask about (as obtained from
  1727. @var{list}).
  1728. If not a string, @var{prompter} should be a function of one argument
  1729. (the next object to ask about) and should return the question text. If
  1730. the value is a string, that is the question to ask the user. The
  1731. function can also return @code{t}, meaning do act on this object (and
  1732. don't ask the user), or @code{nil}, meaning ignore this object (and don't
  1733. ask the user).
  1734. The argument @var{actor} says how to act on the answers that the user
  1735. gives. It should be a function of one argument, and it is called with
  1736. each object that the user says yes for. Its argument is always an
  1737. object obtained from @var{list}.
  1738. If the argument @var{help} is given, it should be a list of this form:
  1739. @example
  1740. (@var{singular} @var{plural} @var{action})
  1741. @end example
  1742. @noindent
  1743. where @var{singular} is a string containing a singular noun that
  1744. describes the objects conceptually being acted on, @var{plural} is the
  1745. corresponding plural noun, and @var{action} is a transitive verb
  1746. describing what @var{actor} does.
  1747. If you don't specify @var{help}, the default is @code{("object"
  1748. "objects" "act on")}.
  1749. Each time a question is asked, the user may enter @kbd{y}, @kbd{Y}, or
  1750. @key{SPC} to act on that object; @kbd{n}, @kbd{N}, or @key{DEL} to skip
  1751. that object; @kbd{!} to act on all following objects; @key{ESC} or
  1752. @kbd{q} to exit (skip all following objects); @kbd{.} (period) to act on
  1753. the current object and then exit; or @kbd{C-h} to get help. These are
  1754. the same answers that @code{query-replace} accepts. The keymap
  1755. @code{query-replace-map} defines their meaning for @code{map-y-or-n-p}
  1756. as well as for @code{query-replace}; see @ref{Search and Replace}.
  1757. You can use @var{action-alist} to specify additional possible answers
  1758. and what they mean. It is an alist of elements of the form
  1759. @code{(@var{char} @var{function} @var{help})}, each of which defines one
  1760. additional answer. In this element, @var{char} is a character (the
  1761. answer); @var{function} is a function of one argument (an object from
  1762. @var{list}); @var{help} is a string.
  1763. When the user responds with @var{char}, @code{map-y-or-n-p} calls
  1764. @var{function}. If it returns non-@code{nil}, the object is considered
  1765. ``acted upon'', and @code{map-y-or-n-p} advances to the next object in
  1766. @var{list}. If it returns @code{nil}, the prompt is repeated for the
  1767. same object.
  1768. Normally, @code{map-y-or-n-p} binds @code{cursor-in-echo-area} while
  1769. prompting. But if @var{no-cursor-in-echo-area} is non-@code{nil}, it
  1770. does not do that.
  1771. If @code{map-y-or-n-p} is called in a command that was invoked using the
  1772. mouse---more precisely, if @code{last-nonmenu-event} (@pxref{Command
  1773. Loop Info}) is either @code{nil} or a list---then it uses a dialog box
  1774. or pop-up menu to ask the question. In this case, it does not use
  1775. keyboard input or the echo area. You can force use either of the mouse or
  1776. of keyboard input by binding @code{last-nonmenu-event} to a suitable
  1777. value around the call.
  1778. The return value of @code{map-y-or-n-p} is the number of objects acted on.
  1779. @end defun
  1780. @c FIXME An example of this would be more useful than all the
  1781. @c preceding examples of simple things.
  1782. @node Reading a Password
  1783. @section Reading a Password
  1784. @cindex passwords, reading
  1785. To read a password to pass to another program, you can use the
  1786. function @code{read-passwd}.
  1787. @defun read-passwd prompt &optional confirm default
  1788. This function reads a password, prompting with @var{prompt}. It does
  1789. not echo the password as the user types it; instead, it echoes
  1790. @samp{.} for each character in the password. If you want to apply
  1791. another character to hide the password, let-bind the variable
  1792. @code{read-hide-char} with that character.
  1793. The optional argument @var{confirm}, if non-@code{nil}, says to read the
  1794. password twice and insist it must be the same both times. If it isn't
  1795. the same, the user has to type it over and over until the last two
  1796. times match.
  1797. The optional argument @var{default} specifies the default password to
  1798. return if the user enters empty input. If @var{default} is @code{nil},
  1799. then @code{read-passwd} returns the null string in that case.
  1800. @end defun
  1801. @node Minibuffer Commands
  1802. @section Minibuffer Commands
  1803. This section describes some commands meant for use in the
  1804. minibuffer.
  1805. @deffn Command exit-minibuffer
  1806. This command exits the active minibuffer. It is normally bound to
  1807. keys in minibuffer local keymaps.
  1808. @end deffn
  1809. @deffn Command self-insert-and-exit
  1810. This command exits the active minibuffer after inserting the last
  1811. character typed on the keyboard (found in @code{last-command-event};
  1812. @pxref{Command Loop Info}).
  1813. @end deffn
  1814. @deffn Command previous-history-element n
  1815. This command replaces the minibuffer contents with the value of the
  1816. @var{n}th previous (older) history element.
  1817. @end deffn
  1818. @deffn Command next-history-element n
  1819. This command replaces the minibuffer contents with the value of the
  1820. @var{n}th more recent history element.
  1821. @end deffn
  1822. @deffn Command previous-matching-history-element pattern n
  1823. This command replaces the minibuffer contents with the value of the
  1824. @var{n}th previous (older) history element that matches @var{pattern} (a
  1825. regular expression).
  1826. @end deffn
  1827. @deffn Command next-matching-history-element pattern n
  1828. This command replaces the minibuffer contents with the value of the
  1829. @var{n}th next (newer) history element that matches @var{pattern} (a
  1830. regular expression).
  1831. @end deffn
  1832. @deffn Command previous-complete-history-element n
  1833. This command replaces the minibuffer contents with the value of the
  1834. @var{n}th previous (older) history element that completes the current
  1835. contents of the minibuffer before the point.
  1836. @end deffn
  1837. @deffn Command next-complete-history-element n
  1838. This command replaces the minibuffer contents with the value of the
  1839. @var{n}th next (newer) history element that completes the current
  1840. contents of the minibuffer before the point.
  1841. @end deffn
  1842. @node Minibuffer Windows
  1843. @section Minibuffer Windows
  1844. @cindex minibuffer windows
  1845. These functions access and select minibuffer windows, test whether they
  1846. are active and control how they get resized.
  1847. @defun active-minibuffer-window
  1848. This function returns the currently active minibuffer window, or
  1849. @code{nil} if there is none.
  1850. @end defun
  1851. @defun minibuffer-window &optional frame
  1852. @anchor{Definition of minibuffer-window}
  1853. This function returns the minibuffer window used for frame @var{frame}.
  1854. If @var{frame} is @code{nil}, that stands for the current frame. Note
  1855. that the minibuffer window used by a frame need not be part of that
  1856. frame---a frame that has no minibuffer of its own necessarily uses some
  1857. other frame's minibuffer window.
  1858. @end defun
  1859. @defun set-minibuffer-window window
  1860. This function specifies @var{window} as the minibuffer window to use.
  1861. This affects where the minibuffer is displayed if you put text in it
  1862. without invoking the usual minibuffer commands. It has no effect on
  1863. the usual minibuffer input functions because they all start by
  1864. choosing the minibuffer window according to the current frame.
  1865. @end defun
  1866. @c Emacs 19 feature
  1867. @defun window-minibuffer-p &optional window
  1868. This function returns non-@code{nil} if @var{window} is a minibuffer
  1869. window.
  1870. @var{window} defaults to the selected window.
  1871. @end defun
  1872. It is not correct to determine whether a given window is a minibuffer by
  1873. comparing it with the result of @code{(minibuffer-window)}, because
  1874. there can be more than one minibuffer window if there is more than one
  1875. frame.
  1876. @defun minibuffer-window-active-p window
  1877. This function returns non-@code{nil} if @var{window} is the currently
  1878. active minibuffer window.
  1879. @end defun
  1880. The following two options control whether minibuffer windows are resized
  1881. automatically and how large they can get in the process.
  1882. @defopt resize-mini-windows
  1883. This option specifies whether minibuffer windows are resized
  1884. automatically. The default value is @code{grow-only}, which means that
  1885. a minibuffer window by default expands automatically to accommodate the
  1886. text it displays and shrinks back to one line as soon as the minibuffer
  1887. gets empty. If the value is @code{t}, Emacs will always try to fit the
  1888. height of a minibuffer window to the text it displays (with a minimum of
  1889. one line). If the value is @code{nil}, a minibuffer window never
  1890. changes size automatically. In that case the window resizing commands
  1891. (@pxref{Resizing Windows}) can be used to adjust its height.
  1892. @end defopt
  1893. @defopt max-mini-window-height
  1894. This option provides a maximum height for resizing minibuffer windows
  1895. automatically. A floating-point number specifies a fraction of the
  1896. frame's height; an integer specifies the maximum number of lines. The
  1897. default value is 0.25.
  1898. @end defopt
  1899. @node Minibuffer Contents
  1900. @section Minibuffer Contents
  1901. @cindex access minibuffer contents
  1902. @cindex minibuffer contents, accessing
  1903. These functions access the minibuffer prompt and contents.
  1904. @defun minibuffer-prompt
  1905. This function returns the prompt string of the currently active
  1906. minibuffer. If no minibuffer is active, it returns @code{nil}.
  1907. @end defun
  1908. @defun minibuffer-prompt-end
  1909. This function returns the current
  1910. position of the end of the minibuffer prompt, if a minibuffer is
  1911. current. Otherwise, it returns the minimum valid buffer position.
  1912. @end defun
  1913. @defun minibuffer-prompt-width
  1914. This function returns the current display-width of the minibuffer
  1915. prompt, if a minibuffer is current. Otherwise, it returns zero.
  1916. @end defun
  1917. @defun minibuffer-contents
  1918. This function returns the editable
  1919. contents of the minibuffer (that is, everything except the prompt) as
  1920. a string, if a minibuffer is current. Otherwise, it returns the
  1921. entire contents of the current buffer.
  1922. @end defun
  1923. @defun minibuffer-contents-no-properties
  1924. This is like @code{minibuffer-contents}, except that it does not copy text
  1925. properties, just the characters themselves. @xref{Text Properties}.
  1926. @end defun
  1927. @defun delete-minibuffer-contents
  1928. This function erases the editable contents of the minibuffer (that is,
  1929. everything except the prompt), if a minibuffer is current. Otherwise,
  1930. it erases the entire current buffer.
  1931. @end defun
  1932. @node Recursive Mini
  1933. @section Recursive Minibuffers
  1934. @cindex recursive minibuffers
  1935. These functions and variables deal with recursive minibuffers
  1936. (@pxref{Recursive Editing}):
  1937. @defun minibuffer-depth
  1938. This function returns the current depth of activations of the
  1939. minibuffer, a nonnegative integer. If no minibuffers are active, it
  1940. returns zero.
  1941. @end defun
  1942. @defopt enable-recursive-minibuffers
  1943. If this variable is non-@code{nil}, you can invoke commands (such as
  1944. @code{find-file}) that use minibuffers even while the minibuffer window
  1945. is active. Such invocation produces a recursive editing level for a new
  1946. minibuffer. The outer-level minibuffer is invisible while you are
  1947. editing the inner one.
  1948. If this variable is @code{nil}, you cannot invoke minibuffer
  1949. commands when the minibuffer window is active, not even if you switch to
  1950. another window to do it.
  1951. @end defopt
  1952. @c Emacs 19 feature
  1953. If a command name has a property @code{enable-recursive-minibuffers}
  1954. that is non-@code{nil}, then the command can use the minibuffer to read
  1955. arguments even if it is invoked from the minibuffer. A command can
  1956. also achieve this by binding @code{enable-recursive-minibuffers}
  1957. to @code{t} in the interactive declaration (@pxref{Using Interactive}).
  1958. The minibuffer command @code{next-matching-history-element} (normally
  1959. @kbd{M-s} in the minibuffer) does the latter.
  1960. @node Minibuffer Misc
  1961. @section Minibuffer Miscellany
  1962. @defun minibufferp &optional buffer-or-name
  1963. This function returns non-@code{nil} if @var{buffer-or-name} is a
  1964. minibuffer. If @var{buffer-or-name} is omitted, it tests the current
  1965. buffer.
  1966. @end defun
  1967. @defvar minibuffer-setup-hook
  1968. This is a normal hook that is run whenever the minibuffer is entered.
  1969. @xref{Hooks}.
  1970. @end defvar
  1971. @defvar minibuffer-exit-hook
  1972. This is a normal hook that is run whenever the minibuffer is exited.
  1973. @xref{Hooks}.
  1974. @end defvar
  1975. @defvar minibuffer-help-form
  1976. @anchor{Definition of minibuffer-help-form}
  1977. The current value of this variable is used to rebind @code{help-form}
  1978. locally inside the minibuffer (@pxref{Help Functions}).
  1979. @end defvar
  1980. @defvar minibuffer-scroll-window
  1981. @anchor{Definition of minibuffer-scroll-window}
  1982. If the value of this variable is non-@code{nil}, it should be a window
  1983. object. When the function @code{scroll-other-window} is called in the
  1984. minibuffer, it scrolls this window.
  1985. @end defvar
  1986. @defun minibuffer-selected-window
  1987. This function returns the window that was selected when the
  1988. minibuffer was entered. If selected window is not a minibuffer
  1989. window, it returns @code{nil}.
  1990. @end defun
  1991. @defopt max-mini-window-height
  1992. This variable specifies the maximum height for resizing minibuffer
  1993. windows. If a float, it specifies a fraction of the height of the
  1994. frame. If an integer, it specifies a number of lines.
  1995. @end defopt
  1996. @vindex minibuffer-message-timeout
  1997. @defun minibuffer-message string &rest args
  1998. This function displays @var{string} temporarily at the end of the
  1999. minibuffer text, for a few seconds, or until the next input event
  2000. arrives, whichever comes first. The variable
  2001. @code{minibuffer-message-timeout} specifies the number of seconds to
  2002. wait in the absence of input. It defaults to 2. If @var{args} is
  2003. non-@code{nil}, the actual message is obtained by passing @var{string}
  2004. and @var{args} through @code{format}. @xref{Formatting Strings}.
  2005. @end defun
  2006. @deffn Command minibuffer-inactive-mode
  2007. This is the major mode used in inactive minibuffers. It uses
  2008. keymap @code{minibuffer-inactive-mode-map}. This can be useful
  2009. if the minibuffer is in a separate frame. @xref{Minibuffers and Frames}.
  2010. @end deffn