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