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