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