(store_symval_forwarding, swap_in_symval_forwarding, Fset, default_value,
[emacs.git] / lisp / startup.el
blobb2b87c9475443c6566fbb0b846798393a17e670d
1 ;;; startup.el --- process Emacs shell arguments
3 ;; Copyright (C) 1985, 1986, 1992, 1994 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Commentary:
26 ; These are processed only at the beginning of the argument list.
27 ; -batch execute noninteractively (messages go to stdout,
28 ; variable noninteractive set to t)
29 ; This option must be the first in the arglist.
30 ; Processed by `main' in emacs.c -- never seen by lisp
31 ; -t file Specify to use file rather than stdin/stdout
32 ; as the terminal.
33 ; This option must be the first in the arglist.
34 ; Processed by `main' in emacs.c -- never seen by lisp
35 ; -nw Inhibit the use of any window-system-specific display
36 ; code; use the current virtual terminal.
37 ; This option must be the first in the arglist.
38 ; Processed by `main' in emacs.c -- never seen by lisp
39 ; -q load no init file
40 ; -no-init-file same
41 ; -u user load user's init file
42 ; -user user same
43 ; -debug-init Don't catch errors in init file; let debugger run.
44 ; -no-site-file Don't load site-run-file.
46 ; These are processed in the order encountered.
47 ; -f function execute function
48 ; -funcall function same
49 ; -l file load file
50 ; -load file same
51 ; -insert file insert file into buffer
52 ; file visit file
53 ; -kill kill (exit) emacs
55 ;;; Code:
57 (setq top-level '(normal-top-level))
59 (defvar command-line-processed nil "t once command line has been processed")
61 (defconst inhibit-startup-message nil
62 "*Non-nil inhibits the initial startup message.
63 This is for use in your personal init file, once you are familiar
64 with the contents of the startup message.")
66 (defconst inhibit-startup-echo-area-message nil
67 "*Non-nil inhibits the initial startup echo area message.
68 Inhibition takes effect only if your `.emacs' file contains
69 a line of this form:
70 (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
71 If your `.emacs' file is byte-compiled, use the following form instead:
72 (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
73 Thus, someone else using a copy of your `.emacs' file will see
74 the startup message unless he personally acts to inhibit it.")
76 (defconst inhibit-default-init nil
77 "*Non-nil inhibits loading the `default' library.")
79 (defconst command-switch-alist nil
80 "Alist of command-line switches.
81 Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
82 HANDLER-FUNCTION receives switch name as sole arg;
83 remaining command-line args are in the variable `command-line-args-left'.")
85 (defvar command-line-args-left nil
86 "List of command-line args not yet processed.")
88 (defvar command-line-functions nil ;; lrs 7/31/89
89 "List of functions to process unrecognized command-line arguments.
90 Each function should access the dynamically bound variables
91 `argi' (the current argument) and `command-line-args-left' (the remaining
92 arguments). The function should return non-nil only if it recognizes and
93 processes `argi'. If it does so, it may consume successive arguments by
94 altering `command-line-args-left' to remove them.")
96 (defvar command-line-default-directory nil
97 "Default directory to use for command line arguments.
98 This is normally copied from `default-directory' when Emacs starts.")
100 (defvar before-init-hook nil
101 "Functions to call after handling urgent options but before init files.
102 The frame system uses this to open frames to display messages while
103 Emacs loads the user's initialization file.")
105 (defvar after-init-hook nil
106 "Functions to call after loading the init file (`~/.emacs').
107 The call is not protected by a condition-case, so you can set `debug-on-error'
108 in `.emacs', and put all the actual code on `after-init-hook'.")
110 (defvar term-setup-hook nil
111 "Functions to be called after loading terminal-specific Lisp code.
112 See `run-hooks'. This variable exists for users to set,
113 so as to override the definitions made by the terminal-specific file.
114 Emacs never sets this variable itself.")
116 (defvar keyboard-type nil
117 "The brand of keyboard you are using.
118 This variable is used to define
119 the proper function and keypad keys for use under X. It is used in a
120 fashion analogous to the environment value TERM.")
122 (defvar window-setup-hook nil
123 "Normal hook run to initialize window system display.
124 Emacs runs this hook after processing the command line arguments and loading
125 the user's init file.")
127 (defconst initial-major-mode 'lisp-interaction-mode
128 "Major mode command symbol to use for the initial *scratch* buffer.")
130 (defvar init-file-user nil
131 "Identity of user whose `.emacs' file is or was read.
132 The value is nil if no init file is being used; otherwise, it may be either
133 the null string, meaning that the init file was taken from the user that
134 originally logged in, or it may be a string containing a user's name.
136 In either of the latter cases, `(concat \"~\" init-file-user \"/\")'
137 evaluates to the name of the directory where the `.emacs' file was
138 looked for.")
140 (defvar site-run-file "site-start"
141 "File containing site-wide run-time initializations.
142 This file is loaded at run-time before `~/.emacs'. It contains inits
143 that need to be in place for the entire site, but which, due to their
144 higher incidence of change, don't make sense to load into emacs'
145 dumped image. Thus, the run-time load order is: 1. file described in
146 this variable, if non-nil; 2. `~/.emacs'; 3. `default.el'.")
148 (defconst iso-8859-1-locale-regexp "8859[-_]?1"
149 "Regexp that specifies when to enable the ISO 8859-1 character set.
150 We do that if this regexp matches the locale name
151 specified by the LC_ALL, LC_CTYPE and LANG environment variables.")
153 (defvar mail-host-address nil
154 "*Name of this machine, for purposes of naming users.")
156 (defvar user-mail-address nil
157 "*Full mailing address of this user.")
159 (defvar init-file-debug nil)
161 (defvar init-file-had-error nil)
163 ;; This function is called from the subdirs.el file.
164 (defun normal-top-level-add-to-load-path (dirs)
165 (let ((tail (member default-directory load-path)))
166 (setcdr tail (append (mapcar 'expand-file-name dirs) (cdr tail)))))
168 (defun normal-top-level ()
169 (if command-line-processed
170 (message "Back to top level.")
171 (setq command-line-processed t)
172 ;; Look in each dir in load-path for a subdirs.el file.
173 ;; If we find one, load it, which will add the appropriate subdirs
174 ;; of that dir into load-path,
175 (let ((tail load-path)
176 new)
177 (while tail
178 (setq new (cons (car tail) new))
179 (let ((default-directory (car tail)))
180 (load (expand-file-name "subdirs.el" (car tail)) t t t))
181 (setq tail (cdr tail))))
182 (if (not (eq system-type 'vax-vms))
183 (progn
184 ;; If the PWD environment variable isn't accurate, delete it.
185 (let ((pwd (getenv "PWD")))
186 (and (stringp pwd)
187 ;; Use FOO/., so that if FOO is a symlink, file-attributes
188 ;; describes the directory linked to, not FOO itself.
189 (or (equal (file-attributes
190 (concat (file-name-as-directory pwd) "."))
191 (file-attributes
192 (concat (file-name-as-directory default-directory)
193 ".")))
194 (setq process-environment
195 (delete (concat "PWD=" pwd)
196 process-environment)))))))
197 (setq default-directory (abbreviate-file-name default-directory))
198 (setq user-mail-address (concat (user-login-name) "@"
199 (or mail-host-address
200 (system-name))))
201 (let ((menubar-bindings-done nil))
202 (unwind-protect
203 (command-line)
204 ;; Do this again, in case .emacs defined more abbreviations.
205 (setq default-directory (abbreviate-file-name default-directory))
206 (run-hooks 'emacs-startup-hook)
207 (and term-setup-hook
208 (run-hooks 'term-setup-hook))
209 ;; Modify the initial frame based on what .emacs puts into
210 ;; ...-frame-alist.
211 (if (fboundp 'frame-notice-user-settings)
212 (frame-notice-user-settings))
213 ;; Now we know the user's default font, so add it to the menu.
214 (if (fboundp 'font-menu-add-default)
215 (font-menu-add-default))
216 (and window-setup-hook
217 (run-hooks 'window-setup-hook))
218 (or menubar-bindings-done
219 (precompute-menubar-bindings))))))
221 ;; Precompute the keyboard equivalents in the menu bar items.
222 (defun precompute-menubar-bindings ()
223 (if (eq window-system 'x)
224 (let ((submap (lookup-key global-map [menu-bar])))
225 (while submap
226 (and (consp (car submap))
227 (symbolp (car (car submap)))
228 (stringp (car-safe (cdr (car submap))))
229 (keymapp (cdr (cdr (car submap))))
230 (x-popup-menu nil (cdr (cdr (car submap)))))
231 (setq submap (cdr submap))))))
233 (defun command-line ()
234 (setq command-line-default-directory default-directory)
236 ;; See if we should import version-control from the environment variable.
237 (let ((vc (getenv "VERSION_CONTROL")))
238 (cond ((eq vc nil)) ;don't do anything if not set
239 ((or (string= vc "t")
240 (string= vc "numbered"))
241 (setq version-control t))
242 ((or (string= vc "nil")
243 (string= vc "existing"))
244 (setq version-control nil))
245 ((or (string= vc "never")
246 (string= vc "simple"))
247 (setq version-control 'never))))
249 (if (let ((ctype
250 ;; Use the first of these three envvars that has a nonempty value.
251 (or (let ((string (getenv "LC_ALL")))
252 (and (not (equal string "")) string))
253 (let ((string (getenv "LC_CTYPE")))
254 (and (not (equal string "")) string))
255 (let ((string (getenv "LANG")))
256 (and (not (equal string "")) string)))))
257 (and ctype
258 (string-match iso-8859-1-locale-regexp ctype)))
259 (progn
260 (require 'disp-table)
261 (standard-display-european t)
262 (require 'iso-syntax)))
264 ;;! This has been commented out; I currently find the behavior when
265 ;;! split-window-keep-point is nil disturbing, but if I can get used
266 ;;! to it, then it would be better to eliminate the option.
267 ;;! ;; Choose a good default value for split-window-keep-point.
268 ;;! (setq split-window-keep-point (> baud-rate 2400))
270 ;; Read window system's init file if using a window system.
271 (condition-case error
272 (if (and window-system (not noninteractive))
273 (load (concat term-file-prefix
274 (symbol-name window-system)
275 "-win")
276 ;; Every window system should have a startup file;
277 ;; barf if we can't find it.
278 nil t))
279 ;; If we can't read it, print the error message and exit.
280 (error
281 (princ
282 (if (eq (car error) 'error)
283 (apply 'concat (cdr error))
284 (if (memq 'file-error (get (car error) 'error-conditions))
285 (format "%s: %s"
286 (nth 1 error)
287 (mapconcat '(lambda (obj) (prin1-to-string obj t))
288 (cdr (cdr error)) ", "))
289 (format "%s: %s"
290 (get (car error) 'error-message)
291 (mapconcat '(lambda (obj) (prin1-to-string obj t))
292 (cdr error) ", "))))
293 'external-debugging-output)
294 (setq window-system nil)
295 (kill-emacs)))
297 (let ((done nil)
298 (args (cdr command-line-args)))
300 ;; Figure out which user's init file to load,
301 ;; either from the environment or from the options.
302 (setq init-file-user (if noninteractive nil (user-login-name)))
303 ;; If user has not done su, use current $HOME to find .emacs.
304 (and init-file-user (string= init-file-user (user-real-login-name))
305 (setq init-file-user ""))
307 ;; Process the command-line args, and delete the arguments
308 ;; processed. This is consistent with the way main in emacs.c
309 ;; does things.
310 (while (and (not done) args)
311 (let ((longopts '(("--no-init-file") ("--no-site-file") ("--user")
312 ("--debug-init")))
313 (argi (car args))
314 (argval nil))
315 (if (string-match "=" argi)
316 (setq argval (substring argi (match-end 0))
317 argi (substring argi 0 (match-beginning 0))))
318 (let ((completion (try-completion argi longopts)))
319 (if (eq completion t)
320 (setq argi (substring argi 1))
321 (if (stringp completion)
322 (let ((elt (assoc completion longopts)))
323 (or elt
324 (error "Option `%s' is ambiguous" argi))
325 (setq argi (substring (car elt) 1))))))
326 (cond
327 ((or (string-equal argi "-q")
328 (string-equal argi "-no-init-file"))
329 (setq init-file-user nil
330 args (cdr args)))
331 ((or (string-equal argi "-u")
332 (string-equal argi "-user"))
333 (or argval
334 (setq argval (car args)
335 args (cdr args)))
336 (setq init-file-user argval
337 argval nil
338 args (cdr args)))
339 ((string-equal argi "-no-site-file")
340 (setq site-run-file nil
341 args (cdr args)))
342 ((string-equal argi "-debug-init")
343 (setq init-file-debug t
344 args (cdr args)))
345 (t (setq done t)))
346 ;; Was argval set but not used?
347 (and argval
348 (error "Option `%s' doesn't allow an argument" argi))))
350 ;; Re-attach the program name to the front of the arg list.
351 (setcdr command-line-args args))
353 ;; Under X Windows, this creates the X frame and deletes the terminal frame.
354 (if (fboundp 'face-initialize)
355 (face-initialize))
356 (if (fboundp 'frame-initialize)
357 (frame-initialize))
358 ;; If frame was created with a menu bar, set menu-bar-mode on.
359 (if (and (eq window-system 'x)
360 (> (cdr (assq 'menu-bar-lines (frame-parameters))) 0))
361 (menu-bar-mode t))
363 (run-hooks 'before-init-hook)
365 ;; Run the site-start library if it exists. The point of this file is
366 ;; that it is run before .emacs. There is no point in doing this after
367 ;; .emacs; that is useless.
368 (if site-run-file
369 (load site-run-file t t))
371 ;; Sites should not disable this. Only individuals should disable
372 ;; the startup message.
373 (setq inhibit-startup-message nil)
375 ;; Load that user's init file, or the default one, or none.
376 (let (debug-on-error-from-init-file
377 debug-on-error-should-be-set
378 (debug-on-error-initial
379 (if (eq init-file-debug t) 'startup init-file-debug)))
380 (let ((debug-on-error debug-on-error-initial)
381 ;; This function actually reads the init files.
382 (inner
383 (function
384 (lambda ()
385 (if init-file-user
386 (progn
387 (setq user-init-file
388 (cond
389 ((eq system-type 'ms-dos)
390 (concat "~" init-file-user "/_emacs"))
391 ((eq system-type 'windows-nt)
392 "~/_emacs")
393 ((eq system-type 'vax-vms)
394 "sys$login:.emacs")
396 (concat "~" init-file-user "/.emacs"))))
397 (load user-init-file t t t)
398 (or inhibit-default-init
399 (let ((inhibit-startup-message nil))
400 ;; Users are supposed to be told their rights.
401 ;; (Plus how to get help and how to undo.)
402 ;; Don't you dare turn this off for anyone
403 ;; except yourself.
404 (load "default" t t)))))))))
405 (if init-file-debug
406 ;; Do this without a condition-case if the user wants to debug.
407 (funcall inner)
408 (condition-case error
409 (progn
410 (funcall inner)
411 (setq init-file-had-error nil))
412 (error (message "Error in init file: %s%s%s"
413 (get (car error) 'error-message)
414 (if (cdr error) ": " "")
415 (mapconcat 'prin1-to-string (cdr error) ", "))
416 (setq init-file-had-error t))))
417 ;; If we can tell that the init file altered debug-on-error,
418 ;; arrange to preserve the value that it set up.
419 (or (eq debug-on-error debug-on-error-initial)
420 (setq debug-on-error-should-be-set t
421 debug-on-error-from-init-file debug-on-error)))
422 (if debug-on-error-should-be-set
423 (setq debug-on-error debug-on-error-from-init-file)))
425 (run-hooks 'after-init-hook)
427 ;; If *scratch* exists and init file didn't change its mode, initialize it.
428 (if (get-buffer "*scratch*")
429 (save-excursion
430 (set-buffer "*scratch*")
431 (if (eq major-mode 'fundamental-mode)
432 (funcall initial-major-mode))))
433 ;; Load library for our terminal type.
434 ;; User init file can set term-file-prefix to nil to prevent this.
435 (and term-file-prefix (not noninteractive) (not window-system)
436 (let ((term (getenv "TERM"))
437 hyphend)
438 (while (and term
439 (not (load (concat term-file-prefix term) t t)))
440 ;; Strip off last hyphen and what follows, then try again
441 (if (setq hyphend (string-match "[-_][^-_]+$" term))
442 (setq term (substring term 0 hyphend))
443 (setq term nil)))))
445 ;; Process the remaining args.
446 (command-line-1 (cdr command-line-args))
448 ;; If -batch, terminate after processing the command options.
449 (if noninteractive (kill-emacs t)))
451 (defun command-line-1 (command-line-args-left)
452 (or noninteractive (input-pending-p) init-file-had-error
453 (and inhibit-startup-echo-area-message
454 (let ((buffer (get-buffer-create " *temp*")))
455 (prog1
456 (condition-case nil
457 (save-excursion
458 (set-buffer buffer)
459 (insert-file-contents user-init-file)
460 (re-search-forward
461 (concat
462 "([ \t\n]*setq[ \t\n]+"
463 "inhibit-startup-echo-area-message[ \t\n]+"
464 (regexp-quote
465 (prin1-to-string
466 (if (string= init-file-user "")
467 (user-login-name)
468 init-file-user)))
469 "[ \t\n]*)")
470 nil t))
471 (error nil))
472 (kill-buffer buffer))))
473 (message (if (eq (key-binding "\C-h\C-p") 'describe-project)
474 "For information about the GNU Project and its goals, type C-h C-p."
475 (substitute-command-keys
476 "For information about the GNU Project and its goals, type \\[describe-project]."))))
477 (if (null command-line-args-left)
478 (cond ((and (not inhibit-startup-message) (not noninteractive)
479 ;; Don't clobber a non-scratch buffer if init file
480 ;; has selected it.
481 (string= (buffer-name) "*scratch*")
482 (not (input-pending-p)))
483 ;; If there are no switches to process, we might as well
484 ;; run this hook now, and there may be some need to do it
485 ;; before doing any output.
486 (and term-setup-hook
487 (run-hooks 'term-setup-hook))
488 ;; Don't let the hook be run twice.
489 (setq term-setup-hook nil)
491 ;; It's important to notice the user settings before we
492 ;; display the startup message; otherwise, the settings
493 ;; won't take effect until the user gives the first
494 ;; keystroke, and that's distracting.
495 (if (fboundp 'frame-notice-user-settings)
496 (frame-notice-user-settings))
498 (and window-setup-hook
499 (run-hooks 'window-setup-hook))
500 (setq window-setup-hook nil)
501 ;; Do this now to avoid an annoying delay if the user
502 ;; clicks the menu bar during the sit-for.
503 (precompute-menubar-bindings)
504 (setq menubar-bindings-done t)
505 (unwind-protect
506 (progn
507 (insert (emacs-version)
509 Copyright (C) 1994 Free Software Foundation, Inc.\n\n")
510 ;; If keys have their default meanings,
511 ;; use precomputed string to save lots of time.
512 (if (and (eq (key-binding "\C-h") 'help-command)
513 (eq (key-binding "\C-xu") 'advertised-undo)
514 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-emacs)
515 (eq (key-binding "\C-h\C-c") 'describe-copying)
516 (eq (key-binding "\C-h\C-d") 'describe-distribution)
517 (eq (key-binding "\C-h\C-w") 'describe-no-warranty)
518 (eq (key-binding "\C-ht") 'help-with-tutorial))
519 (insert
520 "Type C-h for help; C-x u to undo changes. (`C-' means use CTRL key.)
521 To kill the Emacs job, type C-x C-c.
522 Type C-h t for a tutorial on using Emacs.
523 Type C-h i to enter Info, which you can use to read GNU documentation.
525 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
526 You may give out copies of Emacs; type C-h C-c to see the conditions.
527 Type C-h C-d for information on getting the latest version.")
528 (insert (substitute-command-keys
529 "Type \\[help-command] for help; \\[advertised-undo] to undo changes. (`C-' means use CTRL key.)
530 To kill the Emacs job, type \\[save-buffers-kill-emacs].
531 Type \\[help-with-tutorial] for a tutorial on using Emacs.
532 Type \\[info] to enter Info, which you can use to read GNU documentation.
534 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for full details.
535 You may give out copies of Emacs; type \\[describe-copying] to see the conditions.
536 Type \\[describe-distribution] for information on getting the latest version.")))
537 (set-buffer-modified-p nil)
538 (sit-for 120))
539 (save-excursion
540 ;; In case the Emacs server has already selected
541 ;; another buffer, erase the one our message is in.
542 (set-buffer (get-buffer "*scratch*"))
543 (erase-buffer)
544 (set-buffer-modified-p nil)))))
545 (let ((dir command-line-default-directory)
546 (file-count 0)
547 first-file-buffer
548 (line 0))
549 (while command-line-args-left
550 (let ((argi (car command-line-args-left))
551 tem)
552 (setq command-line-args-left (cdr command-line-args-left))
553 (cond ((setq tem (assoc argi command-switch-alist))
554 (funcall (cdr tem) argi))
555 ((or (string-equal argi "-f") ;what the manual claims
556 (string-equal argi "-funcall")
557 (string-equal argi "-e")) ; what the source used to say
558 (setq tem (intern (car command-line-args-left)))
559 (setq command-line-args-left (cdr command-line-args-left))
560 (funcall tem))
561 ((or (string-equal argi "-l")
562 (string-equal argi "-load"))
563 (let ((file (car command-line-args-left)))
564 ;; Take file from default dir if it exists there;
565 ;; otherwise let `load' search for it.
566 (if (file-exists-p (expand-file-name file))
567 (setq file (expand-file-name file)))
568 (load file nil t))
569 (setq command-line-args-left (cdr command-line-args-left)))
570 ((string-equal argi "-insert")
571 (or (stringp (car command-line-args-left))
572 (error "filename omitted from `-insert' option"))
573 (insert-file-contents (car command-line-args-left))
574 (setq command-line-args-left (cdr command-line-args-left)))
575 ((string-equal argi "-kill")
576 (kill-emacs t))
577 ((string-match "^\\+[0-9]+\\'" argi)
578 (setq line (string-to-int argi)))
580 ;; We have almost exhausted our options. See if the
581 ;; user has made any other command-line options available
582 (let ((hooks command-line-functions);; lrs 7/31/89
583 (did-hook nil))
584 (while (and hooks
585 (not (setq did-hook (funcall (car hooks)))))
586 (setq hooks (cdr hooks)))
587 (if (not did-hook)
588 ;; Ok, presume that the argument is a file name
589 (progn
590 (setq file-count (1+ file-count))
591 (cond ((= file-count 1)
592 (setq first-file-buffer
593 (find-file (expand-file-name argi dir))))
595 (find-file-other-window (expand-file-name argi dir))))
596 (or (zerop line)
597 (goto-line line))
598 (setq line 0))))))))
599 ;; If 3 or more files visited, and not all visible,
600 ;; show user what they all are.
601 (if (> file-count 2)
602 (or (get-buffer-window first-file-buffer)
603 (progn (other-window 1)
604 (buffer-menu)))))))
606 ;;; startup.el ends here