Keybindings don't overshadown comint bindings.
[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
JB
25
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
45; These are processed in the order encountered.
46; -f function execute function
47; -funcall function same
48; -l file load file
49; -load file same
a726e0d1
JB
50; -insert file same
51; file visit file
52; -kill kill (exit) emacs
53
630cc463
ER
54;;; Code:
55
a726e0d1
JB
56(setq top-level '(normal-top-level))
57
58(defvar command-line-processed nil "t once command line has been processed")
59
60(defconst inhibit-startup-message nil
1d7da582 61 "*Non-nil inhibits the initial startup message.
a726e0d1
JB
62This is for use in your personal init file, once you are familiar
63with the contents of the startup message.")
64
1d7da582
RS
65(defconst inhibit-startup-echo-area-message nil
66 "*Non-nil inhibits the initial startup echo area message.
67Inhibition takes effect only if your `.emacs' file contains
e5575c06 68a line of this form:
54a003f7 69 (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
e5575c06
RS
70If your `.emacs' file is byte-compiled, use the following form instead:
71 (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
1d7da582
RS
72Thus, someone else using a copy of your `.emacs' file will see
73the startup message unless he personally acts to inhibit it.")
74
a726e0d1
JB
75(defconst inhibit-default-init nil
76 "*Non-nil inhibits loading the `default' library.")
77
78(defconst command-switch-alist nil
79 "Alist of command-line switches.
80Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
81HANDLER-FUNCTION receives switch name as sole arg;
82remaining command-line args are in the variable `command-line-args-left'.")
83
860befc8
RS
84(defvar command-line-args-left nil
85 "List of command-line args not yet processed.")
86
a726e0d1
JB
87(defvar command-line-functions nil ;; lrs 7/31/89
88 "List of functions to process unrecognized command-line arguments.
89Each function should access the dynamically bound variables
b4484ea8 90`argi' (the current argument) and `command-line-args-left' (the remaining
a726e0d1 91arguments). The function should return non-nil only if it recognizes and
b4484ea8
RS
92processes `argi'. If it does so, it may consume successive arguments by
93altering `command-line-args-left' to remove them.")
a726e0d1 94
09a1077c
RS
95(defvar command-line-default-directory nil
96 "Default directory to use for command line arguments.
97This is normally copied from `default-directory' when Emacs starts.")
98
e3bd99f5 99(defvar before-init-hook nil
b4484ea8 100 "Functions to call after handling urgent options but before init files.
0cc89026 101The frame system uses this to open frames to display messages while
3fc958a4
JB
102Emacs loads the user's initialization file.")
103
e3bd99f5 104(defvar after-init-hook nil
a0965605 105 "Functions to call after loading the init file (`~/.emacs').
e3bd99f5 106The call is not protected by a condition-case, so you can set `debug-on-error'
a0965605 107in `.emacs', and put all the actual code on `after-init-hook'.")
e3bd99f5 108
a726e0d1 109(defvar term-setup-hook nil
b4484ea8 110 "Functions to be called after loading terminal-specific Lisp code.
e3bd99f5 111See `run-hooks'. This variable exists for users to set,
a726e0d1
JB
112so as to override the definitions made by the terminal-specific file.
113Emacs never sets this variable itself.")
114
115(defvar keyboard-type nil
b4484ea8
RS
116 "The brand of keyboard you are using.
117This variable is used to define
a726e0d1
JB
118the proper function and keypad keys for use under X. It is used in a
119fashion analogous to the environment value TERM.")
120
121(defvar window-setup-hook nil
b4484ea8
RS
122 "Normal hook run to initialize window system display.
123Emacs runs this hook after processing the command line arguments and loading
124the user's init file.")
a726e0d1
JB
125
126(defconst initial-major-mode 'lisp-interaction-mode
127 "Major mode command symbol to use for the initial *scratch* buffer.")
128
129(defvar init-file-user nil
130 "Identity of user whose `.emacs' file is or was read.
2bdfaa42
KH
131The value is nil if no init file is being used; otherwise, it may be either
132the null string, meaning that the init file was taken from the user that
133originally logged in, or it may be a string containing a user's name.
a726e0d1 134
2bdfaa42
KH
135In either of the latter cases, `(concat \"~\" init-file-user \"/\")'
136evaluates to the name of the directory where the `.emacs' file was
137looked for.")
a726e0d1 138
b7444d31
RS
139(defvar site-run-file "site-start"
140 "File containing site-wide run-time initializations.
141This file is loaded at run-time before `~/.emacs'. It contains inits
142that need to be in place for the entire site, but which, due to their
143higher incidence of change, don't make sense to load into emacs'
144dumped image. Thus, the run-time load order is: 1. file described in
145this variable, if non-nil; 2. `~/.emacs'; 3. `default.el'.")
146
30dc01ea
RS
147(defconst iso-8859-1-locale-regexp "8859[-_]?1"
148 "Regexp that specifies when to enable the ISO 8859-1 character set.
149We do that if this regexp matches the locale name
150specified by the LC_ALL, LC_CTYPE and LANG environment variables.")
151
c10d1f06
RS
152(defvar user-mail-address nil
153 "Full mailing address of this user.")
154
a726e0d1
JB
155(defvar init-file-debug nil)
156
52320897
RS
157(defvar init-file-had-error nil)
158
a726e0d1
JB
159(defun normal-top-level ()
160 (if command-line-processed
161 (message "Back to top level.")
162 (setq command-line-processed t)
ffd56f97 163 (if (not (eq system-type 'vax-vms))
a4b33896
JB
164 (progn
165 ;; If the PWD environment variable isn't accurate, delete it.
166 (let ((pwd (getenv "PWD")))
167 (and (stringp pwd)
168 ;; Use FOO/., so that if FOO is a symlink, file-attributes
169 ;; describes the directory linked to, not FOO itself.
170 (or (equal (file-attributes
171 (concat (file-name-as-directory pwd) "."))
172 (file-attributes
173 (concat (file-name-as-directory default-directory)
174 ".")))
175 (setq process-environment
176 (delete (concat "PWD=" pwd)
177 process-environment)))))))
492878e4 178 (setq default-directory (abbreviate-file-name default-directory))
c10d1f06 179 (setq user-mail-address (concat (user-login-name) "@" (system-name)))
6f2c86fa
KH
180 (let ((menubar-bindings-done nil))
181 (unwind-protect
182 (command-line)
183 ;; Do this again, in case .emacs defined more abbreviations.
184 (setq default-directory (abbreviate-file-name default-directory))
185 (run-hooks 'emacs-startup-hook)
186 (and term-setup-hook
187 (run-hooks 'term-setup-hook))
188 ;; Modify the initial frame based on what .emacs puts into
189 ;; ...-frame-alist.
190 (if (fboundp 'frame-notice-user-settings)
191 (frame-notice-user-settings))
192 ;; Now we know the user's default font, so add it to the menu.
193 (if (fboundp 'font-menu-add-default)
194 (font-menu-add-default))
195 (and window-setup-hook
196 (run-hooks 'window-setup-hook))
197 (or menubar-bindings-done
198 (precompute-menubar-bindings))))))
199
200;; Precompute the keyboard equivalents in the menu bar items.
201(defun precompute-menubar-bindings ()
42a49b29 202 (if (eq window-system 'x)
6f2c86fa
KH
203 (let ((submap (lookup-key global-map [menu-bar])))
204 (while submap
205 (and (consp (car submap))
206 (symbolp (car (car submap)))
207 (stringp (car-safe (cdr (car submap))))
208 (keymapp (cdr (cdr (car submap))))
209 (x-popup-menu nil (cdr (cdr (car submap)))))
210 (setq submap (cdr submap))))))
a726e0d1
JB
211
212(defun command-line ()
09a1077c
RS
213 (setq command-line-default-directory default-directory)
214
74f2ab06 215 ;; See if we should import version-control from the environment variable.
a726e0d1
JB
216 (let ((vc (getenv "VERSION_CONTROL")))
217 (cond ((eq vc nil)) ;don't do anything if not set
218 ((or (string= vc "t")
219 (string= vc "numbered"))
220 (setq version-control t))
221 ((or (string= vc "nil")
222 (string= vc "existing"))
223 (setq version-control nil))
224 ((or (string= vc "never")
225 (string= vc "simple"))
226 (setq version-control 'never))))
227
30dc01ea
RS
228 (if (let ((ctype
229 ;; Use the first of these three envvars that has a nonempty value.
230 (or (let ((string (getenv "LC_ALL")))
231 (and (not (equal string "")) string))
232 (let ((string (getenv "LC_CTYPE")))
233 (and (not (equal string "")) string))
234 (let ((string (getenv "LANG")))
235 (and (not (equal string "")) string)))))
236 (and ctype
237 (string-match iso-8859-1-locale-regexp ctype)))
e9d8e8c7
RS
238 (progn
239 (standard-display-european t)
240 (require 'iso-syntax)))
241
79058860
JB
242 ;;! This has been commented out; I currently find the behavior when
243 ;;! split-window-keep-point is nil disturbing, but if I can get used
244 ;;! to it, then it would be better to eliminate the option.
245 ;;! ;; Choose a good default value for split-window-keep-point.
246 ;;! (setq split-window-keep-point (> baud-rate 2400))
f35fe3c6 247
a726e0d1
JB
248 ;; Read window system's init file if using a window system.
249 (if (and window-system (not noninteractive))
c35f9044
JB
250 (load (concat term-file-prefix
251 (symbol-name window-system)
252 "-win")
253 ;; Every window system should have a startup file;
254 ;; barf if we can't find it.
255 nil t))
a726e0d1 256
03e3c30a
JB
257 (let ((done nil)
258 (args (cdr command-line-args)))
259
a726e0d1
JB
260 ;; Figure out which user's init file to load,
261 ;; either from the environment or from the options.
262 (setq init-file-user (if noninteractive nil (user-login-name)))
263 ;; If user has not done su, use current $HOME to find .emacs.
264 (and init-file-user (string= init-file-user (user-real-login-name))
265 (setq init-file-user ""))
03e3c30a
JB
266
267 ;; Process the command-line args, and delete the arguments
268 ;; processed. This is consistent with the way main in emacs.c
269 ;; does things.
a726e0d1
JB
270 (while (and (not done) args)
271 (let ((argi (car args)))
272 (cond
273 ((or (string-equal argi "-q")
274 (string-equal argi "-no-init-file"))
275 (setq init-file-user nil
276 args (cdr args)))
277 ((or (string-equal argi "-u")
278 (string-equal argi "-user"))
279 (setq args (cdr args)
280 init-file-user (car args)
281 args (cdr args)))
b7444d31
RS
282 ((string-equal argi "-no-site-file")
283 (setq site-run-file nil
284 args (cdr args)))
a726e0d1
JB
285 ((string-equal argi "-debug-init")
286 (setq init-file-debug t
287 args (cdr args)))
03e3c30a
JB
288 (t (setq done t)))))
289
290 ;; Re-attach the program name to the front of the arg list.
291 (setcdr command-line-args args))
a726e0d1 292
c722566c 293 ;; Under X Windows, this creates the X frame and deletes the terminal frame.
4e1b1e72
JB
294 (if (fboundp 'face-initialize)
295 (face-initialize))
853ccbd5
RS
296 (if (fboundp 'frame-initialize)
297 (frame-initialize))
c722566c 298
e3bd99f5 299 (run-hooks 'before-init-hook)
3fc958a4 300
09973c54
RM
301 ;; Run the site-start library if it exists. The point of this file is
302 ;; that it is run before .emacs. There is no point in doing this after
303 ;; .emacs; that is useless.
b7444d31
RS
304 (if site-run-file
305 (load site-run-file t t))
09973c54 306
8a988f45
RS
307 ;; Sites should not disable this. Only individuals should disable
308 ;; the startup message.
309 (setq inhibit-startup-message nil)
310
a726e0d1 311 ;; Load that user's init file, or the default one, or none.
3d1b78f0
RS
312 (let (debug-on-error-from-init-file
313 debug-on-error-should-be-set
314 (debug-on-error-initial
315 (if (eq init-file-debug t) 'startup init-file-debug)))
316 (let ((debug-on-error debug-on-error-initial)
317 ;; This function actually reads the init files.
318 (inner
319 (function
320 (lambda ()
321 (if init-file-user
a4c5c705
RS
322 (progn
323 (setq user-init-file
324 (cond
325 ((eq system-type 'ms-dos)
326 (concat "~" init-file-user "/_emacs"))
327 ((eq system-type 'vax-vms)
328 "sys$login:.emacs")
329 (t
330 (concat "~" init-file-user "/.emacs"))))
331 (load user-init-file t t t)
332 (or inhibit-default-init
333 (let ((inhibit-startup-message nil))
334 ;; Users are supposed to be told their rights.
335 ;; (Plus how to get help and how to undo.)
336 ;; Don't you dare turn this off for anyone
337 ;; except yourself.
338 (load "default" t t)))))))))
3d1b78f0
RS
339 (if init-file-debug
340 ;; Do this without a condition-case if the user wants to debug.
341 (funcall inner)
342 (condition-case error
343 (progn
344 (funcall inner)
345 (setq init-file-had-error nil))
346 (error (message "Error in init file: %s%s%s"
347 (get (car error) 'error-message)
ad2aeb8d 348 (if (cdr error) ": " "")
3d1b78f0
RS
349 (mapconcat 'prin1-to-string (cdr error) ", "))
350 (setq init-file-had-error t))))
ad2aeb8d 351 ;; If we can tell that the init file altered debug-on-error,
3d1b78f0
RS
352 ;; arrange to preserve the value that it set up.
353 (or (eq debug-on-error debug-on-error-initial)
354 (setq debug-on-error-should-be-set t
355 debug-on-error-from-init-file debug-on-error)))
356 (if debug-on-error-should-be-set
357 (setq debug-on-error debug-on-error-from-init-file)))
3fc958a4 358
e3bd99f5
RM
359 (run-hooks 'after-init-hook)
360
a726e0d1
JB
361 ;; If *scratch* exists and init file didn't change its mode, initialize it.
362 (if (get-buffer "*scratch*")
363 (save-excursion
364 (set-buffer "*scratch*")
365 (if (eq major-mode 'fundamental-mode)
366 (funcall initial-major-mode))))
367 ;; Load library for our terminal type.
368 ;; User init file can set term-file-prefix to nil to prevent this.
369 (and term-file-prefix (not noninteractive) (not window-system)
370 (let ((term (getenv "TERM"))
371 hyphend)
372 (while (and term
373 (not (load (concat term-file-prefix term) t t)))
374 ;; Strip off last hyphen and what follows, then try again
375 (if (setq hyphend (string-match "[-_][^-_]+$" term))
376 (setq term (substring term 0 hyphend))
377 (setq term nil)))))
378
03e3c30a 379 ;; Process the remaining args.
a726e0d1
JB
380 (command-line-1 (cdr command-line-args))
381
382 ;; If -batch, terminate after processing the command options.
383 (if noninteractive (kill-emacs t)))
384
385(defun command-line-1 (command-line-args-left)
52320897 386 (or noninteractive (input-pending-p) init-file-had-error
1d7da582
RS
387 (and inhibit-startup-echo-area-message
388 (let ((buffer (get-buffer-create " *temp*")))
389 (prog1
390 (condition-case nil
391 (save-excursion
392 (set-buffer buffer)
393 (insert-file-contents user-init-file)
394 (re-search-forward
395 (concat
d9a71a8f
RS
396 "([ \t\n]*setq[ \t\n]+"
397 "inhibit-startup-echo-area-message[ \t\n]+"
398 (regexp-quote
399 (prin1-to-string
400 (if (string= init-file-user "")
401 (user-login-name)
402 init-file-user)))
403 "[ \t\n]*)")
1d7da582
RS
404 nil t))
405 (error nil))
406 (kill-buffer buffer))))
407 (message (if (eq (key-binding "\C-h\C-p") 'describe-project)
408 "For information about the GNU Project and its goals, type C-h C-p."
409 (substitute-command-keys
410 "For information about the GNU Project and its goals, type \\[describe-project]."))))
a726e0d1
JB
411 (if (null command-line-args-left)
412 (cond ((and (not inhibit-startup-message) (not noninteractive)
413 ;; Don't clobber a non-scratch buffer if init file
414 ;; has selected it.
415 (string= (buffer-name) "*scratch*")
416 (not (input-pending-p)))
417 ;; If there are no switches to process, we might as well
418 ;; run this hook now, and there may be some need to do it
419 ;; before doing any output.
420 (and term-setup-hook
421 (run-hooks 'term-setup-hook))
422 ;; Don't let the hook be run twice.
423 (setq term-setup-hook nil)
4e1b1e72
JB
424
425 ;; It's important to notice the user settings before we
426 ;; display the startup message; otherwise, the settings
427 ;; won't take effect until the user gives the first
428 ;; keystroke, and that's distracting.
429 (if (fboundp 'frame-notice-user-settings)
430 (frame-notice-user-settings))
431
a726e0d1
JB
432 (and window-setup-hook
433 (run-hooks 'window-setup-hook))
434 (setq window-setup-hook nil)
fd11871a
RS
435 ;; Do this now to avoid an annoying delay if the user
436 ;; clicks the menu bar during the sit-for.
437 (precompute-menubar-bindings)
438 (setq menubar-bindings-done t)
a726e0d1
JB
439 (unwind-protect
440 (progn
441 (insert (emacs-version)
442 "
1d7da582 443Copyright (C) 1994 Free Software Foundation, Inc.\n\n")
a726e0d1
JB
444 ;; If keys have their default meanings,
445 ;; use precomputed string to save lots of time.
446 (if (and (eq (key-binding "\C-h") 'help-command)
447 (eq (key-binding "\C-xu") 'advertised-undo)
448 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-emacs)
449 (eq (key-binding "\C-h\C-c") 'describe-copying)
450 (eq (key-binding "\C-h\C-d") 'describe-distribution)
451 (eq (key-binding "\C-h\C-w") 'describe-no-warranty)
452 (eq (key-binding "\C-ht") 'help-with-tutorial))
453 (insert
454 "Type C-h for help; C-x u to undo changes. (`C-' means use CTRL key.)
455To kill the Emacs job, type C-x C-c.
456Type C-h t for a tutorial on using Emacs.
54a0539a 457Type C-h i to enter Info, which you can use to read GNU documentation.
a726e0d1
JB
458
459GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
460You may give out copies of Emacs; type C-h C-c to see the conditions.
461Type C-h C-d for information on getting the latest version.")
462 (insert (substitute-command-keys
463 "Type \\[help-command] for help; \\[advertised-undo] to undo changes. (`C-' means use CTRL key.)
464To kill the Emacs job, type \\[save-buffers-kill-emacs].
465Type \\[help-with-tutorial] for a tutorial on using Emacs.
54a0539a 466Type \\[info] to enter Info, which you can use to read GNU documentation.
a726e0d1
JB
467
468GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for full details.
469You may give out copies of Emacs; type \\[describe-copying] to see the conditions.
470Type \\[describe-distribution] for information on getting the latest version.")))
471 (set-buffer-modified-p nil)
472 (sit-for 120))
473 (save-excursion
474 ;; In case the Emacs server has already selected
475 ;; another buffer, erase the one our message is in.
476 (set-buffer (get-buffer "*scratch*"))
477 (erase-buffer)
478 (set-buffer-modified-p nil)))))
09a1077c 479 (let ((dir command-line-default-directory)
a726e0d1
JB
480 (file-count 0)
481 first-file-buffer
482 (line 0))
483 (while command-line-args-left
484 (let ((argi (car command-line-args-left))
485 tem)
486 (setq command-line-args-left (cdr command-line-args-left))
487 (cond ((setq tem (assoc argi command-switch-alist))
488 (funcall (cdr tem) argi))
a726e0d1
JB
489 ((or (string-equal argi "-f") ;what the manual claims
490 (string-equal argi "-funcall")
491 (string-equal argi "-e")) ; what the source used to say
492 (setq tem (intern (car command-line-args-left)))
493 (setq command-line-args-left (cdr command-line-args-left))
494 (funcall tem))
495 ((or (string-equal argi "-l")
496 (string-equal argi "-load"))
497 (let ((file (car command-line-args-left)))
498 ;; Take file from default dir if it exists there;
499 ;; otherwise let `load' search for it.
500 (if (file-exists-p (expand-file-name file))
501 (setq file (expand-file-name file)))
502 (load file nil t))
503 (setq command-line-args-left (cdr command-line-args-left)))
fbce8654 504 ((string-equal argi "-insert")
cf91c6c8 505 (or (stringp (car command-line-args-left))
fbce8654 506 (error "filename omitted from `-insert' option"))
a726e0d1
JB
507 (insert-file-contents (car command-line-args-left))
508 (setq command-line-args-left (cdr command-line-args-left)))
509 ((string-equal argi "-kill")
510 (kill-emacs t))
511 ((string-match "^\\+[0-9]+\\'" argi)
512 (setq line (string-to-int argi)))
513 (t
514 ;; We have almost exhausted our options. See if the
515 ;; user has made any other command-line options available
516 (let ((hooks command-line-functions);; lrs 7/31/89
517 (did-hook nil))
518 (while (and hooks
519 (not (setq did-hook (funcall (car hooks)))))
520 (setq hooks (cdr hooks)))
521 (if (not did-hook)
522 ;; Ok, presume that the argument is a file name
523 (progn
524 (setq file-count (1+ file-count))
525 (cond ((= file-count 1)
526 (setq first-file-buffer
527 (find-file (expand-file-name argi dir))))
528 (t
529 (find-file-other-window (expand-file-name argi dir))))
530 (or (zerop line)
531 (goto-line line))
532 (setq line 0))))))))
533 ;; If 3 or more files visited, and not all visible,
534 ;; show user what they all are.
535 (if (> file-count 2)
536 (or (get-buffer-window first-file-buffer)
7e0795a4 537 (progn (other-window 1)
e8997612 538 (buffer-menu)))))))
c88ab9ce
ER
539
540;;; startup.el ends here