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