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