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