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