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