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