Merged in changes from CVS HEAD
[bpt/emacs.git] / lisp / startup.el
CommitLineData
c88ab9ce
ER
1;;; startup.el --- process Emacs shell arguments
2
84691156 3;; Copyright (C) 1985, 86, 92, 94, 95, 96, 97, 98, 99, 2000, 2001, 2002
347d0813 4;; Free Software Foundation, Inc.
eea8d4ef 5
630cc463 6;; Maintainer: FSF
d7b4d18f 7;; Keywords: internal
630cc463 8
a726e0d1
JB
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
4746118a 13;; the Free Software Foundation; either version 2, or (at your option)
a726e0d1
JB
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
a726e0d1 25
630cc463 26;;; Commentary:
a726e0d1 27
32e51983
EZ
28;; This file parses the command line and gets Emacs running. Options
29;; on the command line are handled in precedence order. For priorities
30;; see the structure standard_args in the emacs.c file.
a726e0d1 31
630cc463
ER
32;;; Code:
33
a726e0d1
JB
34(setq top-level '(normal-top-level))
35
42a3e6fa 36(defvar command-line-processed nil
2879a13b 37 "Non-nil once command line has been processed.")
a726e0d1 38
2246281f
KL
39(defvar window-system initial-window-system
40 "Name of window system the selected frame is displaying through.
41The value is a symbol--for instance, `x' for X windows.
42The value is nil if the selected frame is on a text-only-terminal.")
43
44(make-variable-frame-local 'window-system)
45
42a3e6fa
RS
46(defgroup initialization nil
47 "Emacs start-up procedure"
48 :group 'internal)
49
50(defcustom inhibit-startup-message nil
1d7da582 51 "*Non-nil inhibits the initial startup message.
a726e0d1 52This is for use in your personal init file, once you are familiar
42a3e6fa
RS
53with the contents of the startup message."
54 :type 'boolean
55 :group 'initialization)
a726e0d1 56
1b207153
CW
57(defvaralias 'inhibit-splash-screen 'inhibit-startup-message)
58
42a3e6fa 59(defcustom inhibit-startup-echo-area-message nil
1d7da582 60 "*Non-nil inhibits the initial startup echo area message.
42a3e6fa
RS
61Setting this variable takes effect
62only if you do it with the customization buffer
94487c4e 63or if your `.emacs' file contains a line of this form:
54a003f7 64 (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
e5575c06
RS
65If your `.emacs' file is byte-compiled, use the following form instead:
66 (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
1d7da582 67Thus, someone else using a copy of your `.emacs' file will see
42a3e6fa
RS
68the startup message unless he personally acts to inhibit it."
69 :type '(choice (const :tag "Don't inhibit")
70 (string :tag "Enter your user name, to inhibit"))
71 :group 'initialization)
1d7da582 72
42a3e6fa
RS
73(defcustom inhibit-default-init nil
74 "*Non-nil inhibits loading the `default' library."
75 :type 'boolean
76 :group 'initialization)
a726e0d1 77
802a980a
GM
78(defcustom inhibit-startup-buffer-menu nil
79 "*Non-nil inhibits display of buffer list when more than 2 files are loaded."
80 :type 'boolean
81 :group 'initialization)
82
d7fa5aa2 83(defvar command-switch-alist nil
a726e0d1
JB
84 "Alist of command-line switches.
85Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
5bb54c61
RS
86HANDLER-FUNCTION receives the switch string as its sole argument;
87the remaining command-line args are in the variable `command-line-args-left'.")
a726e0d1 88
860befc8
RS
89(defvar command-line-args-left nil
90 "List of command-line args not yet processed.")
91
a726e0d1
JB
92(defvar command-line-functions nil ;; lrs 7/31/89
93 "List of functions to process unrecognized command-line arguments.
94Each function should access the dynamically bound variables
b4484ea8 95`argi' (the current argument) and `command-line-args-left' (the remaining
a726e0d1 96arguments). The function should return non-nil only if it recognizes and
b4484ea8
RS
97processes `argi'. If it does so, it may consume successive arguments by
98altering `command-line-args-left' to remove them.")
a726e0d1 99
09a1077c
RS
100(defvar command-line-default-directory nil
101 "Default directory to use for command line arguments.
102This is normally copied from `default-directory' when Emacs starts.")
103
b3afdeb8
RS
104;;; This is here, rather than in x-win.el, so that we can ignore these
105;;; options when we are not using X.
46cfd295 106(defconst command-line-x-option-alist
b3afdeb8
RS
107 '(("-bw" 1 x-handle-numeric-switch border-width)
108 ("-d" 1 x-handle-display)
109 ("-display" 1 x-handle-display)
5676ab57 110 ("-name" 1 x-handle-name-switch)
2c3fef40
RS
111 ("-title" 1 x-handle-switch title)
112 ("-T" 1 x-handle-switch title)
b3afdeb8
RS
113 ("-r" 0 x-handle-switch reverse t)
114 ("-rv" 0 x-handle-switch reverse t)
115 ("-reverse" 0 x-handle-switch reverse t)
116 ("-reverse-video" 0 x-handle-switch reverse t)
117 ("-fn" 1 x-handle-switch font)
118 ("-font" 1 x-handle-switch font)
188b6a5e
EZ
119 ("-fs" 0 x-handle-initial-switch fullscreen fullboth)
120 ("-fw" 0 x-handle-initial-switch fullscreen fullwidth)
121 ("-fh" 0 x-handle-initial-switch fullscreen fullheight)
b3afdeb8 122 ("-ib" 1 x-handle-numeric-switch internal-border-width)
1f7f78f1 123 ("-g" 1 x-handle-geometry)
9ff6bda1 124 ("-lsp" 1 x-handle-numeric-switch line-spacing)
1f7f78f1 125 ("-geometry" 1 x-handle-geometry)
b3afdeb8
RS
126 ("-fg" 1 x-handle-switch foreground-color)
127 ("-foreground" 1 x-handle-switch foreground-color)
128 ("-bg" 1 x-handle-switch background-color)
129 ("-background" 1 x-handle-switch background-color)
130 ("-ms" 1 x-handle-switch mouse-color)
131 ("-itype" 0 x-handle-switch icon-type t)
132 ("-i" 0 x-handle-switch icon-type t)
133 ("-iconic" 0 x-handle-iconic)
134 ("-xrm" 1 x-handle-xrm-switch)
135 ("-cr" 1 x-handle-switch cursor-color)
136 ("-vb" 0 x-handle-switch vertical-scroll-bars t)
137 ("-hb" 0 x-handle-switch horizontal-scroll-bars t)
138 ("-bd" 1 x-handle-switch)
139 ("--border-width" 1 x-handle-numeric-switch border-width)
140 ("--display" 1 x-handle-display)
5676ab57 141 ("--name" 1 x-handle-name-switch)
7775acb8 142 ("--title" 1 x-handle-switch title)
b3afdeb8
RS
143 ("--reverse-video" 0 x-handle-switch reverse t)
144 ("--font" 1 x-handle-switch font)
188b6a5e
EZ
145 ("--fullscreen" 0 x-handle-initial-switch fullscreen fullboth)
146 ("--fullwidth" 0 x-handle-initial-switch fullscreen fullwidth)
147 ("--fullheight" 0 x-handle-initial-switch fullscreen fullheight)
b3afdeb8 148 ("--internal-border" 1 x-handle-numeric-switch internal-border-width)
1f7f78f1 149 ("--geometry" 1 x-handle-geometry)
b3afdeb8
RS
150 ("--foreground-color" 1 x-handle-switch foreground-color)
151 ("--background-color" 1 x-handle-switch background-color)
152 ("--mouse-color" 1 x-handle-switch mouse-color)
153 ("--icon-type" 0 x-handle-switch icon-type t)
154 ("--iconic" 0 x-handle-iconic)
155 ("--xrm" 1 x-handle-xrm-switch)
156 ("--cursor-color" 1 x-handle-switch cursor-color)
157 ("--vertical-scroll-bars" 0 x-handle-switch vertical-scroll-bars t)
9ff6bda1 158 ("--line-spacing" 1 x-handle-numeric-switch line-spacing)
7f944e8e 159 ("--border-color" 1 x-handle-switch border-color)
ca57f55d 160 ("--smid" 1 x-handle-smid))
b3afdeb8
RS
161 "Alist of X Windows options.
162Each element has the form
163 (NAME NUMARGS HANDLER FRAME-PARAM VALUE)
164where NAME is the option name string, NUMARGS is the number of arguments
165that the option accepts, HANDLER is a function to call to handle the option.
166FRAME-PARAM (optional) is the frame parameter this option specifies,
167and VALUE is the value which is given to that frame parameter
168\(most options use the argument for this, so VALUE is not present).")
169
e3bd99f5 170(defvar before-init-hook nil
db3f571a 171 "Normal hook run after handling urgent options but before loading init files.")
3fc958a4 172
e3bd99f5 173(defvar after-init-hook nil
db3f571a
KH
174 "Normal hook run after loading the init files, `~/.emacs' and `default.el'.
175There is no `condition-case' around the running of these functions;
176therefore, if you set `debug-on-error' non-nil in `.emacs',
177an error in one of these functions will invoke the debugger.")
178
179(defvar emacs-startup-hook nil
180 "Normal hook run after loading init files and handling the command line.")
e3bd99f5 181
a726e0d1 182(defvar term-setup-hook nil
db3f571a
KH
183 "Normal hook run after loading terminal-specific Lisp code.
184It also follows `emacs-startup-hook'. This hook exists for users to set,
a726e0d1
JB
185so as to override the definitions made by the terminal-specific file.
186Emacs never sets this variable itself.")
187
deebb0e9
RS
188(defvar inhibit-startup-hooks nil
189 "Non-nil means don't run `term-setup-hook' and `emacs-startup-hook'.
190This is because we already did so.")
191
a726e0d1 192(defvar keyboard-type nil
b4484ea8
RS
193 "The brand of keyboard you are using.
194This variable is used to define
a726e0d1 195the proper function and keypad keys for use under X. It is used in a
94487c4e 196fashion analogous to the environment variable TERM.")
a726e0d1
JB
197
198(defvar window-setup-hook nil
b4484ea8
RS
199 "Normal hook run to initialize window system display.
200Emacs runs this hook after processing the command line arguments and loading
201the user's init file.")
a726e0d1 202
42a3e6fa
RS
203(defcustom initial-major-mode 'lisp-interaction-mode
204 "Major mode command symbol to use for the initial *scratch* buffer."
ce2f921e 205 :type 'function
42a3e6fa 206 :group 'initialization)
a726e0d1 207
42a3e6fa 208(defcustom init-file-user nil
a726e0d1 209 "Identity of user whose `.emacs' file is or was read.
d7fa5aa2
RS
210The value is nil if `-q' or `--no-init-file' was specified,
211meaning do not load any init file.
212
213Otherwise, the value may be the null string, meaning use the init file
214for the user that originally logged in, or it may be a
215string containing a user's name meaning use that person's init file.
a726e0d1 216
2bdfaa42
KH
217In either of the latter cases, `(concat \"~\" init-file-user \"/\")'
218evaluates to the name of the directory where the `.emacs' file was
13fce4e6
RS
219looked for.
220
221Setting `init-file-user' does not prevent Emacs from loading
42a3e6fa
RS
222`site-start.el'. The only way to do that is to use `--no-site-file'."
223 :type '(choice (const :tag "none" nil) string)
224 :group 'initialization)
a726e0d1 225
42a3e6fa 226(defcustom site-run-file "site-start"
b7444d31
RS
227 "File containing site-wide run-time initializations.
228This file is loaded at run-time before `~/.emacs'. It contains inits
229that need to be in place for the entire site, but which, due to their
230higher incidence of change, don't make sense to load into emacs'
231dumped image. Thus, the run-time load order is: 1. file described in
13fce4e6
RS
232this variable, if non-nil; 2. `~/.emacs'; 3. `default.el'.
233
234Don't use the `site-start.el' file for things some users may not like.
235Put them in `default.el' instead, so that users can more easily
236override them. Users can prevent loading `default.el' with the `-q'
237option or by setting `inhibit-default-init' in their own init files,
238but inhibiting `site-start.el' requires `--no-site-file', which
42a3e6fa 239is less convenient."
93812130 240 :type '(choice (const :tag "none" nil) string)
42a3e6fa 241 :group 'initialization)
b7444d31 242
42a3e6fa
RS
243(defcustom mail-host-address nil
244 "*Name of this machine, for purposes of naming users."
245 :type '(choice (const nil) string)
246 :group 'mail)
c13fbb62 247
680ebfa6
RS
248(defcustom user-mail-address (if command-line-processed
249 (concat (user-login-name) "@"
250 (or mail-host-address
251 (system-name)))
252 ;; Empty string means "not set yet".
253 "")
452e9090
RS
254 "*Full mailing address of this user.
255This is initialized based on `mail-host-address',
42a3e6fa
RS
256after your init file is read, in case it sets `mail-host-address'."
257 :type 'string
258 :group 'mail)
c10d1f06 259
42a3e6fa 260(defcustom auto-save-list-file-prefix
73f13e5a
GM
261 (cond ((eq system-type 'ms-dos)
262 ;; MS-DOS cannot have initial dot, and allows only 8.3 names
4b33deaa 263 "~/_emacs.d/auto-save.list/_s")
73f13e5a
GM
264 (t
265 "~/.emacs.d/auto-save-list/.saves-"))
cdee38c3
KH
266 "Prefix for generating `auto-save-list-file-name'.
267This is used after reading your `.emacs' file to initialize
268`auto-save-list-file-name', by appending Emacs's pid and the system name,
269if you have not already set `auto-save-list-file-name' yourself.
73f13e5a 270Directories in the prefix will be created if necessary.
cdee38c3 271Set this to nil if you want to prevent `auto-save-list-file-name'
42a3e6fa 272from being initialized."
3671f444
KH
273 :type '(choice (const :tag "Don't record a session's auto save list" nil)
274 string)
42a3e6fa 275 :group 'auto-save)
2e05d063 276
a726e0d1
JB
277(defvar init-file-debug nil)
278
52320897
RS
279(defvar init-file-had-error nil)
280
95eada65
RS
281(defvar normal-top-level-add-subdirs-inode-list nil)
282
b693e375
RS
283(defvar pure-space-overflow nil
284 "Non-nil if building Emacs overflowed pure space.")
285
859e15c1 286(defun normal-top-level-add-subdirs-to-load-path ()
0412d833
RS
287 "Add all subdirectories of current directory to `load-path'.
288More precisely, this uses only the subdirectories whose names
f734a13e
DL
289start with letters or digits; it excludes any subdirectory named `RCS'
290or `CVS', and any subdirectory that contains a file named `.nosearch'."
f1180544 291 (let (dirs
95eada65 292 attrs
859e15c1
KH
293 (pending (list default-directory)))
294 ;; This loop does a breadth-first tree walk on DIR's subtree,
295 ;; putting each subdir into DIRS as its contents are examined.
296 (while pending
4e0a3971 297 (push (pop pending) dirs)
722a451d
EZ
298 (let* ((this-dir (car dirs))
299 (contents (directory-files this-dir))
300 (default-directory this-dir)
301 (canonicalized (and (eq system-type 'windows-nt)
302 (untranslated-canonical-name this-dir))))
303 ;; The Windows version doesn't report meaningful inode
304 ;; numbers, so use the canonicalized absolute file name of the
305 ;; directory instead.
306 (setq attrs (or canonicalized
307 (nthcdr 10 (file-attributes this-dir))))
95eada65 308 (unless (member attrs normal-top-level-add-subdirs-inode-list)
4e0a3971
SM
309 (push attrs normal-top-level-add-subdirs-inode-list)
310 (dolist (file contents)
c4a40544 311 ;; The lower-case variants of RCS and CVS are for DOS/Windows.
4e0a3971
SM
312 (unless (member file '("." ".." "RCS" "CVS" "rcs" "cvs"))
313 (when (and (string-match "\\`[[:alnum:]]" file)
9d1fb179
RS
314 ;; Avoid doing a `stat' when it isn't necessary
315 ;; because that can cause trouble when an NFS server
316 ;; is down.
4e0a3971
SM
317 (not (string-match "\\.elc?\\'" file))
318 (file-directory-p file))
319 (let ((expanded (expand-file-name file)))
95eada65
RS
320 (unless (file-exists-p (expand-file-name ".nosearch"
321 expanded))
4e0a3971 322 (setq pending (nconc pending (list expanded)))))))))))
bb15e81a 323 (normal-top-level-add-to-load-path (cdr (nreverse dirs)))))
859e15c1 324
537b6e4e
RS
325;; This function is called from a subdirs.el file.
326;; It assumes that default-directory is the directory
327;; in which the subdirs.el file exists,
328;; and it adds to load-path the subdirs of that directory
329;; as specified in DIRS. Normally the elements of DIRS are relative.
e87a7309 330(defun normal-top-level-add-to-load-path (dirs)
cd1c10f6
RS
331 (let ((tail load-path)
332 (thisdir (directory-file-name default-directory)))
333 (while (and tail
138e541f
GM
334 ;;Don't go all the way to the nil terminator.
335 (cdr tail)
cd1c10f6
RS
336 (not (equal thisdir (car tail)))
337 (not (and (memq system-type '(ms-dos windows-nt))
338 (equal (downcase thisdir) (downcase (car tail))))))
339 (setq tail (cdr tail)))
138e541f
GM
340 ;;Splice the new section in.
341 (when tail
342 (setcdr tail (append (mapcar 'expand-file-name dirs) (cdr tail))))))
e87a7309 343
a726e0d1
JB
344(defun normal-top-level ()
345 (if command-line-processed
346 (message "Back to top level.")
347 (setq command-line-processed t)
8d4c2221
RS
348 ;; Give *Messages* the same default-directory as *scratch*,
349 ;; just to keep things predictable.
350 (let ((dir default-directory))
351 (save-excursion
352 (set-buffer (get-buffer "*Messages*"))
353 (setq default-directory dir)))
9715399e
MR
354 ;; `user-full-name' is now known; reset its standard-value here.
355 (put 'user-full-name 'standard-value
356 (list (default-value 'user-full-name)))
80d71556
KH
357 ;; For root, preserve owner and group when editing files.
358 (if (equal (user-uid) 0)
359 (setq backup-by-copying-when-mismatch t))
e87a7309
RS
360 ;; Look in each dir in load-path for a subdirs.el file.
361 ;; If we find one, load it, which will add the appropriate subdirs
362 ;; of that dir into load-path,
357438eb
KH
363 ;; Look for a leim-list.el file too. Loading it will register
364 ;; available input methods.
e87a7309
RS
365 (let ((tail load-path)
366 new)
367 (while tail
4e0a3971 368 (push (car tail) new)
bbfc7591
GM
369 (condition-case nil
370 (let ((default-directory (car tail)))
371 (load (expand-file-name "subdirs.el" (car tail)) t t t)))
357438eb
KH
372 (condition-case nil
373 (let ((default-directory (car tail)))
374 (load (expand-file-name "leim-list.el" (car tail)) t t t)))
e87a7309 375 (setq tail (cdr tail))))
ffd56f97 376 (if (not (eq system-type 'vax-vms))
a4b33896
JB
377 (progn
378 ;; If the PWD environment variable isn't accurate, delete it.
379 (let ((pwd (getenv "PWD")))
380 (and (stringp pwd)
381 ;; Use FOO/., so that if FOO is a symlink, file-attributes
382 ;; describes the directory linked to, not FOO itself.
383 (or (equal (file-attributes
384 (concat (file-name-as-directory pwd) "."))
385 (file-attributes
386 (concat (file-name-as-directory default-directory)
387 ".")))
388 (setq process-environment
389 (delete (concat "PWD=" pwd)
390 process-environment)))))))
492878e4 391 (setq default-directory (abbreviate-file-name default-directory))
6f2c86fa
KH
392 (let ((menubar-bindings-done nil))
393 (unwind-protect
394 (command-line)
395 ;; Do this again, in case .emacs defined more abbreviations.
396 (setq default-directory (abbreviate-file-name default-directory))
b3c7c12c
RS
397 ;; Specify the file for recording all the auto save files of this session.
398 ;; This is used by recover-session.
cdee38c3
KH
399 (or auto-save-list-file-name
400 (and auto-save-list-file-prefix
401 (setq auto-save-list-file-name
1f7f78f1
RS
402 ;; Under MS-DOS our PID is almost always reused between
403 ;; Emacs invocations. We need something more unique.
4b33deaa
EZ
404 (cond ((eq system-type 'ms-dos)
405 ;; We are going to access the auto-save
406 ;; directory, so make sure it exists.
407 (make-directory
408 (file-name-directory auto-save-list-file-prefix)
409 t)
f1180544 410 (concat
4b33deaa
EZ
411 (make-temp-name
412 (expand-file-name
413 auto-save-list-file-prefix))
414 "~"))
415 (t
416 (expand-file-name
417 (format "%s%d-%s~"
418 auto-save-list-file-prefix
419 (emacs-pid)
420 (system-name))))))))
deebb0e9
RS
421 (unless inhibit-startup-hooks
422 (run-hooks 'emacs-startup-hook)
423 (and term-setup-hook
424 (run-hooks 'term-setup-hook)))
6a1e7d67
GM
425
426 ;; Don't do this if we failed to create the initial frame,
427 ;; for instance due to a dense colormap.
538ef02a
GM
428 (when (or frame-initial-frame
429 ;; If frame-initial-frame has no meaning, do this anyway.
2246281f 430 (not (and initial-window-system
538ef02a 431 (not noninteractive)
2246281f 432 (not (eq initial-window-system 'pc)))))
6a1e7d67
GM
433 ;; Modify the initial frame based on what .emacs puts into
434 ;; ...-frame-alist.
435 (if (fboundp 'frame-notice-user-settings)
436 (frame-notice-user-settings))
437 (if (fboundp 'frame-set-background-mode)
438 ;; Set the faces for the initial background mode even if
439 ;; frame-notice-user-settings didn't (such as on a tty).
440 ;; frame-set-background-mode is idempotent, so it won't
441 ;; cause any harm if it's already been done.
442 (let ((frame-background-mode frame-background-mode)
443 (frame (selected-frame))
444 term)
2246281f 445 (when (and (null initial-window-system)
6a1e7d67
GM
446 ;; Don't override a possibly customized value.
447 (null frame-background-mode)
448 ;; Don't override user specifications.
449 (null (frame-parameter frame 'reverse))
450 (let ((bg (frame-parameter frame 'background-color)))
451 (or (null bg)
452 (member bg '(unspecified "unspecified-bg")))))
453 (setq term (getenv "TERM"))
818cd248
EZ
454 ;; Some files in lisp/term do a better job with the
455 ;; background mode, but we leave this here anyway, in
456 ;; case they remove those files.
6a1e7d67
GM
457 (if (string-match "^\\(xterm\\|rxvt\\|dtterm\\|eterm\\)"
458 term)
459 (setq frame-background-mode 'light)))
460 (frame-set-background-mode (selected-frame)))))
13ab33c4 461
6f2c86fa
KH
462 ;; Now we know the user's default font, so add it to the menu.
463 (if (fboundp 'font-menu-add-default)
464 (font-menu-add-default))
465 (and window-setup-hook
466 (run-hooks 'window-setup-hook))
467 (or menubar-bindings-done
66c24e1e
EZ
468 (if (display-popup-menus-p)
469 (precompute-menubar-bindings)))))))
6f2c86fa
KH
470
471;; Precompute the keyboard equivalents in the menu bar items.
472(defun precompute-menubar-bindings ()
365636dc
RS
473 (let ((submap (lookup-key global-map [menu-bar])))
474 (while submap
475 (and (consp (car submap))
476 (symbolp (car (car submap)))
477 (stringp (car-safe (cdr (car submap))))
478 (keymapp (cdr (cdr (car submap))))
a18042d7
RS
479 (progn
480 (x-popup-menu nil (cdr (cdr (car submap))))
481 (if purify-flag
482 (garbage-collect))))
365636dc 483 (setq submap (cdr submap))))
5b61c6a7 484 (setq define-key-rebound-commands t))
a726e0d1 485
c381f482
EZ
486;; Command-line options supported by tty's:
487(defconst tty-long-option-alist
8957d2bf
EZ
488 '(("--name" . "-name")
489 ("--title" . "-T")
490 ("--reverse-video" . "-reverse")
c381f482 491 ("--foreground-color" . "-fg")
8957d2bf
EZ
492 ("--background-color" . "-bg")
493 ("--color" . "-color")))
c381f482 494
b66b6aeb
GM
495(defconst tool-bar-images-pixel-height 24
496 "Height in pixels of images in the tool bar.")
497
3a55d3d0
JR
498(defvar tool-bar-originally-present nil
499 "Non-nil if tool-bars are present before user and site init files are read.")
500
e9cda827
KL
501(defvar handle-args-function-alist '((nil . tty-handle-args))
502 "Functions for processing window-system dependent command-line arguments.
503Window system startup files should add their own function to this
504alist, which should parse the command line arguments. Those
505pertaining to the window system should be processed and removed
506from the returned command line.")
507
508(defvar window-system-initialization-alist '((nil . ignore))
509 "Alist of window-system initialization functions.
510Window-system startup files should add their own initialization
511function to this list. The function should take no arguments,
512and initialize the window system environment to prepare for
513opening the first frame (e.g. open a connection to the server).")
514
bca8c7be 515;; Handle the X-like command-line arguments "-fg", "-bg", "-name", etc.
c381f482 516(defun tty-handle-args (args)
bca8c7be 517 (let (rest)
c381f482
EZ
518 (message "%s" args)
519 (while (and args
520 (not (equal (car args) "--")))
bca8c7be
MS
521 (let* ((argi (pop args))
522 (orig-argi argi)
523 argval completion)
c381f482
EZ
524 ;; Check for long options with attached arguments
525 ;; and separate out the attached option argument into argval.
bca8c7be
MS
526 (when (string-match "^\\(--[^=]*\\)=" argi)
527 (setq argval (substring argi (match-end 0))
528 argi (match-string 1 argi)))
529 (when (string-match "^--" argi)
530 (setq completion (try-completion argi tty-long-option-alist))
c381f482
EZ
531 (if (eq completion t)
532 ;; Exact match for long option.
bca8c7be 533 (setq argi (cdr (assoc argi tty-long-option-alist)))
c381f482
EZ
534 (if (stringp completion)
535 (let ((elt (assoc completion tty-long-option-alist)))
536 ;; Check for abbreviated long option.
537 (or elt
bca8c7be
MS
538 (error "Option `%s' is ambiguous" argi))
539 (setq argi (cdr elt)))
c381f482 540 ;; Check for a short option.
bca8c7be
MS
541 (setq argval nil
542 argi orig-argi))))
543 (cond ((member argi '("-fg" "-foreground"))
544 (push (cons 'foreground-color (or argval (pop args)))
545 default-frame-alist))
546 ((member argi '("-bg" "-background"))
547 (push (cons 'background-color (or argval (pop args)))
548 default-frame-alist))
549 ((member argi '("-T" "-name"))
550 (unless argval (setq argval (pop args)))
551 (push (cons 'title
552 (if (stringp argval)
553 argval
554 (let ((case-fold-search t)
555 i)
556 (setq argval (invocation-name))
557
558 ;; Change any . or * characters in name to
559 ;; hyphens, so as to emulate behavior on X.
560 (while
561 (setq i (string-match "[.*]" argval))
562 (aset argval i ?-))
563 argval)))
564 default-frame-alist))
565 ((member argi '("-r" "-rv" "-reverse"))
566 (push '(reverse . t)
567 default-frame-alist))
568 ((equal argi "-color")
569 (unless argval (setq argval 8)) ; default --color means 8 ANSI colors
570 (push (cons 'tty-color-mode
571 (cond
572 ((numberp argval) argval)
573 ((string-match "-?[0-9]+" argval)
574 (string-to-number argval))
575 (t (intern argval))))
576 default-frame-alist))
577 (t
578 (push argi rest)))))
579 (nreverse rest)))
c381f482 580
a726e0d1 581(defun command-line ()
09a1077c
RS
582 (setq command-line-default-directory default-directory)
583
849eedba
RS
584 ;; Choose a reasonable location for temporary files.
585 (setq temporary-file-directory
586 (file-name-as-directory
587 (cond ((memq system-type '(ms-dos windows-nt))
588 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
589 ((memq system-type '(vax-vms axp-vms))
590 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
591 (t
592 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp")))))
607f86f5
EZ
593 (setq small-temporary-file-directory
594 (if (eq system-type 'ms-dos)
f5568df0 595 (getenv "TMPDIR")))
d88c92eb
EZ
596 (setq auto-save-file-name-transforms
597 (list (list "\\`/[^/]*:\\(.+/\\)*\\(.*\\)"
598 ;; Don't put "\\2" inside expand-file-name, since
599 ;; it will be transformed to "/2" on DOS/Windows.
5ecad17d 600 (concat temporary-file-directory "\\2") t)))
849eedba 601
74f2ab06 602 ;; See if we should import version-control from the environment variable.
a726e0d1
JB
603 (let ((vc (getenv "VERSION_CONTROL")))
604 (cond ((eq vc nil)) ;don't do anything if not set
bca8c7be 605 ((member vc '("t" "numbered"))
a726e0d1 606 (setq version-control t))
bca8c7be 607 ((member vc '("nil" "existing"))
a726e0d1 608 (setq version-control nil))
bca8c7be 609 ((member vc '("never" "simple"))
a726e0d1
JB
610 (setq version-control 'never))))
611
79058860
JB
612 ;;! This has been commented out; I currently find the behavior when
613 ;;! split-window-keep-point is nil disturbing, but if I can get used
614 ;;! to it, then it would be better to eliminate the option.
615 ;;! ;; Choose a good default value for split-window-keep-point.
616 ;;! (setq split-window-keep-point (> baud-rate 2400))
f35fe3c6 617
d4308a4d
EZ
618 ;; Set the default strings to display in mode line for
619 ;; end-of-line formats that aren't native to this platform.
620 (cond
621 ((memq system-type '(ms-dos windows-nt emx))
bca8c7be
MS
622 (setq eol-mnemonic-unix "(Unix)"
623 eol-mnemonic-mac "(Mac)"))
bde61f8b
EZ
624 ;; Both Mac and Unix EOLs are now "native" on Mac OS so keep the
625 ;; abbreviated strings `/' and `:' set in coding.c for them.
626 ((eq system-type 'macos)
627 (setq eol-mnemonic-dos "(DOS)"))
bca8c7be
MS
628 (t ; this is for Unix/GNU/Linux systems
629 (setq eol-mnemonic-dos "(DOS)"
630 eol-mnemonic-mac "(Mac)")))
d4308a4d 631
a726e0d1 632 ;; Read window system's init file if using a window system.
1ed14cfd 633 (condition-case error
e9cda827
KL
634 (unless noninteractive
635 (if (and initial-window-system
636 (not (featurep
637 (intern (concat (symbol-name initial-window-system)
638 "-win")))))
639 (error "Unsupported window system `%s'" initial-window-system))
640 ;; Process window-system specific command line parameters.
641 (setq command-line-args
642 (funcall (or (cdr (assq initial-window-system handle-args-function-alist))
643 (error "Unsupported window system `%s'" initial-window-system))
644 command-line-args))
645 ;; Initialize the window system. (Open connection, etc.)
646 (funcall (or (cdr (assq initial-window-system window-system-initialization-alist))
647 (error "Unsupported window system `%s'" initial-window-system))))
648 ;; If there was an error, print the error message and exit.
1ed14cfd 649 (error
2677ad61
RS
650 (princ
651 (if (eq (car error) 'error)
652 (apply 'concat (cdr error))
653 (if (memq 'file-error (get (car error) 'error-conditions))
654 (format "%s: %s"
bca8c7be
MS
655 (nth 1 error)
656 (mapconcat (lambda (obj) (prin1-to-string obj t))
657 (cdr (cdr error)) ", "))
2677ad61 658 (format "%s: %s"
bca8c7be
MS
659 (get (car error) 'error-message)
660 (mapconcat (lambda (obj) (prin1-to-string obj t))
661 (cdr error) ", "))))
2677ad61 662 'external-debugging-output)
27f5188c 663 (terpri 'external-debugging-output)
2246281f 664 (setq initial-window-system nil)
1ed14cfd 665 (kill-emacs)))
a726e0d1 666
640a9cdd
JR
667 (set-locale-environment nil)
668
c16eb7b0
RS
669 ;; Convert the arguments to Emacs internal representation.
670 (let ((args (cdr command-line-args)))
671 (while args
672 (setcar args
673 (decode-coding-string (car args) locale-coding-system t))
bca8c7be 674 (pop args)))
c16eb7b0 675
03e3c30a
JB
676 (let ((done nil)
677 (args (cdr command-line-args)))
678
a726e0d1
JB
679 ;; Figure out which user's init file to load,
680 ;; either from the environment or from the options.
681 (setq init-file-user (if noninteractive nil (user-login-name)))
682 ;; If user has not done su, use current $HOME to find .emacs.
bca8c7be
MS
683 (and init-file-user
684 (equal init-file-user (user-real-login-name))
a726e0d1 685 (setq init-file-user ""))
03e3c30a
JB
686
687 ;; Process the command-line args, and delete the arguments
688 ;; processed. This is consistent with the way main in emacs.c
689 ;; does things.
a726e0d1 690 (while (and (not done) args)
bca8c7be
MS
691 (let* ((longopts '(("--no-init-file") ("--no-site-file") ("--user")
692 ("--debug-init") ("--iconic") ("--icon-type")))
693 (argi (pop args))
694 (orig-argi argi)
695 argval)
452e9090 696 ;; Handle --OPTION=VALUE format.
bca8c7be 697 (when (string-match "^\\(--[^=]*\\)=" argi)
1b207153 698 (setq argval (substring argi (match-end 0))
bca8c7be 699 argi (match-string 1 argi)))
1b207153
CW
700 (unless (equal argi "--")
701 (let ((completion (try-completion argi longopts)))
702 (if (eq completion t)
703 (setq argi (substring argi 1))
704 (if (stringp completion)
705 (let ((elt (assoc completion longopts)))
706 (or elt
707 (error "Option `%s' is ambiguous" argi))
708 (setq argi (substring (car elt) 1)))
bca8c7be
MS
709 (setq argval nil
710 argi orig-argi)))))
a726e0d1 711 (cond
4e0a3971
SM
712 ((member argi '("-q" "-no-init-file"))
713 (setq init-file-user nil))
714 ((member argi '("-u" "-user"))
bca8c7be 715 (setq init-file-user (or argval (pop args))
4e0a3971 716 argval nil))
bca8c7be 717 ((equal argi "-no-site-file")
4e0a3971 718 (setq site-run-file nil))
bca8c7be 719 ((equal argi "-debug-init")
4e0a3971 720 (setq init-file-debug t))
bca8c7be 721 ((equal argi "-iconic")
4e0a3971 722 (push '(visibility . icon) initial-frame-alist))
bca8c7be 723 ((member argi '("-icon-type" "-i" "-itype"))
4e0a3971
SM
724 (push '(icon-type . t) default-frame-alist))
725 ;; Push the popped arg back on the list of arguments.
bca8c7be
MS
726 (t
727 (push argi args)
728 (setq done t)))
096b7031
KH
729 ;; Was argval set but not used?
730 (and argval
731 (error "Option `%s' doesn't allow an argument" argi))))
732
03e3c30a 733 ;; Re-attach the program name to the front of the arg list.
bca8c7be
MS
734 (and command-line-args
735 (setcdr command-line-args args)))
a726e0d1 736
c722566c 737 ;; Under X Windows, this creates the X frame and deletes the terminal frame.
14f16b9c
GM
738 (when (fboundp 'frame-initialize)
739 (frame-initialize))
740
1fe0333f 741 ;; If frame was created with a menu bar, set menu-bar-mode on.
bca8c7be 742 (unless (or noninteractive
2246281f 743 (and (memq initial-window-system '(x w32))
bca8c7be 744 (<= (frame-parameter nil 'menu-bar-lines) 0)))
8ab9589d 745 (menu-bar-mode 1))
c722566c 746
7d1eb6a4 747 ;; If frame was created with a tool bar, switch tool-bar-mode on.
bca8c7be
MS
748 (unless (or noninteractive
749 (not (display-graphic-p))
750 (<= (frame-parameter nil 'tool-bar-lines) 0))
b66b6aeb 751 (tool-bar-mode 1))
7d1eb6a4 752
2246281f 753 ;; Can't do this init in defcustom because initial-window-system isn't set.
bca8c7be
MS
754 (unless (or noninteractive
755 (eq system-type 'ms-dos)
2246281f 756 (not (memq initial-window-system '(x w32))))
6753393b
GM
757 (setq-default blink-cursor t)
758 (blink-cursor-mode 1))
759
3ecd8ad0
GM
760 (unless noninteractive
761 ;; DOS/Windows systems have a PC-type keyboard which has both
762 ;; <delete> and <backspace> keys.
763 (when (or (memq system-type '(ms-dos windows-nt))
2246281f 764 (and (memq initial-window-system '(x))
3ecd8ad0 765 (fboundp 'x-backspace-delete-keys-p)
7360b8aa
GM
766 (x-backspace-delete-keys-p))
767 ;; If the terminal Emacs is running on has erase char
768 ;; set to ^H, use the Backspace key for deleting
769 ;; backward and, and the Delete key for deleting forward.
2246281f 770 (and (null initial-window-system)
7360b8aa 771 (eq tty-erase-char 8)))
d8e17bc6
EZ
772 (setq-default normal-erase-is-backspace t)
773 (normal-erase-is-backspace-mode 1)))
14f076a8 774
bca8c7be
MS
775 (unless (or noninteractive
776 (not (display-graphic-p))
777 (not (fboundp 'x-show-tip)))
3b73087e
GM
778 (setq-default tooltip-mode t)
779 (tooltip-mode 1))
780
1592c1ef
EZ
781 ;; Register default TTY colors for the case the terminal hasn't a
782 ;; terminal init file.
d448e982
KL
783 ;; We do this regardles of whether the terminal supports colors
784 ;; or not, since they can switch that support on or off in
785 ;; mid-session by setting the tty-color-mode frame parameter.
786 (tty-register-default-colors)
1592c1ef 787
3a55d3d0
JR
788 ;; Record whether the tool-bar is present before the user and site
789 ;; init files are processed. frame-notice-user-settings uses this
790 ;; to determine if the tool-bar has been disabled by the init files,
791 ;; and the frame needs to be resized.
792 (when (fboundp 'frame-notice-user-settings)
793 (let ((tool-bar-lines (or (assq 'tool-bar-lines initial-frame-alist)
794 (assq 'tool-bar-lines default-frame-alist))))
795 (setq tool-bar-originally-present
bca8c7be
MS
796 (and tool-bar-lines
797 (cdr tool-bar-lines)
798 (not (eq 0 (cdr tool-bar-lines)))))))
3a55d3d0 799
752ef254
GM
800 (let ((old-scalable-fonts-allowed scalable-fonts-allowed)
801 (old-font-list-limit font-list-limit)
802 (old-face-ignored-fonts face-ignored-fonts))
803
804 (run-hooks 'before-init-hook)
805
806 ;; Run the site-start library if it exists. The point of this file is
807 ;; that it is run before .emacs. There is no point in doing this after
808 ;; .emacs; that is useless.
f1180544 809 (if site-run-file
752ef254
GM
810 (load site-run-file t t))
811
812 ;; Sites should not disable this. Only individuals should disable
813 ;; the startup message.
814 (setq inhibit-startup-message nil)
815
816 ;; Load that user's init file, or the default one, or none.
817 (let (debug-on-error-from-init-file
818 debug-on-error-should-be-set
819 (debug-on-error-initial
820 (if (eq init-file-debug t) 'startup init-file-debug))
821 (orig-enable-multibyte default-enable-multibyte-characters))
822 (let ((debug-on-error debug-on-error-initial)
823 ;; This function actually reads the init files.
824 (inner
825 (function
826 (lambda ()
827 (if init-file-user
828 (let ((user-init-file-1
4e0a3971 829 (cond
752ef254
GM
830 ((eq system-type 'ms-dos)
831 (concat "~" init-file-user "/_emacs"))
832 ((eq system-type 'windows-nt)
833 (if (directory-files "~" nil "^\\.emacs\\(\\.elc?\\)?$")
834 "~/.emacs"
835 "~/_emacs"))
f1180544 836 ((eq system-type 'vax-vms)
752ef254 837 "sys$login:.emacs")
4e0a3971 838 (t
752ef254
GM
839 (concat "~" init-file-user "/.emacs")))))
840 ;; This tells `load' to store the file name found
841 ;; into user-init-file.
842 (setq user-init-file t)
843 (load user-init-file-1 t t)
f1180544 844
a04d5983
RS
845 (when (eq user-init-file t)
846 ;; If we did not find ~/.emacs, try
847 ;; ~/.emacs.d/.emacs.
848 (let ((otherfile
849 (expand-file-name
850 (file-name-nondirectory user-init-file-1)
851 (file-name-as-directory
852 (expand-file-name
853 ".emacs.d"
854 (file-name-directory user-init-file-1))))))
855 (load otherfile t t)
856
857 ;; If we did not find the user's init file,
858 ;; set user-init-file conclusively.
859 ;; Don't let it be set from default.el.
860 (when (eq user-init-file t)
861 (setq user-init-file user-init-file-1))))
f1180544 862
752ef254
GM
863 ;; If we loaded a compiled file, set
864 ;; `user-init-file' to the source version if that
865 ;; exists.
866 (when (and user-init-file
867 (equal (file-name-extension user-init-file)
868 "elc"))
869 (let* ((source (file-name-sans-extension user-init-file))
870 (alt (concat source ".el")))
871 (setq source (cond ((file-exists-p alt) alt)
872 ((file-exists-p source) source)
873 (t nil)))
874 (when source
875 (when (file-newer-than-file-p source user-init-file)
876 (message "Warning: %s is newer than %s"
877 source user-init-file)
878 (sit-for 1))
879 (setq user-init-file source))))
f1180544 880
bca8c7be
MS
881 (when (stringp custom-file)
882 (unless (assoc custom-file load-history)
883 ;; If the .emacs file has set `custom-file' but hasn't
884 ;; loaded the file yet, let's load it.
885 (load custom-file t t)))
886
887 (unless inhibit-default-init
888 (let ((inhibit-startup-message nil))
889 ;; Users are supposed to be told their rights.
890 ;; (Plus how to get help and how to undo.)
891 ;; Don't you dare turn this off for anyone
892 ;; except yourself.
893 (load "default" t t)))))))))
752ef254
GM
894 (if init-file-debug
895 ;; Do this without a condition-case if the user wants to debug.
896 (funcall inner)
897 (condition-case error
898 (progn
899 (funcall inner)
900 (setq init-file-had-error nil))
901 (error
902 (let ((message-log-max nil))
903 (save-excursion
904 (set-buffer (get-buffer-create "*Messages*"))
905 (insert "\n\n"
906 (format "An error has occurred while loading `%s':\n\n"
907 user-init-file)
908 (format "%s%s%s"
909 (get (car error) 'error-message)
910 (if (cdr error) ": " "")
f5b6cffd 911 (mapconcat (lambda (s) (prin1-to-string s t)) (cdr error) ", "))
752ef254 912 "\n\n"
f5b6cffd
KS
913 "To ensure normal operation, you should investigate and remove the\n"
914 "cause of the error in your initialization file. Start Emacs with\n"
915 "the `--debug-init' option to view a complete error backtrace.\n\n"))
752ef254
GM
916 (message "Error in init file: %s%s%s"
917 (get (car error) 'error-message)
918 (if (cdr error) ": " "")
919 (mapconcat 'prin1-to-string (cdr error) ", "))
f5b6cffd
KS
920 (let ((pop-up-windows nil))
921 (pop-to-buffer "*Messages*"))
752ef254 922 (setq init-file-had-error t)))))
ac3186fd
RS
923
924 ;; If the user has a file of abbrevs, read it.
925 (if (file-exists-p abbrev-file-name)
926 (quietly-read-abbrev-file abbrev-file-name))
927
70b199c3
RS
928 ;; If the abbrevs came entirely from the init file or the
929 ;; abbrevs file, they do not need saving.
930 (setq abbrevs-changed nil)
931
752ef254
GM
932 ;; If we can tell that the init file altered debug-on-error,
933 ;; arrange to preserve the value that it set up.
934 (or (eq debug-on-error debug-on-error-initial)
935 (setq debug-on-error-should-be-set t
936 debug-on-error-from-init-file debug-on-error)))
937 (if debug-on-error-should-be-set
938 (setq debug-on-error debug-on-error-from-init-file))
939 (unless (or default-enable-multibyte-characters
940 (eq orig-enable-multibyte default-enable-multibyte-characters))
941 ;; Init file changed to unibyte. Reset existing multibyte
942 ;; buffers (probably *scratch*, *Messages*, *Minibuff-0*).
943 ;; Arguably this should only be done if they're free of
944 ;; multibyte characters.
945 (mapcar (lambda (buffer)
946 (with-current-buffer buffer
947 (if enable-multibyte-characters
948 (set-buffer-multibyte nil))))
949 (buffer-list))
950 ;; Also re-set the language environment in case it was
951 ;; originally done before unibyte was set and is sensitive to
952 ;; unibyte (display table, terminal coding system &c).
953 (set-language-environment current-language-environment)))
f1180544 954
752ef254 955 ;; Do this here in case the init file sets mail-host-address.
5b88a2c5 956 (if (equal user-mail-address "")
752ef254
GM
957 (setq user-mail-address (concat (user-login-name) "@"
958 (or mail-host-address
959 (system-name)))))
960
961 ;; If parameter have been changed in the init file which influence
962 ;; face realization, clear the face cache so that new faces will
963 ;; be realized.
964 (unless (and (eq scalable-fonts-allowed old-scalable-fonts-allowed)
965 (eq font-list-limit old-font-list-limit)
966 (eq face-ignored-fonts old-face-ignored-fonts))
967 (clear-face-cache)))
f1180544 968
e3bd99f5
RM
969 (run-hooks 'after-init-hook)
970
a726e0d1
JB
971 ;; If *scratch* exists and init file didn't change its mode, initialize it.
972 (if (get-buffer "*scratch*")
694210c4 973 (with-current-buffer "*scratch*"
a726e0d1
JB
974 (if (eq major-mode 'fundamental-mode)
975 (funcall initial-major-mode))))
f1180544 976
a726e0d1
JB
977 ;; Load library for our terminal type.
978 ;; User init file can set term-file-prefix to nil to prevent this.
bca8c7be 979 (unless (or noninteractive
2246281f 980 initial-window-system
bca8c7be
MS
981 (null term-file-prefix))
982 (let ((term (getenv "TERM"))
983 hyphend)
984 (while (and term
985 (not (load (concat term-file-prefix term) t t)))
986 ;; Strip off last hyphen and what follows, then try again
987 (setq term
988 (if (setq hyphend (string-match "[-_][^-_]+$" term))
989 (substring term 0 hyphend)
990 nil)))))
a726e0d1 991
5777a167
RS
992 ;; Update the out-of-memory error message based on user's key bindings
993 ;; for save-some-buffers.
994 (setq memory-signal-data
995 (list 'error
996 (substitute-command-keys "Memory exhausted--use \\[save-some-buffers] then exit and restart Emacs")))
997
03e3c30a 998 ;; Process the remaining args.
a726e0d1
JB
999 (command-line-1 (cdr command-line-args))
1000
1001 ;; If -batch, terminate after processing the command options.
ca57f55d
JD
1002 (if noninteractive (kill-emacs t))
1003
1004 ;; Run emacs-session-restore (session management) if started by
1005 ;; the session manager and we have a session manager connection.
bca8c7be
MS
1006 (if (and (boundp 'x-session-previous-id)
1007 (stringp x-session-previous-id))
70646cf0 1008 (emacs-session-restore x-session-previous-id)))
a726e0d1 1009
46cfd295 1010(defcustom initial-scratch-message (purecopy "\
3b79c219
GM
1011;; This buffer is for notes you don't want to save, and for Lisp evaluation.
1012;; If you want to create a file, visit that file with C-x C-f,
1013;; then enter the text in that file's own buffer.
9fe3219e 1014
46cfd295 1015")
9fe3219e
RS
1016 "Initial message displayed in *scratch* buffer at startup.
1017If this is nil, no message will be displayed."
fa071a47
RS
1018 :type '(choice (text :tag "Message")
1019 (const :tag "none" nil))
1020 :group 'initialization)
9fe3219e 1021
ce9ded5d
GM
1022\f
1023;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1024;;; Fancy splash screen
1025;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1026
1027(defvar fancy-splash-text
26ff68aa 1028 '((:face variable-pitch
926aa0ee 1029 "You can do basic editing with the menu bar and scroll bar \
926aa0ee 1030using the mouse.\n\n"
26ff68aa 1031 :face (variable-pitch :weight bold)
ce9ded5d 1032 "Important Help menu items:\n"
26ff68aa 1033 :face variable-pitch "\
d7618b60 1034Emacs Tutorial\tLearn-by-doing tutorial for using Emacs efficiently
1fc02b3c 1035Emacs FAQ\tFrequently asked questions and answers
5777a167 1036Read the Emacs Manual\tView the Emacs manual using Info
1fc02b3c 1037\(Non)Warranty\tGNU Emacs comes with "
26ff68aa 1038 :face (variable-pitch :slant oblique)
ce9ded5d 1039 "ABSOLUTELY NO WARRANTY\n"
26ff68aa 1040 :face variable-pitch
d3c5bad2 1041 "\
d7618b60 1042Copying Conditions\tConditions for redistributing and changing Emacs
682b60ae 1043More Manuals / Ordering Manuals Buying printed manuals from the FSF\n")
d7618b60
GM
1044 (:face variable-pitch
1045 "You can do basic editing with the menu bar and scroll bar \
1046using the mouse.\n\n"
1047 :face (variable-pitch :weight bold)
1048 "Useful File menu items:\n"
1049 :face variable-pitch "\
1050Exit Emacs\t(Or type Control-x followed by Control-c)
1051Recover Session\tRecover files you were editing before a crash
1052
1053
1054
3f334882 1055
d7618b60
GM
1056"
1057 ))
ce9ded5d
GM
1058 "A list of texts to show in the middle part of splash screens.
1059Each element in the list should be a list of strings or pairs
1060`:face FACE', like `fancy-splash-insert' accepts them.")
1061
1062
fb275c02 1063(defgroup fancy-splash-screen ()
a01bb1db 1064 "Fancy splash screen when Emacs starts."
fb275c02 1065 :version "21.1"
a01bb1db
GM
1066 :group 'initialization)
1067
1068
e9da51a1 1069(defcustom fancy-splash-delay 10
49939379 1070 "*Delay in seconds between splash screens."
a01bb1db 1071 :group 'fancy-splash-screen
ce9ded5d
GM
1072 :type 'integer)
1073
1074
e9da51a1
GM
1075(defcustom fancy-splash-max-time 60
1076 "*Show splash screens for at most this number of seconds.
1077Values less than 60 seconds are ignored."
1078 :group 'fancy-splash-screen
1079 :type 'integer)
1080
1081
8ad8561b
GM
1082(defcustom fancy-splash-image nil
1083 "*The image to show in the splash screens, or nil for defaults."
a01bb1db 1084 :group 'fancy-splash-screen
8ad8561b
GM
1085 :type '(choice (const :tag "Default" nil)
1086 (file :tag "File")))
ce9ded5d
GM
1087
1088
f645586f
GM
1089;; These are temporary storage areas for the splash screen display.
1090
1091(defvar fancy-current-text nil)
1092(defvar fancy-splash-help-echo nil)
e9da51a1 1093(defvar fancy-splash-stop-time nil)
eae91b60 1094(defvar fancy-splash-outer-buffer nil)
f645586f 1095
ce9ded5d
GM
1096(defun fancy-splash-insert (&rest args)
1097 "Insert text into the current buffer, with faces.
1098Arguments from ARGS should be either strings or pairs `:face FACE',
1099where FACE is a valid face specification, as it can be used with
1100`put-text-properties'."
1101 (let ((current-face nil))
1102 (while args
1103 (if (eq (car args) :face)
1104 (setq args (cdr args) current-face (car args))
f645586f
GM
1105 (insert (propertize (car args)
1106 'face current-face
1107 'help-echo fancy-splash-help-echo)))
ce9ded5d
GM
1108 (setq args (cdr args)))))
1109
1110
1111(defun fancy-splash-head ()
1112 "Insert the head part of the splash screen into the current buffer."
51e8cfdd
GM
1113 (let* ((image-file (cond ((stringp fancy-splash-image)
1114 fancy-splash-image)
1115 ((and (display-color-p)
1116 (image-type-available-p 'xpm))
1117 (if (and (fboundp 'x-display-planes)
1118 (= (funcall 'x-display-planes) 8))
1119 "splash8.xpm"
1120 "splash.xpm"))
1121 (t "splash.pbm")))
1122 (img (create-image image-file))
ce9ded5d
GM
1123 (image-width (and img (car (image-size img))))
1124 (window-width (window-width (selected-window))))
1125 (when img
1126 (when (> window-width image-width)
f645586f 1127 ;; Center the image in the window.
ce9ded5d
GM
1128 (let ((pos (/ (- window-width image-width) 2)))
1129 (insert (propertize " " 'display `(space :align-to ,pos))))
f645586f 1130
e7f3afa9
MB
1131 ;; Change the color of the XPM version of the splash image
1132 ;; so that it is visible with a dark frame background.
1133 (when (and (memq 'xpm img)
1134 (eq (frame-parameter nil 'background-mode) 'dark))
1135 (setq img (append img '(:color-symbols (("#000000" . "gray30"))))))
1136
f645586f
GM
1137 ;; Insert the image with a help-echo and a keymap.
1138 (let ((map (make-sparse-keymap))
a622451f 1139 (help-echo "mouse-2: browse http://www.gnu.org/"))
f645586f
GM
1140 (define-key map [mouse-2]
1141 (lambda ()
1142 (interactive)
a622451f 1143 (browse-url "http://www.gnu.org/")
f645586f
GM
1144 (throw 'exit nil)))
1145 (define-key map [down-mouse-2] 'ignore)
1146 (define-key map [up-mouse-2] 'ignore)
1147 (insert-image img (propertize "xxx" 'help-echo help-echo
1148 'keymap map)))
ce9ded5d 1149 (insert "\n"))))
9d5cf87c
RS
1150 (fancy-splash-insert
1151 :face '(variable-pitch :foreground "red")
1152 (if (eq system-type 'gnu/linux)
1153 "GNU Emacs is one component of the GNU/Linux operating system."
b831c087 1154 "GNU Emacs is one component of the GNU operating system."))
eae91b60
RS
1155 (insert "\n")
1156 (unless (equal (buffer-name fancy-splash-outer-buffer) "*scratch*")
1157 (fancy-splash-insert :face 'variable-pitch
1158 (substitute-command-keys
1159 "Type \\[recenter] to begin editing your file.\n"))))
ce9ded5d
GM
1160
1161
1162(defun fancy-splash-tail ()
1163 "Insert the tail part of the splash screen into the current buffer."
95fadcca
GM
1164 (let ((fg (if (eq (frame-parameter nil 'background-mode) 'dark)
1165 "cyan" "darkblue")))
1166 (fancy-splash-insert :face `(variable-pitch :foreground ,fg)
1167 "\nThis is "
1168 (emacs-version)
1169 "\n"
1170 :face '(variable-pitch :height 0.5)
84691156 1171 "Copyright (C) 2002 Free Software Foundation, Inc.")
ed638cc9
RS
1172 (and auto-save-list-file-prefix
1173 ;; Don't signal an error if the
1174 ;; directory for auto-save-list files
1175 ;; does not yet exist.
1176 (file-directory-p (file-name-directory
1177 auto-save-list-file-prefix))
1178 (directory-files
1179 (file-name-directory auto-save-list-file-prefix)
1180 nil
1181 (concat "\\`"
1182 (regexp-quote (file-name-nondirectory
1183 auto-save-list-file-prefix)))
1184 t)
1185 (fancy-splash-insert :face '(variable-pitch :foreground "red")
1186 "\n\nIf an Emacs session crashed recently, "
1187 "type M-x recover-session RET\nto recover"
1188 " the files you were editing."))))
ce9ded5d 1189
f645586f
GM
1190(defun fancy-splash-screens-1 (buffer)
1191 "Timer function displaying a splash screen."
e9da51a1
GM
1192 (when (> (float-time) fancy-splash-stop-time)
1193 (throw 'stop-splashing nil))
f645586f
GM
1194 (unless fancy-current-text
1195 (setq fancy-current-text fancy-splash-text))
1196 (let ((text (car fancy-current-text)))
1197 (set-buffer buffer)
1198 (erase-buffer)
b693e375
RS
1199 (if pure-space-overflow
1200 (insert "Warning Warning Pure space overflow Warning Warning\n"))
f645586f
GM
1201 (fancy-splash-head)
1202 (apply #'fancy-splash-insert text)
1203 (fancy-splash-tail)
1204 (unless (current-message)
1205 (message fancy-splash-help-echo))
1206 (set-buffer-modified-p nil)
dbeb499b 1207 (goto-char (point-min))
f645586f
GM
1208 (force-mode-line-update)
1209 (setq fancy-current-text (cdr fancy-current-text))))
1210
1211
1212(defun fancy-splash-default-action ()
732950b1
JB
1213 "Stop displaying the splash screen buffer.
1214This is an internal function used to turn off the splash screen after
1215the user caused an input event by hitting a key or clicking with the
1216mouse."
f645586f
GM
1217 (interactive)
1218 (push last-command-event unread-command-events)
1219 (throw 'exit nil))
1220
1221
ce9ded5d 1222(defun fancy-splash-screens ()
f645586f 1223 "Display fancy splash screens when Emacs starts."
5b61c6a7 1224 (setq fancy-splash-help-echo (startup-echo-area-message))
848c7757 1225 (let ((old-hourglass display-hourglass)
eae91b60
RS
1226 (fancy-splash-outer-buffer (current-buffer))
1227 splash-buffer
ff4ec1f7 1228 (old-minor-mode-map-alist minor-mode-map-alist)
6b20fb8e 1229 (frame (fancy-splash-frame))
5b61c6a7 1230 timer)
6b20fb8e
RS
1231 (save-selected-window
1232 (select-frame frame)
1233 (switch-to-buffer "GNU Emacs")
1234 (setq tab-width 20)
1235 (setq splash-buffer (current-buffer))
1236 (catch 'stop-splashing
1237 (unwind-protect
1238 (let ((map (make-sparse-keymap)))
1239 (use-local-map map)
1240 (define-key map [switch-frame] 'ignore)
1241 (define-key map [t] 'fancy-splash-default-action)
1242 (define-key map [mouse-movement] 'ignore)
1243 (define-key map [mode-line t] 'ignore)
1244 (setq cursor-type nil
1245 display-hourglass nil
1246 minor-mode-map-alist nil
1247 buffer-undo-list t
f1180544 1248 mode-line-format (propertize "---- %b %-"
6b20fb8e
RS
1249 'face '(:weight bold))
1250 fancy-splash-stop-time (+ (float-time)
1251 (max 60 fancy-splash-max-time))
1252 timer (run-with-timer 0 fancy-splash-delay
1253 #'fancy-splash-screens-1
1254 splash-buffer))
1255 (recursive-edit))
e9da51a1 1256 (cancel-timer timer)
ff4ec1f7
GM
1257 (setq display-hourglass old-hourglass
1258 minor-mode-map-alist old-minor-mode-map-alist)
6b20fb8e
RS
1259 (kill-buffer splash-buffer))))))
1260
1261(defun fancy-splash-frame ()
1262 "Return the frame to use for the fancy splash screen.
1263Returning non-nil does not mean we should necessarily
1264use the fancy splash screen, but if we do use it,
1265we put it on this frame."
1266 (let (chosen-frame)
3a321ddb 1267 (dolist (frame (append (frame-list) (list (selected-frame))))
6b20fb8e
RS
1268 (if (and (frame-visible-p frame)
1269 (not (window-minibuffer-p (frame-selected-window frame))))
1270 (setq chosen-frame frame)))
1271 chosen-frame))
f645586f 1272
285991dc
GM
1273(defun use-fancy-splash-screens-p ()
1274 "Return t if fancy splash screens should be used."
9df076f2
GM
1275 (when (and (display-graphic-p)
1276 (or (and (display-color-p)
285991dc 1277 (image-type-available-p 'xpm))
9df076f2 1278 (image-type-available-p 'pbm)))
16d2fae9
JPW
1279 (let ((frame (fancy-splash-frame)))
1280 (when frame
1281 (let* ((img (create-image (or fancy-splash-image
1282 (if (and (display-color-p)
1283 (image-type-available-p 'xpm))
1284 "splash.xpm" "splash.pbm"))))
1285 (image-height (and img (cdr (image-size img))))
1286 (window-height (1- (window-height (frame-selected-window frame)))))
1287 (> window-height (+ image-height 19)))))))
285991dc
GM
1288
1289
c61453ea
PJ
1290(defun normal-splash-screen ()
1291 "Display splash screen when Emacs starts."
82e736c1
JPW
1292 (let ((prev-buffer (current-buffer)))
1293 (unwind-protect
1294 (with-current-buffer (get-buffer-create "GNU Emacs")
1295 (let ((tab-width 8)
f1180544 1296 (mode-line-format (propertize "---- %b %-"
82e736c1
JPW
1297 'face '(:weight bold))))
1298
b693e375
RS
1299 (if pure-space-overflow
1300 (insert "Warning Warning Pure space overflow Warning Warning\n"))
1301
82e736c1
JPW
1302 ;; The convention for this piece of code is that
1303 ;; each piece of output starts with one or two newlines
1304 ;; and does not end with any newlines.
1305 (insert "Welcome to GNU Emacs")
9d5cf87c
RS
1306 (insert
1307 (if (eq system-type 'gnu/linux)
1308 ", one component of the GNU/Linux operating system.\n"
1309 ", a part of the GNU operating system.\n"))
82e736c1
JPW
1310
1311 (unless (equal (buffer-name prev-buffer) "*scratch*")
1312 (insert (substitute-command-keys
1313 "\nType \\[recenter] to begin editing your file.\n")))
1314
1315 (if (display-mouse-p)
1316 ;; The user can use the mouse to activate menus
1317 ;; so give help in terms of menu items.
1318 (progn
1319 (insert "\
c61453ea
PJ
1320You can do basic editing with the menu bar and scroll bar using the mouse.
1321
1322Useful File menu items:
1323Exit Emacs (or type Control-x followed by Control-c)
1324Recover Session recover files you were editing before a crash
1325
1326Important Help menu items:
1327Emacs Tutorial Learn-by-doing tutorial for using Emacs efficiently.
1328Emacs FAQ Frequently asked questions and answers
5777a167 1329Read the Emacs Manual View the Emacs manual using Info
c61453ea
PJ
1330\(Non)Warranty GNU Emacs comes with ABSOLUTELY NO WARRANTY
1331Copying Conditions Conditions for redistributing and changing Emacs.
1332Getting New Versions How to obtain the latest version of Emacs.
682b60ae 1333More Manuals / Ordering Manuals How to order printed manuals from the FSF.
c61453ea 1334")
82e736c1
JPW
1335 (insert "\n\n" (emacs-version)
1336 "
84691156 1337Copyright (C) 2002 Free Software Foundation, Inc."))
c61453ea 1338
82e736c1
JPW
1339 ;; No mouse menus, so give help using kbd commands.
1340
1341 ;; If keys have their default meanings,
1342 ;; use precomputed string to save lots of time.
1343 (if (and (eq (key-binding "\C-h") 'help-command)
1344 (eq (key-binding "\C-xu") 'advertised-undo)
1345 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-emacs)
1346 (eq (key-binding "\C-ht") 'help-with-tutorial)
1347 (eq (key-binding "\C-hi") 'info)
1348 (eq (key-binding "\C-hr") 'info-emacs-manual)
1349 (eq (key-binding "\C-h\C-n") 'view-emacs-news))
1350 (insert "
c61453ea 1351Get help C-h (Hold down CTRL and press h)
5777a167
RS
1352Emacs manual C-h r
1353Emacs tutorial C-h t Undo changes C-x u
1354Buy manuals C-h C-m Exit Emacs C-x C-c
1355Browse manuals C-h i")
1356
82e736c1
JPW
1357 (insert (substitute-command-keys
1358 (format "\n
c61453ea 1359Get help %s
5777a167 1360Emacs manual \\[info-emacs-manual]
82e736c1
JPW
1361Emacs tutorial \\[help-with-tutorial]\tUndo changes\t\\[advertised-undo]
1362Buy manuals \\[view-order-manuals]\tExit Emacs\t\\[save-buffers-kill-emacs]
5777a167 1363Browse manuals \\[info]"
82e736c1
JPW
1364 (let ((where (where-is-internal
1365 'help-command nil t)))
1366 (if where
1367 (key-description where)
1368 "M-x help"))))))
1369
1370 ;; Say how to use the menu bar with the keyboard.
1371 (if (and (eq (key-binding "\M-`") 'tmm-menubar)
1372 (eq (key-binding [f10]) 'tmm-menubar))
1373 (insert "
c61453ea 1374Activate menubar F10 or ESC ` or M-`")
82e736c1 1375 (insert (substitute-command-keys "
c61453ea
PJ
1376Activate menubar \\[tmm-menubar]")))
1377
82e736c1
JPW
1378 ;; Many users seem to have problems with these.
1379 (insert "
c61453ea
PJ
1380\(`C-' means use the CTRL key. `M-' means use the Meta (or Alt) key.
1381If you have no Meta key, you may instead type ESC followed by the character.)")
1382
82e736c1
JPW
1383 (insert "\n\n" (emacs-version)
1384 "
84691156 1385Copyright (C) 2002 Free Software Foundation, Inc.")
c61453ea 1386
82e736c1
JPW
1387 (if (and (eq (key-binding "\C-h\C-c") 'describe-copying)
1388 (eq (key-binding "\C-h\C-d") 'describe-distribution)
1389 (eq (key-binding "\C-h\C-w") 'describe-no-warranty))
f1180544 1390 (insert
82e736c1 1391 "\n
c61453ea
PJ
1392GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
1393Emacs is Free Software--Free as in Freedom--so you can redistribute copies
1394of Emacs and modify it; type C-h C-c to see the conditions.
1395Type C-h C-d for information on getting the latest version.")
82e736c1
JPW
1396 (insert (substitute-command-keys
1397 "\n
c61453ea
PJ
1398GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for full details.
1399Emacs is Free Software--Free as in Freedom--so you can redistribute copies
1400of Emacs and modify it; type \\[describe-copying] to see the conditions.
1401Type \\[describe-distribution] for information on getting the latest version."))))
1402
82e736c1
JPW
1403 ;; The rest of the startup screen is the same on all
1404 ;; kinds of terminals.
1405
1406 ;; Give information on recovering, if there was a crash.
1407 (and auto-save-list-file-prefix
1408 ;; Don't signal an error if the
1409 ;; directory for auto-save-list files
1410 ;; does not yet exist.
1411 (file-directory-p (file-name-directory
1412 auto-save-list-file-prefix))
1413 (directory-files
1414 (file-name-directory auto-save-list-file-prefix)
1415 nil
1416 (concat "\\`"
1417 (regexp-quote (file-name-nondirectory
1418 auto-save-list-file-prefix)))
1419 t)
1420 (insert "\n\nIf an Emacs session crashed recently, "
1421 "type M-x recover-session RET\nto recover"
1422 " the files you were editing."))
1423
1424 ;; Display the input that we set up in the buffer.
1425 (set-buffer-modified-p nil)
1426 (goto-char (point-min))
1427 (save-window-excursion
1428 (switch-to-buffer (current-buffer))
1429 (sit-for 120))))
1430 ;; Unwind ... ensure splash buffer is killed
1431 (kill-buffer "GNU Emacs"))))
1432
c61453ea 1433
f645586f
GM
1434(defun startup-echo-area-message ()
1435 (if (eq (key-binding "\C-h\C-p") 'describe-project)
1436 "For information about the GNU Project and its goals, type C-h C-p."
1437 (substitute-command-keys
1438 "For information about the GNU Project and its goals, type \
1439\\[describe-project].")))
ce9ded5d
GM
1440
1441
49939379 1442(defun display-startup-echo-area-message ()
15fa6db0
GM
1443 (let ((resize-mini-windows t))
1444 (message (startup-echo-area-message))))
f645586f 1445
49939379 1446
c61453ea
PJ
1447(defun display-splash-screen ()
1448 "Display splash screen according to display.
1449Fancy splash screens are used on graphic displays,
1450normal otherwise."
1451 (interactive)
9df076f2 1452 (if (use-fancy-splash-screens-p)
c61453ea
PJ
1453 (fancy-splash-screens)
1454 (normal-splash-screen)))
1455
1456
a726e0d1 1457(defun command-line-1 (command-line-args-left)
52320897 1458 (or noninteractive (input-pending-p) init-file-had-error
ed638cc9
RS
1459 ;; t if the init file says to inhibit the echo area startup message.
1460 (and inhibit-startup-echo-area-message
1461 user-init-file
1462 (or (and (get 'inhibit-startup-echo-area-message 'saved-value)
1463 (equal inhibit-startup-echo-area-message
bca8c7be 1464 (if (equal init-file-user "")
ed638cc9
RS
1465 (user-login-name)
1466 init-file-user)))
1467 ;; Wasn't set with custom; see if .emacs has a setq.
1468 (let ((buffer (get-buffer-create " *temp*")))
1469 (prog1
1470 (condition-case nil
1471 (save-excursion
1472 (set-buffer buffer)
1473 (insert-file-contents user-init-file)
1474 (re-search-forward
1475 (concat
1476 "([ \t\n]*setq[ \t\n]+"
1477 "inhibit-startup-echo-area-message[ \t\n]+"
1478 (regexp-quote
1479 (prin1-to-string
bca8c7be 1480 (if (equal init-file-user "")
ed638cc9
RS
1481 (user-login-name)
1482 init-file-user)))
1483 "[ \t\n]*)")
1484 nil t))
1485 (error nil))
1486 (kill-buffer buffer)))))
9f8b5809
GM
1487 ;; Stop any "Loading image..." message hiding echo-area-message.
1488 (use-fancy-splash-screens-p)
ed638cc9
RS
1489 (display-startup-echo-area-message))
1490
1491 ;; Delay 2 seconds after an init file error message
1492 ;; was displayed, so user can read it.
bca8c7be
MS
1493 (when init-file-had-error
1494 (sit-for 2))
1495
1496 (when command-line-args-left
1497 ;; We have command args; process them.
1498 (let ((dir command-line-default-directory)
1499 (file-count 0)
1500 first-file-buffer
1501 tem
1502 ;; The directories listed in --directory/-L options will *appear*
1503 ;; at the front of `load-path' in the order they appear on the
1504 ;; command-line. We cannot do this by *placing* them at the front
1505 ;; in the order they appear, so we need this variable to hold them,
1506 ;; temporarily.
1507 extra-load-path
1508 just-files ;; t if this follows the magic -- option.
1509 ;; This includes our standard options' long versions
1510 ;; and long versions of what's on command-switch-alist.
1511 (longopts
1512 (append '(("--funcall") ("--load") ("--insert") ("--kill")
1513 ("--directory") ("--eval") ("--execute") ("--no-splash")
1514 ("--find-file") ("--visit") ("--file"))
1515 (mapcar (lambda (elt)
1516 (list (concat "-" (car elt))))
1517 command-switch-alist)))
1518 (line 0)
1519 (column 0))
1520
1521 ;; Add the long X options to longopts.
1522 (dolist (tem command-line-x-option-alist)
1523 (if (string-match "^--" (car tem))
1524 (push (list (car tem)) longopts)))
1525
1526 ;; Loop, processing options.
1527 (while command-line-args-left
1528 (let* ((argi (car command-line-args-left))
1529 (orig-argi argi)
1530 argval completion)
1531 (setq command-line-args-left (cdr command-line-args-left))
1532
1533 ;; Do preliminary decoding of the option.
1534 (if just-files
1535 ;; After --, don't look for options; treat all args as files.
1536 (setq argi "")
1537 ;; Convert long options to ordinary options
1538 ;; and separate out an attached option argument into argval.
1539 (when (string-match "^\\(--[^=]*\\)=" argi)
1540 (setq argval (substring argi (match-end 0))
1541 argi (match-string 1 argi)))
1542 (if (equal argi "--")
1543 (setq completion nil)
1544 (setq completion (try-completion argi longopts)))
1545 (if (eq completion t)
1546 (setq argi (substring argi 1))
1547 (if (stringp completion)
1548 (let ((elt (assoc completion longopts)))
1549 (or elt
1550 (error "Option `%s' is ambiguous" argi))
1551 (setq argi (substring (car elt) 1)))
1552 (setq argval nil
1553 argi orig-argi))))
1554
1555 ;; Execute the option.
1556 (cond ((setq tem (assoc argi command-switch-alist))
1557 (if argval
1558 (let ((command-line-args-left
1559 (cons argval command-line-args-left)))
1560 (funcall (cdr tem) argi))
1561 (funcall (cdr tem) argi)))
1562
1563 ((equal argi "-no-splash")
1564 (setq inhibit-startup-message t))
1565
1566 ((member argi '("-f" ; what the manual claims
1567 "-funcall"
1568 "-e")) ; what the source used to say
1569 (setq tem (intern (or argval (pop command-line-args-left))))
f39864ec 1570 (if (commandp tem)
bca8c7be
MS
1571 (command-execute tem)
1572 (funcall tem)))
1573
1574 ((member argi '("-eval" "-execute"))
1575 (eval (read (or argval (pop command-line-args-left)))))
1576 ;; Set the default directory as specified in -L.
1577
1578 ((member argi '("-L" "-directory"))
1579 (setq tem (or argval (pop command-line-args-left)))
1580 ;; We will reverse `extra-load-path' and prepend it to
1581 ;; `load-path' after all the arguments have been processed.
1582 (push
1583 (expand-file-name (command-line-normalize-file-name tem))
1584 extra-load-path))
1585
1586 ((member argi '("-l" "-load"))
1587 (let* ((file (command-line-normalize-file-name
1588 (or argval (pop command-line-args-left))))
1589 ;; Take file from default dir if it exists there;
1590 ;; otherwise let `load' search for it.
1591 (file-ex (expand-file-name file)))
1592 (when (file-exists-p file-ex)
1593 (setq file file-ex))
1594 (load file nil t)))
1595
1596 ((equal argi "-insert")
1597 (setq tem (or argval (pop command-line-args-left)))
1598 (or (stringp tem)
1599 (error "File name omitted from `-insert' option"))
1600 (insert-file-contents (command-line-normalize-file-name tem)))
1601
1602 ((equal argi "-kill")
1603 (kill-emacs t))
1604
1605 ((string-match "^\\+[0-9]+\\'" argi)
1606 (setq line (string-to-int argi)))
1607
1608 ((string-match "^\\+\\([0-9]+\\):\\([0-9]+\\)\\'" argi)
1609 (setq line (string-to-int (match-string 1 argi))
1610 column (string-to-int (match-string 2 argi))))
1611
1612 ((setq tem (assoc argi command-line-x-option-alist))
1613 ;; Ignore X-windows options and their args if not using X.
1614 (setq command-line-args-left
1615 (nthcdr (nth 1 tem) command-line-args-left)))
1616
1617 ((member argi '("-find-file" "-file" "-visit"))
1618 ;; An explicit option to specify visiting a file.
1619 (setq tem (or argval (pop command-line-args-left)))
1620 (unless (stringp tem)
1621 (error "File name omitted from `%s' option" argi))
1622 (setq file-count (1+ file-count))
1623 (let ((file (expand-file-name
1624 (command-line-normalize-file-name tem) dir)))
1625 (if (= file-count 1)
1626 (setq first-file-buffer (find-file file))
1627 (find-file-other-window file)))
1628 (or (zerop line)
1629 (goto-line line))
1630 (setq line 0)
1631 (unless (< column 1)
1632 (move-to-column (1- column)))
1633 (setq column 0))
1634
1635 ((equal argi "--")
1636 (setq just-files t))
1637 (t
1638 ;; We have almost exhausted our options. See if the
1639 ;; user has made any other command-line options available
1640 (let ((hooks command-line-functions) ;; lrs 7/31/89
1641 (did-hook nil))
1642 (while (and hooks
1643 (not (setq did-hook (funcall (car hooks)))))
1644 (setq hooks (cdr hooks)))
1645 (if (not did-hook)
1646 ;; Presume that the argument is a file name.
1647 (progn
1648 (if (string-match "\\`-" argi)
1649 (error "Unknown option `%s'" argi))
1650 (setq file-count (1+ file-count))
1651 (let ((file
1652 (expand-file-name
1653 (command-line-normalize-file-name orig-argi)
1654 dir)))
1655 (if (= file-count 1)
1656 (setq first-file-buffer (find-file file))
1657 (find-file-other-window file)))
1658 (or (zerop line)
1659 (goto-line line))
1660 (setq line 0)
1661 (unless (< column 1)
1662 (move-to-column (1- column)))
1663 (setq column 0))))))))
1664
1665 ;; See --directory/-L option above.
1666 (when extra-load-path
1667 (setq load-path (append (nreverse extra-load-path) load-path)))
1668
1669 ;; If 3 or more files visited, and not all visible,
1670 ;; show user what they all are. But leave the last one current.
1671 (and (> file-count 2)
1672 (not noninteractive)
1673 (not inhibit-startup-buffer-menu)
1674 (or (get-buffer-window first-file-buffer)
1675 (list-buffers)))))
eae91b60
RS
1676
1677 ;; Maybe display a startup screen.
1678 (when (and (not inhibit-startup-message) (not noninteractive)
1679 ;; Don't display startup screen if init file
1680 ;; has started some sort of server.
1681 (not (and (fboundp 'process-list)
1682 (process-list))))
1683 ;; Display a startup screen, after some preparations.
1684
1685 ;; If there are no switches to process, we might as well
1686 ;; run this hook now, and there may be some need to do it
1687 ;; before doing any output.
deebb0e9 1688 (run-hooks 'emacs-startup-hook)
eae91b60
RS
1689 (and term-setup-hook
1690 (run-hooks 'term-setup-hook))
deebb0e9 1691 (setq inhibit-startup-hooks t)
eae91b60
RS
1692
1693 ;; It's important to notice the user settings before we
1694 ;; display the startup message; otherwise, the settings
1695 ;; won't take effect until the user gives the first
1696 ;; keystroke, and that's distracting.
1697 (when (fboundp 'frame-notice-user-settings)
1698 (frame-notice-user-settings))
1699
1700 ;; If there are no switches to process, we might as well
1701 ;; run this hook now, and there may be some need to do it
1702 ;; before doing any output.
1703 (when window-setup-hook
1704 (run-hooks 'window-setup-hook)
1705 ;; Don't let the hook be run twice.
1706 (setq window-setup-hook nil))
1707
1708 ;; Do this now to avoid an annoying delay if the user
1709 ;; clicks the menu bar during the sit-for.
1710 (when (display-popup-menus-p)
1711 (precompute-menubar-bindings))
d759a2f5
RS
1712 (with-no-warnings
1713 (setq menubar-bindings-done t))
ed638cc9 1714
eae91b60
RS
1715 ;; If *scratch* is selected and it is empty, insert an
1716 ;; initial message saying not to create a file there.
1717 (when (and initial-scratch-message
bca8c7be 1718 (equal (buffer-name) "*scratch*")
ed638cc9 1719 (= 0 (buffer-size)))
eae91b60
RS
1720 (insert initial-scratch-message)
1721 (set-buffer-modified-p nil))
ed638cc9 1722
eae91b60
RS
1723 ;; If user typed input during all that work,
1724 ;; abort the startup screen. Otherwise, display it now.
c61453ea
PJ
1725 (unless (input-pending-p)
1726 (display-splash-screen))))
c88ab9ce 1727
49939379 1728
47c7adae
RS
1729(defun command-line-normalize-file-name (file)
1730 "Collapse multiple slashes to one, to handle non-Emacs file names."
6b9e794f
RS
1731 (save-match-data
1732 ;; Use arg 1 so that we don't collapse // at the start of the file name.
1733 ;; That is significant on some systems.
1734 ;; However, /// at the beginning is supposed to mean just /, not //.
1735 (if (string-match "^///+" file)
1736 (setq file (replace-match "/" t t file)))
1737 (while (string-match "//+" file 1)
1738 (setq file (replace-match "/" t t file)))
1739 file))
47c7adae 1740
ab5796a9 1741;;; arch-tag: 7e294698-244d-4758-984b-4047f887a5db
c88ab9ce 1742;;; startup.el ends here