* w32fns.c (Fx_create_frame): Remove duplicate unwind_protect.
[bpt/emacs.git] / lisp / startup.el
CommitLineData
c88ab9ce
ER
1;;; startup.el --- process Emacs shell arguments
2
a3194d03 3;; Copyright (C) 1985, 1986, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
93926b07
GM
4;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5;; Free Software Foundation, Inc.
eea8d4ef 6
630cc463 7;; Maintainer: FSF
d7b4d18f 8;; Keywords: internal
630cc463 9
a726e0d1
JB
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
a726e0d1 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
a726e0d1
JB
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a726e0d1 24
630cc463 25;;; Commentary:
a726e0d1 26
6b61353c
KH
27;; This file parses the command line and gets Emacs running. Options
28;; on the command line are handled in precedence order. For priorities
29;; see the structure standard_args in the emacs.c file.
a726e0d1 30
630cc463
ER
31;;; Code:
32
a726e0d1
JB
33(setq top-level '(normal-top-level))
34
42a3e6fa 35(defvar command-line-processed nil
2879a13b 36 "Non-nil once command line has been processed.")
a726e0d1 37
42a3e6fa 38(defgroup initialization nil
ca3685a3 39 "Emacs start-up procedure."
26cdce23
JL
40 :group 'environment)
41
42(defcustom initial-buffer-choice nil
43 "Buffer to show after starting Emacs.
490a1ad6 44If the value is nil and `inhibit-startup-screen' is nil, show the
26cdce23
JL
45startup screen. If the value is string, visit the specified file or
46directory using `find-file'. If t, open the `*scratch*' buffer."
47 :type '(choice
490a1ad6 48 (const :tag "Startup screen" nil)
26cdce23
JL
49 (directory :tag "Directory" :value "~/")
50 (file :tag "File" :value "~/file.txt")
51 (const :tag "Lisp scratch buffer" t))
52 :version "23.1"
53 :group 'initialization)
42a3e6fa 54
490a1ad6 55(defcustom inhibit-startup-screen nil
249d9683 56 "Non-nil inhibits the startup screen.
249d9683 57
531b591d
AM
58This is for use in your personal init file (but NOT site-start.el), once
59you are familiar with the contents of the startup screen."
42a3e6fa
RS
60 :type 'boolean
61 :group 'initialization)
a726e0d1 62
490a1ad6
JL
63(defvaralias 'inhibit-splash-screen 'inhibit-startup-screen)
64(defvaralias 'inhibit-startup-message 'inhibit-startup-screen)
1b207153 65
22a58255 66(defvar startup-screen-inhibit-startup-screen nil)
1b207153 67
42a3e6fa 68(defcustom inhibit-startup-echo-area-message nil
1d7da582 69 "*Non-nil inhibits the initial startup echo area message.
42a3e6fa
RS
70Setting this variable takes effect
71only if you do it with the customization buffer
94487c4e 72or if your `.emacs' file contains a line of this form:
54a003f7 73 (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
e5575c06
RS
74If your `.emacs' file is byte-compiled, use the following form instead:
75 (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
1d7da582 76Thus, someone else using a copy of your `.emacs' file will see
42a3e6fa
RS
77the startup message unless he personally acts to inhibit it."
78 :type '(choice (const :tag "Don't inhibit")
79 (string :tag "Enter your user name, to inhibit"))
80 :group 'initialization)
1d7da582 81
42a3e6fa
RS
82(defcustom inhibit-default-init nil
83 "*Non-nil inhibits loading the `default' library."
84 :type 'boolean
85 :group 'initialization)
a726e0d1 86
802a980a
GM
87(defcustom inhibit-startup-buffer-menu nil
88 "*Non-nil inhibits display of buffer list when more than 2 files are loaded."
89 :type 'boolean
90 :group 'initialization)
91
d7fa5aa2 92(defvar command-switch-alist nil
a726e0d1
JB
93 "Alist of command-line switches.
94Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
6b61353c
KH
95HANDLER-FUNCTION receives the switch string as its sole argument;
96the remaining command-line args are in the variable `command-line-args-left'.")
a726e0d1 97
860befc8
RS
98(defvar command-line-args-left nil
99 "List of command-line args not yet processed.")
100
aa5310e4
DK
101(defvaralias 'argv 'command-line-args-left
102 "List of command-line args not yet processed.
103This is a convenience alias, so that one can write \(pop argv\)
104inside of --eval command line arguments in order to access
105following arguments.")
106
a726e0d1
JB
107(defvar command-line-functions nil ;; lrs 7/31/89
108 "List of functions to process unrecognized command-line arguments.
109Each function should access the dynamically bound variables
b4484ea8 110`argi' (the current argument) and `command-line-args-left' (the remaining
a726e0d1 111arguments). The function should return non-nil only if it recognizes and
b4484ea8
RS
112processes `argi'. If it does so, it may consume successive arguments by
113altering `command-line-args-left' to remove them.")
a726e0d1 114
09a1077c
RS
115(defvar command-line-default-directory nil
116 "Default directory to use for command line arguments.
117This is normally copied from `default-directory' when Emacs starts.")
118
b3afdeb8
RS
119;;; This is here, rather than in x-win.el, so that we can ignore these
120;;; options when we are not using X.
46cfd295 121(defconst command-line-x-option-alist
b3afdeb8
RS
122 '(("-bw" 1 x-handle-numeric-switch border-width)
123 ("-d" 1 x-handle-display)
124 ("-display" 1 x-handle-display)
5676ab57 125 ("-name" 1 x-handle-name-switch)
2c3fef40
RS
126 ("-title" 1 x-handle-switch title)
127 ("-T" 1 x-handle-switch title)
b3afdeb8
RS
128 ("-r" 0 x-handle-switch reverse t)
129 ("-rv" 0 x-handle-switch reverse t)
130 ("-reverse" 0 x-handle-switch reverse t)
131 ("-reverse-video" 0 x-handle-switch reverse t)
132 ("-fn" 1 x-handle-switch font)
133 ("-font" 1 x-handle-switch font)
188b6a5e
EZ
134 ("-fs" 0 x-handle-initial-switch fullscreen fullboth)
135 ("-fw" 0 x-handle-initial-switch fullscreen fullwidth)
136 ("-fh" 0 x-handle-initial-switch fullscreen fullheight)
b3afdeb8 137 ("-ib" 1 x-handle-numeric-switch internal-border-width)
1f7f78f1 138 ("-g" 1 x-handle-geometry)
9ff6bda1 139 ("-lsp" 1 x-handle-numeric-switch line-spacing)
1f7f78f1 140 ("-geometry" 1 x-handle-geometry)
b3afdeb8
RS
141 ("-fg" 1 x-handle-switch foreground-color)
142 ("-foreground" 1 x-handle-switch foreground-color)
143 ("-bg" 1 x-handle-switch background-color)
144 ("-background" 1 x-handle-switch background-color)
145 ("-ms" 1 x-handle-switch mouse-color)
c755acf3 146 ("-nbi" 0 x-handle-switch icon-type nil)
b3afdeb8
RS
147 ("-iconic" 0 x-handle-iconic)
148 ("-xrm" 1 x-handle-xrm-switch)
149 ("-cr" 1 x-handle-switch cursor-color)
150 ("-vb" 0 x-handle-switch vertical-scroll-bars t)
151 ("-hb" 0 x-handle-switch horizontal-scroll-bars t)
152 ("-bd" 1 x-handle-switch)
153 ("--border-width" 1 x-handle-numeric-switch border-width)
154 ("--display" 1 x-handle-display)
5676ab57 155 ("--name" 1 x-handle-name-switch)
7775acb8 156 ("--title" 1 x-handle-switch title)
b3afdeb8
RS
157 ("--reverse-video" 0 x-handle-switch reverse t)
158 ("--font" 1 x-handle-switch font)
188b6a5e
EZ
159 ("--fullscreen" 0 x-handle-initial-switch fullscreen fullboth)
160 ("--fullwidth" 0 x-handle-initial-switch fullscreen fullwidth)
161 ("--fullheight" 0 x-handle-initial-switch fullscreen fullheight)
b3afdeb8 162 ("--internal-border" 1 x-handle-numeric-switch internal-border-width)
1f7f78f1 163 ("--geometry" 1 x-handle-geometry)
b3afdeb8
RS
164 ("--foreground-color" 1 x-handle-switch foreground-color)
165 ("--background-color" 1 x-handle-switch background-color)
166 ("--mouse-color" 1 x-handle-switch mouse-color)
bb1f0f78 167 ("--no-bitmap-icon" 0 x-handle-no-bitmap-icon)
b3afdeb8
RS
168 ("--iconic" 0 x-handle-iconic)
169 ("--xrm" 1 x-handle-xrm-switch)
170 ("--cursor-color" 1 x-handle-switch cursor-color)
171 ("--vertical-scroll-bars" 0 x-handle-switch vertical-scroll-bars t)
9ff6bda1 172 ("--line-spacing" 1 x-handle-numeric-switch line-spacing)
7f944e8e 173 ("--border-color" 1 x-handle-switch border-color)
c434f203
JD
174 ("--smid" 1 x-handle-smid)
175 ("--parent-id" 1 x-handle-parent-id))
b3afdeb8
RS
176 "Alist of X Windows options.
177Each element has the form
178 (NAME NUMARGS HANDLER FRAME-PARAM VALUE)
179where NAME is the option name string, NUMARGS is the number of arguments
180that the option accepts, HANDLER is a function to call to handle the option.
181FRAME-PARAM (optional) is the frame parameter this option specifies,
182and VALUE is the value which is given to that frame parameter
183\(most options use the argument for this, so VALUE is not present).")
184
e3bd99f5 185(defvar before-init-hook nil
db3f571a 186 "Normal hook run after handling urgent options but before loading init files.")
3fc958a4 187
e3bd99f5 188(defvar after-init-hook nil
db3f571a
KH
189 "Normal hook run after loading the init files, `~/.emacs' and `default.el'.
190There is no `condition-case' around the running of these functions;
191therefore, if you set `debug-on-error' non-nil in `.emacs',
192an error in one of these functions will invoke the debugger.")
193
5c9fad41
GM
194(defvar before-init-time nil
195 "Value of `current-time' before Emacs begins initialization.")
238cbdf8 196
5ab0e67e
JL
197(defvar after-init-time nil
198 "Value of `current-time' after loading the init files.")
199
db3f571a
KH
200(defvar emacs-startup-hook nil
201 "Normal hook run after loading init files and handling the command line.")
e3bd99f5 202
a726e0d1 203(defvar term-setup-hook nil
db3f571a
KH
204 "Normal hook run after loading terminal-specific Lisp code.
205It also follows `emacs-startup-hook'. This hook exists for users to set,
a726e0d1
JB
206so as to override the definitions made by the terminal-specific file.
207Emacs never sets this variable itself.")
208
deebb0e9
RS
209(defvar inhibit-startup-hooks nil
210 "Non-nil means don't run `term-setup-hook' and `emacs-startup-hook'.
211This is because we already did so.")
212
a726e0d1 213(defvar keyboard-type nil
b4484ea8 214 "The brand of keyboard you are using.
9ab281f0
JB
215This variable is used to define the proper function and keypad
216keys for use under X. It is used in a fashion analogous to the
217environment variable TERM.")
a726e0d1
JB
218
219(defvar window-setup-hook nil
b4484ea8
RS
220 "Normal hook run to initialize window system display.
221Emacs runs this hook after processing the command line arguments and loading
222the user's init file.")
a726e0d1 223
42a3e6fa 224(defcustom initial-major-mode 'lisp-interaction-mode
ab863ac2 225 "Major mode command symbol to use for the initial `*scratch*' buffer."
ce2f921e 226 :type 'function
42a3e6fa 227 :group 'initialization)
a726e0d1 228
ecbff273 229(defvar init-file-user nil
a726e0d1 230 "Identity of user whose `.emacs' file is or was read.
d7fa5aa2
RS
231The value is nil if `-q' or `--no-init-file' was specified,
232meaning do not load any init file.
233
d3a81eee
EZ
234Otherwise, the value may be an empty string, meaning
235use the init file for the user who originally logged in,
236or it may be a string containing a user's name meaning
237use that person's init file.
a726e0d1 238
2bdfaa42
KH
239In either of the latter cases, `(concat \"~\" init-file-user \"/\")'
240evaluates to the name of the directory where the `.emacs' file was
13fce4e6
RS
241looked for.
242
243Setting `init-file-user' does not prevent Emacs from loading
ecbff273 244`site-start.el'. The only way to do that is to use `--no-site-file'.")
a726e0d1 245
42a3e6fa 246(defcustom site-run-file "site-start"
b7444d31
RS
247 "File containing site-wide run-time initializations.
248This file is loaded at run-time before `~/.emacs'. It contains inits
249that need to be in place for the entire site, but which, due to their
fcaf7de9 250higher incidence of change, don't make sense to load into Emacs's
b7444d31 251dumped image. Thus, the run-time load order is: 1. file described in
13fce4e6
RS
252this variable, if non-nil; 2. `~/.emacs'; 3. `default.el'.
253
254Don't use the `site-start.el' file for things some users may not like.
255Put them in `default.el' instead, so that users can more easily
256override them. Users can prevent loading `default.el' with the `-q'
257option or by setting `inhibit-default-init' in their own init files,
258but inhibiting `site-start.el' requires `--no-site-file', which
842a1eac
RS
259is less convenient.
260
261This variable is defined for customization so as to make
262it visible in the relevant context. However, actually customizing it
263is not allowed, since it would not work anyway. The only way to set
9ab281f0 264this variable usefully is to set it while building and dumping Emacs."
93812130 265 :type '(choice (const :tag "none" nil) string)
842a1eac
RS
266 :group 'initialization
267 :initialize 'custom-initialize-default
268 :set '(lambda (variable value)
269 (error "Customizing `site-run-file' does not work")))
b7444d31 270
42a3e6fa
RS
271(defcustom mail-host-address nil
272 "*Name of this machine, for purposes of naming users."
273 :type '(choice (const nil) string)
274 :group 'mail)
c13fbb62 275
680ebfa6 276(defcustom user-mail-address (if command-line-processed
cee0c996
EZ
277 (or (getenv "EMAIL")
278 (concat (user-login-name) "@"
279 (or mail-host-address
280 (system-name))))
680ebfa6
RS
281 ;; Empty string means "not set yet".
282 "")
452e9090 283 "*Full mailing address of this user.
cee0c996
EZ
284This is initialized with environment variable `EMAIL' or, as a
285fallback, using `mail-host-address'. This is done after your
286init file is read, in case it sets `mail-host-address'."
42a3e6fa
RS
287 :type 'string
288 :group 'mail)
c10d1f06 289
42a3e6fa 290(defcustom auto-save-list-file-prefix
73f13e5a
GM
291 (cond ((eq system-type 'ms-dos)
292 ;; MS-DOS cannot have initial dot, and allows only 8.3 names
34b68a62 293 (concat user-emacs-directory "auto-save.list/_s"))
73f13e5a 294 (t
34b68a62 295 (concat user-emacs-directory "auto-save-list/.saves-")))
cdee38c3
KH
296 "Prefix for generating `auto-save-list-file-name'.
297This is used after reading your `.emacs' file to initialize
298`auto-save-list-file-name', by appending Emacs's pid and the system name,
299if you have not already set `auto-save-list-file-name' yourself.
73f13e5a 300Directories in the prefix will be created if necessary.
cdee38c3 301Set this to nil if you want to prevent `auto-save-list-file-name'
42a3e6fa 302from being initialized."
3671f444
KH
303 :type '(choice (const :tag "Don't record a session's auto save list" nil)
304 string)
42a3e6fa 305 :group 'auto-save)
2e05d063 306
6b61353c
KH
307(defvar emacs-quick-startup nil)
308
dc58296d
RS
309(defvar emacs-basic-display nil)
310
a726e0d1
JB
311(defvar init-file-debug nil)
312
5b309ebf
RS
313(defvar init-file-had-error nil
314 "Non-nil if there was an error loading the user's init file.")
52320897 315
95eada65
RS
316(defvar normal-top-level-add-subdirs-inode-list nil)
317
178b4542
JL
318(defvar no-blinking-cursor nil)
319
0a480f3d
RS
320(defvar default-frame-background-mode)
321
b693e375
RS
322(defvar pure-space-overflow nil
323 "Non-nil if building Emacs overflowed pure space.")
324
22a58255
CY
325(defvar pure-space-overflow-message "\
326Warning Warning!!! Pure space overflow !!!Warning Warning
327\(See the node Pure Storage in the Lisp manual for details.)\n")
328
5f5ba825 329(defvar tutorial-directory nil
f51a80af
GM
330 "Directory containing the Emacs TUTORIAL files.")
331
5f5ba825
GM
332;; Get correct value in a dumped, installed Emacs.
333(eval-at-startup
334 (setq tutorial-directory (file-name-as-directory
335 (expand-file-name "tutorials" data-directory))))
336
859e15c1 337(defun normal-top-level-add-subdirs-to-load-path ()
0412d833
RS
338 "Add all subdirectories of current directory to `load-path'.
339More precisely, this uses only the subdirectories whose names
f734a13e
DL
340start with letters or digits; it excludes any subdirectory named `RCS'
341or `CVS', and any subdirectory that contains a file named `.nosearch'."
f1180544 342 (let (dirs
95eada65 343 attrs
859e15c1
KH
344 (pending (list default-directory)))
345 ;; This loop does a breadth-first tree walk on DIR's subtree,
346 ;; putting each subdir into DIRS as its contents are examined.
347 (while pending
4e0a3971 348 (push (pop pending) dirs)
722a451d
EZ
349 (let* ((this-dir (car dirs))
350 (contents (directory-files this-dir))
351 (default-directory this-dir)
fcaf7de9
SM
352 (canonicalized (if (fboundp 'untranslated-canonical-name)
353 (untranslated-canonical-name this-dir))))
722a451d
EZ
354 ;; The Windows version doesn't report meaningful inode
355 ;; numbers, so use the canonicalized absolute file name of the
356 ;; directory instead.
357 (setq attrs (or canonicalized
358 (nthcdr 10 (file-attributes this-dir))))
95eada65 359 (unless (member attrs normal-top-level-add-subdirs-inode-list)
4e0a3971
SM
360 (push attrs normal-top-level-add-subdirs-inode-list)
361 (dolist (file contents)
c4a40544 362 ;; The lower-case variants of RCS and CVS are for DOS/Windows.
4e0a3971
SM
363 (unless (member file '("." ".." "RCS" "CVS" "rcs" "cvs"))
364 (when (and (string-match "\\`[[:alnum:]]" file)
9d1fb179
RS
365 ;; Avoid doing a `stat' when it isn't necessary
366 ;; because that can cause trouble when an NFS server
367 ;; is down.
4e0a3971
SM
368 (not (string-match "\\.elc?\\'" file))
369 (file-directory-p file))
370 (let ((expanded (expand-file-name file)))
95eada65
RS
371 (unless (file-exists-p (expand-file-name ".nosearch"
372 expanded))
4e0a3971 373 (setq pending (nconc pending (list expanded)))))))))))
bb15e81a 374 (normal-top-level-add-to-load-path (cdr (nreverse dirs)))))
859e15c1 375
537b6e4e
RS
376;; This function is called from a subdirs.el file.
377;; It assumes that default-directory is the directory
378;; in which the subdirs.el file exists,
379;; and it adds to load-path the subdirs of that directory
380;; as specified in DIRS. Normally the elements of DIRS are relative.
e87a7309 381(defun normal-top-level-add-to-load-path (dirs)
cd1c10f6
RS
382 (let ((tail load-path)
383 (thisdir (directory-file-name default-directory)))
384 (while (and tail
138e541f
GM
385 ;;Don't go all the way to the nil terminator.
386 (cdr tail)
cd1c10f6
RS
387 (not (equal thisdir (car tail)))
388 (not (and (memq system-type '(ms-dos windows-nt))
389 (equal (downcase thisdir) (downcase (car tail))))))
390 (setq tail (cdr tail)))
138e541f
GM
391 ;;Splice the new section in.
392 (when tail
393 (setcdr tail (append (mapcar 'expand-file-name dirs) (cdr tail))))))
e87a7309 394
a726e0d1
JB
395(defun normal-top-level ()
396 (if command-line-processed
397 (message "Back to top level.")
398 (setq command-line-processed t)
8d4c2221 399 (let ((dir default-directory))
fcaf7de9 400 (with-current-buffer "*Messages*"
a1562258
SM
401 ;; Make it easy to do like "tail -f".
402 (set (make-local-variable 'window-point-insertion-type) t)
403 ;; Give *Messages* the same default-directory as *scratch*,
404 ;; just to keep things predictable.
8d4c2221 405 (setq default-directory dir)))
9715399e
MR
406 ;; `user-full-name' is now known; reset its standard-value here.
407 (put 'user-full-name 'standard-value
408 (list (default-value 'user-full-name)))
80d71556
KH
409 ;; For root, preserve owner and group when editing files.
410 (if (equal (user-uid) 0)
411 (setq backup-by-copying-when-mismatch t))
e87a7309
RS
412 ;; Look in each dir in load-path for a subdirs.el file.
413 ;; If we find one, load it, which will add the appropriate subdirs
414 ;; of that dir into load-path,
357438eb
KH
415 ;; Look for a leim-list.el file too. Loading it will register
416 ;; available input methods.
b6666b5f
SM
417 (let ((tail load-path) dir)
418 (while tail
419 (setq dir (car tail))
420 (let ((default-directory dir))
421 (load (expand-file-name "subdirs.el") t t t))
422 (let ((default-directory dir))
423 (load (expand-file-name "leim-list.el") t t t))
424 ;; We don't use a dolist loop and we put this "setq-cdr" command at
425 ;; the end, because the subdirs.el files may add elements to the end
426 ;; of load-path and we want to take it into account.
427 (setq tail (cdr tail))))
fcaf7de9
SM
428 (unless (eq system-type 'vax-vms)
429 ;; If the PWD environment variable isn't accurate, delete it.
430 (let ((pwd (getenv "PWD")))
431 (and (stringp pwd)
432 ;; Use FOO/., so that if FOO is a symlink, file-attributes
433 ;; describes the directory linked to, not FOO itself.
434 (or (equal (file-attributes
435 (concat (file-name-as-directory pwd) "."))
436 (file-attributes
437 (concat (file-name-as-directory default-directory)
438 ".")))
439 (setq process-environment
440 (delete (concat "PWD=" pwd)
441 process-environment))))))
492878e4 442 (setq default-directory (abbreviate-file-name default-directory))
6f2c86fa
KH
443 (let ((menubar-bindings-done nil))
444 (unwind-protect
445 (command-line)
446 ;; Do this again, in case .emacs defined more abbreviations.
447 (setq default-directory (abbreviate-file-name default-directory))
b3c7c12c
RS
448 ;; Specify the file for recording all the auto save files of this session.
449 ;; This is used by recover-session.
cdee38c3
KH
450 (or auto-save-list-file-name
451 (and auto-save-list-file-prefix
452 (setq auto-save-list-file-name
1f7f78f1
RS
453 ;; Under MS-DOS our PID is almost always reused between
454 ;; Emacs invocations. We need something more unique.
4b33deaa
EZ
455 (cond ((eq system-type 'ms-dos)
456 ;; We are going to access the auto-save
457 ;; directory, so make sure it exists.
458 (make-directory
459 (file-name-directory auto-save-list-file-prefix)
460 t)
f1180544 461 (concat
4b33deaa
EZ
462 (make-temp-name
463 (expand-file-name
464 auto-save-list-file-prefix))
465 "~"))
466 (t
467 (expand-file-name
468 (format "%s%d-%s~"
469 auto-save-list-file-prefix
470 (emacs-pid)
471 (system-name))))))))
deebb0e9
RS
472 (unless inhibit-startup-hooks
473 (run-hooks 'emacs-startup-hook)
474 (and term-setup-hook
475 (run-hooks 'term-setup-hook)))
6a1e7d67
GM
476
477 ;; Don't do this if we failed to create the initial frame,
478 ;; for instance due to a dense colormap.
538ef02a
GM
479 (when (or frame-initial-frame
480 ;; If frame-initial-frame has no meaning, do this anyway.
2246281f 481 (not (and initial-window-system
538ef02a 482 (not noninteractive)
2246281f 483 (not (eq initial-window-system 'pc)))))
6a1e7d67
GM
484 ;; Modify the initial frame based on what .emacs puts into
485 ;; ...-frame-alist.
486 (if (fboundp 'frame-notice-user-settings)
487 (frame-notice-user-settings))
2666355c
KL
488 ;; Set the faces for the initial background mode even if
489 ;; frame-notice-user-settings didn't (such as on a tty).
490 ;; frame-set-background-mode is idempotent, so it won't
491 ;; cause any harm if it's already been done.
6a1e7d67 492 (if (fboundp 'frame-set-background-mode)
2666355c 493 (frame-set-background-mode (selected-frame))))
13ab33c4 494
6f2c86fa
KH
495 ;; Now we know the user's default font, so add it to the menu.
496 (if (fboundp 'font-menu-add-default)
497 (font-menu-add-default))
498 (and window-setup-hook
499 (run-hooks 'window-setup-hook))
500 (or menubar-bindings-done
66c24e1e 501 (if (display-popup-menus-p)
fdb88463
SM
502 (precompute-menubar-bindings)))))
503 ;; Subprocesses of Emacs do not have direct access to the terminal, so
504 ;; unless told otherwise they should only assume a dumb terminal.
505 ;; We are careful to do it late (after term-setup-hook), although the
506 ;; new multi-tty code does not use $TERM any more there anyway.
4bbfda32
SM
507 (setenv "TERM" "dumb")
508 ;; Remove DISPLAY from the process-environment as well. This allows
509 ;; `callproc.c' to give it a useful adaptive default which is either
510 ;; the value of the `display' frame-parameter or the DISPLAY value
511 ;; from initial-environment.
512 (let ((display (frame-parameter nil 'display)))
513 ;; Be careful which DISPLAY to remove from process-environment: follow
514 ;; the logic of `callproc.c'.
515 (if (stringp display) (setq display (concat "DISPLAY=" display))
516 (dolist (varval initial-environment)
517 (if (string-match "\\`DISPLAY=" varval)
518 (setq display varval))))
519 (when display
4bbfda32 520 (delete display process-environment)))))
6f2c86fa
KH
521
522;; Precompute the keyboard equivalents in the menu bar items.
523(defun precompute-menubar-bindings ()
365636dc
RS
524 (let ((submap (lookup-key global-map [menu-bar])))
525 (while submap
526 (and (consp (car submap))
527 (symbolp (car (car submap)))
528 (stringp (car-safe (cdr (car submap))))
529 (keymapp (cdr (cdr (car submap))))
a18042d7
RS
530 (progn
531 (x-popup-menu nil (cdr (cdr (car submap))))
532 (if purify-flag
533 (garbage-collect))))
365636dc 534 (setq submap (cdr submap))))
5b61c6a7 535 (setq define-key-rebound-commands t))
a726e0d1 536
c381f482
EZ
537;; Command-line options supported by tty's:
538(defconst tty-long-option-alist
8957d2bf
EZ
539 '(("--name" . "-name")
540 ("--title" . "-T")
541 ("--reverse-video" . "-reverse")
c381f482 542 ("--foreground-color" . "-fg")
8957d2bf
EZ
543 ("--background-color" . "-bg")
544 ("--color" . "-color")))
c381f482 545
b66b6aeb
GM
546(defconst tool-bar-images-pixel-height 24
547 "Height in pixels of images in the tool bar.")
548
3a55d3d0
JR
549(defvar tool-bar-originally-present nil
550 "Non-nil if tool-bars are present before user and site init files are read.")
551
e9cda827
KL
552(defvar handle-args-function-alist '((nil . tty-handle-args))
553 "Functions for processing window-system dependent command-line arguments.
554Window system startup files should add their own function to this
555alist, which should parse the command line arguments. Those
556pertaining to the window system should be processed and removed
557from the returned command line.")
558
559(defvar window-system-initialization-alist '((nil . ignore))
560 "Alist of window-system initialization functions.
561Window-system startup files should add their own initialization
562function to this list. The function should take no arguments,
563and initialize the window system environment to prepare for
2bb819d5 564opening the first frame (e.g. open a connection to an X server).")
e9cda827 565
bca8c7be 566;; Handle the X-like command-line arguments "-fg", "-bg", "-name", etc.
c381f482 567(defun tty-handle-args (args)
bca8c7be 568 (let (rest)
ae8bf5ab 569 (message "%S" args)
c381f482
EZ
570 (while (and args
571 (not (equal (car args) "--")))
bca8c7be
MS
572 (let* ((argi (pop args))
573 (orig-argi argi)
574 argval completion)
c381f482
EZ
575 ;; Check for long options with attached arguments
576 ;; and separate out the attached option argument into argval.
bca8c7be
MS
577 (when (string-match "^\\(--[^=]*\\)=" argi)
578 (setq argval (substring argi (match-end 0))
579 argi (match-string 1 argi)))
580 (when (string-match "^--" argi)
581 (setq completion (try-completion argi tty-long-option-alist))
c381f482
EZ
582 (if (eq completion t)
583 ;; Exact match for long option.
bca8c7be 584 (setq argi (cdr (assoc argi tty-long-option-alist)))
c381f482
EZ
585 (if (stringp completion)
586 (let ((elt (assoc completion tty-long-option-alist)))
587 ;; Check for abbreviated long option.
588 (or elt
bca8c7be
MS
589 (error "Option `%s' is ambiguous" argi))
590 (setq argi (cdr elt)))
c381f482 591 ;; Check for a short option.
bca8c7be
MS
592 (setq argval nil
593 argi orig-argi))))
594 (cond ((member argi '("-fg" "-foreground"))
595 (push (cons 'foreground-color (or argval (pop args)))
596 default-frame-alist))
597 ((member argi '("-bg" "-background"))
598 (push (cons 'background-color (or argval (pop args)))
599 default-frame-alist))
600 ((member argi '("-T" "-name"))
601 (unless argval (setq argval (pop args)))
602 (push (cons 'title
603 (if (stringp argval)
604 argval
605 (let ((case-fold-search t)
606 i)
607 (setq argval (invocation-name))
608
609 ;; Change any . or * characters in name to
610 ;; hyphens, so as to emulate behavior on X.
611 (while
612 (setq i (string-match "[.*]" argval))
613 (aset argval i ?-))
614 argval)))
615 default-frame-alist))
616 ((member argi '("-r" "-rv" "-reverse"))
617 (push '(reverse . t)
618 default-frame-alist))
619 ((equal argi "-color")
620 (unless argval (setq argval 8)) ; default --color means 8 ANSI colors
621 (push (cons 'tty-color-mode
622 (cond
623 ((numberp argval) argval)
624 ((string-match "-?[0-9]+" argval)
625 (string-to-number argval))
626 (t (intern argval))))
627 default-frame-alist))
628 (t
629 (push argi rest)))))
630 (nreverse rest)))
c381f482 631
aa360da1
GM
632(declare-function x-get-resource "frame.c"
633 (attribute class &optional component subclass))
634(declare-function tool-bar-mode "tool-bar" (&optional arg))
635
a726e0d1 636(defun command-line ()
5c9fad41 637 (setq before-init-time (current-time)
238cbdf8 638 command-line-default-directory default-directory)
09a1077c 639
849eedba 640 ;; Choose a reasonable location for temporary files.
a3194d03 641 (custom-reevaluate-setting 'temporary-file-directory)
51db08eb 642 (custom-reevaluate-setting 'small-temporary-file-directory)
a3194d03 643 (custom-reevaluate-setting 'auto-save-file-name-transforms)
849eedba 644
74f2ab06 645 ;; See if we should import version-control from the environment variable.
a726e0d1
JB
646 (let ((vc (getenv "VERSION_CONTROL")))
647 (cond ((eq vc nil)) ;don't do anything if not set
bca8c7be 648 ((member vc '("t" "numbered"))
a726e0d1 649 (setq version-control t))
bca8c7be 650 ((member vc '("nil" "existing"))
a726e0d1 651 (setq version-control nil))
bca8c7be 652 ((member vc '("never" "simple"))
a726e0d1
JB
653 (setq version-control 'never))))
654
79058860
JB
655 ;;! This has been commented out; I currently find the behavior when
656 ;;! split-window-keep-point is nil disturbing, but if I can get used
657 ;;! to it, then it would be better to eliminate the option.
658 ;;! ;; Choose a good default value for split-window-keep-point.
659 ;;! (setq split-window-keep-point (> baud-rate 2400))
f35fe3c6 660
d4308a4d
EZ
661 ;; Set the default strings to display in mode line for
662 ;; end-of-line formats that aren't native to this platform.
663 (cond
664 ((memq system-type '(ms-dos windows-nt emx))
bca8c7be
MS
665 (setq eol-mnemonic-unix "(Unix)"
666 eol-mnemonic-mac "(Mac)"))
bde61f8b
EZ
667 ;; Both Mac and Unix EOLs are now "native" on Mac OS so keep the
668 ;; abbreviated strings `/' and `:' set in coding.c for them.
669 ((eq system-type 'macos)
670 (setq eol-mnemonic-dos "(DOS)"))
bca8c7be
MS
671 (t ; this is for Unix/GNU/Linux systems
672 (setq eol-mnemonic-dos "(DOS)"
673 eol-mnemonic-mac "(Mac)")))
d4308a4d 674
2666355c 675 ;; Make sure window system's init file was loaded in loadup.el if using a window system.
1ed14cfd 676 (condition-case error
e9cda827
KL
677 (unless noninteractive
678 (if (and initial-window-system
679 (not (featurep
2666355c 680 (intern (concat (symbol-name initial-window-system) "-win")))))
e9cda827
KL
681 (error "Unsupported window system `%s'" initial-window-system))
682 ;; Process window-system specific command line parameters.
683 (setq command-line-args
684 (funcall (or (cdr (assq initial-window-system handle-args-function-alist))
685 (error "Unsupported window system `%s'" initial-window-system))
686 command-line-args))
687 ;; Initialize the window system. (Open connection, etc.)
688 (funcall (or (cdr (assq initial-window-system window-system-initialization-alist))
689 (error "Unsupported window system `%s'" initial-window-system))))
690 ;; If there was an error, print the error message and exit.
1ed14cfd 691 (error
2677ad61
RS
692 (princ
693 (if (eq (car error) 'error)
694 (apply 'concat (cdr error))
695 (if (memq 'file-error (get (car error) 'error-conditions))
696 (format "%s: %s"
bca8c7be
MS
697 (nth 1 error)
698 (mapconcat (lambda (obj) (prin1-to-string obj t))
699 (cdr (cdr error)) ", "))
2677ad61 700 (format "%s: %s"
bca8c7be
MS
701 (get (car error) 'error-message)
702 (mapconcat (lambda (obj) (prin1-to-string obj t))
703 (cdr error) ", "))))
2677ad61 704 'external-debugging-output)
27f5188c 705 (terpri 'external-debugging-output)
2246281f 706 (setq initial-window-system nil)
1ed14cfd 707 (kill-emacs)))
a726e0d1 708
640a9cdd
JR
709 (set-locale-environment nil)
710
735895f1
EZ
711 ;; Convert preloaded file names in load-history to absolute.
712 (let ((simple-file-name
3b61abfe
EZ
713 ;; Look for simple.el or simple.elc and use their directory
714 ;; as the place where all Lisp files live.
735895f1
EZ
715 (locate-file "simple" load-path (get-load-suffixes)))
716 lisp-dir)
717 ;; Don't abort if simple.el cannot be found, but print a warning.
718 (if (null simple-file-name)
719 (progn
720 (princ "Warning: Could not find simple.el nor simple.elc"
721 'external-debugging-output)
722 (terpri 'external-debugging-output))
723 (setq lisp-dir (file-truename (file-name-directory simple-file-name)))
724 (setq load-history
725 (mapcar (lambda (elt)
726 (if (and (stringp (car elt))
727 (not (file-name-absolute-p (car elt))))
728 (cons (concat lisp-dir
729 (car elt))
730 (cdr elt))
731 elt))
732 load-history))))
4b816985 733
c16eb7b0
RS
734 ;; Convert the arguments to Emacs internal representation.
735 (let ((args (cdr command-line-args)))
736 (while args
737 (setcar args
738 (decode-coding-string (car args) locale-coding-system t))
bca8c7be 739 (pop args)))
c16eb7b0 740
03e3c30a
JB
741 (let ((done nil)
742 (args (cdr command-line-args)))
743
a726e0d1
JB
744 ;; Figure out which user's init file to load,
745 ;; either from the environment or from the options.
746 (setq init-file-user (if noninteractive nil (user-login-name)))
747 ;; If user has not done su, use current $HOME to find .emacs.
bca8c7be
MS
748 (and init-file-user
749 (equal init-file-user (user-real-login-name))
a726e0d1 750 (setq init-file-user ""))
03e3c30a
JB
751
752 ;; Process the command-line args, and delete the arguments
753 ;; processed. This is consistent with the way main in emacs.c
754 ;; does things.
a726e0d1 755 (while (and (not done) args)
9ab281f0
JB
756 (let* ((longopts '(("--no-init-file") ("--no-site-file") ("--debug-init")
757 ("--user") ("--iconic") ("--icon-type") ("--quick")
758 ("--no-blinking-cursor") ("--basic-display")))
bca8c7be
MS
759 (argi (pop args))
760 (orig-argi argi)
761 argval)
452e9090 762 ;; Handle --OPTION=VALUE format.
bca8c7be 763 (when (string-match "^\\(--[^=]*\\)=" argi)
1b207153 764 (setq argval (substring argi (match-end 0))
bca8c7be 765 argi (match-string 1 argi)))
1b207153
CW
766 (unless (equal argi "--")
767 (let ((completion (try-completion argi longopts)))
768 (if (eq completion t)
769 (setq argi (substring argi 1))
770 (if (stringp completion)
771 (let ((elt (assoc completion longopts)))
772 (or elt
773 (error "Option `%s' is ambiguous" argi))
774 (setq argi (substring (car elt) 1)))
bca8c7be
MS
775 (setq argval nil
776 argi orig-argi)))))
a726e0d1 777 (cond
dc58296d 778 ((member argi '("-Q" "-quick"))
6b61353c
KH
779 (setq init-file-user nil
780 site-run-file nil
dc58296d
RS
781 emacs-quick-startup t))
782 ((member argi '("-D" "-basic-display"))
783 (setq no-blinking-cursor t
784 emacs-basic-display t)
6b61353c 785 (push '(vertical-scroll-bars . nil) initial-frame-alist))
4e0a3971
SM
786 ((member argi '("-q" "-no-init-file"))
787 (setq init-file-user nil))
788 ((member argi '("-u" "-user"))
bca8c7be 789 (setq init-file-user (or argval (pop args))
4e0a3971 790 argval nil))
bca8c7be 791 ((equal argi "-no-site-file")
4e0a3971 792 (setq site-run-file nil))
bca8c7be 793 ((equal argi "-debug-init")
4e0a3971 794 (setq init-file-debug t))
bca8c7be 795 ((equal argi "-iconic")
4e0a3971 796 (push '(visibility . icon) initial-frame-alist))
bca8c7be 797 ((member argi '("-icon-type" "-i" "-itype"))
4e0a3971 798 (push '(icon-type . t) default-frame-alist))
178b4542
JL
799 ((member argi '("-nbc" "-no-blinking-cursor"))
800 (setq no-blinking-cursor t))
4e0a3971 801 ;; Push the popped arg back on the list of arguments.
bca8c7be
MS
802 (t
803 (push argi args)
804 (setq done t)))
096b7031
KH
805 ;; Was argval set but not used?
806 (and argval
807 (error "Option `%s' doesn't allow an argument" argi))))
808
03e3c30a 809 ;; Re-attach the program name to the front of the arg list.
bca8c7be
MS
810 (and command-line-args
811 (setcdr command-line-args args)))
a726e0d1 812
ce98f555
RS
813 (run-hooks 'before-init-hook)
814
ed0fb1f1 815 ;; Under X Window, this creates the X frame and deletes the terminal frame.
14f16b9c
GM
816 (when (fboundp 'frame-initialize)
817 (frame-initialize))
818
ed0fb1f1 819 ;; Turn off blinking cursor if so specified in X resources. This is here
b08cb5a6 820 ;; only because all other settings of no-blinking-cursor are here.
ed0fb1f1
JD
821 (unless (or noninteractive
822 emacs-basic-display
823 (and (memq window-system '(x w32 mac))
824 (not (member (x-get-resource "cursorBlink" "CursorBlink")
825 '("off" "false")))))
826 (setq no-blinking-cursor t))
827
1fe0333f 828 ;; If frame was created with a menu bar, set menu-bar-mode on.
bca8c7be 829 (unless (or noninteractive
dc58296d 830 emacs-basic-display
2246281f 831 (and (memq initial-window-system '(x w32))
bca8c7be 832 (<= (frame-parameter nil 'menu-bar-lines) 0)))
8ab9589d 833 (menu-bar-mode 1))
c722566c 834
7d1eb6a4 835 ;; If frame was created with a tool bar, switch tool-bar-mode on.
bca8c7be 836 (unless (or noninteractive
dc58296d 837 emacs-basic-display
bca8c7be
MS
838 (not (display-graphic-p))
839 (<= (frame-parameter nil 'tool-bar-lines) 0))
b66b6aeb 840 (tool-bar-mode 1))
7d1eb6a4 841
3413f928 842 ;; Can't do this init in defcustom because the relevant variables
a3194d03
SM
843 ;; are not set.
844 (custom-reevaluate-setting 'blink-cursor-mode)
dc86f0a9 845 (custom-reevaluate-setting 'tooltip-mode)
c442c26d 846 (custom-reevaluate-setting 'global-font-lock-mode)
97a7dbee
YM
847 (custom-reevaluate-setting 'mouse-wheel-down-event)
848 (custom-reevaluate-setting 'mouse-wheel-up-event)
db441dba 849 (custom-reevaluate-setting 'file-name-shadow-mode)
a496452f 850 (custom-reevaluate-setting 'send-mail-function)
1e2d7573 851 (custom-reevaluate-setting 'focus-follows-mouse)
2cc11fd4 852 (custom-reevaluate-setting 'global-auto-composition-mode)
a2b84f35 853 (custom-reevaluate-setting 'transient-mark-mode)
6f8a4190 854 (custom-reevaluate-setting 'auto-encryption-mode)
3b73087e 855
30a2fded
KL
856 (normal-erase-is-backspace-setup-frame)
857
1592c1ef 858 ;; Register default TTY colors for the case the terminal hasn't a
79c3172f
KL
859 ;; terminal init file. We do this regardles of whether the terminal
860 ;; supports colors or not and regardless the current display type,
861 ;; since users can connect to color-capable terminals and also
862 ;; switch color support on or off in mid-session by setting the
863 ;; tty-color-mode frame parameter.
d448e982 864 (tty-register-default-colors)
1592c1ef 865
3a55d3d0
JR
866 ;; Record whether the tool-bar is present before the user and site
867 ;; init files are processed. frame-notice-user-settings uses this
868 ;; to determine if the tool-bar has been disabled by the init files,
869 ;; and the frame needs to be resized.
870 (when (fboundp 'frame-notice-user-settings)
871 (let ((tool-bar-lines (or (assq 'tool-bar-lines initial-frame-alist)
872 (assq 'tool-bar-lines default-frame-alist))))
873 (setq tool-bar-originally-present
bca8c7be
MS
874 (and tool-bar-lines
875 (cdr tool-bar-lines)
876 (not (eq 0 (cdr tool-bar-lines)))))))
3a55d3d0 877
752ef254
GM
878 (let ((old-scalable-fonts-allowed scalable-fonts-allowed)
879 (old-font-list-limit font-list-limit)
880 (old-face-ignored-fonts face-ignored-fonts))
881
752ef254
GM
882 ;; Run the site-start library if it exists. The point of this file is
883 ;; that it is run before .emacs. There is no point in doing this after
884 ;; .emacs; that is useless.
5f144784
GM
885 ;; Note that user-init-file is nil at this point. Code that might
886 ;; be loaded from site-run-file and wants to test if -q was given
887 ;; should check init-file-user instead, since that is already set.
888 ;; See cus-edit.el for an example.
f1180544 889 (if site-run-file
752ef254
GM
890 (load site-run-file t t))
891
892 ;; Sites should not disable this. Only individuals should disable
1cff9ad1
JL
893 ;; the startup screen.
894 (setq inhibit-startup-screen nil)
752ef254 895
f963ea40 896 ;; Warn for invalid user name.
ce98f555
RS
897 (when init-file-user
898 (if (string-match "[~/:\n]" init-file-user)
899 (display-warning 'initialization
900 (format "Invalid user name %s"
901 init-file-user)
902 :error)
1df1e49e
EZ
903 (if (file-directory-p (expand-file-name
904 ;; We don't support ~USER on MS-Windows except
905 ;; for the current user, and always load .emacs
906 ;; from the current user's home directory (see
907 ;; below). So always check "~", even if invoked
908 ;; with "-u USER", or if $USER or $LOGNAME are
909 ;; set to something different.
910 (if (eq system-type 'windows-nt)
911 "~"
912 (concat "~" init-file-user))))
ce98f555
RS
913 nil
914 (display-warning 'initialization
915 (format "User %s has no home directory"
916 init-file-user)
917 :error))))
f963ea40 918
752ef254
GM
919 ;; Load that user's init file, or the default one, or none.
920 (let (debug-on-error-from-init-file
921 debug-on-error-should-be-set
922 (debug-on-error-initial
923 (if (eq init-file-debug t) 'startup init-file-debug))
924 (orig-enable-multibyte default-enable-multibyte-characters))
925 (let ((debug-on-error debug-on-error-initial)
926 ;; This function actually reads the init files.
927 (inner
928 (function
929 (lambda ()
930 (if init-file-user
931 (let ((user-init-file-1
4e0a3971 932 (cond
752ef254
GM
933 ((eq system-type 'ms-dos)
934 (concat "~" init-file-user "/_emacs"))
935 ((eq system-type 'windows-nt)
fe7ecaf2 936 ;; Prefer .emacs on Windows.
752ef254
GM
937 (if (directory-files "~" nil "^\\.emacs\\(\\.elc?\\)?$")
938 "~/.emacs"
fe7ecaf2
JR
939 ;; Also support _emacs for compatibility.
940 (if (directory-files "~" nil "^_emacs\\(\\.elc?\\)?$")
941 "~/_emacs"
942 ;; But default to .emacs if _emacs does not exist.
943 "~/.emacs")))
f1180544 944 ((eq system-type 'vax-vms)
752ef254 945 "sys$login:.emacs")
4e0a3971 946 (t
752ef254
GM
947 (concat "~" init-file-user "/.emacs")))))
948 ;; This tells `load' to store the file name found
949 ;; into user-init-file.
950 (setq user-init-file t)
951 (load user-init-file-1 t t)
f1180544 952
a04d5983
RS
953 (when (eq user-init-file t)
954 ;; If we did not find ~/.emacs, try
c3ab3701 955 ;; ~/.emacs.d/init.el.
a04d5983
RS
956 (let ((otherfile
957 (expand-file-name
c3ab3701 958 "init"
a04d5983 959 (file-name-as-directory
c3ab3701 960 (concat "~" init-file-user "/.emacs.d")))))
a04d5983
RS
961 (load otherfile t t)
962
963 ;; If we did not find the user's init file,
964 ;; set user-init-file conclusively.
965 ;; Don't let it be set from default.el.
966 (when (eq user-init-file t)
967 (setq user-init-file user-init-file-1))))
f1180544 968
752ef254
GM
969 ;; If we loaded a compiled file, set
970 ;; `user-init-file' to the source version if that
971 ;; exists.
972 (when (and user-init-file
973 (equal (file-name-extension user-init-file)
974 "elc"))
975 (let* ((source (file-name-sans-extension user-init-file))
976 (alt (concat source ".el")))
977 (setq source (cond ((file-exists-p alt) alt)
978 ((file-exists-p source) source)
979 (t nil)))
980 (when source
981 (when (file-newer-than-file-p source user-init-file)
982 (message "Warning: %s is newer than %s"
983 source user-init-file)
984 (sit-for 1))
985 (setq user-init-file source))))
f1180544 986
bca8c7be 987 (unless inhibit-default-init
1cff9ad1 988 (let ((inhibit-startup-screen nil))
bca8c7be
MS
989 ;; Users are supposed to be told their rights.
990 ;; (Plus how to get help and how to undo.)
991 ;; Don't you dare turn this off for anyone
992 ;; except yourself.
993 (load "default" t t)))))))))
752ef254
GM
994 (if init-file-debug
995 ;; Do this without a condition-case if the user wants to debug.
996 (funcall inner)
997 (condition-case error
998 (progn
999 (funcall inner)
1000 (setq init-file-had-error nil))
1001 (error
1002 (let ((message-log-max nil))
a1562258 1003 (with-current-buffer (get-buffer-create "*Messages*")
752ef254
GM
1004 (insert "\n\n"
1005 (format "An error has occurred while loading `%s':\n\n"
1006 user-init-file)
1007 (format "%s%s%s"
1008 (get (car error) 'error-message)
1009 (if (cdr error) ": " "")
f5b6cffd 1010 (mapconcat (lambda (s) (prin1-to-string s t)) (cdr error) ", "))
752ef254 1011 "\n\n"
f5b6cffd
KS
1012 "To ensure normal operation, you should investigate and remove the\n"
1013 "cause of the error in your initialization file. Start Emacs with\n"
1014 "the `--debug-init' option to view a complete error backtrace.\n\n"))
752ef254
GM
1015 (message "Error in init file: %s%s%s"
1016 (get (car error) 'error-message)
1017 (if (cdr error) ": " "")
1018 (mapconcat 'prin1-to-string (cdr error) ", "))
f5b6cffd
KS
1019 (let ((pop-up-windows nil))
1020 (pop-to-buffer "*Messages*"))
752ef254 1021 (setq init-file-had-error t)))))
ac3186fd 1022
9c8b2150
RS
1023 (if (and deactivate-mark transient-mark-mode)
1024 (with-current-buffer (window-buffer)
1025 (deactivate-mark)))
1026
d5bd2558
GM
1027 ;; If the user has a file of abbrevs, read it (unless -batch).
1028 (when (and (not noninteractive)
1029 (file-exists-p abbrev-file-name)
2863a9be 1030 (file-readable-p abbrev-file-name))
ac3186fd
RS
1031 (quietly-read-abbrev-file abbrev-file-name))
1032
70b199c3
RS
1033 ;; If the abbrevs came entirely from the init file or the
1034 ;; abbrevs file, they do not need saving.
1035 (setq abbrevs-changed nil)
1036
752ef254
GM
1037 ;; If we can tell that the init file altered debug-on-error,
1038 ;; arrange to preserve the value that it set up.
1039 (or (eq debug-on-error debug-on-error-initial)
1040 (setq debug-on-error-should-be-set t
1041 debug-on-error-from-init-file debug-on-error)))
1042 (if debug-on-error-should-be-set
1043 (setq debug-on-error debug-on-error-from-init-file))
1044 (unless (or default-enable-multibyte-characters
1045 (eq orig-enable-multibyte default-enable-multibyte-characters))
1046 ;; Init file changed to unibyte. Reset existing multibyte
1047 ;; buffers (probably *scratch*, *Messages*, *Minibuff-0*).
1048 ;; Arguably this should only be done if they're free of
1049 ;; multibyte characters.
e18c18e8
JB
1050 (mapc (lambda (buffer)
1051 (with-current-buffer buffer
1052 (if enable-multibyte-characters
1053 (set-buffer-multibyte nil))))
1054 (buffer-list))
752ef254
GM
1055 ;; Also re-set the language environment in case it was
1056 ;; originally done before unibyte was set and is sensitive to
1057 ;; unibyte (display table, terminal coding system &c).
1058 (set-language-environment current-language-environment)))
f1180544 1059
752ef254 1060 ;; Do this here in case the init file sets mail-host-address.
5b88a2c5 1061 (if (equal user-mail-address "")
cee0c996
EZ
1062 (setq user-mail-address (or (getenv "EMAIL")
1063 (concat (user-login-name) "@"
1064 (or mail-host-address
1065 (system-name))))))
752ef254 1066
d60b49ac
DN
1067 ;; Originally face attributes were specified via
1068 ;; `font-lock-face-attributes'. Users then changed the default
1069 ;; face attributes by setting that variable. However, we try and
1070 ;; be back-compatible and respect its value if set except for
1071 ;; faces where M-x customize has been used to save changes for the
1072 ;; face.
1073 (when (boundp 'font-lock-face-attributes)
1074 (let ((face-attributes font-lock-face-attributes))
1075 (while face-attributes
1076 (let* ((face-attribute (pop face-attributes))
1077 (face (car face-attribute)))
1078 ;; Rustle up a `defface' SPEC from a
1079 ;; `font-lock-face-attributes' entry.
1080 (unless (get face 'saved-face)
1081 (let ((foreground (nth 1 face-attribute))
1082 (background (nth 2 face-attribute))
1083 (bold-p (nth 3 face-attribute))
1084 (italic-p (nth 4 face-attribute))
1085 (underline-p (nth 5 face-attribute))
1086 face-spec)
1087 (when foreground
1088 (setq face-spec (cons ':foreground (cons foreground face-spec))))
1089 (when background
1090 (setq face-spec (cons ':background (cons background face-spec))))
1091 (when bold-p
1092 (setq face-spec (append '(:weight bold) face-spec)))
1093 (when italic-p
1094 (setq face-spec (append '(:slant italic) face-spec)))
1095 (when underline-p
1096 (setq face-spec (append '(:underline t) face-spec)))
1097 (face-spec-set face (list (list t face-spec)) nil)))))))
1098
752ef254
GM
1099 ;; If parameter have been changed in the init file which influence
1100 ;; face realization, clear the face cache so that new faces will
1101 ;; be realized.
1102 (unless (and (eq scalable-fonts-allowed old-scalable-fonts-allowed)
1103 (eq font-list-limit old-font-list-limit)
1104 (eq face-ignored-fonts old-face-ignored-fonts))
1105 (clear-face-cache)))
f1180544 1106
5ab0e67e 1107 (setq after-init-time (current-time))
e3bd99f5
RM
1108 (run-hooks 'after-init-hook)
1109
1fc67c11 1110 ;; Decode all default-directory.
cea927ed
KH
1111 (if (and default-enable-multibyte-characters locale-coding-system)
1112 (save-excursion
1113 (dolist (elt (buffer-list))
1114 (set-buffer elt)
1115 (if default-directory
1116 (setq default-directory
1117 (decode-coding-string default-directory
1118 locale-coding-system t))))
1119 (setq command-line-default-directory
1120 (decode-coding-string command-line-default-directory
1121 locale-coding-system t))))
1122
a726e0d1
JB
1123 ;; If *scratch* exists and init file didn't change its mode, initialize it.
1124 (if (get-buffer "*scratch*")
694210c4 1125 (with-current-buffer "*scratch*"
a726e0d1 1126 (if (eq major-mode 'fundamental-mode)
26cdce23 1127 (funcall initial-major-mode))))
f1180544 1128
a726e0d1
JB
1129 ;; Load library for our terminal type.
1130 ;; User init file can set term-file-prefix to nil to prevent this.
bca8c7be 1131 (unless (or noninteractive
cd85984a
KL
1132 initial-window-system)
1133 (tty-run-terminal-initialization (selected-frame)))
a726e0d1 1134
5777a167
RS
1135 ;; Update the out-of-memory error message based on user's key bindings
1136 ;; for save-some-buffers.
1137 (setq memory-signal-data
1138 (list 'error
1139 (substitute-command-keys "Memory exhausted--use \\[save-some-buffers] then exit and restart Emacs")))
1140
03e3c30a 1141 ;; Process the remaining args.
a726e0d1
JB
1142 (command-line-1 (cdr command-line-args))
1143
1144 ;; If -batch, terminate after processing the command options.
ca57f55d
JD
1145 (if noninteractive (kill-emacs t))
1146
1147 ;; Run emacs-session-restore (session management) if started by
1148 ;; the session manager and we have a session manager connection.
bca8c7be
MS
1149 (if (and (boundp 'x-session-previous-id)
1150 (stringp x-session-previous-id))
a99df87d
RS
1151 (with-no-warnings
1152 (emacs-session-restore x-session-previous-id))))
a726e0d1 1153
46cfd295 1154(defcustom initial-scratch-message (purecopy "\
3b79c219
GM
1155;; This buffer is for notes you don't want to save, and for Lisp evaluation.
1156;; If you want to create a file, visit that file with C-x C-f,
1157;; then enter the text in that file's own buffer.
9fe3219e 1158
46cfd295 1159")
9fe3219e 1160 "Initial message displayed in *scratch* buffer at startup.
9bc6f805 1161If this is nil, no message will be displayed."
fa071a47
RS
1162 :type '(choice (text :tag "Message")
1163 (const :tag "none" nil))
1164 :group 'initialization)
9fe3219e 1165
ce9ded5d
GM
1166\f
1167;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1168;;; Fancy splash screen
1169;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1170
fad930b6 1171(defvar fancy-startup-text
422abbfd 1172 '((:face (variable-pitch (:foreground "red"))
22a58255 1173 "Welcome to "
6e0cfad3
JL
1174 :link ("GNU Emacs"
1175 (lambda (button) (browse-url "http://www.gnu.org/software/emacs/"))
1176 "Browse http://www.gnu.org/software/emacs/")
22a58255
CY
1177 ", one component of the "
1178 :link
1179 (lambda ()
1180 (if (eq system-type 'gnu/linux)
6e0cfad3
JL
1181 '("GNU/Linux"
1182 (lambda (button) (browse-url "http://www.gnu.org/gnu/linux-and-gnu.html"))
1183 "Browse http://www.gnu.org/gnu/linux-and-gnu.html")
92b2e7a3 1184 '("GNU" (lambda (button) (describe-gnu-project))
6e0cfad3 1185 "Display info on the GNU project")))
22a58255 1186 " operating system.\n"
34acda03
GM
1187 :face variable-pitch "To quit a partially entered command, type "
1188 :face default "Control-g"
1189 :face variable-pitch ".\n\n"
fad930b6 1190 :link ("Emacs Tutorial" (lambda (button) (help-with-tutorial)))
22a58255 1191 "\tLearn basic keystroke commands"
fad930b6
RS
1192 (lambda ()
1193 (let* ((en "TUTORIAL")
1194 (tut (or (get-language-info current-language-environment
1195 'tutorial)
1196 en))
1197 (title (with-temp-buffer
1198 (insert-file-contents
1199 (expand-file-name tut tutorial-directory)
1200 nil 0 256)
1201 (search-forward ".")
1202 (buffer-substring (point-min) (1- (point))))))
1203 ;; If there is a specific tutorial for the current language
1204 ;; environment and it is not English, append its title.
1205 (if (string= en tut)
1206 ""
1207 (concat " (" title ")"))))
1208 "\n"
1209 :face variable-pitch
fad930b6 1210 :link ("Emacs Guided Tour"
6e0cfad3
JL
1211 (lambda (button) (browse-url "http://www.gnu.org/software/emacs/tour/"))
1212 "Browse http://www.gnu.org/software/emacs/tour/")
fad930b6 1213 "\tOverview of Emacs features\n"
490a1ad6
JL
1214 :link ("View Emacs Manual" (lambda (button) (info-emacs-manual)))
1215 "\tView the Emacs manual using Info\n"
fad930b6
RS
1216 :link ("Absence of Warranty" (lambda (button) (describe-no-warranty)))
1217 "\tGNU Emacs comes with "
422abbfd 1218 :face (variable-pitch (:slant oblique))
fad930b6
RS
1219 "ABSOLUTELY NO WARRANTY\n"
1220 :face variable-pitch
1221 :link ("Copying Conditions" (lambda (button) (describe-copying)))
1222 "\tConditions for redistributing and changing Emacs\n"
22a58255
CY
1223 :link ("Ordering Manuals" (lambda (button) (view-order-manuals)))
1224 "\tPurchasing printed copies of manuals\n"
fad930b6 1225 "\n"))
ce9ded5d
GM
1226 "A list of texts to show in the middle part of splash screens.
1227Each element in the list should be a list of strings or pairs
1228`:face FACE', like `fancy-splash-insert' accepts them.")
1229
1d865f15 1230(defvar fancy-about-text
422abbfd 1231 '((:face (variable-pitch (:foreground "red"))
22a58255 1232 "This is "
6e0cfad3
JL
1233 :link ("GNU Emacs"
1234 (lambda (button) (browse-url "http://www.gnu.org/software/emacs/"))
1235 "Browse http://www.gnu.org/software/emacs/")
22a58255
CY
1236 ", one component of the "
1237 :link
1238 (lambda ()
1239 (if (eq system-type 'gnu/linux)
6e0cfad3
JL
1240 '("GNU/Linux"
1241 (lambda (button) (browse-url "http://www.gnu.org/gnu/linux-and-gnu.html"))
1242 "Browse http://www.gnu.org/gnu/linux-and-gnu.html")
92b2e7a3 1243 '("GNU" (lambda (button) (describe-gnu-project))
6e0cfad3 1244 "Display info on the GNU project.")))
22a58255 1245 " operating system.\n"
34acda03 1246 :face (lambda ()
422abbfd
RS
1247 (list 'variable-pitch
1248 (list :foreground
1249 (if (eq (frame-parameter nil 'background-mode) 'dark)
1250 "cyan" "darkblue"))))
22a58255
CY
1251 "\n"
1252 (lambda () (emacs-version))
1253 "\n"
245a7698 1254 :face (variable-pitch (:height 0.8))
22a58255
CY
1255 (lambda () emacs-copyright)
1256 "\n\n"
1257 :face variable-pitch
490a1ad6
JL
1258 :link ("Authors"
1259 (lambda (button)
1260 (view-file (expand-file-name "AUTHORS" data-directory))
1261 (goto-char (point-min))))
1262 "\tMany people have contributed code included in GNU Emacs\n"
1263 :link ("Contributing"
1264 (lambda (button)
1265 (view-file (expand-file-name "CONTRIBUTE" data-directory))
1266 (goto-char (point-min))))
1267 "\tHow to contribute improvements to Emacs\n"
1268 "\n"
92b2e7a3 1269 :link ("GNU and Freedom" (lambda (button) (describe-gnu-project)))
fad930b6
RS
1270 "\tWhy we developed GNU Emacs, and the GNU operating system\n"
1271 :link ("Absence of Warranty" (lambda (button) (describe-no-warranty)))
1272 "\tGNU Emacs comes with "
422abbfd 1273 :face (variable-pitch (:slant oblique))
fad930b6
RS
1274 "ABSOLUTELY NO WARRANTY\n"
1275 :face variable-pitch
1276 :link ("Copying Conditions" (lambda (button) (describe-copying)))
1277 "\tConditions for redistributing and changing Emacs\n"
1278 :link ("Getting New Versions" (lambda (button) (describe-distribution)))
1279 "\tHow to obtain the latest version of Emacs\n"
1cff9ad1
JL
1280 :link ("Ordering Manuals" (lambda (button) (view-order-manuals)))
1281 "\tBuying printed manuals from the FSF\n"
fad930b6
RS
1282 "\n"
1283 :link ("Emacs Tutorial" (lambda (button) (help-with-tutorial)))
1284 "\tLearn basic Emacs keystroke commands"
1285 (lambda ()
1286 (let* ((en "TUTORIAL")
1287 (tut (or (get-language-info current-language-environment
1288 'tutorial)
1289 en))
1290 (title (with-temp-buffer
1291 (insert-file-contents
1292 (expand-file-name tut tutorial-directory)
1293 nil 0 256)
1294 (search-forward ".")
1295 (buffer-substring (point-min) (1- (point))))))
1296 ;; If there is a specific tutorial for the current language
1297 ;; environment and it is not English, append its title.
1298 (if (string= en tut)
1299 ""
1300 (concat " (" title ")"))))
1301 "\n"
1302 :link ("Emacs Guided Tour"
6e0cfad3
JL
1303 (lambda (button) (browse-url "http://www.gnu.org/software/emacs/tour/"))
1304 "Browse http://www.gnu.org/software/emacs/tour/")
fad930b6
RS
1305 "\tSee an overview of the many facilities of GNU Emacs"
1306 ))
1d865f15
JL
1307 "A list of texts to show in the middle part of the About screen.
1308Each element in the list should be a list of strings or pairs
1309`:face FACE', like `fancy-splash-insert' accepts them.")
1310
ce9ded5d 1311
fb275c02 1312(defgroup fancy-splash-screen ()
a01bb1db 1313 "Fancy splash screen when Emacs starts."
fb275c02 1314 :version "21.1"
a01bb1db
GM
1315 :group 'initialization)
1316
8ad8561b
GM
1317(defcustom fancy-splash-image nil
1318 "*The image to show in the splash screens, or nil for defaults."
a01bb1db 1319 :group 'fancy-splash-screen
8ad8561b
GM
1320 :type '(choice (const :tag "Default" nil)
1321 (file :tag "File")))
ce9ded5d
GM
1322
1323
aeb6faec 1324(defvar splash-screen-keymap
26cdce23
JL
1325 (let ((map (make-sparse-keymap)))
1326 (suppress-keymap map)
1327 (set-keymap-parent map button-buffer-map)
aeb6faec
JL
1328 (define-key map "\C-?" 'scroll-down)
1329 (define-key map " " 'scroll-up)
1330 (define-key map "q" 'exit-splash-screen)
26cdce23
JL
1331 map)
1332 "Keymap for splash screen buffer.")
1333
f645586f
GM
1334;; These are temporary storage areas for the splash screen display.
1335
ce9ded5d
GM
1336(defun fancy-splash-insert (&rest args)
1337 "Insert text into the current buffer, with faces.
22a58255
CY
1338Arguments from ARGS should be either strings; functions called
1339with no args that return a string; pairs `:face FACE', where FACE
1340is a face specification usable with `put-text-property'; or pairs
1341`:link LINK' where LINK is a list of arguments to pass to
6e0cfad3
JL
1342`insert-button', of the form (LABEL ACTION [HELP-ECHO]), which
1343specifies the button's label, `action' property and help-echo string.
1344FACE and LINK can also be functions, which are evaluated to obtain
1345a face or button specification."
ce9ded5d
GM
1346 (let ((current-face nil))
1347 (while args
26cdce23 1348 (cond ((eq (car args) :face)
22a58255
CY
1349 (setq args (cdr args) current-face (car args))
1350 (if (functionp current-face)
1351 (setq current-face (funcall current-face))))
26cdce23
JL
1352 ((eq (car args) :link)
1353 (setq args (cdr args))
1354 (let ((spec (car args)))
22a58255
CY
1355 (if (functionp spec)
1356 (setq spec (funcall spec)))
26cdce23
JL
1357 (insert-button (car spec)
1358 'face (list 'link current-face)
1359 'action (cadr spec)
6e0cfad3
JL
1360 'help-echo (concat "mouse-2, RET: "
1361 (or (nth 2 spec)
1362 "Follow this link"))
26cdce23
JL
1363 'follow-link t)))
1364 (t (insert (propertize (let ((it (car args)))
1365 (if (functionp it)
1366 (funcall it)
1367 it))
1368 'face current-face
d4a629de 1369 'help-echo (startup-echo-area-message)))))
ce9ded5d
GM
1370 (setq args (cdr args)))))
1371
aa360da1 1372(declare-function image-size "image.c" (spec &optional pixels frame))
ce9ded5d
GM
1373
1374(defun fancy-splash-head ()
1375 "Insert the head part of the splash screen into the current buffer."
51e8cfdd
GM
1376 (let* ((image-file (cond ((stringp fancy-splash-image)
1377 fancy-splash-image)
68548459
JB
1378 ((display-color-p)
1379 (cond ((image-type-available-p 'svg)
1380 "splash.svg")
1381 ((image-type-available-p 'png)
1382 "splash.png")
1383 ((image-type-available-p 'xpm)
1384 (if (and (fboundp 'x-display-planes)
1385 (= (funcall 'x-display-planes) 8))
1386 "splash8.xpm"
1387 "splash.xpm"))))
1388 (t "splash.pbm")))
51e8cfdd 1389 (img (create-image image-file))
ce9ded5d
GM
1390 (image-width (and img (car (image-size img))))
1391 (window-width (window-width (selected-window))))
1392 (when img
1393 (when (> window-width image-width)
f645586f 1394 ;; Center the image in the window.
6b61353c
KH
1395 (insert (propertize " " 'display
1396 `(space :align-to (+ center (-0.5 . ,img)))))
f645586f 1397
e7f3afa9
MB
1398 ;; Change the color of the XPM version of the splash image
1399 ;; so that it is visible with a dark frame background.
1400 (when (and (memq 'xpm img)
1401 (eq (frame-parameter nil 'background-mode) 'dark))
1402 (setq img (append img '(:color-symbols (("#000000" . "gray30"))))))
1403
aeb6faec
JL
1404 ;; Insert the image with a help-echo and a link.
1405 (make-button (prog1 (point) (insert-image img)) (point)
1406 'face 'default
6e0cfad3 1407 'help-echo "mouse-2, RET: Browse http://www.gnu.org/"
aeb6faec
JL
1408 'action (lambda (button) (browse-url "http://www.gnu.org/"))
1409 'follow-link t)
22a58255
CY
1410 (insert "\n\n")))))
1411
1cff9ad1 1412(defun fancy-startup-tail (&optional concise)
ce9ded5d 1413 "Insert the tail part of the splash screen into the current buffer."
95fadcca
GM
1414 (let ((fg (if (eq (frame-parameter nil 'background-mode) 'dark)
1415 "cyan" "darkblue")))
1cff9ad1
JL
1416 (unless concise
1417 (fancy-splash-insert
1418 :face 'variable-pitch
1419 "\nTo start... "
1420 :link '("Open a File"
6e0cfad3
JL
1421 (lambda (button) (call-interactively 'find-file))
1422 "Specify a new file's name, to edit the file")
1cff9ad1
JL
1423 " "
1424 :link '("Open Home Directory"
6e0cfad3
JL
1425 (lambda (button) (dired "~"))
1426 "Open your home directory, to operate on its files")
1cff9ad1
JL
1427 " "
1428 :link '("Customize Startup"
6e0cfad3
JL
1429 (lambda (button) (customize-group 'initialization))
1430 "Change initialization settings including this screen")
1cff9ad1 1431 "\n"))
422abbfd 1432 (fancy-splash-insert :face `(variable-pitch (:foreground ,fg))
95fadcca
GM
1433 "\nThis is "
1434 (emacs-version)
1435 "\n"
245a7698 1436 :face '(variable-pitch (:height 0.8))
22a58255
CY
1437 emacs-copyright
1438 "\n")
ed638cc9
RS
1439 (and auto-save-list-file-prefix
1440 ;; Don't signal an error if the
1441 ;; directory for auto-save-list files
1442 ;; does not yet exist.
1443 (file-directory-p (file-name-directory
1444 auto-save-list-file-prefix))
1445 (directory-files
1446 (file-name-directory auto-save-list-file-prefix)
1447 nil
1448 (concat "\\`"
1449 (regexp-quote (file-name-nondirectory
1450 auto-save-list-file-prefix)))
1451 t)
422abbfd 1452 (fancy-splash-insert :face '(variable-pitch (:foreground "red"))
1d865f15 1453 "\nIf an Emacs session crashed recently, "
5b309ebf
RS
1454 "type "
1455 :face '(fixed-pitch :foreground "red")
1456 "Meta-x recover-session RET"
422abbfd 1457 :face '(variable-pitch (:foreground "red"))
5b309ebf 1458 "\nto recover"
22a58255
CY
1459 " the files you were editing."))
1460
1cff9ad1
JL
1461 (when concise
1462 (fancy-splash-insert
3d9d55e6 1463 :face 'variable-pitch "\n"
09723615
JL
1464 :link '("Dismiss this startup screen"
1465 (lambda (button)
1466 (when startup-screen-inhibit-startup-screen
1467 (customize-set-variable 'inhibit-startup-screen t)
1468 (customize-mark-to-save 'inhibit-startup-screen)
1469 (custom-save-all))
1470 (let ((w (get-buffer-window "*GNU Emacs*")))
1471 (and w (not (one-window-p)) (delete-window w)))
1472 (kill-buffer "*GNU Emacs*")))
1cff9ad1
JL
1473 " ")
1474 (when (or user-init-file custom-file)
1475 (let ((checked (create-image "\300\300\141\143\067\076\034\030"
22a58255 1476 'xbm t :width 8 :height 8 :background "grey75"
1cff9ad1
JL
1477 :foreground "black" :relief -2 :ascent 'center))
1478 (unchecked (create-image (make-string 8 0)
1479 'xbm t :width 8 :height 8 :background "grey75"
1480 :foreground "black" :relief -2 :ascent 'center)))
1481 (insert-button
1482 " " :on-glyph checked :off-glyph unchecked 'checked nil
1483 'display unchecked 'follow-link t
1484 'action (lambda (button)
1485 (if (overlay-get button 'checked)
1486 (progn (overlay-put button 'checked nil)
1487 (overlay-put button 'display (overlay-get button :off-glyph))
1488 (setq startup-screen-inhibit-startup-screen nil))
1489 (overlay-put button 'checked t)
1490 (overlay-put button 'display (overlay-get button :on-glyph))
1491 (setq startup-screen-inhibit-startup-screen t)))))
422abbfd 1492 (fancy-splash-insert :face '(variable-pitch (:height 0.9))
09723615 1493 " Never show it again.")))))
f645586f 1494
aeb6faec 1495(defun exit-splash-screen ()
26cdce23 1496 "Stop displaying the splash screen buffer."
f645586f 1497 (interactive)
1d865f15
JL
1498 (quit-window t))
1499
1cff9ad1 1500(defun fancy-startup-screen (&optional concise)
22a58255 1501 "Display fancy startup screen.
1cff9ad1
JL
1502If CONCISE is non-nil, display a concise version of the
1503splash screen in another window."
3d9d55e6 1504 (let ((splash-buffer (get-buffer-create "*GNU Emacs*")))
09723615 1505 (with-current-buffer splash-buffer
3d9d55e6
CY
1506 (let ((inhibit-read-only t))
1507 (erase-buffer)
4222d0a4 1508 (setq default-directory command-line-default-directory)
3d9d55e6
CY
1509 (make-local-variable 'startup-screen-inhibit-startup-screen)
1510 (if pure-space-overflow
1511 (insert pure-space-overflow-message))
1512 (unless concise
1513 (fancy-splash-head))
1514 (dolist (text fancy-startup-text)
1515 (apply #'fancy-splash-insert text)
1516 (insert "\n"))
1517 (skip-chars-backward "\n")
1518 (delete-region (point) (point-max))
1519 (insert "\n")
1520 (fancy-startup-tail concise))
1521 (use-local-map splash-screen-keymap)
1522 (setq tab-width 22
1523 buffer-read-only t)
1524 (set-buffer-modified-p nil)
1525 (if (and view-read-only (not view-mode))
1526 (view-mode-enter nil 'kill-buffer))
09723615
JL
1527 (goto-char (point-min))
1528 (forward-line (if concise 2 4)))
3d9d55e6
CY
1529 (if concise
1530 (progn
1531 (display-buffer splash-buffer)
1532 ;; If the splash screen is in a split window, fit it.
1533 (let ((window (get-buffer-window splash-buffer t)))
1534 (or (null window)
1535 (eq window (selected-window))
1536 (eq window (next-window window))
1537 (fit-window-to-buffer window))))
1538 (switch-to-buffer splash-buffer))))
22a58255
CY
1539
1540(defun fancy-about-screen ()
1541 "Display fancy About screen."
1542 (let ((frame (fancy-splash-frame)))
1543 (save-selected-window
1544 (select-frame frame)
1545 (switch-to-buffer "*About GNU Emacs*")
1546 (setq buffer-undo-list t
1547 mode-line-format (propertize "---- %b %-"
1548 'face 'mode-line-buffer-id))
1549 (let ((inhibit-read-only t))
1550 (erase-buffer)
1551 (if pure-space-overflow
1552 (insert pure-space-overflow-message))
1553 (fancy-splash-head)
1554 (dolist (text fancy-about-text)
1555 (apply #'fancy-splash-insert text)
1556 (insert "\n"))
22a58255
CY
1557 (set-buffer-modified-p nil)
1558 (goto-char (point-min))
1559 (force-mode-line-update))
aeb6faec 1560 (use-local-map splash-screen-keymap)
cb3fe757 1561 (setq tab-width 22)
22a58255 1562 (message "%s" (startup-echo-area-message))
08326816 1563 (setq buffer-read-only t)
09723615
JL
1564 (goto-char (point-min))
1565 (forward-line 3))))
6b20fb8e
RS
1566
1567(defun fancy-splash-frame ()
1568 "Return the frame to use for the fancy splash screen.
1569Returning non-nil does not mean we should necessarily
1570use the fancy splash screen, but if we do use it,
1571we put it on this frame."
1572 (let (chosen-frame)
3a321ddb 1573 (dolist (frame (append (frame-list) (list (selected-frame))))
6b20fb8e
RS
1574 (if (and (frame-visible-p frame)
1575 (not (window-minibuffer-p (frame-selected-window frame))))
1576 (setq chosen-frame frame)))
1577 chosen-frame))
f645586f 1578
285991dc
GM
1579(defun use-fancy-splash-screens-p ()
1580 "Return t if fancy splash screens should be used."
6b61353c
KH
1581 (when (and (display-graphic-p)
1582 (or (and (display-color-p)
285991dc 1583 (image-type-available-p 'xpm))
6b61353c 1584 (image-type-available-p 'pbm)))
16d2fae9
JPW
1585 (let ((frame (fancy-splash-frame)))
1586 (when frame
1587 (let* ((img (create-image (or fancy-splash-image
1588 (if (and (display-color-p)
1589 (image-type-available-p 'xpm))
1590 "splash.xpm" "splash.pbm"))))
a68ccb40
RS
1591 (image-height (and img (cdr (image-size img nil frame))))
1592 ;; We test frame-height so that, if the frame is split
1593 ;; by displaying a warning, that doesn't cause the normal
1594 ;; splash screen to be used.
1595 (frame-height (1- (frame-height frame))))
1596 (> frame-height (+ image-height 19)))))))
285991dc
GM
1597
1598
79b1c79c 1599(defun normal-splash-screen (&optional startup concise)
1d865f15
JL
1600 "Display non-graphic splash screen.
1601If optional argument STARTUP is non-nil, display the startup screen
79b1c79c
JL
1602after Emacs starts. If STARTUP is nil, display the About screen.
1603If CONCISE is non-nil, display a concise version of the
1604splash screen in another window."
1605 (let ((splash-buffer (get-buffer-create "*About GNU Emacs*")))
1606 (with-current-buffer splash-buffer
1d865f15
JL
1607 (setq buffer-read-only nil)
1608 (erase-buffer)
4222d0a4 1609 (setq default-directory command-line-default-directory)
1d865f15
JL
1610 (set (make-local-variable 'tab-width) 8)
1611 (if (not startup)
1612 (set (make-local-variable 'mode-line-format)
1613 (propertize "---- %b %-" 'face 'mode-line-buffer-id)))
1614
1615 (if pure-space-overflow
22a58255 1616 (insert pure-space-overflow-message))
260a5020 1617
1d865f15
JL
1618 ;; The convention for this piece of code is that
1619 ;; each piece of output starts with one or two newlines
1620 ;; and does not end with any newlines.
22a58255 1621 (insert (if startup "Welcome to GNU Emacs" "This is GNU Emacs"))
1d865f15
JL
1622 (insert
1623 (if (eq system-type 'gnu/linux)
1624 ", one component of the GNU/Linux operating system.\n"
1625 ", a part of the GNU operating system.\n"))
1626
1627 (if startup
1628 (if (display-mouse-p)
1629 ;; The user can use the mouse to activate menus
1630 ;; so give help in terms of menu items.
fad930b6 1631 (normal-mouse-startup-screen)
c61453ea 1632
e349ae3b 1633 ;; No mouse menus, so give help using kbd commands.
fad930b6 1634 (normal-no-mouse-startup-screen))
e349ae3b 1635
fad930b6 1636 (normal-about-screen))
1d865f15
JL
1637
1638 ;; The rest of the startup screen is the same on all
1639 ;; kinds of terminals.
1640
1641 ;; Give information on recovering, if there was a crash.
1642 (and startup
1643 auto-save-list-file-prefix
1644 ;; Don't signal an error if the
1645 ;; directory for auto-save-list files
1646 ;; does not yet exist.
1647 (file-directory-p (file-name-directory
1648 auto-save-list-file-prefix))
1649 (directory-files
1650 (file-name-directory auto-save-list-file-prefix)
1651 nil
1652 (concat "\\`"
1653 (regexp-quote (file-name-nondirectory
1654 auto-save-list-file-prefix)))
1655 t)
1656 (insert "\n\nIf an Emacs session crashed recently, "
1657 "type Meta-x recover-session RET\nto recover"
1658 " the files you were editing.\n"))
26cdce23 1659
1d865f15 1660 (use-local-map splash-screen-keymap)
e349ae3b 1661
1d865f15
JL
1662 ;; Display the input that we set up in the buffer.
1663 (set-buffer-modified-p nil)
1664 (setq buffer-read-only t)
1665 (if (and view-read-only (not view-mode))
1666 (view-mode-enter nil 'kill-buffer))
1d865f15 1667 (if startup (rename-buffer "*GNU Emacs*" t))
79b1c79c
JL
1668 (goto-char (point-min)))
1669 (if concise
1670 (display-buffer splash-buffer)
1671 (switch-to-buffer splash-buffer))))
82e736c1 1672
fad930b6
RS
1673(defun normal-mouse-startup-screen ()
1674 ;; The user can use the mouse to activate menus
1675 ;; so give help in terms of menu items.
1676 (insert "\
09723615 1677To follow a link, click Mouse-1 on it, or move to it and type RET.
26cdce23
JL
1678To quit a partially entered command, type Control-g.\n")
1679
fad930b6
RS
1680 (insert "\nImportant Help menu items:\n")
1681 (insert-button "Emacs Tutorial"
1682 'action (lambda (button) (help-with-tutorial))
1683 'follow-link t)
1684 (insert "\t\tLearn basic Emacs keystroke commands\n")
1685 (insert-button "Read the Emacs Manual"
1686 'action (lambda (button) (info-emacs-manual))
1687 'follow-link t)
1688 (insert "\tView the Emacs manual using Info\n")
1689 (insert-button "\(Non)Warranty"
1690 'action (lambda (button) (describe-no-warranty))
1691 'follow-link t)
1692 (insert "\t\tGNU Emacs comes with ABSOLUTELY NO WARRANTY\n")
1693 (insert-button "Copying Conditions"
1694 'action (lambda (button) (describe-copying))
1695 'follow-link t)
1696 (insert "\tConditions for redistributing and changing Emacs\n")
1697 (insert-button "More Manuals / Ordering Manuals"
1698 'action (lambda (button) (view-order-manuals))
1699 'follow-link t)
1700 (insert " How to order printed manuals from the FSF\n")
1701
1702 (insert "\nUseful tasks:\n")
1703 (insert-button "Visit New File"
1704 'action (lambda (button) (call-interactively 'find-file))
1705 'follow-link t)
1706 (insert "\t\tSpecify a new file's name, to edit the file\n")
1707 (insert-button "Open Home Directory"
1708 'action (lambda (button) (dired "~"))
1709 'follow-link t)
1710 (insert "\tOpen your home directory, to operate on its files\n")
1711 (insert-button "Customize Startup"
1712 'action (lambda (button) (customize-group 'initialization))
1713 'follow-link t)
1714 (insert "\tChange initialization settings including this screen\n")
1715
1716 (insert "\n" (emacs-version)
1717 "\n" emacs-copyright))
1718
1719;; No mouse menus, so give help using kbd commands.
1720(defun normal-no-mouse-startup-screen ()
1721
1722 ;; If keys have their default meanings,
1723 ;; use precomputed string to save lots of time.
1724 (if (and (eq (key-binding "\C-h") 'help-command)
1725 (eq (key-binding "\C-xu") 'advertised-undo)
1726 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-terminal)
1727 (eq (key-binding "\C-ht") 'help-with-tutorial)
1728 (eq (key-binding "\C-hi") 'info)
1729 (eq (key-binding "\C-hr") 'info-emacs-manual)
1730 (eq (key-binding "\C-h\C-n") 'view-emacs-news))
1731 (progn
1732 (insert "
1733Get help\t C-h (Hold down CTRL and press h)
cd6ab24f 1734")
fad930b6
RS
1735 (insert-button "Emacs manual"
1736 'action (lambda (button) (info-emacs-manual))
1737 'follow-link t)
1738 (insert " C-h r\t")
1739 (insert-button "Browse manuals"
1740 'action (lambda (button) (Info-directory))
1741 'follow-link t)
1742 (insert "\t C-h i
cd6ab24f 1743")
fad930b6
RS
1744 (insert-button "Emacs tutorial"
1745 'action (lambda (button) (help-with-tutorial))
1746 'follow-link t)
1747 (insert " C-h t\tUndo changes\t C-x u
cd6ab24f 1748")
fad930b6
RS
1749 (insert-button "Buy manuals"
1750 'action (lambda (button) (view-order-manuals))
1751 'follow-link t)
1752 (insert "\t C-h C-m\tExit Emacs\t C-x C-c"))
5777a167 1753
fad930b6
RS
1754 (insert (format "
1755Get help\t %s
cd6ab24f 1756"
fad930b6
RS
1757 (let ((where (where-is-internal
1758 'help-command nil t)))
1759 (if where
1760 (key-description where)
1761 "M-x help"))))
1762 (insert-button "Emacs manual"
1763 'action (lambda (button) (info-emacs-manual))
1764 'follow-link t)
1765 (insert (substitute-command-keys"\t \\[info-emacs-manual]\t"))
1766 (insert-button "Browse manuals"
1767 'action (lambda (button) (Info-directory))
1768 'follow-link t)
1769 (insert (substitute-command-keys "\t \\[info]
cd6ab24f 1770"))
fad930b6
RS
1771 (insert-button "Emacs tutorial"
1772 'action (lambda (button) (help-with-tutorial))
1773 'follow-link t)
1774 (insert (substitute-command-keys
1775 "\t \\[help-with-tutorial]\tUndo changes\t \\[advertised-undo]
cd6ab24f 1776"))
fad930b6
RS
1777 (insert-button "Buy manuals"
1778 'action (lambda (button) (view-order-manuals))
1779 'follow-link t)
1780 (insert (substitute-command-keys
1781 "\t \\[view-order-manuals]\tExit Emacs\t \\[save-buffers-kill-terminal]")))
1782
1783 ;; Say how to use the menu bar with the keyboard.
1784 (insert "\n")
1785 (insert-button "Activate menubar"
1786 'action (lambda (button) (tmm-menubar))
1787 'follow-link t)
1788 (if (and (eq (key-binding "\M-`") 'tmm-menubar)
1789 (eq (key-binding [f10]) 'tmm-menubar))
1790 (insert " F10 or ESC ` or M-`")
1791 (insert (substitute-command-keys " \\[tmm-menubar]")))
1792
1793 ;; Many users seem to have problems with these.
1794 (insert "
c61453ea
PJ
1795\(`C-' means use the CTRL key. `M-' means use the Meta (or Alt) key.
1796If you have no Meta key, you may instead type ESC followed by the character.)")
1797
fad930b6
RS
1798 ;; Insert links to useful tasks
1799 (insert "\nUseful tasks:\n")
1800
1801 (insert-button "Visit New File"
1802 'action (lambda (button) (call-interactively 'find-file))
1803 'follow-link t)
1804 (insert "\t\t\t")
1805 (insert-button "Open Home Directory"
1806 'action (lambda (button) (dired "~"))
1807 'follow-link t)
1808 (insert "\n")
1809
1810 (insert-button "Customize Startup"
1811 'action (lambda (button) (customize-group 'initialization))
1812 'follow-link t)
1813 (insert "\t\t")
1814 (insert-button "Open *scratch* buffer"
1815 'action (lambda (button) (switch-to-buffer
1816 (get-buffer-create "*scratch*")))
1817 'follow-link t)
1818 (insert "\n")
1819 (insert "\n" (emacs-version) "\n" emacs-copyright "\n")
1820
1821 (if (and (eq (key-binding "\C-h\C-c") 'describe-copying)
1822 (eq (key-binding "\C-h\C-d") 'describe-distribution)
1823 (eq (key-binding "\C-h\C-w") 'describe-no-warranty))
1824 (progn
1825 (insert
490a1ad6 1826 "
cd6ab24f 1827GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for ")
fad930b6
RS
1828 (insert-button "full details"
1829 'action (lambda (button) (describe-no-warranty))
1830 'follow-link t)
1831 (insert ".
c61453ea 1832Emacs is Free Software--Free as in Freedom--so you can redistribute copies
cd6ab24f 1833of Emacs and modify it; type C-h C-c to see ")
fad930b6
RS
1834 (insert-button "the conditions"
1835 'action (lambda (button) (describe-copying))
1836 'follow-link t)
1837 (insert ".
cd6ab24f 1838Type C-h C-d for information on ")
fad930b6
RS
1839 (insert-button "getting the latest version"
1840 'action (lambda (button) (describe-distribution))
1841 'follow-link t)
1842 (insert "."))
1843 (insert (substitute-command-keys
490a1ad6 1844 "
cd6ab24f 1845GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for "))
fad930b6
RS
1846 (insert-button "full details"
1847 'action (lambda (button) (describe-no-warranty))
1848 'follow-link t)
1849 (insert (substitute-command-keys ".
c61453ea 1850Emacs is Free Software--Free as in Freedom--so you can redistribute copies
cd6ab24f 1851of Emacs and modify it; type \\[describe-copying] to see "))
fad930b6
RS
1852 (insert-button "the conditions"
1853 'action (lambda (button) (describe-copying))
1854 'follow-link t)
1855 (insert (substitute-command-keys".
cd6ab24f 1856Type \\[describe-distribution] for information on "))
fad930b6
RS
1857 (insert-button "getting the latest version"
1858 'action (lambda (button) (describe-distribution))
1859 'follow-link t)
1860 (insert ".")))
1861
1862(defun normal-about-screen ()
1863 (insert "\n" (emacs-version) "\n" emacs-copyright "\n\n")
1864
1865 (insert "To follow a link, click Mouse-1 on it, or move to it and type RET.\n\n")
1866
490a1ad6
JL
1867 (insert-button "Authors"
1868 'action
1869 (lambda (button)
1870 (view-file (expand-file-name "AUTHORS" data-directory))
1871 (goto-char (point-min)))
1872 'follow-link t)
1873 (insert "\t\tMany people have contributed code included in GNU Emacs\n")
1874
1875 (insert-button "Contributing"
1876 'action
1877 (lambda (button)
1878 (view-file (expand-file-name "CONTRIBUTE" data-directory))
1879 (goto-char (point-min)))
1880 'follow-link t)
1881 (insert "\tHow to contribute improvements to Emacs\n\n")
1882
fad930b6 1883 (insert-button "GNU and Freedom"
92b2e7a3 1884 'action (lambda (button) (describe-gnu-project))
fad930b6
RS
1885 'follow-link t)
1886 (insert "\t\tWhy we developed GNU Emacs and the GNU system\n")
1887
1888 (insert-button "Absence of Warranty"
1889 'action (lambda (button) (describe-no-warranty))
1890 'follow-link t)
1891 (insert "\tGNU Emacs comes with ABSOLUTELY NO WARRANTY\n")
1892
1893 (insert-button "Copying Conditions"
1894 'action (lambda (button) (describe-copying))
1895 'follow-link t)
1896 (insert "\tConditions for redistributing and changing Emacs\n")
1897
1898 (insert-button "Getting New Versions"
1899 'action (lambda (button) (describe-distribution))
1900 'follow-link t)
1901 (insert "\tHow to get the latest version of GNU Emacs\n")
1902
1903 (insert-button "More Manuals / Ordering Manuals"
1904 'action (lambda (button) (view-order-manuals))
1905 'follow-link t)
1906 (insert "\tBuying printed manuals from the FSF\n"))
c61453ea 1907
f645586f 1908(defun startup-echo-area-message ()
bd8b0ebb 1909 (if (eq (key-binding "\C-h\C-a") 'about-emacs)
1cff9ad1 1910 "For information about GNU Emacs and the GNU system, type C-h C-a."
f645586f 1911 (substitute-command-keys
1cff9ad1
JL
1912 "For information about GNU Emacs and the GNU system, type \
1913\\[about-emacs].")))
ce9ded5d
GM
1914
1915
49939379 1916(defun display-startup-echo-area-message ()
15fa6db0 1917 (let ((resize-mini-windows t))
f1be5774
KL
1918 (or noninteractive ;(input-pending-p) init-file-had-error
1919 ;; t if the init file says to inhibit the echo area startup message.
1920 (and inhibit-startup-echo-area-message
1921 user-init-file
1922 (or (and (get 'inhibit-startup-echo-area-message 'saved-value)
1923 (equal inhibit-startup-echo-area-message
1924 (if (equal init-file-user "")
1925 (user-login-name)
1926 init-file-user)))
1927 ;; Wasn't set with custom; see if .emacs has a setq.
1928 (let ((buffer (get-buffer-create " *temp*")))
1929 (prog1
1930 (condition-case nil
1931 (save-excursion
1932 (set-buffer buffer)
1933 (insert-file-contents user-init-file)
1934 (re-search-forward
1935 (concat
1936 "([ \t\n]*setq[ \t\n]+"
1937 "inhibit-startup-echo-area-message[ \t\n]+"
1938 (regexp-quote
1939 (prin1-to-string
1940 (if (equal init-file-user "")
1941 (user-login-name)
1942 init-file-user)))
1943 "[ \t\n]*)")
1944 nil t))
1945 (error nil))
1946 (kill-buffer buffer)))))
1d865f15 1947 (message "%s" (startup-echo-area-message)))))
f645586f 1948
1cff9ad1 1949(defun display-startup-screen (&optional concise)
22a58255
CY
1950 "Display startup screen according to display.
1951A fancy display is used on graphic displays, normal otherwise.
49939379 1952
22a58255
CY
1953If CONCISE is non-nil, display a concise version of the startup
1954screen."
526039df 1955 ;; Prevent recursive calls from server-process-filter.
1cff9ad1 1956 (if (not (get-buffer "*GNU Emacs*"))
526039df 1957 (if (use-fancy-splash-screens-p)
22a58255 1958 (fancy-startup-screen concise)
79b1c79c 1959 (normal-splash-screen t concise))))
22a58255
CY
1960
1961(defun display-about-screen ()
1962 "Display the *About GNU Emacs* buffer.
1963A fancy display is used on graphic displays, normal otherwise."
1964 (interactive)
6b61353c 1965 (if (use-fancy-splash-screens-p)
1cff9ad1
JL
1966 (fancy-about-screen)
1967 (normal-splash-screen nil)))
c61453ea 1968
22a58255 1969(defalias 'about-emacs 'display-about-screen)
1cff9ad1 1970(defalias 'display-splash-screen 'display-startup-screen)
c61453ea 1971
a726e0d1 1972(defun command-line-1 (command-line-args-left)
f1be5774 1973 (display-startup-echo-area-message)
ed638cc9
RS
1974
1975 ;; Delay 2 seconds after an init file error message
1976 ;; was displayed, so user can read it.
bca8c7be
MS
1977 (when init-file-had-error
1978 (sit-for 2))
1979
cb58ea33
RS
1980 (when (and pure-space-overflow
1981 (not noninteractive))
1982 (display-warning
1983 'initialization
2dca98f3 1984 "Building Emacs overflowed pure space. (See the node Pure Storage in the Lisp manual for details.)"
cb58ea33
RS
1985 :warning))
1986
22a58255
CY
1987 (let ((file-count 0)
1988 first-file-buffer)
1989 (when command-line-args-left
1990 ;; We have command args; process them.
1991 (let ((dir command-line-default-directory)
1992 tem
1993 ;; This approach loses for "-batch -L DIR --eval "(require foo)",
1994 ;; if foo is intended to be found in DIR.
1995 ;;
1996 ;; ;; The directories listed in --directory/-L options will *appear*
1997 ;; ;; at the front of `load-path' in the order they appear on the
1998 ;; ;; command-line. We cannot do this by *placing* them at the front
1999 ;; ;; in the order they appear, so we need this variable to hold them,
2000 ;; ;; temporarily.
2001 ;; extra-load-path
2002 ;;
2003 ;; To DTRT we keep track of the splice point and modify `load-path'
2004 ;; straight away upon any --directory/-L option.
2005 splice
2006 just-files ;; t if this follows the magic -- option.
2007 ;; This includes our standard options' long versions
2008 ;; and long versions of what's on command-switch-alist.
2009 (longopts
2010 (append '(("--funcall") ("--load") ("--insert") ("--kill")
2011 ("--directory") ("--eval") ("--execute") ("--no-splash")
2012 ("--find-file") ("--visit") ("--file") ("--no-desktop"))
2013 (mapcar (lambda (elt)
2014 (list (concat "-" (car elt))))
2015 command-switch-alist)))
2016 (line 0)
2017 (column 0))
2018
2019 ;; Add the long X options to longopts.
2020 (dolist (tem command-line-x-option-alist)
2021 (if (string-match "^--" (car tem))
2022 (push (list (car tem)) longopts)))
2023
2024 ;; Loop, processing options.
2025 (while command-line-args-left
2026 (let* ((argi (car command-line-args-left))
2027 (orig-argi argi)
2028 argval completion)
2029 (setq command-line-args-left (cdr command-line-args-left))
2030
2031 ;; Do preliminary decoding of the option.
2032 (if just-files
2033 ;; After --, don't look for options; treat all args as files.
2034 (setq argi "")
2035 ;; Convert long options to ordinary options
2036 ;; and separate out an attached option argument into argval.
2037 (when (string-match "^\\(--[^=]*\\)=" argi)
2038 (setq argval (substring argi (match-end 0))
2039 argi (match-string 1 argi)))
2040 (if (equal argi "--")
2041 (setq completion nil)
2042 (setq completion (try-completion argi longopts)))
2043 (if (eq completion t)
2044 (setq argi (substring argi 1))
2045 (if (stringp completion)
2046 (let ((elt (assoc completion longopts)))
2047 (or elt
2048 (error "Option `%s' is ambiguous" argi))
2049 (setq argi (substring (car elt) 1)))
2050 (setq argval nil
2051 argi orig-argi))))
2052
2053 ;; Execute the option.
2054 (cond ((setq tem (assoc argi command-switch-alist))
2055 (if argval
2056 (let ((command-line-args-left
2057 (cons argval command-line-args-left)))
2058 (funcall (cdr tem) argi))
2059 (funcall (cdr tem) argi)))
2060
2061 ((equal argi "-no-splash")
1cff9ad1 2062 (setq inhibit-startup-screen t))
22a58255
CY
2063
2064 ((member argi '("-f" ; what the manual claims
2065 "-funcall"
2066 "-e")) ; what the source used to say
1cff9ad1 2067 (setq inhibit-startup-screen t)
22a58255
CY
2068 (setq tem (intern (or argval (pop command-line-args-left))))
2069 (if (commandp tem)
2070 (command-execute tem)
2071 (funcall tem)))
2072
2073 ((member argi '("-eval" "-execute"))
1cff9ad1 2074 (setq inhibit-startup-screen t)
22a58255
CY
2075 (eval (read (or argval (pop command-line-args-left)))))
2076
2077 ((member argi '("-L" "-directory"))
2078 (setq tem (expand-file-name
2079 (command-line-normalize-file-name
2080 (or argval (pop command-line-args-left)))))
2081 (cond (splice (setcdr splice (cons tem (cdr splice)))
2082 (setq splice (cdr splice)))
2083 (t (setq load-path (cons tem load-path)
2084 splice load-path))))
2085
2086 ((member argi '("-l" "-load"))
2087 (let* ((file (command-line-normalize-file-name
2088 (or argval (pop command-line-args-left))))
2089 ;; Take file from default dir if it exists there;
2090 ;; otherwise let `load' search for it.
2091 (file-ex (expand-file-name file)))
2092 (when (file-exists-p file-ex)
2093 (setq file file-ex))
2094 (load file nil t)))
2095
2096 ;; This is used to handle -script. It's not clear
f71d844b 2097 ;; we need to document it (it is totally internal).
1d1100d7 2098 ((member argi '("-scriptload"))
22a58255
CY
2099 (let* ((file (command-line-normalize-file-name
2100 (or argval (pop command-line-args-left))))
2101 ;; Take file from default dir.
2102 (file-ex (expand-file-name file)))
2103 (load file-ex nil t t)))
2104
2105 ((equal argi "-insert")
1cff9ad1 2106 (setq inhibit-startup-screen t)
22a58255
CY
2107 (setq tem (or argval (pop command-line-args-left)))
2108 (or (stringp tem)
2109 (error "File name omitted from `-insert' option"))
2110 (insert-file-contents (command-line-normalize-file-name tem)))
2111
2112 ((equal argi "-kill")
2113 (kill-emacs t))
2114
2115 ;; This is for when they use --no-desktop with -q, or
2116 ;; don't load Desktop in their .emacs. If desktop.el
2117 ;; _is_ loaded, it will handle this switch, and we
2118 ;; won't see it by the time we get here.
2119 ((equal argi "-no-desktop")
2120 (message "\"--no-desktop\" ignored because the Desktop package is not loaded"))
2121
2122 ((string-match "^\\+[0-9]+\\'" argi)
2123 (setq line (string-to-number argi)))
2124
2125 ((string-match "^\\+\\([0-9]+\\):\\([0-9]+\\)\\'" argi)
2126 (setq line (string-to-number (match-string 1 argi))
2127 column (string-to-number (match-string 2 argi))))
2128
2129 ((setq tem (assoc argi command-line-x-option-alist))
2130 ;; Ignore X-windows options and their args if not using X.
2131 (setq command-line-args-left
2132 (nthcdr (nth 1 tem) command-line-args-left)))
2133
2134 ((member argi '("-find-file" "-file" "-visit"))
1cff9ad1 2135 (setq inhibit-startup-screen t)
22a58255
CY
2136 ;; An explicit option to specify visiting a file.
2137 (setq tem (or argval (pop command-line-args-left)))
2138 (unless (stringp tem)
2139 (error "File name omitted from `%s' option" argi))
2140 (setq file-count (1+ file-count))
2141 (let ((file (expand-file-name
2142 (command-line-normalize-file-name tem) dir)))
2143 (if (= file-count 1)
2144 (setq first-file-buffer (find-file file))
2145 (find-file-other-window file)))
2146 (or (zerop line)
2147 (goto-line line))
2148 (setq line 0)
2149 (unless (< column 1)
2150 (move-to-column (1- column)))
2151 (setq column 0))
2152
2153 ((equal argi "--")
2154 (setq just-files t))
2155 (t
2156 ;; We have almost exhausted our options. See if the
2157 ;; user has made any other command-line options available
2158 (let ((hooks command-line-functions)
2159 (did-hook nil))
2160 (while (and hooks
2161 (not (setq did-hook (funcall (car hooks)))))
2162 (setq hooks (cdr hooks)))
2163 (if (not did-hook)
2164 ;; Presume that the argument is a file name.
2165 (progn
2166 (if (string-match "\\`-" argi)
2167 (error "Unknown option `%s'" argi))
1cff9ad1
JL
2168 (unless initial-window-system
2169 (setq inhibit-startup-screen t))
22a58255
CY
2170 (setq file-count (1+ file-count))
2171 (let ((file
2172 (expand-file-name
2173 (command-line-normalize-file-name orig-argi)
2174 dir)))
3d9d55e6
CY
2175 (cond ((= file-count 1)
2176 (setq first-file-buffer (find-file file)))
2177 (inhibit-startup-screen
2178 (find-file-other-window file))
2179 (t (find-file file))))
22a58255
CY
2180 (or (zerop line)
2181 (goto-line line))
2182 (setq line 0)
2183 (unless (< column 1)
2184 (move-to-column (1- column)))
2185 (setq column 0))))))
2186 ;; In unusual circumstances, the execution of Lisp code due
2187 ;; to command-line options can cause the last visible frame
2188 ;; to be deleted. In this case, kill emacs to avoid an
2189 ;; abort later.
2190 (unless (frame-live-p (selected-frame)) (kill-emacs nil))))))
2191
2192 (when initial-buffer-choice
2193 (cond ((eq initial-buffer-choice t)
2194 (switch-to-buffer (get-buffer-create "*scratch*")))
2195 ((stringp initial-buffer-choice)
2196 (find-file initial-buffer-choice))))
2197
8d86c6da
JL
2198 ;; If *scratch* exists and is empty, insert initial-scratch-message.
2199 (and initial-scratch-message
2200 (get-buffer "*scratch*")
2201 (with-current-buffer "*scratch*"
2202 (when (zerop (buffer-size))
2203 (insert initial-scratch-message)
2204 (set-buffer-modified-p nil))))
2205
1cff9ad1 2206 (if (or inhibit-startup-screen
22a58255
CY
2207 initial-buffer-choice
2208 noninteractive
2209 emacs-quick-startup)
2210
2211 ;; Not displaying a startup screen. If 3 or more files
2212 ;; visited, and not all visible, show user what they all are.
2213 (and (> file-count 2)
2214 (not noninteractive)
2215 (not inhibit-startup-buffer-menu)
2216 (or (get-buffer-window first-file-buffer)
2217 (list-buffers)))
2218
2219 ;; Display a startup screen, after some preparations.
2220
2221 ;; If there are no switches to process, we might as well
2222 ;; run this hook now, and there may be some need to do it
2223 ;; before doing any output.
2224 (run-hooks 'emacs-startup-hook)
2225 (and term-setup-hook
2226 (run-hooks 'term-setup-hook))
2227 (setq inhibit-startup-hooks t)
2228
2229 ;; It's important to notice the user settings before we
2230 ;; display the startup message; otherwise, the settings
2231 ;; won't take effect until the user gives the first
2232 ;; keystroke, and that's distracting.
2233 (when (fboundp 'frame-notice-user-settings)
2234 (frame-notice-user-settings))
2235
2236 ;; If there are no switches to process, we might as well
2237 ;; run this hook now, and there may be some need to do it
2238 ;; before doing any output.
2239 (when window-setup-hook
2240 (run-hooks 'window-setup-hook)
2241 ;; Don't let the hook be run twice.
2242 (setq window-setup-hook nil))
2243
04ef2618
CY
2244 ;; ;; Do this now to avoid an annoying delay if the user
2245 ;; ;; clicks the menu bar during the sit-for.
2246 ;; (when (display-popup-menus-p)
2247 ;; (precompute-menubar-bindings))
2248 ;; (with-no-warnings
2249 ;; (setq menubar-bindings-done t))
22a58255 2250
1cff9ad1
JL
2251 (if (> file-count 0)
2252 (display-startup-screen t)
2253 (display-startup-screen nil)))))
49939379 2254
47c7adae
RS
2255(defun command-line-normalize-file-name (file)
2256 "Collapse multiple slashes to one, to handle non-Emacs file names."
6b9e794f
RS
2257 (save-match-data
2258 ;; Use arg 1 so that we don't collapse // at the start of the file name.
2259 ;; That is significant on some systems.
2260 ;; However, /// at the beginning is supposed to mean just /, not //.
2261 (if (string-match "^///+" file)
2262 (setq file (replace-match "/" t t file)))
2263 (while (string-match "//+" file 1)
2264 (setq file (replace-match "/" t t file)))
2265 file))
47c7adae 2266
a3194d03 2267;; arch-tag: 7e294698-244d-4758-984b-4047f887a5db
c88ab9ce 2268;;; startup.el ends here