Set no-byte-compile local variable t to work around a byte-compiler bug.
[emacs.git] / lisp / startup.el
blobda88df9e8e1f8de3ec81881783c23f9bddc8088e
1 ;;; startup.el --- process Emacs shell arguments
3 ;; Copyright (C) 1985, 1986, 1992 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 ; -i file insert file into buffer
51 ; -insert file same
52 ; file visit file
53 ; -kill kill (exit) emacs
55 ;;; Code:
57 (setq top-level '(normal-top-level))
59 (defvar command-line-processed nil "t once command line has been processed")
61 (defconst inhibit-startup-message nil
62 "*Non-nil inhibits the initial startup messages.
63 This is for use in your personal init file, once you are familiar
64 with the contents of the startup message.")
66 (defconst inhibit-default-init nil
67 "*Non-nil inhibits loading the `default' library.")
69 (defconst command-switch-alist nil
70 "Alist of command-line switches.
71 Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
72 HANDLER-FUNCTION receives switch name as sole arg;
73 remaining command-line args are in the variable `command-line-args-left'.")
75 (defvar command-line-functions nil ;; lrs 7/31/89
76 "List of functions to process unrecognized command-line arguments.
77 Each function should access the dynamically bound variables
78 argi (the current argument) and command-line-args-left (the remaining
79 arguments). The function should return non-nil only if it recognizes and
80 processes argi. If it does so, it may consume successive arguments by
81 altering command-line-args-left to remove them.")
83 (defvar before-init-hook nil
84 "Functions to call after handling urgent options but before loading init file.
85 The frame system uses this to open frames to display messages while
86 Emacs loads the user's initialization file.")
88 (defvar after-init-hook nil
89 "Functions to call after loading the init file (`~/.emacs').
90 The call is not protected by a condition-case, so you can set `debug-on-error'
91 in `.emacs', and put all the actual code on `after-init-hook'.")
93 (defvar term-setup-hook nil
94 "Functions to be called after loading terminal-specific lisp code.
95 See `run-hooks'. This variable exists for users to set,
96 so as to override the definitions made by the terminal-specific file.
97 Emacs never sets this variable itself.")
99 (defvar keyboard-type nil
100 "The brand of keyboard you are using. This variable is used to define
101 the proper function and keypad keys for use under X. It is used in a
102 fashion analogous to the environment value TERM.")
104 (defvar window-setup-hook nil
105 "Function called to initialize window system display.
106 Emacs calls this after processing the command line arguments and loading
107 the user's init file.
109 Users should not set this variable; use term-setup-hook instead.")
111 (defconst initial-major-mode 'lisp-interaction-mode
112 "Major mode command symbol to use for the initial *scratch* buffer.")
114 (defvar init-file-user nil
115 "Identity of user whose `.emacs' file is or was read.
116 The value may be the null string or a string containing a user's name.
117 If the value is a null string, it means that the init file was taken from
118 the user that originally logged in.
120 In all cases, `(concat \"~\" init-file-user \"/\")' evaluates to the
121 directory name of the directory where the `.emacs' file was looked for.")
123 (defvar init-file-debug nil)
125 (defun normal-top-level ()
126 (if command-line-processed
127 (message "Back to top level.")
128 (setq command-line-processed t)
129 (if (not (eq system-type 'vax-vms))
130 (progn
131 ;; If the PWD environment variable isn't accurate, delete it.
132 (let ((pwd (getenv "PWD")))
133 (and (stringp pwd)
134 ;; Use FOO/., so that if FOO is a symlink, file-attributes
135 ;; describes the directory linked to, not FOO itself.
136 (or (equal (file-attributes
137 (concat (file-name-as-directory pwd) "."))
138 (file-attributes
139 (concat (file-name-as-directory default-directory)
140 ".")))
141 (setq process-environment
142 (delete (concat "PWD=" pwd)
143 process-environment)))))))
144 (setq default-directory (abbreviate-file-name default-directory))
145 (unwind-protect
146 (command-line)
147 (run-hooks 'emacs-startup-hook)
148 (and term-setup-hook
149 (run-hooks 'term-setup-hook))
150 (and window-setup-hook
151 (run-hooks 'window-setup-hook)))))
153 (defun command-line ()
154 ;; See if we should import version-control from the environment variable.
155 (let ((vc (getenv "VERSION_CONTROL")))
156 (cond ((eq vc nil)) ;don't do anything if not set
157 ((or (string= vc "t")
158 (string= vc "numbered"))
159 (setq version-control t))
160 ((or (string= vc "nil")
161 (string= vc "existing"))
162 (setq version-control nil))
163 ((or (string= vc "never")
164 (string= vc "simple"))
165 (setq version-control 'never))))
167 ;;! This has been commented out; I currently find the behavior when
168 ;;! split-window-keep-point is nil disturbing, but if I can get used
169 ;;! to it, then it would be better to eliminate the option.
170 ;;! ;; Choose a good default value for split-window-keep-point.
171 ;;! (setq split-window-keep-point (> baud-rate 2400))
173 ;; Read window system's init file if using a window system.
174 (if (and window-system (not noninteractive))
175 (load (concat term-file-prefix
176 (symbol-name window-system)
177 "-win")
178 ;; Every window system should have a startup file;
179 ;; barf if we can't find it.
180 nil t))
182 (let ((done nil)
183 (args (cdr command-line-args)))
185 ;; Figure out which user's init file to load,
186 ;; either from the environment or from the options.
187 (setq init-file-user (if noninteractive nil (user-login-name)))
188 ;; If user has not done su, use current $HOME to find .emacs.
189 (and init-file-user (string= init-file-user (user-real-login-name))
190 (setq init-file-user ""))
192 ;; Process the command-line args, and delete the arguments
193 ;; processed. This is consistent with the way main in emacs.c
194 ;; does things.
195 (while (and (not done) args)
196 (let ((argi (car args)))
197 (cond
198 ((or (string-equal argi "-q")
199 (string-equal argi "-no-init-file"))
200 (setq init-file-user nil
201 args (cdr args)))
202 ((or (string-equal argi "-u")
203 (string-equal argi "-user"))
204 (setq args (cdr args)
205 init-file-user (car args)
206 args (cdr args)))
207 ((string-equal argi "-debug-init")
208 (setq init-file-debug t
209 args (cdr args)))
210 (t (setq done t)))))
212 ;; Re-attach the program name to the front of the arg list.
213 (setcdr command-line-args args))
215 (run-hooks 'before-init-hook)
217 ;; Run the site-start library if it exists. The point of this file is
218 ;; that it is run before .emacs. There is no point in doing this after
219 ;; .emacs; that is useless.
220 (load "site-start" t t)
222 ;; Load that user's init file, or the default one, or none.
223 (let ((debug-on-error init-file-debug)
224 ;; This function actually reads the init files.
225 (inner
226 (function
227 (lambda ()
228 (if init-file-user
229 (progn (load (if (eq system-type 'vax-vms)
230 "sys$login:.emacs"
231 (concat "~" init-file-user "/.emacs"))
232 t t t)
233 (or inhibit-default-init
234 (let ((inhibit-startup-message nil))
235 ;; Users are supposed to be told their rights.
236 ;; (Plus how to get help and how to undo.)
237 ;; Don't you dare turn this off for anyone
238 ;; except yourself.
239 (load "default" t t)))))))))
240 (if init-file-debug
241 ;; Do this without a condition-case if the user wants to debug.
242 (funcall inner)
243 (condition-case error
244 (funcall inner)
245 (error (message "Error in init file: %s%s%s"
246 (get (car error) 'error-message)
247 (if (cdr error) ": ")
248 (mapconcat 'prin1-to-string (cdr error) ", "))))))
250 (run-hooks 'after-init-hook)
252 ;; If *scratch* exists and init file didn't change its mode, initialize it.
253 (if (get-buffer "*scratch*")
254 (save-excursion
255 (set-buffer "*scratch*")
256 (if (eq major-mode 'fundamental-mode)
257 (funcall initial-major-mode))))
258 ;; Load library for our terminal type.
259 ;; User init file can set term-file-prefix to nil to prevent this.
260 (and term-file-prefix (not noninteractive) (not window-system)
261 (let ((term (getenv "TERM"))
262 hyphend)
263 (while (and term
264 (not (load (concat term-file-prefix term) t t)))
265 ;; Strip off last hyphen and what follows, then try again
266 (if (setq hyphend (string-match "[-_][^-_]+$" term))
267 (setq term (substring term 0 hyphend))
268 (setq term nil)))))
270 ;; Process the remaining args.
271 (command-line-1 (cdr command-line-args))
273 ;; If -batch, terminate after processing the command options.
274 (if noninteractive (kill-emacs t)))
276 (defun command-line-1 (command-line-args-left)
277 (if (null command-line-args-left)
278 (cond ((and (not inhibit-startup-message) (not noninteractive)
279 ;; Don't clobber a non-scratch buffer if init file
280 ;; has selected it.
281 (string= (buffer-name) "*scratch*")
282 (not (input-pending-p)))
283 ;; If there are no switches to process, we might as well
284 ;; run this hook now, and there may be some need to do it
285 ;; before doing any output.
286 (and term-setup-hook
287 (run-hooks 'term-setup-hook))
288 ;; Don't let the hook be run twice.
289 (setq term-setup-hook nil)
290 (and window-setup-hook
291 (run-hooks 'window-setup-hook))
292 (setq window-setup-hook nil)
293 (unwind-protect
294 (progn
295 (insert (emacs-version)
297 Copyright (C) 1993 Free Software Foundation, Inc.\n\n")
298 ;; If keys have their default meanings,
299 ;; use precomputed string to save lots of time.
300 (if (and (eq (key-binding "\C-h") 'help-command)
301 (eq (key-binding "\C-xu") 'advertised-undo)
302 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-emacs)
303 (eq (key-binding "\C-h\C-c") 'describe-copying)
304 (eq (key-binding "\C-h\C-d") 'describe-distribution)
305 (eq (key-binding "\C-h\C-w") 'describe-no-warranty)
306 (eq (key-binding "\C-ht") 'help-with-tutorial))
307 (insert
308 "Type C-h for help; C-x u to undo changes. (`C-' means use CTRL key.)
309 To kill the Emacs job, type C-x C-c.
310 Type C-h t for a tutorial on using Emacs.
311 Type C-h i to enter Info, which you can use to read GNU documentation.
313 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
314 You may give out copies of Emacs; type C-h C-c to see the conditions.
315 Type C-h C-d for information on getting the latest version.")
316 (insert (substitute-command-keys
317 "Type \\[help-command] for help; \\[advertised-undo] to undo changes. (`C-' means use CTRL key.)
318 To kill the Emacs job, type \\[save-buffers-kill-emacs].
319 Type \\[help-with-tutorial] for a tutorial on using Emacs.
320 Type \\[info] to enter Info, which you can use to read GNU documentation.
322 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for full details.
323 You may give out copies of Emacs; type \\[describe-copying] to see the conditions.
324 Type \\[describe-distribution] for information on getting the latest version.")))
325 (set-buffer-modified-p nil)
326 (sit-for 120))
327 (save-excursion
328 ;; In case the Emacs server has already selected
329 ;; another buffer, erase the one our message is in.
330 (set-buffer (get-buffer "*scratch*"))
331 (erase-buffer)
332 (set-buffer-modified-p nil)))))
333 (let ((dir default-directory)
334 (file-count 0)
335 first-file-buffer
336 (line 0))
337 (while command-line-args-left
338 (let ((argi (car command-line-args-left))
339 tem)
340 (setq command-line-args-left (cdr command-line-args-left))
341 (cond ((setq tem (assoc argi command-switch-alist))
342 (funcall (cdr tem) argi))
343 ((or (string-equal argi "-f") ;what the manual claims
344 (string-equal argi "-funcall")
345 (string-equal argi "-e")) ; what the source used to say
346 (setq tem (intern (car command-line-args-left)))
347 (setq command-line-args-left (cdr command-line-args-left))
348 (funcall tem))
349 ((or (string-equal argi "-l")
350 (string-equal argi "-load"))
351 (let ((file (car command-line-args-left)))
352 ;; Take file from default dir if it exists there;
353 ;; otherwise let `load' search for it.
354 (if (file-exists-p (expand-file-name file))
355 (setq file (expand-file-name file)))
356 (load file nil t))
357 (setq command-line-args-left (cdr command-line-args-left)))
358 ((or (string-equal argi "-i")
359 (string-equal argi "-insert"))
360 (or (stringp (car command-line-args-left))
361 (error "filename omitted from `-i' option"))
362 (insert-file-contents (car command-line-args-left))
363 (setq command-line-args-left (cdr command-line-args-left)))
364 ((string-equal argi "-kill")
365 (kill-emacs t))
366 ((string-match "^\\+[0-9]+\\'" argi)
367 (setq line (string-to-int argi)))
369 ;; We have almost exhausted our options. See if the
370 ;; user has made any other command-line options available
371 (let ((hooks command-line-functions);; lrs 7/31/89
372 (did-hook nil))
373 (while (and hooks
374 (not (setq did-hook (funcall (car hooks)))))
375 (setq hooks (cdr hooks)))
376 (if (not did-hook)
377 ;; Ok, presume that the argument is a file name
378 (progn
379 (setq file-count (1+ file-count))
380 (cond ((= file-count 1)
381 (setq first-file-buffer
382 (find-file (expand-file-name argi dir))))
384 (find-file-other-window (expand-file-name argi dir))))
385 (or (zerop line)
386 (goto-line line))
387 (setq line 0))))))))
388 ;; If 3 or more files visited, and not all visible,
389 ;; show user what they all are.
390 (if (> file-count 2)
391 (or (get-buffer-window first-file-buffer)
392 (progn (other-window)
393 (buffer-menu)))))))
395 ;;; startup.el ends here