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