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