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