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