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