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