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