os.texi 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990-1995, 1998-1999, 2001-2015 Free Software
  4. @c Foundation, Inc.
  5. @c See the file elisp.texi for copying conditions.
  6. @node System Interface
  7. @chapter Operating System Interface
  8. This chapter is about starting and getting out of Emacs, access to
  9. values in the operating system environment, and terminal input, output.
  10. @xref{Building Emacs}, for related information. @xref{Display}, for
  11. additional operating system status information pertaining to the
  12. terminal and the screen.
  13. @menu
  14. * Starting Up:: Customizing Emacs startup processing.
  15. * Getting Out:: How exiting works (permanent or temporary).
  16. * System Environment:: Distinguish the name and kind of system.
  17. * User Identification:: Finding the name and user id of the user.
  18. * Time of Day:: Getting the current time.
  19. * Time Conversion:: Converting a time from numeric form to
  20. calendrical data and vice versa.
  21. * Time Parsing:: Converting a time from numeric form to text
  22. and vice versa.
  23. * Processor Run Time:: Getting the run time used by Emacs.
  24. * Time Calculations:: Adding, subtracting, comparing times, etc.
  25. * Timers:: Setting a timer to call a function at a certain time.
  26. * Idle Timers:: Setting a timer to call a function when Emacs has
  27. been idle for a certain length of time.
  28. * Terminal Input:: Accessing and recording terminal input.
  29. * Terminal Output:: Controlling and recording terminal output.
  30. * Sound Output:: Playing sounds on the computer's speaker.
  31. * X11 Keysyms:: Operating on key symbols for X Windows.
  32. * Batch Mode:: Running Emacs without terminal interaction.
  33. * Session Management:: Saving and restoring state with X Session Management.
  34. * Desktop Notifications:: Desktop notifications.
  35. * File Notifications:: File notifications.
  36. * Dynamic Libraries:: On-demand loading of support libraries.
  37. @end menu
  38. @node Starting Up
  39. @section Starting Up Emacs
  40. This section describes what Emacs does when it is started, and how you
  41. can customize these actions.
  42. @menu
  43. * Startup Summary:: Sequence of actions Emacs performs at startup.
  44. * Init File:: Details on reading the init file.
  45. * Terminal-Specific:: How the terminal-specific Lisp file is read.
  46. * Command-Line Arguments:: How command-line arguments are processed,
  47. and how you can customize them.
  48. @end menu
  49. @node Startup Summary
  50. @subsection Summary: Sequence of Actions at Startup
  51. @cindex initialization of Emacs
  52. @cindex startup of Emacs
  53. @cindex @file{startup.el}
  54. When Emacs is started up, it performs the following operations
  55. (see @code{normal-top-level} in @file{startup.el}):
  56. @enumerate
  57. @item
  58. It adds subdirectories to @code{load-path}, by running the file named
  59. @file{subdirs.el} in each directory in the list. Normally, this file
  60. adds the directory's subdirectories to the list, and those are scanned
  61. in their turn. The files @file{subdirs.el} are normally generated
  62. automatically when Emacs is installed.
  63. @item
  64. It loads any @file{leim-list.el} that it finds in the @code{load-path}
  65. directories. This file is intended for registering input methods.
  66. The search is only for any personal @file{leim-list.el} files that you
  67. may have created; it skips the directories containing the standard Emacs
  68. libraries (these should contain only a single @file{leim-list.el} file,
  69. which is compiled into the Emacs executable).
  70. @vindex before-init-time
  71. @item
  72. It sets the variable @code{before-init-time} to the value of
  73. @code{current-time} (@pxref{Time of Day}). It also sets
  74. @code{after-init-time} to @code{nil}, which signals to Lisp programs
  75. that Emacs is being initialized.
  76. @c set-locale-environment
  77. @item
  78. It sets the language environment and the terminal coding system,
  79. if requested by environment variables such as @env{LANG}.
  80. @item
  81. It does some basic parsing of the command-line arguments.
  82. @vindex initial-window-system@r{, and startup}
  83. @vindex window-system-initialization-alist
  84. @item
  85. If not running in batch mode, it initializes the window system that
  86. the variable @code{initial-window-system} specifies (@pxref{Window
  87. Systems, initial-window-system}). The initialization function for
  88. each supported window system is specified by
  89. @code{window-system-initialization-alist}. If the value
  90. of @code{initial-window-system} is @var{windowsystem}, then the
  91. appropriate initialization function is defined in the file
  92. @file{term/@var{windowsystem}-win.el}. This file should have been
  93. compiled into the Emacs executable when it was built.
  94. @item
  95. It runs the normal hook @code{before-init-hook}.
  96. @item
  97. If appropriate, it creates a graphical frame. This is not done if the
  98. options @samp{--batch} or @samp{--daemon} were specified.
  99. @item
  100. It initializes the initial frame's faces, and sets up the menu bar
  101. and tool bar if needed. If graphical frames are supported, it sets up
  102. the tool bar even if the current frame is not a graphical one, since a
  103. graphical frame may be created later on.
  104. @item
  105. It use @code{custom-reevaluate-setting} to re-initialize the members
  106. of the list @code{custom-delayed-init-variables}. These are any
  107. pre-loaded user options whose default value depends on the run-time,
  108. rather than build-time, context.
  109. @xref{Building Emacs, custom-initialize-delay}.
  110. @c @item
  111. @c It registers the colors available for tty frames.
  112. @item
  113. It loads the library @file{site-start}, if it exists. This is not
  114. done if the options @samp{-Q} or @samp{--no-site-file} were specified.
  115. @cindex @file{site-start.el}
  116. @item
  117. It loads your init file (@pxref{Init File}). This is not done if the
  118. options @samp{-q}, @samp{-Q}, or @samp{--batch} were specified. If
  119. the @samp{-u} option was specified, Emacs looks for the init file in
  120. that user's home directory instead.
  121. @item
  122. It loads the library @file{default}, if it exists. This is not done
  123. if @code{inhibit-default-init} is non-@code{nil}, nor if the options
  124. @samp{-q}, @samp{-Q}, or @samp{--batch} were specified.
  125. @cindex @file{default.el}
  126. @item
  127. It loads your abbrevs from the file specified by
  128. @code{abbrev-file-name}, if that file exists and can be read
  129. (@pxref{Abbrev Files, abbrev-file-name}). This is not done if the
  130. option @samp{--batch} was specified.
  131. @item
  132. If @code{package-enable-at-startup} is non-@code{nil}, it calls the
  133. function @code{package-initialize} to activate any optional Emacs Lisp
  134. package that has been installed. @xref{Packaging Basics}.
  135. @vindex after-init-time
  136. @item
  137. It sets the variable @code{after-init-time} to the value of
  138. @code{current-time}. This variable was set to @code{nil} earlier;
  139. setting it to the current time signals that the initialization phase
  140. is over, and, together with @code{before-init-time}, provides the
  141. measurement of how long it took.
  142. @item
  143. It runs the normal hook @code{after-init-hook}.
  144. @item
  145. If the buffer @file{*scratch*} exists and is still in Fundamental mode
  146. (as it should be by default), it sets its major mode according to
  147. @code{initial-major-mode}.
  148. @item
  149. If started on a text terminal, it loads the terminal-specific
  150. Lisp library (@pxref{Terminal-Specific}), and runs the hook
  151. @code{tty-setup-hook}. This is not done
  152. in @code{--batch} mode, nor if @code{term-file-prefix} is @code{nil}.
  153. @c Now command-line calls command-line-1.
  154. @item
  155. It displays the initial echo area message, unless you have suppressed
  156. that with @code{inhibit-startup-echo-area-message}.
  157. @item
  158. It processes any command-line options that were not handled earlier.
  159. @c This next one is back in command-line, but the remaining bits of
  160. @c command-line-1 are not done if noninteractive.
  161. @item
  162. It now exits if the option @code{--batch} was specified.
  163. @item
  164. If @code{initial-buffer-choice} is a string, it visits the file (or
  165. directory) with that name. If it is a function, it calls the function
  166. with no arguments and selects the buffer that it returns.
  167. @ignore
  168. @c I do not think this should be mentioned. AFAICS it is just a dodge
  169. @c around inhibit-startup-screen not being settable on a site-wide basis.
  170. If it is @code{t}, it selects the @file{*scratch*} buffer.
  171. @end ignore
  172. If the @file{*scratch*} buffer exists and is empty, it inserts
  173. @code{initial-scratch-message} into that buffer.
  174. @c To make things nice and confusing, the next three items can be
  175. @c called from two places. If displaying a startup screen, they are
  176. @c called in command-line-1 before the startup screen is shown.
  177. @c inhibit-startup-hooks is then set and window-setup-hook set to nil.
  178. @c If not displaying a startup screen, they are are called in
  179. @c normal-top-level.
  180. @c FIXME? So it seems they can be called before or after the
  181. @c daemon/session restore step?
  182. @item
  183. It runs @code{emacs-startup-hook}.
  184. @item
  185. It calls @code{frame-notice-user-settings}, which modifies the
  186. parameters of the selected frame according to whatever the init files
  187. specify.
  188. @item
  189. It runs @code{window-setup-hook}. The only difference between this
  190. hook and @code{emacs-startup-hook} is that this one runs after the
  191. previously mentioned modifications to the frame parameters.
  192. @item
  193. @cindex startup screen
  194. It displays the @dfn{startup screen}, which is a special buffer that
  195. contains information about copyleft and basic Emacs usage. This is
  196. not done if @code{inhibit-startup-screen} or @code{initial-buffer-choice}
  197. are non-@code{nil}, or if the @samp{--no-splash} or @samp{-Q} command-line
  198. options were specified.
  199. @c End of command-line-1.
  200. @c Back to command-line from command-line-1.
  201. @c This is the point at which we actually exit in batch mode, but the
  202. @c last few bits of command-line-1 are not done in batch mode.
  203. @item
  204. If the option @code{--daemon} was specified, it calls
  205. @code{server-start}, and on Posix systems also detaches from the
  206. controlling terminal. @xref{Emacs Server,,, emacs, The GNU Emacs
  207. Manual}.
  208. @item
  209. If started by the X session manager, it calls
  210. @code{emacs-session-restore} passing it as argument the ID of the
  211. previous session. @xref{Session Management}.
  212. @c End of command-line.
  213. @c Back to normal-top-level from command-line.
  214. @end enumerate
  215. @noindent
  216. The following options affect some aspects of the startup sequence.
  217. @defopt inhibit-startup-screen
  218. This variable, if non-@code{nil}, inhibits the startup screen. In
  219. that case, Emacs typically displays the @file{*scratch*} buffer; but
  220. see @code{initial-buffer-choice}, below.
  221. Do not set this variable in the init file of a new user, or in a way
  222. that affects more than one user, as that would prevent new users from
  223. receiving information about copyleft and basic Emacs usage.
  224. @vindex inhibit-startup-message
  225. @vindex inhibit-splash-screen
  226. @code{inhibit-startup-message} and @code{inhibit-splash-screen} are
  227. aliases for this variable.
  228. @end defopt
  229. @defopt initial-buffer-choice
  230. If non-@code{nil}, this variable is a string that specifies a file or
  231. directory for Emacs to display after starting up, instead of the
  232. startup screen.
  233. If its value is a function, Emacs calls that function which must
  234. return a buffer which is then displayed.
  235. If its value is @code{t}, Emacs displays the @file{*scratch*} buffer.
  236. @end defopt
  237. @defopt inhibit-startup-echo-area-message
  238. This variable controls the display of the startup echo area message.
  239. You can suppress the startup echo area message by adding text with this
  240. form to your init file:
  241. @example
  242. (setq inhibit-startup-echo-area-message
  243. "@var{your-login-name}")
  244. @end example
  245. Emacs explicitly checks for an expression as shown above in your init
  246. file; your login name must appear in the expression as a Lisp string
  247. constant. You can also use the Customize interface. Other methods of
  248. setting @code{inhibit-startup-echo-area-message} to the same value do
  249. not inhibit the startup message. This way, you can easily inhibit the
  250. message for yourself if you wish, but thoughtless copying of your init
  251. file will not inhibit the message for someone else.
  252. @end defopt
  253. @defopt initial-scratch-message
  254. This variable, if non-@code{nil}, should be a string, which is
  255. inserted into the @file{*scratch*} buffer when Emacs starts up. If it
  256. is @code{nil}, the @file{*scratch*} buffer is empty.
  257. @end defopt
  258. @noindent
  259. The following command-line options affect some aspects of the startup
  260. sequence. @xref{Initial Options,,, emacs, The GNU Emacs Manual}.
  261. @table @code
  262. @item --no-splash
  263. Do not display a splash screen.
  264. @item --batch
  265. Run without an interactive terminal. @xref{Batch Mode}.
  266. @item --daemon
  267. Do not initialize any display; just start a server in the background.
  268. @item --no-init-file
  269. @itemx -q
  270. Do not load either the init file, or the @file{default} library.
  271. @item --no-site-file
  272. Do not load the @file{site-start} library.
  273. @item --quick
  274. @itemx -Q
  275. Equivalent to @samp{-q --no-site-file --no-splash}.
  276. @c and --no-site-lisp, but let's not mention that here.
  277. @end table
  278. @node Init File
  279. @subsection The Init File
  280. @cindex init file
  281. @cindex @file{.emacs}
  282. @cindex @file{init.el}
  283. When you start Emacs, it normally attempts to load your @dfn{init
  284. file}. This is either a file named @file{.emacs} or @file{.emacs.el}
  285. in your home directory, or a file named @file{init.el} in a
  286. subdirectory named @file{.emacs.d} in your home directory.
  287. @ignore
  288. Whichever place you use, you can also compile the file (@pxref{Byte
  289. Compilation}); then the actual file loaded will be @file{.emacs.elc}
  290. or @file{init.elc}.
  291. @end ignore
  292. The command-line switches @samp{-q}, @samp{-Q}, and @samp{-u}
  293. control whether and where to find the init file; @samp{-q} (and the
  294. stronger @samp{-Q}) says not to load an init file, while @samp{-u
  295. @var{user}} says to load @var{user}'s init file instead of yours.
  296. @xref{Entering Emacs,,, emacs, The GNU Emacs Manual}. If neither
  297. option is specified, Emacs uses the @env{LOGNAME} environment
  298. variable, or the @env{USER} (most systems) or @env{USERNAME} (MS
  299. systems) variable, to find your home directory and thus your init
  300. file; this way, even if you have su'd, Emacs still loads your own init
  301. file. If those environment variables are absent, though, Emacs uses
  302. your user-id to find your home directory.
  303. @cindex default init file
  304. An Emacs installation may have a @dfn{default init file}, which is a
  305. Lisp library named @file{default.el}. Emacs finds this file through
  306. the standard search path for libraries (@pxref{How Programs Do
  307. Loading}). The Emacs distribution does not come with this file; it is
  308. intended for local customizations. If the default init file exists,
  309. it is loaded whenever you start Emacs. But your own personal init
  310. file, if any, is loaded first; if it sets @code{inhibit-default-init}
  311. to a non-@code{nil} value, then Emacs does not subsequently load the
  312. @file{default.el} file. In batch mode, or if you specify @samp{-q}
  313. (or @samp{-Q}), Emacs loads neither your personal init file nor
  314. the default init file.
  315. Another file for site-customization is @file{site-start.el}. Emacs
  316. loads this @emph{before} the user's init file. You can inhibit the
  317. loading of this file with the option @samp{--no-site-file}.
  318. @defopt site-run-file
  319. This variable specifies the site-customization file to load before the
  320. user's init file. Its normal value is @code{"site-start"}. The only
  321. way you can change it with real effect is to do so before dumping
  322. Emacs.
  323. @c So why even mention it here. I imagine it is almost never changed.
  324. @end defopt
  325. @xref{Init Examples,, Init File Examples, emacs, The GNU Emacs Manual}, for
  326. examples of how to make various commonly desired customizations in your
  327. @file{.emacs} file.
  328. @defopt inhibit-default-init
  329. If this variable is non-@code{nil}, it prevents Emacs from loading the
  330. default initialization library file. The default value is @code{nil}.
  331. @end defopt
  332. @defvar before-init-hook
  333. This normal hook is run, once, just before loading all the init files
  334. (@file{site-start.el}, your init file, and @file{default.el}).
  335. (The only way to change it with real effect is before dumping Emacs.)
  336. @end defvar
  337. @defvar after-init-hook
  338. This normal hook is run, once, just after loading all the init files
  339. (@file{site-start.el}, your init file, and @file{default.el}),
  340. before loading the terminal-specific library (if started on a text
  341. terminal) and processing the command-line action arguments.
  342. @end defvar
  343. @defvar emacs-startup-hook
  344. This normal hook is run, once, just after handling the command line
  345. arguments. In batch mode, Emacs does not run this hook.
  346. @end defvar
  347. @defvar window-setup-hook
  348. This normal hook is very similar to @code{emacs-startup-hook}.
  349. The only difference is that it runs slightly later, after setting
  350. of the frame parameters. @xref{Startup Summary, window-setup-hook}.
  351. @end defvar
  352. @defvar user-init-file
  353. This variable holds the absolute file name of the user's init file. If the
  354. actual init file loaded is a compiled file, such as @file{.emacs.elc},
  355. the value refers to the corresponding source file.
  356. @end defvar
  357. @defvar user-emacs-directory
  358. This variable holds the name of the @file{.emacs.d} directory. It is
  359. @file{~/.emacs.d} on all platforms but MS-DOS.
  360. @end defvar
  361. @node Terminal-Specific
  362. @subsection Terminal-Specific Initialization
  363. @cindex terminal-specific initialization
  364. Each terminal type can have its own Lisp library that Emacs loads when
  365. run on that type of terminal. The library's name is constructed by
  366. concatenating the value of the variable @code{term-file-prefix} and the
  367. terminal type (specified by the environment variable @env{TERM}).
  368. Normally, @code{term-file-prefix} has the value @code{"term/"};
  369. changing this is not recommended. If there is an entry matching
  370. @env{TERM} in the @code{term-file-aliases} association list,
  371. Emacs uses the associated value in place of @env{TERM}.
  372. Emacs finds the file in the normal manner, by searching the
  373. @code{load-path} directories, and trying the @samp{.elc} and
  374. @samp{.el} suffixes.
  375. @cindex Termcap
  376. The usual role of a terminal-specific library is to enable special
  377. keys to send sequences that Emacs can recognize. It may also need to
  378. set or add to @code{input-decode-map} if the Termcap or Terminfo entry
  379. does not specify all the terminal's function keys. @xref{Terminal Input}.
  380. When the name of the terminal type contains a hyphen or underscore,
  381. and no library is found whose name is identical to the terminal's
  382. name, Emacs strips from the terminal's name the last hyphen or
  383. underscore and everything that follows
  384. it, and tries again. This process is repeated until Emacs finds a
  385. matching library, or until there are no more hyphens or underscores in the name
  386. (i.e., there is no terminal-specific library). For example, if the
  387. terminal name is @samp{xterm-256color} and there is no
  388. @file{term/xterm-256color.el} library, Emacs tries to load
  389. @file{term/xterm.el}. If necessary, the terminal library can evaluate
  390. @code{(getenv "TERM")} to find the full name of the terminal type.
  391. Your init file can prevent the loading of the terminal-specific
  392. library by setting the variable @code{term-file-prefix} to @code{nil}.
  393. You can also arrange to override some of the actions of the
  394. terminal-specific library by using @code{tty-setup-hook}. This is
  395. a normal hook that Emacs runs after initializing a new text terminal.
  396. You could use this hook to define initializations for terminals that do not
  397. have their own libraries. @xref{Hooks}.
  398. @defopt term-file-prefix
  399. @cindex @env{TERM} environment variable
  400. If the value of this variable is non-@code{nil}, Emacs loads a
  401. terminal-specific initialization file as follows:
  402. @example
  403. (load (concat term-file-prefix (getenv "TERM")))
  404. @end example
  405. @noindent
  406. You may set the @code{term-file-prefix} variable to @code{nil} in your
  407. init file if you do not wish to load the
  408. terminal-initialization file.
  409. On MS-DOS, Emacs sets the @env{TERM} environment variable to @samp{internal}.
  410. @end defopt
  411. @defopt term-file-aliases
  412. This variable is an an association list mapping terminal types to
  413. their aliases. For example, an element of the form @code{("vt102"
  414. . "vt100")} means to treat a terminal of type @samp{vt102} like one of
  415. type @samp{vt100}.
  416. @end defopt
  417. @defvar tty-setup-hook
  418. This variable is a normal hook that Emacs runs after initializing a
  419. new text terminal. (This applies when Emacs starts up in non-windowed
  420. mode, and when making a tty @command{emacsclient} connection.) The
  421. hook runs after loading your init file (if applicable) and the
  422. terminal-specific Lisp file, so you can use it to adjust the
  423. definitions made by that file.
  424. For a related feature, @pxref{Init File, window-setup-hook}.
  425. @end defvar
  426. @node Command-Line Arguments
  427. @subsection Command-Line Arguments
  428. @cindex command-line arguments
  429. You can use command-line arguments to request various actions when
  430. you start Emacs. Note that the recommended way of using Emacs is to
  431. start it just once, after logging in, and then do all editing in the same
  432. Emacs session (@pxref{Entering Emacs,,, emacs, The GNU Emacs Manual}).
  433. For this reason, you might not use command-line arguments very often;
  434. nonetheless, they can be useful when invoking Emacs from session
  435. scripts or debugging Emacs. This section describes how Emacs
  436. processes command-line arguments.
  437. @defun command-line
  438. This function parses the command line that Emacs was called with,
  439. processes it, and (amongst other things) loads the user's init file and
  440. displays the startup messages.
  441. @end defun
  442. @defvar command-line-processed
  443. The value of this variable is @code{t} once the command line has been
  444. processed.
  445. If you redump Emacs by calling @code{dump-emacs} (@pxref{Building
  446. Emacs}), you may wish to set this variable to @code{nil} first in
  447. order to cause the new dumped Emacs to process its new command-line
  448. arguments.
  449. @end defvar
  450. @defvar command-switch-alist
  451. @cindex switches on command line
  452. @cindex options on command line
  453. @cindex command-line options
  454. This variable is an alist of user-defined command-line options and
  455. associated handler functions. By default it is empty, but you can
  456. add elements if you wish.
  457. A @dfn{command-line option} is an argument on the command line, which
  458. has the form:
  459. @example
  460. -@var{option}
  461. @end example
  462. The elements of the @code{command-switch-alist} look like this:
  463. @example
  464. (@var{option} . @var{handler-function})
  465. @end example
  466. The @sc{car}, @var{option}, is a string, the name of a command-line
  467. option (not including the initial hyphen). The @var{handler-function}
  468. is called to handle @var{option}, and receives the option name as its
  469. sole argument.
  470. In some cases, the option is followed in the command line by an
  471. argument. In these cases, the @var{handler-function} can find all the
  472. remaining command-line arguments in the variable
  473. @code{command-line-args-left} (see below). (The entire list of
  474. command-line arguments is in @code{command-line-args}.)
  475. The command-line arguments are parsed by the @code{command-line-1}
  476. function in the @file{startup.el} file. See also @ref{Emacs
  477. Invocation, , Command Line Arguments for Emacs Invocation, emacs, The
  478. GNU Emacs Manual}.
  479. @end defvar
  480. @defvar command-line-args
  481. The value of this variable is the list of command-line arguments passed
  482. to Emacs.
  483. @end defvar
  484. @defvar command-line-args-left
  485. @vindex argv
  486. The value of this variable is the list of command-line arguments that
  487. have not yet been processed.
  488. @c Don't mention this, since it is a "bad name for a dynamically bound variable"
  489. @c @code{argv} is an alias for this.
  490. @end defvar
  491. @defvar command-line-functions
  492. This variable's value is a list of functions for handling an
  493. unrecognized command-line argument. Each time the next argument to be
  494. processed has no special meaning, the functions in this list are called,
  495. in order of appearance, until one of them returns a non-@code{nil}
  496. value.
  497. These functions are called with no arguments. They can access the
  498. command-line argument under consideration through the variable
  499. @code{argi}, which is bound temporarily at this point. The remaining
  500. arguments (not including the current one) are in the variable
  501. @code{command-line-args-left}.
  502. When a function recognizes and processes the argument in @code{argi}, it
  503. should return a non-@code{nil} value to say it has dealt with that
  504. argument. If it has also dealt with some of the following arguments, it
  505. can indicate that by deleting them from @code{command-line-args-left}.
  506. If all of these functions return @code{nil}, then the argument is treated
  507. as a file name to visit.
  508. @end defvar
  509. @node Getting Out
  510. @section Getting Out of Emacs
  511. @cindex exiting Emacs
  512. There are two ways to get out of Emacs: you can kill the Emacs job,
  513. which exits permanently, or you can suspend it, which permits you to
  514. reenter the Emacs process later. (In a graphical environment, you can
  515. of course simply switch to another application without doing anything
  516. special to Emacs, then switch back to Emacs when you want.)
  517. @menu
  518. * Killing Emacs:: Exiting Emacs irreversibly.
  519. * Suspending Emacs:: Exiting Emacs reversibly.
  520. @end menu
  521. @node Killing Emacs
  522. @subsection Killing Emacs
  523. @cindex killing Emacs
  524. Killing Emacs means ending the execution of the Emacs process.
  525. If you started Emacs from a terminal, the parent process normally
  526. resumes control. The low-level primitive for killing Emacs is
  527. @code{kill-emacs}.
  528. @deffn Command kill-emacs &optional exit-data
  529. This command calls the hook @code{kill-emacs-hook}, then exits the
  530. Emacs process and kills it.
  531. If @var{exit-data} is an integer, that is used as the exit status of
  532. the Emacs process. (This is useful primarily in batch operation; see
  533. @ref{Batch Mode}.)
  534. If @var{exit-data} is a string, its contents are stuffed into the
  535. terminal input buffer so that the shell (or whatever program next reads
  536. input) can read them.
  537. @end deffn
  538. @cindex SIGTERM
  539. @cindex SIGHUP
  540. @cindex SIGINT
  541. @cindex operating system signal
  542. The @code{kill-emacs} function is normally called via the
  543. higher-level command @kbd{C-x C-c}
  544. (@code{save-buffers-kill-terminal}). @xref{Exiting,,, emacs, The GNU
  545. Emacs Manual}. It is also called automatically if Emacs receives a
  546. @code{SIGTERM} or @code{SIGHUP} operating system signal (e.g., when the
  547. controlling terminal is disconnected), or if it receives a
  548. @code{SIGINT} signal while running in batch mode (@pxref{Batch Mode}).
  549. @defvar kill-emacs-hook
  550. This normal hook is run by @code{kill-emacs}, before it kills Emacs.
  551. Because @code{kill-emacs} can be called in situations where user
  552. interaction is impossible (e.g., when the terminal is disconnected),
  553. functions on this hook should not attempt to interact with the user.
  554. If you want to interact with the user when Emacs is shutting down, use
  555. @code{kill-emacs-query-functions}, described below.
  556. @end defvar
  557. When Emacs is killed, all the information in the Emacs process,
  558. aside from files that have been saved, is lost. Because killing Emacs
  559. inadvertently can lose a lot of work, the
  560. @code{save-buffers-kill-terminal} command queries for confirmation if
  561. you have buffers that need saving or subprocesses that are running.
  562. It also runs the abnormal hook @code{kill-emacs-query-functions}:
  563. @defvar kill-emacs-query-functions
  564. When @code{save-buffers-kill-terminal} is killing Emacs, it calls the
  565. functions in this hook, after asking the standard questions and before
  566. calling @code{kill-emacs}. The functions are called in order of
  567. appearance, with no arguments. Each function can ask for additional
  568. confirmation from the user. If any of them returns @code{nil},
  569. @code{save-buffers-kill-emacs} does not kill Emacs, and does not run
  570. the remaining functions in this hook. Calling @code{kill-emacs}
  571. directly does not run this hook.
  572. @end defvar
  573. @node Suspending Emacs
  574. @subsection Suspending Emacs
  575. @cindex suspending Emacs
  576. On text terminals, it is possible to @dfn{suspend Emacs}, which
  577. means stopping Emacs temporarily and returning control to its superior
  578. process, which is usually the shell. This allows you to resume
  579. editing later in the same Emacs process, with the same buffers, the
  580. same kill ring, the same undo history, and so on. To resume Emacs,
  581. use the appropriate command in the parent shell---most likely
  582. @code{fg}.
  583. @cindex controlling terminal
  584. Suspending works only on a terminal device from which the Emacs
  585. session was started. We call that device the @dfn{controlling
  586. terminal} of the session. Suspending is not allowed if the
  587. controlling terminal is a graphical terminal. Suspending is usually
  588. not relevant in graphical environments, since you can simply switch to
  589. another application without doing anything special to Emacs.
  590. @c FIXME? Are there any systems Emacs still supports that do not
  591. @c have SIGTSTP?
  592. @cindex SIGTSTP
  593. Some operating systems (those without @code{SIGTSTP}, or MS-DOS) do
  594. not support suspension of jobs; on these systems, ``suspension''
  595. actually creates a new shell temporarily as a subprocess of Emacs.
  596. Then you would exit the shell to return to Emacs.
  597. @deffn Command suspend-emacs &optional string
  598. This function stops Emacs and returns control to the superior process.
  599. If and when the superior process resumes Emacs, @code{suspend-emacs}
  600. returns @code{nil} to its caller in Lisp.
  601. This function works only on the controlling terminal of the Emacs
  602. session; to relinquish control of other tty devices, use
  603. @code{suspend-tty} (see below). If the Emacs session uses more than
  604. one terminal, you must delete the frames on all the other terminals
  605. before suspending Emacs, or this function signals an error.
  606. @xref{Multiple Terminals}.
  607. If @var{string} is non-@code{nil}, its characters are sent to Emacs's
  608. superior shell, to be read as terminal input.
  609. @c FIXME? It seems to me that shell does echo STRING.
  610. The characters in @var{string} are not echoed by the superior shell;
  611. only the results appear.
  612. Before suspending, @code{suspend-emacs} runs the normal hook
  613. @code{suspend-hook}. After the user resumes Emacs,
  614. @code{suspend-emacs} runs the normal hook @code{suspend-resume-hook}.
  615. @xref{Hooks}.
  616. The next redisplay after resumption will redraw the entire screen,
  617. unless the variable @code{no-redraw-on-reenter} is non-@code{nil}.
  618. @xref{Refresh Screen}.
  619. Here is an example of how you could use these hooks:
  620. @smallexample
  621. @group
  622. (add-hook 'suspend-hook
  623. (lambda () (or (y-or-n-p "Really suspend? ")
  624. (error "Suspend canceled"))))
  625. @end group
  626. (add-hook 'suspend-resume-hook (lambda () (message "Resumed!")
  627. (sit-for 2)))
  628. @end smallexample
  629. @c The sit-for prevents the @code{nil} that suspend-emacs returns
  630. @c hiding the message.
  631. Here is what you would see upon evaluating @code{(suspend-emacs "pwd")}:
  632. @smallexample
  633. @group
  634. ---------- Buffer: Minibuffer ----------
  635. Really suspend? @kbd{y}
  636. ---------- Buffer: Minibuffer ----------
  637. @end group
  638. @group
  639. ---------- Parent Shell ----------
  640. bash$ /home/username
  641. bash$ fg
  642. @end group
  643. @group
  644. ---------- Echo Area ----------
  645. Resumed!
  646. @end group
  647. @end smallexample
  648. @c FIXME? AFAICS, it is echoed.
  649. Note that @samp{pwd} is not echoed after Emacs is suspended. But it
  650. is read and executed by the shell.
  651. @end deffn
  652. @defvar suspend-hook
  653. This variable is a normal hook that Emacs runs before suspending.
  654. @end defvar
  655. @defvar suspend-resume-hook
  656. This variable is a normal hook that Emacs runs on resuming
  657. after a suspension.
  658. @end defvar
  659. @defun suspend-tty &optional tty
  660. If @var{tty} specifies a terminal device used by Emacs, this function
  661. relinquishes the device and restores it to its prior state. Frames
  662. that used the device continue to exist, but are not updated and Emacs
  663. doesn't read input from them. @var{tty} can be a terminal object, a
  664. frame (meaning the terminal for that frame), or @code{nil} (meaning
  665. the terminal for the selected frame). @xref{Multiple Terminals}.
  666. If @var{tty} is already suspended, this function does nothing.
  667. @vindex suspend-tty-functions
  668. This function runs the hook @code{suspend-tty-functions}, passing the
  669. terminal object as an argument to each function.
  670. @end defun
  671. @defun resume-tty &optional tty
  672. This function resumes the previously suspended terminal device
  673. @var{tty}; where @var{tty} has the same possible values as it does
  674. for @code{suspend-tty}.
  675. @vindex resume-tty-functions
  676. This function reopens the terminal device, re-initializes it, and
  677. redraws it with that terminal's selected frame. It then runs the
  678. hook @code{resume-tty-functions}, passing the terminal object as an
  679. argument to each function.
  680. If the same device is already used by another Emacs terminal, this
  681. function signals an error. If @var{tty} is not suspended, this
  682. function does nothing.
  683. @end defun
  684. @defun controlling-tty-p &optional tty
  685. This function returns non-@code{nil} if @var{tty} is the
  686. controlling terminal of the Emacs session; @var{tty} can be a
  687. terminal object, a frame (meaning the terminal for that frame), or
  688. @code{nil} (meaning the terminal for the selected frame).
  689. @end defun
  690. @deffn Command suspend-frame
  691. This command @dfn{suspends} a frame. For GUI frames, it calls
  692. @code{iconify-frame} (@pxref{Visibility of Frames}); for frames on
  693. text terminals, it calls either @code{suspend-emacs} or
  694. @code{suspend-tty}, depending on whether the frame is displayed on the
  695. controlling terminal device or not.
  696. @end deffn
  697. @node System Environment
  698. @section Operating System Environment
  699. @cindex operating system environment
  700. Emacs provides access to variables in the operating system environment
  701. through various functions. These variables include the name of the
  702. system, the user's @acronym{UID}, and so on.
  703. @defvar system-configuration
  704. This variable holds the standard GNU configuration name for the
  705. hardware/software configuration of your system, as a string. For
  706. example, a typical value for a 64-bit GNU/Linux system is
  707. @samp{"x86_64-unknown-linux-gnu"}.
  708. @end defvar
  709. @cindex system type and name
  710. @defvar system-type
  711. The value of this variable is a symbol indicating the type of operating
  712. system Emacs is running on. The possible values are:
  713. @table @code
  714. @item aix
  715. IBM's AIX.
  716. @item berkeley-unix
  717. Berkeley BSD and its variants.
  718. @item cygwin
  719. Cygwin, a Posix layer on top of MS-Windows.
  720. @item darwin
  721. Darwin (Mac OS X).
  722. @item gnu
  723. The GNU system (using the GNU kernel, which consists of the HURD and Mach).
  724. @item gnu/linux
  725. A GNU/Linux system---that is, a variant GNU system, using the Linux
  726. kernel. (These systems are the ones people often call ``Linux'', but
  727. actually Linux is just the kernel, not the whole system.)
  728. @item gnu/kfreebsd
  729. A GNU (glibc-based) system with a FreeBSD kernel.
  730. @item hpux
  731. Hewlett-Packard HPUX operating system.
  732. @item irix
  733. Silicon Graphics Irix system.
  734. @item ms-dos
  735. Microsoft's DOS@. Emacs compiled with DJGPP for MS-DOS binds
  736. @code{system-type} to @code{ms-dos} even when you run it on MS-Windows.
  737. @item usg-unix-v
  738. AT&T Unix System V.
  739. @item windows-nt
  740. Microsoft Windows NT, 9X and later. The value of @code{system-type}
  741. is always @code{windows-nt}, e.g., even on Windows 7.
  742. @end table
  743. We do not wish to add new symbols to make finer distinctions unless it
  744. is absolutely necessary! In fact, we hope to eliminate some of these
  745. alternatives in the future. If you need to make a finer distinction
  746. than @code{system-type} allows for, you can test
  747. @code{system-configuration}, e.g., against a regexp.
  748. @end defvar
  749. @defun system-name
  750. This function returns the name of the machine you are running on, as a
  751. string.
  752. @end defun
  753. @c FIXME seems like this section is not the best place for this option?
  754. @defopt mail-host-address
  755. If this variable is non-@code{nil}, it is used instead of
  756. @code{system-name} for purposes of generating email addresses. For
  757. example, it is used when constructing the default value of
  758. @code{user-mail-address}. @xref{User Identification}. (Since this is
  759. done when Emacs starts up, the value actually used is the one saved when
  760. Emacs was dumped. @xref{Building Emacs}.)
  761. @c FIXME sounds like should probably give this a :set-after and some
  762. @c custom-initialize-delay voodoo.
  763. @end defopt
  764. @deffn Command getenv var &optional frame
  765. @cindex environment variable access
  766. This function returns the value of the environment variable @var{var},
  767. as a string. @var{var} should be a string. If @var{var} is undefined
  768. in the environment, @code{getenv} returns @code{nil}. It returns
  769. @samp{""} if @var{var} is set but null. Within Emacs, a list of environment
  770. variables and their values is kept in the variable @code{process-environment}.
  771. @example
  772. @group
  773. (getenv "USER")
  774. @result{} "lewis"
  775. @end group
  776. @end example
  777. The shell command @code{printenv} prints all or part of the environment:
  778. @example
  779. @group
  780. bash$ printenv
  781. PATH=/usr/local/bin:/usr/bin:/bin
  782. USER=lewis
  783. @end group
  784. @group
  785. TERM=xterm
  786. SHELL=/bin/bash
  787. HOME=/home/lewis
  788. @end group
  789. @dots{}
  790. @end example
  791. @end deffn
  792. @deffn Command setenv variable &optional value substitute
  793. This command sets the value of the environment variable named
  794. @var{variable} to @var{value}. @var{variable} should be a string.
  795. Internally, Emacs Lisp can handle any string. However, normally
  796. @var{variable} should be a valid shell identifier, that is, a sequence
  797. of letters, digits and underscores, starting with a letter or
  798. underscore. Otherwise, errors may occur if subprocesses of Emacs try
  799. to access the value of @var{variable}. If @var{value} is omitted or
  800. @code{nil} (or, interactively, with a prefix argument), @code{setenv}
  801. removes @var{variable} from the environment. Otherwise, @var{value}
  802. should be a string.
  803. @c FIXME: Document 'substitute-env-vars'? --xfq
  804. If the optional argument @var{substitute} is non-@code{nil}, Emacs
  805. calls the function @code{substitute-env-vars} to expand any
  806. environment variables in @var{value}.
  807. @code{setenv} works by modifying @code{process-environment}; binding
  808. that variable with @code{let} is also reasonable practice.
  809. @code{setenv} returns the new value of @var{variable}, or @code{nil}
  810. if it removed @var{variable} from the environment.
  811. @end deffn
  812. @defvar process-environment
  813. This variable is a list of strings, each describing one environment
  814. variable. The functions @code{getenv} and @code{setenv} work by means
  815. of this variable.
  816. @smallexample
  817. @group
  818. process-environment
  819. @result{} ("PATH=/usr/local/bin:/usr/bin:/bin"
  820. "USER=lewis"
  821. @end group
  822. @group
  823. "TERM=xterm"
  824. "SHELL=/bin/bash"
  825. "HOME=/home/lewis"
  826. @dots{})
  827. @end group
  828. @end smallexample
  829. If @code{process-environment} contains ``duplicate'' elements that
  830. specify the same environment variable, the first of these elements
  831. specifies the variable, and the other ``duplicates'' are ignored.
  832. @end defvar
  833. @defvar initial-environment
  834. This variable holds the list of environment variables Emacs inherited
  835. from its parent process when Emacs started.
  836. @end defvar
  837. @defvar path-separator
  838. This variable holds a string that says which character separates
  839. directories in a search path (as found in an environment variable). Its
  840. value is @code{":"} for Unix and GNU systems, and @code{";"} for MS systems.
  841. @end defvar
  842. @defun parse-colon-path path
  843. This function takes a search path string such as the value of
  844. the @env{PATH} environment variable, and splits it at the separators,
  845. returning a list of directory names. @code{nil} in this list means
  846. the current directory. Although the function's name says
  847. ``colon'', it actually uses the value of @code{path-separator}.
  848. @example
  849. (parse-colon-path ":/foo:/bar")
  850. @result{} (nil "/foo/" "/bar/")
  851. @end example
  852. @end defun
  853. @defvar invocation-name
  854. This variable holds the program name under which Emacs was invoked. The
  855. value is a string, and does not include a directory name.
  856. @end defvar
  857. @defvar invocation-directory
  858. This variable holds the directory from which the Emacs executable was
  859. invoked, or @code{nil} if that directory cannot be determined.
  860. @end defvar
  861. @defvar installation-directory
  862. If non-@code{nil}, this is a directory within which to look for the
  863. @file{lib-src} and @file{etc} subdirectories. In an installed Emacs,
  864. it is normally @code{nil}. It is non-@code{nil}
  865. when Emacs can't find those directories in their standard installed
  866. locations, but can find them in a directory related somehow to the one
  867. containing the Emacs executable (i.e., @code{invocation-directory}).
  868. @end defvar
  869. @defun load-average &optional use-float
  870. This function returns the current 1-minute, 5-minute, and 15-minute
  871. system load averages, in a list. The load average indicates the
  872. number of processes trying to run on the system.
  873. By default, the values are integers that are 100 times the system load
  874. averages, but if @var{use-float} is non-@code{nil}, then they are
  875. returned as floating-point numbers without multiplying by 100.
  876. If it is impossible to obtain the load average, this function signals
  877. an error. On some platforms, access to load averages requires
  878. installing Emacs as setuid or setgid so that it can read kernel
  879. information, and that usually isn't advisable.
  880. @c FIXME which platforms are these? Are they still relevant?
  881. If the 1-minute load average is available, but the 5- or 15-minute
  882. averages are not, this function returns a shortened list containing
  883. the available averages.
  884. @example
  885. @group
  886. (load-average)
  887. @result{} (169 48 36)
  888. @end group
  889. @group
  890. (load-average t)
  891. @result{} (1.69 0.48 0.36)
  892. @end group
  893. @end example
  894. The shell command @code{uptime} returns similar information.
  895. @end defun
  896. @defun emacs-pid
  897. This function returns the process @acronym{ID} of the Emacs process,
  898. as an integer.
  899. @end defun
  900. @defvar tty-erase-char
  901. This variable holds the erase character that was selected
  902. in the system's terminal driver, before Emacs was started.
  903. @c FIXME? Seems untrue since 23.1. For me, it is 0.
  904. @c The value is @code{nil} if Emacs is running under a window system.
  905. @end defvar
  906. @node User Identification
  907. @section User Identification
  908. @cindex user identification
  909. @defvar init-file-user
  910. This variable says which user's init files should be used by
  911. Emacs---or @code{nil} if none. @code{""} stands for the user who
  912. originally logged in. The value reflects command-line options such as
  913. @samp{-q} or @samp{-u @var{user}}.
  914. Lisp packages that load files of customizations, or any other sort of
  915. user profile, should obey this variable in deciding where to find it.
  916. They should load the profile of the user name found in this variable.
  917. If @code{init-file-user} is @code{nil}, meaning that the @samp{-q},
  918. @samp{-Q}, or @samp{-batch} option was used, then Lisp packages should
  919. not load any customization files or user profile.
  920. @end defvar
  921. @defopt user-mail-address
  922. This holds the nominal email address of the user who is using Emacs.
  923. Emacs normally sets this variable to a default value after reading your
  924. init files, but not if you have already set it. So you can set the
  925. variable to some other value in your init file if you do not
  926. want to use the default value.
  927. @end defopt
  928. @defun user-login-name &optional uid
  929. This function returns the name under which the user is logged in.
  930. It uses the environment variables @env{LOGNAME} or @env{USER} if
  931. either is set. Otherwise, the value is based on the effective
  932. @acronym{UID}, not the real @acronym{UID}.
  933. If you specify @var{uid} (a number), the result is the user name that
  934. corresponds to @var{uid}, or @code{nil} if there is no such user.
  935. @end defun
  936. @defun user-real-login-name
  937. This function returns the user name corresponding to Emacs's real
  938. @acronym{UID}. This ignores the effective @acronym{UID}, and the
  939. environment variables @env{LOGNAME} and @env{USER}.
  940. @end defun
  941. @defun user-full-name &optional uid
  942. This function returns the full name of the logged-in user---or the value
  943. of the environment variable @env{NAME}, if that is set.
  944. If the Emacs process's user-id does not correspond to any known user (and
  945. provided @code{NAME} is not set), the result is @code{"unknown"}.
  946. If @var{uid} is non-@code{nil}, then it should be a number (a user-id)
  947. or a string (a login name). Then @code{user-full-name} returns the full
  948. name corresponding to that user-id or login name. If you specify a
  949. user-id or login name that isn't defined, it returns @code{nil}.
  950. @end defun
  951. @vindex user-full-name
  952. @vindex user-real-login-name
  953. @vindex user-login-name
  954. The symbols @code{user-login-name}, @code{user-real-login-name} and
  955. @code{user-full-name} are variables as well as functions. The functions
  956. return the same values that the variables hold. These variables allow
  957. you to ``fake out'' Emacs by telling the functions what to return. The
  958. variables are also useful for constructing frame titles (@pxref{Frame
  959. Titles}).
  960. @cindex UID
  961. @defun user-real-uid
  962. This function returns the real @acronym{UID} of the user.
  963. The value may be floating point, in the (unlikely) event that
  964. the UID is too large to fit in a Lisp integer.
  965. @end defun
  966. @defun user-uid
  967. This function returns the effective @acronym{UID} of the user.
  968. The value may be floating point.
  969. @end defun
  970. @cindex GID
  971. @defun group-gid
  972. This function returns the effective @acronym{GID} of the Emacs process.
  973. The value may be floating point.
  974. @end defun
  975. @defun group-real-gid
  976. This function returns the real @acronym{GID} of the Emacs process.
  977. The value may be floating point.
  978. @end defun
  979. @defun system-users
  980. This function returns a list of strings, listing the user names on the
  981. system. If Emacs cannot retrieve this information, the return value
  982. is a list containing just the value of @code{user-real-login-name}.
  983. @end defun
  984. @cindex user groups
  985. @defun system-groups
  986. This function returns a list of strings, listing the names of user
  987. groups on the system. If Emacs cannot retrieve this information, the
  988. return value is @code{nil}.
  989. @end defun
  990. @node Time of Day
  991. @section Time of Day
  992. @cindex time of day
  993. This section explains how to determine the current time and time
  994. zone.
  995. @cindex epoch
  996. Most of these functions represent time as a list of four integers
  997. @code{(@var{sec-high} @var{sec-low} @var{microsec} @var{picosec})}.
  998. This represents the number of seconds from the @dfn{epoch} (January
  999. 1, 1970 at 00:00 UTC), using the formula:
  1000. @ifnottex
  1001. @var{high} * 2**16 + @var{low} + @var{micro} * 10**@minus{}6 +
  1002. @var{pico} * 10**@minus{}12.
  1003. @end ifnottex
  1004. @tex
  1005. $high*2^{16} + low + micro*10^{-6} + pico*10^{-12}$.
  1006. @end tex
  1007. The return value of @code{current-time} represents time using this
  1008. form, as do the timestamps in the return values of other functions
  1009. such as @code{file-attributes} (@pxref{Definition of
  1010. file-attributes}). In some cases, functions may return two- or
  1011. three-element lists, with omitted @var{microsec} and @var{picosec}
  1012. components defaulting to zero.
  1013. @cindex time value
  1014. Function arguments, e.g., the @var{time-value} argument to
  1015. @code{current-time-string}, accept a more-general @dfn{time value}
  1016. format, which can be a list of integers as above, or a single number
  1017. for seconds since the epoch, or @code{nil} for the current time. You
  1018. can convert a time value into a human-readable string using
  1019. @code{current-time-string} and @code{format-time-string}, into a list
  1020. of integers using @code{seconds-to-time}, and into other forms using
  1021. @code{decode-time} and @code{float-time}. These functions are
  1022. described in the following sections.
  1023. @defun current-time-string &optional time-value
  1024. This function returns the current time and date as a human-readable
  1025. string. The format does not vary for the initial part of the string,
  1026. which contains the day of week, month, day of month, and time of day
  1027. in that order: the number of characters used for these fields is
  1028. always the same, so you can reliably
  1029. use @code{substring} to extract them. You should count
  1030. characters from the beginning of the string rather than from the end,
  1031. as the year might not have exactly four digits, and additional
  1032. information may some day be added at the end.
  1033. The argument @var{time-value}, if given, specifies a time to format,
  1034. instead of the current time.
  1035. @example
  1036. @group
  1037. (current-time-string)
  1038. @result{} "Wed Oct 14 22:21:05 1987"
  1039. @end group
  1040. @end example
  1041. @end defun
  1042. @defun current-time
  1043. This function returns the current time, represented as a list of four
  1044. integers @code{(@var{sec-high} @var{sec-low} @var{microsec} @var{picosec})}.
  1045. These integers have trailing zeros on systems that return time with
  1046. lower resolutions. On all current machines @var{picosec} is a
  1047. multiple of 1000, but this may change as higher-resolution clocks
  1048. become available.
  1049. @end defun
  1050. @defun float-time &optional time-value
  1051. This function returns the current time as a floating-point number of
  1052. seconds since the epoch. The optional argument @var{time-value}, if
  1053. given, specifies a time to convert instead of the current time.
  1054. @emph{Warning}: Since the result is floating point, it may not be
  1055. exact. Do not use this function if precise time stamps are required.
  1056. @code{time-to-seconds} is an alias for this function.
  1057. @end defun
  1058. @defun seconds-to-time time-value
  1059. This function converts a time value to list-of-integer form.
  1060. For example, if @var{time-value} is a number, @code{(time-to-seconds
  1061. (seconds-to-time @var{time-value}))} equals the number unless overflow
  1062. or rounding errors occur.
  1063. @end defun
  1064. @defun current-time-zone &optional time-value
  1065. @cindex time zone, current
  1066. This function returns a list describing the time zone that the user is
  1067. in.
  1068. The value has the form @code{(@var{offset} @var{name})}. Here
  1069. @var{offset} is an integer giving the number of seconds ahead of UTC
  1070. (east of Greenwich). A negative value means west of Greenwich. The
  1071. second element, @var{name}, is a string giving the name of the time
  1072. zone. Both elements change when daylight saving time begins or ends;
  1073. if the user has specified a time zone that does not use a seasonal time
  1074. adjustment, then the value is constant through time.
  1075. If the operating system doesn't supply all the information necessary to
  1076. compute the value, the unknown elements of the list are @code{nil}.
  1077. The argument @var{time-value}, if given, specifies a time value to
  1078. analyze instead of the current time.
  1079. @end defun
  1080. The current time zone is determined by the @env{TZ} environment
  1081. variable. @xref{System Environment}. For example, you can tell Emacs
  1082. to use universal time with @code{(setenv "TZ" "UTC0")}. If @env{TZ}
  1083. is not in the environment, Emacs uses a platform-dependent default
  1084. time zone.
  1085. @node Time Conversion
  1086. @section Time Conversion
  1087. @cindex calendrical information
  1088. @cindex time conversion
  1089. These functions convert time values (@pxref{Time of Day}) into
  1090. calendrical information and vice versa.
  1091. Many 32-bit operating systems are limited to system times containing
  1092. 32 bits of information in their seconds component; these systems
  1093. typically handle only the times from 1901-12-13 20:45:52 UTC through
  1094. 2038-01-19 03:14:07 UTC@. However, 64-bit and some 32-bit operating
  1095. systems have larger seconds components, and can represent times far in
  1096. the past or future.
  1097. Time conversion functions always use the Gregorian calendar, even
  1098. for dates before the Gregorian calendar was introduced. Year numbers
  1099. count the number of years since the year 1 B.C., and do not skip zero
  1100. as traditional Gregorian years do; for example, the year number
  1101. @minus{}37 represents the Gregorian year 38 B.C@.
  1102. @defun decode-time &optional time-value
  1103. This function converts a time value into calendrical information. If
  1104. you don't specify @var{time-value}, it decodes the current time. The return
  1105. value is a list of nine elements, as follows:
  1106. @example
  1107. (@var{seconds} @var{minutes} @var{hour} @var{day} @var{month} @var{year} @var{dow} @var{dst} @var{zone})
  1108. @end example
  1109. Here is what the elements mean:
  1110. @table @var
  1111. @item seconds
  1112. The number of seconds past the minute, as an integer between 0 and 59.
  1113. On some operating systems, this is 60 for leap seconds.
  1114. @item minutes
  1115. The number of minutes past the hour, as an integer between 0 and 59.
  1116. @item hour
  1117. The hour of the day, as an integer between 0 and 23.
  1118. @item day
  1119. The day of the month, as an integer between 1 and 31.
  1120. @item month
  1121. The month of the year, as an integer between 1 and 12.
  1122. @item year
  1123. The year, an integer typically greater than 1900.
  1124. @item dow
  1125. The day of week, as an integer between 0 and 6, where 0 stands for
  1126. Sunday.
  1127. @item dst
  1128. @code{t} if daylight saving time is effect, otherwise @code{nil}.
  1129. @item zone
  1130. An integer indicating the time zone, as the number of seconds east of
  1131. Greenwich.
  1132. @end table
  1133. @strong{Common Lisp Note:} Common Lisp has different meanings for
  1134. @var{dow} and @var{zone}.
  1135. @end defun
  1136. @defun encode-time seconds minutes hour day month year &optional zone
  1137. This function is the inverse of @code{decode-time}. It converts seven
  1138. items of calendrical data into a list-of-integer time value. For the
  1139. meanings of the arguments, see the table above under
  1140. @code{decode-time}.
  1141. Year numbers less than 100 are not treated specially. If you want them
  1142. to stand for years above 1900, or years above 2000, you must alter them
  1143. yourself before you call @code{encode-time}.
  1144. The optional argument @var{zone} defaults to the current time zone and
  1145. its daylight saving time rules. If specified, it can be either a list
  1146. (as you would get from @code{current-time-zone}), a string as in the
  1147. @env{TZ} environment variable, @code{t} for Universal Time, or an
  1148. integer (as you would get from @code{decode-time}). The specified
  1149. zone is used without any further alteration for daylight saving time.
  1150. If you pass more than seven arguments to @code{encode-time}, the first
  1151. six are used as @var{seconds} through @var{year}, the last argument is
  1152. used as @var{zone}, and the arguments in between are ignored. This
  1153. feature makes it possible to use the elements of a list returned by
  1154. @code{decode-time} as the arguments to @code{encode-time}, like this:
  1155. @example
  1156. (apply 'encode-time (decode-time @dots{}))
  1157. @end example
  1158. You can perform simple date arithmetic by using out-of-range values for
  1159. the @var{seconds}, @var{minutes}, @var{hour}, @var{day}, and @var{month}
  1160. arguments; for example, day 0 means the day preceding the given month.
  1161. The operating system puts limits on the range of possible time values;
  1162. if you try to encode a time that is out of range, an error results.
  1163. For instance, years before 1970 do not work on some systems;
  1164. on others, years as early as 1901 do work.
  1165. @end defun
  1166. @node Time Parsing
  1167. @section Parsing and Formatting Times
  1168. @cindex time parsing
  1169. @cindex time formatting
  1170. @cindex formatting time values
  1171. These functions convert time values to text in a string, and vice versa.
  1172. Time values are lists of two to four integers (@pxref{Time of Day}).
  1173. @defun date-to-time string
  1174. This function parses the time-string @var{string} and returns the
  1175. corresponding time value.
  1176. @end defun
  1177. @defun format-time-string format-string &optional time-value universal
  1178. This function converts @var{time-value} (or the current time, if
  1179. @var{time-value} is omitted) to a string according to
  1180. @var{format-string}. The argument
  1181. @var{format-string} may contain @samp{%}-sequences which say to
  1182. substitute parts of the time. Here is a table of what the
  1183. @samp{%}-sequences mean:
  1184. @table @samp
  1185. @item %a
  1186. This stands for the abbreviated name of the day of week.
  1187. @item %A
  1188. This stands for the full name of the day of week.
  1189. @item %b
  1190. This stands for the abbreviated name of the month.
  1191. @item %B
  1192. This stands for the full name of the month.
  1193. @item %c
  1194. This is a synonym for @samp{%x %X}.
  1195. @item %C
  1196. This has a locale-specific meaning. In the default locale (named C), it
  1197. is equivalent to @samp{%A, %B %e, %Y}.
  1198. @item %d
  1199. This stands for the day of month, zero-padded.
  1200. @item %D
  1201. This is a synonym for @samp{%m/%d/%y}.
  1202. @item %e
  1203. This stands for the day of month, blank-padded.
  1204. @item %h
  1205. This is a synonym for @samp{%b}.
  1206. @item %H
  1207. This stands for the hour (00--23).
  1208. @item %I
  1209. This stands for the hour (01--12).
  1210. @item %j
  1211. This stands for the day of the year (001--366).
  1212. @item %k
  1213. This stands for the hour (0--23), blank padded.
  1214. @item %l
  1215. This stands for the hour (1--12), blank padded.
  1216. @item %m
  1217. This stands for the month (01--12).
  1218. @item %M
  1219. This stands for the minute (00--59).
  1220. @item %n
  1221. This stands for a newline.
  1222. @item %N
  1223. This stands for the nanoseconds (000000000--999999999). To ask for
  1224. fewer digits, use @samp{%3N} for milliseconds, @samp{%6N} for
  1225. microseconds, etc. Any excess digits are discarded, without rounding.
  1226. @item %p
  1227. This stands for @samp{AM} or @samp{PM}, as appropriate.
  1228. @item %r
  1229. This is a synonym for @samp{%I:%M:%S %p}.
  1230. @item %R
  1231. This is a synonym for @samp{%H:%M}.
  1232. @item %S
  1233. This stands for the seconds (00--59).
  1234. @item %t
  1235. This stands for a tab character.
  1236. @item %T
  1237. This is a synonym for @samp{%H:%M:%S}.
  1238. @item %U
  1239. This stands for the week of the year (01--52), assuming that weeks
  1240. start on Sunday.
  1241. @item %w
  1242. This stands for the numeric day of week (0--6). Sunday is day 0.
  1243. @item %W
  1244. This stands for the week of the year (01--52), assuming that weeks
  1245. start on Monday.
  1246. @item %x
  1247. This has a locale-specific meaning. In the default locale (named
  1248. @samp{C}), it is equivalent to @samp{%D}.
  1249. @item %X
  1250. This has a locale-specific meaning. In the default locale (named
  1251. @samp{C}), it is equivalent to @samp{%T}.
  1252. @item %y
  1253. This stands for the year without century (00--99).
  1254. @item %Y
  1255. This stands for the year with century.
  1256. @item %Z
  1257. This stands for the time zone abbreviation (e.g., @samp{EST}).
  1258. @item %z
  1259. This stands for the time zone numerical offset (e.g., @samp{-0500}).
  1260. @end table
  1261. You can also specify the field width and type of padding for any of
  1262. these @samp{%}-sequences. This works as in @code{printf}: you write
  1263. the field width as digits in the middle of a @samp{%}-sequences. If you
  1264. start the field width with @samp{0}, it means to pad with zeros. If you
  1265. start the field width with @samp{_}, it means to pad with spaces.
  1266. For example, @samp{%S} specifies the number of seconds since the minute;
  1267. @samp{%03S} means to pad this with zeros to 3 positions, @samp{%_3S} to
  1268. pad with spaces to 3 positions. Plain @samp{%3S} pads with zeros,
  1269. because that is how @samp{%S} normally pads to two positions.
  1270. The characters @samp{E} and @samp{O} act as modifiers when used between
  1271. @samp{%} and one of the letters in the table above. @samp{E} specifies
  1272. using the current locale's ``alternative'' version of the date and time.
  1273. In a Japanese locale, for example, @code{%Ex} might yield a date format
  1274. based on the Japanese Emperors' reigns. @samp{E} is allowed in
  1275. @samp{%Ec}, @samp{%EC}, @samp{%Ex}, @samp{%EX}, @samp{%Ey}, and
  1276. @samp{%EY}.
  1277. @samp{O} means to use the current locale's ``alternative''
  1278. representation of numbers, instead of the ordinary decimal digits. This
  1279. is allowed with most letters, all the ones that output numbers.
  1280. If @var{universal} is non-@code{nil}, that means to describe the time as
  1281. Universal Time; @code{nil} means describe it using what Emacs believes
  1282. is the local time zone (see @code{current-time-zone}).
  1283. This function uses the C library function @code{strftime}
  1284. (@pxref{Formatting Calendar Time,,, libc, The GNU C Library Reference
  1285. Manual}) to do most of the work. In order to communicate with that
  1286. function, it first encodes its argument using the coding system
  1287. specified by @code{locale-coding-system} (@pxref{Locales}); after
  1288. @code{strftime} returns the resulting string,
  1289. @code{format-time-string} decodes the string using that same coding
  1290. system.
  1291. @end defun
  1292. @defun format-seconds format-string seconds
  1293. This function converts its argument @var{seconds} into a string of
  1294. years, days, hours, etc., according to @var{format-string}. The
  1295. argument @var{format-string} may contain @samp{%}-sequences which
  1296. control the conversion. Here is a table of what the
  1297. @samp{%}-sequences mean:
  1298. @table @samp
  1299. @item %y
  1300. @itemx %Y
  1301. The integer number of 365-day years.
  1302. @item %d
  1303. @itemx %D
  1304. The integer number of days.
  1305. @item %h
  1306. @itemx %H
  1307. The integer number of hours.
  1308. @item %m
  1309. @itemx %M
  1310. The integer number of minutes.
  1311. @item %s
  1312. @itemx %S
  1313. The integer number of seconds.
  1314. @item %z
  1315. Non-printing control flag. When it is used, other specifiers must be
  1316. given in the order of decreasing size, i.e., years before days, hours
  1317. before minutes, etc. Nothing will be produced in the result string to
  1318. the left of @samp{%z} until the first non-zero conversion is
  1319. encountered. For example, the default format used by
  1320. @code{emacs-uptime} (@pxref{Processor Run Time, emacs-uptime})
  1321. @w{@code{"%Y, %D, %H, %M, %z%S"}} means that the number of seconds
  1322. will always be produced, but years, days, hours, and minutes will only
  1323. be shown if they are non-zero.
  1324. @item %%
  1325. Produces a literal @samp{%}.
  1326. @end table
  1327. Upper-case format sequences produce the units in addition to the
  1328. numbers, lower-case formats produce only the numbers.
  1329. You can also specify the field width by following the @samp{%} with a
  1330. number; shorter numbers will be padded with blanks. An optional
  1331. period before the width requests zero-padding instead. For example,
  1332. @code{"%.3Y"} might produce @code{"004 years"}.
  1333. @emph{Warning:} This function works only with values of @var{seconds}
  1334. that don't exceed @code{most-positive-fixnum} (@pxref{Integer Basics,
  1335. most-positive-fixnum}).
  1336. @end defun
  1337. @node Processor Run Time
  1338. @section Processor Run time
  1339. @cindex processor run time
  1340. @cindex Emacs process run time
  1341. Emacs provides several functions and primitives that return time,
  1342. both elapsed and processor time, used by the Emacs process.
  1343. @deffn Command emacs-uptime &optional format
  1344. @cindex uptime of Emacs
  1345. This function returns a string representing the Emacs
  1346. @dfn{uptime}---the elapsed wall-clock time this instance of Emacs is
  1347. running. The string is formatted by @code{format-seconds} according
  1348. to the optional argument @var{format}. For the available format
  1349. descriptors, see @ref{Time Parsing, format-seconds}. If @var{format}
  1350. is @code{nil} or omitted, it defaults to @code{"%Y, %D, %H, %M,
  1351. %z%S"}.
  1352. When called interactively, it prints the uptime in the echo area.
  1353. @end deffn
  1354. @defun get-internal-run-time
  1355. This function returns the processor run time used by Emacs as a list
  1356. of four integers: @code{(@var{sec-high} @var{sec-low} @var{microsec}
  1357. @var{picosec})}, using the same format as @code{current-time}
  1358. (@pxref{Time of Day}).
  1359. Note that the time returned by this function excludes the time Emacs
  1360. was not using the processor, and if the Emacs process has several
  1361. threads, the returned value is the sum of the processor times used up
  1362. by all Emacs threads.
  1363. If the system doesn't provide a way to determine the processor run
  1364. time, @code{get-internal-run-time} returns the same time as
  1365. @code{current-time}.
  1366. @end defun
  1367. @deffn Command emacs-init-time
  1368. This function returns the duration of the Emacs initialization
  1369. (@pxref{Startup Summary}) in seconds, as a string. When called
  1370. interactively, it prints the duration in the echo area.
  1371. @end deffn
  1372. @node Time Calculations
  1373. @section Time Calculations
  1374. @cindex time calculations
  1375. @cindex comparing time values
  1376. @cindex calendrical computations
  1377. These functions perform calendrical computations using time values
  1378. (@pxref{Time of Day}).
  1379. @defun time-less-p t1 t2
  1380. This returns @code{t} if time value @var{t1} is less than time value
  1381. @var{t2}.
  1382. @end defun
  1383. @defun time-subtract t1 t2
  1384. This returns the time difference @var{t1} @minus{} @var{t2} between
  1385. two time values, as a time value.
  1386. @end defun
  1387. @defun time-add t1 t2
  1388. This returns the sum of two time values, as a time value.
  1389. One argument should represent a time difference rather than a point in time.
  1390. Here is how to add a number of seconds to a time value:
  1391. @example
  1392. (time-add @var{time} @var{seconds})
  1393. @end example
  1394. @end defun
  1395. @defun time-to-days time-value
  1396. This function returns the number of days between the beginning of year
  1397. 1 and @var{time-value}.
  1398. @end defun
  1399. @defun time-to-day-in-year time-value
  1400. This returns the day number within the year corresponding to @var{time-value}.
  1401. @end defun
  1402. @defun date-leap-year-p year
  1403. This function returns @code{t} if @var{year} is a leap year.
  1404. @end defun
  1405. @node Timers
  1406. @section Timers for Delayed Execution
  1407. @cindex timer
  1408. You can set up a @dfn{timer} to call a function at a specified
  1409. future time or after a certain length of idleness.
  1410. Emacs cannot run timers at any arbitrary point in a Lisp program; it
  1411. can run them only when Emacs could accept output from a subprocess:
  1412. namely, while waiting or inside certain primitive functions such as
  1413. @code{sit-for} or @code{read-event} which @emph{can} wait. Therefore, a
  1414. timer's execution may be delayed if Emacs is busy. However, the time of
  1415. execution is very precise if Emacs is idle.
  1416. Emacs binds @code{inhibit-quit} to @code{t} before calling the timer
  1417. function, because quitting out of many timer functions can leave
  1418. things in an inconsistent state. This is normally unproblematical
  1419. because most timer functions don't do a lot of work. Indeed, for a
  1420. timer to call a function that takes substantial time to run is likely
  1421. to be annoying. If a timer function needs to allow quitting, it
  1422. should use @code{with-local-quit} (@pxref{Quitting}). For example, if
  1423. a timer function calls @code{accept-process-output} to receive output
  1424. from an external process, that call should be wrapped inside
  1425. @code{with-local-quit}, to ensure that @kbd{C-g} works if the external
  1426. process hangs.
  1427. It is usually a bad idea for timer functions to alter buffer
  1428. contents. When they do, they usually should call @code{undo-boundary}
  1429. both before and after changing the buffer, to separate the timer's
  1430. changes from user commands' changes and prevent a single undo entry
  1431. from growing to be quite large.
  1432. Timer functions should also avoid calling functions that cause Emacs
  1433. to wait, such as @code{sit-for} (@pxref{Waiting}). This can lead to
  1434. unpredictable effects, since other timers (or even the same timer) can
  1435. run while waiting. If a timer function needs to perform an action
  1436. after a certain time has elapsed, it can do this by scheduling a new
  1437. timer.
  1438. If a timer function calls functions that can change the match data,
  1439. it should save and restore the match data. @xref{Saving Match Data}.
  1440. @deffn Command run-at-time time repeat function &rest args
  1441. This sets up a timer that calls the function @var{function} with
  1442. arguments @var{args} at time @var{time}. If @var{repeat} is a number
  1443. (integer or floating point), the timer is scheduled to run again every
  1444. @var{repeat} seconds after @var{time}. If @var{repeat} is @code{nil},
  1445. the timer runs only once.
  1446. @var{time} may specify an absolute or a relative time.
  1447. Absolute times may be specified using a string with a limited variety
  1448. of formats, and are taken to be times @emph{today}, even if already in
  1449. the past. The recognized forms are @samp{@var{xxxx}},
  1450. @samp{@var{x}:@var{xx}}, or @samp{@var{xx}:@var{xx}} (military time),
  1451. and @samp{@var{xx}am}, @samp{@var{xx}AM}, @samp{@var{xx}pm},
  1452. @samp{@var{xx}PM}, @samp{@var{xx}:@var{xx}am},
  1453. @samp{@var{xx}:@var{xx}AM}, @samp{@var{xx}:@var{xx}pm}, or
  1454. @samp{@var{xx}:@var{xx}PM}. A period can be used instead of a colon
  1455. to separate the hour and minute parts.
  1456. To specify a relative time as a string, use numbers followed by units.
  1457. For example:
  1458. @table @samp
  1459. @item 1 min
  1460. denotes 1 minute from now.
  1461. @item 1 min 5 sec
  1462. denotes 65 seconds from now.
  1463. @item 1 min 2 sec 3 hour 4 day 5 week 6 fortnight 7 month 8 year
  1464. denotes exactly 103 months, 123 days, and 10862 seconds from now.
  1465. @end table
  1466. For relative time values, Emacs considers a month to be exactly thirty
  1467. days, and a year to be exactly 365.25 days.
  1468. Not all convenient formats are strings. If @var{time} is a number
  1469. (integer or floating point), that specifies a relative time measured in
  1470. seconds. The result of @code{encode-time} can also be used to specify
  1471. an absolute value for @var{time}.
  1472. In most cases, @var{repeat} has no effect on when @emph{first} call
  1473. takes place---@var{time} alone specifies that. There is one exception:
  1474. if @var{time} is @code{t}, then the timer runs whenever the time is a
  1475. multiple of @var{repeat} seconds after the epoch. This is useful for
  1476. functions like @code{display-time}.
  1477. The function @code{run-at-time} returns a timer value that identifies
  1478. the particular scheduled future action. You can use this value to call
  1479. @code{cancel-timer} (see below).
  1480. @end deffn
  1481. A repeating timer nominally ought to run every @var{repeat} seconds,
  1482. but remember that any invocation of a timer can be late. Lateness of
  1483. one repetition has no effect on the scheduled time of the next
  1484. repetition. For instance, if Emacs is busy computing for long enough
  1485. to cover three scheduled repetitions of the timer, and then starts to
  1486. wait, it will immediately call the timer function three times in
  1487. immediate succession (presuming no other timers trigger before or
  1488. between them). If you want a timer to run again no less than @var{n}
  1489. seconds after the last invocation, don't use the @var{repeat} argument.
  1490. Instead, the timer function should explicitly reschedule the timer.
  1491. @defopt timer-max-repeats
  1492. This variable's value specifies the maximum number of times to repeat
  1493. calling a timer function in a row, when many previously scheduled
  1494. calls were unavoidably delayed.
  1495. @end defopt
  1496. @defmac with-timeout (seconds timeout-forms@dots{}) body@dots{}
  1497. Execute @var{body}, but give up after @var{seconds} seconds. If
  1498. @var{body} finishes before the time is up, @code{with-timeout} returns
  1499. the value of the last form in @var{body}. If, however, the execution of
  1500. @var{body} is cut short by the timeout, then @code{with-timeout}
  1501. executes all the @var{timeout-forms} and returns the value of the last
  1502. of them.
  1503. This macro works by setting a timer to run after @var{seconds} seconds. If
  1504. @var{body} finishes before that time, it cancels the timer. If the
  1505. timer actually runs, it terminates execution of @var{body}, then
  1506. executes @var{timeout-forms}.
  1507. Since timers can run within a Lisp program only when the program calls a
  1508. primitive that can wait, @code{with-timeout} cannot stop executing
  1509. @var{body} while it is in the midst of a computation---only when it
  1510. calls one of those primitives. So use @code{with-timeout} only with a
  1511. @var{body} that waits for input, not one that does a long computation.
  1512. @end defmac
  1513. The function @code{y-or-n-p-with-timeout} provides a simple way to use
  1514. a timer to avoid waiting too long for an answer. @xref{Yes-or-No
  1515. Queries}.
  1516. @defun cancel-timer timer
  1517. This cancels the requested action for @var{timer}, which should be a
  1518. timer---usually, one previously returned by @code{run-at-time} or
  1519. @code{run-with-idle-timer}. This cancels the effect of that call to
  1520. one of these functions; the arrival of the specified time will not
  1521. cause anything special to happen.
  1522. @end defun
  1523. @node Idle Timers
  1524. @section Idle Timers
  1525. @cindex idle timers
  1526. Here is how to set up a timer that runs when Emacs is idle for a
  1527. certain length of time. Aside from how to set them up, idle timers
  1528. work just like ordinary timers.
  1529. @deffn Command run-with-idle-timer secs repeat function &rest args
  1530. Set up a timer which runs the next time Emacs is idle for @var{secs}
  1531. seconds. The value of @var{secs} may be a number or a value of the type
  1532. returned by @code{current-idle-time}.
  1533. If @var{repeat} is @code{nil}, the timer runs just once, the first time
  1534. Emacs remains idle for a long enough time. More often @var{repeat} is
  1535. non-@code{nil}, which means to run the timer @emph{each time} Emacs
  1536. remains idle for @var{secs} seconds.
  1537. The function @code{run-with-idle-timer} returns a timer value which you
  1538. can use in calling @code{cancel-timer} (@pxref{Timers}).
  1539. @end deffn
  1540. @cindex idleness
  1541. Emacs becomes @dfn{idle} when it starts waiting for user input, and
  1542. it remains idle until the user provides some input. If a timer is set
  1543. for five seconds of idleness, it runs approximately five seconds after
  1544. Emacs first becomes idle. Even if @var{repeat} is non-@code{nil},
  1545. this timer will not run again as long as Emacs remains idle, because
  1546. the duration of idleness will continue to increase and will not go
  1547. down to five seconds again.
  1548. Emacs can do various things while idle: garbage collect, autosave or
  1549. handle data from a subprocess. But these interludes during idleness do
  1550. not interfere with idle timers, because they do not reset the clock of
  1551. idleness to zero. An idle timer set for 600 seconds will run when ten
  1552. minutes have elapsed since the last user command was finished, even if
  1553. subprocess output has been accepted thousands of times within those ten
  1554. minutes, and even if there have been garbage collections and autosaves.
  1555. When the user supplies input, Emacs becomes non-idle while executing the
  1556. input. Then it becomes idle again, and all the idle timers that are
  1557. set up to repeat will subsequently run another time, one by one.
  1558. Do not write an idle timer function containing a loop which does a
  1559. certain amount of processing each time around, and exits when
  1560. @code{(input-pending-p)} is non-@code{nil}. This approach seems very
  1561. natural but has two problems:
  1562. @itemize
  1563. @item
  1564. It blocks out all process output (since Emacs accepts process output
  1565. only while waiting).
  1566. @item
  1567. It blocks out any idle timers that ought to run during that time.
  1568. @end itemize
  1569. @noindent
  1570. Similarly, do not write an idle timer function that sets up another
  1571. idle timer (including the same idle timer) with @var{secs} argument
  1572. less than or equal to the current idleness time. Such a timer will
  1573. run almost immediately, and continue running again and again, instead
  1574. of waiting for the next time Emacs becomes idle. The correct approach
  1575. is to reschedule with an appropriate increment of the current value of
  1576. the idleness time, as described below.
  1577. @defun current-idle-time
  1578. If Emacs is idle, this function returns the length of time Emacs has
  1579. been idle, as a list of four integers: @code{(@var{sec-high}
  1580. @var{sec-low} @var{microsec} @var{picosec})}, using the same format as
  1581. @code{current-time} (@pxref{Time of Day}).
  1582. When Emacs is not idle, @code{current-idle-time} returns @code{nil}.
  1583. This is a convenient way to test whether Emacs is idle.
  1584. @end defun
  1585. The main use of @code{current-idle-time} is when an idle timer
  1586. function wants to ``take a break'' for a while. It can set up another
  1587. idle timer to call the same function again, after a few seconds more
  1588. idleness. Here's an example:
  1589. @example
  1590. (defvar my-resume-timer nil
  1591. "Timer for `my-timer-function' to reschedule itself, or nil.")
  1592. (defun my-timer-function ()
  1593. ;; @r{If the user types a command while @code{my-resume-timer}}
  1594. ;; @r{is active, the next time this function is called from}
  1595. ;; @r{its main idle timer, deactivate @code{my-resume-timer}.}
  1596. (when my-resume-timer
  1597. (cancel-timer my-resume-timer))
  1598. ...@var{do the work for a while}...
  1599. (when @var{taking-a-break}
  1600. (setq my-resume-timer
  1601. (run-with-idle-timer
  1602. ;; Compute an idle time @var{break-length}
  1603. ;; more than the current value.
  1604. (time-add (current-idle-time) @var{break-length})
  1605. nil
  1606. 'my-timer-function))))
  1607. @end example
  1608. @node Terminal Input
  1609. @section Terminal Input
  1610. @cindex terminal input
  1611. This section describes functions and variables for recording or
  1612. manipulating terminal input. See @ref{Display}, for related
  1613. functions.
  1614. @menu
  1615. * Input Modes:: Options for how input is processed.
  1616. * Recording Input:: Saving histories of recent or all input events.
  1617. @end menu
  1618. @node Input Modes
  1619. @subsection Input Modes
  1620. @cindex input modes
  1621. @cindex terminal input modes
  1622. @defun set-input-mode interrupt flow meta &optional quit-char
  1623. This function sets the mode for reading keyboard input. If
  1624. @var{interrupt} is non-@code{nil}, then Emacs uses input interrupts.
  1625. If it is @code{nil}, then it uses @sc{cbreak} mode. The default
  1626. setting is system-dependent. Some systems always use @sc{cbreak} mode
  1627. regardless of what is specified.
  1628. When Emacs communicates directly with X, it ignores this argument and
  1629. uses interrupts if that is the way it knows how to communicate.
  1630. If @var{flow} is non-@code{nil}, then Emacs uses @sc{xon/xoff}
  1631. (@kbd{C-q}, @kbd{C-s}) flow control for output to the terminal. This
  1632. has no effect except in @sc{cbreak} mode.
  1633. The argument @var{meta} controls support for input character codes
  1634. above 127. If @var{meta} is @code{t}, Emacs converts characters with
  1635. the 8th bit set into Meta characters. If @var{meta} is @code{nil},
  1636. Emacs disregards the 8th bit; this is necessary when the terminal uses
  1637. it as a parity bit. If @var{meta} is neither @code{t} nor @code{nil},
  1638. Emacs uses all 8 bits of input unchanged. This is good for terminals
  1639. that use 8-bit character sets.
  1640. If @var{quit-char} is non-@code{nil}, it specifies the character to
  1641. use for quitting. Normally this character is @kbd{C-g}.
  1642. @xref{Quitting}.
  1643. @end defun
  1644. The @code{current-input-mode} function returns the input mode settings
  1645. Emacs is currently using.
  1646. @defun current-input-mode
  1647. This function returns the current mode for reading keyboard input. It
  1648. returns a list, corresponding to the arguments of @code{set-input-mode},
  1649. of the form @code{(@var{interrupt} @var{flow} @var{meta} @var{quit})} in
  1650. which:
  1651. @table @var
  1652. @item interrupt
  1653. is non-@code{nil} when Emacs is using interrupt-driven input. If
  1654. @code{nil}, Emacs is using @sc{cbreak} mode.
  1655. @item flow
  1656. is non-@code{nil} if Emacs uses @sc{xon/xoff} (@kbd{C-q}, @kbd{C-s})
  1657. flow control for output to the terminal. This value is meaningful only
  1658. when @var{interrupt} is @code{nil}.
  1659. @item meta
  1660. is @code{t} if Emacs treats the eighth bit of input characters as
  1661. the meta bit; @code{nil} means Emacs clears the eighth bit of every
  1662. input character; any other value means Emacs uses all eight bits as the
  1663. basic character code.
  1664. @item quit
  1665. is the character Emacs currently uses for quitting, usually @kbd{C-g}.
  1666. @end table
  1667. @end defun
  1668. @node Recording Input
  1669. @subsection Recording Input
  1670. @cindex recording input
  1671. @defun recent-keys
  1672. This function returns a vector containing the last 300 input events from
  1673. the keyboard or mouse. All input events are included, whether or not
  1674. they were used as parts of key sequences. Thus, you always get the last
  1675. 300 input events, not counting events generated by keyboard macros.
  1676. (These are excluded because they are less interesting for debugging; it
  1677. should be enough to see the events that invoked the macros.)
  1678. A call to @code{clear-this-command-keys} (@pxref{Command Loop Info})
  1679. causes this function to return an empty vector immediately afterward.
  1680. @end defun
  1681. @deffn Command open-dribble-file filename
  1682. @cindex dribble file
  1683. This function opens a @dfn{dribble file} named @var{filename}. When a
  1684. dribble file is open, each input event from the keyboard or mouse (but
  1685. not those from keyboard macros) is written in that file. A
  1686. non-character event is expressed using its printed representation
  1687. surrounded by @samp{<@dots{}>}. Be aware that sensitive information
  1688. (such as passwords) may end up recorded in the dribble file.
  1689. You close the dribble file by calling this function with an argument
  1690. of @code{nil}.
  1691. @end deffn
  1692. See also the @code{open-termscript} function (@pxref{Terminal Output}).
  1693. @node Terminal Output
  1694. @section Terminal Output
  1695. @cindex terminal output
  1696. The terminal output functions send output to a text terminal, or keep
  1697. track of output sent to the terminal. The variable @code{baud-rate}
  1698. tells you what Emacs thinks is the output speed of the terminal.
  1699. @defopt baud-rate
  1700. This variable's value is the output speed of the terminal, as far as
  1701. Emacs knows. Setting this variable does not change the speed of actual
  1702. data transmission, but the value is used for calculations such as
  1703. padding.
  1704. It also affects decisions about whether to scroll part of the
  1705. screen or repaint on text terminals. @xref{Forcing Redisplay},
  1706. for the corresponding functionality on graphical terminals.
  1707. The value is measured in baud.
  1708. @end defopt
  1709. If you are running across a network, and different parts of the
  1710. network work at different baud rates, the value returned by Emacs may be
  1711. different from the value used by your local terminal. Some network
  1712. protocols communicate the local terminal speed to the remote machine, so
  1713. that Emacs and other programs can get the proper value, but others do
  1714. not. If Emacs has the wrong value, it makes decisions that are less
  1715. than optimal. To fix the problem, set @code{baud-rate}.
  1716. @defun send-string-to-terminal string &optional terminal
  1717. This function sends @var{string} to @var{terminal} without alteration.
  1718. Control characters in @var{string} have terminal-dependent effects.
  1719. This function operates only on text terminals. @var{terminal} may be
  1720. a terminal object, a frame, or @code{nil} for the selected frame's
  1721. terminal. In batch mode, @var{string} is sent to @code{stdout} when
  1722. @var{terminal} is @code{nil}.
  1723. One use of this function is to define function keys on terminals that
  1724. have downloadable function key definitions. For example, this is how (on
  1725. certain terminals) to define function key 4 to move forward four
  1726. characters (by transmitting the characters @kbd{C-u C-f} to the
  1727. computer):
  1728. @example
  1729. @group
  1730. (send-string-to-terminal "\eF4\^U\^F")
  1731. @result{} nil
  1732. @end group
  1733. @end example
  1734. @end defun
  1735. @deffn Command open-termscript filename
  1736. @cindex termscript file
  1737. This function is used to open a @dfn{termscript file} that will record
  1738. all the characters sent by Emacs to the terminal. It returns
  1739. @code{nil}. Termscript files are useful for investigating problems
  1740. where Emacs garbles the screen, problems that are due to incorrect
  1741. Termcap entries or to undesirable settings of terminal options more
  1742. often than to actual Emacs bugs. Once you are certain which characters
  1743. were actually output, you can determine reliably whether they correspond
  1744. to the Termcap specifications in use.
  1745. @example
  1746. @group
  1747. (open-termscript "../junk/termscript")
  1748. @result{} nil
  1749. @end group
  1750. @end example
  1751. You close the termscript file by calling this function with an
  1752. argument of @code{nil}.
  1753. See also @code{open-dribble-file} in @ref{Recording Input}.
  1754. @end deffn
  1755. @node Sound Output
  1756. @section Sound Output
  1757. @cindex sound
  1758. To play sound using Emacs, use the function @code{play-sound}. Only
  1759. certain systems are supported; if you call @code{play-sound} on a
  1760. system which cannot really do the job, it gives an error.
  1761. @c FIXME: Add indexes for Au and WAV? --xfq
  1762. The sound must be stored as a file in RIFF-WAVE format (@samp{.wav})
  1763. or Sun Audio format (@samp{.au}).
  1764. @defun play-sound sound
  1765. This function plays a specified sound. The argument, @var{sound}, has
  1766. the form @code{(sound @var{properties}...)}, where the @var{properties}
  1767. consist of alternating keywords (particular symbols recognized
  1768. specially) and values corresponding to them.
  1769. Here is a table of the keywords that are currently meaningful in
  1770. @var{sound}, and their meanings:
  1771. @table @code
  1772. @item :file @var{file}
  1773. This specifies the file containing the sound to play.
  1774. If the file name is not absolute, it is expanded against
  1775. the directory @code{data-directory}.
  1776. @item :data @var{data}
  1777. This specifies the sound to play without need to refer to a file. The
  1778. value, @var{data}, should be a string containing the same bytes as a
  1779. sound file. We recommend using a unibyte string.
  1780. @item :volume @var{volume}
  1781. This specifies how loud to play the sound. It should be a number in the
  1782. range of 0 to 1. The default is to use whatever volume has been
  1783. specified before.
  1784. @item :device @var{device}
  1785. This specifies the system device on which to play the sound, as a
  1786. string. The default device is system-dependent.
  1787. @end table
  1788. Before actually playing the sound, @code{play-sound}
  1789. calls the functions in the list @code{play-sound-functions}.
  1790. Each function is called with one argument, @var{sound}.
  1791. @end defun
  1792. @deffn Command play-sound-file file &optional volume device
  1793. This function is an alternative interface to playing a sound @var{file}
  1794. specifying an optional @var{volume} and @var{device}.
  1795. @end deffn
  1796. @defvar play-sound-functions
  1797. A list of functions to be called before playing a sound. Each function
  1798. is called with one argument, a property list that describes the sound.
  1799. @end defvar
  1800. @node X11 Keysyms
  1801. @section Operating on X11 Keysyms
  1802. @cindex X11 keysyms
  1803. To define system-specific X11 keysyms, set the variable
  1804. @code{system-key-alist}.
  1805. @defvar system-key-alist
  1806. This variable's value should be an alist with one element for each
  1807. system-specific keysym. Each element has the form @code{(@var{code}
  1808. . @var{symbol})}, where @var{code} is the numeric keysym code (not
  1809. including the ``vendor specific'' bit,
  1810. @ifnottex
  1811. @minus{}2**28),
  1812. @end ifnottex
  1813. @tex
  1814. $-2^{28}$),
  1815. @end tex
  1816. and @var{symbol} is the name for the function key.
  1817. For example @code{(168 . mute-acute)} defines a system-specific key (used
  1818. by HP X servers) whose numeric code is
  1819. @ifnottex
  1820. @minus{}2**28
  1821. @end ifnottex
  1822. @tex
  1823. $-2^{28}$
  1824. @end tex
  1825. + 168.
  1826. It is not crucial to exclude from the alist the keysyms of other X
  1827. servers; those do no harm, as long as they don't conflict with the ones
  1828. used by the X server actually in use.
  1829. The variable is always local to the current terminal, and cannot be
  1830. buffer-local. @xref{Multiple Terminals}.
  1831. @end defvar
  1832. You can specify which keysyms Emacs should use for the Meta, Alt, Hyper, and Super modifiers by setting these variables:
  1833. @defvar x-alt-keysym
  1834. @defvarx x-meta-keysym
  1835. @defvarx x-hyper-keysym
  1836. @defvarx x-super-keysym
  1837. The name of the keysym that should stand for the Alt modifier
  1838. (respectively, for Meta, Hyper, and Super). For example, here is
  1839. how to swap the Meta and Alt modifiers within Emacs:
  1840. @lisp
  1841. (setq x-alt-keysym 'meta)
  1842. (setq x-meta-keysym 'alt)
  1843. @end lisp
  1844. @end defvar
  1845. @node Batch Mode
  1846. @section Batch Mode
  1847. @cindex batch mode
  1848. The command-line option @samp{-batch} causes Emacs to run
  1849. noninteractively. In this mode, Emacs does not read commands from the
  1850. terminal, it does not alter the terminal modes, and it does not expect
  1851. to be outputting to an erasable screen. The idea is that you specify
  1852. Lisp programs to run; when they are finished, Emacs should exit. The
  1853. way to specify the programs to run is with @samp{-l @var{file}}, which
  1854. loads the library named @var{file}, or @samp{-f @var{function}}, which
  1855. calls @var{function} with no arguments, or @samp{--eval @var{form}}.
  1856. Any Lisp program output that would normally go to the echo area,
  1857. either using @code{message}, or using @code{prin1}, etc., with @code{t}
  1858. as the stream, goes instead to Emacs's standard error descriptor when
  1859. in batch mode. Similarly, input that would normally come from the
  1860. minibuffer is read from the standard input descriptor.
  1861. Thus, Emacs behaves much like a noninteractive
  1862. application program. (The echo area output that Emacs itself normally
  1863. generates, such as command echoing, is suppressed entirely.)
  1864. @defvar noninteractive
  1865. This variable is non-@code{nil} when Emacs is running in batch mode.
  1866. @end defvar
  1867. @node Session Management
  1868. @section Session Management
  1869. @cindex session manager
  1870. Emacs supports the X Session Management Protocol, which is used to
  1871. suspend and restart applications. In the X Window System, a program
  1872. called the @dfn{session manager} is responsible for keeping track of
  1873. the applications that are running. When the X server shuts down, the
  1874. session manager asks applications to save their state, and delays the
  1875. actual shutdown until they respond. An application can also cancel
  1876. the shutdown.
  1877. When the session manager restarts a suspended session, it directs
  1878. these applications to individually reload their saved state. It does
  1879. this by specifying a special command-line argument that says what
  1880. saved session to restore. For Emacs, this argument is @samp{--smid
  1881. @var{session}}.
  1882. @defvar emacs-save-session-functions
  1883. @cindex session file
  1884. Emacs supports saving state via a hook called
  1885. @code{emacs-save-session-functions}. Emacs runs this hook when the
  1886. session manager tells it that the window system is shutting down. The
  1887. functions are called with no arguments, and with the current buffer
  1888. set to a temporary buffer. Each function can use @code{insert} to add
  1889. Lisp code to this buffer. At the end, Emacs saves the buffer in a
  1890. file, called the @dfn{session file}.
  1891. @findex emacs-session-restore
  1892. Subsequently, when the session manager restarts Emacs, it loads the
  1893. session file automatically (@pxref{Loading}). This is performed by a
  1894. function named @code{emacs-session-restore}, which is called during
  1895. startup. @xref{Startup Summary}.
  1896. If a function in @code{emacs-save-session-functions} returns
  1897. non-@code{nil}, Emacs tells the session manager to cancel the
  1898. shutdown.
  1899. @end defvar
  1900. Here is an example that just inserts some text into @file{*scratch*} when
  1901. Emacs is restarted by the session manager.
  1902. @example
  1903. @group
  1904. (add-hook 'emacs-save-session-functions 'save-yourself-test)
  1905. @end group
  1906. @group
  1907. (defun save-yourself-test ()
  1908. (insert "(save-current-buffer
  1909. (switch-to-buffer \"*scratch*\")
  1910. (insert \"I am restored\"))")
  1911. nil)
  1912. @end group
  1913. @end example
  1914. @node Desktop Notifications
  1915. @section Desktop Notifications
  1916. @cindex desktop notifications
  1917. @cindex notifications, on desktop
  1918. Emacs is able to send @dfn{notifications} on systems that support the
  1919. freedesktop.org Desktop Notifications Specification. In order to use
  1920. this functionality, Emacs must have been compiled with D-Bus support,
  1921. and the @code{notifications} library must be loaded. @xref{Top, ,
  1922. D-Bus,dbus,D-Bus integration in Emacs}.
  1923. @defun notifications-notify &rest params
  1924. This function sends a notification to the desktop via D-Bus,
  1925. consisting of the parameters specified by the @var{params} arguments.
  1926. These arguments should consist of alternating keyword and value pairs.
  1927. The supported keywords and values are as follows:
  1928. @table @code
  1929. @item :bus @var{bus}
  1930. The D-Bus bus. This argument is needed only if a bus other than
  1931. @code{:session} shall be used.
  1932. @item :title @var{title}
  1933. The notification title.
  1934. @item :body @var{text}
  1935. The notification body text. Depending on the implementation of the
  1936. notification server, the text could contain HTML markups, like
  1937. @samp{"<b>bold text</b>"}, hyperlinks, or images. Special HTML
  1938. characters must be encoded, as @samp{"Contact
  1939. &lt;postmaster@@localhost&gt;!"}.
  1940. @item :app-name @var{name}
  1941. The name of the application sending the notification. The default is
  1942. @code{notifications-application-name}.
  1943. @item :replaces-id @var{id}
  1944. The notification @var{id} that this notification replaces. @var{id}
  1945. must be the result of a previous @code{notifications-notify} call.
  1946. @item :app-icon @var{icon-file}
  1947. The file name of the notification icon. If set to @code{nil}, no icon
  1948. is displayed. The default is @code{notifications-application-icon}.
  1949. @item :actions (@var{key} @var{title} @var{key} @var{title} ...)
  1950. A list of actions to be applied. @var{key} and @var{title} are both
  1951. strings. The default action (usually invoked by clicking the
  1952. notification) should have a key named @samp{"default"}. The title can
  1953. be anything, though implementations are free not to display it.
  1954. @item :timeout @var{timeout}
  1955. The timeout time in milliseconds since the display of the notification
  1956. at which the notification should automatically close. If @minus{}1, the
  1957. notification's expiration time is dependent on the notification
  1958. server's settings, and may vary for the type of notification. If 0,
  1959. the notification never expires. Default value is @minus{}1.
  1960. @item :urgency @var{urgency}
  1961. The urgency level. It can be @code{low}, @code{normal}, or @code{critical}.
  1962. @item :action-items
  1963. When this keyword is given, the @var{title} string of the actions is
  1964. interpreted as icon name.
  1965. @item :category @var{category}
  1966. The type of notification this is, a string. See the
  1967. @uref{http://developer.gnome.org/notification-spec/#categories,
  1968. Desktop Notifications Specification} for a list of standard
  1969. categories.
  1970. @item :desktop-entry @var{filename}
  1971. This specifies the name of the desktop filename representing the
  1972. calling program, like @samp{"emacs"}.
  1973. @item :image-data (@var{width} @var{height} @var{rowstride} @var{has-alpha} @var{bits} @var{channels} @var{data})
  1974. This is a raw data image format that describes the width, height,
  1975. rowstride, whether there is an alpha channel, bits per sample,
  1976. channels and image data, respectively.
  1977. @item :image-path @var{path}
  1978. This is represented either as a URI (@samp{file://} is the only URI
  1979. schema supported right now) or a name in a freedesktop.org-compliant
  1980. icon theme from @samp{$XDG_DATA_DIRS/icons}.
  1981. @item :sound-file @var{filename}
  1982. The path to a sound file to play when the notification pops up.
  1983. @item :sound-name @var{name}
  1984. A themable named sound from the freedesktop.org sound naming
  1985. specification from @samp{$XDG_DATA_DIRS/sounds}, to play when the
  1986. notification pops up. Similar to the icon name, only for sounds. An
  1987. example would be @samp{"message-new-instant"}.
  1988. @item :suppress-sound
  1989. Causes the server to suppress playing any sounds, if it has that
  1990. ability.
  1991. @item :resident
  1992. When set the server will not automatically remove the notification
  1993. when an action has been invoked. The notification will remain resident
  1994. in the server until it is explicitly removed by the user or by the
  1995. sender. This hint is likely only useful when the server has the
  1996. @code{:persistence} capability.
  1997. @item :transient
  1998. When set the server will treat the notification as transient and
  1999. by-pass the server's persistence capability, if it should exist.
  2000. @item :x @var{position}
  2001. @itemx :y @var{position}
  2002. Specifies the X, Y location on the screen that the
  2003. notification should point to. Both arguments must be used together.
  2004. @item :on-action @var{function}
  2005. Function to call when an action is invoked. The notification @var{id}
  2006. and the @var{key} of the action are passed as arguments to the
  2007. function.
  2008. @item :on-close @var{function}
  2009. Function to call when the notification has been closed by timeout or
  2010. by the user. The function receive the notification @var{id} and the closing
  2011. @var{reason} as arguments:
  2012. @itemize
  2013. @item @code{expired} if the notification has expired
  2014. @item @code{dismissed} if the notification was dismissed by the user
  2015. @item @code{close-notification} if the notification was closed by a call to
  2016. @code{notifications-close-notification}
  2017. @item @code{undefined} if the notification server hasn't provided a reason
  2018. @end itemize
  2019. @end table
  2020. Which parameters are accepted by the notification server can be
  2021. checked via @code{notifications-get-capabilities}.
  2022. This function returns a notification id, an integer, which can be used
  2023. to manipulate the notification item with
  2024. @code{notifications-close-notification} or the @code{:replaces-id}
  2025. argument of another @code{notifications-notify} call. For example:
  2026. @example
  2027. @group
  2028. (defun my-on-action-function (id key)
  2029. (message "Message %d, key \"%s\" pressed" id key))
  2030. @result{} my-on-action-function
  2031. @end group
  2032. @group
  2033. (defun my-on-close-function (id reason)
  2034. (message "Message %d, closed due to \"%s\"" id reason))
  2035. @result{} my-on-close-function
  2036. @end group
  2037. @group
  2038. (notifications-notify
  2039. :title "Title"
  2040. :body "This is <b>important</b>."
  2041. :actions '("Confirm" "I agree" "Refuse" "I disagree")
  2042. :on-action 'my-on-action-function
  2043. :on-close 'my-on-close-function)
  2044. @result{} 22
  2045. @end group
  2046. @group
  2047. A message window opens on the desktop. Press "I agree"
  2048. @result{} Message 22, key "Confirm" pressed
  2049. Message 22, closed due to "dismissed"
  2050. @end group
  2051. @end example
  2052. @end defun
  2053. @defun notifications-close-notification id &optional bus
  2054. This function closes a notification with identifier @var{id}.
  2055. @var{bus} can be a string denoting a D-Bus connection, the default is
  2056. @code{:session}.
  2057. @end defun
  2058. @defun notifications-get-capabilities &optional bus
  2059. Returns the capabilities of the notification server, a list of
  2060. symbols. @var{bus} can be a string denoting a D-Bus connection, the
  2061. default is @code{:session}. The following capabilities can be
  2062. expected:
  2063. @table @code
  2064. @item :actions
  2065. The server will provide the specified actions to the user.
  2066. @item :body
  2067. Supports body text.
  2068. @item :body-hyperlinks
  2069. The server supports hyperlinks in the notifications.
  2070. @item :body-images
  2071. The server supports images in the notifications.
  2072. @item :body-markup
  2073. Supports markup in the body text.
  2074. @item :icon-multi
  2075. The server will render an animation of all the frames in a given image
  2076. array.
  2077. @item :icon-static
  2078. Supports display of exactly 1 frame of any given image array. This
  2079. value is mutually exclusive with @code{:icon-multi}.
  2080. @item :persistence
  2081. The server supports persistence of notifications.
  2082. @item :sound
  2083. The server supports sounds on notifications.
  2084. @end table
  2085. Further vendor-specific caps start with @code{:x-vendor}, like
  2086. @code{:x-gnome-foo-cap}.
  2087. @end defun
  2088. @defun notifications-get-server-information &optional bus
  2089. Return information on the notification server, a list of strings.
  2090. @var{bus} can be a string denoting a D-Bus connection, the default is
  2091. @code{:session}. The returned list is @code{(@var{name} @var{vendor}
  2092. @var{version} @var{spec-version})}.
  2093. @table @var
  2094. @item name
  2095. The product name of the server.
  2096. @item vendor
  2097. The vendor name. For example, @samp{"KDE"}, @samp{"GNOME"}.
  2098. @item version
  2099. The server's version number.
  2100. @item spec-version
  2101. The specification version the server is compliant with.
  2102. @end table
  2103. If @var{spec_version} is @code{nil}, the server supports a
  2104. specification prior to @samp{"1.0"}.
  2105. @end defun
  2106. @node File Notifications
  2107. @section Notifications on File Changes
  2108. @cindex file notifications
  2109. @cindex watch, for filesystem events
  2110. Several operating systems support watching of filesystems for changes
  2111. of files. If configured properly, Emacs links a respective library
  2112. like @file{gfilenotify}, @file{inotify}, or @file{w32notify}
  2113. statically. These libraries enable watching of filesystems on the
  2114. local machine.
  2115. It is also possible to watch filesystems on remote machines,
  2116. @pxref{Remote Files,, Remote Files, emacs, The GNU Emacs Manual}
  2117. This does not depend on one of the libraries linked to Emacs.
  2118. Since all these libraries emit different events on notified file
  2119. changes, there is the Emacs library @code{filenotify} which provides a
  2120. unique interface.
  2121. @defun file-notify-add-watch file flags callback
  2122. Add a watch for filesystem events pertaining to @var{file}. This
  2123. arranges for filesystem events pertaining to @var{file} to be reported
  2124. to Emacs.
  2125. The returned value is a descriptor for the added watch. Its type
  2126. depends on the underlying library, it cannot be assumed to be an
  2127. integer as in the example below. It should be used for comparison by
  2128. @code{equal} only.
  2129. If the @var{file} cannot be watched for some reason, this function
  2130. signals a @code{file-notify-error} error.
  2131. Sometimes, mounted filesystems cannot be watched for file changes.
  2132. This is not detected by this function, a non-@code{nil} return value
  2133. does not guarantee that changes on @var{file} will be notified.
  2134. @var{flags} is a list of conditions to set what will be watched for.
  2135. It can include the following symbols:
  2136. @table @code
  2137. @item change
  2138. watch for file changes
  2139. @item attribute-change
  2140. watch for file attribute changes, like permissions or modification
  2141. time
  2142. @end table
  2143. If @var{file} is a directory, changes for all files in that directory
  2144. will be notified. This does not work recursively.
  2145. When any event happens, Emacs will call the @var{callback} function
  2146. passing it a single argument @var{event}, which is of the form
  2147. @lisp
  2148. (@var{descriptor} @var{action} @var{file} [@var{file1}])
  2149. @end lisp
  2150. @var{descriptor} is the same object as the one returned by this
  2151. function. @var{action} is the description of the event. It could be
  2152. any one of the following symbols:
  2153. @table @code
  2154. @item created
  2155. @var{file} was created
  2156. @item deleted
  2157. @var{file} was deleted
  2158. @item changed
  2159. @var{file} has changed
  2160. @item renamed
  2161. @var{file} has been renamed to @var{file1}
  2162. @item attribute-changed
  2163. a @var{file} attribute was changed
  2164. @end table
  2165. @var{file} and @var{file1} are the name of the file(s) whose event is
  2166. being reported. For example:
  2167. @example
  2168. @group
  2169. (require 'filenotify)
  2170. @result{} filenotify
  2171. @end group
  2172. @group
  2173. (defun my-notify-callback (event)
  2174. (message "Event %S" event))
  2175. @result{} my-notify-callback
  2176. @end group
  2177. @group
  2178. (file-notify-add-watch
  2179. "/tmp" '(change attribute-change) 'my-notify-callback)
  2180. @result{} 35025468
  2181. @end group
  2182. @group
  2183. (write-region "foo" nil "/tmp/foo")
  2184. @result{} Event (35025468 created "/tmp/.#foo")
  2185. Event (35025468 created "/tmp/foo")
  2186. Event (35025468 changed "/tmp/foo")
  2187. Event (35025468 deleted "/tmp/.#foo")
  2188. @end group
  2189. @group
  2190. (write-region "bla" nil "/tmp/foo")
  2191. @result{} Event (35025468 created "/tmp/.#foo")
  2192. Event (35025468 changed "/tmp/foo") [2 times]
  2193. Event (35025468 deleted "/tmp/.#foo")
  2194. @end group
  2195. @group
  2196. (set-file-modes "/tmp/foo" (default-file-modes))
  2197. @result{} Event (35025468 attribute-changed "/tmp/foo")
  2198. @end group
  2199. @end example
  2200. Whether the action @code{renamed} is returned, depends on the used
  2201. watch library. It can be expected, when a directory is watched, and
  2202. both @var{file} and @var{file1} belong to this directory. Otherwise,
  2203. the actions @code{deleted} and @code{created} could be returned in a
  2204. random order.
  2205. @example
  2206. @group
  2207. (rename-file "/tmp/foo" "/tmp/bla")
  2208. @result{} Event (35025468 renamed "/tmp/foo" "/tmp/bla")
  2209. @end group
  2210. @group
  2211. (file-notify-add-watch
  2212. "/var/tmp" '(change attribute-change) 'my-notify-callback)
  2213. @result{} 35025504
  2214. @end group
  2215. @group
  2216. (rename-file "/tmp/bla" "/var/tmp/bla")
  2217. @result{} ;; gfilenotify
  2218. Event (35025468 renamed "/tmp/bla" "/var/tmp/bla")
  2219. @result{} ;; inotify
  2220. Event (35025504 created "/var/tmp/bla")
  2221. Event (35025468 deleted "/tmp/bla")
  2222. @end group
  2223. @end example
  2224. @end defun
  2225. @defun file-notify-rm-watch descriptor
  2226. Removes an existing file watch specified by its @var{descriptor}.
  2227. @var{descriptor} should be an object returned by
  2228. @code{file-notify-add-watch}.
  2229. @end defun
  2230. @node Dynamic Libraries
  2231. @section Dynamically Loaded Libraries
  2232. @cindex dynamic libraries
  2233. A @dfn{dynamically loaded library} is a library that is loaded on
  2234. demand, when its facilities are first needed. Emacs supports such
  2235. on-demand loading of support libraries for some of its features.
  2236. @defvar dynamic-library-alist
  2237. This is an alist of dynamic libraries and external library files
  2238. implementing them.
  2239. Each element is a list of the form
  2240. @w{@code{(@var{library} @var{files}@dots{})}}, where the @code{car} is
  2241. a symbol representing a supported external library, and the rest are
  2242. strings giving alternate filenames for that library.
  2243. Emacs tries to load the library from the files in the order they
  2244. appear in the list; if none is found, the Emacs session won't have
  2245. access to that library, and the features it provides will be
  2246. unavailable.
  2247. Image support on some platforms uses this facility. Here's an example
  2248. of setting this variable for supporting images on MS-Windows:
  2249. @example
  2250. (setq dynamic-library-alist
  2251. '((xpm "libxpm.dll" "xpm4.dll" "libXpm-nox4.dll")
  2252. (png "libpng12d.dll" "libpng12.dll" "libpng.dll"
  2253. "libpng13d.dll" "libpng13.dll")
  2254. (jpeg "jpeg62.dll" "libjpeg.dll" "jpeg-62.dll"
  2255. "jpeg.dll")
  2256. (tiff "libtiff3.dll" "libtiff.dll")
  2257. (gif "giflib4.dll" "libungif4.dll" "libungif.dll")
  2258. (svg "librsvg-2-2.dll")
  2259. (gdk-pixbuf "libgdk_pixbuf-2.0-0.dll")
  2260. (glib "libglib-2.0-0.dll")
  2261. (gobject "libgobject-2.0-0.dll")))
  2262. @end example
  2263. Note that image types @code{pbm} and @code{xbm} do not need entries in
  2264. this variable because they do not depend on external libraries and are
  2265. always available in Emacs.
  2266. Also note that this variable is not meant to be a generic facility for
  2267. accessing external libraries; only those already known by Emacs can
  2268. be loaded through it.
  2269. This variable is ignored if the given @var{library} is statically
  2270. linked into Emacs.
  2271. @end defvar