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