* gnus.el (gnus-article-date-ut): Avoid race condition.
[bpt/emacs.git] / lisp / startup.el
CommitLineData
c88ab9ce
ER
1;;; startup.el --- process Emacs shell arguments
2
9596811a 3;; Copyright (C) 1985, 86, 92, 94, 95, 1996 Free Software Foundation, Inc.
eea8d4ef 4
630cc463 5;; Maintainer: FSF
d7b4d18f 6;; Keywords: internal
630cc463 7
a726e0d1
JB
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
4746118a 12;; the Free Software Foundation; either version 2, or (at your option)
a726e0d1
JB
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
a726e0d1 24
630cc463 25;;; Commentary:
a726e0d1 26
ab30fc8a
RS
27;; This file parses the command line and gets Emacs running. Options on
28;; the command line are handled in precedence order. The order is the
29;; one in the list below; first described means first handled. Options
30;; within each category (delimited by a bar) are handled in the order
31;; encountered on the command line.
32
33;; -------------------------
34;; -version Print Emacs version to stderr, then exit
35;; --version successfully right away.
36;; This option is handled by emacs.c
37;; -------------------------
38;; -help Print a short usage description and exit
39;; --help successfully right away.
40;; This option is handled by emacs.c
41;; -------------------------
42;; -nl Do not use shared memory (for systems that
43;; -no-shared-memory support this) for the dumped Emacs data.
44;; This option is handled by emacs.c
45;;
46;; -map For VMS.
47;; --map-data This option is handled by emacs.c
48;; -------------------------
49;; -t FILE Use FILE as the name of the terminal.
50;; --terminal FILE Using this implies "-nw" also.
51;; This option is handled by emacs.c
52;; -------------------------
53;; -d DISPNAME Use DISPNAME as the name of the X-windows
54;; -display DISPNAME display for the initial frame.
55;; --display DISPNAME This option is handled by emacs.c
56;; -------------------------
57;; -nw Do not use a windows system (but use the
58;; --no-windows terminal instead.)
59;; This option is handled by emacs.c
60;; -------------------------
61;; -batch Execute noninteractively (messages go to stdout,
62;; --batch variable noninteractive set to t)
63;; This option is handled by emacs.c
64;; -------------------------
65;; -q Do not load user's init file and do not load
66;; -no-init-file "default.el". Regardless of this switch,
6f9340dd 67;; --no-init-file "site-start" is still loaded.
ab30fc8a 68;; -------------------------
6f9340dd 69;; -no-site-file Do not load "site-start.el". (This is the ONLY
ab30fc8a
RS
70;; --no-site-file way to prevent loading that file.)
71;; -------------------------
72;; -u USER Load USER's init file instead of the init
73;; -user USER file belonging to the user starting Emacs.
74;; --user USER
75;; -------------------------
76;; -debug-init Don't catch errors in init files; let the
77;; --debug-init debugger run.
78;; -------------------------
79;; -i ICONTYPE Set type of icon using when Emacs is
80;; -itype ICONTYPE iconified under X-windows.
81;; --icon-type ICONTYPE This option is passed on to term/x-win.el
82;;
83;; -iconic Start Emacs iconified under X-windows.
84;; --iconic This option is passed on to term/x-win.el
85;; -------------------------
86;; Various X-windows options for colors/fonts/geometry/title etc.
87;; These options are passed on to term/x-win.el which see. Certain
88;; of these are also found in term/pc-win.el
89;; -------------------------
90;; FILE Visit FILE.
91;;
92;; -L DIRNAME Add DIRNAME to load-path
93;; -directory DIRNAME
94;; --directory DIRNAME
95;;
96;; -l FILE Load and execute the Emacs lisp code
97;; -load FILE in FILE.
98;; --load FILE
99;;
100;; -f FUNC Execute Emacs lisp function FUNC with
101;; -funcall FUNC no arguments. The "-e" form is outdated
102;; --funcall FUNC and should not be used. (It's a typo
103;; -e FUNC promoted to a feature.)
104;;
955093c9
EN
105;; -eval FORM Execute Emacs lisp form FORM.
106;; --eval FORM
e6b75e30 107;;
ab30fc8a
RS
108;; -insert FILE Insert the contents of FILE into buffer.
109;; --insert FILE
110;; -------------------------
111;; -kill Kill (exit) Emacs right away.
112;; --kill
113;; -------------------------
a726e0d1 114
630cc463
ER
115;;; Code:
116
a726e0d1
JB
117(setq top-level '(normal-top-level))
118
119(defvar command-line-processed nil "t once command line has been processed")
120
121(defconst inhibit-startup-message nil
1d7da582 122 "*Non-nil inhibits the initial startup message.
a726e0d1
JB
123This is for use in your personal init file, once you are familiar
124with the contents of the startup message.")
125
1d7da582
RS
126(defconst inhibit-startup-echo-area-message nil
127 "*Non-nil inhibits the initial startup echo area message.
128Inhibition takes effect only if your `.emacs' file contains
e5575c06 129a line of this form:
54a003f7 130 (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
e5575c06
RS
131If your `.emacs' file is byte-compiled, use the following form instead:
132 (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
1d7da582
RS
133Thus, someone else using a copy of your `.emacs' file will see
134the startup message unless he personally acts to inhibit it.")
135
a726e0d1
JB
136(defconst inhibit-default-init nil
137 "*Non-nil inhibits loading the `default' library.")
138
139(defconst command-switch-alist nil
140 "Alist of command-line switches.
141Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
142HANDLER-FUNCTION receives switch name as sole arg;
143remaining command-line args are in the variable `command-line-args-left'.")
144
860befc8
RS
145(defvar command-line-args-left nil
146 "List of command-line args not yet processed.")
147
a726e0d1
JB
148(defvar command-line-functions nil ;; lrs 7/31/89
149 "List of functions to process unrecognized command-line arguments.
150Each function should access the dynamically bound variables
b4484ea8 151`argi' (the current argument) and `command-line-args-left' (the remaining
a726e0d1 152arguments). The function should return non-nil only if it recognizes and
b4484ea8
RS
153processes `argi'. If it does so, it may consume successive arguments by
154altering `command-line-args-left' to remove them.")
a726e0d1 155
09a1077c
RS
156(defvar command-line-default-directory nil
157 "Default directory to use for command line arguments.
158This is normally copied from `default-directory' when Emacs starts.")
159
b3afdeb8
RS
160;;; This is here, rather than in x-win.el, so that we can ignore these
161;;; options when we are not using X.
162(defvar command-line-x-option-alist
163 '(("-bw" 1 x-handle-numeric-switch border-width)
164 ("-d" 1 x-handle-display)
165 ("-display" 1 x-handle-display)
166 ("-name" 1 x-handle-name-rn-switch)
167 ("-rn" 1 x-handle-name-rn-switch)
2c3fef40
RS
168 ("-title" 1 x-handle-switch title)
169 ("-T" 1 x-handle-switch title)
b3afdeb8
RS
170 ("-r" 0 x-handle-switch reverse t)
171 ("-rv" 0 x-handle-switch reverse t)
172 ("-reverse" 0 x-handle-switch reverse t)
173 ("-reverse-video" 0 x-handle-switch reverse t)
174 ("-fn" 1 x-handle-switch font)
175 ("-font" 1 x-handle-switch font)
176 ("-ib" 1 x-handle-numeric-switch internal-border-width)
1f7f78f1
RS
177 ("-g" 1 x-handle-geometry)
178 ("-geometry" 1 x-handle-geometry)
b3afdeb8
RS
179 ("-fg" 1 x-handle-switch foreground-color)
180 ("-foreground" 1 x-handle-switch foreground-color)
181 ("-bg" 1 x-handle-switch background-color)
182 ("-background" 1 x-handle-switch background-color)
183 ("-ms" 1 x-handle-switch mouse-color)
184 ("-itype" 0 x-handle-switch icon-type t)
185 ("-i" 0 x-handle-switch icon-type t)
186 ("-iconic" 0 x-handle-iconic)
187 ("-xrm" 1 x-handle-xrm-switch)
188 ("-cr" 1 x-handle-switch cursor-color)
189 ("-vb" 0 x-handle-switch vertical-scroll-bars t)
190 ("-hb" 0 x-handle-switch horizontal-scroll-bars t)
191 ("-bd" 1 x-handle-switch)
192 ("--border-width" 1 x-handle-numeric-switch border-width)
193 ("--display" 1 x-handle-display)
194 ("--name" 1 x-handle-name-rn-switch)
7775acb8 195 ("--title" 1 x-handle-switch title)
b3afdeb8
RS
196 ("--reverse-video" 0 x-handle-switch reverse t)
197 ("--font" 1 x-handle-switch font)
198 ("--internal-border" 1 x-handle-numeric-switch internal-border-width)
1f7f78f1 199 ("--geometry" 1 x-handle-geometry)
b3afdeb8
RS
200 ("--foreground-color" 1 x-handle-switch foreground-color)
201 ("--background-color" 1 x-handle-switch background-color)
202 ("--mouse-color" 1 x-handle-switch mouse-color)
203 ("--icon-type" 0 x-handle-switch icon-type t)
204 ("--iconic" 0 x-handle-iconic)
205 ("--xrm" 1 x-handle-xrm-switch)
206 ("--cursor-color" 1 x-handle-switch cursor-color)
207 ("--vertical-scroll-bars" 0 x-handle-switch vertical-scroll-bars t)
208 ("--border-color" 1 x-handle-switch border-width))
209 "Alist of X Windows options.
210Each element has the form
211 (NAME NUMARGS HANDLER FRAME-PARAM VALUE)
212where NAME is the option name string, NUMARGS is the number of arguments
213that the option accepts, HANDLER is a function to call to handle the option.
214FRAME-PARAM (optional) is the frame parameter this option specifies,
215and VALUE is the value which is given to that frame parameter
216\(most options use the argument for this, so VALUE is not present).")
217
e3bd99f5 218(defvar before-init-hook nil
db3f571a 219 "Normal hook run after handling urgent options but before loading init files.")
3fc958a4 220
e3bd99f5 221(defvar after-init-hook nil
db3f571a
KH
222 "Normal hook run after loading the init files, `~/.emacs' and `default.el'.
223There is no `condition-case' around the running of these functions;
224therefore, if you set `debug-on-error' non-nil in `.emacs',
225an error in one of these functions will invoke the debugger.")
226
227(defvar emacs-startup-hook nil
228 "Normal hook run after loading init files and handling the command line.")
e3bd99f5 229
a726e0d1 230(defvar term-setup-hook nil
db3f571a
KH
231 "Normal hook run after loading terminal-specific Lisp code.
232It also follows `emacs-startup-hook'. This hook exists for users to set,
a726e0d1
JB
233so as to override the definitions made by the terminal-specific file.
234Emacs never sets this variable itself.")
235
236(defvar keyboard-type nil
b4484ea8
RS
237 "The brand of keyboard you are using.
238This variable is used to define
a726e0d1
JB
239the proper function and keypad keys for use under X. It is used in a
240fashion analogous to the environment value TERM.")
241
242(defvar window-setup-hook nil
b4484ea8
RS
243 "Normal hook run to initialize window system display.
244Emacs runs this hook after processing the command line arguments and loading
245the user's init file.")
a726e0d1
JB
246
247(defconst initial-major-mode 'lisp-interaction-mode
248 "Major mode command symbol to use for the initial *scratch* buffer.")
249
250(defvar init-file-user nil
251 "Identity of user whose `.emacs' file is or was read.
2bdfaa42
KH
252The value is nil if no init file is being used; otherwise, it may be either
253the null string, meaning that the init file was taken from the user that
254originally logged in, or it may be a string containing a user's name.
a726e0d1 255
2bdfaa42
KH
256In either of the latter cases, `(concat \"~\" init-file-user \"/\")'
257evaluates to the name of the directory where the `.emacs' file was
13fce4e6
RS
258looked for.
259
260Setting `init-file-user' does not prevent Emacs from loading
261`site-start.el'. The only way to do that is to use `--no-site-file'.")
a726e0d1 262
b7444d31
RS
263(defvar site-run-file "site-start"
264 "File containing site-wide run-time initializations.
265This file is loaded at run-time before `~/.emacs'. It contains inits
266that need to be in place for the entire site, but which, due to their
267higher incidence of change, don't make sense to load into emacs'
268dumped image. Thus, the run-time load order is: 1. file described in
13fce4e6
RS
269this variable, if non-nil; 2. `~/.emacs'; 3. `default.el'.
270
271Don't use the `site-start.el' file for things some users may not like.
272Put them in `default.el' instead, so that users can more easily
273override them. Users can prevent loading `default.el' with the `-q'
274option or by setting `inhibit-default-init' in their own init files,
275but inhibiting `site-start.el' requires `--no-site-file', which
276is less convenient.")
b7444d31 277
30dc01ea
RS
278(defconst iso-8859-1-locale-regexp "8859[-_]?1"
279 "Regexp that specifies when to enable the ISO 8859-1 character set.
280We do that if this regexp matches the locale name
281specified by the LC_ALL, LC_CTYPE and LANG environment variables.")
282
c13fbb62
RS
283(defvar mail-host-address nil
284 "*Name of this machine, for purposes of naming users.")
285
c10d1f06 286(defvar user-mail-address nil
452e9090
RS
287 "*Full mailing address of this user.
288This is initialized based on `mail-host-address',
289after your init file is read, in case it sets `mail-host-address'.")
c10d1f06 290
1f7f78f1
RS
291(defvar auto-save-list-file-prefix
292 (if (eq system-type 'ms-dos)
293 "~/_s" ; MS-DOS cannot have initial dot, and allows only 8.3 names
294 "~/.saves-")
cdee38c3
KH
295 "Prefix for generating `auto-save-list-file-name'.
296This is used after reading your `.emacs' file to initialize
297`auto-save-list-file-name', by appending Emacs's pid and the system name,
298if you have not already set `auto-save-list-file-name' yourself.
299Set this to nil if you want to prevent `auto-save-list-file-name'
300from being initialized.")
2e05d063 301
a726e0d1
JB
302(defvar init-file-debug nil)
303
52320897
RS
304(defvar init-file-had-error nil)
305
e87a7309
RS
306;; This function is called from the subdirs.el file.
307(defun normal-top-level-add-to-load-path (dirs)
308 (let ((tail (member default-directory load-path)))
309 (setcdr tail (append (mapcar 'expand-file-name dirs) (cdr tail)))))
310
a726e0d1
JB
311(defun normal-top-level ()
312 (if command-line-processed
313 (message "Back to top level.")
314 (setq command-line-processed t)
8d4c2221
RS
315 ;; Give *Messages* the same default-directory as *scratch*,
316 ;; just to keep things predictable.
317 (let ((dir default-directory))
318 (save-excursion
319 (set-buffer (get-buffer "*Messages*"))
320 (setq default-directory dir)))
e87a7309
RS
321 ;; Look in each dir in load-path for a subdirs.el file.
322 ;; If we find one, load it, which will add the appropriate subdirs
323 ;; of that dir into load-path,
324 (let ((tail load-path)
325 new)
326 (while tail
327 (setq new (cons (car tail) new))
328 (let ((default-directory (car tail)))
329 (load (expand-file-name "subdirs.el" (car tail)) t t t))
330 (setq tail (cdr tail))))
ffd56f97 331 (if (not (eq system-type 'vax-vms))
a4b33896
JB
332 (progn
333 ;; If the PWD environment variable isn't accurate, delete it.
334 (let ((pwd (getenv "PWD")))
335 (and (stringp pwd)
336 ;; Use FOO/., so that if FOO is a symlink, file-attributes
337 ;; describes the directory linked to, not FOO itself.
338 (or (equal (file-attributes
339 (concat (file-name-as-directory pwd) "."))
340 (file-attributes
341 (concat (file-name-as-directory default-directory)
342 ".")))
343 (setq process-environment
344 (delete (concat "PWD=" pwd)
345 process-environment)))))))
492878e4 346 (setq default-directory (abbreviate-file-name default-directory))
6f2c86fa
KH
347 (let ((menubar-bindings-done nil))
348 (unwind-protect
349 (command-line)
350 ;; Do this again, in case .emacs defined more abbreviations.
351 (setq default-directory (abbreviate-file-name default-directory))
b3c7c12c
RS
352 ;; Specify the file for recording all the auto save files of this session.
353 ;; This is used by recover-session.
cdee38c3
KH
354 (or auto-save-list-file-name
355 (and auto-save-list-file-prefix
356 (setq auto-save-list-file-name
1f7f78f1
RS
357 ;; Under MS-DOS our PID is almost always reused between
358 ;; Emacs invocations. We need something more unique.
359 (if (eq system-type 'ms-dos)
2c42ec0b
RS
360 (concat
361 (make-temp-name
362 (expand-file-name auto-save-list-file-prefix))
363 "~")
364
365 (expand-file-name (format "%s%d-%s~"
1f7f78f1
RS
366 auto-save-list-file-prefix
367 (emacs-pid)
368 (system-name)))))))
6f2c86fa
KH
369 (run-hooks 'emacs-startup-hook)
370 (and term-setup-hook
371 (run-hooks 'term-setup-hook))
372 ;; Modify the initial frame based on what .emacs puts into
373 ;; ...-frame-alist.
374 (if (fboundp 'frame-notice-user-settings)
375 (frame-notice-user-settings))
376 ;; Now we know the user's default font, so add it to the menu.
377 (if (fboundp 'font-menu-add-default)
378 (font-menu-add-default))
379 (and window-setup-hook
380 (run-hooks 'window-setup-hook))
381 (or menubar-bindings-done
39d6d6be 382 (if (or (eq window-system 'x) (eq window-system 'win32))
365636dc 383 (precompute-menubar-bindings)))))))
6f2c86fa
KH
384
385;; Precompute the keyboard equivalents in the menu bar items.
386(defun precompute-menubar-bindings ()
365636dc
RS
387 (let ((submap (lookup-key global-map [menu-bar])))
388 (while submap
389 (and (consp (car submap))
390 (symbolp (car (car submap)))
391 (stringp (car-safe (cdr (car submap))))
392 (keymapp (cdr (cdr (car submap))))
a18042d7
RS
393 (progn
394 (x-popup-menu nil (cdr (cdr (car submap))))
395 (if purify-flag
396 (garbage-collect))))
365636dc
RS
397 (setq submap (cdr submap))))
398 (setq define-key-rebound-commands t))
a726e0d1
JB
399
400(defun command-line ()
09a1077c
RS
401 (setq command-line-default-directory default-directory)
402
74f2ab06 403 ;; See if we should import version-control from the environment variable.
a726e0d1
JB
404 (let ((vc (getenv "VERSION_CONTROL")))
405 (cond ((eq vc nil)) ;don't do anything if not set
406 ((or (string= vc "t")
407 (string= vc "numbered"))
408 (setq version-control t))
409 ((or (string= vc "nil")
410 (string= vc "existing"))
411 (setq version-control nil))
412 ((or (string= vc "never")
413 (string= vc "simple"))
414 (setq version-control 'never))))
415
30dc01ea
RS
416 (if (let ((ctype
417 ;; Use the first of these three envvars that has a nonempty value.
418 (or (let ((string (getenv "LC_ALL")))
419 (and (not (equal string "")) string))
420 (let ((string (getenv "LC_CTYPE")))
421 (and (not (equal string "")) string))
422 (let ((string (getenv "LANG")))
423 (and (not (equal string "")) string)))))
424 (and ctype
425 (string-match iso-8859-1-locale-regexp ctype)))
e9d8e8c7 426 (progn
62bb5440 427 (require 'disp-table)
e9d8e8c7
RS
428 (standard-display-european t)
429 (require 'iso-syntax)))
430
79058860
JB
431 ;;! This has been commented out; I currently find the behavior when
432 ;;! split-window-keep-point is nil disturbing, but if I can get used
433 ;;! to it, then it would be better to eliminate the option.
434 ;;! ;; Choose a good default value for split-window-keep-point.
435 ;;! (setq split-window-keep-point (> baud-rate 2400))
f35fe3c6 436
a726e0d1 437 ;; Read window system's init file if using a window system.
1ed14cfd
RS
438 (condition-case error
439 (if (and window-system (not noninteractive))
440 (load (concat term-file-prefix
441 (symbol-name window-system)
442 "-win")
443 ;; Every window system should have a startup file;
444 ;; barf if we can't find it.
445 nil t))
446 ;; If we can't read it, print the error message and exit.
447 (error
2677ad61
RS
448 (princ
449 (if (eq (car error) 'error)
450 (apply 'concat (cdr error))
451 (if (memq 'file-error (get (car error) 'error-conditions))
452 (format "%s: %s"
453 (nth 1 error)
454 (mapconcat '(lambda (obj) (prin1-to-string obj t))
455 (cdr (cdr error)) ", "))
456 (format "%s: %s"
457 (get (car error) 'error-message)
458 (mapconcat '(lambda (obj) (prin1-to-string obj t))
459 (cdr error) ", "))))
460 'external-debugging-output)
461 (setq window-system nil)
1ed14cfd 462 (kill-emacs)))
a726e0d1 463
03e3c30a
JB
464 (let ((done nil)
465 (args (cdr command-line-args)))
466
a726e0d1
JB
467 ;; Figure out which user's init file to load,
468 ;; either from the environment or from the options.
469 (setq init-file-user (if noninteractive nil (user-login-name)))
470 ;; If user has not done su, use current $HOME to find .emacs.
471 (and init-file-user (string= init-file-user (user-real-login-name))
472 (setq init-file-user ""))
03e3c30a
JB
473
474 ;; Process the command-line args, and delete the arguments
475 ;; processed. This is consistent with the way main in emacs.c
476 ;; does things.
a726e0d1 477 (while (and (not done) args)
096b7031 478 (let ((longopts '(("--no-init-file") ("--no-site-file") ("--user")
a360cae9 479 ("--debug-init") ("--iconic") ("--icon-type")))
096b7031
KH
480 (argi (car args))
481 (argval nil))
452e9090
RS
482 ;; Handle --OPTION=VALUE format.
483 (if (and (string-match "\\`--" argi)
484 (string-match "=" argi))
a981e7ff
KH
485 (setq argval (substring argi (match-end 0))
486 argi (substring argi 0 (match-beginning 0))))
096b7031
KH
487 (let ((completion (try-completion argi longopts)))
488 (if (eq completion t)
489 (setq argi (substring argi 1))
490 (if (stringp completion)
491 (let ((elt (assoc completion longopts)))
492 (or elt
493 (error "Option `%s' is ambiguous" argi))
3f53ddd0
RS
494 (setq argi (substring (car elt) 1)))
495 (setq argval nil))))
a726e0d1
JB
496 (cond
497 ((or (string-equal argi "-q")
498 (string-equal argi "-no-init-file"))
499 (setq init-file-user nil
500 args (cdr args)))
501 ((or (string-equal argi "-u")
502 (string-equal argi "-user"))
096b7031 503 (or argval
d3bea05f
RS
504 (setq args (cdr args)
505 argval (car args)))
096b7031
KH
506 (setq init-file-user argval
507 argval nil
a726e0d1 508 args (cdr args)))
b7444d31
RS
509 ((string-equal argi "-no-site-file")
510 (setq site-run-file nil
511 args (cdr args)))
a726e0d1
JB
512 ((string-equal argi "-debug-init")
513 (setq init-file-debug t
514 args (cdr args)))
a360cae9
RS
515 ((string-equal argi "-iconic")
516 (setq initial-frame-alist
517 (cons '(visibility . icon) initial-frame-alist))
518 (setq args (cdr args)))
519 ((or (string-equal argi "-icon-type")
520 (string-equal argi "-i")
521 (string-equal argi "-itype"))
522 (setq default-frame-alist
523 (cons '(icon-type . t) default-frame-alist))
524 (setq args (cdr args)))
096b7031
KH
525 (t (setq done t)))
526 ;; Was argval set but not used?
527 (and argval
528 (error "Option `%s' doesn't allow an argument" argi))))
529
03e3c30a 530 ;; Re-attach the program name to the front of the arg list.
4048e56d 531 (and command-line-args (setcdr command-line-args args)))
a726e0d1 532
c722566c 533 ;; Under X Windows, this creates the X frame and deletes the terminal frame.
4e1b1e72
JB
534 (if (fboundp 'face-initialize)
535 (face-initialize))
853ccbd5
RS
536 (if (fboundp 'frame-initialize)
537 (frame-initialize))
1fe0333f 538 ;; If frame was created with a menu bar, set menu-bar-mode on.
39d6d6be 539 (if (or (not (or (eq window-system 'x) (eq window-system 'win32)))
3ede3ece 540 (> (cdr (assq 'menu-bar-lines (frame-parameters))) 0))
1fe0333f 541 (menu-bar-mode t))
c722566c 542
e3bd99f5 543 (run-hooks 'before-init-hook)
3fc958a4 544
09973c54
RM
545 ;; Run the site-start library if it exists. The point of this file is
546 ;; that it is run before .emacs. There is no point in doing this after
547 ;; .emacs; that is useless.
b7444d31
RS
548 (if site-run-file
549 (load site-run-file t t))
09973c54 550
8a988f45
RS
551 ;; Sites should not disable this. Only individuals should disable
552 ;; the startup message.
553 (setq inhibit-startup-message nil)
554
a726e0d1 555 ;; Load that user's init file, or the default one, or none.
3d1b78f0
RS
556 (let (debug-on-error-from-init-file
557 debug-on-error-should-be-set
558 (debug-on-error-initial
559 (if (eq init-file-debug t) 'startup init-file-debug)))
560 (let ((debug-on-error debug-on-error-initial)
561 ;; This function actually reads the init files.
562 (inner
563 (function
564 (lambda ()
565 (if init-file-user
a4c5c705
RS
566 (progn
567 (setq user-init-file
568 (cond
569 ((eq system-type 'ms-dos)
570 (concat "~" init-file-user "/_emacs"))
7d1aa45d 571 ((eq system-type 'windows-nt)
521736d1
RS
572 (if (file-exists-p "~/.emacs")
573 "~/.emacs"
574 "~/_emacs"))
a4c5c705
RS
575 ((eq system-type 'vax-vms)
576 "sys$login:.emacs")
577 (t
578 (concat "~" init-file-user "/.emacs"))))
579 (load user-init-file t t t)
580 (or inhibit-default-init
581 (let ((inhibit-startup-message nil))
582 ;; Users are supposed to be told their rights.
583 ;; (Plus how to get help and how to undo.)
584 ;; Don't you dare turn this off for anyone
585 ;; except yourself.
586 (load "default" t t)))))))))
3d1b78f0
RS
587 (if init-file-debug
588 ;; Do this without a condition-case if the user wants to debug.
589 (funcall inner)
590 (condition-case error
591 (progn
592 (funcall inner)
593 (setq init-file-had-error nil))
594 (error (message "Error in init file: %s%s%s"
595 (get (car error) 'error-message)
ad2aeb8d 596 (if (cdr error) ": " "")
3d1b78f0
RS
597 (mapconcat 'prin1-to-string (cdr error) ", "))
598 (setq init-file-had-error t))))
ad2aeb8d 599 ;; If we can tell that the init file altered debug-on-error,
3d1b78f0
RS
600 ;; arrange to preserve the value that it set up.
601 (or (eq debug-on-error debug-on-error-initial)
602 (setq debug-on-error-should-be-set t
603 debug-on-error-from-init-file debug-on-error)))
604 (if debug-on-error-should-be-set
605 (setq debug-on-error debug-on-error-from-init-file)))
3fc958a4 606
556f7d77
RS
607 ;; Do this here in case the init file sets mail-host-address.
608 (or user-mail-address
609 (setq user-mail-address (concat (user-login-name) "@"
610 (or mail-host-address
611 (system-name)))))
612
e3bd99f5
RM
613 (run-hooks 'after-init-hook)
614
a726e0d1
JB
615 ;; If *scratch* exists and init file didn't change its mode, initialize it.
616 (if (get-buffer "*scratch*")
617 (save-excursion
618 (set-buffer "*scratch*")
619 (if (eq major-mode 'fundamental-mode)
620 (funcall initial-major-mode))))
621 ;; Load library for our terminal type.
622 ;; User init file can set term-file-prefix to nil to prevent this.
623 (and term-file-prefix (not noninteractive) (not window-system)
624 (let ((term (getenv "TERM"))
625 hyphend)
626 (while (and term
627 (not (load (concat term-file-prefix term) t t)))
628 ;; Strip off last hyphen and what follows, then try again
629 (if (setq hyphend (string-match "[-_][^-_]+$" term))
630 (setq term (substring term 0 hyphend))
631 (setq term nil)))))
632
03e3c30a 633 ;; Process the remaining args.
a726e0d1
JB
634 (command-line-1 (cdr command-line-args))
635
636 ;; If -batch, terminate after processing the command options.
637 (if noninteractive (kill-emacs t)))
638
639(defun command-line-1 (command-line-args-left)
52320897 640 (or noninteractive (input-pending-p) init-file-had-error
1d7da582
RS
641 (and inhibit-startup-echo-area-message
642 (let ((buffer (get-buffer-create " *temp*")))
643 (prog1
644 (condition-case nil
645 (save-excursion
646 (set-buffer buffer)
647 (insert-file-contents user-init-file)
648 (re-search-forward
649 (concat
d9a71a8f
RS
650 "([ \t\n]*setq[ \t\n]+"
651 "inhibit-startup-echo-area-message[ \t\n]+"
652 (regexp-quote
653 (prin1-to-string
654 (if (string= init-file-user "")
655 (user-login-name)
656 init-file-user)))
657 "[ \t\n]*)")
1d7da582
RS
658 nil t))
659 (error nil))
660 (kill-buffer buffer))))
661 (message (if (eq (key-binding "\C-h\C-p") 'describe-project)
662 "For information about the GNU Project and its goals, type C-h C-p."
663 (substitute-command-keys
664 "For information about the GNU Project and its goals, type \\[describe-project]."))))
a726e0d1
JB
665 (if (null command-line-args-left)
666 (cond ((and (not inhibit-startup-message) (not noninteractive)
667 ;; Don't clobber a non-scratch buffer if init file
668 ;; has selected it.
669 (string= (buffer-name) "*scratch*")
670 (not (input-pending-p)))
671 ;; If there are no switches to process, we might as well
672 ;; run this hook now, and there may be some need to do it
673 ;; before doing any output.
674 (and term-setup-hook
675 (run-hooks 'term-setup-hook))
676 ;; Don't let the hook be run twice.
677 (setq term-setup-hook nil)
4e1b1e72
JB
678
679 ;; It's important to notice the user settings before we
680 ;; display the startup message; otherwise, the settings
681 ;; won't take effect until the user gives the first
682 ;; keystroke, and that's distracting.
683 (if (fboundp 'frame-notice-user-settings)
684 (frame-notice-user-settings))
685
a726e0d1
JB
686 (and window-setup-hook
687 (run-hooks 'window-setup-hook))
688 (setq window-setup-hook nil)
fd11871a
RS
689 ;; Do this now to avoid an annoying delay if the user
690 ;; clicks the menu bar during the sit-for.
39d6d6be 691 (if (or (eq window-system 'x) (eq window-system 'win32))
365636dc 692 (precompute-menubar-bindings))
fd11871a 693 (setq menubar-bindings-done t)
a726e0d1
JB
694 (unwind-protect
695 (progn
6695496f
RS
696 ;; The convention for this piece of code is that
697 ;; each piece of output starts with one or two newlines
698 ;; and does not end with any newlines.
a726e0d1
JB
699 (insert (emacs-version)
700 "
40e23599 701Copyright (C) 1996 Free Software Foundation, Inc.")
a726e0d1
JB
702 ;; If keys have their default meanings,
703 ;; use precomputed string to save lots of time.
704 (if (and (eq (key-binding "\C-h") 'help-command)
705 (eq (key-binding "\C-xu") 'advertised-undo)
706 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-emacs)
e65a6404
RS
707 (eq (key-binding "\C-ht") 'help-with-tutorial)
708 (eq (key-binding "\C-hi") 'info))
6695496f 709 (insert "\n
2e465cef
RS
710Type C-x C-c to exit Emacs.
711Type C-h for help; C-x u to undo changes.
a726e0d1 712Type C-h t for a tutorial on using Emacs.
e65a6404
RS
713Type C-h i to enter Info, which you can use to read GNU documentation.")
714 (insert (substitute-command-keys
6695496f 715 (format "\n
2e465cef
RS
716Type \\[save-buffers-kill-emacs] to exit Emacs.
717Type %s for help; \\[advertised-undo] to undo changes.
e65a6404
RS
718Type \\[help-with-tutorial] for a tutorial on using Emacs.
719Type \\[info] to enter Info, which you can use to read GNU documentation."
720 (let ((where (where-is-internal
721 'help-command nil t)))
722 (if where
723 (key-description where)
724 "M-x help"))))))
2e465cef
RS
725 ;; Many users seem to have problems with these.
726 (insert "
727(`C-' means use the CTRL key. `M-' means use the Meta (or Alt) key.
728If you have no Meta key, you may instead type ESC followed by the character.)")
3ede3ece
RS
729 ;; Say how to use the menu bar
730 ;; if that is not with the mouse.
731 (if (not (assq 'display (frame-parameters)))
732 (if (eq (key-binding "\M-`") 'tmm-menubar)
2e465cef 733 (insert "\n\nType F10 or M-` to use the menu bar.")
3ede3ece
RS
734 (insert (substitute-command-keys
735 "\n\nType \\[tmm-menubar] to use the menu bar."))))
e65a6404
RS
736
737 ;; Windows and MSDOS (currently) do not count as
738 ;; window systems, but do have mouse support.
1f7f78f1 739 (if window-system
6695496f 740 (insert "\n
e65a6404 741C-mouse-3 (third mouse button, with Control) gets a mode-specific menu."))
95127745
KH
742 (and auto-save-list-file-prefix
743 (directory-files
744 (file-name-directory auto-save-list-file-prefix)
745 nil
746 (concat "\\`"
747 (regexp-quote (file-name-nondirectory
748 auto-save-list-file-prefix)))
749 t)
750 (insert "\n\nIf an Emacs session crashed recently,\n"
751 "type M-x recover-session RET to recover"
752 " the files you were editing."))
6695496f 753
e65a6404
RS
754 (if (and (eq (key-binding "\C-h\C-c") 'describe-copying)
755 (eq (key-binding "\C-h\C-d") 'describe-distribution)
756 (eq (key-binding "\C-h\C-w") 'describe-no-warranty))
757 (insert
6695496f 758 "\n
a726e0d1
JB
759GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
760You may give out copies of Emacs; type C-h C-c to see the conditions.
761Type C-h C-d for information on getting the latest version.")
762 (insert (substitute-command-keys
6695496f 763 "\n
a726e0d1
JB
764GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for full details.
765You may give out copies of Emacs; type \\[describe-copying] to see the conditions.
766Type \\[describe-distribution] for information on getting the latest version.")))
fb3afe87 767
a726e0d1
JB
768 (set-buffer-modified-p nil)
769 (sit-for 120))
770 (save-excursion
771 ;; In case the Emacs server has already selected
772 ;; another buffer, erase the one our message is in.
773 (set-buffer (get-buffer "*scratch*"))
774 (erase-buffer)
775 (set-buffer-modified-p nil)))))
eca1cf26
RS
776 ;; Delay 2 seconds after the init file error message
777 ;; was displayed, so user can read it.
778 (if init-file-had-error
779 (sit-for 2))
09a1077c 780 (let ((dir command-line-default-directory)
a726e0d1
JB
781 (file-count 0)
782 first-file-buffer
783 (line 0))
784 (while command-line-args-left
3f53ddd0
RS
785 (let* ((argi (car command-line-args-left))
786 (orig-argi argi)
787 ;; This includes our standard options' long versions
788 ;; and long versions of what's on command-switch-alist.
789 (longopts
aba66e2a 790 (append '(("--funcall") ("--load") ("--insert") ("--kill")
e6b75e30 791 ("--directory") ("--eval"))
3f53ddd0
RS
792 (mapcar '(lambda (elt)
793 (list (concat "-" (car elt))))
794 command-switch-alist)))
aba66e2a
RS
795 tem argval completion
796 ;; List of directories specified in -L/--directory,
797 ;; in reverse of the order specified.
798 extra-load-path
799 (initial-load-path load-path))
a726e0d1 800 (setq command-line-args-left (cdr command-line-args-left))
3f53ddd0 801
b3afdeb8
RS
802 ;; Add the long X options to longopts.
803 (setq tem command-line-x-option-alist)
804 (while tem
805 (if (string-match "^--" (car (car tem)))
806 (setq longopts (cons (list (car (car tem))) longopts)))
807 (setq tem (cdr tem)))
808
3f53ddd0
RS
809 ;; Convert long options to ordinary options
810 ;; and separate out an attached option argument into argval.
811 (if (string-match "^--[^=]*=" argi)
812 (setq argval (substring argi (match-end 0))
813 argi (substring argi 0 (1- (match-end 0)))))
814 (setq completion (try-completion argi longopts))
815 (if (eq completion t)
816 (setq argi (substring argi 1))
817 (if (stringp completion)
818 (let ((elt (assoc completion longopts)))
819 (or elt
820 (error "Option `%s' is ambiguous" argi))
821 (setq argi (substring (car elt) 1)))
822 (setq argval nil argi orig-argi)))
823
824 ;; Execute the option.
a726e0d1 825 (cond ((setq tem (assoc argi command-switch-alist))
3f53ddd0
RS
826 (if argval
827 (let ((command-line-args-left
828 (cons argval command-line-args-left)))
829 (funcall (cdr tem) argi))
830 (funcall (cdr tem) argi)))
a726e0d1
JB
831 ((or (string-equal argi "-f") ;what the manual claims
832 (string-equal argi "-funcall")
833 (string-equal argi "-e")) ; what the source used to say
3f53ddd0
RS
834 (if argval
835 (setq tem (intern argval))
836 (setq tem (intern (car command-line-args-left)))
837 (setq command-line-args-left (cdr command-line-args-left)))
1908c565
RS
838 (if (arrayp (symbol-function tem))
839 (command-execute tem)
840 (funcall tem)))
e6b75e30
RS
841 ((string-equal argi "-eval")
842 (if argval
843 (setq tem argval)
844 (setq tem (car command-line-args-left))
845 (setq command-line-args-left (cdr command-line-args-left)))
955093c9 846 (eval (read tem)))
aba66e2a
RS
847 ;; Set the default directory as specified in -L.
848 ((or (string-equal argi "-L")
849 (string-equal argi "-directory"))
850 (if argval
851 (setq tem argval)
852 (setq tem (car command-line-args-left)
853 command-line-args-left (cdr command-line-args-left)))
47c7adae 854 (setq tem (command-line-normalize-file-name tem))
aba66e2a
RS
855 (setq extra-load-path
856 (cons (expand-file-name tem) extra-load-path))
857 (setq load-path (append (nreverse extra-load-path)
858 initial-load-path)))
a726e0d1
JB
859 ((or (string-equal argi "-l")
860 (string-equal argi "-load"))
3f53ddd0
RS
861 (if argval
862 (setq tem argval)
863 (setq tem (car command-line-args-left)
864 command-line-args-left (cdr command-line-args-left)))
47c7adae 865 (let ((file (command-line-normalize-file-name tem)))
a726e0d1
JB
866 ;; Take file from default dir if it exists there;
867 ;; otherwise let `load' search for it.
868 (if (file-exists-p (expand-file-name file))
869 (setq file (expand-file-name file)))
3f53ddd0 870 (load file nil t)))
fbce8654 871 ((string-equal argi "-insert")
3f53ddd0
RS
872 (if argval
873 (setq tem argval)
874 (setq tem (car command-line-args-left)
875 command-line-args-left (cdr command-line-args-left)))
2d2aae54
EN
876 (or (stringp tem)
877 (error "File name omitted from `-insert' option"))
47c7adae 878 (insert-file-contents (command-line-normalize-file-name tem)))
a726e0d1
JB
879 ((string-equal argi "-kill")
880 (kill-emacs t))
881 ((string-match "^\\+[0-9]+\\'" argi)
882 (setq line (string-to-int argi)))
b3afdeb8
RS
883 ((setq tem (assoc argi command-line-x-option-alist))
884 ;; Ignore X-windows options and their args if not using X.
885 (setq command-line-args-left
886 (nthcdr (nth 1 tem) command-line-args-left)))
a726e0d1
JB
887 (t
888 ;; We have almost exhausted our options. See if the
889 ;; user has made any other command-line options available
890 (let ((hooks command-line-functions);; lrs 7/31/89
891 (did-hook nil))
892 (while (and hooks
893 (not (setq did-hook (funcall (car hooks)))))
894 (setq hooks (cdr hooks)))
895 (if (not did-hook)
896 ;; Ok, presume that the argument is a file name
897 (progn
7a79b90c
RS
898 (if (string-match "\\`-" argi)
899 (error "Unknown option `%s'" argi))
a726e0d1 900 (setq file-count (1+ file-count))
47c7adae 901 (setq argi (command-line-normalize-file-name argi))
a726e0d1
JB
902 (cond ((= file-count 1)
903 (setq first-file-buffer
904 (find-file (expand-file-name argi dir))))
905 (t
906 (find-file-other-window (expand-file-name argi dir))))
907 (or (zerop line)
908 (goto-line line))
909 (setq line 0))))))))
910 ;; If 3 or more files visited, and not all visible,
911 ;; show user what they all are.
912 (if (> file-count 2)
913 (or (get-buffer-window first-file-buffer)
7e0795a4 914 (progn (other-window 1)
e8997612 915 (buffer-menu)))))))
c88ab9ce 916
47c7adae
RS
917(defun command-line-normalize-file-name (file)
918 "Collapse multiple slashes to one, to handle non-Emacs file names."
6b9e794f
RS
919 (save-match-data
920 ;; Use arg 1 so that we don't collapse // at the start of the file name.
921 ;; That is significant on some systems.
922 ;; However, /// at the beginning is supposed to mean just /, not //.
923 (if (string-match "^///+" file)
924 (setq file (replace-match "/" t t file)))
925 (while (string-match "//+" file 1)
926 (setq file (replace-match "/" t t file)))
927 file))
47c7adae 928
c88ab9ce 929;;; startup.el ends here