os.texi 100 KB
Newer Older
Glenn Morris's avatar
Glenn Morris committed
1 2
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
3 4
@c Copyright (C) 1990-1995, 1998-1999, 2001-2013 Free Software
@c Foundation, Inc.
Glenn Morris's avatar
Glenn Morris committed
5
@c See the file elisp.texi for copying conditions.
6
@node System Interface
Glenn Morris's avatar
Glenn Morris committed
7 8 9
@chapter Operating System Interface

  This chapter is about starting and getting out of Emacs, access to
Glenn Morris's avatar
Glenn Morris committed
10
values in the operating system environment, and terminal input, output.
Glenn Morris's avatar
Glenn Morris committed
11

12 13 14
  @xref{Building Emacs}, for related information.  @xref{Display}, for
additional operating system status information pertaining to the
terminal and the screen.
Glenn Morris's avatar
Glenn Morris committed
15 16 17 18 19 20

@menu
* Starting Up::         Customizing Emacs startup processing.
* Getting Out::         How exiting works (permanent or temporary).
* System Environment::  Distinguish the name and kind of system.
* User Identification:: Finding the name and user id of the user.
Glenn Morris's avatar
Glenn Morris committed
21
* Time of Day::         Getting the current time.
22
* Time Conversion::     Converting a time from numeric form to
23
                          calendrical data and vice versa.
Glenn Morris's avatar
Glenn Morris committed
24 25 26 27
* Time Parsing::        Converting a time from numeric form to text
                          and vice versa.
* Processor Run Time::  Getting the run time used by Emacs.
* Time Calculations::   Adding, subtracting, comparing times, etc.
Glenn Morris's avatar
Glenn Morris committed
28
* Timers::              Setting a timer to call a function at a certain time.
Glenn Morris's avatar
Glenn Morris committed
29 30 31 32 33
* Idle Timers::         Setting a timer to call a function when Emacs has
                          been idle for a certain length of time.
* Terminal Input::      Accessing and recording terminal input.
* Terminal Output::     Controlling and recording terminal output.
* Sound Output::        Playing sounds on the computer's speaker.
34
* X11 Keysyms::         Operating on key symbols for X Windows.
Glenn Morris's avatar
Glenn Morris committed
35 36
* Batch Mode::          Running Emacs without terminal interaction.
* Session Management::  Saving and restoring state with X Session Management.
37 38
* Desktop Notifications:: Desktop notifications.
* File Notifications::  File notifications.
39
* Dynamic Libraries::   On-demand loading of support libraries.
Glenn Morris's avatar
Glenn Morris committed
40 41 42 43 44 45 46 47 48 49
@end menu

@node Starting Up
@section Starting Up Emacs

  This section describes what Emacs does when it is started, and how you
can customize these actions.

@menu
* Startup Summary::         Sequence of actions Emacs performs at startup.
50
* Init File::               Details on reading the init file.
Glenn Morris's avatar
Glenn Morris committed
51 52 53 54 55 56 57 58 59 60 61
* Terminal-Specific::       How the terminal-specific Lisp file is read.
* Command-Line Arguments::  How command-line arguments are processed,
                              and how you can customize them.
@end menu

@node Startup Summary
@subsection Summary: Sequence of Actions at Startup
@cindex initialization of Emacs
@cindex startup of Emacs
@cindex @file{startup.el}

62
  When Emacs is started up, it performs the following operations
Glenn Morris's avatar
Glenn Morris committed
63
(see @code{normal-top-level} in @file{startup.el}):
Glenn Morris's avatar
Glenn Morris committed
64 65 66 67

@enumerate
@item
It adds subdirectories to @code{load-path}, by running the file named
68 69 70 71
@file{subdirs.el} in each directory in the list.  Normally, this file
adds the directory's subdirectories to the list, and those are scanned
in their turn.  The files @file{subdirs.el} are normally generated
automatically when Emacs is installed.
Glenn Morris's avatar
Glenn Morris committed
72

Glenn Morris's avatar
Glenn Morris committed
73
@item
74 75 76 77 78
If the library @file{leim-list.el} exists, Emacs loads it.  This
optional library is intended for registering input methods; Emacs
looks for it in @code{load-path} (@pxref{Library Search}), skipping
those directories containing the standard Emacs libraries (since
@file{leim-list.el} should not exist in those directories).
Glenn Morris's avatar
Glenn Morris committed
79

80
@vindex before-init-time
Glenn Morris's avatar
Glenn Morris committed
81
@item
82
It sets the variable @code{before-init-time} to the value of
83
@code{current-time} (@pxref{Time of Day}).  It also sets
84 85
@code{after-init-time} to @code{nil}, which signals to Lisp programs
that Emacs is being initialized.
Glenn Morris's avatar
Glenn Morris committed
86

Glenn Morris's avatar
Glenn Morris committed
87 88 89
@c set-locale-environment
@item
It sets the language environment and the terminal coding system,
90
if requested by environment variables such as @env{LANG}.
Glenn Morris's avatar
Glenn Morris committed
91 92 93 94

@item
It does some basic parsing of the command-line arguments.

95 96
@vindex initial-window-system@r{, and startup}
@vindex window-system-initialization-alist
Glenn Morris's avatar
Glenn Morris committed
97
@item
Glenn Morris's avatar
Glenn Morris committed
98 99 100 101 102 103 104 105 106
If not running in batch mode, it initializes the window system that
the variable @code{initial-window-system} specifies (@pxref{Window
Systems, initial-window-system}).  The initialization function for
each supported window system is specified by
@code{window-system-initialization-alist}.  If the value
of @code{initial-window-system} is @var{windowsystem}, then the
appropriate initialization function is defined in the file
@file{term/@var{windowsystem}-win.el}.  This file should have been
compiled into the Emacs executable when it was built.
107 108

@item
Glenn Morris's avatar
Glenn Morris committed
109
It runs the normal hook @code{before-init-hook}.
Glenn Morris's avatar
Glenn Morris committed
110 111

@item
Glenn Morris's avatar
Glenn Morris committed
112 113
If appropriate, it creates a graphical frame.  This is not done if the
options @samp{--batch} or @samp{--daemon} were specified.
Glenn Morris's avatar
Glenn Morris committed
114 115

@item
Glenn Morris's avatar
Glenn Morris committed
116 117 118 119
It initializes the initial frame's faces, and sets up the menu bar
and tool bar if needed.  If graphical frames are supported, it sets up
the tool bar even if the current frame is not a graphical one, since a
graphical frame may be created later on.
Glenn Morris's avatar
Glenn Morris committed
120 121

@item
Glenn Morris's avatar
Glenn Morris committed
122 123 124 125 126 127 128 129
It use @code{custom-reevaluate-setting} to re-initialize the members
of the list @code{custom-delayed-init-variables}.  These are any
pre-loaded user options whose default value depends on the run-time,
rather than build-time, context.
@xref{Building Emacs, custom-initialize-delay}.

@c @item
@c It registers the colors available for tty frames.
130 131

@item
132 133
It loads the library @file{site-start}, if it exists.  This is not
done if the options @samp{-Q} or @samp{--no-site-file} were specified.
Glenn Morris's avatar
Glenn Morris committed
134 135 136
@cindex @file{site-start.el}

@item
137 138 139 140
It loads your init file (@pxref{Init File}).  This is not done if the
options @samp{-q}, @samp{-Q}, or @samp{--batch} were specified.  If
the @samp{-u} option was specified, Emacs looks for the init file in
that user's home directory instead.
Glenn Morris's avatar
Glenn Morris committed
141 142

@item
143 144 145
It loads the library @file{default}, if it exists.  This is not done
if @code{inhibit-default-init} is non-@code{nil}, nor if the options
@samp{-q}, @samp{-Q}, or @samp{--batch} were specified.
Glenn Morris's avatar
Glenn Morris committed
146 147
@cindex @file{default.el}

148 149
@item
It loads your abbrevs from the file specified by
150 151 152
@code{abbrev-file-name}, if that file exists and can be read
(@pxref{Abbrev Files, abbrev-file-name}).  This is not done if the
option @samp{--batch} was specified.
153

Chong Yidong's avatar
Chong Yidong committed
154 155 156 157 158
@item
If @code{package-enable-at-startup} is non-@code{nil}, it calls the
function @code{package-initialize} to activate any optional Emacs Lisp
package that has been installed.  @xref{Packaging Basics}.

159 160
@vindex after-init-time
@item
161 162 163 164
It sets the variable @code{after-init-time} to the value of
@code{current-time}.  This variable was set to @code{nil} earlier;
setting it to the current time signals that the initialization phase
is over, and, together with @code{before-init-time}, provides the
165 166
measurement of how long it took.

Glenn Morris's avatar
Glenn Morris committed
167 168 169 170
@item
It runs the normal hook @code{after-init-hook}.

@item
171
If the buffer @file{*scratch*} exists and is still in Fundamental mode
172 173
(as it should be by default), it sets its major mode according to
@code{initial-major-mode}.
Glenn Morris's avatar
Glenn Morris committed
174 175

@item
176
If started on a text terminal, it loads the terminal-specific
177 178 179
Lisp library, which is specified by the variable
@code{term-file-prefix} (@pxref{Terminal-Specific}).  This is not done
in @code{--batch} mode, nor if @code{term-file-prefix} is @code{nil}.
Glenn Morris's avatar
Glenn Morris committed
180

Glenn Morris's avatar
Glenn Morris committed
181 182
@c Now command-line calls command-line-1.

Glenn Morris's avatar
Glenn Morris committed
183 184 185 186 187
@item
It displays the initial echo area message, unless you have suppressed
that with @code{inhibit-startup-echo-area-message}.

@item
Glenn Morris's avatar
Glenn Morris committed
188
It processes any command-line options that were not handled earlier.
Glenn Morris's avatar
Glenn Morris committed
189

Glenn Morris's avatar
Glenn Morris committed
190 191
@c This next one is back in command-line, but the remaining bits of
@c command-line-1 are not done if noninteractive.
192 193 194 195 196
@item
It now exits if the option @code{--batch} was specified.

@item
If @code{initial-buffer-choice} is a string, it visits the file with
197
that name.  If the @file{*scratch*} buffer exists and is
198 199
empty, it inserts @code{initial-scratch-message} into that buffer.

Glenn Morris's avatar
Glenn Morris committed
200 201 202 203 204 205 206 207 208
@c To make things nice and confusing, the next three items can be
@c called from two places.  If displaying a startup screen, they are
@c called in command-line-1 before the startup screen is shown.
@c inhibit-startup-hooks is then set and window-setup-hook set to nil.
@c If not displaying a startup screen, they are are called in
@c normal-top-level.
@c FIXME?  So it seems they can be called before or after the
@c daemon/session restore step?

Glenn Morris's avatar
Glenn Morris committed
209 210 211 212 213 214 215 216 217 218 219
@item
It runs @code{emacs-startup-hook} and then @code{term-setup-hook}.

@item
It calls @code{frame-notice-user-settings}, which modifies the
parameters of the selected frame according to whatever the init files
specify.

@item
It runs @code{window-setup-hook}.  @xref{Window Systems}.

220
@item
221
@cindex startup screen
222 223
It displays the @dfn{startup screen}, which is a special buffer that
contains information about copyleft and basic Emacs usage.  This is
Glenn Morris's avatar
Glenn Morris committed
224 225 226
not done if @code{inhibit-startup-screen} or @code{initial-buffer-choice}
are non-@code{nil}, or if the @samp{--no-splash} or @samp{-Q} command-line
options were specified.
227

Glenn Morris's avatar
Glenn Morris committed
228 229 230 231 232 233 234 235 236 237 238 239
@c End of command-line-1.

@c Back to command-line from command-line-1.

@c This is the point at which we actually exit in batch mode, but the
@c last few bits of command-line-1 are not done in batch mode.

@item
If the option @code{--daemon} was specified, it calls
@code{server-start} and detaches from the controlling terminal.
@xref{Emacs Server,,, emacs, The GNU Emacs Manual}.

240 241 242
@item
If started by the X session manager, it calls
@code{emacs-session-restore} passing it as argument the ID of the
243
previous session.  @xref{Session Management}.
Glenn Morris's avatar
Glenn Morris committed
244 245 246 247 248

@c End of command-line.

@c Back to normal-top-level from command-line.

Glenn Morris's avatar
Glenn Morris committed
249 250
@end enumerate

251 252 253
@noindent
The following options affect some aspects of the startup sequence.

254
@defopt inhibit-startup-screen
255
This variable, if non-@code{nil}, inhibits the startup screen.  In
256
that case, Emacs typically displays the @file{*scratch*} buffer; but
257
see @code{initial-buffer-choice}, below.
Glenn Morris's avatar
Glenn Morris committed
258

259 260 261
Do not set this variable in the init file of a new user, or in a way
that affects more than one user, as that would prevent new users from
receiving information about copyleft and basic Emacs usage.
262

263 264
@vindex inhibit-startup-message
@vindex inhibit-splash-screen
265 266 267 268 269
@code{inhibit-startup-message} and @code{inhibit-splash-screen} are
aliases for this variable.
@end defopt

@defopt initial-buffer-choice
Glenn Morris's avatar
Glenn Morris committed
270 271 272
If non-@code{nil}, this variable is a string that specifies a file or
directory for Emacs to display after starting up, instead of the
startup screen.
273 274 275
@ignore
@c I do not think this should be mentioned.  AFAICS it is just a dodge
@c around inhibit-startup-screen not being settable on a site-wide basis.
276
If its value is @code{t}, Emacs displays the @file{*scratch*} buffer.
277
@end ignore
Glenn Morris's avatar
Glenn Morris committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291
@end defopt

@defopt inhibit-startup-echo-area-message
This variable controls the display of the startup echo area message.
You can suppress the startup echo area message by adding text with this
form to your init file:

@example
(setq inhibit-startup-echo-area-message
      "@var{your-login-name}")
@end example

Emacs explicitly checks for an expression as shown above in your init
file; your login name must appear in the expression as a Lisp string
292 293 294
constant.  You can also use the Customize interface.  Other methods of
setting @code{inhibit-startup-echo-area-message} to the same value do
not inhibit the startup message.  This way, you can easily inhibit the
295 296 297
message for yourself if you wish, but thoughtless copying of your init
file will not inhibit the message for someone else.
@end defopt
Glenn Morris's avatar
Glenn Morris committed
298

299 300
@defopt initial-scratch-message
This variable, if non-@code{nil}, should be a string, which is
301 302
inserted into the @file{*scratch*} buffer when Emacs starts up.  If it
is @code{nil}, the @file{*scratch*} buffer is empty.
Glenn Morris's avatar
Glenn Morris committed
303 304
@end defopt

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
@noindent
The following command-line options affect some aspects of the startup
sequence.  @xref{Initial Options,,, emacs, The GNU Emacs Manual}.

@table @code
@item --no-splash
Do not display a splash screen.

@item --batch
Run without an interactive terminal.  @xref{Batch Mode}.

@item --daemon
Do not initialize any display; just start a server in the background.

@item --no-init-file
320
@itemx -q
321 322 323 324 325 326 327 328 329 330 331 332
Do not load either the init file, or the @file{default} library.

@item --no-site-file
Do not load the @file{site-start} library.

@item --quick
@itemx -Q
Equivalent to @samp{-q --no-site-file --no-splash}.
@c and --no-site-lisp, but let's not mention that here.
@end table


Glenn Morris's avatar
Glenn Morris committed
333
@node Init File
Chong Yidong's avatar
Chong Yidong committed
334
@subsection The Init File
Glenn Morris's avatar
Glenn Morris committed
335 336
@cindex init file
@cindex @file{.emacs}
Chong Yidong's avatar
Chong Yidong committed
337
@cindex @file{init.el}
Glenn Morris's avatar
Glenn Morris committed
338 339

  When you start Emacs, it normally attempts to load your @dfn{init
340 341
file}.  This is either a file named @file{.emacs} or @file{.emacs.el}
in your home directory, or a file named @file{init.el} in a
Glenn Morris's avatar
Glenn Morris committed
342 343 344
subdirectory named @file{.emacs.d} in your home directory.
@ignore
Whichever place you use, you can also compile the file (@pxref{Byte
345 346
Compilation}); then the actual file loaded will be @file{.emacs.elc}
or @file{init.elc}.
Glenn Morris's avatar
Glenn Morris committed
347
@end ignore
Glenn Morris's avatar
Glenn Morris committed
348 349 350 351 352 353

  The command-line switches @samp{-q}, @samp{-Q}, and @samp{-u}
control whether and where to find the init file; @samp{-q} (and the
stronger @samp{-Q}) says not to load an init file, while @samp{-u
@var{user}} says to load @var{user}'s init file instead of yours.
@xref{Entering Emacs,,, emacs, The GNU Emacs Manual}.  If neither
354 355
option is specified, Emacs uses the @env{LOGNAME} environment
variable, or the @env{USER} (most systems) or @env{USERNAME} (MS
Glenn Morris's avatar
Glenn Morris committed
356 357 358 359 360 361
systems) variable, to find your home directory and thus your init
file; this way, even if you have su'd, Emacs still loads your own init
file.  If those environment variables are absent, though, Emacs uses
your user-id to find your home directory.

@cindex default init file
Chong Yidong's avatar
Chong Yidong committed
362 363 364 365 366
  An Emacs installation may have a @dfn{default init file}, which is a
Lisp library named @file{default.el}.  Emacs finds this file through
the standard search path for libraries (@pxref{How Programs Do
Loading}).  The Emacs distribution does not come with this file; it is
intended for local customizations.  If the default init file exists,
Glenn Morris's avatar
Glenn Morris committed
367
it is loaded whenever you start Emacs.  But your own personal init
Glenn Morris's avatar
Glenn Morris committed
368 369
file, if any, is loaded first; if it sets @code{inhibit-default-init}
to a non-@code{nil} value, then Emacs does not subsequently load the
Glenn Morris's avatar
Glenn Morris committed
370 371 372
@file{default.el} file.  In batch mode, or if you specify @samp{-q}
(or @samp{-Q}), Emacs loads neither your personal init file nor
the default init file.
Glenn Morris's avatar
Glenn Morris committed
373 374 375 376 377

  Another file for site-customization is @file{site-start.el}.  Emacs
loads this @emph{before} the user's init file.  You can inhibit the
loading of this file with the option @samp{--no-site-file}.

378
@defopt site-run-file
Glenn Morris's avatar
Glenn Morris committed
379 380 381 382
This variable specifies the site-customization file to load before the
user's init file.  Its normal value is @code{"site-start"}.  The only
way you can change it with real effect is to do so before dumping
Emacs.
Glenn Morris's avatar
Glenn Morris committed
383
@c So why even mention it here.  I imagine it is almost never changed.
384
@end defopt
Glenn Morris's avatar
Glenn Morris committed
385 386 387 388 389 390

  @xref{Init Examples,, Init File Examples, emacs, The GNU Emacs Manual}, for
examples of how to make various commonly desired customizations in your
@file{.emacs} file.

@defopt inhibit-default-init
Glenn Morris's avatar
Glenn Morris committed
391 392
If this variable is non-@code{nil}, it prevents Emacs from loading the
default initialization library file.  The default value is @code{nil}.
Glenn Morris's avatar
Glenn Morris committed
393 394 395 396
@end defopt

@defvar before-init-hook
This normal hook is run, once, just before loading all the init files
Glenn Morris's avatar
Glenn Morris committed
397
(@file{site-start.el}, your init file, and @file{default.el}).
Glenn Morris's avatar
Glenn Morris committed
398 399 400 401 402
(The only way to change it with real effect is before dumping Emacs.)
@end defvar

@defvar after-init-hook
This normal hook is run, once, just after loading all the init files
Glenn Morris's avatar
Glenn Morris committed
403 404 405
(@file{site-start.el}, your init file, and @file{default.el}),
before loading the terminal-specific library (if started on a text
terminal) and processing the command-line action arguments.
Glenn Morris's avatar
Glenn Morris committed
406 407 408 409
@end defvar

@defvar emacs-startup-hook
This normal hook is run, once, just after handling the command line
Glenn Morris's avatar
Glenn Morris committed
410 411
arguments, just before @code{term-setup-hook}.  In batch mode, Emacs
does not run either of these hooks.
Glenn Morris's avatar
Glenn Morris committed
412 413 414 415 416 417 418 419 420 421
@end defvar

@defvar user-init-file
This variable holds the absolute file name of the user's init file.  If the
actual init file loaded is a compiled file, such as @file{.emacs.elc},
the value refers to the corresponding source file.
@end defvar

@defvar user-emacs-directory
This variable holds the name of the @file{.emacs.d} directory.  It is
Glenn Morris's avatar
Glenn Morris committed
422
@file{~/.emacs.d} on all platforms but MS-DOS.
Glenn Morris's avatar
Glenn Morris committed
423 424 425 426 427 428 429 430 431
@end defvar

@node Terminal-Specific
@subsection Terminal-Specific Initialization
@cindex terminal-specific initialization

  Each terminal type can have its own Lisp library that Emacs loads when
run on that type of terminal.  The library's name is constructed by
concatenating the value of the variable @code{term-file-prefix} and the
432
terminal type (specified by the environment variable @env{TERM}).
Glenn Morris's avatar
Glenn Morris committed
433 434 435 436 437 438
Normally, @code{term-file-prefix} has the value
@code{"term/"}; changing this is not recommended.  Emacs finds the file
in the normal manner, by searching the @code{load-path} directories, and
trying the @samp{.elc} and @samp{.el} suffixes.

@cindex Termcap
Chong Yidong's avatar
Chong Yidong committed
439 440 441 442 443
  The usual role of a terminal-specific library is to enable special
keys to send sequences that Emacs can recognize.  It may also need to
set or add to @code{input-decode-map} if the Termcap or Terminfo entry
does not specify all the terminal's function keys.  @xref{Terminal
Input}.
Glenn Morris's avatar
Glenn Morris committed
444

Glenn Morris's avatar
Glenn Morris committed
445
  When the name of the terminal type contains a hyphen or underscore, and no library
Glenn Morris's avatar
Glenn Morris committed
446
is found whose name is identical to the terminal's name, Emacs strips
Glenn Morris's avatar
Glenn Morris committed
447
from the terminal's name the last hyphen or underscore and everything that follows
Glenn Morris's avatar
Glenn Morris committed
448
it, and tries again.  This process is repeated until Emacs finds a
Glenn Morris's avatar
Glenn Morris committed
449
matching library, or until there are no more hyphens or underscores in the name
450
(i.e., there is no terminal-specific library).  For example, if the
Chong Yidong's avatar
Chong Yidong committed
451 452 453 454
terminal name is @samp{xterm-256color} and there is no
@file{term/xterm-256color.el} library, Emacs tries to load
@file{term/xterm.el}.  If necessary, the terminal library can evaluate
@code{(getenv "TERM")} to find the full name of the terminal type.
Glenn Morris's avatar
Glenn Morris committed
455 456 457 458 459 460 461 462

  Your init file can prevent the loading of the
terminal-specific library by setting the variable
@code{term-file-prefix} to @code{nil}.  This feature is useful when
experimenting with your own peculiar customizations.

  You can also arrange to override some of the actions of the
terminal-specific library by setting the variable
Glenn Morris's avatar
Glenn Morris committed
463
@code{term-setup-hook}.  This is a normal hook that Emacs runs
464
at the end of its initialization, after loading both
Glenn Morris's avatar
Glenn Morris committed
465 466
your init file and any terminal-specific libraries.  You could
use this hook to define initializations for terminals that do not
Glenn Morris's avatar
Glenn Morris committed
467 468 469
have their own libraries.  @xref{Hooks}.

@defvar term-file-prefix
470
@cindex @env{TERM} environment variable
Chong Yidong's avatar
Chong Yidong committed
471 472
If the value of this variable is non-@code{nil}, Emacs loads a
terminal-specific initialization file as follows:
Glenn Morris's avatar
Glenn Morris committed
473 474 475 476 477 478 479 480

@example
(load (concat term-file-prefix (getenv "TERM")))
@end example

@noindent
You may set the @code{term-file-prefix} variable to @code{nil} in your
init file if you do not wish to load the
Glenn Morris's avatar
Glenn Morris committed
481
terminal-initialization file.
Glenn Morris's avatar
Glenn Morris committed
482

483
On MS-DOS, Emacs sets the @env{TERM} environment variable to @samp{internal}.
Glenn Morris's avatar
Glenn Morris committed
484 485 486 487 488 489 490 491 492 493
@end defvar

@defvar term-setup-hook
This variable is a normal hook that Emacs runs after loading your
init file, the default initialization file (if any) and the
terminal-specific Lisp file.

You can use @code{term-setup-hook} to override the definitions made by a
terminal-specific file.

Glenn Morris's avatar
Glenn Morris committed
494 495
For a related feature, @pxref{Window Systems, window-setup-hook}.
@end defvar
Glenn Morris's avatar
Glenn Morris committed
496 497 498 499 500

@node Command-Line Arguments
@subsection Command-Line Arguments
@cindex command-line arguments

Chong Yidong's avatar
Chong Yidong committed
501
  You can use command-line arguments to request various actions when
Glenn Morris's avatar
Glenn Morris committed
502 503 504 505 506 507 508
you start Emacs.  Note that the recommended way of using Emacs is to
start it just once, after logging in, and then do all editing in the same
Emacs session (@pxref{Entering Emacs,,, emacs, The GNU Emacs Manual}).
For this reason, you might not use command-line arguments very often;
nonetheless, they can be useful when invoking Emacs from session
scripts or debugging Emacs.  This section describes how Emacs
processes command-line arguments.
Glenn Morris's avatar
Glenn Morris committed
509 510 511

@defun command-line
This function parses the command line that Emacs was called with,
Glenn Morris's avatar
Glenn Morris committed
512 513
processes it, and (amongst other things) loads the user's init file and
displays the startup messages.
Glenn Morris's avatar
Glenn Morris committed
514 515 516 517 518 519
@end defun

@defvar command-line-processed
The value of this variable is @code{t} once the command line has been
processed.

Xue Fuqiao's avatar
Xue Fuqiao committed
520 521 522 523
If you redump Emacs by calling @code{dump-emacs} (@pxref{Building
Emacs}), you may wish to set this variable to @code{nil} first in
order to cause the new dumped Emacs to process its new command-line
arguments.
Glenn Morris's avatar
Glenn Morris committed
524 525 526 527 528 529
@end defvar

@defvar command-switch-alist
@cindex switches on command line
@cindex options on command line
@cindex command-line options
Glenn Morris's avatar
Glenn Morris committed
530 531 532
This variable is an alist of user-defined command-line options and
associated handler functions.  By default it is empty, but you can
add elements if you wish.
Glenn Morris's avatar
Glenn Morris committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554

A @dfn{command-line option} is an argument on the command line, which
has the form:

@example
-@var{option}
@end example

The elements of the @code{command-switch-alist} look like this:

@example
(@var{option} . @var{handler-function})
@end example

The @sc{car}, @var{option}, is a string, the name of a command-line
option (not including the initial hyphen).  The @var{handler-function}
is called to handle @var{option}, and receives the option name as its
sole argument.

In some cases, the option is followed in the command line by an
argument.  In these cases, the @var{handler-function} can find all the
remaining command-line arguments in the variable
Xue Fuqiao's avatar
Xue Fuqiao committed
555 556
@code{command-line-args-left} (see below).  (The entire list of
command-line arguments is in @code{command-line-args}.)
Glenn Morris's avatar
Glenn Morris committed
557 558 559 560 561 562 563 564 565 566 567 568

The command-line arguments are parsed by the @code{command-line-1}
function in the @file{startup.el} file.  See also @ref{Emacs
Invocation, , Command Line Arguments for Emacs Invocation, emacs, The
GNU Emacs Manual}.
@end defvar

@defvar command-line-args
The value of this variable is the list of command-line arguments passed
to Emacs.
@end defvar

569
@defvar command-line-args-left
570
@vindex argv
571
The value of this variable is the list of command-line arguments that
Glenn Morris's avatar
Glenn Morris committed
572 573 574
have not yet been processed.
@c Don't mention this, since it is a "bad name for a dynamically bound variable"
@c @code{argv} is an alias for this.
575 576
@end defvar

Glenn Morris's avatar
Glenn Morris committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
@defvar command-line-functions
This variable's value is a list of functions for handling an
unrecognized command-line argument.  Each time the next argument to be
processed has no special meaning, the functions in this list are called,
in order of appearance, until one of them returns a non-@code{nil}
value.

These functions are called with no arguments.  They can access the
command-line argument under consideration through the variable
@code{argi}, which is bound temporarily at this point.  The remaining
arguments (not including the current one) are in the variable
@code{command-line-args-left}.

When a function recognizes and processes the argument in @code{argi}, it
should return a non-@code{nil} value to say it has dealt with that
argument.  If it has also dealt with some of the following arguments, it
can indicate that by deleting them from @code{command-line-args-left}.

Glenn Morris's avatar
Glenn Morris committed
595
If all of these functions return @code{nil}, then the argument is treated
Glenn Morris's avatar
Glenn Morris committed
596 597 598 599 600 601 602 603 604
as a file name to visit.
@end defvar

@node Getting Out
@section Getting Out of Emacs
@cindex exiting Emacs

  There are two ways to get out of Emacs: you can kill the Emacs job,
which exits permanently, or you can suspend it, which permits you to
605 606 607
reenter the Emacs process later.  (In a graphical environment, you can
of course simply switch to another application without doing anything
special to Emacs, then switch back to Emacs when you want.)
Glenn Morris's avatar
Glenn Morris committed
608 609 610 611 612 613 614 615 616 617

@menu
* Killing Emacs::        Exiting Emacs irreversibly.
* Suspending Emacs::     Exiting Emacs reversibly.
@end menu

@node Killing Emacs
@subsection Killing Emacs
@cindex killing Emacs

618 619 620 621
  Killing Emacs means ending the execution of the Emacs process.
If you started Emacs from a terminal, the parent process normally
resumes control.  The low-level primitive for killing Emacs is
@code{kill-emacs}.
Glenn Morris's avatar
Glenn Morris committed
622

623
@deffn Command kill-emacs &optional exit-data
624 625
This command calls the hook @code{kill-emacs-hook}, then exits the
Emacs process and kills it.
Glenn Morris's avatar
Glenn Morris committed
626

627 628
If @var{exit-data} is an integer, that is used as the exit status of
the Emacs process.  (This is useful primarily in batch operation; see
Glenn Morris's avatar
Glenn Morris committed
629 630 631 632 633
@ref{Batch Mode}.)

If @var{exit-data} is a string, its contents are stuffed into the
terminal input buffer so that the shell (or whatever program next reads
input) can read them.
634
@end deffn
Glenn Morris's avatar
Glenn Morris committed
635

636 637 638 639 640 641 642 643
@cindex SIGTERM
@cindex SIGHUP
@cindex SIGINT
@cindex operating system signal
  The @code{kill-emacs} function is normally called via the
higher-level command @kbd{C-x C-c}
(@code{save-buffers-kill-terminal}).  @xref{Exiting,,, emacs, The GNU
Emacs Manual}.  It is also called automatically if Emacs receives a
644
@code{SIGTERM} or @code{SIGHUP} operating system signal (e.g., when the
645 646
controlling terminal is disconnected), or if it receives a
@code{SIGINT} signal while running in batch mode (@pxref{Batch Mode}).
Glenn Morris's avatar
Glenn Morris committed
647

648 649 650 651
@defvar kill-emacs-hook
This normal hook is run by @code{kill-emacs}, before it kills Emacs.

Because @code{kill-emacs} can be called in situations where user
652
interaction is impossible (e.g., when the terminal is disconnected),
653 654 655
functions on this hook should not attempt to interact with the user.
If you want to interact with the user when Emacs is shutting down, use
@code{kill-emacs-query-functions}, described below.
Glenn Morris's avatar
Glenn Morris committed
656 657
@end defvar

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
  When Emacs is killed, all the information in the Emacs process,
aside from files that have been saved, is lost.  Because killing Emacs
inadvertently can lose a lot of work, the
@code{save-buffers-kill-terminal} command queries for confirmation if
you have buffers that need saving or subprocesses that are running.
It also runs the abnormal hook @code{kill-emacs-query-functions}:

@defvar kill-emacs-query-functions
When @code{save-buffers-kill-terminal} is killing Emacs, it calls the
functions in this hook, after asking the standard questions and before
calling @code{kill-emacs}.  The functions are called in order of
appearance, with no arguments.  Each function can ask for additional
confirmation from the user.  If any of them returns @code{nil},
@code{save-buffers-kill-emacs} does not kill Emacs, and does not run
the remaining functions in this hook.  Calling @code{kill-emacs}
directly does not run this hook.
Glenn Morris's avatar
Glenn Morris committed
674 675 676 677 678 679
@end defvar

@node Suspending Emacs
@subsection Suspending Emacs
@cindex suspending Emacs

680
  On text terminals, it is possible to @dfn{suspend Emacs}, which
681 682 683 684 685 686
means stopping Emacs temporarily and returning control to its superior
process, which is usually the shell.  This allows you to resume
editing later in the same Emacs process, with the same buffers, the
same kill ring, the same undo history, and so on.  To resume Emacs,
use the appropriate command in the parent shell---most likely
@code{fg}.
Glenn Morris's avatar
Glenn Morris committed
687

688 689 690
@cindex controlling terminal
  Suspending works only on a terminal device from which the Emacs
session was started.  We call that device the @dfn{controlling
691
terminal} of the session.  Suspending is not allowed if the
692 693 694 695 696 697 698 699 700 701 702
controlling terminal is a graphical terminal.  Suspending is usually
not relevant in graphical environments, since you can simply switch to
another application without doing anything special to Emacs.

@c FIXME?  Are there any systems Emacs still supports that do not
@c have SIGTSTP?
@cindex SIGTSTP
  Some operating systems (those without @code{SIGTSTP}, or MS-DOS) do
not support suspension of jobs; on these systems, ``suspension''
actually creates a new shell temporarily as a subprocess of Emacs.
Then you would exit the shell to return to Emacs.
Glenn Morris's avatar
Glenn Morris committed
703

704
@deffn Command suspend-emacs &optional string
Glenn Morris's avatar
Glenn Morris committed
705 706 707 708
This function stops Emacs and returns control to the superior process.
If and when the superior process resumes Emacs, @code{suspend-emacs}
returns @code{nil} to its caller in Lisp.

709 710
This function works only on the controlling terminal of the Emacs
session; to relinquish control of other tty devices, use
711
@code{suspend-tty} (see below).  If the Emacs session uses more than
712 713 714
one terminal, you must delete the frames on all the other terminals
before suspending Emacs, or this function signals an error.
@xref{Multiple Terminals}.
715

716
If @var{string} is non-@code{nil}, its characters are sent to Emacs's
717 718 719 720
superior shell, to be read as terminal input.
@c FIXME?  It seems to me that shell does echo STRING.
The characters in @var{string} are not echoed by the superior shell;
only the results appear.
Glenn Morris's avatar
Glenn Morris committed
721 722

Before suspending, @code{suspend-emacs} runs the normal hook
723 724 725
@code{suspend-hook}.  After the user resumes Emacs,
@code{suspend-emacs} runs the normal hook @code{suspend-resume-hook}.
@xref{Hooks}.
Glenn Morris's avatar
Glenn Morris committed
726 727

The next redisplay after resumption will redraw the entire screen,
728 729
unless the variable @code{no-redraw-on-reenter} is non-@code{nil}.
@xref{Refresh Screen}.
Glenn Morris's avatar
Glenn Morris committed
730

731
Here is an example of how you could use these hooks:
Glenn Morris's avatar
Glenn Morris committed
732 733 734 735

@smallexample
@group
(add-hook 'suspend-hook
736 737
          (lambda () (or (y-or-n-p "Really suspend? ")
                         (error "Suspend canceled"))))
Glenn Morris's avatar
Glenn Morris committed
738
@end group
739 740 741 742 743 744 745 746 747
(add-hook 'suspend-resume-hook (lambda () (message "Resumed!")
                                 (sit-for 2)))
@end smallexample
@c The sit-for prevents the ``nil'' that suspend-emacs returns
@c hiding the message.

Here is what you would see upon evaluating @code{(suspend-emacs "pwd")}:

@smallexample
Glenn Morris's avatar
Glenn Morris committed
748 749 750 751 752 753 754 755
@group
---------- Buffer: Minibuffer ----------
Really suspend? @kbd{y}
---------- Buffer: Minibuffer ----------
@end group

@group
---------- Parent Shell ----------
756 757
bash$ /home/username
bash$ fg
Glenn Morris's avatar
Glenn Morris committed
758 759 760 761 762 763 764
@end group

@group
---------- Echo Area ----------
Resumed!
@end group
@end smallexample
765 766 767 768

@c FIXME?  AFAICS, it is echoed.
Note that @samp{pwd} is not echoed after Emacs is suspended.  But it
is read and executed by the shell.
769
@end deffn
Glenn Morris's avatar
Glenn Morris committed
770 771 772 773 774 775 776 777 778 779

@defvar suspend-hook
This variable is a normal hook that Emacs runs before suspending.
@end defvar

@defvar suspend-resume-hook
This variable is a normal hook that Emacs runs on resuming
after a suspension.
@end defvar

780 781 782 783
@defun suspend-tty &optional tty
If @var{tty} specifies a terminal device used by Emacs, this function
relinquishes the device and restores it to its prior state.  Frames
that used the device continue to exist, but are not updated and Emacs
784 785 786 787 788 789
doesn't read input from them.  @var{tty} can be a terminal object, a
frame (meaning the terminal for that frame), or @code{nil} (meaning
the terminal for the selected frame).  @xref{Multiple Terminals}.

If @var{tty} is already suspended, this function does nothing.

790
@vindex suspend-tty-functions
791 792
This function runs the hook @code{suspend-tty-functions}, passing the
terminal object as an argument to each function.
793 794 795
@end defun

@defun resume-tty &optional tty
796
This function resumes the previously suspended terminal device
797 798
@var{tty}; where @var{tty} has the same possible values as it does
for @code{suspend-tty}.
799

800
@vindex resume-tty-functions
801
This function reopens the terminal device, re-initializes it, and
802
redraws it with that terminal's selected frame.  It then runs the
803 804
hook @code{resume-tty-functions}, passing the terminal object as an
argument to each function.
805 806

If the same device is already used by another Emacs terminal, this
807 808
function signals an error.  If @var{tty} is not suspended, this
function does nothing.
809 810
@end defun

811 812 813
@defun controlling-tty-p &optional tty
This function returns non-@code{nil} if @var{tty} is the
controlling terminal of the Emacs session; @var{tty} can be a
814 815
terminal object, a frame (meaning the terminal for that frame), or
@code{nil} (meaning the terminal for the selected frame).
816 817 818 819
@end defun

@deffn Command suspend-frame
This command @dfn{suspends} a frame.  For GUI frames, it calls
820 821 822 823
@code{iconify-frame} (@pxref{Visibility of Frames}); for frames on
text terminals, it calls either @code{suspend-emacs} or
@code{suspend-tty}, depending on whether the frame is displayed on the
controlling terminal device or not.
824 825
@end deffn

Glenn Morris's avatar
Glenn Morris committed
826 827 828 829 830 831 832 833 834 835
@node System Environment
@section Operating System Environment
@cindex operating system environment

  Emacs provides access to variables in the operating system environment
through various functions.  These variables include the name of the
system, the user's @acronym{UID}, and so on.

@defvar system-configuration
This variable holds the standard GNU configuration name for the
836 837 838
hardware/software configuration of your system, as a string.  For
example, a typical value for a 64-bit GNU/Linux system is
@samp{"x86_64-unknown-linux-gnu"}.
Glenn Morris's avatar
Glenn Morris committed
839 840 841 842 843
@end defvar

@cindex system type and name
@defvar system-type
The value of this variable is a symbol indicating the type of operating
844
system Emacs is running on.  The possible values are:
Glenn Morris's avatar
Glenn Morris committed
845

846
@table @code
847 848
@item aix
IBM's AIX.
Glenn Morris's avatar
Glenn Morris committed
849 850

@item berkeley-unix
851
Berkeley BSD and its variants.
Glenn Morris's avatar
Glenn Morris committed
852 853

@item cygwin
854 855 856 857
Cygwin, a Posix layer on top of MS-Windows.

@item darwin
Darwin (Mac OS X).
Glenn Morris's avatar
Glenn Morris committed
858 859

@item gnu
860
The GNU system (using the GNU kernel, which consists of the HURD and Mach).
Glenn Morris's avatar
Glenn Morris committed
861 862 863

@item gnu/linux
A GNU/Linux system---that is, a variant GNU system, using the Linux
864
kernel.  (These systems are the ones people often call ``Linux'', but
Glenn Morris's avatar
Glenn Morris committed
865 866
actually Linux is just the kernel, not the whole system.)

867 868 869
@item gnu/kfreebsd
A GNU (glibc-based) system with a FreeBSD kernel.

Glenn Morris's avatar
Glenn Morris committed
870 871 872 873 874 875 876
@item hpux
Hewlett-Packard HPUX operating system.

@item irix
Silicon Graphics Irix system.

@item ms-dos
877
Microsoft's DOS@.  Emacs compiled with DJGPP for MS-DOS binds
878
@code{system-type} to @code{ms-dos} even when you run it on MS-Windows.
Glenn Morris's avatar
Glenn Morris committed
879 880

@item usg-unix-v
881
AT&T Unix System V.
Glenn Morris's avatar
Glenn Morris committed
882 883

@item windows-nt
884
Microsoft Windows NT, 9X and later.  The value of @code{system-type}
885
is always @code{windows-nt}, e.g., even on Windows 7.
Glenn Morris's avatar
Glenn Morris committed
886 887 888 889 890

@end table

We do not wish to add new symbols to make finer distinctions unless it
is absolutely necessary!  In fact, we hope to eliminate some of these
891 892
alternatives in the future.  If you need to make a finer distinction
than @code{system-type} allows for, you can test
893
@code{system-configuration}, e.g., against a regexp.
Glenn Morris's avatar
Glenn Morris committed
894 895 896
@end defvar

@defun system-name
897 898
This function returns the name of the machine you are running on, as a
string.
Glenn Morris's avatar
Glenn Morris committed
899 900 901 902 903 904 905 906 907
@end defun

  The symbol @code{system-name} is a variable as well as a function.  In
fact, the function returns whatever value the variable
@code{system-name} currently holds.  Thus, you can set the variable
@code{system-name} in case Emacs is confused about the name of your
system.  The variable is also useful for constructing frame titles
(@pxref{Frame Titles}).

908
@c FIXME seems like this section is not the best place for this option?
909
@defopt mail-host-address
Glenn Morris's avatar
Glenn Morris committed
910 911 912 913 914 915
If this variable is non-@code{nil}, it is used instead of
@code{system-name} for purposes of generating email addresses.  For
example, it is used when constructing the default value of
@code{user-mail-address}.  @xref{User Identification}.  (Since this is
done when Emacs starts up, the value actually used is the one saved when
Emacs was dumped.  @xref{Building Emacs}.)
916 917
@c FIXME sounds like should probably give this a :set-after and some
@c custom-initialize-delay voodoo.
918
@end defopt
Glenn Morris's avatar
Glenn Morris committed
919

920
@deffn Command getenv var &optional frame
Glenn Morris's avatar
Glenn Morris committed
921 922 923
@cindex environment variable access
This function returns the value of the environment variable @var{var},
as a string.  @var{var} should be a string.  If @var{var} is undefined
924 925 926
in the environment, @code{getenv} returns @code{nil}.  It returns
@samp{""} if @var{var} is set but null.  Within Emacs, a list of environment
variables and their values is kept in the variable @code{process-environment}.
Glenn Morris's avatar
Glenn Morris committed
927 928 929 930 931 932

@example
@group
(getenv "USER")
     @result{} "lewis"
@end group
933 934 935
@end example

The shell command @code{printenv} prints all or part of the environment:
Glenn Morris's avatar
Glenn Morris committed
936

937
@example
Glenn Morris's avatar
Glenn Morris committed
938
@group
939 940
bash$ printenv
PATH=/usr/local/bin:/usr/bin:/bin
Glenn Morris's avatar
Glenn Morris committed
941 942 943
USER=lewis
@end group
@group
944 945 946
TERM=xterm
SHELL=/bin/bash
HOME=/home/lewis
Glenn Morris's avatar
Glenn Morris committed
947
@end group
948
@dots{}
Glenn Morris's avatar
Glenn Morris committed
949 950 951
@end example
@end deffn

952
@deffn Command setenv variable &optional value substitute
Glenn Morris's avatar
Glenn Morris committed
953 954 955 956 957 958 959
This command sets the value of the environment variable named
@var{variable} to @var{value}.  @var{variable} should be a string.
Internally, Emacs Lisp can handle any string.  However, normally
@var{variable} should be a valid shell identifier, that is, a sequence
of letters, digits and underscores, starting with a letter or
underscore.  Otherwise, errors may occur if subprocesses of Emacs try
to access the value of @var{variable}.  If @var{value} is omitted or
960 961 962 963
@code{nil} (or, interactively, with a prefix argument), @code{setenv}
removes @var{variable} from the environment.  Otherwise, @var{value}
should be a string.

Xue Fuqiao's avatar
Xue Fuqiao committed
964
@c FIXME: Document `substitute-env-vars'?  --xfq
965 966 967
If the optional argument @var{substitute} is non-@code{nil}, Emacs
calls the function @code{substitute-env-vars} to expand any
environment variables in @var{value}.
Glenn Morris's avatar
Glenn Morris committed
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983

@code{setenv} works by modifying @code{process-environment}; binding
that variable with @code{let} is also reasonable practice.

@code{setenv} returns the new value of @var{variable}, or @code{nil}
if it removed @var{variable} from the environment.
@end deffn

@defvar process-environment
This variable is a list of strings, each describing one environment
variable.  The functions @code{getenv} and @code{setenv} work by means
of this variable.

@smallexample
@group
process-environment
984
@result{} ("PATH=/usr/local/bin:/usr/bin:/bin"
Glenn Morris's avatar
Glenn Morris committed
985 986 987
    "USER=lewis"
@end group
@group
988 989 990 991
    "TERM=xterm"
    "SHELL=/bin/bash"
    "HOME=/home/lewis"
    @dots{})
Glenn Morris's avatar
Glenn Morris committed
992 993 994 995 996 997 998 999
@end group
@end smallexample

If @code{process-environment} contains ``duplicate'' elements that
specify the same environment variable, the first of these elements
specifies the variable, and the other ``duplicates'' are ignored.
@end defvar

1000 1001
@defvar initial-environment
This variable holds the list of environment variables Emacs inherited
1002
from its parent process when Emacs started.
1003 1004
@end defvar

Glenn Morris's avatar
Glenn Morris committed
1005
@defvar path-separator
1006
This variable holds a string that says which character separates
Glenn Morris's avatar
Glenn Morris committed
1007
directories in a search path (as found in an environment variable).  Its
1008
value is @code{":"} for Unix and GNU systems, and @code{";"} for MS systems.
Glenn Morris's avatar
Glenn Morris committed
1009 1010 1011
@end defvar

@defun parse-colon-path path
1012
This function takes a search path string such as the value of
1013
the @env{PATH} environment variable, and splits it at the separators,
1014 1015 1016
returning a list of directory names.  @code{nil} in this list means
the current directory.  Although the function's name says
``colon'', it actually uses the value of @code{path-separator}.
Glenn Morris's avatar
Glenn Morris committed
1017 1018