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