(texinfo-section-types-regexp): Define here.
[bpt/emacs.git] / lisp / startup.el
1 ;;; startup.el --- process Emacs shell arguments
2
3 ;; Copyright (C) 1985, 1986, 1992 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
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
55 ;;; Code:
56
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.
63 This is for use in your personal init file, once you are familiar
64 with 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.
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'.")
74
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.")
82
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.")
87
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'.")
92
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.")
98
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.")
103
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.
108
109 Users 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.
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.
119
120 In all cases, `(concat \"~\" init-file-user \"/\")' evaluates to the
121 directory 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 (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 (if (fboundp 'frame-notice-user-settings)
151 (frame-notice-user-settings))
152 (and window-setup-hook
153 (run-hooks 'window-setup-hook)))))
154
155 (defun command-line ()
156 ;; See if we should import version-control from the environment variable.
157 (let ((vc (getenv "VERSION_CONTROL")))
158 (cond ((eq vc nil)) ;don't do anything if not set
159 ((or (string= vc "t")
160 (string= vc "numbered"))
161 (setq version-control t))
162 ((or (string= vc "nil")
163 (string= vc "existing"))
164 (setq version-control nil))
165 ((or (string= vc "never")
166 (string= vc "simple"))
167 (setq version-control 'never))))
168
169 ;;! This has been commented out; I currently find the behavior when
170 ;;! split-window-keep-point is nil disturbing, but if I can get used
171 ;;! to it, then it would be better to eliminate the option.
172 ;;! ;; Choose a good default value for split-window-keep-point.
173 ;;! (setq split-window-keep-point (> baud-rate 2400))
174
175 ;; Read window system's init file if using a window system.
176 (if (and window-system (not noninteractive))
177 (load (concat term-file-prefix
178 (symbol-name window-system)
179 "-win")
180 ;; Every window system should have a startup file;
181 ;; barf if we can't find it.
182 nil t))
183
184 (let ((done nil)
185 (args (cdr command-line-args)))
186
187 ;; Figure out which user's init file to load,
188 ;; either from the environment or from the options.
189 (setq init-file-user (if noninteractive nil (user-login-name)))
190 ;; If user has not done su, use current $HOME to find .emacs.
191 (and init-file-user (string= init-file-user (user-real-login-name))
192 (setq init-file-user ""))
193
194 ;; Process the command-line args, and delete the arguments
195 ;; processed. This is consistent with the way main in emacs.c
196 ;; does things.
197 (while (and (not done) args)
198 (let ((argi (car args)))
199 (cond
200 ((or (string-equal argi "-q")
201 (string-equal argi "-no-init-file"))
202 (setq init-file-user nil
203 args (cdr args)))
204 ((or (string-equal argi "-u")
205 (string-equal argi "-user"))
206 (setq args (cdr args)
207 init-file-user (car args)
208 args (cdr args)))
209 ((string-equal argi "-debug-init")
210 (setq init-file-debug t
211 args (cdr args)))
212 (t (setq done t)))))
213
214 ;; Re-attach the program name to the front of the arg list.
215 (setcdr command-line-args args))
216
217 ;; Under X Windows, this creates the X frame and deletes the terminal frame.
218 (if (fboundp 'frame-initialize)
219 (frame-initialize))
220 (if (fboundp 'face-initialize)
221 (face-initialize))
222
223 (run-hooks 'before-init-hook)
224
225 ;; Run the site-start library if it exists. The point of this file is
226 ;; that it is run before .emacs. There is no point in doing this after
227 ;; .emacs; that is useless.
228 (load "site-start" t t)
229
230 ;; Load that user's init file, or the default one, or none.
231 (let ((debug-on-error init-file-debug)
232 ;; This function actually reads the init files.
233 (inner
234 (function
235 (lambda ()
236 (if init-file-user
237 (progn (load (if (eq system-type 'vax-vms)
238 "sys$login:.emacs"
239 (concat "~" init-file-user "/.emacs"))
240 t t t)
241 (or inhibit-default-init
242 (let ((inhibit-startup-message nil))
243 ;; Users are supposed to be told their rights.
244 ;; (Plus how to get help and how to undo.)
245 ;; Don't you dare turn this off for anyone
246 ;; except yourself.
247 (load "default" t t)))))))))
248 (if init-file-debug
249 ;; Do this without a condition-case if the user wants to debug.
250 (funcall inner)
251 (condition-case error
252 (funcall inner)
253 (error (message "Error in init file: %s%s%s"
254 (get (car error) 'error-message)
255 (if (cdr error) ": ")
256 (mapconcat 'prin1-to-string (cdr error) ", "))))))
257
258 (run-hooks 'after-init-hook)
259
260 ;; If *scratch* exists and init file didn't change its mode, initialize it.
261 (if (get-buffer "*scratch*")
262 (save-excursion
263 (set-buffer "*scratch*")
264 (if (eq major-mode 'fundamental-mode)
265 (funcall initial-major-mode))))
266 ;; Load library for our terminal type.
267 ;; User init file can set term-file-prefix to nil to prevent this.
268 (and term-file-prefix (not noninteractive) (not window-system)
269 (let ((term (getenv "TERM"))
270 hyphend)
271 (while (and term
272 (not (load (concat term-file-prefix term) t t)))
273 ;; Strip off last hyphen and what follows, then try again
274 (if (setq hyphend (string-match "[-_][^-_]+$" term))
275 (setq term (substring term 0 hyphend))
276 (setq term nil)))))
277
278 ;; Process the remaining args.
279 (command-line-1 (cdr command-line-args))
280
281 ;; If -batch, terminate after processing the command options.
282 (if noninteractive (kill-emacs t)))
283
284 (defun command-line-1 (command-line-args-left)
285 (if (null command-line-args-left)
286 (cond ((and (not inhibit-startup-message) (not noninteractive)
287 ;; Don't clobber a non-scratch buffer if init file
288 ;; has selected it.
289 (string= (buffer-name) "*scratch*")
290 (not (input-pending-p)))
291 ;; If there are no switches to process, we might as well
292 ;; run this hook now, and there may be some need to do it
293 ;; before doing any output.
294 (and term-setup-hook
295 (run-hooks 'term-setup-hook))
296 ;; Don't let the hook be run twice.
297 (setq term-setup-hook nil)
298
299 ;; It's important to notice the user settings before we
300 ;; display the startup message; otherwise, the settings
301 ;; won't take effect until the user gives the first
302 ;; keystroke, and that's distracting.
303 (if (fboundp 'frame-notice-user-settings)
304 (frame-notice-user-settings))
305
306 (and window-setup-hook
307 (run-hooks 'window-setup-hook))
308 (setq window-setup-hook nil)
309 (unwind-protect
310 (progn
311 (insert (emacs-version)
312 "
313 Copyright (C) 1993 Free Software Foundation, Inc.\n\n")
314 ;; If keys have their default meanings,
315 ;; use precomputed string to save lots of time.
316 (if (and (eq (key-binding "\C-h") 'help-command)
317 (eq (key-binding "\C-xu") 'advertised-undo)
318 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-emacs)
319 (eq (key-binding "\C-h\C-c") 'describe-copying)
320 (eq (key-binding "\C-h\C-d") 'describe-distribution)
321 (eq (key-binding "\C-h\C-w") 'describe-no-warranty)
322 (eq (key-binding "\C-ht") 'help-with-tutorial))
323 (insert
324 "Type C-h for help; C-x u to undo changes. (`C-' means use CTRL key.)
325 To kill the Emacs job, type C-x C-c.
326 Type C-h t for a tutorial on using Emacs.
327 Type C-h i to enter Info, which you can use to read GNU documentation.
328
329 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
330 You may give out copies of Emacs; type C-h C-c to see the conditions.
331 Type C-h C-d for information on getting the latest version.")
332 (insert (substitute-command-keys
333 "Type \\[help-command] for help; \\[advertised-undo] to undo changes. (`C-' means use CTRL key.)
334 To kill the Emacs job, type \\[save-buffers-kill-emacs].
335 Type \\[help-with-tutorial] for a tutorial on using Emacs.
336 Type \\[info] to enter Info, which you can use to read GNU documentation.
337
338 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for full details.
339 You may give out copies of Emacs; type \\[describe-copying] to see the conditions.
340 Type \\[describe-distribution] for information on getting the latest version.")))
341 (set-buffer-modified-p nil)
342 (sit-for 120))
343 (save-excursion
344 ;; In case the Emacs server has already selected
345 ;; another buffer, erase the one our message is in.
346 (set-buffer (get-buffer "*scratch*"))
347 (erase-buffer)
348 (set-buffer-modified-p nil)))))
349 (let ((dir default-directory)
350 (file-count 0)
351 first-file-buffer
352 (line 0))
353 (while command-line-args-left
354 (let ((argi (car command-line-args-left))
355 tem)
356 (setq command-line-args-left (cdr command-line-args-left))
357 (cond ((setq tem (assoc argi command-switch-alist))
358 (funcall (cdr tem) argi))
359 ((or (string-equal argi "-f") ;what the manual claims
360 (string-equal argi "-funcall")
361 (string-equal argi "-e")) ; what the source used to say
362 (setq tem (intern (car command-line-args-left)))
363 (setq command-line-args-left (cdr command-line-args-left))
364 (funcall tem))
365 ((or (string-equal argi "-l")
366 (string-equal argi "-load"))
367 (let ((file (car command-line-args-left)))
368 ;; Take file from default dir if it exists there;
369 ;; otherwise let `load' search for it.
370 (if (file-exists-p (expand-file-name file))
371 (setq file (expand-file-name file)))
372 (load file nil t))
373 (setq command-line-args-left (cdr command-line-args-left)))
374 ((or (string-equal argi "-i")
375 (string-equal argi "-insert"))
376 (or (stringp (car command-line-args-left))
377 (error "filename omitted from `-i' option"))
378 (insert-file-contents (car command-line-args-left))
379 (setq command-line-args-left (cdr command-line-args-left)))
380 ((string-equal argi "-kill")
381 (kill-emacs t))
382 ((string-match "^\\+[0-9]+\\'" argi)
383 (setq line (string-to-int argi)))
384 (t
385 ;; We have almost exhausted our options. See if the
386 ;; user has made any other command-line options available
387 (let ((hooks command-line-functions);; lrs 7/31/89
388 (did-hook nil))
389 (while (and hooks
390 (not (setq did-hook (funcall (car hooks)))))
391 (setq hooks (cdr hooks)))
392 (if (not did-hook)
393 ;; Ok, presume that the argument is a file name
394 (progn
395 (setq file-count (1+ file-count))
396 (cond ((= file-count 1)
397 (setq first-file-buffer
398 (find-file (expand-file-name argi dir))))
399 (t
400 (find-file-other-window (expand-file-name argi dir))))
401 (or (zerop line)
402 (goto-line line))
403 (setq line 0))))))))
404 ;; If 3 or more files visited, and not all visible,
405 ;; show user what they all are.
406 (if (> file-count 2)
407 (or (get-buffer-window first-file-buffer)
408 (progn (other-window)
409 (buffer-menu)))))))
410
411 ;;; startup.el ends here