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