*** empty log message ***
[bpt/emacs.git] / lisp / startup.el
1 ;;; startup.el --- process Emacs shell arguments
2
3 ;; Maintainer: FSF
4 ;; Last-Modified: 9 Jul 1992
5
6 ;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
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 ;; In presence of symlinks, switch to cleaner form of default directory.
130 (if (not (eq system-type 'vax-vms))
131 (mapcar (function
132 (lambda (var)
133 (let ((value (getenv var)))
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")))
141 (setq default-directory (abbreviate-file-name default-directory))
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 ()
151 ;; See if we should import version-control from the envionment variable.
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
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))
169
170 ;; Read window system's init file if using a window system.
171 (if (and window-system (not noninteractive))
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))
178
179 (let ((done nil)
180 (args (cdr command-line-args)))
181
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 ""))
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.
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)))
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))
211
212 (run-hooks 'before-init-hook)
213
214 ;; Load that user's init file, or the default one, or none.
215 (let ((debug-on-error init-file-debug)
216 ;; This function actually reads the init files.
217 (inner
218 (function
219 (lambda ()
220 (if init-file-user
221 (progn (load (if (eq system-type 'vax-vms)
222 "sys$login:.emacs"
223 (concat "~" init-file-user "/.emacs"))
224 t t t)
225 (or inhibit-default-init
226 (let ((inhibit-startup-message nil))
227 ;; Users are supposed to be told their rights.
228 ;; (Plus how to get help and how to undo.)
229 ;; Don't you dare turn this off for anyone
230 ;; except yourself.
231 (load "default" t t)))))))))
232 (if init-file-debug
233 ;; Do this without a condition-case if the user wants to debug.
234 (funcall inner)
235 (condition-case error
236 (funcall inner)
237 (error (message "Error in init file: %s%s%s"
238 (get (car error) 'error-message)
239 (if (cdr error) ": ")
240 (mapconcat 'prin1-to-string (cdr error) ", "))))))
241
242 (run-hooks 'after-init-hook)
243
244 ;; If *scratch* exists and init file didn't change its mode, initialize it.
245 (if (get-buffer "*scratch*")
246 (save-excursion
247 (set-buffer "*scratch*")
248 (if (eq major-mode 'fundamental-mode)
249 (funcall initial-major-mode))))
250 ;; Load library for our terminal type.
251 ;; User init file can set term-file-prefix to nil to prevent this.
252 (and term-file-prefix (not noninteractive) (not window-system)
253 (let ((term (getenv "TERM"))
254 hyphend)
255 (while (and term
256 (not (load (concat term-file-prefix term) t t)))
257 ;; Strip off last hyphen and what follows, then try again
258 (if (setq hyphend (string-match "[-_][^-_]+$" term))
259 (setq term (substring term 0 hyphend))
260 (setq term nil)))))
261
262 ;; Process the remaining args.
263 (command-line-1 (cdr command-line-args))
264
265 ;; If -batch, terminate after processing the command options.
266 (if noninteractive (kill-emacs t)))
267
268 (defun command-line-1 (command-line-args-left)
269 (if (null command-line-args-left)
270 (cond ((and (not inhibit-startup-message) (not noninteractive)
271 ;; Don't clobber a non-scratch buffer if init file
272 ;; has selected it.
273 (string= (buffer-name) "*scratch*")
274 (not (input-pending-p)))
275 ;; If there are no switches to process, we might as well
276 ;; run this hook now, and there may be some need to do it
277 ;; before doing any output.
278 (and term-setup-hook
279 (run-hooks 'term-setup-hook))
280 ;; Don't let the hook be run twice.
281 (setq term-setup-hook nil)
282 (and window-setup-hook
283 (run-hooks 'window-setup-hook))
284 (setq window-setup-hook nil)
285 (unwind-protect
286 (progn
287 (insert (emacs-version)
288 "
289 Copyright (C) 1991 Free Software Foundation, Inc.\n\n")
290 ;; If keys have their default meanings,
291 ;; use precomputed string to save lots of time.
292 (if (and (eq (key-binding "\C-h") 'help-command)
293 (eq (key-binding "\C-xu") 'advertised-undo)
294 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-emacs)
295 (eq (key-binding "\C-h\C-c") 'describe-copying)
296 (eq (key-binding "\C-h\C-d") 'describe-distribution)
297 (eq (key-binding "\C-h\C-w") 'describe-no-warranty)
298 (eq (key-binding "\C-ht") 'help-with-tutorial))
299 (insert
300 "Type C-h for help; C-x u to undo changes. (`C-' means use CTRL key.)
301 To kill the Emacs job, type C-x C-c.
302 Type C-h t for a tutorial on using Emacs.
303
304 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
305 You may give out copies of Emacs; type C-h C-c to see the conditions.
306 Type C-h C-d for information on getting the latest version.")
307 (insert (substitute-command-keys
308 "Type \\[help-command] for help; \\[advertised-undo] to undo changes. (`C-' means use CTRL key.)
309 To kill the Emacs job, type \\[save-buffers-kill-emacs].
310 Type \\[help-with-tutorial] for a tutorial on using Emacs.
311
312 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for full details.
313 You may give out copies of Emacs; type \\[describe-copying] to see the conditions.
314 Type \\[describe-distribution] for information on getting the latest version.")))
315 (set-buffer-modified-p nil)
316 (sit-for 120))
317 (save-excursion
318 ;; In case the Emacs server has already selected
319 ;; another buffer, erase the one our message is in.
320 (set-buffer (get-buffer "*scratch*"))
321 (erase-buffer)
322 (set-buffer-modified-p nil)))))
323 (let ((dir default-directory)
324 (file-count 0)
325 first-file-buffer
326 (line 0))
327 (while command-line-args-left
328 (let ((argi (car command-line-args-left))
329 tem)
330 (setq command-line-args-left (cdr command-line-args-left))
331 (cond ((setq tem (assoc argi command-switch-alist))
332 (funcall (cdr tem) argi))
333 ((or (string-equal argi "-f") ;what the manual claims
334 (string-equal argi "-funcall")
335 (string-equal argi "-e")) ; what the source used to say
336 (setq tem (intern (car command-line-args-left)))
337 (setq command-line-args-left (cdr command-line-args-left))
338 (funcall tem))
339 ((or (string-equal argi "-l")
340 (string-equal argi "-load"))
341 (let ((file (car command-line-args-left)))
342 ;; Take file from default dir if it exists there;
343 ;; otherwise let `load' search for it.
344 (if (file-exists-p (expand-file-name file))
345 (setq file (expand-file-name file)))
346 (load file nil t))
347 (setq command-line-args-left (cdr command-line-args-left)))
348 ((or (string-equal argi "-i")
349 (string-equal argi "-insert"))
350 (or (stringp (car command-line-args-left))
351 (error "filename omitted from `-i' option"))
352 (insert-file-contents (car command-line-args-left))
353 (setq command-line-args-left (cdr command-line-args-left)))
354 ((string-equal argi "-kill")
355 (kill-emacs t))
356 ((string-match "^\\+[0-9]+\\'" argi)
357 (setq line (string-to-int argi)))
358 (t
359 ;; We have almost exhausted our options. See if the
360 ;; user has made any other command-line options available
361 (let ((hooks command-line-functions);; lrs 7/31/89
362 (did-hook nil))
363 (while (and hooks
364 (not (setq did-hook (funcall (car hooks)))))
365 (setq hooks (cdr hooks)))
366 (if (not did-hook)
367 ;; Ok, presume that the argument is a file name
368 (progn
369 (setq file-count (1+ file-count))
370 (cond ((= file-count 1)
371 (setq first-file-buffer
372 (find-file (expand-file-name argi dir))))
373 (t
374 (find-file-other-window (expand-file-name argi dir))))
375 (or (zerop line)
376 (goto-line line))
377 (setq line 0))))))))
378 ;; If 3 or more files visited, and not all visible,
379 ;; show user what they all are.
380 (if (> file-count 2)
381 (or (get-buffer-window first-file-buffer)
382 (progn (other-window)
383 (buffer-menu)))))))
384
385 ;;; startup.el ends here