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