commands.texi 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990-1995, 1998-1999, 2001-2016 Free Software
  4. @c Foundation, Inc.
  5. @c See the file elisp.texi for copying conditions.
  6. @node Command Loop
  7. @chapter Command Loop
  8. @cindex editor command loop
  9. @cindex command loop
  10. When you run Emacs, it enters the @dfn{editor command loop} almost
  11. immediately. This loop reads key sequences, executes their definitions,
  12. and displays the results. In this chapter, we describe how these things
  13. are done, and the subroutines that allow Lisp programs to do them.
  14. @menu
  15. * Command Overview:: How the command loop reads commands.
  16. * Defining Commands:: Specifying how a function should read arguments.
  17. * Interactive Call:: Calling a command, so that it will read arguments.
  18. * Distinguish Interactive:: Making a command distinguish interactive calls.
  19. * Command Loop Info:: Variables set by the command loop for you to examine.
  20. * Adjusting Point:: Adjustment of point after a command.
  21. * Input Events:: What input looks like when you read it.
  22. * Reading Input:: How to read input events from the keyboard or mouse.
  23. * Special Events:: Events processed immediately and individually.
  24. * Waiting:: Waiting for user input or elapsed time.
  25. * Quitting:: How @kbd{C-g} works. How to catch or defer quitting.
  26. * Prefix Command Arguments:: How the commands to set prefix args work.
  27. * Recursive Editing:: Entering a recursive edit,
  28. and why you usually shouldn't.
  29. * Disabling Commands:: How the command loop handles disabled commands.
  30. * Command History:: How the command history is set up, and how accessed.
  31. * Keyboard Macros:: How keyboard macros are implemented.
  32. @end menu
  33. @node Command Overview
  34. @section Command Loop Overview
  35. The first thing the command loop must do is read a key sequence,
  36. which is a sequence of input events that translates into a command.
  37. It does this by calling the function @code{read-key-sequence}. Lisp
  38. programs can also call this function (@pxref{Key Sequence Input}).
  39. They can also read input at a lower level with @code{read-key} or
  40. @code{read-event} (@pxref{Reading One Event}), or discard pending
  41. input with @code{discard-input} (@pxref{Event Input Misc}).
  42. The key sequence is translated into a command through the currently
  43. active keymaps. @xref{Key Lookup}, for information on how this is done.
  44. The result should be a keyboard macro or an interactively callable
  45. function. If the key is @kbd{M-x}, then it reads the name of another
  46. command, which it then calls. This is done by the command
  47. @code{execute-extended-command} (@pxref{Interactive Call}).
  48. Prior to executing the command, Emacs runs @code{undo-boundary} to
  49. create an undo boundary. @xref{Maintaining Undo}.
  50. To execute a command, Emacs first reads its arguments by calling
  51. @code{command-execute} (@pxref{Interactive Call}). For commands
  52. written in Lisp, the @code{interactive} specification says how to read
  53. the arguments. This may use the prefix argument (@pxref{Prefix
  54. Command Arguments}) or may read with prompting in the minibuffer
  55. (@pxref{Minibuffers}). For example, the command @code{find-file} has
  56. an @code{interactive} specification which says to read a file name
  57. using the minibuffer. The function body of @code{find-file} does not
  58. use the minibuffer, so if you call @code{find-file} as a function from
  59. Lisp code, you must supply the file name string as an ordinary Lisp
  60. function argument.
  61. If the command is a keyboard macro (i.e., a string or vector),
  62. Emacs executes it using @code{execute-kbd-macro} (@pxref{Keyboard
  63. Macros}).
  64. @defvar pre-command-hook
  65. This normal hook is run by the editor command loop before it executes
  66. each command. At that time, @code{this-command} contains the command
  67. that is about to run, and @code{last-command} describes the previous
  68. command. @xref{Command Loop Info}.
  69. @end defvar
  70. @defvar post-command-hook
  71. This normal hook is run by the editor command loop after it executes
  72. each command (including commands terminated prematurely by quitting or
  73. by errors). At that time, @code{this-command} refers to the command
  74. that just ran, and @code{last-command} refers to the command before
  75. that.
  76. This hook is also run when Emacs first enters the command loop (at
  77. which point @code{this-command} and @code{last-command} are both
  78. @code{nil}).
  79. @end defvar
  80. Quitting is suppressed while running @code{pre-command-hook} and
  81. @code{post-command-hook}. If an error happens while executing one of
  82. these hooks, it does not terminate execution of the hook; instead
  83. the error is silenced and the function in which the error occurred
  84. is removed from the hook.
  85. A request coming into the Emacs server (@pxref{Emacs Server,,,
  86. emacs, The GNU Emacs Manual}) runs these two hooks just as a keyboard
  87. command does.
  88. @node Defining Commands
  89. @section Defining Commands
  90. @cindex defining commands
  91. @cindex commands, defining
  92. @cindex functions, making them interactive
  93. @cindex interactive function
  94. The special form @code{interactive} turns a Lisp function into a
  95. command. The @code{interactive} form must be located at top-level in
  96. the function body, usually as the first form in the body; this applies
  97. to both lambda expressions (@pxref{Lambda Expressions}) and
  98. @code{defun} forms (@pxref{Defining Functions}). This form does
  99. nothing during the actual execution of the function; its presence
  100. serves as a flag, telling the Emacs command loop that the function can
  101. be called interactively. The argument of the @code{interactive} form
  102. specifies how the arguments for an interactive call should be read.
  103. @cindex @code{interactive-form} property
  104. Alternatively, an @code{interactive} form may be specified in a
  105. function symbol's @code{interactive-form} property. A non-@code{nil}
  106. value for this property takes precedence over any @code{interactive}
  107. form in the function body itself. This feature is seldom used.
  108. @anchor{The interactive-only property}
  109. @cindex @code{interactive-only} property
  110. Sometimes, a function is only intended to be called interactively,
  111. never directly from Lisp. In that case, give the function a
  112. non-@code{nil} @code{interactive-only} property, either directly
  113. or via @code{declare} (@pxref{Declare Form}). This causes the
  114. byte compiler to warn if the command is called from Lisp. The output
  115. of @code{describe-function} will include similar information.
  116. The value of the property can be: a string, which the byte-compiler
  117. will use directly in its warning (it should end with a period, and not
  118. start with a capital, e.g., @code{"use (system-name) instead."}); @code{t}; any
  119. other symbol, which should be an alternative function to use in Lisp
  120. code.
  121. @menu
  122. * Using Interactive:: General rules for @code{interactive}.
  123. * Interactive Codes:: The standard letter-codes for reading arguments
  124. in various ways.
  125. * Interactive Examples:: Examples of how to read interactive arguments.
  126. * Generic Commands:: Select among command alternatives.
  127. @end menu
  128. @node Using Interactive
  129. @subsection Using @code{interactive}
  130. @cindex arguments, interactive entry
  131. @cindex interactive spec, using
  132. This section describes how to write the @code{interactive} form that
  133. makes a Lisp function an interactively-callable command, and how to
  134. examine a command's @code{interactive} form.
  135. @defspec interactive arg-descriptor
  136. This special form declares that a function is a command, and that it
  137. may therefore be called interactively (via @kbd{M-x} or by entering a
  138. key sequence bound to it). The argument @var{arg-descriptor} declares
  139. how to compute the arguments to the command when the command is called
  140. interactively.
  141. A command may be called from Lisp programs like any other function, but
  142. then the caller supplies the arguments and @var{arg-descriptor} has no
  143. effect.
  144. @cindex @code{interactive-form}, symbol property
  145. The @code{interactive} form must be located at top-level in the
  146. function body, or in the function symbol's @code{interactive-form}
  147. property (@pxref{Symbol Properties}). It has its effect because the
  148. command loop looks for it before calling the function
  149. (@pxref{Interactive Call}). Once the function is called, all its body
  150. forms are executed; at this time, if the @code{interactive} form
  151. occurs within the body, the form simply returns @code{nil} without
  152. even evaluating its argument.
  153. By convention, you should put the @code{interactive} form in the
  154. function body, as the first top-level form. If there is an
  155. @code{interactive} form in both the @code{interactive-form} symbol
  156. property and the function body, the former takes precedence. The
  157. @code{interactive-form} symbol property can be used to add an
  158. interactive form to an existing function, or change how its arguments
  159. are processed interactively, without redefining the function.
  160. @end defspec
  161. There are three possibilities for the argument @var{arg-descriptor}:
  162. @itemize @bullet
  163. @item
  164. It may be omitted or @code{nil}; then the command is called with no
  165. arguments. This leads quickly to an error if the command requires one
  166. or more arguments.
  167. @item
  168. It may be a string; its contents are a sequence of elements separated
  169. by newlines, one for each argument@footnote{Some elements actually
  170. supply two arguments.}. Each element consists of a code character
  171. (@pxref{Interactive Codes}) optionally followed by a prompt (which
  172. some code characters use and some ignore). Here is an example:
  173. @smallexample
  174. (interactive "P\nbFrobnicate buffer: ")
  175. @end smallexample
  176. @noindent
  177. The code letter @samp{P} sets the command's first argument to the raw
  178. command prefix (@pxref{Prefix Command Arguments}). @samp{bFrobnicate
  179. buffer: } prompts the user with @samp{Frobnicate buffer: } to enter
  180. the name of an existing buffer, which becomes the second and final
  181. argument.
  182. The prompt string can use @samp{%} to include previous argument values
  183. (starting with the first argument) in the prompt. This is done using
  184. @code{format-message} (@pxref{Formatting Strings}). For example, here is how
  185. you could read the name of an existing buffer followed by a new name to
  186. give to that buffer:
  187. @smallexample
  188. @group
  189. (interactive "bBuffer to rename: \nsRename buffer %s to: ")
  190. @end group
  191. @end smallexample
  192. @cindex @samp{*} in @code{interactive}
  193. @cindex read-only buffers in interactive
  194. If @samp{*} appears at the beginning of the string, then an error is
  195. signaled if the buffer is read-only.
  196. @cindex @samp{@@} in @code{interactive}
  197. If @samp{@@} appears at the beginning of the string, and if the key
  198. sequence used to invoke the command includes any mouse events, then
  199. the window associated with the first of those events is selected
  200. before the command is run.
  201. @cindex @samp{^} in @code{interactive}
  202. @cindex shift-selection, and @code{interactive} spec
  203. If @samp{^} appears at the beginning of the string, and if the command
  204. was invoked through @dfn{shift-translation}, set the mark and activate
  205. the region temporarily, or extend an already active region, before the
  206. command is run. If the command was invoked without shift-translation,
  207. and the region is temporarily active, deactivate the region before the
  208. command is run. Shift-translation is controlled on the user level by
  209. @code{shift-select-mode}; see @ref{Shift Selection,,, emacs, The GNU
  210. Emacs Manual}.
  211. You can use @samp{*}, @samp{@@}, and @code{^} together; the order does
  212. not matter. Actual reading of arguments is controlled by the rest of
  213. the prompt string (starting with the first character that is not
  214. @samp{*}, @samp{@@}, or @samp{^}).
  215. @item
  216. It may be a Lisp expression that is not a string; then it should be a
  217. form that is evaluated to get a list of arguments to pass to the
  218. command. Usually this form will call various functions to read input
  219. from the user, most often through the minibuffer (@pxref{Minibuffers})
  220. or directly from the keyboard (@pxref{Reading Input}).
  221. Providing point or the mark as an argument value is also common, but
  222. if you do this @emph{and} read input (whether using the minibuffer or
  223. not), be sure to get the integer values of point or the mark after
  224. reading. The current buffer may be receiving subprocess output; if
  225. subprocess output arrives while the command is waiting for input, it
  226. could relocate point and the mark.
  227. Here's an example of what @emph{not} to do:
  228. @smallexample
  229. (interactive
  230. (list (region-beginning) (region-end)
  231. (read-string "Foo: " nil 'my-history)))
  232. @end smallexample
  233. @noindent
  234. Here's how to avoid the problem, by examining point and the mark after
  235. reading the keyboard input:
  236. @smallexample
  237. (interactive
  238. (let ((string (read-string "Foo: " nil 'my-history)))
  239. (list (region-beginning) (region-end) string)))
  240. @end smallexample
  241. @strong{Warning:} the argument values should not include any data
  242. types that can't be printed and then read. Some facilities save
  243. @code{command-history} in a file to be read in the subsequent
  244. sessions; if a command's arguments contain a data type that prints
  245. using @samp{#<@dots{}>} syntax, those facilities won't work.
  246. There are, however, a few exceptions: it is ok to use a limited set of
  247. expressions such as @code{(point)}, @code{(mark)},
  248. @code{(region-beginning)}, and @code{(region-end)}, because Emacs
  249. recognizes them specially and puts the expression (rather than its
  250. value) into the command history. To see whether the expression you
  251. wrote is one of these exceptions, run the command, then examine
  252. @code{(car command-history)}.
  253. @end itemize
  254. @cindex examining the @code{interactive} form
  255. @defun interactive-form function
  256. This function returns the @code{interactive} form of @var{function}.
  257. If @var{function} is an interactively callable function
  258. (@pxref{Interactive Call}), the value is the command's
  259. @code{interactive} form @code{(interactive @var{spec})}, which
  260. specifies how to compute its arguments. Otherwise, the value is
  261. @code{nil}. If @var{function} is a symbol, its function definition is
  262. used.
  263. @end defun
  264. @node Interactive Codes
  265. @subsection Code Characters for @code{interactive}
  266. @cindex interactive code description
  267. @cindex description for interactive codes
  268. @cindex codes, interactive, description of
  269. @cindex characters for interactive codes
  270. The code character descriptions below contain a number of key words,
  271. defined here as follows:
  272. @table @b
  273. @item Completion
  274. @cindex interactive completion
  275. Provide completion. @key{TAB}, @key{SPC}, and @key{RET} perform name
  276. completion because the argument is read using @code{completing-read}
  277. (@pxref{Completion}). @kbd{?} displays a list of possible completions.
  278. @item Existing
  279. Require the name of an existing object. An invalid name is not
  280. accepted; the commands to exit the minibuffer do not exit if the current
  281. input is not valid.
  282. @item Default
  283. @cindex default argument string
  284. A default value of some sort is used if the user enters no text in the
  285. minibuffer. The default depends on the code character.
  286. @item No I/O
  287. This code letter computes an argument without reading any input.
  288. Therefore, it does not use a prompt string, and any prompt string you
  289. supply is ignored.
  290. Even though the code letter doesn't use a prompt string, you must follow
  291. it with a newline if it is not the last code character in the string.
  292. @item Prompt
  293. A prompt immediately follows the code character. The prompt ends either
  294. with the end of the string or with a newline.
  295. @item Special
  296. This code character is meaningful only at the beginning of the
  297. interactive string, and it does not look for a prompt or a newline.
  298. It is a single, isolated character.
  299. @end table
  300. @cindex reading interactive arguments
  301. Here are the code character descriptions for use with @code{interactive}:
  302. @table @samp
  303. @item *
  304. Signal an error if the current buffer is read-only. Special.
  305. @item @@
  306. Select the window mentioned in the first mouse event in the key
  307. sequence that invoked this command. Special.
  308. @item ^
  309. If the command was invoked through shift-translation, set the mark and
  310. activate the region temporarily, or extend an already active region,
  311. before the command is run. If the command was invoked without
  312. shift-translation, and the region is temporarily active, deactivate
  313. the region before the command is run. Special.
  314. @item a
  315. A function name (i.e., a symbol satisfying @code{fboundp}). Existing,
  316. Completion, Prompt.
  317. @item b
  318. The name of an existing buffer. By default, uses the name of the
  319. current buffer (@pxref{Buffers}). Existing, Completion, Default,
  320. Prompt.
  321. @item B
  322. A buffer name. The buffer need not exist. By default, uses the name of
  323. a recently used buffer other than the current buffer. Completion,
  324. Default, Prompt.
  325. @item c
  326. A character. The cursor does not move into the echo area. Prompt.
  327. @item C
  328. A command name (i.e., a symbol satisfying @code{commandp}). Existing,
  329. Completion, Prompt.
  330. @item d
  331. @cindex position argument
  332. The position of point, as an integer (@pxref{Point}). No I/O.
  333. @item D
  334. A directory name. The default is the current default directory of the
  335. current buffer, @code{default-directory} (@pxref{File Name Expansion}).
  336. Existing, Completion, Default, Prompt.
  337. @item e
  338. The first or next non-keyboard event in the key sequence that invoked
  339. the command. More precisely, @samp{e} gets events that are lists, so
  340. you can look at the data in the lists. @xref{Input Events}. No I/O.
  341. You use @samp{e} for mouse events and for special system events
  342. (@pxref{Misc Events}). The event list that the command receives
  343. depends on the event. @xref{Input Events}, which describes the forms
  344. of the list for each event in the corresponding subsections.
  345. You can use @samp{e} more than once in a single command's interactive
  346. specification. If the key sequence that invoked the command has
  347. @var{n} events that are lists, the @var{n}th @samp{e} provides the
  348. @var{n}th such event. Events that are not lists, such as function keys
  349. and @acronym{ASCII} characters, do not count where @samp{e} is concerned.
  350. @item f
  351. A file name of an existing file (@pxref{File Names}). The default
  352. directory is @code{default-directory}. Existing, Completion, Default,
  353. Prompt.
  354. @item F
  355. A file name. The file need not exist. Completion, Default, Prompt.
  356. @item G
  357. A file name. The file need not exist. If the user enters just a
  358. directory name, then the value is just that directory name, with no
  359. file name within the directory added. Completion, Default, Prompt.
  360. @item i
  361. An irrelevant argument. This code always supplies @code{nil} as
  362. the argument's value. No I/O.
  363. @item k
  364. A key sequence (@pxref{Key Sequences}). This keeps reading events
  365. until a command (or undefined command) is found in the current key
  366. maps. The key sequence argument is represented as a string or vector.
  367. The cursor does not move into the echo area. Prompt.
  368. If @samp{k} reads a key sequence that ends with a down-event, it also
  369. reads and discards the following up-event. You can get access to that
  370. up-event with the @samp{U} code character.
  371. This kind of input is used by commands such as @code{describe-key} and
  372. @code{global-set-key}.
  373. @item K
  374. A key sequence, whose definition you intend to change. This works like
  375. @samp{k}, except that it suppresses, for the last input event in the key
  376. sequence, the conversions that are normally used (when necessary) to
  377. convert an undefined key into a defined one.
  378. @item m
  379. @cindex marker argument
  380. The position of the mark, as an integer. No I/O.
  381. @item M
  382. Arbitrary text, read in the minibuffer using the current buffer's input
  383. method, and returned as a string (@pxref{Input Methods,,, emacs, The GNU
  384. Emacs Manual}). Prompt.
  385. @item n
  386. A number, read with the minibuffer. If the input is not a number, the
  387. user has to try again. @samp{n} never uses the prefix argument.
  388. Prompt.
  389. @item N
  390. The numeric prefix argument; but if there is no prefix argument, read
  391. a number as with @kbd{n}. The value is always a number. @xref{Prefix
  392. Command Arguments}. Prompt.
  393. @item p
  394. @cindex numeric prefix argument usage
  395. The numeric prefix argument. (Note that this @samp{p} is lower case.)
  396. No I/O.
  397. @item P
  398. @cindex raw prefix argument usage
  399. The raw prefix argument. (Note that this @samp{P} is upper case.) No
  400. I/O.
  401. @item r
  402. @cindex region argument
  403. Point and the mark, as two numeric arguments, smallest first. This is
  404. the only code letter that specifies two successive arguments rather than
  405. one. No I/O.
  406. @item s
  407. Arbitrary text, read in the minibuffer and returned as a string
  408. (@pxref{Text from Minibuffer}). Terminate the input with either
  409. @kbd{C-j} or @key{RET}. (@kbd{C-q} may be used to include either of
  410. these characters in the input.) Prompt.
  411. @item S
  412. An interned symbol whose name is read in the minibuffer. Terminate
  413. the input with either @kbd{C-j} or @key{RET}. Other characters that
  414. normally terminate a symbol (e.g., whitespace, parentheses and
  415. brackets) do not do so here. Prompt.
  416. @item U
  417. A key sequence or @code{nil}. Can be used after a @samp{k} or
  418. @samp{K} argument to get the up-event that was discarded (if any)
  419. after @samp{k} or @samp{K} read a down-event. If no up-event has been
  420. discarded, @samp{U} provides @code{nil} as the argument. No I/O.
  421. @item v
  422. A variable declared to be a user option (i.e., satisfying the
  423. predicate @code{custom-variable-p}). This reads the variable using
  424. @code{read-variable}. @xref{Definition of read-variable}. Existing,
  425. Completion, Prompt.
  426. @item x
  427. A Lisp object, specified with its read syntax, terminated with a
  428. @kbd{C-j} or @key{RET}. The object is not evaluated. @xref{Object from
  429. Minibuffer}. Prompt.
  430. @item X
  431. @cindex evaluated expression argument
  432. A Lisp form's value. @samp{X} reads as @samp{x} does, then evaluates
  433. the form so that its value becomes the argument for the command.
  434. Prompt.
  435. @item z
  436. A coding system name (a symbol). If the user enters null input, the
  437. argument value is @code{nil}. @xref{Coding Systems}. Completion,
  438. Existing, Prompt.
  439. @item Z
  440. A coding system name (a symbol)---but only if this command has a prefix
  441. argument. With no prefix argument, @samp{Z} provides @code{nil} as the
  442. argument value. Completion, Existing, Prompt.
  443. @end table
  444. @node Interactive Examples
  445. @subsection Examples of Using @code{interactive}
  446. @cindex examples of using @code{interactive}
  447. @cindex @code{interactive}, examples of using
  448. Here are some examples of @code{interactive}:
  449. @example
  450. @group
  451. (defun foo1 () ; @r{@code{foo1} takes no arguments,}
  452. (interactive) ; @r{just moves forward two words.}
  453. (forward-word 2))
  454. @result{} foo1
  455. @end group
  456. @group
  457. (defun foo2 (n) ; @r{@code{foo2} takes one argument,}
  458. (interactive "^p") ; @r{which is the numeric prefix.}
  459. ; @r{under @code{shift-select-mode},}
  460. ; @r{will activate or extend region.}
  461. (forward-word (* 2 n)))
  462. @result{} foo2
  463. @end group
  464. @group
  465. (defun foo3 (n) ; @r{@code{foo3} takes one argument,}
  466. (interactive "nCount:") ; @r{which is read with the Minibuffer.}
  467. (forward-word (* 2 n)))
  468. @result{} foo3
  469. @end group
  470. @group
  471. (defun three-b (b1 b2 b3)
  472. "Select three existing buffers.
  473. Put them into three windows, selecting the last one."
  474. @end group
  475. (interactive "bBuffer1:\nbBuffer2:\nbBuffer3:")
  476. (delete-other-windows)
  477. (split-window (selected-window) 8)
  478. (switch-to-buffer b1)
  479. (other-window 1)
  480. (split-window (selected-window) 8)
  481. (switch-to-buffer b2)
  482. (other-window 1)
  483. (switch-to-buffer b3))
  484. @result{} three-b
  485. @group
  486. (three-b "*scratch*" "declarations.texi" "*mail*")
  487. @result{} nil
  488. @end group
  489. @end example
  490. @node Generic Commands
  491. @subsection Select among Command Alternatives
  492. @cindex generic commands
  493. @cindex alternatives, defining
  494. The macro @code{define-alternatives} can be used to define
  495. @dfn{generic commands}. These are interactive functions whose
  496. implementation can be selected from several alternatives, as a matter
  497. of user preference.
  498. @defmac define-alternatives command &rest customizations
  499. Define the new command @var{command}, a symbol.
  500. When a user runs @kbd{M-x @var{command} @key{RET}} for the first time,
  501. Emacs prompts for which real form of the command to use, and records
  502. the selection by way of a custom variable. Using a prefix argument
  503. repeats this process of choosing an alternative.
  504. The variable @code{@var{command}-alternatives} should contain an alist
  505. with alternative implementations of @var{command}.
  506. Until this variable is set, @code{define-alternatives} has no effect.
  507. If @var{customizations} is non-@code{nil}, it should consist of
  508. alternating @code{defcustom} keywords (typically @code{:group} and
  509. @code{:version}) and values to add to the declaration of
  510. @code{@var{command}-alternatives}.
  511. @end defmac
  512. @node Interactive Call
  513. @section Interactive Call
  514. @cindex interactive call
  515. After the command loop has translated a key sequence into a command,
  516. it invokes that command using the function @code{command-execute}. If
  517. the command is a function, @code{command-execute} calls
  518. @code{call-interactively}, which reads the arguments and calls the
  519. command. You can also call these functions yourself.
  520. Note that the term ``command'', in this context, refers to an
  521. interactively callable function (or function-like object), or a
  522. keyboard macro. It does not refer to the key sequence used to invoke
  523. a command (@pxref{Keymaps}).
  524. @defun commandp object &optional for-call-interactively
  525. This function returns @code{t} if @var{object} is a command.
  526. Otherwise, it returns @code{nil}.
  527. Commands include strings and vectors (which are treated as keyboard
  528. macros), lambda expressions that contain a top-level
  529. @code{interactive} form (@pxref{Using Interactive}), byte-code
  530. function objects made from such lambda expressions, autoload objects
  531. that are declared as interactive (non-@code{nil} fourth argument to
  532. @code{autoload}), and some primitive functions. Also, a symbol is
  533. considered a command if it has a non-@code{nil}
  534. @code{interactive-form} property, or if its function definition
  535. satisfies @code{commandp}.
  536. If @var{for-call-interactively} is non-@code{nil}, then
  537. @code{commandp} returns @code{t} only for objects that
  538. @code{call-interactively} could call---thus, not for keyboard macros.
  539. See @code{documentation} in @ref{Accessing Documentation}, for a
  540. realistic example of using @code{commandp}.
  541. @end defun
  542. @defun call-interactively command &optional record-flag keys
  543. This function calls the interactively callable function @var{command},
  544. providing arguments according to its interactive calling specifications.
  545. It returns whatever @var{command} returns.
  546. If, for instance, you have a function with the following signature:
  547. @example
  548. (defun foo (begin end)
  549. (interactive "r")
  550. ...)
  551. @end example
  552. then saying
  553. @example
  554. (call-interactively 'foo)
  555. @end example
  556. will call @code{foo} with the region (@code{point} and @code{mark}) as
  557. the arguments.
  558. An error is signaled if @var{command} is not a function or if it
  559. cannot be called interactively (i.e., is not a command). Note that
  560. keyboard macros (strings and vectors) are not accepted, even though
  561. they are considered commands, because they are not functions. If
  562. @var{command} is a symbol, then @code{call-interactively} uses its
  563. function definition.
  564. @cindex record command history
  565. If @var{record-flag} is non-@code{nil}, then this command and its
  566. arguments are unconditionally added to the list @code{command-history}.
  567. Otherwise, the command is added only if it uses the minibuffer to read
  568. an argument. @xref{Command History}.
  569. The argument @var{keys}, if given, should be a vector which specifies
  570. the sequence of events to supply if the command inquires which events
  571. were used to invoke it. If @var{keys} is omitted or @code{nil}, the
  572. default is the return value of @code{this-command-keys-vector}.
  573. @xref{Definition of this-command-keys-vector}.
  574. @end defun
  575. @defun funcall-interactively function &rest arguments
  576. This function works like @code{funcall} (@pxref{Calling Functions}),
  577. but it makes the call look like an interactive invocation: a call to
  578. @code{called-interactively-p} inside @var{function} will return
  579. @code{t}. If @var{function} is not a command, it is called without
  580. signaling an error.
  581. @end defun
  582. @defun command-execute command &optional record-flag keys special
  583. @cindex keyboard macro execution
  584. This function executes @var{command}. The argument @var{command} must
  585. satisfy the @code{commandp} predicate; i.e., it must be an interactively
  586. callable function or a keyboard macro.
  587. A string or vector as @var{command} is executed with
  588. @code{execute-kbd-macro}. A function is passed to
  589. @code{call-interactively} (see above), along with the
  590. @var{record-flag} and @var{keys} arguments.
  591. If @var{command} is a symbol, its function definition is used in its
  592. place. A symbol with an @code{autoload} definition counts as a
  593. command if it was declared to stand for an interactively callable
  594. function. Such a definition is handled by loading the specified
  595. library and then rechecking the definition of the symbol.
  596. The argument @var{special}, if given, means to ignore the prefix
  597. argument and not clear it. This is used for executing special events
  598. (@pxref{Special Events}).
  599. @end defun
  600. @deffn Command execute-extended-command prefix-argument
  601. @cindex read command name
  602. This function reads a command name from the minibuffer using
  603. @code{completing-read} (@pxref{Completion}). Then it uses
  604. @code{command-execute} to call the specified command. Whatever that
  605. command returns becomes the value of @code{execute-extended-command}.
  606. @cindex execute with prefix argument
  607. If the command asks for a prefix argument, it receives the value
  608. @var{prefix-argument}. If @code{execute-extended-command} is called
  609. interactively, the current raw prefix argument is used for
  610. @var{prefix-argument}, and thus passed on to whatever command is run.
  611. @c !!! Should this be @kindex?
  612. @cindex @kbd{M-x}
  613. @code{execute-extended-command} is the normal definition of @kbd{M-x},
  614. so it uses the string @w{@samp{M-x }} as a prompt. (It would be better
  615. to take the prompt from the events used to invoke
  616. @code{execute-extended-command}, but that is painful to implement.) A
  617. description of the value of the prefix argument, if any, also becomes
  618. part of the prompt.
  619. @example
  620. @group
  621. (execute-extended-command 3)
  622. ---------- Buffer: Minibuffer ----------
  623. 3 M-x forward-word RET
  624. ---------- Buffer: Minibuffer ----------
  625. @result{} t
  626. @end group
  627. @end example
  628. @end deffn
  629. @node Distinguish Interactive
  630. @section Distinguish Interactive Calls
  631. @cindex distinguish interactive calls
  632. @cindex is this call interactive
  633. Sometimes a command should display additional visual feedback (such
  634. as an informative message in the echo area) for interactive calls
  635. only. There are three ways to do this. The recommended way to test
  636. whether the function was called using @code{call-interactively} is to
  637. give it an optional argument @code{print-message} and use the
  638. @code{interactive} spec to make it non-@code{nil} in interactive
  639. calls. Here's an example:
  640. @example
  641. (defun foo (&optional print-message)
  642. (interactive "p")
  643. (when print-message
  644. (message "foo")))
  645. @end example
  646. @noindent
  647. We use @code{"p"} because the numeric prefix argument is never
  648. @code{nil}. Defined in this way, the function does display the
  649. message when called from a keyboard macro.
  650. The above method with the additional argument is usually best,
  651. because it allows callers to say ``treat this call as interactive''.
  652. But you can also do the job by testing @code{called-interactively-p}.
  653. @defun called-interactively-p kind
  654. This function returns @code{t} when the calling function was called
  655. using @code{call-interactively}.
  656. The argument @var{kind} should be either the symbol @code{interactive}
  657. or the symbol @code{any}. If it is @code{interactive}, then
  658. @code{called-interactively-p} returns @code{t} only if the call was
  659. made directly by the user---e.g., if the user typed a key sequence
  660. bound to the calling function, but @emph{not} if the user ran a
  661. keyboard macro that called the function (@pxref{Keyboard Macros}). If
  662. @var{kind} is @code{any}, @code{called-interactively-p} returns
  663. @code{t} for any kind of interactive call, including keyboard macros.
  664. If in doubt, use @code{any}; the only known proper use of
  665. @code{interactive} is if you need to decide whether to display a
  666. helpful message while a function is running.
  667. A function is never considered to be called interactively if it was
  668. called via Lisp evaluation (or with @code{apply} or @code{funcall}).
  669. @end defun
  670. @noindent
  671. Here is an example of using @code{called-interactively-p}:
  672. @example
  673. @group
  674. (defun foo ()
  675. (interactive)
  676. (when (called-interactively-p 'any)
  677. (message "Interactive!")
  678. 'foo-called-interactively))
  679. @end group
  680. @group
  681. ;; @r{Type @kbd{M-x foo}.}
  682. @print{} Interactive!
  683. @end group
  684. @group
  685. (foo)
  686. @result{} nil
  687. @end group
  688. @end example
  689. @noindent
  690. Here is another example that contrasts direct and indirect calls to
  691. @code{called-interactively-p}.
  692. @example
  693. @group
  694. (defun bar ()
  695. (interactive)
  696. (message "%s" (list (foo) (called-interactively-p 'any))))
  697. @end group
  698. @group
  699. ;; @r{Type @kbd{M-x bar}.}
  700. @print{} (nil t)
  701. @end group
  702. @end example
  703. @node Command Loop Info
  704. @section Information from the Command Loop
  705. @cindex command loop variables
  706. The editor command loop sets several Lisp variables to keep status
  707. records for itself and for commands that are run. With the exception of
  708. @code{this-command} and @code{last-command} it's generally a bad idea to
  709. change any of these variables in a Lisp program.
  710. @defvar last-command
  711. This variable records the name of the previous command executed by the
  712. command loop (the one before the current command). Normally the value
  713. is a symbol with a function definition, but this is not guaranteed.
  714. The value is copied from @code{this-command} when a command returns to
  715. the command loop, except when the command has specified a prefix
  716. argument for the following command.
  717. This variable is always local to the current terminal and cannot be
  718. buffer-local. @xref{Multiple Terminals}.
  719. @end defvar
  720. @defvar real-last-command
  721. This variable is set up by Emacs just like @code{last-command},
  722. but never altered by Lisp programs.
  723. @end defvar
  724. @defvar last-repeatable-command
  725. This variable stores the most recently executed command that was not
  726. part of an input event. This is the command @code{repeat} will try to
  727. repeat, @xref{Repeating,,, emacs, The GNU Emacs Manual}.
  728. @end defvar
  729. @defvar this-command
  730. @cindex current command
  731. This variable records the name of the command now being executed by
  732. the editor command loop. Like @code{last-command}, it is normally a symbol
  733. with a function definition.
  734. The command loop sets this variable just before running a command, and
  735. copies its value into @code{last-command} when the command finishes
  736. (unless the command specified a prefix argument for the following
  737. command).
  738. @cindex kill command repetition
  739. Some commands set this variable during their execution, as a flag for
  740. whatever command runs next. In particular, the functions for killing text
  741. set @code{this-command} to @code{kill-region} so that any kill commands
  742. immediately following will know to append the killed text to the
  743. previous kill.
  744. @end defvar
  745. If you do not want a particular command to be recognized as the previous
  746. command in the case where it got an error, you must code that command to
  747. prevent this. One way is to set @code{this-command} to @code{t} at the
  748. beginning of the command, and set @code{this-command} back to its proper
  749. value at the end, like this:
  750. @example
  751. (defun foo (args@dots{})
  752. (interactive @dots{})
  753. (let ((old-this-command this-command))
  754. (setq this-command t)
  755. @r{@dots{}do the work@dots{}}
  756. (setq this-command old-this-command)))
  757. @end example
  758. @noindent
  759. We do not bind @code{this-command} with @code{let} because that would
  760. restore the old value in case of error---a feature of @code{let} which
  761. in this case does precisely what we want to avoid.
  762. @defvar this-original-command
  763. This has the same value as @code{this-command} except when command
  764. remapping occurs (@pxref{Remapping Commands}). In that case,
  765. @code{this-command} gives the command actually run (the result of
  766. remapping), and @code{this-original-command} gives the command that
  767. was specified to run but remapped into another command.
  768. @end defvar
  769. @defun this-command-keys
  770. This function returns a string or vector containing the key sequence
  771. that invoked the present command, plus any previous commands that
  772. generated the prefix argument for this command. Any events read by the
  773. command using @code{read-event} without a timeout get tacked on to the end.
  774. However, if the command has called @code{read-key-sequence}, it
  775. returns the last read key sequence. @xref{Key Sequence Input}. The
  776. value is a string if all events in the sequence were characters that
  777. fit in a string. @xref{Input Events}.
  778. @example
  779. @group
  780. (this-command-keys)
  781. ;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
  782. @result{} "^U^X^E"
  783. @end group
  784. @end example
  785. @end defun
  786. @defun this-command-keys-vector
  787. @anchor{Definition of this-command-keys-vector}
  788. Like @code{this-command-keys}, except that it always returns the events
  789. in a vector, so you don't need to deal with the complexities of storing
  790. input events in a string (@pxref{Strings of Events}).
  791. @end defun
  792. @defun clear-this-command-keys &optional keep-record
  793. This function empties out the table of events for
  794. @code{this-command-keys} to return. Unless @var{keep-record} is
  795. non-@code{nil}, it also empties the records that the function
  796. @code{recent-keys} (@pxref{Recording Input}) will subsequently return.
  797. This is useful after reading a password, to prevent the password from
  798. echoing inadvertently as part of the next command in certain cases.
  799. @end defun
  800. @defvar last-nonmenu-event
  801. This variable holds the last input event read as part of a key sequence,
  802. not counting events resulting from mouse menus.
  803. One use of this variable is for telling @code{x-popup-menu} where to pop
  804. up a menu. It is also used internally by @code{y-or-n-p}
  805. (@pxref{Yes-or-No Queries}).
  806. @end defvar
  807. @defvar last-command-event
  808. This variable is set to the last input event that was read by the
  809. command loop as part of a command. The principal use of this variable
  810. is in @code{self-insert-command}, which uses it to decide which
  811. character to insert.
  812. @example
  813. @group
  814. last-command-event
  815. ;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
  816. @result{} 5
  817. @end group
  818. @end example
  819. @noindent
  820. The value is 5 because that is the @acronym{ASCII} code for @kbd{C-e}.
  821. @end defvar
  822. @defvar last-event-frame
  823. This variable records which frame the last input event was directed to.
  824. Usually this is the frame that was selected when the event was
  825. generated, but if that frame has redirected input focus to another
  826. frame, the value is the frame to which the event was redirected.
  827. @xref{Input Focus}.
  828. If the last event came from a keyboard macro, the value is @code{macro}.
  829. @end defvar
  830. @node Adjusting Point
  831. @section Adjusting Point After Commands
  832. @cindex adjusting point
  833. @cindex invisible/intangible text, and point
  834. @cindex @code{display} property, and point display
  835. @cindex @code{composition} property, and point display
  836. It is not easy to display a value of point in the middle of a
  837. sequence of text that has the @code{display}, @code{composition} or
  838. is invisible. Therefore, after a command finishes and returns to the
  839. command loop, if point is within such a sequence, the command loop
  840. normally moves point to the edge of the sequence.
  841. A command can inhibit this feature by setting the variable
  842. @code{disable-point-adjustment}:
  843. @defvar disable-point-adjustment
  844. If this variable is non-@code{nil} when a command returns to the
  845. command loop, then the command loop does not check for those text
  846. properties, and does not move point out of sequences that have them.
  847. The command loop sets this variable to @code{nil} before each command,
  848. so if a command sets it, the effect applies only to that command.
  849. @end defvar
  850. @defvar global-disable-point-adjustment
  851. If you set this variable to a non-@code{nil} value, the feature of
  852. moving point out of these sequences is completely turned off.
  853. @end defvar
  854. @node Input Events
  855. @section Input Events
  856. @cindex events
  857. @cindex input events
  858. The Emacs command loop reads a sequence of @dfn{input events} that
  859. represent keyboard or mouse activity, or system events sent to Emacs.
  860. The events for keyboard activity are characters or symbols; other
  861. events are always lists. This section describes the representation
  862. and meaning of input events in detail.
  863. @defun eventp object
  864. This function returns non-@code{nil} if @var{object} is an input event
  865. or event type.
  866. Note that any symbol might be used as an event or an event type.
  867. @code{eventp} cannot distinguish whether a symbol is intended by Lisp
  868. code to be used as an event. Instead, it distinguishes whether the
  869. symbol has actually been used in an event that has been read as input in
  870. the current Emacs session. If a symbol has not yet been so used,
  871. @code{eventp} returns @code{nil}.
  872. @end defun
  873. @menu
  874. * Keyboard Events:: Ordinary characters -- keys with symbols on them.
  875. * Function Keys:: Function keys -- keys with names, not symbols.
  876. * Mouse Events:: Overview of mouse events.
  877. * Click Events:: Pushing and releasing a mouse button.
  878. * Drag Events:: Moving the mouse before releasing the button.
  879. * Button-Down Events:: A button was pushed and not yet released.
  880. * Repeat Events:: Double and triple click (or drag, or down).
  881. * Motion Events:: Just moving the mouse, not pushing a button.
  882. * Focus Events:: Moving the mouse between frames.
  883. * Misc Events:: Other events the system can generate.
  884. * Event Examples:: Examples of the lists for mouse events.
  885. * Classifying Events:: Finding the modifier keys in an event symbol.
  886. Event types.
  887. * Accessing Mouse:: Functions to extract info from mouse events.
  888. * Accessing Scroll:: Functions to get info from scroll bar events.
  889. * Strings of Events:: Special considerations for putting
  890. keyboard character events in a string.
  891. @end menu
  892. @node Keyboard Events
  893. @subsection Keyboard Events
  894. @cindex keyboard events
  895. There are two kinds of input you can get from the keyboard: ordinary
  896. keys, and function keys. Ordinary keys correspond to characters; the
  897. events they generate are represented in Lisp as characters. The event
  898. type of a character event is the character itself (an integer); see
  899. @ref{Classifying Events}.
  900. @cindex modifier bits (of input character)
  901. @cindex basic code (of input character)
  902. An input character event consists of a @dfn{basic code} between 0 and
  903. 524287, plus any or all of these @dfn{modifier bits}:
  904. @table @asis
  905. @item meta
  906. The
  907. @tex
  908. @math{2^{27}}
  909. @end tex
  910. @ifnottex
  911. 2**27
  912. @end ifnottex
  913. bit in the character code indicates a character
  914. typed with the meta key held down.
  915. @item control
  916. The
  917. @tex
  918. @math{2^{26}}
  919. @end tex
  920. @ifnottex
  921. 2**26
  922. @end ifnottex
  923. bit in the character code indicates a non-@acronym{ASCII}
  924. control character.
  925. @sc{ascii} control characters such as @kbd{C-a} have special basic
  926. codes of their own, so Emacs needs no special bit to indicate them.
  927. Thus, the code for @kbd{C-a} is just 1.
  928. But if you type a control combination not in @acronym{ASCII}, such as
  929. @kbd{%} with the control key, the numeric value you get is the code
  930. for @kbd{%} plus
  931. @tex
  932. @math{2^{26}}
  933. @end tex
  934. @ifnottex
  935. 2**26
  936. @end ifnottex
  937. (assuming the terminal supports non-@acronym{ASCII}
  938. control characters).
  939. @item shift
  940. The
  941. @tex
  942. @math{2^{25}}
  943. @end tex
  944. @ifnottex
  945. 2**25
  946. @end ifnottex
  947. bit in the character code indicates an @acronym{ASCII} control
  948. character typed with the shift key held down.
  949. For letters, the basic code itself indicates upper versus lower case;
  950. for digits and punctuation, the shift key selects an entirely different
  951. character with a different basic code. In order to keep within the
  952. @acronym{ASCII} character set whenever possible, Emacs avoids using the
  953. @tex
  954. @math{2^{25}}
  955. @end tex
  956. @ifnottex
  957. 2**25
  958. @end ifnottex
  959. bit for those characters.
  960. However, @acronym{ASCII} provides no way to distinguish @kbd{C-A} from
  961. @kbd{C-a}, so Emacs uses the
  962. @tex
  963. @math{2^{25}}
  964. @end tex
  965. @ifnottex
  966. 2**25
  967. @end ifnottex
  968. bit in @kbd{C-A} and not in
  969. @kbd{C-a}.
  970. @item hyper
  971. The
  972. @tex
  973. @math{2^{24}}
  974. @end tex
  975. @ifnottex
  976. 2**24
  977. @end ifnottex
  978. bit in the character code indicates a character
  979. typed with the hyper key held down.
  980. @item super
  981. The
  982. @tex
  983. @math{2^{23}}
  984. @end tex
  985. @ifnottex
  986. 2**23
  987. @end ifnottex
  988. bit in the character code indicates a character
  989. typed with the super key held down.
  990. @item alt
  991. The
  992. @tex
  993. @math{2^{22}}
  994. @end tex
  995. @ifnottex
  996. 2**22
  997. @end ifnottex
  998. bit in the character code indicates a character typed with the alt key
  999. held down. (The key labeled @key{Alt} on most keyboards is actually
  1000. treated as the meta key, not this.)
  1001. @end table
  1002. It is best to avoid mentioning specific bit numbers in your program.
  1003. To test the modifier bits of a character, use the function
  1004. @code{event-modifiers} (@pxref{Classifying Events}). When making key
  1005. bindings, you can use the read syntax for characters with modifier bits
  1006. (@samp{\C-}, @samp{\M-}, and so on). For making key bindings with
  1007. @code{define-key}, you can use lists such as @code{(control hyper ?x)} to
  1008. specify the characters (@pxref{Changing Key Bindings}). The function
  1009. @code{event-convert-list} converts such a list into an event type
  1010. (@pxref{Classifying Events}).
  1011. @node Function Keys
  1012. @subsection Function Keys
  1013. @cindex function keys
  1014. Most keyboards also have @dfn{function keys}---keys that have names or
  1015. symbols that are not characters. Function keys are represented in
  1016. Emacs Lisp as symbols; the symbol's name is the function key's label,
  1017. in lower case. For example, pressing a key labeled @key{F1} generates
  1018. an input event represented by the symbol @code{f1}.
  1019. The event type of a function key event is the event symbol itself.
  1020. @xref{Classifying Events}.
  1021. Here are a few special cases in the symbol-naming convention for
  1022. function keys:
  1023. @table @asis
  1024. @item @code{backspace}, @code{tab}, @code{newline}, @code{return}, @code{delete}
  1025. These keys correspond to common @acronym{ASCII} control characters that have
  1026. special keys on most keyboards.
  1027. In @acronym{ASCII}, @kbd{C-i} and @key{TAB} are the same character. If the
  1028. terminal can distinguish between them, Emacs conveys the distinction to
  1029. Lisp programs by representing the former as the integer 9, and the
  1030. latter as the symbol @code{tab}.
  1031. Most of the time, it's not useful to distinguish the two. So normally
  1032. @code{local-function-key-map} (@pxref{Translation Keymaps}) is set up
  1033. to map @code{tab} into 9. Thus, a key binding for character code 9
  1034. (the character @kbd{C-i}) also applies to @code{tab}. Likewise for
  1035. the other symbols in this group. The function @code{read-char}
  1036. likewise converts these events into characters.
  1037. In @acronym{ASCII}, @key{BS} is really @kbd{C-h}. But @code{backspace}
  1038. converts into the character code 127 (@key{DEL}), not into code 8
  1039. (@key{BS}). This is what most users prefer.
  1040. @item @code{left}, @code{up}, @code{right}, @code{down}
  1041. Cursor arrow keys
  1042. @item @code{kp-add}, @code{kp-decimal}, @code{kp-divide}, @dots{}
  1043. Keypad keys (to the right of the regular keyboard).
  1044. @item @code{kp-0}, @code{kp-1}, @dots{}
  1045. Keypad keys with digits.
  1046. @item @code{kp-f1}, @code{kp-f2}, @code{kp-f3}, @code{kp-f4}
  1047. Keypad PF keys.
  1048. @item @code{kp-home}, @code{kp-left}, @code{kp-up}, @code{kp-right}, @code{kp-down}
  1049. Keypad arrow keys. Emacs normally translates these into the
  1050. corresponding non-keypad keys @code{home}, @code{left}, @dots{}
  1051. @item @code{kp-prior}, @code{kp-next}, @code{kp-end}, @code{kp-begin}, @code{kp-insert}, @code{kp-delete}
  1052. Additional keypad duplicates of keys ordinarily found elsewhere. Emacs
  1053. normally translates these into the like-named non-keypad keys.
  1054. @end table
  1055. You can use the modifier keys @key{ALT}, @key{CTRL}, @key{HYPER},
  1056. @key{META}, @key{SHIFT}, and @key{SUPER} with function keys. The way to
  1057. represent them is with prefixes in the symbol name:
  1058. @table @samp
  1059. @item A-
  1060. The alt modifier.
  1061. @item C-
  1062. The control modifier.
  1063. @item H-
  1064. The hyper modifier.
  1065. @item M-
  1066. The meta modifier.
  1067. @item S-
  1068. The shift modifier.
  1069. @item s-
  1070. The super modifier.
  1071. @end table
  1072. Thus, the symbol for the key @key{F3} with @key{META} held down is
  1073. @code{M-f3}. When you use more than one prefix, we recommend you
  1074. write them in alphabetical order; but the order does not matter in
  1075. arguments to the key-binding lookup and modification functions.
  1076. @node Mouse Events
  1077. @subsection Mouse Events
  1078. Emacs supports four kinds of mouse events: click events, drag events,
  1079. button-down events, and motion events. All mouse events are represented
  1080. as lists. The @sc{car} of the list is the event type; this says which
  1081. mouse button was involved, and which modifier keys were used with it.
  1082. The event type can also distinguish double or triple button presses
  1083. (@pxref{Repeat Events}). The rest of the list elements give position
  1084. and time information.
  1085. For key lookup, only the event type matters: two events of the same type
  1086. necessarily run the same command. The command can access the full
  1087. values of these events using the @samp{e} interactive code.
  1088. @xref{Interactive Codes}.
  1089. A key sequence that starts with a mouse event is read using the keymaps
  1090. of the buffer in the window that the mouse was in, not the current
  1091. buffer. This does not imply that clicking in a window selects that
  1092. window or its buffer---that is entirely under the control of the command
  1093. binding of the key sequence.
  1094. @node Click Events
  1095. @subsection Click Events
  1096. @cindex click event
  1097. @cindex mouse click event
  1098. When the user presses a mouse button and releases it at the same
  1099. location, that generates a @dfn{click} event. All mouse click event
  1100. share the same format:
  1101. @example
  1102. (@var{event-type} @var{position} @var{click-count})
  1103. @end example
  1104. @table @asis
  1105. @item @var{event-type}
  1106. This is a symbol that indicates which mouse button was used. It is
  1107. one of the symbols @code{mouse-1}, @code{mouse-2}, @dots{}, where the
  1108. buttons are numbered left to right.
  1109. You can also use prefixes @samp{A-}, @samp{C-}, @samp{H-}, @samp{M-},
  1110. @samp{S-} and @samp{s-} for modifiers alt, control, hyper, meta, shift
  1111. and super, just as you would with function keys.
  1112. This symbol also serves as the event type of the event. Key bindings
  1113. describe events by their types; thus, if there is a key binding for
  1114. @code{mouse-1}, that binding would apply to all events whose
  1115. @var{event-type} is @code{mouse-1}.
  1116. @item @var{position}
  1117. @cindex mouse position list
  1118. This is a @dfn{mouse position list} specifying where the mouse click
  1119. occurred; see below for details.
  1120. @item @var{click-count}
  1121. This is the number of rapid repeated presses so far of the same mouse
  1122. button. @xref{Repeat Events}.
  1123. @end table
  1124. To access the contents of a mouse position list in the
  1125. @var{position} slot of a click event, you should typically use the
  1126. functions documented in @ref{Accessing Mouse}. The explicit format of
  1127. the list depends on where the click occurred. For clicks in the text
  1128. area, mode line, header line, or in the fringe or marginal areas, the
  1129. mouse position list has the form
  1130. @example
  1131. (@var{window} @var{pos-or-area} (@var{x} . @var{y}) @var{timestamp}
  1132. @var{object} @var{text-pos} (@var{col} . @var{row})
  1133. @var{image} (@var{dx} . @var{dy}) (@var{width} . @var{height}))
  1134. @end example
  1135. @noindent
  1136. The meanings of these list elements are as follows:
  1137. @table @asis
  1138. @item @var{window}
  1139. The window in which the click occurred.
  1140. @item @var{pos-or-area}
  1141. The buffer position of the character clicked on in the text area; or,
  1142. if the click was outside the text area, the window area where it
  1143. occurred. It is one of the symbols @code{mode-line},
  1144. @code{header-line}, @code{vertical-line}, @code{left-margin},
  1145. @code{right-margin}, @code{left-fringe}, or @code{right-fringe}.
  1146. In one special case, @var{pos-or-area} is a list containing a symbol
  1147. (one of the symbols listed above) instead of just the symbol. This
  1148. happens after the imaginary prefix keys for the event are registered
  1149. by Emacs. @xref{Key Sequence Input}.
  1150. @item @var{x}, @var{y}
  1151. The relative pixel coordinates of the click. For clicks in the text
  1152. area of a window, the coordinate origin @code{(0 . 0)} is taken to be
  1153. the top left corner of the text area. @xref{Window Sizes}. For
  1154. clicks in a mode line or header line, the coordinate origin is the top
  1155. left corner of the window itself. For fringes, margins, and the
  1156. vertical border, @var{x} does not have meaningful data. For fringes
  1157. and margins, @var{y} is relative to the bottom edge of the header
  1158. line. In all cases, the @var{x} and @var{y} coordinates increase
  1159. rightward and downward respectively.
  1160. @item @var{timestamp}
  1161. The time at which the event occurred, as an integer number of
  1162. milliseconds since a system-dependent initial time.
  1163. @item @var{object}
  1164. Either @code{nil} if there is no string-type text property at the
  1165. click position, or a cons cell of the form (@var{string}
  1166. . @var{string-pos}) if there is one:
  1167. @table @asis
  1168. @item @var{string}
  1169. The string which was clicked on, including any properties.
  1170. @item @var{string-pos}
  1171. The position in the string where the click occurred.
  1172. @end table
  1173. @item @var{text-pos}
  1174. For clicks on a marginal area or on a fringe, this is the buffer
  1175. position of the first visible character in the corresponding line in
  1176. the window. For clicks on the mode line or the header line, this is
  1177. @code{nil}. For other events, it is the buffer position closest to
  1178. the click.
  1179. @item @var{col}, @var{row}
  1180. These are the actual column and row coordinate numbers of the glyph
  1181. under the @var{x}, @var{y} position. If @var{x} lies beyond the last
  1182. column of actual text on its line, @var{col} is reported by adding
  1183. fictional extra columns that have the default character width. Row 0
  1184. is taken to be the header line if the window has one, or the topmost
  1185. row of the text area otherwise. Column 0 is taken to be the leftmost
  1186. column of the text area for clicks on a window text area, or the
  1187. leftmost mode line or header line column for clicks there. For clicks
  1188. on fringes or vertical borders, these have no meaningful data. For
  1189. clicks on margins, @var{col} is measured from the left edge of the
  1190. margin area and @var{row} is measured from the top of the margin area.
  1191. @item @var{image}
  1192. This is the image object on which the click occurred. It is either
  1193. @code{nil} if there is no image at the position clicked on, or it is
  1194. an image object as returned by @code{find-image} if click was in an image.
  1195. @item @var{dx}, @var{dy}
  1196. These are the pixel coordinates of the click, relative to
  1197. the top left corner of @var{object}, which is @code{(0 . 0)}. If
  1198. @var{object} is @code{nil}, the coordinates are relative to the top
  1199. left corner of the character glyph clicked on.
  1200. @item @var{width}, @var{height}
  1201. These are the pixel width and height of @var{object} or, if this is
  1202. @code{nil}, those of the character glyph clicked on.
  1203. @end table
  1204. For clicks on a scroll bar, @var{position} has this form:
  1205. @example
  1206. (@var{window} @var{area} (@var{portion} . @var{whole}) @var{timestamp} @var{part})
  1207. @end example
  1208. @table @asis
  1209. @item @var{window}
  1210. The window whose scroll bar was clicked on.
  1211. @item @var{area}
  1212. This is the symbol @code{vertical-scroll-bar}.
  1213. @item @var{portion}
  1214. The number of pixels from the top of the scroll bar to the click
  1215. position. On some toolkits, including GTK+, Emacs cannot extract this
  1216. data, so the value is always @code{0}.
  1217. @item @var{whole}
  1218. The total length, in pixels, of the scroll bar. On some toolkits,
  1219. including GTK+, Emacs cannot extract this data, so the value is always
  1220. @code{0}.
  1221. @item @var{timestamp}
  1222. The time at which the event occurred, in milliseconds. On some
  1223. toolkits, including GTK+, Emacs cannot extract this data, so the value
  1224. is always @code{0}.
  1225. @item @var{part}
  1226. The part of the scroll bar on which the click occurred. It is one of
  1227. the symbols @code{handle} (the scroll bar handle), @code{above-handle}
  1228. (the area above the handle), @code{below-handle} (the area below the
  1229. handle), @code{up} (the up arrow at one end of the scroll bar), or
  1230. @code{down} (the down arrow at one end of the scroll bar).
  1231. @c The 'top', 'bottom', and 'end-scroll' codes don't seem to be used.
  1232. @end table
  1233. @node Drag Events
  1234. @subsection Drag Events
  1235. @cindex drag event
  1236. @cindex mouse drag event
  1237. With Emacs, you can have a drag event without even changing your
  1238. clothes. A @dfn{drag event} happens every time the user presses a mouse
  1239. button and then moves the mouse to a different character position before
  1240. releasing the button. Like all mouse events, drag events are
  1241. represented in Lisp as lists. The lists record both the starting mouse
  1242. position and the final position, like this:
  1243. @example
  1244. (@var{event-type}
  1245. (@var{window1} START-POSITION)
  1246. (@var{window2} END-POSITION))
  1247. @end example
  1248. For a drag event, the name of the symbol @var{event-type} contains the
  1249. prefix @samp{drag-}. For example, dragging the mouse with button 2
  1250. held down generates a @code{drag-mouse-2} event. The second and third
  1251. elements of the event give the starting and ending position of the
  1252. drag, as mouse position lists (@pxref{Click Events}). You can access
  1253. the second element of any mouse event in the same way. However, the
  1254. drag event may end outside the boundaries of the frame that was
  1255. initially selected. In that case, the third element's position list
  1256. contains that frame in place of a window.
  1257. The @samp{drag-} prefix follows the modifier key prefixes such as
  1258. @samp{C-} and @samp{M-}.
  1259. If @code{read-key-sequence} receives a drag event that has no key
  1260. binding, and the corresponding click event does have a binding, it
  1261. changes the drag event into a click event at the drag's starting
  1262. position. This means that you don't have to distinguish between click
  1263. and drag events unless you want to.
  1264. @node Button-Down Events
  1265. @subsection Button-Down Events
  1266. @cindex button-down event
  1267. Click and drag events happen when the user releases a mouse button.
  1268. They cannot happen earlier, because there is no way to distinguish a
  1269. click from a drag until the button is released.
  1270. If you want to take action as soon as a button is pressed, you need to
  1271. handle @dfn{button-down} events.@footnote{Button-down is the
  1272. conservative antithesis of drag.} These occur as soon as a button is
  1273. pressed. They are represented by lists that look exactly like click
  1274. events (@pxref{Click Events}), except that the @var{event-type} symbol
  1275. name contains the prefix @samp{down-}. The @samp{down-} prefix follows
  1276. modifier key prefixes such as @samp{C-} and @samp{M-}.
  1277. The function @code{read-key-sequence} ignores any button-down events
  1278. that don't have command bindings; therefore, the Emacs command loop
  1279. ignores them too. This means that you need not worry about defining
  1280. button-down events unless you want them to do something. The usual
  1281. reason to define a button-down event is so that you can track mouse
  1282. motion (by reading motion events) until the button is released.
  1283. @xref{Motion Events}.
  1284. @node Repeat Events
  1285. @subsection Repeat Events
  1286. @cindex repeat events
  1287. @cindex double-click events
  1288. @cindex triple-click events
  1289. @cindex mouse events, repeated
  1290. If you press the same mouse button more than once in quick succession
  1291. without moving the mouse, Emacs generates special @dfn{repeat} mouse
  1292. events for the second and subsequent presses.
  1293. The most common repeat events are @dfn{double-click} events. Emacs
  1294. generates a double-click event when you click a button twice; the event
  1295. happens when you release the button (as is normal for all click
  1296. events).
  1297. The event type of a double-click event contains the prefix
  1298. @samp{double-}. Thus, a double click on the second mouse button with
  1299. @key{meta} held down comes to the Lisp program as
  1300. @code{M-double-mouse-2}. If a double-click event has no binding, the
  1301. binding of the corresponding ordinary click event is used to execute
  1302. it. Thus, you need not pay attention to the double click feature
  1303. unless you really want to.
  1304. When the user performs a double click, Emacs generates first an ordinary
  1305. click event, and then a double-click event. Therefore, you must design
  1306. the command binding of the double click event to assume that the
  1307. single-click command has already run. It must produce the desired
  1308. results of a double click, starting from the results of a single click.
  1309. This is convenient, if the meaning of a double click somehow builds
  1310. on the meaning of a single click---which is recommended user interface
  1311. design practice for double clicks.
  1312. If you click a button, then press it down again and start moving the
  1313. mouse with the button held down, then you get a @dfn{double-drag} event
  1314. when you ultimately release the button. Its event type contains
  1315. @samp{double-drag} instead of just @samp{drag}. If a double-drag event
  1316. has no binding, Emacs looks for an alternate binding as if the event
  1317. were an ordinary drag.
  1318. Before the double-click or double-drag event, Emacs generates a
  1319. @dfn{double-down} event when the user presses the button down for the
  1320. second time. Its event type contains @samp{double-down} instead of just
  1321. @samp{down}. If a double-down event has no binding, Emacs looks for an
  1322. alternate binding as if the event were an ordinary button-down event.
  1323. If it finds no binding that way either, the double-down event is
  1324. ignored.
  1325. To summarize, when you click a button and then press it again right
  1326. away, Emacs generates a down event and a click event for the first
  1327. click, a double-down event when you press the button again, and finally
  1328. either a double-click or a double-drag event.
  1329. If you click a button twice and then press it again, all in quick
  1330. succession, Emacs generates a @dfn{triple-down} event, followed by
  1331. either a @dfn{triple-click} or a @dfn{triple-drag}. The event types of
  1332. these events contain @samp{triple} instead of @samp{double}. If any
  1333. triple event has no binding, Emacs uses the binding that it would use
  1334. for the corresponding double event.
  1335. If you click a button three or more times and then press it again, the
  1336. events for the presses beyond the third are all triple events. Emacs
  1337. does not have separate event types for quadruple, quintuple, etc.@:
  1338. events. However, you can look at the event list to find out precisely
  1339. how many times the button was pressed.
  1340. @defun event-click-count event
  1341. This function returns the number of consecutive button presses that led
  1342. up to @var{event}. If @var{event} is a double-down, double-click or
  1343. double-drag event, the value is 2. If @var{event} is a triple event,
  1344. the value is 3 or greater. If @var{event} is an ordinary mouse event
  1345. (not a repeat event), the value is 1.
  1346. @end defun
  1347. @defopt double-click-fuzz
  1348. To generate repeat events, successive mouse button presses must be at
  1349. approximately the same screen position. The value of
  1350. @code{double-click-fuzz} specifies the maximum number of pixels the
  1351. mouse may be moved (horizontally or vertically) between two successive
  1352. clicks to make a double-click.
  1353. This variable is also the threshold for motion of the mouse to count
  1354. as a drag.
  1355. @end defopt
  1356. @defopt double-click-time
  1357. To generate repeat events, the number of milliseconds between
  1358. successive button presses must be less than the value of
  1359. @code{double-click-time}. Setting @code{double-click-time} to
  1360. @code{nil} disables multi-click detection entirely. Setting it to
  1361. @code{t} removes the time limit; Emacs then detects multi-clicks by
  1362. position only.
  1363. @end defopt
  1364. @node Motion Events
  1365. @subsection Motion Events
  1366. @cindex motion event
  1367. @cindex mouse motion events
  1368. Emacs sometimes generates @dfn{mouse motion} events to describe motion
  1369. of the mouse without any button activity. Mouse motion events are
  1370. represented by lists that look like this:
  1371. @example
  1372. (mouse-movement POSITION)
  1373. @end example
  1374. @noindent
  1375. @var{position} is a mouse position list (@pxref{Click Events}),
  1376. specifying the current position of the mouse cursor. As with the
  1377. end-position of a drag event, this position list may represent a
  1378. location outside the boundaries of the initially selected frame, in
  1379. which case the list contains that frame in place of a window.
  1380. The special form @code{track-mouse} enables generation of motion
  1381. events within its body. Outside of @code{track-mouse} forms, Emacs
  1382. does not generate events for mere motion of the mouse, and these
  1383. events do not appear. @xref{Mouse Tracking}.
  1384. @node Focus Events
  1385. @subsection Focus Events
  1386. @cindex focus event
  1387. Window systems provide general ways for the user to control which window
  1388. gets keyboard input. This choice of window is called the @dfn{focus}.
  1389. When the user does something to switch between Emacs frames, that
  1390. generates a @dfn{focus event}. The normal definition of a focus event,
  1391. in the global keymap, is to select a new frame within Emacs, as the user
  1392. would expect. @xref{Input Focus}, which also describes hooks related
  1393. to focus events.
  1394. Focus events are represented in Lisp as lists that look like this:
  1395. @example
  1396. (switch-frame @var{new-frame})
  1397. @end example
  1398. @noindent
  1399. where @var{new-frame} is the frame switched to.
  1400. Some X window managers are set up so that just moving the mouse into a
  1401. window is enough to set the focus there. Usually, there is no need
  1402. for a Lisp program to know about the focus change until some other
  1403. kind of input arrives. Emacs generates a focus event only when the
  1404. user actually types a keyboard key or presses a mouse button in the
  1405. new frame; just moving the mouse between frames does not generate a
  1406. focus event.
  1407. A focus event in the middle of a key sequence would garble the
  1408. sequence. So Emacs never generates a focus event in the middle of a key
  1409. sequence. If the user changes focus in the middle of a key
  1410. sequence---that is, after a prefix key---then Emacs reorders the events
  1411. so that the focus event comes either before or after the multi-event key
  1412. sequence, and not within it.
  1413. @node Misc Events
  1414. @subsection Miscellaneous System Events
  1415. A few other event types represent occurrences within the system.
  1416. @table @code
  1417. @cindex @code{delete-frame} event
  1418. @item (delete-frame (@var{frame}))
  1419. This kind of event indicates that the user gave the window manager
  1420. a command to delete a particular window, which happens to be an Emacs frame.
  1421. The standard definition of the @code{delete-frame} event is to delete @var{frame}.
  1422. @cindex @code{iconify-frame} event
  1423. @item (iconify-frame (@var{frame}))
  1424. This kind of event indicates that the user iconified @var{frame} using
  1425. the window manager. Its standard definition is @code{ignore}; since the
  1426. frame has already been iconified, Emacs has no work to do. The purpose
  1427. of this event type is so that you can keep track of such events if you
  1428. want to.
  1429. @cindex @code{make-frame-visible} event
  1430. @item (make-frame-visible (@var{frame}))
  1431. This kind of event indicates that the user deiconified @var{frame} using
  1432. the window manager. Its standard definition is @code{ignore}; since the
  1433. frame has already been made visible, Emacs has no work to do.
  1434. @cindex @code{wheel-up} event
  1435. @cindex @code{wheel-down} event
  1436. @item (wheel-up @var{position})
  1437. @itemx (wheel-down @var{position})
  1438. These kinds of event are generated by moving a mouse wheel. The
  1439. @var{position} element is a mouse position list (@pxref{Click
  1440. Events}), specifying the position of the mouse cursor when the event
  1441. occurred.
  1442. @vindex mouse-wheel-up-event
  1443. @vindex mouse-wheel-down-event
  1444. This kind of event is generated only on some kinds of systems. On some
  1445. systems, @code{mouse-4} and @code{mouse-5} are used instead. For
  1446. portable code, use the variables @code{mouse-wheel-up-event} and
  1447. @code{mouse-wheel-down-event} defined in @file{mwheel.el} to determine
  1448. what event types to expect for the mouse wheel.
  1449. @cindex @code{drag-n-drop} event
  1450. @item (drag-n-drop @var{position} @var{files})
  1451. This kind of event is generated when a group of files is
  1452. selected in an application outside of Emacs, and then dragged and
  1453. dropped onto an Emacs frame.
  1454. The element @var{position} is a list describing the position of the
  1455. event, in the same format as used in a mouse-click event (@pxref{Click
  1456. Events}), and @var{files} is the list of file names that were dragged
  1457. and dropped. The usual way to handle this event is by visiting these
  1458. files.
  1459. This kind of event is generated, at present, only on some kinds of
  1460. systems.
  1461. @cindex @code{help-echo} event
  1462. @item help-echo
  1463. This kind of event is generated when a mouse pointer moves onto a
  1464. portion of buffer text which has a @code{help-echo} text property.
  1465. The generated event has this form:
  1466. @example
  1467. (help-echo @var{frame} @var{help} @var{window} @var{object} @var{pos})
  1468. @end example
  1469. @noindent
  1470. The precise meaning of the event parameters and the way these
  1471. parameters are used to display the help-echo text are described in
  1472. @ref{Text help-echo}.
  1473. @cindex @code{sigusr1} event
  1474. @cindex @code{sigusr2} event
  1475. @cindex user signals
  1476. @item sigusr1
  1477. @itemx sigusr2
  1478. These events are generated when the Emacs process receives
  1479. the signals @code{SIGUSR1} and @code{SIGUSR2}. They contain no
  1480. additional data because signals do not carry additional information.
  1481. They can be useful for debugging (@pxref{Error Debugging}).
  1482. To catch a user signal, bind the corresponding event to an interactive
  1483. command in the @code{special-event-map} (@pxref{Active Keymaps}).
  1484. The command is called with no arguments, and the specific signal event is
  1485. available in @code{last-input-event}. For example:
  1486. @smallexample
  1487. (defun sigusr-handler ()
  1488. (interactive)
  1489. (message "Caught signal %S" last-input-event))
  1490. (define-key special-event-map [sigusr1] 'sigusr-handler)
  1491. @end smallexample
  1492. To test the signal handler, you can make Emacs send a signal to itself:
  1493. @smallexample
  1494. (signal-process (emacs-pid) 'sigusr1)
  1495. @end smallexample
  1496. @cindex @code{language-change} event
  1497. @item language-change
  1498. This kind of event is generated on MS-Windows when the input language
  1499. has changed. This typically means that the keyboard keys will send to
  1500. Emacs characters from a different language. The generated event has
  1501. this form:
  1502. @smallexample
  1503. (language-change @var{frame} @var{codepage} @var{language-id})
  1504. @end smallexample
  1505. @noindent
  1506. Here @var{frame} is the frame which was current when the input
  1507. language changed; @var{codepage} is the new codepage number; and
  1508. @var{language-id} is the numerical ID of the new input language. The
  1509. coding-system (@pxref{Coding Systems}) that corresponds to
  1510. @var{codepage} is @code{cp@var{codepage}} or
  1511. @code{windows-@var{codepage}}. To convert @var{language-id} to a
  1512. string (e.g., to use it for various language-dependent features, such
  1513. as @code{set-language-environment}), use the
  1514. @code{w32-get-locale-info} function, like this:
  1515. @smallexample
  1516. ;; Get the abbreviated language name, such as "ENU" for English
  1517. (w32-get-locale-info language-id)
  1518. ;; Get the full English name of the language,
  1519. ;; such as "English (United States)"
  1520. (w32-get-locale-info language-id 4097)
  1521. ;; Get the full localized name of the language
  1522. (w32-get-locale-info language-id t)
  1523. @end smallexample
  1524. @end table
  1525. If one of these events arrives in the middle of a key sequence---that
  1526. is, after a prefix key---then Emacs reorders the events so that this
  1527. event comes either before or after the multi-event key sequence, not
  1528. within it.
  1529. @node Event Examples
  1530. @subsection Event Examples
  1531. If the user presses and releases the left mouse button over the same
  1532. location, that generates a sequence of events like this:
  1533. @smallexample
  1534. (down-mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864320))
  1535. (mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864180))
  1536. @end smallexample
  1537. While holding the control key down, the user might hold down the
  1538. second mouse button, and drag the mouse from one line to the next.
  1539. That produces two events, as shown here:
  1540. @smallexample
  1541. (C-down-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219))
  1542. (C-drag-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219)
  1543. (#<window 18 on NEWS> 3510 (0 . 28) -729648))
  1544. @end smallexample
  1545. While holding down the meta and shift keys, the user might press the
  1546. second mouse button on the window's mode line, and then drag the mouse
  1547. into another window. That produces a pair of events like these:
  1548. @smallexample
  1549. (M-S-down-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844))
  1550. (M-S-drag-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844)
  1551. (#<window 20 on carlton-sanskrit.tex> 161 (33 . 3)
  1552. -453816))
  1553. @end smallexample
  1554. The frame with input focus might not take up the entire screen, and
  1555. the user might move the mouse outside the scope of the frame. Inside
  1556. the @code{track-mouse} special form, that produces an event like this:
  1557. @smallexample
  1558. (mouse-movement (#<frame *ielm* 0x102849a30> nil (563 . 205) 532301936))
  1559. @end smallexample
  1560. To handle a SIGUSR1 signal, define an interactive function, and
  1561. bind it to the @code{signal usr1} event sequence:
  1562. @smallexample
  1563. (defun usr1-handler ()
  1564. (interactive)
  1565. (message "Got USR1 signal"))
  1566. (global-set-key [signal usr1] 'usr1-handler)
  1567. @end smallexample
  1568. @node Classifying Events
  1569. @subsection Classifying Events
  1570. @cindex event type
  1571. @cindex classifying events
  1572. Every event has an @dfn{event type}, which classifies the event for
  1573. key binding purposes. For a keyboard event, the event type equals the
  1574. event value; thus, the event type for a character is the character, and
  1575. the event type for a function key symbol is the symbol itself. For
  1576. events that are lists, the event type is the symbol in the @sc{car} of
  1577. the list. Thus, the event type is always a symbol or a character.
  1578. Two events of the same type are equivalent where key bindings are
  1579. concerned; thus, they always run the same command. That does not
  1580. necessarily mean they do the same things, however, as some commands look
  1581. at the whole event to decide what to do. For example, some commands use
  1582. the location of a mouse event to decide where in the buffer to act.
  1583. Sometimes broader classifications of events are useful. For example,
  1584. you might want to ask whether an event involved the @key{META} key,
  1585. regardless of which other key or mouse button was used.
  1586. The functions @code{event-modifiers} and @code{event-basic-type} are
  1587. provided to get such information conveniently.
  1588. @defun event-modifiers event
  1589. This function returns a list of the modifiers that @var{event} has. The
  1590. modifiers are symbols; they include @code{shift}, @code{control},
  1591. @code{meta}, @code{alt}, @code{hyper} and @code{super}. In addition,
  1592. the modifiers list of a mouse event symbol always contains one of
  1593. @code{click}, @code{drag}, and @code{down}. For double or triple
  1594. events, it also contains @code{double} or @code{triple}.
  1595. The argument @var{event} may be an entire event object, or just an
  1596. event type. If @var{event} is a symbol that has never been used in an
  1597. event that has been read as input in the current Emacs session, then
  1598. @code{event-modifiers} can return @code{nil}, even when @var{event}
  1599. actually has modifiers.
  1600. Here are some examples:
  1601. @example
  1602. (event-modifiers ?a)
  1603. @result{} nil
  1604. (event-modifiers ?A)
  1605. @result{} (shift)
  1606. (event-modifiers ?\C-a)
  1607. @result{} (control)
  1608. (event-modifiers ?\C-%)
  1609. @result{} (control)
  1610. (event-modifiers ?\C-\S-a)
  1611. @result{} (control shift)
  1612. (event-modifiers 'f5)
  1613. @result{} nil
  1614. (event-modifiers 's-f5)
  1615. @result{} (super)
  1616. (event-modifiers 'M-S-f5)
  1617. @result{} (meta shift)
  1618. (event-modifiers 'mouse-1)
  1619. @result{} (click)
  1620. (event-modifiers 'down-mouse-1)
  1621. @result{} (down)
  1622. @end example
  1623. The modifiers list for a click event explicitly contains @code{click},
  1624. but the event symbol name itself does not contain @samp{click}.
  1625. @end defun
  1626. @defun event-basic-type event
  1627. This function returns the key or mouse button that @var{event}
  1628. describes, with all modifiers removed. The @var{event} argument is as
  1629. in @code{event-modifiers}. For example:
  1630. @example
  1631. (event-basic-type ?a)
  1632. @result{} 97
  1633. (event-basic-type ?A)
  1634. @result{} 97
  1635. (event-basic-type ?\C-a)
  1636. @result{} 97
  1637. (event-basic-type ?\C-\S-a)
  1638. @result{} 97
  1639. (event-basic-type 'f5)
  1640. @result{} f5
  1641. (event-basic-type 's-f5)
  1642. @result{} f5
  1643. (event-basic-type 'M-S-f5)
  1644. @result{} f5
  1645. (event-basic-type 'down-mouse-1)
  1646. @result{} mouse-1
  1647. @end example
  1648. @end defun
  1649. @defun mouse-movement-p object
  1650. This function returns non-@code{nil} if @var{object} is a mouse movement
  1651. event.
  1652. @end defun
  1653. @defun event-convert-list list
  1654. This function converts a list of modifier names and a basic event type
  1655. to an event type which specifies all of them. The basic event type
  1656. must be the last element of the list. For example,
  1657. @example
  1658. (event-convert-list '(control ?a))
  1659. @result{} 1
  1660. (event-convert-list '(control meta ?a))
  1661. @result{} -134217727
  1662. (event-convert-list '(control super f1))
  1663. @result{} C-s-f1
  1664. @end example
  1665. @end defun
  1666. @node Accessing Mouse
  1667. @subsection Accessing Mouse Events
  1668. @cindex mouse events, data in
  1669. @cindex keyboard events, data in
  1670. This section describes convenient functions for accessing the data in
  1671. a mouse button or motion event. Keyboard event data can be accessed
  1672. using the same functions, but data elements that aren't applicable to
  1673. keyboard events are zero or @code{nil}.
  1674. The following two functions return a mouse position list
  1675. (@pxref{Click Events}), specifying the position of a mouse event.
  1676. @defun event-start event
  1677. This returns the starting position of @var{event}.
  1678. If @var{event} is a click or button-down event, this returns the
  1679. location of the event. If @var{event} is a drag event, this returns the
  1680. drag's starting position.
  1681. @end defun
  1682. @defun event-end event
  1683. This returns the ending position of @var{event}.
  1684. If @var{event} is a drag event, this returns the position where the user
  1685. released the mouse button. If @var{event} is a click or button-down
  1686. event, the value is actually the starting position, which is the only
  1687. position such events have.
  1688. @end defun
  1689. @defun posnp object
  1690. This function returns non-@code{nil} if @var{object} is a mouse
  1691. position list, in either of the formats documented in @ref{Click
  1692. Events}); and @code{nil} otherwise.
  1693. @end defun
  1694. @cindex mouse position list, accessing
  1695. These functions take a mouse position list as argument, and return
  1696. various parts of it:
  1697. @defun posn-window position
  1698. Return the window that @var{position} is in. If @var{position}
  1699. represents a location outside the frame where the event was initiated,
  1700. return that frame instead.
  1701. @end defun
  1702. @defun posn-area position
  1703. Return the window area recorded in @var{position}. It returns @code{nil}
  1704. when the event occurred in the text area of the window; otherwise, it
  1705. is a symbol identifying the area in which the event occurred.
  1706. @end defun
  1707. @defun posn-point position
  1708. Return the buffer position in @var{position}. When the event occurred
  1709. in the text area of the window, in a marginal area, or on a fringe,
  1710. this is an integer specifying a buffer position. Otherwise, the value
  1711. is undefined.
  1712. @end defun
  1713. @defun posn-x-y position
  1714. Return the pixel-based x and y coordinates in @var{position}, as a
  1715. cons cell @code{(@var{x} . @var{y})}. These coordinates are relative
  1716. to the window given by @code{posn-window}.
  1717. This example shows how to convert the window-relative coordinates in
  1718. the text area of a window into frame-relative coordinates:
  1719. @example
  1720. (defun frame-relative-coordinates (position)
  1721. "Return frame-relative coordinates from POSITION.
  1722. POSITION is assumed to lie in a window text area."
  1723. (let* ((x-y (posn-x-y position))
  1724. (window (posn-window position))
  1725. (edges (window-inside-pixel-edges window)))
  1726. (cons (+ (car x-y) (car edges))
  1727. (+ (cdr x-y) (cadr edges)))))
  1728. @end example
  1729. @end defun
  1730. @defun posn-col-row position
  1731. This function returns a cons cell @code{(@var{col} . @var{row})},
  1732. containing the estimated column and row corresponding to buffer
  1733. position in @var{position}. The return value is given in units of the
  1734. frame's default character width and default line height (including
  1735. spacing), as computed from the @var{x} and @var{y} values
  1736. corresponding to @var{position}. (So, if the actual characters have
  1737. non-default sizes, the actual row and column may differ from these
  1738. computed values.)
  1739. Note that @var{row} is counted from the top of the text area. If the
  1740. window given by @var{position} possesses a header line (@pxref{Header
  1741. Lines}), it is @emph{not} included in the @var{row} count.
  1742. @end defun
  1743. @defun posn-actual-col-row position
  1744. Return the actual row and column in @var{position}, as a cons cell
  1745. @code{(@var{col} . @var{row})}. The values are the actual row and
  1746. column numbers in the window given by @var{position}. @xref{Click
  1747. Events}, for details. The function returns @code{nil} if
  1748. @var{position} does not include actual position values; in that case
  1749. @code{posn-col-row} can be used to get approximate values.
  1750. Note that this function doesn't account for the visual width of
  1751. characters on display, like the number of visual columns taken by a
  1752. tab character or an image. If you need the coordinates in canonical
  1753. character units, use @code{posn-col-row} instead.
  1754. @end defun
  1755. @defun posn-string position
  1756. Return the string object in @var{position}, either @code{nil}, or a
  1757. cons cell @code{(@var{string} . @var{string-pos})}.
  1758. @end defun
  1759. @defun posn-image position
  1760. Return the image object in @var{position}, either @code{nil}, or an
  1761. image @code{(image ...)}.
  1762. @end defun
  1763. @defun posn-object position
  1764. Return the image or string object in @var{position}, either
  1765. @code{nil}, an image @code{(image ...)}, or a cons cell
  1766. @code{(@var{string} . @var{string-pos})}.
  1767. @end defun
  1768. @defun posn-object-x-y position
  1769. Return the pixel-based x and y coordinates relative to the upper left
  1770. corner of the object in @var{position} as a cons cell @code{(@var{dx}
  1771. . @var{dy})}. If the @var{position} is on buffer text, return the
  1772. relative position of the buffer-text character closest to that
  1773. position.
  1774. @end defun
  1775. @defun posn-object-width-height position
  1776. Return the pixel width and height of the object in @var{position} as a
  1777. cons cell @code{(@var{width} . @var{height})}. If the @var{position}
  1778. is a buffer position, return the size of the character at that position.
  1779. @end defun
  1780. @cindex timestamp of a mouse event
  1781. @defun posn-timestamp position
  1782. Return the timestamp in @var{position}. This is the time at which the
  1783. event occurred, in milliseconds.
  1784. @end defun
  1785. These functions compute a position list given particular buffer
  1786. position or screen position. You can access the data in this position
  1787. list with the functions described above.
  1788. @defun posn-at-point &optional pos window
  1789. This function returns a position list for position @var{pos} in
  1790. @var{window}. @var{pos} defaults to point in @var{window};
  1791. @var{window} defaults to the selected window.
  1792. @code{posn-at-point} returns @code{nil} if @var{pos} is not visible in
  1793. @var{window}.
  1794. @end defun
  1795. @defun posn-at-x-y x y &optional frame-or-window whole
  1796. This function returns position information corresponding to pixel
  1797. coordinates @var{x} and @var{y} in a specified frame or window,
  1798. @var{frame-or-window}, which defaults to the selected window.
  1799. The coordinates @var{x} and @var{y} are relative to the
  1800. frame or window used.
  1801. If @var{whole} is @code{nil}, the coordinates are relative
  1802. to the window text area, otherwise they are relative to
  1803. the entire window area including scroll bars, margins and fringes.
  1804. @end defun
  1805. @node Accessing Scroll
  1806. @subsection Accessing Scroll Bar Events
  1807. @cindex scroll bar events, data in
  1808. These functions are useful for decoding scroll bar events.
  1809. @defun scroll-bar-event-ratio event
  1810. This function returns the fractional vertical position of a scroll bar
  1811. event within the scroll bar. The value is a cons cell
  1812. @code{(@var{portion} . @var{whole})} containing two integers whose ratio
  1813. is the fractional position.
  1814. @end defun
  1815. @defun scroll-bar-scale ratio total
  1816. This function multiplies (in effect) @var{ratio} by @var{total},
  1817. rounding the result to an integer. The argument @var{ratio} is not a
  1818. number, but rather a pair @code{(@var{num} . @var{denom})}---typically a
  1819. value returned by @code{scroll-bar-event-ratio}.
  1820. This function is handy for scaling a position on a scroll bar into a
  1821. buffer position. Here's how to do that:
  1822. @example
  1823. (+ (point-min)
  1824. (scroll-bar-scale
  1825. (posn-x-y (event-start event))
  1826. (- (point-max) (point-min))))
  1827. @end example
  1828. Recall that scroll bar events have two integers forming a ratio, in place
  1829. of a pair of x and y coordinates.
  1830. @end defun
  1831. @node Strings of Events
  1832. @subsection Putting Keyboard Events in Strings
  1833. @cindex keyboard events in strings
  1834. @cindex strings with keyboard events
  1835. In most of the places where strings are used, we conceptualize the
  1836. string as containing text characters---the same kind of characters found
  1837. in buffers or files. Occasionally Lisp programs use strings that
  1838. conceptually contain keyboard characters; for example, they may be key
  1839. sequences or keyboard macro definitions. However, storing keyboard
  1840. characters in a string is a complex matter, for reasons of historical
  1841. compatibility, and it is not always possible.
  1842. We recommend that new programs avoid dealing with these complexities
  1843. by not storing keyboard events in strings. Here is how to do that:
  1844. @itemize @bullet
  1845. @item
  1846. Use vectors instead of strings for key sequences, when you plan to use
  1847. them for anything other than as arguments to @code{lookup-key} and
  1848. @code{define-key}. For example, you can use
  1849. @code{read-key-sequence-vector} instead of @code{read-key-sequence}, and
  1850. @code{this-command-keys-vector} instead of @code{this-command-keys}.
  1851. @item
  1852. Use vectors to write key sequence constants containing meta characters,
  1853. even when passing them directly to @code{define-key}.
  1854. @item
  1855. When you have to look at the contents of a key sequence that might be a
  1856. string, use @code{listify-key-sequence} (@pxref{Event Input Misc})
  1857. first, to convert it to a list.
  1858. @end itemize
  1859. The complexities stem from the modifier bits that keyboard input
  1860. characters can include. Aside from the Meta modifier, none of these
  1861. modifier bits can be included in a string, and the Meta modifier is
  1862. allowed only in special cases.
  1863. The earliest GNU Emacs versions represented meta characters as codes
  1864. in the range of 128 to 255. At that time, the basic character codes
  1865. ranged from 0 to 127, so all keyboard character codes did fit in a
  1866. string. Many Lisp programs used @samp{\M-} in string constants to stand
  1867. for meta characters, especially in arguments to @code{define-key} and
  1868. similar functions, and key sequences and sequences of events were always
  1869. represented as strings.
  1870. When we added support for larger basic character codes beyond 127, and
  1871. additional modifier bits, we had to change the representation of meta
  1872. characters. Now the flag that represents the Meta modifier in a
  1873. character is
  1874. @tex
  1875. @math{2^{27}}
  1876. @end tex
  1877. @ifnottex
  1878. 2**27
  1879. @end ifnottex
  1880. and such numbers cannot be included in a string.
  1881. To support programs with @samp{\M-} in string constants, there are
  1882. special rules for including certain meta characters in a string.
  1883. Here are the rules for interpreting a string as a sequence of input
  1884. characters:
  1885. @itemize @bullet
  1886. @item
  1887. If the keyboard character value is in the range of 0 to 127, it can go
  1888. in the string unchanged.
  1889. @item
  1890. The meta variants of those characters, with codes in the range of
  1891. @tex
  1892. @math{2^{27}}
  1893. @end tex
  1894. @ifnottex
  1895. 2**27
  1896. @end ifnottex
  1897. to
  1898. @tex
  1899. @math{2^{27} + 127},
  1900. @end tex
  1901. @ifnottex
  1902. 2**27+127,
  1903. @end ifnottex
  1904. can also go in the string, but you must change their
  1905. numeric values. You must set the
  1906. @tex
  1907. @math{2^{7}}
  1908. @end tex
  1909. @ifnottex
  1910. 2**7
  1911. @end ifnottex
  1912. bit instead of the
  1913. @tex
  1914. @math{2^{27}}
  1915. @end tex
  1916. @ifnottex
  1917. 2**27
  1918. @end ifnottex
  1919. bit, resulting in a value between 128 and 255. Only a unibyte string
  1920. can include these codes.
  1921. @item
  1922. Non-@acronym{ASCII} characters above 256 can be included in a multibyte string.
  1923. @item
  1924. Other keyboard character events cannot fit in a string. This includes
  1925. keyboard events in the range of 128 to 255.
  1926. @end itemize
  1927. Functions such as @code{read-key-sequence} that construct strings of
  1928. keyboard input characters follow these rules: they construct vectors
  1929. instead of strings, when the events won't fit in a string.
  1930. When you use the read syntax @samp{\M-} in a string, it produces a
  1931. code in the range of 128 to 255---the same code that you get if you
  1932. modify the corresponding keyboard event to put it in the string. Thus,
  1933. meta events in strings work consistently regardless of how they get into
  1934. the strings.
  1935. However, most programs would do well to avoid these issues by
  1936. following the recommendations at the beginning of this section.
  1937. @node Reading Input
  1938. @section Reading Input
  1939. @cindex read input
  1940. @cindex keyboard input
  1941. The editor command loop reads key sequences using the function
  1942. @code{read-key-sequence}, which uses @code{read-event}. These and other
  1943. functions for event input are also available for use in Lisp programs.
  1944. See also @code{momentary-string-display} in @ref{Temporary Displays},
  1945. and @code{sit-for} in @ref{Waiting}. @xref{Terminal Input}, for
  1946. functions and variables for controlling terminal input modes and
  1947. debugging terminal input.
  1948. For higher-level input facilities, see @ref{Minibuffers}.
  1949. @menu
  1950. * Key Sequence Input:: How to read one key sequence.
  1951. * Reading One Event:: How to read just one event.
  1952. * Event Mod:: How Emacs modifies events as they are read.
  1953. * Invoking the Input Method:: How reading an event uses the input method.
  1954. * Quoted Character Input:: Asking the user to specify a character.
  1955. * Event Input Misc:: How to reread or throw away input events.
  1956. @end menu
  1957. @node Key Sequence Input
  1958. @subsection Key Sequence Input
  1959. @cindex key sequence input
  1960. The command loop reads input a key sequence at a time, by calling
  1961. @code{read-key-sequence}. Lisp programs can also call this function;
  1962. for example, @code{describe-key} uses it to read the key to describe.
  1963. @defun read-key-sequence prompt &optional continue-echo dont-downcase-last switch-frame-ok command-loop
  1964. This function reads a key sequence and returns it as a string or
  1965. vector. It keeps reading events until it has accumulated a complete key
  1966. sequence; that is, enough to specify a non-prefix command using the
  1967. currently active keymaps. (Remember that a key sequence that starts
  1968. with a mouse event is read using the keymaps of the buffer in the
  1969. window that the mouse was in, not the current buffer.)
  1970. If the events are all characters and all can fit in a string, then
  1971. @code{read-key-sequence} returns a string (@pxref{Strings of Events}).
  1972. Otherwise, it returns a vector, since a vector can hold all kinds of
  1973. events---characters, symbols, and lists. The elements of the string or
  1974. vector are the events in the key sequence.
  1975. Reading a key sequence includes translating the events in various
  1976. ways. @xref{Translation Keymaps}.
  1977. The argument @var{prompt} is either a string to be displayed in the
  1978. echo area as a prompt, or @code{nil}, meaning not to display a prompt.
  1979. The argument @var{continue-echo}, if non-@code{nil}, means to echo
  1980. this key as a continuation of the previous key.
  1981. Normally any upper case event is converted to lower case if the
  1982. original event is undefined and the lower case equivalent is defined.
  1983. The argument @var{dont-downcase-last}, if non-@code{nil}, means do not
  1984. convert the last event to lower case. This is appropriate for reading
  1985. a key sequence to be defined.
  1986. The argument @var{switch-frame-ok}, if non-@code{nil}, means that this
  1987. function should process a @code{switch-frame} event if the user
  1988. switches frames before typing anything. If the user switches frames
  1989. in the middle of a key sequence, or at the start of the sequence but
  1990. @var{switch-frame-ok} is @code{nil}, then the event will be put off
  1991. until after the current key sequence.
  1992. The argument @var{command-loop}, if non-@code{nil}, means that this
  1993. key sequence is being read by something that will read commands one
  1994. after another. It should be @code{nil} if the caller will read just
  1995. one key sequence.
  1996. In the following example, Emacs displays the prompt @samp{?} in the
  1997. echo area, and then the user types @kbd{C-x C-f}.
  1998. @example
  1999. (read-key-sequence "?")
  2000. @group
  2001. ---------- Echo Area ----------
  2002. ?@kbd{C-x C-f}
  2003. ---------- Echo Area ----------
  2004. @result{} "^X^F"
  2005. @end group
  2006. @end example
  2007. The function @code{read-key-sequence} suppresses quitting: @kbd{C-g}
  2008. typed while reading with this function works like any other character,
  2009. and does not set @code{quit-flag}. @xref{Quitting}.
  2010. @end defun
  2011. @defun read-key-sequence-vector prompt &optional continue-echo dont-downcase-last switch-frame-ok command-loop
  2012. This is like @code{read-key-sequence} except that it always
  2013. returns the key sequence as a vector, never as a string.
  2014. @xref{Strings of Events}.
  2015. @end defun
  2016. @cindex upper case key sequence
  2017. @cindex downcasing in @code{lookup-key}
  2018. @cindex shift-translation
  2019. If an input character is upper-case (or has the shift modifier) and
  2020. has no key binding, but its lower-case equivalent has one, then
  2021. @code{read-key-sequence} converts the character to lower case. Note
  2022. that @code{lookup-key} does not perform case conversion in this way.
  2023. @vindex this-command-keys-shift-translated
  2024. When reading input results in such a @dfn{shift-translation}, Emacs
  2025. sets the variable @code{this-command-keys-shift-translated} to a
  2026. non-@code{nil} value. Lisp programs can examine this variable if they
  2027. need to modify their behavior when invoked by shift-translated keys.
  2028. For example, the function @code{handle-shift-selection} examines the
  2029. value of this variable to determine how to activate or deactivate the
  2030. region (@pxref{The Mark, handle-shift-selection}).
  2031. The function @code{read-key-sequence} also transforms some mouse events.
  2032. It converts unbound drag events into click events, and discards unbound
  2033. button-down events entirely. It also reshuffles focus events and
  2034. miscellaneous window events so that they never appear in a key sequence
  2035. with any other events.
  2036. @cindex @code{header-line} prefix key
  2037. @cindex @code{mode-line} prefix key
  2038. @cindex @code{vertical-line} prefix key
  2039. @cindex @code{horizontal-scroll-bar} prefix key
  2040. @cindex @code{vertical-scroll-bar} prefix key
  2041. @cindex @code{menu-bar} prefix key
  2042. @cindex mouse events, in special parts of frame
  2043. When mouse events occur in special parts of a window, such as a mode
  2044. line or a scroll bar, the event type shows nothing special---it is the
  2045. same symbol that would normally represent that combination of mouse
  2046. button and modifier keys. The information about the window part is kept
  2047. elsewhere in the event---in the coordinates. But
  2048. @code{read-key-sequence} translates this information into imaginary
  2049. prefix keys, all of which are symbols: @code{header-line},
  2050. @code{horizontal-scroll-bar}, @code{menu-bar}, @code{mode-line},
  2051. @code{vertical-line}, and @code{vertical-scroll-bar}. You can define
  2052. meanings for mouse clicks in special window parts by defining key
  2053. sequences using these imaginary prefix keys.
  2054. For example, if you call @code{read-key-sequence} and then click the
  2055. mouse on the window's mode line, you get two events, like this:
  2056. @example
  2057. (read-key-sequence "Click on the mode line: ")
  2058. @result{} [mode-line
  2059. (mouse-1
  2060. (#<window 6 on NEWS> mode-line
  2061. (40 . 63) 5959987))]
  2062. @end example
  2063. @defvar num-input-keys
  2064. This variable's value is the number of key sequences processed so far in
  2065. this Emacs session. This includes key sequences read from the terminal
  2066. and key sequences read from keyboard macros being executed.
  2067. @end defvar
  2068. @node Reading One Event
  2069. @subsection Reading One Event
  2070. @cindex reading a single event
  2071. @cindex event, reading only one
  2072. The lowest level functions for command input are @code{read-event},
  2073. @code{read-char}, and @code{read-char-exclusive}.
  2074. @defun read-event &optional prompt inherit-input-method seconds
  2075. This function reads and returns the next event of command input,
  2076. waiting if necessary until an event is available.
  2077. The returned event may come directly from the user, or from a keyboard
  2078. macro. It is not decoded by the keyboard's input coding system
  2079. (@pxref{Terminal I/O Encoding}).
  2080. If the optional argument @var{prompt} is non-@code{nil}, it should be a
  2081. string to display in the echo area as a prompt. Otherwise,
  2082. @code{read-event} does not display any message to indicate it is waiting
  2083. for input; instead, it prompts by echoing: it displays descriptions of
  2084. the events that led to or were read by the current command. @xref{The
  2085. Echo Area}.
  2086. If @var{inherit-input-method} is non-@code{nil}, then the current input
  2087. method (if any) is employed to make it possible to enter a
  2088. non-@acronym{ASCII} character. Otherwise, input method handling is disabled
  2089. for reading this event.
  2090. If @code{cursor-in-echo-area} is non-@code{nil}, then @code{read-event}
  2091. moves the cursor temporarily to the echo area, to the end of any message
  2092. displayed there. Otherwise @code{read-event} does not move the cursor.
  2093. If @var{seconds} is non-@code{nil}, it should be a number specifying
  2094. the maximum time to wait for input, in seconds. If no input arrives
  2095. within that time, @code{read-event} stops waiting and returns
  2096. @code{nil}. A floating point @var{seconds} means to wait
  2097. for a fractional number of seconds. Some systems support only a whole
  2098. number of seconds; on these systems, @var{seconds} is rounded down.
  2099. If @var{seconds} is @code{nil}, @code{read-event} waits as long as
  2100. necessary for input to arrive.
  2101. If @var{seconds} is @code{nil}, Emacs is considered idle while waiting
  2102. for user input to arrive. Idle timers---those created with
  2103. @code{run-with-idle-timer} (@pxref{Idle Timers})---can run during this
  2104. period. However, if @var{seconds} is non-@code{nil}, the state of
  2105. idleness remains unchanged. If Emacs is non-idle when
  2106. @code{read-event} is called, it remains non-idle throughout the
  2107. operation of @code{read-event}; if Emacs is idle (which can happen if
  2108. the call happens inside an idle timer), it remains idle.
  2109. If @code{read-event} gets an event that is defined as a help character,
  2110. then in some cases @code{read-event} processes the event directly without
  2111. returning. @xref{Help Functions}. Certain other events, called
  2112. @dfn{special events}, are also processed directly within
  2113. @code{read-event} (@pxref{Special Events}).
  2114. Here is what happens if you call @code{read-event} and then press the
  2115. right-arrow function key:
  2116. @example
  2117. @group
  2118. (read-event)
  2119. @result{} right
  2120. @end group
  2121. @end example
  2122. @end defun
  2123. @defun read-char &optional prompt inherit-input-method seconds
  2124. This function reads and returns a character of command input. If the
  2125. user generates an event which is not a character (i.e., a mouse click or
  2126. function key event), @code{read-char} signals an error. The arguments
  2127. work as in @code{read-event}.
  2128. In the first example, the user types the character @kbd{1} (@acronym{ASCII}
  2129. code 49). The second example shows a keyboard macro definition that
  2130. calls @code{read-char} from the minibuffer using @code{eval-expression}.
  2131. @code{read-char} reads the keyboard macro's very next character, which
  2132. is @kbd{1}. Then @code{eval-expression} displays its return value in
  2133. the echo area.
  2134. @example
  2135. @group
  2136. (read-char)
  2137. @result{} 49
  2138. @end group
  2139. @group
  2140. ;; @r{We assume here you use @kbd{M-:} to evaluate this.}
  2141. (symbol-function 'foo)
  2142. @result{} "^[:(read-char)^M1"
  2143. @end group
  2144. @group
  2145. (execute-kbd-macro 'foo)
  2146. @print{} 49
  2147. @result{} nil
  2148. @end group
  2149. @end example
  2150. @end defun
  2151. @defun read-char-exclusive &optional prompt inherit-input-method seconds
  2152. This function reads and returns a character of command input. If the
  2153. user generates an event which is not a character,
  2154. @code{read-char-exclusive} ignores it and reads another event, until it
  2155. gets a character. The arguments work as in @code{read-event}.
  2156. @end defun
  2157. None of the above functions suppress quitting.
  2158. @defvar num-nonmacro-input-events
  2159. This variable holds the total number of input events received so far
  2160. from the terminal---not counting those generated by keyboard macros.
  2161. @end defvar
  2162. We emphasize that, unlike @code{read-key-sequence}, the functions
  2163. @code{read-event}, @code{read-char}, and @code{read-char-exclusive} do
  2164. not perform the translations described in @ref{Translation Keymaps}.
  2165. If you wish to read a single key taking these translations into
  2166. account, use the function @code{read-key}:
  2167. @defun read-key &optional prompt
  2168. This function reads a single key. It is intermediate between
  2169. @code{read-key-sequence} and @code{read-event}. Unlike the former, it
  2170. reads a single key, not a key sequence. Unlike the latter, it does
  2171. not return a raw event, but decodes and translates the user input
  2172. according to @code{input-decode-map}, @code{local-function-key-map},
  2173. and @code{key-translation-map} (@pxref{Translation Keymaps}).
  2174. The argument @var{prompt} is either a string to be displayed in the
  2175. echo area as a prompt, or @code{nil}, meaning not to display a prompt.
  2176. @end defun
  2177. @defun read-char-choice prompt chars &optional inhibit-quit
  2178. This function uses @code{read-key} to read and return a single
  2179. character. It ignores any input that is not a member of @var{chars},
  2180. a list of accepted characters. Optionally, it will also ignore
  2181. keyboard-quit events while it is waiting for valid input. If you bind
  2182. @code{help-form} (@pxref{Help Functions}) to a non-@code{nil} value
  2183. while calling @code{read-char-choice}, then pressing @code{help-char}
  2184. causes it to evaluate @code{help-form} and display the result. It
  2185. then continues to wait for a valid input character, or keyboard-quit.
  2186. @end defun
  2187. @defun read-multiple-choice prompt choices
  2188. Ask user a multiple choice question. @var{prompt} should be a string
  2189. that will be displayed as the prompt.
  2190. @var{choices} is an alist where the first element in each entry is a
  2191. character to be entered, the second element is a short name for the
  2192. entry to be displayed while prompting (if there's room, it might be
  2193. shortened), and the third, optional entry is a longer explanation that
  2194. will be displayed in a help buffer if the user requests more help.
  2195. The return value is the matching value from @var{choices}.
  2196. @lisp
  2197. (read-multiple-choice
  2198. "Continue connecting?"
  2199. '((?a "always" "Accept this certificate this session and for all future sessions.")
  2200. (?s "session only" "Accept this certificate this session only.")
  2201. (?n "no" "Refuse to use this certificate, and close the connection.")))
  2202. @end lisp
  2203. The @code{read-multiple-choice-face} face is used to highlight the
  2204. matching characters in the name string on graphical terminals.
  2205. @end defun
  2206. @node Event Mod
  2207. @subsection Modifying and Translating Input Events
  2208. @cindex modifiers of events
  2209. @cindex translating input events
  2210. @cindex event translation
  2211. Emacs modifies every event it reads according to
  2212. @code{extra-keyboard-modifiers}, then translates it through
  2213. @code{keyboard-translate-table} (if applicable), before returning it
  2214. from @code{read-event}.
  2215. @defvar extra-keyboard-modifiers
  2216. This variable lets Lisp programs ``press'' the modifier keys on the
  2217. keyboard. The value is a character. Only the modifiers of the
  2218. character matter. Each time the user types a keyboard key, it is
  2219. altered as if those modifier keys were held down. For instance, if
  2220. you bind @code{extra-keyboard-modifiers} to @code{?\C-\M-a}, then all
  2221. keyboard input characters typed during the scope of the binding will
  2222. have the control and meta modifiers applied to them. The character
  2223. @code{?\C-@@}, equivalent to the integer 0, does not count as a control
  2224. character for this purpose, but as a character with no modifiers.
  2225. Thus, setting @code{extra-keyboard-modifiers} to zero cancels any
  2226. modification.
  2227. When using a window system, the program can press any of the
  2228. modifier keys in this way. Otherwise, only the @key{CTL} and @key{META}
  2229. keys can be virtually pressed.
  2230. Note that this variable applies only to events that really come from
  2231. the keyboard, and has no effect on mouse events or any other events.
  2232. @end defvar
  2233. @defvar keyboard-translate-table
  2234. This terminal-local variable is the translate table for keyboard
  2235. characters. It lets you reshuffle the keys on the keyboard without
  2236. changing any command bindings. Its value is normally a char-table, or
  2237. else @code{nil}. (It can also be a string or vector, but this is
  2238. considered obsolete.)
  2239. If @code{keyboard-translate-table} is a char-table
  2240. (@pxref{Char-Tables}), then each character read from the keyboard is
  2241. looked up in this char-table. If the value found there is
  2242. non-@code{nil}, then it is used instead of the actual input character.
  2243. Note that this translation is the first thing that happens to a
  2244. character after it is read from the terminal. Record-keeping features
  2245. such as @code{recent-keys} and dribble files record the characters after
  2246. translation.
  2247. Note also that this translation is done before the characters are
  2248. supplied to input methods (@pxref{Input Methods}). Use
  2249. @code{translation-table-for-input} (@pxref{Translation of Characters}),
  2250. if you want to translate characters after input methods operate.
  2251. @end defvar
  2252. @defun keyboard-translate from to
  2253. This function modifies @code{keyboard-translate-table} to translate
  2254. character code @var{from} into character code @var{to}. It creates
  2255. the keyboard translate table if necessary.
  2256. @end defun
  2257. Here's an example of using the @code{keyboard-translate-table} to
  2258. make @kbd{C-x}, @kbd{C-c} and @kbd{C-v} perform the cut, copy and paste
  2259. operations:
  2260. @example
  2261. (keyboard-translate ?\C-x 'control-x)
  2262. (keyboard-translate ?\C-c 'control-c)
  2263. (keyboard-translate ?\C-v 'control-v)
  2264. (global-set-key [control-x] 'kill-region)
  2265. (global-set-key [control-c] 'kill-ring-save)
  2266. (global-set-key [control-v] 'yank)
  2267. @end example
  2268. @noindent
  2269. On a graphical terminal that supports extended @acronym{ASCII} input,
  2270. you can still get the standard Emacs meanings of one of those
  2271. characters by typing it with the shift key. That makes it a different
  2272. character as far as keyboard translation is concerned, but it has the
  2273. same usual meaning.
  2274. @xref{Translation Keymaps}, for mechanisms that translate event sequences
  2275. at the level of @code{read-key-sequence}.
  2276. @node Invoking the Input Method
  2277. @subsection Invoking the Input Method
  2278. @cindex invoking input method
  2279. The event-reading functions invoke the current input method, if any
  2280. (@pxref{Input Methods}). If the value of @code{input-method-function}
  2281. is non-@code{nil}, it should be a function; when @code{read-event} reads
  2282. a printing character (including @key{SPC}) with no modifier bits, it
  2283. calls that function, passing the character as an argument.
  2284. @defvar input-method-function
  2285. If this is non-@code{nil}, its value specifies the current input method
  2286. function.
  2287. @strong{Warning:} don't bind this variable with @code{let}. It is often
  2288. buffer-local, and if you bind it around reading input (which is exactly
  2289. when you @emph{would} bind it), switching buffers asynchronously while
  2290. Emacs is waiting will cause the value to be restored in the wrong
  2291. buffer.
  2292. @end defvar
  2293. The input method function should return a list of events which should
  2294. be used as input. (If the list is @code{nil}, that means there is no
  2295. input, so @code{read-event} waits for another event.) These events are
  2296. processed before the events in @code{unread-command-events}
  2297. (@pxref{Event Input Misc}). Events
  2298. returned by the input method function are not passed to the input method
  2299. function again, even if they are printing characters with no modifier
  2300. bits.
  2301. If the input method function calls @code{read-event} or
  2302. @code{read-key-sequence}, it should bind @code{input-method-function} to
  2303. @code{nil} first, to prevent recursion.
  2304. The input method function is not called when reading the second and
  2305. subsequent events of a key sequence. Thus, these characters are not
  2306. subject to input method processing. The input method function should
  2307. test the values of @code{overriding-local-map} and
  2308. @code{overriding-terminal-local-map}; if either of these variables is
  2309. non-@code{nil}, the input method should put its argument into a list and
  2310. return that list with no further processing.
  2311. @node Quoted Character Input
  2312. @subsection Quoted Character Input
  2313. @cindex quoted character input
  2314. You can use the function @code{read-quoted-char} to ask the user to
  2315. specify a character, and allow the user to specify a control or meta
  2316. character conveniently, either literally or as an octal character code.
  2317. The command @code{quoted-insert} uses this function.
  2318. @defun read-quoted-char &optional prompt
  2319. @cindex octal character input
  2320. @cindex control characters, reading
  2321. @cindex nonprinting characters, reading
  2322. This function is like @code{read-char}, except that if the first
  2323. character read is an octal digit (0--7), it reads any number of octal
  2324. digits (but stopping if a non-octal digit is found), and returns the
  2325. character represented by that numeric character code. If the
  2326. character that terminates the sequence of octal digits is @key{RET},
  2327. it is discarded. Any other terminating character is used as input
  2328. after this function returns.
  2329. Quitting is suppressed when the first character is read, so that the
  2330. user can enter a @kbd{C-g}. @xref{Quitting}.
  2331. If @var{prompt} is supplied, it specifies a string for prompting the
  2332. user. The prompt string is always displayed in the echo area, followed
  2333. by a single @samp{-}.
  2334. In the following example, the user types in the octal number 177 (which
  2335. is 127 in decimal).
  2336. @example
  2337. (read-quoted-char "What character")
  2338. @group
  2339. ---------- Echo Area ----------
  2340. What character @kbd{1 7 7}-
  2341. ---------- Echo Area ----------
  2342. @result{} 127
  2343. @end group
  2344. @end example
  2345. @end defun
  2346. @need 2000
  2347. @node Event Input Misc
  2348. @subsection Miscellaneous Event Input Features
  2349. This section describes how to peek ahead at events without using
  2350. them up, how to check for pending input, and how to discard pending
  2351. input. See also the function @code{read-passwd} (@pxref{Reading a
  2352. Password}).
  2353. @defvar unread-command-events
  2354. @cindex next input
  2355. @cindex peeking at input
  2356. This variable holds a list of events waiting to be read as command
  2357. input. The events are used in the order they appear in the list, and
  2358. removed one by one as they are used.
  2359. The variable is needed because in some cases a function reads an event
  2360. and then decides not to use it. Storing the event in this variable
  2361. causes it to be processed normally, by the command loop or by the
  2362. functions to read command input.
  2363. @cindex prefix argument unreading
  2364. For example, the function that implements numeric prefix arguments reads
  2365. any number of digits. When it finds a non-digit event, it must unread
  2366. the event so that it can be read normally by the command loop.
  2367. Likewise, incremental search uses this feature to unread events with no
  2368. special meaning in a search, because these events should exit the search
  2369. and then execute normally.
  2370. The reliable and easy way to extract events from a key sequence so as
  2371. to put them in @code{unread-command-events} is to use
  2372. @code{listify-key-sequence} (see below).
  2373. Normally you add events to the front of this list, so that the events
  2374. most recently unread will be reread first.
  2375. Events read from this list are not normally added to the current
  2376. command's key sequence (as returned by, e.g., @code{this-command-keys}),
  2377. as the events will already have been added once as they were read for
  2378. the first time. An element of the form @w{@code{(t . @var{event})}}
  2379. forces @var{event} to be added to the current command's key sequence.
  2380. @end defvar
  2381. @defun listify-key-sequence key
  2382. This function converts the string or vector @var{key} to a list of
  2383. individual events, which you can put in @code{unread-command-events}.
  2384. @end defun
  2385. @defun input-pending-p &optional check-timers
  2386. @cindex waiting for command key input
  2387. This function determines whether any command input is currently
  2388. available to be read. It returns immediately, with value @code{t} if
  2389. there is available input, @code{nil} otherwise. On rare occasions it
  2390. may return @code{t} when no input is available.
  2391. If the optional argument @var{check-timers} is non-@code{nil}, then if
  2392. no input is available, Emacs runs any timers which are ready.
  2393. @xref{Timers}.
  2394. @end defun
  2395. @defvar last-input-event
  2396. This variable records the last terminal input event read, whether
  2397. as part of a command or explicitly by a Lisp program.
  2398. In the example below, the Lisp program reads the character @kbd{1},
  2399. @acronym{ASCII} code 49. It becomes the value of @code{last-input-event},
  2400. while @kbd{C-e} (we assume @kbd{C-x C-e} command is used to evaluate
  2401. this expression) remains the value of @code{last-command-event}.
  2402. @example
  2403. @group
  2404. (progn (print (read-char))
  2405. (print last-command-event)
  2406. last-input-event)
  2407. @print{} 49
  2408. @print{} 5
  2409. @result{} 49
  2410. @end group
  2411. @end example
  2412. @end defvar
  2413. @defmac while-no-input body@dots{}
  2414. This construct runs the @var{body} forms and returns the value of the
  2415. last one---but only if no input arrives. If any input arrives during
  2416. the execution of the @var{body} forms, it aborts them (working much
  2417. like a quit). The @code{while-no-input} form returns @code{nil} if
  2418. aborted by a real quit, and returns @code{t} if aborted by arrival of
  2419. other input.
  2420. If a part of @var{body} binds @code{inhibit-quit} to non-@code{nil},
  2421. arrival of input during those parts won't cause an abort until
  2422. the end of that part.
  2423. If you want to be able to distinguish all possible values computed
  2424. by @var{body} from both kinds of abort conditions, write the code
  2425. like this:
  2426. @example
  2427. (while-no-input
  2428. (list
  2429. (progn . @var{body})))
  2430. @end example
  2431. @end defmac
  2432. @defun discard-input
  2433. @cindex flushing input
  2434. @cindex discarding input
  2435. @cindex keyboard macro, terminating
  2436. This function discards the contents of the terminal input buffer and
  2437. cancels any keyboard macro that might be in the process of definition.
  2438. It returns @code{nil}.
  2439. In the following example, the user may type a number of characters right
  2440. after starting the evaluation of the form. After the @code{sleep-for}
  2441. finishes sleeping, @code{discard-input} discards any characters typed
  2442. during the sleep.
  2443. @example
  2444. (progn (sleep-for 2)
  2445. (discard-input))
  2446. @result{} nil
  2447. @end example
  2448. @end defun
  2449. @node Special Events
  2450. @section Special Events
  2451. @cindex special events
  2452. Certain @dfn{special events} are handled at a very low level---as soon
  2453. as they are read. The @code{read-event} function processes these
  2454. events itself, and never returns them. Instead, it keeps waiting for
  2455. the first event that is not special and returns that one.
  2456. Special events do not echo, they are never grouped into key
  2457. sequences, and they never appear in the value of
  2458. @code{last-command-event} or @code{(this-command-keys)}. They do not
  2459. discard a numeric argument, they cannot be unread with
  2460. @code{unread-command-events}, they may not appear in a keyboard macro,
  2461. and they are not recorded in a keyboard macro while you are defining
  2462. one.
  2463. Special events do, however, appear in @code{last-input-event}
  2464. immediately after they are read, and this is the way for the event's
  2465. definition to find the actual event.
  2466. The events types @code{iconify-frame}, @code{make-frame-visible},
  2467. @code{delete-frame}, @code{drag-n-drop}, @code{language-change}, and
  2468. user signals like @code{sigusr1} are normally handled in this way.
  2469. The keymap which defines how to handle special events---and which
  2470. events are special---is in the variable @code{special-event-map}
  2471. (@pxref{Active Keymaps}).
  2472. @node Waiting
  2473. @section Waiting for Elapsed Time or Input
  2474. @cindex waiting
  2475. The wait functions are designed to wait for a certain amount of time
  2476. to pass or until there is input. For example, you may wish to pause in
  2477. the middle of a computation to allow the user time to view the display.
  2478. @code{sit-for} pauses and updates the screen, and returns immediately if
  2479. input comes in, while @code{sleep-for} pauses without updating the
  2480. screen.
  2481. @defun sit-for seconds &optional nodisp
  2482. This function performs redisplay (provided there is no pending input
  2483. from the user), then waits @var{seconds} seconds, or until input is
  2484. available. The usual purpose of @code{sit-for} is to give the user
  2485. time to read text that you display. The value is @code{t} if
  2486. @code{sit-for} waited the full time with no input arriving
  2487. (@pxref{Event Input Misc}). Otherwise, the value is @code{nil}.
  2488. The argument @var{seconds} need not be an integer. If it is floating
  2489. point, @code{sit-for} waits for a fractional number of seconds.
  2490. Some systems support only a whole number of seconds; on these systems,
  2491. @var{seconds} is rounded down.
  2492. The expression @code{(sit-for 0)} is equivalent to @code{(redisplay)},
  2493. i.e., it requests a redisplay, without any delay, if there is no pending input.
  2494. @xref{Forcing Redisplay}.
  2495. If @var{nodisp} is non-@code{nil}, then @code{sit-for} does not
  2496. redisplay, but it still returns as soon as input is available (or when
  2497. the timeout elapses).
  2498. In batch mode (@pxref{Batch Mode}), @code{sit-for} cannot be
  2499. interrupted, even by input from the standard input descriptor. It is
  2500. thus equivalent to @code{sleep-for}, which is described below.
  2501. It is also possible to call @code{sit-for} with three arguments,
  2502. as @code{(sit-for @var{seconds} @var{millisec} @var{nodisp})},
  2503. but that is considered obsolete.
  2504. @end defun
  2505. @defun sleep-for seconds &optional millisec
  2506. This function simply pauses for @var{seconds} seconds without updating
  2507. the display. It pays no attention to available input. It returns
  2508. @code{nil}.
  2509. The argument @var{seconds} need not be an integer. If it is floating
  2510. point, @code{sleep-for} waits for a fractional number of seconds.
  2511. Some systems support only a whole number of seconds; on these systems,
  2512. @var{seconds} is rounded down.
  2513. The optional argument @var{millisec} specifies an additional waiting
  2514. period measured in milliseconds. This adds to the period specified by
  2515. @var{seconds}. If the system doesn't support waiting fractions of a
  2516. second, you get an error if you specify nonzero @var{millisec}.
  2517. Use @code{sleep-for} when you wish to guarantee a delay.
  2518. @end defun
  2519. @xref{Time of Day}, for functions to get the current time.
  2520. @node Quitting
  2521. @section Quitting
  2522. @cindex @kbd{C-g}
  2523. @cindex quitting
  2524. @cindex interrupt Lisp functions
  2525. Typing @kbd{C-g} while a Lisp function is running causes Emacs to
  2526. @dfn{quit} whatever it is doing. This means that control returns to the
  2527. innermost active command loop.
  2528. Typing @kbd{C-g} while the command loop is waiting for keyboard input
  2529. does not cause a quit; it acts as an ordinary input character. In the
  2530. simplest case, you cannot tell the difference, because @kbd{C-g}
  2531. normally runs the command @code{keyboard-quit}, whose effect is to quit.
  2532. However, when @kbd{C-g} follows a prefix key, they combine to form an
  2533. undefined key. The effect is to cancel the prefix key as well as any
  2534. prefix argument.
  2535. In the minibuffer, @kbd{C-g} has a different definition: it aborts out
  2536. of the minibuffer. This means, in effect, that it exits the minibuffer
  2537. and then quits. (Simply quitting would return to the command loop
  2538. @emph{within} the minibuffer.) The reason why @kbd{C-g} does not quit
  2539. directly when the command reader is reading input is so that its meaning
  2540. can be redefined in the minibuffer in this way. @kbd{C-g} following a
  2541. prefix key is not redefined in the minibuffer, and it has its normal
  2542. effect of canceling the prefix key and prefix argument. This too
  2543. would not be possible if @kbd{C-g} always quit directly.
  2544. When @kbd{C-g} does directly quit, it does so by setting the variable
  2545. @code{quit-flag} to @code{t}. Emacs checks this variable at appropriate
  2546. times and quits if it is not @code{nil}. Setting @code{quit-flag}
  2547. non-@code{nil} in any way thus causes a quit.
  2548. At the level of C code, quitting cannot happen just anywhere; only at the
  2549. special places that check @code{quit-flag}. The reason for this is
  2550. that quitting at other places might leave an inconsistency in Emacs's
  2551. internal state. Because quitting is delayed until a safe place, quitting
  2552. cannot make Emacs crash.
  2553. Certain functions such as @code{read-key-sequence} or
  2554. @code{read-quoted-char} prevent quitting entirely even though they wait
  2555. for input. Instead of quitting, @kbd{C-g} serves as the requested
  2556. input. In the case of @code{read-key-sequence}, this serves to bring
  2557. about the special behavior of @kbd{C-g} in the command loop. In the
  2558. case of @code{read-quoted-char}, this is so that @kbd{C-q} can be used
  2559. to quote a @kbd{C-g}.
  2560. @cindex preventing quitting
  2561. You can prevent quitting for a portion of a Lisp function by binding
  2562. the variable @code{inhibit-quit} to a non-@code{nil} value. Then,
  2563. although @kbd{C-g} still sets @code{quit-flag} to @code{t} as usual, the
  2564. usual result of this---a quit---is prevented. Eventually,
  2565. @code{inhibit-quit} will become @code{nil} again, such as when its
  2566. binding is unwound at the end of a @code{let} form. At that time, if
  2567. @code{quit-flag} is still non-@code{nil}, the requested quit happens
  2568. immediately. This behavior is ideal when you wish to make sure that
  2569. quitting does not happen within a critical section of the program.
  2570. @cindex @code{read-quoted-char} quitting
  2571. In some functions (such as @code{read-quoted-char}), @kbd{C-g} is
  2572. handled in a special way that does not involve quitting. This is done
  2573. by reading the input with @code{inhibit-quit} bound to @code{t}, and
  2574. setting @code{quit-flag} to @code{nil} before @code{inhibit-quit}
  2575. becomes @code{nil} again. This excerpt from the definition of
  2576. @code{read-quoted-char} shows how this is done; it also shows that
  2577. normal quitting is permitted after the first character of input.
  2578. @example
  2579. (defun read-quoted-char (&optional prompt)
  2580. "@dots{}@var{documentation}@dots{}"
  2581. (let ((message-log-max nil) done (first t) (code 0) char)
  2582. (while (not done)
  2583. (let ((inhibit-quit first)
  2584. @dots{})
  2585. (and prompt (message "%s-" prompt))
  2586. (setq char (read-event))
  2587. (if inhibit-quit (setq quit-flag nil)))
  2588. @r{@dots{}set the variable @code{code}@dots{}})
  2589. code))
  2590. @end example
  2591. @defvar quit-flag
  2592. If this variable is non-@code{nil}, then Emacs quits immediately, unless
  2593. @code{inhibit-quit} is non-@code{nil}. Typing @kbd{C-g} ordinarily sets
  2594. @code{quit-flag} non-@code{nil}, regardless of @code{inhibit-quit}.
  2595. @end defvar
  2596. @defvar inhibit-quit
  2597. This variable determines whether Emacs should quit when @code{quit-flag}
  2598. is set to a value other than @code{nil}. If @code{inhibit-quit} is
  2599. non-@code{nil}, then @code{quit-flag} has no special effect.
  2600. @end defvar
  2601. @defmac with-local-quit body@dots{}
  2602. This macro executes @var{body} forms in sequence, but allows quitting, at
  2603. least locally, within @var{body} even if @code{inhibit-quit} was
  2604. non-@code{nil} outside this construct. It returns the value of the
  2605. last form in @var{body}, unless exited by quitting, in which case
  2606. it returns @code{nil}.
  2607. If @code{inhibit-quit} is @code{nil} on entry to @code{with-local-quit},
  2608. it only executes the @var{body}, and setting @code{quit-flag} causes
  2609. a normal quit. However, if @code{inhibit-quit} is non-@code{nil} so
  2610. that ordinary quitting is delayed, a non-@code{nil} @code{quit-flag}
  2611. triggers a special kind of local quit. This ends the execution of
  2612. @var{body} and exits the @code{with-local-quit} body with
  2613. @code{quit-flag} still non-@code{nil}, so that another (ordinary) quit
  2614. will happen as soon as that is allowed. If @code{quit-flag} is
  2615. already non-@code{nil} at the beginning of @var{body}, the local quit
  2616. happens immediately and the body doesn't execute at all.
  2617. This macro is mainly useful in functions that can be called from
  2618. timers, process filters, process sentinels, @code{pre-command-hook},
  2619. @code{post-command-hook}, and other places where @code{inhibit-quit} is
  2620. normally bound to @code{t}.
  2621. @end defmac
  2622. @deffn Command keyboard-quit
  2623. This function signals the @code{quit} condition with @code{(signal 'quit
  2624. nil)}. This is the same thing that quitting does. (See @code{signal}
  2625. in @ref{Errors}.)
  2626. @end deffn
  2627. You can specify a character other than @kbd{C-g} to use for quitting.
  2628. See the function @code{set-input-mode} in @ref{Input Modes}.
  2629. @node Prefix Command Arguments
  2630. @section Prefix Command Arguments
  2631. @cindex prefix argument
  2632. @cindex raw prefix argument
  2633. @cindex numeric prefix argument
  2634. Most Emacs commands can use a @dfn{prefix argument}, a number
  2635. specified before the command itself. (Don't confuse prefix arguments
  2636. with prefix keys.) The prefix argument is at all times represented by a
  2637. value, which may be @code{nil}, meaning there is currently no prefix
  2638. argument. Each command may use the prefix argument or ignore it.
  2639. There are two representations of the prefix argument: @dfn{raw} and
  2640. @dfn{numeric}. The editor command loop uses the raw representation
  2641. internally, and so do the Lisp variables that store the information, but
  2642. commands can request either representation.
  2643. Here are the possible values of a raw prefix argument:
  2644. @itemize @bullet
  2645. @item
  2646. @code{nil}, meaning there is no prefix argument. Its numeric value is
  2647. 1, but numerous commands make a distinction between @code{nil} and the
  2648. integer 1.
  2649. @item
  2650. An integer, which stands for itself.
  2651. @item
  2652. A list of one element, which is an integer. This form of prefix
  2653. argument results from one or a succession of @kbd{C-u}s with no
  2654. digits. The numeric value is the integer in the list, but some
  2655. commands make a distinction between such a list and an integer alone.
  2656. @item
  2657. The symbol @code{-}. This indicates that @kbd{M--} or @kbd{C-u -} was
  2658. typed, without following digits. The equivalent numeric value is
  2659. @minus{}1, but some commands make a distinction between the integer
  2660. @minus{}1 and the symbol @code{-}.
  2661. @end itemize
  2662. We illustrate these possibilities by calling the following function with
  2663. various prefixes:
  2664. @example
  2665. @group
  2666. (defun display-prefix (arg)
  2667. "Display the value of the raw prefix arg."
  2668. (interactive "P")
  2669. (message "%s" arg))
  2670. @end group
  2671. @end example
  2672. @noindent
  2673. Here are the results of calling @code{display-prefix} with various
  2674. raw prefix arguments:
  2675. @example
  2676. M-x display-prefix @print{} nil
  2677. C-u M-x display-prefix @print{} (4)
  2678. C-u C-u M-x display-prefix @print{} (16)
  2679. C-u 3 M-x display-prefix @print{} 3
  2680. M-3 M-x display-prefix @print{} 3 ; @r{(Same as @code{C-u 3}.)}
  2681. C-u - M-x display-prefix @print{} -
  2682. M-- M-x display-prefix @print{} - ; @r{(Same as @code{C-u -}.)}
  2683. C-u - 7 M-x display-prefix @print{} -7
  2684. M-- 7 M-x display-prefix @print{} -7 ; @r{(Same as @code{C-u -7}.)}
  2685. @end example
  2686. Emacs uses two variables to store the prefix argument:
  2687. @code{prefix-arg} and @code{current-prefix-arg}. Commands such as
  2688. @code{universal-argument} that set up prefix arguments for other
  2689. commands store them in @code{prefix-arg}. In contrast,
  2690. @code{current-prefix-arg} conveys the prefix argument to the current
  2691. command, so setting it has no effect on the prefix arguments for future
  2692. commands.
  2693. Normally, commands specify which representation to use for the prefix
  2694. argument, either numeric or raw, in the @code{interactive} specification.
  2695. (@xref{Using Interactive}.) Alternatively, functions may look at the
  2696. value of the prefix argument directly in the variable
  2697. @code{current-prefix-arg}, but this is less clean.
  2698. @defun prefix-numeric-value arg
  2699. This function returns the numeric meaning of a valid raw prefix argument
  2700. value, @var{arg}. The argument may be a symbol, a number, or a list.
  2701. If it is @code{nil}, the value 1 is returned; if it is @code{-}, the
  2702. value @minus{}1 is returned; if it is a number, that number is returned;
  2703. if it is a list, the @sc{car} of that list (which should be a number) is
  2704. returned.
  2705. @end defun
  2706. @defvar current-prefix-arg
  2707. This variable holds the raw prefix argument for the @emph{current}
  2708. command. Commands may examine it directly, but the usual method for
  2709. accessing it is with @code{(interactive "P")}.
  2710. @end defvar
  2711. @defvar prefix-arg
  2712. The value of this variable is the raw prefix argument for the
  2713. @emph{next} editing command. Commands such as @code{universal-argument}
  2714. that specify prefix arguments for the following command work by setting
  2715. this variable.
  2716. @end defvar
  2717. @defvar last-prefix-arg
  2718. The raw prefix argument value used by the previous command.
  2719. @end defvar
  2720. The following commands exist to set up prefix arguments for the
  2721. following command. Do not call them for any other reason.
  2722. @deffn Command universal-argument
  2723. This command reads input and specifies a prefix argument for the
  2724. following command. Don't call this command yourself unless you know
  2725. what you are doing.
  2726. @end deffn
  2727. @deffn Command digit-argument arg
  2728. This command adds to the prefix argument for the following command. The
  2729. argument @var{arg} is the raw prefix argument as it was before this
  2730. command; it is used to compute the updated prefix argument. Don't call
  2731. this command yourself unless you know what you are doing.
  2732. @end deffn
  2733. @deffn Command negative-argument arg
  2734. This command adds to the numeric argument for the next command. The
  2735. argument @var{arg} is the raw prefix argument as it was before this
  2736. command; its value is negated to form the new prefix argument. Don't
  2737. call this command yourself unless you know what you are doing.
  2738. @end deffn
  2739. @node Recursive Editing
  2740. @section Recursive Editing
  2741. @cindex recursive command loop
  2742. @cindex recursive editing level
  2743. @cindex command loop, recursive
  2744. The Emacs command loop is entered automatically when Emacs starts up.
  2745. This top-level invocation of the command loop never exits; it keeps
  2746. running as long as Emacs does. Lisp programs can also invoke the
  2747. command loop. Since this makes more than one activation of the command
  2748. loop, we call it @dfn{recursive editing}. A recursive editing level has
  2749. the effect of suspending whatever command invoked it and permitting the
  2750. user to do arbitrary editing before resuming that command.
  2751. The commands available during recursive editing are the same ones
  2752. available in the top-level editing loop and defined in the keymaps.
  2753. Only a few special commands exit the recursive editing level; the others
  2754. return to the recursive editing level when they finish. (The special
  2755. commands for exiting are always available, but they do nothing when
  2756. recursive editing is not in progress.)
  2757. All command loops, including recursive ones, set up all-purpose error
  2758. handlers so that an error in a command run from the command loop will
  2759. not exit the loop.
  2760. @cindex minibuffer input
  2761. Minibuffer input is a special kind of recursive editing. It has a few
  2762. special wrinkles, such as enabling display of the minibuffer and the
  2763. minibuffer window, but fewer than you might suppose. Certain keys
  2764. behave differently in the minibuffer, but that is only because of the
  2765. minibuffer's local map; if you switch windows, you get the usual Emacs
  2766. commands.
  2767. @cindex @code{throw} example
  2768. @kindex exit
  2769. @cindex exit recursive editing
  2770. @cindex aborting
  2771. To invoke a recursive editing level, call the function
  2772. @code{recursive-edit}. This function contains the command loop; it also
  2773. contains a call to @code{catch} with tag @code{exit}, which makes it
  2774. possible to exit the recursive editing level by throwing to @code{exit}
  2775. (@pxref{Catch and Throw}). If you throw a value other than @code{t},
  2776. then @code{recursive-edit} returns normally to the function that called
  2777. it. The command @kbd{C-M-c} (@code{exit-recursive-edit}) does this.
  2778. Throwing a @code{t} value causes @code{recursive-edit} to quit, so that
  2779. control returns to the command loop one level up. This is called
  2780. @dfn{aborting}, and is done by @kbd{C-]} (@code{abort-recursive-edit}).
  2781. Most applications should not use recursive editing, except as part of
  2782. using the minibuffer. Usually it is more convenient for the user if you
  2783. change the major mode of the current buffer temporarily to a special
  2784. major mode, which should have a command to go back to the previous mode.
  2785. (The @kbd{e} command in Rmail uses this technique.) Or, if you wish to
  2786. give the user different text to edit recursively, create and select
  2787. a new buffer in a special mode. In this mode, define a command to
  2788. complete the processing and go back to the previous buffer. (The
  2789. @kbd{m} command in Rmail does this.)
  2790. Recursive edits are useful in debugging. You can insert a call to
  2791. @code{debug} into a function definition as a sort of breakpoint, so that
  2792. you can look around when the function gets there. @code{debug} invokes
  2793. a recursive edit but also provides the other features of the debugger.
  2794. Recursive editing levels are also used when you type @kbd{C-r} in
  2795. @code{query-replace} or use @kbd{C-x q} (@code{kbd-macro-query}).
  2796. @deffn Command recursive-edit
  2797. @cindex suspend evaluation
  2798. This function invokes the editor command loop. It is called
  2799. automatically by the initialization of Emacs, to let the user begin
  2800. editing. When called from a Lisp program, it enters a recursive editing
  2801. level.
  2802. If the current buffer is not the same as the selected window's buffer,
  2803. @code{recursive-edit} saves and restores the current buffer. Otherwise,
  2804. if you switch buffers, the buffer you switched to is current after
  2805. @code{recursive-edit} returns.
  2806. In the following example, the function @code{simple-rec} first
  2807. advances point one word, then enters a recursive edit, printing out a
  2808. message in the echo area. The user can then do any editing desired, and
  2809. then type @kbd{C-M-c} to exit and continue executing @code{simple-rec}.
  2810. @example
  2811. (defun simple-rec ()
  2812. (forward-word 1)
  2813. (message "Recursive edit in progress")
  2814. (recursive-edit)
  2815. (forward-word 1))
  2816. @result{} simple-rec
  2817. (simple-rec)
  2818. @result{} nil
  2819. @end example
  2820. @end deffn
  2821. @deffn Command exit-recursive-edit
  2822. This function exits from the innermost recursive edit (including
  2823. minibuffer input). Its definition is effectively @code{(throw 'exit
  2824. nil)}.
  2825. @end deffn
  2826. @deffn Command abort-recursive-edit
  2827. This function aborts the command that requested the innermost recursive
  2828. edit (including minibuffer input), by signaling @code{quit}
  2829. after exiting the recursive edit. Its definition is effectively
  2830. @code{(throw 'exit t)}. @xref{Quitting}.
  2831. @end deffn
  2832. @deffn Command top-level
  2833. This function exits all recursive editing levels; it does not return a
  2834. value, as it jumps completely out of any computation directly back to
  2835. the main command loop.
  2836. @end deffn
  2837. @defun recursion-depth
  2838. This function returns the current depth of recursive edits. When no
  2839. recursive edit is active, it returns 0.
  2840. @end defun
  2841. @node Disabling Commands
  2842. @section Disabling Commands
  2843. @cindex disabled command
  2844. @dfn{Disabling a command} marks the command as requiring user
  2845. confirmation before it can be executed. Disabling is used for commands
  2846. which might be confusing to beginning users, to prevent them from using
  2847. the commands by accident.
  2848. @kindex disabled
  2849. The low-level mechanism for disabling a command is to put a
  2850. non-@code{nil} @code{disabled} property on the Lisp symbol for the
  2851. command. These properties are normally set up by the user's
  2852. init file (@pxref{Init File}) with Lisp expressions such as this:
  2853. @example
  2854. (put 'upcase-region 'disabled t)
  2855. @end example
  2856. @noindent
  2857. For a few commands, these properties are present by default (you can
  2858. remove them in your init file if you wish).
  2859. If the value of the @code{disabled} property is a string, the message
  2860. saying the command is disabled includes that string. For example:
  2861. @example
  2862. (put 'delete-region 'disabled
  2863. "Text deleted this way cannot be yanked back!\n")
  2864. @end example
  2865. @xref{Disabling,,, emacs, The GNU Emacs Manual}, for the details on
  2866. what happens when a disabled command is invoked interactively.
  2867. Disabling a command has no effect on calling it as a function from Lisp
  2868. programs.
  2869. @deffn Command enable-command command
  2870. Allow @var{command} (a symbol) to be executed without special
  2871. confirmation from now on, and alter the user's init file (@pxref{Init
  2872. File}) so that this will apply to future sessions.
  2873. @end deffn
  2874. @deffn Command disable-command command
  2875. Require special confirmation to execute @var{command} from now on, and
  2876. alter the user's init file so that this will apply to future sessions.
  2877. @end deffn
  2878. @defvar disabled-command-function
  2879. The value of this variable should be a function. When the user
  2880. invokes a disabled command interactively, this function is called
  2881. instead of the disabled command. It can use @code{this-command-keys}
  2882. to determine what the user typed to run the command, and thus find the
  2883. command itself.
  2884. The value may also be @code{nil}. Then all commands work normally,
  2885. even disabled ones.
  2886. By default, the value is a function that asks the user whether to
  2887. proceed.
  2888. @end defvar
  2889. @node Command History
  2890. @section Command History
  2891. @cindex command history
  2892. @cindex complex command
  2893. @cindex history of commands
  2894. The command loop keeps a history of the complex commands that have
  2895. been executed, to make it convenient to repeat these commands. A
  2896. @dfn{complex command} is one for which the interactive argument reading
  2897. uses the minibuffer. This includes any @kbd{M-x} command, any
  2898. @kbd{M-:} command, and any command whose @code{interactive}
  2899. specification reads an argument from the minibuffer. Explicit use of
  2900. the minibuffer during the execution of the command itself does not cause
  2901. the command to be considered complex.
  2902. @defvar command-history
  2903. This variable's value is a list of recent complex commands, each
  2904. represented as a form to evaluate. It continues to accumulate all
  2905. complex commands for the duration of the editing session, but when it
  2906. reaches the maximum size (@pxref{Minibuffer History}), the oldest
  2907. elements are deleted as new ones are added.
  2908. @example
  2909. @group
  2910. command-history
  2911. @result{} ((switch-to-buffer "chistory.texi")
  2912. (describe-key "^X^[")
  2913. (visit-tags-table "~/emacs/src/")
  2914. (find-tag "repeat-complex-command"))
  2915. @end group
  2916. @end example
  2917. @end defvar
  2918. This history list is actually a special case of minibuffer history
  2919. (@pxref{Minibuffer History}), with one special twist: the elements are
  2920. expressions rather than strings.
  2921. There are a number of commands devoted to the editing and recall of
  2922. previous commands. The commands @code{repeat-complex-command}, and
  2923. @code{list-command-history} are described in the user manual
  2924. (@pxref{Repetition,,, emacs, The GNU Emacs Manual}). Within the
  2925. minibuffer, the usual minibuffer history commands are available.
  2926. @node Keyboard Macros
  2927. @section Keyboard Macros
  2928. @cindex keyboard macros
  2929. A @dfn{keyboard macro} is a canned sequence of input events that can
  2930. be considered a command and made the definition of a key. The Lisp
  2931. representation of a keyboard macro is a string or vector containing the
  2932. events. Don't confuse keyboard macros with Lisp macros
  2933. (@pxref{Macros}).
  2934. @defun execute-kbd-macro kbdmacro &optional count loopfunc
  2935. This function executes @var{kbdmacro} as a sequence of events. If
  2936. @var{kbdmacro} is a string or vector, then the events in it are executed
  2937. exactly as if they had been input by the user. The sequence is
  2938. @emph{not} expected to be a single key sequence; normally a keyboard
  2939. macro definition consists of several key sequences concatenated.
  2940. If @var{kbdmacro} is a symbol, then its function definition is used in
  2941. place of @var{kbdmacro}. If that is another symbol, this process repeats.
  2942. Eventually the result should be a string or vector. If the result is
  2943. not a symbol, string, or vector, an error is signaled.
  2944. The argument @var{count} is a repeat count; @var{kbdmacro} is executed that
  2945. many times. If @var{count} is omitted or @code{nil}, @var{kbdmacro} is
  2946. executed once. If it is 0, @var{kbdmacro} is executed over and over until it
  2947. encounters an error or a failing search.
  2948. If @var{loopfunc} is non-@code{nil}, it is a function that is called,
  2949. without arguments, prior to each iteration of the macro. If
  2950. @var{loopfunc} returns @code{nil}, then this stops execution of the macro.
  2951. @xref{Reading One Event}, for an example of using @code{execute-kbd-macro}.
  2952. @end defun
  2953. @defvar executing-kbd-macro
  2954. This variable contains the string or vector that defines the keyboard
  2955. macro that is currently executing. It is @code{nil} if no macro is
  2956. currently executing. A command can test this variable so as to behave
  2957. differently when run from an executing macro. Do not set this variable
  2958. yourself.
  2959. @end defvar
  2960. @defvar defining-kbd-macro
  2961. This variable is non-@code{nil} if and only if a keyboard macro is
  2962. being defined. A command can test this variable so as to behave
  2963. differently while a macro is being defined. The value is
  2964. @code{append} while appending to the definition of an existing macro.
  2965. The commands @code{start-kbd-macro}, @code{kmacro-start-macro} and
  2966. @code{end-kbd-macro} set this variable---do not set it yourself.
  2967. The variable is always local to the current terminal and cannot be
  2968. buffer-local. @xref{Multiple Terminals}.
  2969. @end defvar
  2970. @defvar last-kbd-macro
  2971. This variable is the definition of the most recently defined keyboard
  2972. macro. Its value is a string or vector, or @code{nil}.
  2973. The variable is always local to the current terminal and cannot be
  2974. buffer-local. @xref{Multiple Terminals}.
  2975. @end defvar
  2976. @defvar kbd-macro-termination-hook
  2977. This normal hook is run when a keyboard macro terminates, regardless
  2978. of what caused it to terminate (reaching the macro end or an error
  2979. which ended the macro prematurely).
  2980. @end defvar