1 ;;; startup.el --- process Emacs shell arguments
3 ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
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)
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.
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
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
41 ; -u user load user's init file
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
51 ; -insert file insert file into buffer
53 ; -kill kill (exit) emacs
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
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
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
)
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
))
184 ;; If the PWD environment variable isn't accurate, delete it.
185 (let ((pwd (getenv "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
) "."))
192 (concat (file-name-as-directory default-directory
)
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
201 (let ((menubar-bindings-done nil
))
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
)
208 (run-hooks 'term-setup-hook
))
209 ;; Modify the initial frame based on what .emacs puts into
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
])))
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
))))
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
)))))
258 (string-match iso-8859-1-locale-regexp ctype
)))
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
)
276 ;; Every window system should have a startup file;
277 ;; barf if we can't find it.
279 ;; If we can't read it, print the error message and exit.
282 (if (eq (car error
) 'error
)
283 (apply 'concat
(cdr error
))
284 (if (memq 'file-error
(get (car error
) 'error-conditions
))
287 (mapconcat '(lambda (obj) (prin1-to-string obj t
))
288 (cdr (cdr error
)) ", "))
290 (get (car error
) 'error-message
)
291 (mapconcat '(lambda (obj) (prin1-to-string obj t
))
293 'external-debugging-output
)
294 (setq window-system 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
310 (while (and (not done
) args
)
311 (let ((longopts '(("--no-init-file") ("--no-site-file") ("--user")
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
)))
324 (error "Option `%s' is ambiguous" argi
))
325 (setq argi
(substring (car elt
) 1))))))
327 ((or (string-equal argi
"-q")
328 (string-equal argi
"-no-init-file"))
329 (setq init-file-user nil
331 ((or (string-equal argi
"-u")
332 (string-equal argi
"-user"))
334 (setq argval
(car args
)
336 (setq init-file-user argval
339 ((string-equal argi
"-no-site-file")
340 (setq site-run-file nil
342 ((string-equal argi
"-debug-init")
343 (setq init-file-debug t
346 ;; Was argval set but not used?
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
)
356 (if (fboundp '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))
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.
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.
389 ((eq system-type
'ms-dos
)
390 (concat "~" init-file-user
"/_emacs"))
391 ((eq system-type
'vax-vms
)
394 (concat "~" init-file-user
"/.emacs"))))
395 (load user-init-file t t t
)
396 (or inhibit-default-init
397 (let ((inhibit-startup-message nil
))
398 ;; Users are supposed to be told their rights.
399 ;; (Plus how to get help and how to undo.)
400 ;; Don't you dare turn this off for anyone
402 (load "default" t t
)))))))))
404 ;; Do this without a condition-case if the user wants to debug.
406 (condition-case error
409 (setq init-file-had-error nil
))
410 (error (message "Error in init file: %s%s%s"
411 (get (car error
) 'error-message
)
412 (if (cdr error
) ": " "")
413 (mapconcat 'prin1-to-string
(cdr error
) ", "))
414 (setq init-file-had-error t
))))
415 ;; If we can tell that the init file altered debug-on-error,
416 ;; arrange to preserve the value that it set up.
417 (or (eq debug-on-error debug-on-error-initial
)
418 (setq debug-on-error-should-be-set t
419 debug-on-error-from-init-file debug-on-error
)))
420 (if debug-on-error-should-be-set
421 (setq debug-on-error debug-on-error-from-init-file
)))
423 (run-hooks 'after-init-hook
)
425 ;; If *scratch* exists and init file didn't change its mode, initialize it.
426 (if (get-buffer "*scratch*")
428 (set-buffer "*scratch*")
429 (if (eq major-mode
'fundamental-mode
)
430 (funcall initial-major-mode
))))
431 ;; Load library for our terminal type.
432 ;; User init file can set term-file-prefix to nil to prevent this.
433 (and term-file-prefix
(not noninteractive
) (not window-system
)
434 (let ((term (getenv "TERM"))
437 (not (load (concat term-file-prefix term
) t t
)))
438 ;; Strip off last hyphen and what follows, then try again
439 (if (setq hyphend
(string-match "[-_][^-_]+$" term
))
440 (setq term
(substring term
0 hyphend
))
443 ;; Process the remaining args.
444 (command-line-1 (cdr command-line-args
))
446 ;; If -batch, terminate after processing the command options.
447 (if noninteractive
(kill-emacs t
)))
449 (defun command-line-1 (command-line-args-left)
450 (or noninteractive
(input-pending-p) init-file-had-error
451 (and inhibit-startup-echo-area-message
452 (let ((buffer (get-buffer-create " *temp*")))
457 (insert-file-contents user-init-file
)
460 "([ \t\n]*setq[ \t\n]+"
461 "inhibit-startup-echo-area-message[ \t\n]+"
464 (if (string= init-file-user
"")
470 (kill-buffer buffer
))))
471 (message (if (eq (key-binding "\C-h\C-p") 'describe-project
)
472 "For information about the GNU Project and its goals, type C-h C-p."
473 (substitute-command-keys
474 "For information about the GNU Project and its goals, type \\[describe-project]."))))
475 (if (null command-line-args-left
)
476 (cond ((and (not inhibit-startup-message
) (not noninteractive
)
477 ;; Don't clobber a non-scratch buffer if init file
479 (string= (buffer-name) "*scratch*")
480 (not (input-pending-p)))
481 ;; If there are no switches to process, we might as well
482 ;; run this hook now, and there may be some need to do it
483 ;; before doing any output.
485 (run-hooks 'term-setup-hook
))
486 ;; Don't let the hook be run twice.
487 (setq term-setup-hook nil
)
489 ;; It's important to notice the user settings before we
490 ;; display the startup message; otherwise, the settings
491 ;; won't take effect until the user gives the first
492 ;; keystroke, and that's distracting.
493 (if (fboundp 'frame-notice-user-settings
)
494 (frame-notice-user-settings))
496 (and window-setup-hook
497 (run-hooks 'window-setup-hook
))
498 (setq window-setup-hook nil
)
499 ;; Do this now to avoid an annoying delay if the user
500 ;; clicks the menu bar during the sit-for.
501 (precompute-menubar-bindings)
502 (setq menubar-bindings-done t
)
505 (insert (emacs-version)
507 Copyright (C) 1994 Free Software Foundation, Inc.\n\n")
508 ;; If keys have their default meanings,
509 ;; use precomputed string to save lots of time.
510 (if (and (eq (key-binding "\C-h") 'help-command
)
511 (eq (key-binding "\C-xu") 'advertised-undo
)
512 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-emacs
)
513 (eq (key-binding "\C-h\C-c") 'describe-copying
)
514 (eq (key-binding "\C-h\C-d") 'describe-distribution
)
515 (eq (key-binding "\C-h\C-w") 'describe-no-warranty
)
516 (eq (key-binding "\C-ht") 'help-with-tutorial
))
518 "Type C-h for help; C-x u to undo changes. (`C-' means use CTRL key.)
519 To kill the Emacs job, type C-x C-c.
520 Type C-h t for a tutorial on using Emacs.
521 Type C-h i to enter Info, which you can use to read GNU documentation.
523 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
524 You may give out copies of Emacs; type C-h C-c to see the conditions.
525 Type C-h C-d for information on getting the latest version.")
526 (insert (substitute-command-keys
527 "Type \\[help-command] for help; \\[advertised-undo] to undo changes. (`C-' means use CTRL key.)
528 To kill the Emacs job, type \\[save-buffers-kill-emacs].
529 Type \\[help-with-tutorial] for a tutorial on using Emacs.
530 Type \\[info] to enter Info, which you can use to read GNU documentation.
532 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for full details.
533 You may give out copies of Emacs; type \\[describe-copying] to see the conditions.
534 Type \\[describe-distribution] for information on getting the latest version.")))
535 (set-buffer-modified-p nil
)
538 ;; In case the Emacs server has already selected
539 ;; another buffer, erase the one our message is in.
540 (set-buffer (get-buffer "*scratch*"))
542 (set-buffer-modified-p nil
)))))
543 (let ((dir command-line-default-directory
)
547 (while command-line-args-left
548 (let ((argi (car command-line-args-left
))
550 (setq command-line-args-left
(cdr command-line-args-left
))
551 (cond ((setq tem
(assoc argi command-switch-alist
))
552 (funcall (cdr tem
) argi
))
553 ((or (string-equal argi
"-f") ;what the manual claims
554 (string-equal argi
"-funcall")
555 (string-equal argi
"-e")) ; what the source used to say
556 (setq tem
(intern (car command-line-args-left
)))
557 (setq command-line-args-left
(cdr command-line-args-left
))
559 ((or (string-equal argi
"-l")
560 (string-equal argi
"-load"))
561 (let ((file (car command-line-args-left
)))
562 ;; Take file from default dir if it exists there;
563 ;; otherwise let `load' search for it.
564 (if (file-exists-p (expand-file-name file
))
565 (setq file
(expand-file-name file
)))
567 (setq command-line-args-left
(cdr command-line-args-left
)))
568 ((string-equal argi
"-insert")
569 (or (stringp (car command-line-args-left
))
570 (error "filename omitted from `-insert' option"))
571 (insert-file-contents (car command-line-args-left
))
572 (setq command-line-args-left
(cdr command-line-args-left
)))
573 ((string-equal argi
"-kill")
575 ((string-match "^\\+[0-9]+\\'" argi
)
576 (setq line
(string-to-int argi
)))
578 ;; We have almost exhausted our options. See if the
579 ;; user has made any other command-line options available
580 (let ((hooks command-line-functions
);; lrs 7/31/89
583 (not (setq did-hook
(funcall (car hooks
)))))
584 (setq hooks
(cdr hooks
)))
586 ;; Ok, presume that the argument is a file name
588 (setq file-count
(1+ file-count
))
589 (cond ((= file-count
1)
590 (setq first-file-buffer
591 (find-file (expand-file-name argi dir
))))
593 (find-file-other-window (expand-file-name argi dir
))))
597 ;; If 3 or more files visited, and not all visible,
598 ;; show user what they all are.
600 (or (get-buffer-window first-file-buffer
)
601 (progn (other-window 1)
604 ;;; startup.el ends here