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