Merged from emacs@sv.gnu.org
[bpt/emacs.git] / lisp / startup.el
1 ;;; startup.el --- process Emacs shell arguments
2
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This file parses the command line and gets Emacs running. Options
29 ;; on the command line are handled in precedence order. For priorities
30 ;; see the structure standard_args in the emacs.c file.
31
32 ;;; Code:
33
34 (setq top-level '(normal-top-level))
35
36 (defvar command-line-processed nil
37 "Non-nil once command line has been processed.")
38
39 (defvar window-system initial-window-system
40 "Name of window system the selected frame is displaying through.
41 The value is a symbol--for instance, `x' for X windows.
42 The value is nil if the selected frame is on a text-only-terminal.")
43
44 (make-variable-frame-local 'window-system)
45
46 (defgroup initialization nil
47 "Emacs start-up procedure."
48 :group 'internal)
49
50 (defcustom inhibit-splash-screen nil
51 "Non-nil inhibits the startup screen.
52 It also inhibits display of the initial message in the `*scratch*' buffer.
53
54 This is for use in your personal init file (but NOT site-start.el), once
55 you are familiar with the contents of the startup screen."
56 :type 'boolean
57 :group 'initialization)
58
59 (defvaralias 'inhibit-startup-message 'inhibit-splash-screen)
60
61 (defcustom inhibit-startup-echo-area-message nil
62 "*Non-nil inhibits the initial startup echo area message.
63 Setting this variable takes effect
64 only if you do it with the customization buffer
65 or if your `.emacs' file contains a line of this form:
66 (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
67 If your `.emacs' file is byte-compiled, use the following form instead:
68 (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
69 Thus, someone else using a copy of your `.emacs' file will see
70 the startup message unless he personally acts to inhibit it."
71 :type '(choice (const :tag "Don't inhibit")
72 (string :tag "Enter your user name, to inhibit"))
73 :group 'initialization)
74
75 (defcustom inhibit-default-init nil
76 "*Non-nil inhibits loading the `default' library."
77 :type 'boolean
78 :group 'initialization)
79
80 (defcustom inhibit-startup-buffer-menu nil
81 "*Non-nil inhibits display of buffer list when more than 2 files are loaded."
82 :type 'boolean
83 :group 'initialization)
84
85 (defvar command-switch-alist nil
86 "Alist of command-line switches.
87 Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
88 HANDLER-FUNCTION receives the switch string as its sole argument;
89 the remaining command-line args are in the variable `command-line-args-left'.")
90
91 (defvar command-line-args-left nil
92 "List of command-line args not yet processed.")
93
94 (defvar command-line-functions nil ;; lrs 7/31/89
95 "List of functions to process unrecognized command-line arguments.
96 Each function should access the dynamically bound variables
97 `argi' (the current argument) and `command-line-args-left' (the remaining
98 arguments). The function should return non-nil only if it recognizes and
99 processes `argi'. If it does so, it may consume successive arguments by
100 altering `command-line-args-left' to remove them.")
101
102 (defvar command-line-default-directory nil
103 "Default directory to use for command line arguments.
104 This is normally copied from `default-directory' when Emacs starts.")
105
106 ;;; This is here, rather than in x-win.el, so that we can ignore these
107 ;;; options when we are not using X.
108 (defconst command-line-x-option-alist
109 '(("-bw" 1 x-handle-numeric-switch border-width)
110 ("-d" 1 x-handle-display)
111 ("-display" 1 x-handle-display)
112 ("-name" 1 x-handle-name-switch)
113 ("-title" 1 x-handle-switch title)
114 ("-T" 1 x-handle-switch title)
115 ("-r" 0 x-handle-switch reverse t)
116 ("-rv" 0 x-handle-switch reverse t)
117 ("-reverse" 0 x-handle-switch reverse t)
118 ("-reverse-video" 0 x-handle-switch reverse t)
119 ("-fn" 1 x-handle-switch font)
120 ("-font" 1 x-handle-switch font)
121 ("-fs" 0 x-handle-initial-switch fullscreen fullboth)
122 ("-fw" 0 x-handle-initial-switch fullscreen fullwidth)
123 ("-fh" 0 x-handle-initial-switch fullscreen fullheight)
124 ("-ib" 1 x-handle-numeric-switch internal-border-width)
125 ("-g" 1 x-handle-geometry)
126 ("-lsp" 1 x-handle-numeric-switch line-spacing)
127 ("-geometry" 1 x-handle-geometry)
128 ("-fg" 1 x-handle-switch foreground-color)
129 ("-foreground" 1 x-handle-switch foreground-color)
130 ("-bg" 1 x-handle-switch background-color)
131 ("-background" 1 x-handle-switch background-color)
132 ("-ms" 1 x-handle-switch mouse-color)
133 ("-nbi" 0 x-handle-switch icon-type nil)
134 ("-iconic" 0 x-handle-iconic)
135 ("-xrm" 1 x-handle-xrm-switch)
136 ("-cr" 1 x-handle-switch cursor-color)
137 ("-vb" 0 x-handle-switch vertical-scroll-bars t)
138 ("-hb" 0 x-handle-switch horizontal-scroll-bars t)
139 ("-bd" 1 x-handle-switch)
140 ("--border-width" 1 x-handle-numeric-switch border-width)
141 ("--display" 1 x-handle-display)
142 ("--name" 1 x-handle-name-switch)
143 ("--title" 1 x-handle-switch title)
144 ("--reverse-video" 0 x-handle-switch reverse t)
145 ("--font" 1 x-handle-switch font)
146 ("--fullscreen" 0 x-handle-initial-switch fullscreen fullboth)
147 ("--fullwidth" 0 x-handle-initial-switch fullscreen fullwidth)
148 ("--fullheight" 0 x-handle-initial-switch fullscreen fullheight)
149 ("--internal-border" 1 x-handle-numeric-switch internal-border-width)
150 ("--geometry" 1 x-handle-geometry)
151 ("--foreground-color" 1 x-handle-switch foreground-color)
152 ("--background-color" 1 x-handle-switch background-color)
153 ("--mouse-color" 1 x-handle-switch mouse-color)
154 ("--no-bitmap-icon" 0 x-handle-switch icon-type nil)
155 ("--iconic" 0 x-handle-iconic)
156 ("--xrm" 1 x-handle-xrm-switch)
157 ("--cursor-color" 1 x-handle-switch cursor-color)
158 ("--vertical-scroll-bars" 0 x-handle-switch vertical-scroll-bars t)
159 ("--line-spacing" 1 x-handle-numeric-switch line-spacing)
160 ("--border-color" 1 x-handle-switch border-color)
161 ("--smid" 1 x-handle-smid))
162 "Alist of X Windows options.
163 Each element has the form
164 (NAME NUMARGS HANDLER FRAME-PARAM VALUE)
165 where NAME is the option name string, NUMARGS is the number of arguments
166 that the option accepts, HANDLER is a function to call to handle the option.
167 FRAME-PARAM (optional) is the frame parameter this option specifies,
168 and VALUE is the value which is given to that frame parameter
169 \(most options use the argument for this, so VALUE is not present).")
170
171 (defvar before-init-hook nil
172 "Normal hook run after handling urgent options but before loading init files.")
173
174 (defvar after-init-hook nil
175 "Normal hook run after loading the init files, `~/.emacs' and `default.el'.
176 There is no `condition-case' around the running of these functions;
177 therefore, if you set `debug-on-error' non-nil in `.emacs',
178 an error in one of these functions will invoke the debugger.")
179
180 (defvar emacs-startup-hook nil
181 "Normal hook run after loading init files and handling the command line.")
182
183 (defvar term-setup-hook nil
184 "Normal hook run after loading terminal-specific Lisp code.
185 It also follows `emacs-startup-hook'. This hook exists for users to set,
186 so as to override the definitions made by the terminal-specific file.
187 Emacs never sets this variable itself.")
188
189 (defvar inhibit-startup-hooks nil
190 "Non-nil means don't run `term-setup-hook' and `emacs-startup-hook'.
191 This is because we already did so.")
192
193 (defvar keyboard-type nil
194 "The brand of keyboard you are using.
195 This variable is used to define the proper function and keypad
196 keys for use under X. It is used in a fashion analogous to the
197 environment variable TERM.")
198
199 (defvar window-setup-hook nil
200 "Normal hook run to initialize window system display.
201 Emacs runs this hook after processing the command line arguments and loading
202 the user's init file.")
203
204 (defcustom initial-major-mode 'lisp-interaction-mode
205 "Major mode command symbol to use for the initial `*scratch*' buffer."
206 :type 'function
207 :group 'initialization)
208
209 (defvar init-file-user nil
210 "Identity of user whose `.emacs' file is or was read.
211 The value is nil if `-q' or `--no-init-file' was specified,
212 meaning do not load any init file.
213
214 Otherwise, the value may be an empty string, meaning
215 use the init file for the user who originally logged in,
216 or it may be a string containing a user's name meaning
217 use that person's init file.
218
219 In either of the latter cases, `(concat \"~\" init-file-user \"/\")'
220 evaluates to the name of the directory where the `.emacs' file was
221 looked for.
222
223 Setting `init-file-user' does not prevent Emacs from loading
224 `site-start.el'. The only way to do that is to use `--no-site-file'.")
225
226 (defcustom site-run-file "site-start"
227 "File containing site-wide run-time initializations.
228 This file is loaded at run-time before `~/.emacs'. It contains inits
229 that need to be in place for the entire site, but which, due to their
230 higher incidence of change, don't make sense to load into Emacs's
231 dumped image. Thus, the run-time load order is: 1. file described in
232 this variable, if non-nil; 2. `~/.emacs'; 3. `default.el'.
233
234 Don't use the `site-start.el' file for things some users may not like.
235 Put them in `default.el' instead, so that users can more easily
236 override them. Users can prevent loading `default.el' with the `-q'
237 option or by setting `inhibit-default-init' in their own init files,
238 but inhibiting `site-start.el' requires `--no-site-file', which
239 is less convenient.
240
241 This variable is defined for customization so as to make
242 it visible in the relevant context. However, actually customizing it
243 is not allowed, since it would not work anyway. The only way to set
244 this variable usefully is to set it while building and dumping Emacs."
245 :type '(choice (const :tag "none" nil) string)
246 :group 'initialization
247 :initialize 'custom-initialize-default
248 :set '(lambda (variable value)
249 (error "Customizing `site-run-file' does not work")))
250
251 (defcustom mail-host-address nil
252 "*Name of this machine, for purposes of naming users."
253 :type '(choice (const nil) string)
254 :group 'mail)
255
256 (defcustom user-mail-address (if command-line-processed
257 (or (getenv "EMAIL")
258 (concat (user-login-name) "@"
259 (or mail-host-address
260 (system-name))))
261 ;; Empty string means "not set yet".
262 "")
263 "*Full mailing address of this user.
264 This is initialized with environment variable `EMAIL' or, as a
265 fallback, using `mail-host-address'. This is done after your
266 init file is read, in case it sets `mail-host-address'."
267 :type 'string
268 :group 'mail)
269
270 (defcustom auto-save-list-file-prefix
271 (cond ((eq system-type 'ms-dos)
272 ;; MS-DOS cannot have initial dot, and allows only 8.3 names
273 "~/_emacs.d/auto-save.list/_s")
274 (t
275 "~/.emacs.d/auto-save-list/.saves-"))
276 "Prefix for generating `auto-save-list-file-name'.
277 This is used after reading your `.emacs' file to initialize
278 `auto-save-list-file-name', by appending Emacs's pid and the system name,
279 if you have not already set `auto-save-list-file-name' yourself.
280 Directories in the prefix will be created if necessary.
281 Set this to nil if you want to prevent `auto-save-list-file-name'
282 from being initialized."
283 :type '(choice (const :tag "Don't record a session's auto save list" nil)
284 string)
285 :group 'auto-save)
286
287 (defvar emacs-quick-startup nil)
288
289 (defvar emacs-basic-display nil)
290
291 (defvar init-file-debug nil)
292
293 (defvar init-file-had-error nil
294 "Non-nil if there was an error loading the user's init file.")
295
296 (defvar normal-top-level-add-subdirs-inode-list nil)
297
298 (defvar no-blinking-cursor nil)
299
300 (defvar default-frame-background-mode)
301
302 (defvar pure-space-overflow nil
303 "Non-nil if building Emacs overflowed pure space.")
304
305 (defun normal-top-level-add-subdirs-to-load-path ()
306 "Add all subdirectories of current directory to `load-path'.
307 More precisely, this uses only the subdirectories whose names
308 start with letters or digits; it excludes any subdirectory named `RCS'
309 or `CVS', and any subdirectory that contains a file named `.nosearch'."
310 (let (dirs
311 attrs
312 (pending (list default-directory)))
313 ;; This loop does a breadth-first tree walk on DIR's subtree,
314 ;; putting each subdir into DIRS as its contents are examined.
315 (while pending
316 (push (pop pending) dirs)
317 (let* ((this-dir (car dirs))
318 (contents (directory-files this-dir))
319 (default-directory this-dir)
320 (canonicalized (if (fboundp 'untranslated-canonical-name)
321 (untranslated-canonical-name this-dir))))
322 ;; The Windows version doesn't report meaningful inode
323 ;; numbers, so use the canonicalized absolute file name of the
324 ;; directory instead.
325 (setq attrs (or canonicalized
326 (nthcdr 10 (file-attributes this-dir))))
327 (unless (member attrs normal-top-level-add-subdirs-inode-list)
328 (push attrs normal-top-level-add-subdirs-inode-list)
329 (dolist (file contents)
330 ;; The lower-case variants of RCS and CVS are for DOS/Windows.
331 (unless (member file '("." ".." "RCS" "CVS" "rcs" "cvs"))
332 (when (and (string-match "\\`[[:alnum:]]" file)
333 ;; Avoid doing a `stat' when it isn't necessary
334 ;; because that can cause trouble when an NFS server
335 ;; is down.
336 (not (string-match "\\.elc?\\'" file))
337 (file-directory-p file))
338 (let ((expanded (expand-file-name file)))
339 (unless (file-exists-p (expand-file-name ".nosearch"
340 expanded))
341 (setq pending (nconc pending (list expanded)))))))))))
342 (normal-top-level-add-to-load-path (cdr (nreverse dirs)))))
343
344 ;; This function is called from a subdirs.el file.
345 ;; It assumes that default-directory is the directory
346 ;; in which the subdirs.el file exists,
347 ;; and it adds to load-path the subdirs of that directory
348 ;; as specified in DIRS. Normally the elements of DIRS are relative.
349 (defun normal-top-level-add-to-load-path (dirs)
350 (let ((tail load-path)
351 (thisdir (directory-file-name default-directory)))
352 (while (and tail
353 ;;Don't go all the way to the nil terminator.
354 (cdr tail)
355 (not (equal thisdir (car tail)))
356 (not (and (memq system-type '(ms-dos windows-nt))
357 (equal (downcase thisdir) (downcase (car tail))))))
358 (setq tail (cdr tail)))
359 ;;Splice the new section in.
360 (when tail
361 (setcdr tail (append (mapcar 'expand-file-name dirs) (cdr tail))))))
362
363 (defun normal-top-level ()
364 (if command-line-processed
365 (message "Back to top level.")
366 (setq command-line-processed t)
367 ;; Give *Messages* the same default-directory as *scratch*,
368 ;; just to keep things predictable.
369 (let ((dir default-directory))
370 (with-current-buffer "*Messages*"
371 (setq default-directory dir)))
372 ;; `user-full-name' is now known; reset its standard-value here.
373 (put 'user-full-name 'standard-value
374 (list (default-value 'user-full-name)))
375 ;; For root, preserve owner and group when editing files.
376 (if (equal (user-uid) 0)
377 (setq backup-by-copying-when-mismatch t))
378 ;; Look in each dir in load-path for a subdirs.el file.
379 ;; If we find one, load it, which will add the appropriate subdirs
380 ;; of that dir into load-path,
381 ;; Look for a leim-list.el file too. Loading it will register
382 ;; available input methods.
383 (let ((tail load-path) dir)
384 (while tail
385 (setq dir (car tail))
386 (let ((default-directory dir))
387 (load (expand-file-name "subdirs.el") t t t))
388 (let ((default-directory dir))
389 (load (expand-file-name "leim-list.el") t t t))
390 ;; We don't use a dolist loop and we put this "setq-cdr" command at
391 ;; the end, because the subdirs.el files may add elements to the end
392 ;; of load-path and we want to take it into account.
393 (setq tail (cdr tail))))
394 (unless (eq system-type 'vax-vms)
395 ;; If the PWD environment variable isn't accurate, delete it.
396 (let ((pwd (getenv "PWD")))
397 (and (stringp pwd)
398 ;; Use FOO/., so that if FOO is a symlink, file-attributes
399 ;; describes the directory linked to, not FOO itself.
400 (or (equal (file-attributes
401 (concat (file-name-as-directory pwd) "."))
402 (file-attributes
403 (concat (file-name-as-directory default-directory)
404 ".")))
405 (setq process-environment
406 (delete (concat "PWD=" pwd)
407 process-environment))))))
408 (setq default-directory (abbreviate-file-name default-directory))
409 (let ((menubar-bindings-done nil))
410 (unwind-protect
411 (command-line)
412 ;; Do this again, in case .emacs defined more abbreviations.
413 (setq default-directory (abbreviate-file-name default-directory))
414 ;; Specify the file for recording all the auto save files of this session.
415 ;; This is used by recover-session.
416 (or auto-save-list-file-name
417 (and auto-save-list-file-prefix
418 (setq auto-save-list-file-name
419 ;; Under MS-DOS our PID is almost always reused between
420 ;; Emacs invocations. We need something more unique.
421 (cond ((eq system-type 'ms-dos)
422 ;; We are going to access the auto-save
423 ;; directory, so make sure it exists.
424 (make-directory
425 (file-name-directory auto-save-list-file-prefix)
426 t)
427 (concat
428 (make-temp-name
429 (expand-file-name
430 auto-save-list-file-prefix))
431 "~"))
432 (t
433 (expand-file-name
434 (format "%s%d-%s~"
435 auto-save-list-file-prefix
436 (emacs-pid)
437 (system-name))))))))
438 (unless inhibit-startup-hooks
439 (run-hooks 'emacs-startup-hook)
440 (and term-setup-hook
441 (run-hooks 'term-setup-hook)))
442
443 ;; Don't do this if we failed to create the initial frame,
444 ;; for instance due to a dense colormap.
445 (when (or frame-initial-frame
446 ;; If frame-initial-frame has no meaning, do this anyway.
447 (not (and initial-window-system
448 (not noninteractive)
449 (not (eq initial-window-system 'pc)))))
450 ;; Modify the initial frame based on what .emacs puts into
451 ;; ...-frame-alist.
452 (if (fboundp 'frame-notice-user-settings)
453 (frame-notice-user-settings))
454 ;; Set the faces for the initial background mode even if
455 ;; frame-notice-user-settings didn't (such as on a tty).
456 ;; frame-set-background-mode is idempotent, so it won't
457 ;; cause any harm if it's already been done.
458 (if (fboundp 'frame-set-background-mode)
459 (frame-set-background-mode (selected-frame))))
460
461 ;; Now we know the user's default font, so add it to the menu.
462 (if (fboundp 'font-menu-add-default)
463 (font-menu-add-default))
464 (and window-setup-hook
465 (run-hooks 'window-setup-hook))
466 (or menubar-bindings-done
467 (if (display-popup-menus-p)
468 (precompute-menubar-bindings)))))))
469
470 ;; Precompute the keyboard equivalents in the menu bar items.
471 (defun precompute-menubar-bindings ()
472 (let ((submap (lookup-key global-map [menu-bar])))
473 (while submap
474 (and (consp (car submap))
475 (symbolp (car (car submap)))
476 (stringp (car-safe (cdr (car submap))))
477 (keymapp (cdr (cdr (car submap))))
478 (progn
479 (x-popup-menu nil (cdr (cdr (car submap))))
480 (if purify-flag
481 (garbage-collect))))
482 (setq submap (cdr submap))))
483 (setq define-key-rebound-commands t))
484
485 ;; Command-line options supported by tty's:
486 (defconst tty-long-option-alist
487 '(("--name" . "-name")
488 ("--title" . "-T")
489 ("--reverse-video" . "-reverse")
490 ("--foreground-color" . "-fg")
491 ("--background-color" . "-bg")
492 ("--color" . "-color")))
493
494 (defconst tool-bar-images-pixel-height 24
495 "Height in pixels of images in the tool bar.")
496
497 (defvar tool-bar-originally-present nil
498 "Non-nil if tool-bars are present before user and site init files are read.")
499
500 (defvar handle-args-function-alist '((nil . tty-handle-args))
501 "Functions for processing window-system dependent command-line arguments.
502 Window system startup files should add their own function to this
503 alist, which should parse the command line arguments. Those
504 pertaining to the window system should be processed and removed
505 from the returned command line.")
506
507 (defvar window-system-initialization-alist '((nil . ignore))
508 "Alist of window-system initialization functions.
509 Window-system startup files should add their own initialization
510 function to this list. The function should take no arguments,
511 and initialize the window system environment to prepare for
512 opening the first frame (e.g. open a connection to an X server).")
513
514 ;; Handle the X-like command-line arguments "-fg", "-bg", "-name", etc.
515 (defun tty-handle-args (args)
516 (let (rest)
517 (message "%S" args)
518 (while (and args
519 (not (equal (car args) "--")))
520 (let* ((argi (pop args))
521 (orig-argi argi)
522 argval completion)
523 ;; Check for long options with attached arguments
524 ;; and separate out the attached option argument into argval.
525 (when (string-match "^\\(--[^=]*\\)=" argi)
526 (setq argval (substring argi (match-end 0))
527 argi (match-string 1 argi)))
528 (when (string-match "^--" argi)
529 (setq completion (try-completion argi tty-long-option-alist))
530 (if (eq completion t)
531 ;; Exact match for long option.
532 (setq argi (cdr (assoc argi tty-long-option-alist)))
533 (if (stringp completion)
534 (let ((elt (assoc completion tty-long-option-alist)))
535 ;; Check for abbreviated long option.
536 (or elt
537 (error "Option `%s' is ambiguous" argi))
538 (setq argi (cdr elt)))
539 ;; Check for a short option.
540 (setq argval nil
541 argi orig-argi))))
542 (cond ((member argi '("-fg" "-foreground"))
543 (push (cons 'foreground-color (or argval (pop args)))
544 default-frame-alist))
545 ((member argi '("-bg" "-background"))
546 (push (cons 'background-color (or argval (pop args)))
547 default-frame-alist))
548 ((member argi '("-T" "-name"))
549 (unless argval (setq argval (pop args)))
550 (push (cons 'title
551 (if (stringp argval)
552 argval
553 (let ((case-fold-search t)
554 i)
555 (setq argval (invocation-name))
556
557 ;; Change any . or * characters in name to
558 ;; hyphens, so as to emulate behavior on X.
559 (while
560 (setq i (string-match "[.*]" argval))
561 (aset argval i ?-))
562 argval)))
563 default-frame-alist))
564 ((member argi '("-r" "-rv" "-reverse"))
565 (push '(reverse . t)
566 default-frame-alist))
567 ((equal argi "-color")
568 (unless argval (setq argval 8)) ; default --color means 8 ANSI colors
569 (push (cons 'tty-color-mode
570 (cond
571 ((numberp argval) argval)
572 ((string-match "-?[0-9]+" argval)
573 (string-to-number argval))
574 (t (intern argval))))
575 default-frame-alist))
576 (t
577 (push argi rest)))))
578 (nreverse rest)))
579
580 (defun command-line ()
581 (setq command-line-default-directory default-directory)
582
583 ;; Choose a reasonable location for temporary files.
584 (custom-reevaluate-setting 'temporary-file-directory)
585 (custom-reevaluate-setting 'small-temporary-file-directory)
586 (custom-reevaluate-setting 'auto-save-file-name-transforms)
587
588 ;; See if we should import version-control from the environment variable.
589 (let ((vc (getenv "VERSION_CONTROL")))
590 (cond ((eq vc nil)) ;don't do anything if not set
591 ((member vc '("t" "numbered"))
592 (setq version-control t))
593 ((member vc '("nil" "existing"))
594 (setq version-control nil))
595 ((member vc '("never" "simple"))
596 (setq version-control 'never))))
597
598 ;;! This has been commented out; I currently find the behavior when
599 ;;! split-window-keep-point is nil disturbing, but if I can get used
600 ;;! to it, then it would be better to eliminate the option.
601 ;;! ;; Choose a good default value for split-window-keep-point.
602 ;;! (setq split-window-keep-point (> baud-rate 2400))
603
604 ;; Set the default strings to display in mode line for
605 ;; end-of-line formats that aren't native to this platform.
606 (cond
607 ((memq system-type '(ms-dos windows-nt emx))
608 (setq eol-mnemonic-unix "(Unix)"
609 eol-mnemonic-mac "(Mac)"))
610 ;; Both Mac and Unix EOLs are now "native" on Mac OS so keep the
611 ;; abbreviated strings `/' and `:' set in coding.c for them.
612 ((eq system-type 'macos)
613 (setq eol-mnemonic-dos "(DOS)"))
614 (t ; this is for Unix/GNU/Linux systems
615 (setq eol-mnemonic-dos "(DOS)"
616 eol-mnemonic-mac "(Mac)")))
617
618 ;; Make sure window system's init file was loaded in loadup.el if using a window system.
619 (condition-case error
620 (unless noninteractive
621 (if (and initial-window-system
622 (not (featurep
623 (intern (concat (symbol-name initial-window-system) "-win")))))
624 (error "Unsupported window system `%s'" initial-window-system))
625 ;; Process window-system specific command line parameters.
626 (setq command-line-args
627 (funcall (or (cdr (assq initial-window-system handle-args-function-alist))
628 (error "Unsupported window system `%s'" initial-window-system))
629 command-line-args))
630 ;; Initialize the window system. (Open connection, etc.)
631 (funcall (or (cdr (assq initial-window-system window-system-initialization-alist))
632 (error "Unsupported window system `%s'" initial-window-system))))
633 ;; If there was an error, print the error message and exit.
634 (error
635 (princ
636 (if (eq (car error) 'error)
637 (apply 'concat (cdr error))
638 (if (memq 'file-error (get (car error) 'error-conditions))
639 (format "%s: %s"
640 (nth 1 error)
641 (mapconcat (lambda (obj) (prin1-to-string obj t))
642 (cdr (cdr error)) ", "))
643 (format "%s: %s"
644 (get (car error) 'error-message)
645 (mapconcat (lambda (obj) (prin1-to-string obj t))
646 (cdr error) ", "))))
647 'external-debugging-output)
648 (terpri 'external-debugging-output)
649 (setq initial-window-system nil)
650 (kill-emacs)))
651
652 (set-locale-environment nil)
653
654 ;; Convert preloaded file names in load-history to absolute.
655 (let ((simple-file-name
656 ;; Look for simple.el or simple.elc and use their directory
657 ;; as the place where all Lisp files live.
658 (locate-file "simple" load-path (get-load-suffixes)))
659 lisp-dir)
660 ;; Don't abort if simple.el cannot be found, but print a warning.
661 (if (null simple-file-name)
662 (progn
663 (princ "Warning: Could not find simple.el nor simple.elc"
664 'external-debugging-output)
665 (terpri 'external-debugging-output))
666 (setq lisp-dir (file-truename (file-name-directory simple-file-name)))
667 (setq load-history
668 (mapcar (lambda (elt)
669 (if (and (stringp (car elt))
670 (not (file-name-absolute-p (car elt))))
671 (cons (concat lisp-dir
672 (car elt))
673 (cdr elt))
674 elt))
675 load-history))))
676
677 ;; Convert the arguments to Emacs internal representation.
678 (let ((args (cdr command-line-args)))
679 (while args
680 (setcar args
681 (decode-coding-string (car args) locale-coding-system t))
682 (pop args)))
683
684 (let ((done nil)
685 (args (cdr command-line-args)))
686
687 ;; Figure out which user's init file to load,
688 ;; either from the environment or from the options.
689 (setq init-file-user (if noninteractive nil (user-login-name)))
690 ;; If user has not done su, use current $HOME to find .emacs.
691 (and init-file-user
692 (equal init-file-user (user-real-login-name))
693 (setq init-file-user ""))
694
695 ;; Process the command-line args, and delete the arguments
696 ;; processed. This is consistent with the way main in emacs.c
697 ;; does things.
698 (while (and (not done) args)
699 (let* ((longopts '(("--no-init-file") ("--no-site-file") ("--debug-init")
700 ("--user") ("--iconic") ("--icon-type") ("--quick")
701 ("--no-blinking-cursor") ("--basic-display")))
702 (argi (pop args))
703 (orig-argi argi)
704 argval)
705 ;; Handle --OPTION=VALUE format.
706 (when (string-match "^\\(--[^=]*\\)=" argi)
707 (setq argval (substring argi (match-end 0))
708 argi (match-string 1 argi)))
709 (unless (equal argi "--")
710 (let ((completion (try-completion argi longopts)))
711 (if (eq completion t)
712 (setq argi (substring argi 1))
713 (if (stringp completion)
714 (let ((elt (assoc completion longopts)))
715 (or elt
716 (error "Option `%s' is ambiguous" argi))
717 (setq argi (substring (car elt) 1)))
718 (setq argval nil
719 argi orig-argi)))))
720 (cond
721 ((member argi '("-Q" "-quick"))
722 (setq init-file-user nil
723 site-run-file nil
724 emacs-quick-startup t))
725 ((member argi '("-D" "-basic-display"))
726 (setq no-blinking-cursor t
727 emacs-basic-display t)
728 (push '(vertical-scroll-bars . nil) initial-frame-alist))
729 ((member argi '("-q" "-no-init-file"))
730 (setq init-file-user nil))
731 ((member argi '("-u" "-user"))
732 (setq init-file-user (or argval (pop args))
733 argval nil))
734 ((equal argi "-no-site-file")
735 (setq site-run-file nil))
736 ((equal argi "-debug-init")
737 (setq init-file-debug t))
738 ((equal argi "-iconic")
739 (push '(visibility . icon) initial-frame-alist))
740 ((member argi '("-icon-type" "-i" "-itype"))
741 (push '(icon-type . t) default-frame-alist))
742 ((member argi '("-nbc" "-no-blinking-cursor"))
743 (setq no-blinking-cursor t))
744 ;; Push the popped arg back on the list of arguments.
745 (t
746 (push argi args)
747 (setq done t)))
748 ;; Was argval set but not used?
749 (and argval
750 (error "Option `%s' doesn't allow an argument" argi))))
751
752 ;; Re-attach the program name to the front of the arg list.
753 (and command-line-args
754 (setcdr command-line-args args)))
755
756 (run-hooks 'before-init-hook)
757
758 ;; Under X Window, this creates the X frame and deletes the terminal frame.
759 (when (fboundp 'frame-initialize)
760 (frame-initialize))
761
762 ;; Turn off blinking cursor if so specified in X resources. This is here
763 ;; only because all other settings of no-blinking-cursor are here.
764 (unless (or noninteractive
765 emacs-basic-display
766 (and (memq window-system '(x w32 mac))
767 (not (member (x-get-resource "cursorBlink" "CursorBlink")
768 '("off" "false")))))
769 (setq no-blinking-cursor t))
770
771 ;; If frame was created with a menu bar, set menu-bar-mode on.
772 (unless (or noninteractive
773 emacs-basic-display
774 (and (memq initial-window-system '(x w32))
775 (<= (frame-parameter nil 'menu-bar-lines) 0)))
776 (menu-bar-mode 1))
777
778 ;; If frame was created with a tool bar, switch tool-bar-mode on.
779 (unless (or noninteractive
780 emacs-basic-display
781 (not (display-graphic-p))
782 (<= (frame-parameter nil 'tool-bar-lines) 0))
783 (tool-bar-mode 1))
784
785 ;; Can't do this init in defcustom because the relevant variables
786 ;; are not set.
787 (custom-reevaluate-setting 'blink-cursor-mode)
788 (custom-reevaluate-setting 'tooltip-mode)
789 (custom-reevaluate-setting 'global-font-lock-mode)
790 (custom-reevaluate-setting 'mouse-wheel-down-event)
791 (custom-reevaluate-setting 'mouse-wheel-up-event)
792 (custom-reevaluate-setting 'file-name-shadow-mode)
793 (custom-reevaluate-setting 'send-mail-function)
794 (custom-reevaluate-setting 'focus-follows-mouse)
795
796 (normal-erase-is-backspace-setup-frame)
797
798 ;; Register default TTY colors for the case the terminal hasn't a
799 ;; terminal init file. We do this regardles of whether the terminal
800 ;; supports colors or not and regardless the current display type,
801 ;; since users can connect to color-capable terminals and also
802 ;; switch color support on or off in mid-session by setting the
803 ;; tty-color-mode frame parameter.
804 (tty-register-default-colors)
805
806 ;; Record whether the tool-bar is present before the user and site
807 ;; init files are processed. frame-notice-user-settings uses this
808 ;; to determine if the tool-bar has been disabled by the init files,
809 ;; and the frame needs to be resized.
810 (when (fboundp 'frame-notice-user-settings)
811 (let ((tool-bar-lines (or (assq 'tool-bar-lines initial-frame-alist)
812 (assq 'tool-bar-lines default-frame-alist))))
813 (setq tool-bar-originally-present
814 (and tool-bar-lines
815 (cdr tool-bar-lines)
816 (not (eq 0 (cdr tool-bar-lines)))))))
817
818 (let ((old-scalable-fonts-allowed scalable-fonts-allowed)
819 (old-font-list-limit font-list-limit)
820 (old-face-ignored-fonts face-ignored-fonts))
821
822 ;; Run the site-start library if it exists. The point of this file is
823 ;; that it is run before .emacs. There is no point in doing this after
824 ;; .emacs; that is useless.
825 (if site-run-file
826 (load site-run-file t t))
827
828 ;; Sites should not disable this. Only individuals should disable
829 ;; the startup message.
830 (setq inhibit-startup-message nil)
831
832 ;; Warn for invalid user name.
833 (when init-file-user
834 (if (string-match "[~/:\n]" init-file-user)
835 (display-warning 'initialization
836 (format "Invalid user name %s"
837 init-file-user)
838 :error)
839 (if (file-directory-p (expand-file-name
840 ;; We don't support ~USER on MS-Windows except
841 ;; for the current user, and always load .emacs
842 ;; from the current user's home directory (see
843 ;; below). So always check "~", even if invoked
844 ;; with "-u USER", or if $USER or $LOGNAME are
845 ;; set to something different.
846 (if (eq system-type 'windows-nt)
847 "~"
848 (concat "~" init-file-user))))
849 nil
850 (display-warning 'initialization
851 (format "User %s has no home directory"
852 init-file-user)
853 :error))))
854
855 ;; Load that user's init file, or the default one, or none.
856 (let (debug-on-error-from-init-file
857 debug-on-error-should-be-set
858 (debug-on-error-initial
859 (if (eq init-file-debug t) 'startup init-file-debug))
860 (orig-enable-multibyte default-enable-multibyte-characters))
861 (let ((debug-on-error debug-on-error-initial)
862 ;; This function actually reads the init files.
863 (inner
864 (function
865 (lambda ()
866 (if init-file-user
867 (let ((user-init-file-1
868 (cond
869 ((eq system-type 'ms-dos)
870 (concat "~" init-file-user "/_emacs"))
871 ((eq system-type 'windows-nt)
872 ;; Prefer .emacs on Windows.
873 (if (directory-files "~" nil "^\\.emacs\\(\\.elc?\\)?$")
874 "~/.emacs"
875 ;; Also support _emacs for compatibility.
876 (if (directory-files "~" nil "^_emacs\\(\\.elc?\\)?$")
877 "~/_emacs"
878 ;; But default to .emacs if _emacs does not exist.
879 "~/.emacs")))
880 ((eq system-type 'vax-vms)
881 "sys$login:.emacs")
882 (t
883 (concat "~" init-file-user "/.emacs")))))
884 ;; This tells `load' to store the file name found
885 ;; into user-init-file.
886 (setq user-init-file t)
887 (load user-init-file-1 t t)
888
889 (when (eq user-init-file t)
890 ;; If we did not find ~/.emacs, try
891 ;; ~/.emacs.d/init.el.
892 (let ((otherfile
893 (expand-file-name
894 "init"
895 (file-name-as-directory
896 (concat "~" init-file-user "/.emacs.d")))))
897 (load otherfile t t)
898
899 ;; If we did not find the user's init file,
900 ;; set user-init-file conclusively.
901 ;; Don't let it be set from default.el.
902 (when (eq user-init-file t)
903 (setq user-init-file user-init-file-1))))
904
905 ;; If we loaded a compiled file, set
906 ;; `user-init-file' to the source version if that
907 ;; exists.
908 (when (and user-init-file
909 (equal (file-name-extension user-init-file)
910 "elc"))
911 (let* ((source (file-name-sans-extension user-init-file))
912 (alt (concat source ".el")))
913 (setq source (cond ((file-exists-p alt) alt)
914 ((file-exists-p source) source)
915 (t nil)))
916 (when source
917 (when (file-newer-than-file-p source user-init-file)
918 (message "Warning: %s is newer than %s"
919 source user-init-file)
920 (sit-for 1))
921 (setq user-init-file source))))
922
923 (unless inhibit-default-init
924 (let ((inhibit-startup-message nil))
925 ;; Users are supposed to be told their rights.
926 ;; (Plus how to get help and how to undo.)
927 ;; Don't you dare turn this off for anyone
928 ;; except yourself.
929 (load "default" t t)))))))))
930 (if init-file-debug
931 ;; Do this without a condition-case if the user wants to debug.
932 (funcall inner)
933 (condition-case error
934 (progn
935 (funcall inner)
936 (setq init-file-had-error nil))
937 (error
938 (let ((message-log-max nil))
939 (save-excursion
940 (set-buffer (get-buffer-create "*Messages*"))
941 (insert "\n\n"
942 (format "An error has occurred while loading `%s':\n\n"
943 user-init-file)
944 (format "%s%s%s"
945 (get (car error) 'error-message)
946 (if (cdr error) ": " "")
947 (mapconcat (lambda (s) (prin1-to-string s t)) (cdr error) ", "))
948 "\n\n"
949 "To ensure normal operation, you should investigate and remove the\n"
950 "cause of the error in your initialization file. Start Emacs with\n"
951 "the `--debug-init' option to view a complete error backtrace.\n\n"))
952 (message "Error in init file: %s%s%s"
953 (get (car error) 'error-message)
954 (if (cdr error) ": " "")
955 (mapconcat 'prin1-to-string (cdr error) ", "))
956 (let ((pop-up-windows nil))
957 (pop-to-buffer "*Messages*"))
958 (setq init-file-had-error t)))))
959
960 (if (and deactivate-mark transient-mark-mode)
961 (with-current-buffer (window-buffer)
962 (deactivate-mark)))
963
964 ;; If the user has a file of abbrevs, read it.
965 ;; FIXME: after the 22.0 release this should be changed so
966 ;; that it does not read the abbrev file when -batch is used
967 ;; on the command line.
968 (when (and (file-exists-p abbrev-file-name)
969 (file-readable-p abbrev-file-name))
970 (quietly-read-abbrev-file abbrev-file-name))
971
972 ;; If the abbrevs came entirely from the init file or the
973 ;; abbrevs file, they do not need saving.
974 (setq abbrevs-changed nil)
975
976 ;; If we can tell that the init file altered debug-on-error,
977 ;; arrange to preserve the value that it set up.
978 (or (eq debug-on-error debug-on-error-initial)
979 (setq debug-on-error-should-be-set t
980 debug-on-error-from-init-file debug-on-error)))
981 (if debug-on-error-should-be-set
982 (setq debug-on-error debug-on-error-from-init-file))
983 (unless (or default-enable-multibyte-characters
984 (eq orig-enable-multibyte default-enable-multibyte-characters))
985 ;; Init file changed to unibyte. Reset existing multibyte
986 ;; buffers (probably *scratch*, *Messages*, *Minibuff-0*).
987 ;; Arguably this should only be done if they're free of
988 ;; multibyte characters.
989 (mapcar (lambda (buffer)
990 (with-current-buffer buffer
991 (if enable-multibyte-characters
992 (set-buffer-multibyte nil))))
993 (buffer-list))
994 ;; Also re-set the language environment in case it was
995 ;; originally done before unibyte was set and is sensitive to
996 ;; unibyte (display table, terminal coding system &c).
997 (set-language-environment current-language-environment)))
998
999 ;; Do this here in case the init file sets mail-host-address.
1000 (if (equal user-mail-address "")
1001 (setq user-mail-address (or (getenv "EMAIL")
1002 (concat (user-login-name) "@"
1003 (or mail-host-address
1004 (system-name))))))
1005
1006 ;; Originally face attributes were specified via
1007 ;; `font-lock-face-attributes'. Users then changed the default
1008 ;; face attributes by setting that variable. However, we try and
1009 ;; be back-compatible and respect its value if set except for
1010 ;; faces where M-x customize has been used to save changes for the
1011 ;; face.
1012 (when (boundp 'font-lock-face-attributes)
1013 (let ((face-attributes font-lock-face-attributes))
1014 (while face-attributes
1015 (let* ((face-attribute (pop face-attributes))
1016 (face (car face-attribute)))
1017 ;; Rustle up a `defface' SPEC from a
1018 ;; `font-lock-face-attributes' entry.
1019 (unless (get face 'saved-face)
1020 (let ((foreground (nth 1 face-attribute))
1021 (background (nth 2 face-attribute))
1022 (bold-p (nth 3 face-attribute))
1023 (italic-p (nth 4 face-attribute))
1024 (underline-p (nth 5 face-attribute))
1025 face-spec)
1026 (when foreground
1027 (setq face-spec (cons ':foreground (cons foreground face-spec))))
1028 (when background
1029 (setq face-spec (cons ':background (cons background face-spec))))
1030 (when bold-p
1031 (setq face-spec (append '(:weight bold) face-spec)))
1032 (when italic-p
1033 (setq face-spec (append '(:slant italic) face-spec)))
1034 (when underline-p
1035 (setq face-spec (append '(:underline t) face-spec)))
1036 (face-spec-set face (list (list t face-spec)) nil)))))))
1037
1038 ;; If parameter have been changed in the init file which influence
1039 ;; face realization, clear the face cache so that new faces will
1040 ;; be realized.
1041 (unless (and (eq scalable-fonts-allowed old-scalable-fonts-allowed)
1042 (eq font-list-limit old-font-list-limit)
1043 (eq face-ignored-fonts old-face-ignored-fonts))
1044 (clear-face-cache)))
1045
1046 (run-hooks 'after-init-hook)
1047
1048 ;; Decode all default-directory.
1049 (if (and default-enable-multibyte-characters locale-coding-system)
1050 (save-excursion
1051 (dolist (elt (buffer-list))
1052 (set-buffer elt)
1053 (if default-directory
1054 (setq default-directory
1055 (decode-coding-string default-directory
1056 locale-coding-system t))))
1057 (setq command-line-default-directory
1058 (decode-coding-string command-line-default-directory
1059 locale-coding-system t))))
1060
1061 ;; If *scratch* exists and init file didn't change its mode, initialize it.
1062 (if (get-buffer "*scratch*")
1063 (with-current-buffer "*scratch*"
1064 (if (eq major-mode 'fundamental-mode)
1065 (funcall initial-major-mode))))
1066
1067 ;; Load library for our terminal type.
1068 ;; User init file can set term-file-prefix to nil to prevent this.
1069 (unless (or noninteractive
1070 initial-window-system)
1071 (tty-run-terminal-initialization (selected-frame)))
1072
1073 ;; Update the out-of-memory error message based on user's key bindings
1074 ;; for save-some-buffers.
1075 (setq memory-signal-data
1076 (list 'error
1077 (substitute-command-keys "Memory exhausted--use \\[save-some-buffers] then exit and restart Emacs")))
1078
1079 ;; Process the remaining args.
1080 (command-line-1 (cdr command-line-args))
1081
1082 ;; If -batch, terminate after processing the command options.
1083 (if noninteractive (kill-emacs t))
1084
1085 ;; Run emacs-session-restore (session management) if started by
1086 ;; the session manager and we have a session manager connection.
1087 (if (and (boundp 'x-session-previous-id)
1088 (stringp x-session-previous-id))
1089 (with-no-warnings
1090 (emacs-session-restore x-session-previous-id))))
1091
1092 (defcustom initial-scratch-message (purecopy "\
1093 ;; This buffer is for notes you don't want to save, and for Lisp evaluation.
1094 ;; If you want to create a file, visit that file with C-x C-f,
1095 ;; then enter the text in that file's own buffer.
1096
1097 ")
1098 "Initial message displayed in *scratch* buffer at startup.
1099 If this is nil, no message will be displayed.
1100 If `inhibit-splash-screen' is non-nil, then no message is displayed,
1101 regardless of the value of this variable."
1102 :type '(choice (text :tag "Message")
1103 (const :tag "none" nil))
1104 :group 'initialization)
1105
1106 \f
1107 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1108 ;;; Fancy splash screen
1109 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1110
1111 (defvar fancy-splash-text
1112 '((:face (variable-pitch :weight bold)
1113 "Important Help menu items:\n"
1114 :face variable-pitch
1115 (lambda ()
1116 (let* ((en "TUTORIAL")
1117 (tut (or (get-language-info current-language-environment
1118 'tutorial)
1119 en))
1120 (title (with-temp-buffer
1121 (insert-file-contents
1122 (expand-file-name tut data-directory)
1123 nil 0 256)
1124 (search-forward ".")
1125 (buffer-substring (point-min) (1- (point))))))
1126 ;; If there is a specific tutorial for the current language
1127 ;; environment and it is not English, append its title.
1128 (concat
1129 "Emacs Tutorial\t\tLearn how to use Emacs efficiently"
1130 (if (string= en tut)
1131 ""
1132 (concat " (" title ")"))
1133 "\n")))
1134 :face variable-pitch "\
1135 Emacs FAQ\t\tFrequently asked questions and answers
1136 View Emacs Manual\tView the Emacs manual using Info
1137 Absence of Warranty\tGNU Emacs comes with "
1138 :face (variable-pitch :slant oblique)
1139 "ABSOLUTELY NO WARRANTY\n"
1140 :face variable-pitch
1141 "\
1142 Copying Conditions\tConditions for redistributing and changing Emacs
1143 Getting New Versions\tHow to obtain the latest version of Emacs
1144 More Manuals / Ordering Manuals Buying printed manuals from the FSF\n")
1145 (:face variable-pitch
1146 "To quit a partially entered command, type "
1147 :face default
1148 "Control-g"
1149 :face variable-pitch
1150 ".\n"
1151 :face (variable-pitch :weight bold)
1152 "Useful File menu items:\n"
1153 :face variable-pitch
1154 "Exit Emacs\t\t(Or type "
1155 :face default
1156 "Control-x"
1157 :face variable-pitch
1158 " followed by "
1159 :face default
1160 "Control-c"
1161 :face variable-pitch
1162 ")
1163 Recover Crashed Session\tRecover files you were editing before a crash
1164
1165
1166
1167
1168
1169 "
1170 ))
1171 "A list of texts to show in the middle part of splash screens.
1172 Each element in the list should be a list of strings or pairs
1173 `:face FACE', like `fancy-splash-insert' accepts them.")
1174
1175
1176 (defgroup fancy-splash-screen ()
1177 "Fancy splash screen when Emacs starts."
1178 :version "21.1"
1179 :group 'initialization)
1180
1181
1182 (defcustom fancy-splash-delay 7
1183 "*Delay in seconds between splash screens."
1184 :group 'fancy-splash-screen
1185 :type 'integer)
1186
1187
1188 (defcustom fancy-splash-max-time 30
1189 "*Show splash screens for at most this number of seconds.
1190 Values less than twice `fancy-splash-delay' are ignored."
1191 :group 'fancy-splash-screen
1192 :type 'integer)
1193
1194
1195 (defcustom fancy-splash-image nil
1196 "*The image to show in the splash screens, or nil for defaults."
1197 :group 'fancy-splash-screen
1198 :type '(choice (const :tag "Default" nil)
1199 (file :tag "File")))
1200
1201
1202 ;; These are temporary storage areas for the splash screen display.
1203
1204 (defvar fancy-current-text nil)
1205 (defvar fancy-splash-help-echo nil)
1206 (defvar fancy-splash-stop-time nil)
1207 (defvar fancy-splash-outer-buffer nil)
1208 (defvar fancy-splash-last-input-event nil)
1209
1210 (defun fancy-splash-insert (&rest args)
1211 "Insert text into the current buffer, with faces.
1212 Arguments from ARGS should be either strings, functions called
1213 with no args that return a string, or pairs `:face FACE',
1214 where FACE is a valid face specification, as it can be used with
1215 `put-text-property'."
1216 (let ((current-face nil))
1217 (while args
1218 (if (eq (car args) :face)
1219 (setq args (cdr args) current-face (car args))
1220 (insert (propertize (let ((it (car args)))
1221 (if (functionp it)
1222 (funcall it)
1223 it))
1224 'face current-face
1225 'help-echo fancy-splash-help-echo)))
1226 (setq args (cdr args)))))
1227
1228
1229 (defun fancy-splash-head ()
1230 "Insert the head part of the splash screen into the current buffer."
1231 (let* ((image-file (cond ((stringp fancy-splash-image)
1232 fancy-splash-image)
1233 ((and (display-color-p)
1234 (image-type-available-p 'xpm))
1235 (if (and (fboundp 'x-display-planes)
1236 (= (funcall 'x-display-planes) 8))
1237 "splash8.xpm"
1238 "splash.xpm"))
1239 (t "splash.pbm")))
1240 (img (create-image image-file))
1241 (image-width (and img (car (image-size img))))
1242 (window-width (window-width (selected-window))))
1243 (when img
1244 (when (> window-width image-width)
1245 ;; Center the image in the window.
1246 (insert (propertize " " 'display
1247 `(space :align-to (+ center (-0.5 . ,img)))))
1248
1249 ;; Change the color of the XPM version of the splash image
1250 ;; so that it is visible with a dark frame background.
1251 (when (and (memq 'xpm img)
1252 (eq (frame-parameter nil 'background-mode) 'dark))
1253 (setq img (append img '(:color-symbols (("#000000" . "gray30"))))))
1254
1255 ;; Insert the image with a help-echo and a keymap.
1256 (let ((map (make-sparse-keymap))
1257 (help-echo "mouse-2: browse http://www.gnu.org/"))
1258 (define-key map [mouse-2]
1259 (lambda ()
1260 (interactive)
1261 (browse-url "http://www.gnu.org/")
1262 (throw 'exit nil)))
1263 (define-key map [down-mouse-2] 'ignore)
1264 (define-key map [up-mouse-2] 'ignore)
1265 (insert-image img (propertize "xxx" 'help-echo help-echo
1266 'keymap map)))
1267 (insert "\n"))))
1268 (fancy-splash-insert
1269 :face '(variable-pitch :foreground "red")
1270 (if (eq system-type 'gnu/linux)
1271 "GNU Emacs is one component of the GNU/Linux operating system."
1272 "GNU Emacs is one component of the GNU operating system."))
1273 (insert "\n")
1274 (fancy-splash-insert
1275 :face 'variable-pitch
1276 "You can do basic editing with the menu bar and scroll bar \
1277 using the mouse.\n\n")
1278 (when fancy-splash-outer-buffer
1279 (fancy-splash-insert
1280 :face 'variable-pitch
1281 "Type "
1282 :face 'default
1283 "Control-l"
1284 :face 'variable-pitch
1285 " to begin editing"
1286 (if (equal (buffer-name fancy-splash-outer-buffer)
1287 "*scratch*")
1288 ".\n"
1289 " your file.\n"))))
1290
1291 (defun fancy-splash-tail ()
1292 "Insert the tail part of the splash screen into the current buffer."
1293 (let ((fg (if (eq (frame-parameter nil 'background-mode) 'dark)
1294 "cyan" "darkblue")))
1295 (fancy-splash-insert :face `(variable-pitch :foreground ,fg)
1296 "\nThis is "
1297 (emacs-version)
1298 "\n"
1299 :face '(variable-pitch :height 0.5)
1300 "Copyright (C) 2007 Free Software Foundation, Inc.")
1301 (and auto-save-list-file-prefix
1302 ;; Don't signal an error if the
1303 ;; directory for auto-save-list files
1304 ;; does not yet exist.
1305 (file-directory-p (file-name-directory
1306 auto-save-list-file-prefix))
1307 (directory-files
1308 (file-name-directory auto-save-list-file-prefix)
1309 nil
1310 (concat "\\`"
1311 (regexp-quote (file-name-nondirectory
1312 auto-save-list-file-prefix)))
1313 t)
1314 (fancy-splash-insert :face '(variable-pitch :foreground "red")
1315 "\n\nIf an Emacs session crashed recently, "
1316 "type "
1317 :face '(fixed-pitch :foreground "red")
1318 "Meta-x recover-session RET"
1319 :face '(variable-pitch :foreground "red")
1320 "\nto recover"
1321 " the files you were editing."))))
1322
1323 (defun fancy-splash-screens-1 (buffer)
1324 "Timer function displaying a splash screen."
1325 (when (> (float-time) fancy-splash-stop-time)
1326 (throw 'stop-splashing nil))
1327 (unless fancy-current-text
1328 (setq fancy-current-text fancy-splash-text))
1329 (let ((text (car fancy-current-text)))
1330 (set-buffer buffer)
1331 (erase-buffer)
1332 (if pure-space-overflow
1333 (insert "\
1334 Warning Warning!!! Pure space overflow !!!Warning Warning
1335 \(See the node Pure Storage in the Lisp manual for details.)\n"))
1336 (fancy-splash-head)
1337 (apply #'fancy-splash-insert text)
1338 (fancy-splash-tail)
1339 (unless (current-message)
1340 (message fancy-splash-help-echo))
1341 (set-buffer-modified-p nil)
1342 (goto-char (point-min))
1343 (force-mode-line-update)
1344 (setq fancy-current-text (cdr fancy-current-text))))
1345
1346 (defun fancy-splash-default-action ()
1347 "Stop displaying the splash screen buffer.
1348 This is an internal function used to turn off the splash screen after
1349 the user caused an input event by hitting a key or clicking with the
1350 mouse."
1351 (interactive)
1352 (if (and (memq 'down (event-modifiers last-command-event))
1353 (eq (posn-window (event-start last-command-event))
1354 (selected-window)))
1355 ;; This is a mouse-down event in the splash screen window.
1356 ;; Ignore it and consume the corresponding mouse-up event.
1357 (read-event)
1358 (push last-command-event unread-command-events))
1359 (throw 'exit nil))
1360
1361 (defun fancy-splash-exit ()
1362 "Exit the splash screen."
1363 (if (get-buffer "GNU Emacs")
1364 (throw 'stop-splashing nil)))
1365
1366 (defun fancy-splash-delete-frame (frame)
1367 "Exit the splash screen after the frame is deleted."
1368 ;; We can not throw from `delete-frame-events', so we set up a timer
1369 ;; to exit the recursive edit as soon as Emacs is idle again.
1370 (if (frame-live-p frame)
1371 (run-at-time 0 nil 'fancy-splash-exit)))
1372
1373 (defun fancy-splash-screens (&optional hide-on-input)
1374 "Display fancy splash screens when Emacs starts."
1375 (if hide-on-input
1376 (let ((old-hourglass display-hourglass)
1377 (fancy-splash-outer-buffer (current-buffer))
1378 splash-buffer
1379 (old-minor-mode-map-alist minor-mode-map-alist)
1380 (old-emulation-mode-map-alists emulation-mode-map-alists)
1381 (old-special-event-map special-event-map)
1382 (frame (fancy-splash-frame))
1383 timer)
1384 (save-selected-window
1385 (select-frame frame)
1386 (switch-to-buffer " GNU Emacs")
1387 (make-local-variable 'cursor-type)
1388 (setq splash-buffer (current-buffer))
1389 (catch 'stop-splashing
1390 (unwind-protect
1391 (let* ((map (make-sparse-keymap))
1392 (cursor-type nil)
1393 (overriding-local-map map)
1394 ;; Catch if our frame is deleted; the delete-frame
1395 ;; event is unreliable and is handled by
1396 ;; `special-event-map' anyway.
1397 (delete-frame-functions (cons 'fancy-splash-delete-frame
1398 delete-frame-functions)))
1399 (define-key map [t] 'fancy-splash-default-action)
1400 (define-key map [mouse-movement] 'ignore)
1401 (define-key map [mode-line t] 'ignore)
1402 (define-key map [select-window] 'ignore)
1403 ;; Temporarily bind special events to
1404 ;; fancy-splash-special-event-action so as to stop
1405 ;; displaying splash screens with such events.
1406 ;; Otherwise, drag-n-drop into splash screens may
1407 ;; leave us in recursive editing with invisible
1408 ;; cursors for a while.
1409 (setq special-event-map (make-sparse-keymap))
1410 (map-keymap
1411 (lambda (key def)
1412 (define-key special-event-map (vector key)
1413 (if (eq def 'ignore)
1414 'ignore
1415 'fancy-splash-special-event-action)))
1416 old-special-event-map)
1417 (setq display-hourglass nil
1418 minor-mode-map-alist nil
1419 emulation-mode-map-alists nil
1420 buffer-undo-list t
1421 mode-line-format (propertize "---- %b %-"
1422 'face 'mode-line-buffer-id)
1423 fancy-splash-stop-time (+ (float-time)
1424 fancy-splash-max-time)
1425 timer (run-with-timer 0 fancy-splash-delay
1426 #'fancy-splash-screens-1
1427 splash-buffer))
1428 (message "%s" (startup-echo-area-message))
1429 (recursive-edit))
1430 (cancel-timer timer)
1431 (setq display-hourglass old-hourglass
1432 minor-mode-map-alist old-minor-mode-map-alist
1433 emulation-mode-map-alists old-emulation-mode-map-alists
1434 special-event-map old-special-event-map)
1435 (kill-buffer splash-buffer)
1436 (when (frame-live-p frame)
1437 (select-frame frame)
1438 (switch-to-buffer fancy-splash-outer-buffer))
1439 (when fancy-splash-last-input-event
1440 (setq last-input-event fancy-splash-last-input-event
1441 fancy-splash-last-input-event nil)
1442 (command-execute (lookup-key special-event-map
1443 (vector last-input-event))
1444 nil (vector last-input-event) t))))))
1445 ;; If hide-on-input is nil, don't hide the buffer on input.
1446 (if (or (window-minibuffer-p)
1447 (window-dedicated-p (selected-window)))
1448 (pop-to-buffer (current-buffer))
1449 (switch-to-buffer "*About GNU Emacs*"))
1450 (setq buffer-read-only nil)
1451 (erase-buffer)
1452 (if pure-space-overflow
1453 (insert "\
1454 Warning Warning!!! Pure space overflow !!!Warning Warning
1455 \(See the node Pure Storage in the Lisp manual for details.)\n"))
1456 (let (fancy-splash-outer-buffer)
1457 (fancy-splash-head)
1458 (dolist (text fancy-splash-text)
1459 (apply #'fancy-splash-insert text)
1460 (insert "\n"))
1461 (skip-chars-backward "\n")
1462 (delete-region (point) (point-max))
1463 (insert "\n")
1464 (fancy-splash-tail)
1465 (set-buffer-modified-p nil)
1466 (setq buffer-read-only t)
1467 (if (and view-read-only (not view-mode))
1468 (view-mode-enter nil 'kill-buffer))
1469 (goto-char (point-min)))))
1470
1471 (defun fancy-splash-special-event-action ()
1472 "Save the last event and stop displaying the splash screen buffer.
1473 This is an internal function used to turn off the splash screen after
1474 the user caused an input event that is bound in `special-event-map'"
1475 (interactive)
1476 (setq fancy-splash-last-input-event last-input-event)
1477 (throw 'exit nil))
1478
1479
1480 (defun fancy-splash-frame ()
1481 "Return the frame to use for the fancy splash screen.
1482 Returning non-nil does not mean we should necessarily
1483 use the fancy splash screen, but if we do use it,
1484 we put it on this frame."
1485 (let (chosen-frame)
1486 (dolist (frame (append (frame-list) (list (selected-frame))))
1487 (if (and (frame-visible-p frame)
1488 (not (window-minibuffer-p (frame-selected-window frame))))
1489 (setq chosen-frame frame)))
1490 chosen-frame))
1491
1492 (defun use-fancy-splash-screens-p ()
1493 "Return t if fancy splash screens should be used."
1494 (when (and (display-graphic-p)
1495 (or (and (display-color-p)
1496 (image-type-available-p 'xpm))
1497 (image-type-available-p 'pbm)))
1498 (let ((frame (fancy-splash-frame)))
1499 (when frame
1500 (let* ((img (create-image (or fancy-splash-image
1501 (if (and (display-color-p)
1502 (image-type-available-p 'xpm))
1503 "splash.xpm" "splash.pbm"))))
1504 (image-height (and img (cdr (image-size img nil frame))))
1505 ;; We test frame-height so that, if the frame is split
1506 ;; by displaying a warning, that doesn't cause the normal
1507 ;; splash screen to be used.
1508 (frame-height (1- (frame-height frame))))
1509 (> frame-height (+ image-height 19)))))))
1510
1511
1512 (defun normal-splash-screen (&optional hide-on-input)
1513 "Display splash screen when Emacs starts."
1514 (let ((prev-buffer (current-buffer)))
1515 (unwind-protect
1516 (with-current-buffer (get-buffer-create "GNU Emacs")
1517 (setq buffer-read-only nil)
1518 (erase-buffer)
1519 (set (make-local-variable 'tab-width) 8)
1520 (if hide-on-input
1521 (set (make-local-variable 'mode-line-format)
1522 (propertize "---- %b %-" 'face 'mode-line-buffer-id)))
1523
1524 (if pure-space-overflow
1525 (insert "\
1526 Warning Warning!!! Pure space overflow !!!Warning Warning
1527 \(See the node Pure Storage in the Lisp manual for details.)\n"))
1528
1529 ;; The convention for this piece of code is that
1530 ;; each piece of output starts with one or two newlines
1531 ;; and does not end with any newlines.
1532 (insert "Welcome to GNU Emacs")
1533 (insert
1534 (if (eq system-type 'gnu/linux)
1535 ", one component of the GNU/Linux operating system.\n"
1536 ", a part of the GNU operating system.\n"))
1537
1538 (if hide-on-input
1539 (insert (substitute-command-keys
1540 (concat
1541 "\nType \\[recenter] to begin editing"
1542 (if (equal (buffer-name prev-buffer) "*scratch*")
1543 ".\n"
1544 " your file.\n")))))
1545
1546 (if (display-mouse-p)
1547 ;; The user can use the mouse to activate menus
1548 ;; so give help in terms of menu items.
1549 (progn
1550 (insert "\
1551 You can do basic editing with the menu bar and scroll bar using the mouse.
1552 To quit a partially entered command, type Control-g.
1553
1554 Useful File menu items:
1555 Exit Emacs (or type Control-x followed by Control-c)
1556 Recover Crashed Session Recover files you were editing before a crash
1557
1558 Important Help menu items:
1559 Emacs Tutorial Learn how to use Emacs efficiently
1560 Emacs FAQ Frequently asked questions and answers
1561 Read the Emacs Manual View the Emacs manual using Info
1562 \(Non)Warranty GNU Emacs comes with ABSOLUTELY NO WARRANTY
1563 Copying Conditions Conditions for redistributing and changing Emacs
1564 Getting New Versions How to obtain the latest version of Emacs
1565 More Manuals / Ordering Manuals How to order printed manuals from the FSF
1566 ")
1567 (insert "\n\n" (emacs-version)
1568 "
1569 Copyright (C) 2007 Free Software Foundation, Inc."))
1570
1571 ;; No mouse menus, so give help using kbd commands.
1572
1573 ;; If keys have their default meanings,
1574 ;; use precomputed string to save lots of time.
1575 (if (and (eq (key-binding "\C-h") 'help-command)
1576 (eq (key-binding "\C-xu") 'advertised-undo)
1577 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-terminal)
1578 (eq (key-binding "\C-ht") 'help-with-tutorial)
1579 (eq (key-binding "\C-hi") 'info)
1580 (eq (key-binding "\C-hr") 'info-emacs-manual)
1581 (eq (key-binding "\C-h\C-n") 'view-emacs-news))
1582 (insert "
1583 Get help C-h (Hold down CTRL and press h)
1584 Emacs manual C-h r
1585 Emacs tutorial C-h t Undo changes C-x u
1586 Buy manuals C-h C-m Exit Emacs C-x C-c
1587 Browse manuals C-h i")
1588
1589 (insert (substitute-command-keys
1590 (format "\n
1591 Get help %s
1592 Emacs manual \\[info-emacs-manual]
1593 Emacs tutorial \\[help-with-tutorial]\tUndo changes\t\\[advertised-undo]
1594 Buy manuals \\[view-order-manuals]\tExit Emacs\t\\[save-buffers-kill-terminal]
1595 Browse manuals \\[info]"
1596 (let ((where (where-is-internal
1597 'help-command nil t)))
1598 (if where
1599 (key-description where)
1600 "M-x help"))))))
1601
1602 ;; Say how to use the menu bar with the keyboard.
1603 (if (and (eq (key-binding "\M-`") 'tmm-menubar)
1604 (eq (key-binding [f10]) 'tmm-menubar))
1605 (insert "
1606 Activate menubar F10 or ESC ` or M-`")
1607 (insert (substitute-command-keys "
1608 Activate menubar \\[tmm-menubar]")))
1609
1610 ;; Many users seem to have problems with these.
1611 (insert "
1612 \(`C-' means use the CTRL key. `M-' means use the Meta (or Alt) key.
1613 If you have no Meta key, you may instead type ESC followed by the character.)")
1614
1615 (insert "\n\n" (emacs-version)
1616 "
1617 Copyright (C) 2007 Free Software Foundation, Inc.")
1618
1619 (if (and (eq (key-binding "\C-h\C-c") 'describe-copying)
1620 (eq (key-binding "\C-h\C-d") 'describe-distribution)
1621 (eq (key-binding "\C-h\C-w") 'describe-no-warranty))
1622 (insert
1623 "\n
1624 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
1625 Emacs is Free Software--Free as in Freedom--so you can redistribute copies
1626 of Emacs and modify it; type C-h C-c to see the conditions.
1627 Type C-h C-d for information on getting the latest version.")
1628 (insert (substitute-command-keys
1629 "\n
1630 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for full details.
1631 Emacs is Free Software--Free as in Freedom--so you can redistribute copies
1632 of Emacs and modify it; type \\[describe-copying] to see the conditions.
1633 Type \\[describe-distribution] for information on getting the latest version."))))
1634
1635 ;; The rest of the startup screen is the same on all
1636 ;; kinds of terminals.
1637
1638 ;; Give information on recovering, if there was a crash.
1639 (and auto-save-list-file-prefix
1640 ;; Don't signal an error if the
1641 ;; directory for auto-save-list files
1642 ;; does not yet exist.
1643 (file-directory-p (file-name-directory
1644 auto-save-list-file-prefix))
1645 (directory-files
1646 (file-name-directory auto-save-list-file-prefix)
1647 nil
1648 (concat "\\`"
1649 (regexp-quote (file-name-nondirectory
1650 auto-save-list-file-prefix)))
1651 t)
1652 (insert "\n\nIf an Emacs session crashed recently, "
1653 "type Meta-x recover-session RET\nto recover"
1654 " the files you were editing."))
1655
1656 ;; Display the input that we set up in the buffer.
1657 (set-buffer-modified-p nil)
1658 (setq buffer-read-only t)
1659 (if (and view-read-only (not view-mode))
1660 (view-mode-enter nil 'kill-buffer))
1661 (goto-char (point-min))
1662 (if hide-on-input
1663 (if (or (window-minibuffer-p)
1664 (window-dedicated-p (selected-window)))
1665 ;; If hide-on-input is nil, creating a new frame will
1666 ;; generate enough events that the subsequent `sit-for'
1667 ;; will immediately return anyway.
1668 nil ;; (pop-to-buffer (current-buffer))
1669 (save-window-excursion
1670 (switch-to-buffer (current-buffer))
1671 (sit-for 120))
1672 (condition-case nil
1673 (switch-to-buffer (current-buffer))))))
1674 ;; Unwind ... ensure splash buffer is killed
1675 (if hide-on-input
1676 (kill-buffer "GNU Emacs")
1677 (switch-to-buffer "GNU Emacs")
1678 (rename-buffer "*About GNU Emacs*" t)))))
1679
1680
1681 (defun startup-echo-area-message ()
1682 (if (eq (key-binding "\C-h\C-p") 'describe-project)
1683 "For information about the GNU Project and its goals, type C-h C-p."
1684 (substitute-command-keys
1685 "For information about the GNU Project and its goals, type \
1686 \\[describe-project].")))
1687
1688
1689 (defun display-startup-echo-area-message ()
1690 (let ((resize-mini-windows t))
1691 (or noninteractive ;(input-pending-p) init-file-had-error
1692 ;; t if the init file says to inhibit the echo area startup message.
1693 (and inhibit-startup-echo-area-message
1694 user-init-file
1695 (or (and (get 'inhibit-startup-echo-area-message 'saved-value)
1696 (equal inhibit-startup-echo-area-message
1697 (if (equal init-file-user "")
1698 (user-login-name)
1699 init-file-user)))
1700 ;; Wasn't set with custom; see if .emacs has a setq.
1701 (let ((buffer (get-buffer-create " *temp*")))
1702 (prog1
1703 (condition-case nil
1704 (save-excursion
1705 (set-buffer buffer)
1706 (insert-file-contents user-init-file)
1707 (re-search-forward
1708 (concat
1709 "([ \t\n]*setq[ \t\n]+"
1710 "inhibit-startup-echo-area-message[ \t\n]+"
1711 (regexp-quote
1712 (prin1-to-string
1713 (if (equal init-file-user "")
1714 (user-login-name)
1715 init-file-user)))
1716 "[ \t\n]*)")
1717 nil t))
1718 (error nil))
1719 (kill-buffer buffer)))))
1720 ;; display-splash-screen at the end of command-line-1 calls
1721 ;; use-fancy-splash-screens-p. This can cause image.el to be
1722 ;; loaded, putting "Loading image... done" in the echo area.
1723 ;; This hides startup-echo-area-message. So
1724 ;; use-fancy-splash-screens-p is called here simply to get the
1725 ;; loading of image.el (if needed) out of the way before
1726 ;; display-startup-echo-area-message runs.
1727 (progn
1728 (use-fancy-splash-screens-p)
1729 (message "%s" (startup-echo-area-message))))))
1730
1731
1732 (defun display-splash-screen (&optional hide-on-input)
1733 "Display splash screen according to display.
1734 Fancy splash screens are used on graphic displays,
1735 normal otherwise.
1736 With a prefix argument, any user input hides the splash screen."
1737 (interactive "P")
1738 ;; Prevent recursive calls from server-process-filter.
1739 (if (not (get-buffer "GNU Emacs"))
1740 (if (use-fancy-splash-screens-p)
1741 (fancy-splash-screens hide-on-input)
1742 (normal-splash-screen hide-on-input))))
1743
1744 (defun command-line-1 (command-line-args-left)
1745 (display-startup-echo-area-message)
1746
1747 ;; Delay 2 seconds after an init file error message
1748 ;; was displayed, so user can read it.
1749 (when init-file-had-error
1750 (sit-for 2))
1751
1752 (when (and pure-space-overflow
1753 (not noninteractive))
1754 (display-warning
1755 'initialization
1756 "Building Emacs overflowed pure space. (See the node Pure Storage in the Lisp manual for details.)"
1757 :warning))
1758
1759 (when command-line-args-left
1760 ;; We have command args; process them.
1761 (let ((dir command-line-default-directory)
1762 (file-count 0)
1763 first-file-buffer
1764 tem
1765 ;; This approach loses for "-batch -L DIR --eval "(require foo)",
1766 ;; if foo is intended to be found in DIR.
1767 ;;
1768 ;; ;; The directories listed in --directory/-L options will *appear*
1769 ;; ;; at the front of `load-path' in the order they appear on the
1770 ;; ;; command-line. We cannot do this by *placing* them at the front
1771 ;; ;; in the order they appear, so we need this variable to hold them,
1772 ;; ;; temporarily.
1773 ;; extra-load-path
1774 ;;
1775 ;; To DTRT we keep track of the splice point and modify `load-path'
1776 ;; straight away upon any --directory/-L option.
1777 splice
1778 just-files ;; t if this follows the magic -- option.
1779 ;; This includes our standard options' long versions
1780 ;; and long versions of what's on command-switch-alist.
1781 (longopts
1782 (append '(("--funcall") ("--load") ("--insert") ("--kill")
1783 ("--directory") ("--eval") ("--execute") ("--no-splash")
1784 ("--find-file") ("--visit") ("--file") ("--no-desktop"))
1785 (mapcar (lambda (elt)
1786 (list (concat "-" (car elt))))
1787 command-switch-alist)))
1788 (line 0)
1789 (column 0))
1790
1791 ;; Add the long X options to longopts.
1792 (dolist (tem command-line-x-option-alist)
1793 (if (string-match "^--" (car tem))
1794 (push (list (car tem)) longopts)))
1795
1796 ;; Loop, processing options.
1797 (while command-line-args-left
1798 (let* ((argi (car command-line-args-left))
1799 (orig-argi argi)
1800 argval completion)
1801 (setq command-line-args-left (cdr command-line-args-left))
1802
1803 ;; Do preliminary decoding of the option.
1804 (if just-files
1805 ;; After --, don't look for options; treat all args as files.
1806 (setq argi "")
1807 ;; Convert long options to ordinary options
1808 ;; and separate out an attached option argument into argval.
1809 (when (string-match "^\\(--[^=]*\\)=" argi)
1810 (setq argval (substring argi (match-end 0))
1811 argi (match-string 1 argi)))
1812 (if (equal argi "--")
1813 (setq completion nil)
1814 (setq completion (try-completion argi longopts)))
1815 (if (eq completion t)
1816 (setq argi (substring argi 1))
1817 (if (stringp completion)
1818 (let ((elt (assoc completion longopts)))
1819 (or elt
1820 (error "Option `%s' is ambiguous" argi))
1821 (setq argi (substring (car elt) 1)))
1822 (setq argval nil
1823 argi orig-argi))))
1824
1825 ;; Execute the option.
1826 (cond ((setq tem (assoc argi command-switch-alist))
1827 (if argval
1828 (let ((command-line-args-left
1829 (cons argval command-line-args-left)))
1830 (funcall (cdr tem) argi))
1831 (funcall (cdr tem) argi)))
1832
1833 ((equal argi "-no-splash")
1834 (setq inhibit-startup-message t))
1835
1836 ((member argi '("-f" ; what the manual claims
1837 "-funcall"
1838 "-e")) ; what the source used to say
1839 (setq tem (intern (or argval (pop command-line-args-left))))
1840 (if (commandp tem)
1841 (command-execute tem)
1842 (funcall tem)))
1843
1844 ((member argi '("-eval" "-execute"))
1845 (eval (read (or argval (pop command-line-args-left)))))
1846
1847 ((member argi '("-L" "-directory"))
1848 (setq tem (expand-file-name
1849 (command-line-normalize-file-name
1850 (or argval (pop command-line-args-left)))))
1851 (cond (splice (setcdr splice (cons tem (cdr splice)))
1852 (setq splice (cdr splice)))
1853 (t (setq load-path (cons tem load-path)
1854 splice load-path))))
1855
1856 ((member argi '("-l" "-load"))
1857 (let* ((file (command-line-normalize-file-name
1858 (or argval (pop command-line-args-left))))
1859 ;; Take file from default dir if it exists there;
1860 ;; otherwise let `load' search for it.
1861 (file-ex (expand-file-name file)))
1862 (when (file-exists-p file-ex)
1863 (setq file file-ex))
1864 (load file nil t)))
1865
1866 ;; This is used to handle -script. It's not clear
1867 ;; we need to document it.
1868 ((member argi '("-scriptload"))
1869 (let* ((file (command-line-normalize-file-name
1870 (or argval (pop command-line-args-left))))
1871 ;; Take file from default dir.
1872 (file-ex (expand-file-name file)))
1873 (load file-ex nil t t)))
1874
1875 ((equal argi "-insert")
1876 (setq tem (or argval (pop command-line-args-left)))
1877 (or (stringp tem)
1878 (error "File name omitted from `-insert' option"))
1879 (insert-file-contents (command-line-normalize-file-name tem)))
1880
1881 ((equal argi "-kill")
1882 (kill-emacs t))
1883
1884 ;; This is for when they use --no-desktop with -q, or
1885 ;; don't load Desktop in their .emacs. If desktop.el
1886 ;; _is_ loaded, it will handle this switch, and we
1887 ;; won't see it by the time we get here.
1888 ((equal argi "-no-desktop")
1889 (message "\"--no-desktop\" ignored because the Desktop package is not loaded"))
1890
1891 ((string-match "^\\+[0-9]+\\'" argi)
1892 (setq line (string-to-number argi)))
1893
1894 ((string-match "^\\+\\([0-9]+\\):\\([0-9]+\\)\\'" argi)
1895 (setq line (string-to-number (match-string 1 argi))
1896 column (string-to-number (match-string 2 argi))))
1897
1898 ((setq tem (assoc argi command-line-x-option-alist))
1899 ;; Ignore X-windows options and their args if not using X.
1900 (setq command-line-args-left
1901 (nthcdr (nth 1 tem) command-line-args-left)))
1902
1903 ((member argi '("-find-file" "-file" "-visit"))
1904 ;; An explicit option to specify visiting a file.
1905 (setq tem (or argval (pop command-line-args-left)))
1906 (unless (stringp tem)
1907 (error "File name omitted from `%s' option" argi))
1908 (setq file-count (1+ file-count))
1909 (let ((file (expand-file-name
1910 (command-line-normalize-file-name tem) dir)))
1911 (if (= file-count 1)
1912 (setq first-file-buffer (find-file file))
1913 (find-file-other-window file)))
1914 (or (zerop line)
1915 (goto-line line))
1916 (setq line 0)
1917 (unless (< column 1)
1918 (move-to-column (1- column)))
1919 (setq column 0))
1920
1921 ((equal argi "--")
1922 (setq just-files t))
1923 (t
1924 ;; We have almost exhausted our options. See if the
1925 ;; user has made any other command-line options available
1926 (let ((hooks command-line-functions) ;; lrs 7/31/89
1927 (did-hook nil))
1928 (while (and hooks
1929 (not (setq did-hook (funcall (car hooks)))))
1930 (setq hooks (cdr hooks)))
1931 (if (not did-hook)
1932 ;; Presume that the argument is a file name.
1933 (progn
1934 (if (string-match "\\`-" argi)
1935 (error "Unknown option `%s'" argi))
1936 (setq file-count (1+ file-count))
1937 (let ((file
1938 (expand-file-name
1939 (command-line-normalize-file-name orig-argi)
1940 dir)))
1941 (if (= file-count 1)
1942 (setq first-file-buffer (find-file file))
1943 (find-file-other-window file)))
1944 (or (zerop line)
1945 (goto-line line))
1946 (setq line 0)
1947 (unless (< column 1)
1948 (move-to-column (1- column)))
1949 (setq column 0))))))
1950 ;; In unusual circumstances, the execution of Lisp code due
1951 ;; to command-line options can cause the last visible frame
1952 ;; to be deleted. In this case, kill emacs to avoid an
1953 ;; abort later.
1954 (unless (frame-live-p (selected-frame)) (kill-emacs nil))))
1955
1956 ;; If 3 or more files visited, and not all visible,
1957 ;; show user what they all are. But leave the last one current.
1958 (and (> file-count 2)
1959 (not noninteractive)
1960 (not inhibit-startup-buffer-menu)
1961 (or (get-buffer-window first-file-buffer)
1962 (list-buffers)))))
1963
1964 ;; Maybe display a startup screen.
1965 (unless (or inhibit-startup-message
1966 noninteractive
1967 emacs-quick-startup)
1968 ;; Display a startup screen, after some preparations.
1969
1970 ;; If there are no switches to process, we might as well
1971 ;; run this hook now, and there may be some need to do it
1972 ;; before doing any output.
1973 (run-hooks 'emacs-startup-hook)
1974 (and term-setup-hook
1975 (run-hooks 'term-setup-hook))
1976 (setq inhibit-startup-hooks t)
1977
1978 ;; It's important to notice the user settings before we
1979 ;; display the startup message; otherwise, the settings
1980 ;; won't take effect until the user gives the first
1981 ;; keystroke, and that's distracting.
1982 (when (fboundp 'frame-notice-user-settings)
1983 (frame-notice-user-settings))
1984
1985 ;; If there are no switches to process, we might as well
1986 ;; run this hook now, and there may be some need to do it
1987 ;; before doing any output.
1988 (when window-setup-hook
1989 (run-hooks 'window-setup-hook)
1990 ;; Don't let the hook be run twice.
1991 (setq window-setup-hook nil))
1992
1993 ;; Do this now to avoid an annoying delay if the user
1994 ;; clicks the menu bar during the sit-for.
1995 (when (display-popup-menus-p)
1996 (precompute-menubar-bindings))
1997 (with-no-warnings
1998 (setq menubar-bindings-done t))
1999
2000 ;; If *scratch* exists and is empty, insert initial-scratch-message.
2001 (and initial-scratch-message
2002 (get-buffer "*scratch*")
2003 (with-current-buffer "*scratch*"
2004 (when (zerop (buffer-size))
2005 (insert initial-scratch-message)
2006 (set-buffer-modified-p nil))))
2007
2008 ;; If user typed input during all that work,
2009 ;; abort the startup screen. Otherwise, display it now.
2010 (unless (input-pending-p)
2011 (display-splash-screen t))))
2012
2013
2014 (defun command-line-normalize-file-name (file)
2015 "Collapse multiple slashes to one, to handle non-Emacs file names."
2016 (save-match-data
2017 ;; Use arg 1 so that we don't collapse // at the start of the file name.
2018 ;; That is significant on some systems.
2019 ;; However, /// at the beginning is supposed to mean just /, not //.
2020 (if (string-match "^///+" file)
2021 (setq file (replace-match "/" t t file)))
2022 (while (string-match "//+" file 1)
2023 (setq file (replace-match "/" t t file)))
2024 file))
2025
2026 ;; arch-tag: 7e294698-244d-4758-984b-4047f887a5db
2027 ;;; startup.el ends here