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