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