startup.el 93.8 KB
Newer Older
1
;;; startup.el --- process Emacs shell arguments  -*- lexical-binding: t -*-
Eric S. Raymond's avatar
Eric S. Raymond committed
2

3
;; Copyright (C) 1985-1986, 1992, 1994-2013 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

Eric S. Raymond's avatar
Eric S. Raymond committed
5
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Keywords: internal
7
;; Package: emacs
Eric S. Raymond's avatar
Eric S. Raymond committed
8

Jim Blandy's avatar
Jim Blandy committed
9 10
;; This file is part of GNU Emacs.

11
;; GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
15 16 17 18 19 20 21

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
22
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Jim Blandy's avatar
Jim Blandy committed
23

Eric S. Raymond's avatar
Eric S. Raymond committed
24
;;; Commentary:
Jim Blandy's avatar
Jim Blandy committed
25

Kenichi Handa's avatar
Kenichi Handa committed
26 27 28
;; This file parses the command line and gets Emacs running.  Options
;; on the command line are handled in precedence order.  For priorities
;; see the structure standard_args in the emacs.c file.
Jim Blandy's avatar
Jim Blandy committed
29

Eric S. Raymond's avatar
Eric S. Raymond committed
30 31
;;; Code:

Jim Blandy's avatar
Jim Blandy committed
32 33
(setq top-level '(normal-top-level))

Richard M. Stallman's avatar
Richard M. Stallman committed
34
(defvar command-line-processed nil
35
  "Non-nil once command line has been processed.")
Jim Blandy's avatar
Jim Blandy committed
36

Richard M. Stallman's avatar
Richard M. Stallman committed
37
(defgroup initialization nil
38
  "Emacs start-up procedure."
39 40 41 42
  :group 'environment)

(defcustom initial-buffer-choice nil
  "Buffer to show after starting Emacs.
43
If the value is nil and `inhibit-startup-screen' is nil, show the
44 45 46 47
startup screen.  If the value is a string, switch to a buffer
visiting the file or directory specified by that string.  If the
value is a function, switch to the buffer returned by that
function.  If t, open the `*scratch*' buffer.
48 49 50

A string value also causes emacsclient to open the specified file
or directory when no target file is specified."
51
  :type '(choice
52
	  (const     :tag "Startup screen" nil)
53
	  (directory :tag "Directory" :value "~/")
54
	  (file      :tag "File" :value "~/.emacs")
55 56
	  (const     :tag "Notes buffer" remember-notes)
	  (function  :tag "Function")
57
	  (const     :tag "Lisp scratch buffer" t))
58
  :version "24.4"
59
  :group 'initialization)
Richard M. Stallman's avatar
Richard M. Stallman committed
60

61
(defcustom inhibit-startup-screen nil
62 63
  "Non-nil inhibits the startup screen.

64 65
This is for use in your personal init file (but NOT site-start.el),
once you are familiar with the contents of the startup screen."
Richard M. Stallman's avatar
Richard M. Stallman committed
66 67
  :type 'boolean
  :group 'initialization)
Jim Blandy's avatar
Jim Blandy committed
68

69 70
(defvaralias 'inhibit-splash-screen 'inhibit-startup-screen)
(defvaralias 'inhibit-startup-message 'inhibit-startup-screen)
71

72
(defvar startup-screen-inhibit-startup-screen nil)
73

Glenn Morris's avatar
Glenn Morris committed
74 75
;; FIXME? Why does this get such weirdly extreme treatment, when the
;; more important inhibit-startup-screen does not.
Richard M. Stallman's avatar
Richard M. Stallman committed
76
(defcustom inhibit-startup-echo-area-message nil
77
  "Non-nil inhibits the initial startup echo area message.
Richard M. Stallman's avatar
Richard M. Stallman committed
78 79
Setting this variable takes effect
only if you do it with the customization buffer
80
or if your init file contains a line of this form:
81
 (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
82 83
If your init file is byte-compiled, use the following form
instead:
84
 (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
85 86
Thus, someone else using a copy of your init file will see the
startup message unless he personally acts to inhibit it."
Richard M. Stallman's avatar
Richard M. Stallman committed
87 88 89
  :type '(choice (const :tag "Don't inhibit")
		 (string :tag "Enter your user name, to inhibit"))
  :group 'initialization)
90

Richard M. Stallman's avatar
Richard M. Stallman committed
91
(defcustom inhibit-default-init nil
92
  "Non-nil inhibits loading the `default' library."
Richard M. Stallman's avatar
Richard M. Stallman committed
93 94
  :type 'boolean
  :group 'initialization)
Jim Blandy's avatar
Jim Blandy committed
95

96
(defcustom inhibit-startup-buffer-menu nil
97
  "Non-nil inhibits display of buffer list when more than 2 files are loaded."
98 99 100
  :type 'boolean
  :group 'initialization)

101
(defvar command-switch-alist nil
Jim Blandy's avatar
Jim Blandy committed
102 103
  "Alist of command-line switches.
Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
Kenichi Handa's avatar
Kenichi Handa committed
104 105
HANDLER-FUNCTION receives the switch string as its sole argument;
the remaining command-line args are in the variable `command-line-args-left'.")
Jim Blandy's avatar
Jim Blandy committed
106

107 108 109
(defvar command-line-args-left nil
  "List of command-line args not yet processed.")

110 111 112 113 114
(defvaralias 'argv 'command-line-args-left
  "List of command-line args not yet processed.
This is a convenience alias, so that one can write \(pop argv\)
inside of --eval command line arguments in order to access
following arguments.")
115
(internal-make-var-non-special 'argv)
116

117 118 119
(defvar argi nil
  "Current command-line argument.")
(internal-make-var-non-special 'argi)
120

Jim Blandy's avatar
Jim Blandy committed
121 122 123
(defvar command-line-functions nil    ;; lrs 7/31/89
  "List of functions to process unrecognized command-line arguments.
Each function should access the dynamically bound variables
Richard M. Stallman's avatar
Richard M. Stallman committed
124
`argi' (the current argument) and `command-line-args-left' (the remaining
Jim Blandy's avatar
Jim Blandy committed
125
arguments).  The function should return non-nil only if it recognizes and
Richard M. Stallman's avatar
Richard M. Stallman committed
126 127
processes `argi'.  If it does so, it may consume successive arguments by
altering `command-line-args-left' to remove them.")
Jim Blandy's avatar
Jim Blandy committed
128

129 130 131 132
(defvar command-line-default-directory nil
  "Default directory to use for command line arguments.
This is normally copied from `default-directory' when Emacs starts.")

Stefan Monnier's avatar
Stefan Monnier committed
133 134
;; This is here, rather than in x-win.el, so that we can ignore these
;; options when we are not using X.
135
(defconst command-line-x-option-alist
136 137 138
  '(("-bw" 1 x-handle-numeric-switch border-width)
    ("-d" 1 x-handle-display)
    ("-display" 1 x-handle-display)
139
    ("-name" 1 x-handle-name-switch)
140 141
    ("-title" 1 x-handle-switch title)
    ("-T" 1 x-handle-switch title)
142 143 144 145 146 147
    ("-r" 0 x-handle-switch reverse t)
    ("-rv" 0 x-handle-switch reverse t)
    ("-reverse" 0 x-handle-switch reverse t)
    ("-reverse-video" 0 x-handle-switch reverse t)
    ("-fn" 1 x-handle-switch font)
    ("-font" 1 x-handle-switch font)
148 149 150
    ("-fs" 0 x-handle-initial-switch fullscreen fullboth)
    ("-fw" 0 x-handle-initial-switch fullscreen fullwidth)
    ("-fh" 0 x-handle-initial-switch fullscreen fullheight)
151
    ("-mm" 0 x-handle-initial-switch fullscreen maximized)
152
    ("-ib" 1 x-handle-numeric-switch internal-border-width)
153
    ("-g" 1 x-handle-geometry)
154
    ("-lsp" 1 x-handle-numeric-switch line-spacing)
155
    ("-geometry" 1 x-handle-geometry)
156 157 158 159 160
    ("-fg" 1 x-handle-switch foreground-color)
    ("-foreground" 1 x-handle-switch foreground-color)
    ("-bg" 1 x-handle-switch background-color)
    ("-background" 1 x-handle-switch background-color)
    ("-ms" 1 x-handle-switch mouse-color)
161
    ("-nbi" 0 x-handle-switch icon-type nil)
162 163 164 165 166 167 168 169
    ("-iconic" 0 x-handle-iconic)
    ("-xrm" 1 x-handle-xrm-switch)
    ("-cr" 1 x-handle-switch cursor-color)
    ("-vb" 0 x-handle-switch vertical-scroll-bars t)
    ("-hb" 0 x-handle-switch horizontal-scroll-bars t)
    ("-bd" 1 x-handle-switch)
    ("--border-width" 1 x-handle-numeric-switch border-width)
    ("--display" 1 x-handle-display)
170
    ("--name" 1 x-handle-name-switch)
171
    ("--title" 1 x-handle-switch title)
172 173
    ("--reverse-video" 0 x-handle-switch reverse t)
    ("--font" 1 x-handle-switch font)
174 175 176
    ("--fullscreen" 0 x-handle-initial-switch fullscreen fullboth)
    ("--fullwidth" 0 x-handle-initial-switch fullscreen fullwidth)
    ("--fullheight" 0 x-handle-initial-switch fullscreen fullheight)
177
    ("--maximized" 0 x-handle-initial-switch fullscreen maximized)
178
    ("--internal-border" 1 x-handle-numeric-switch internal-border-width)
179
    ("--geometry" 1 x-handle-geometry)
180 181 182
    ("--foreground-color" 1 x-handle-switch foreground-color)
    ("--background-color" 1 x-handle-switch background-color)
    ("--mouse-color" 1 x-handle-switch mouse-color)
183
    ("--no-bitmap-icon" 0 x-handle-no-bitmap-icon)
184 185 186 187
    ("--iconic" 0 x-handle-iconic)
    ("--xrm" 1 x-handle-xrm-switch)
    ("--cursor-color" 1 x-handle-switch cursor-color)
    ("--vertical-scroll-bars" 0 x-handle-switch vertical-scroll-bars t)
188
    ("--line-spacing" 1 x-handle-numeric-switch line-spacing)
189
    ("--border-color" 1 x-handle-switch border-color)
190 191
    ("--smid" 1 x-handle-smid)
    ("--parent-id" 1 x-handle-parent-id))
192 193 194 195 196 197 198 199 200
  "Alist of X Windows options.
Each element has the form
  (NAME NUMARGS HANDLER FRAME-PARAM VALUE)
where NAME is the option name string, NUMARGS is the number of arguments
that the option accepts, HANDLER is a function to call to handle the option.
FRAME-PARAM (optional) is the frame parameter this option specifies,
and VALUE is the value which is given to that frame parameter
\(most options use the argument for this, so VALUE is not present).")

201 202 203
(defconst command-line-ns-option-alist
  '(("-NSAutoLaunch" 1 ns-ignore-1-arg)
    ("-NXAutoLaunch" 1 ns-ignore-1-arg)
204
    ("-macosx" 0 ignore)
205 206 207 208 209 210 211 212 213 214 215
    ("-NSHost" 1 ns-ignore-1-arg)
    ("-_NSMachLaunch" 1 ns-ignore-1-arg)
    ("-MachLaunch" 1 ns-ignore-1-arg)
    ("-NXOpen" 1 ns-ignore-1-arg)
    ("-NSOpen" 1 ns-handle-nxopen)
    ("-NXOpenTemp" 1 ns-ignore-1-arg)
    ("-NSOpenTemp" 1 ns-handle-nxopentemp)
    ("-GSFilePath" 1 ns-handle-nxopen)
    ;;("-bw" .              x-handle-numeric-switch)
    ;;("-d" .               x-handle-display)
    ;;("-display" .         x-handle-display)
216 217 218 219 220 221 222 223 224
    ("-name" 1 x-handle-name-switch)
    ("-title" 1 x-handle-switch title)
    ("-T" 1 x-handle-switch title)
    ("-r" 0 x-handle-switch reverse t)
    ("-rv" 0 x-handle-switch reverse t)
    ("-reverse" 0 x-handle-switch reverse t)
    ("-fn" 1 x-handle-switch font)
    ("-font" 1 x-handle-switch font)
    ("-ib" 1 x-handle-numeric-switch internal-border-width)
225 226
    ("-g" 1 x-handle-geometry)
    ("-geometry" 1 x-handle-geometry)
227 228 229 230 231 232 233 234
    ("-fg" 1 x-handle-switch foreground-color)
    ("-foreground" 1 x-handle-switch foreground-color)
    ("-bg" 1 x-handle-switch background-color)
    ("-background" 1 x-handle-switch background-color)
;    ("-ms" 1 x-handle-switch mouse-color)
    ("-itype" 0 x-handle-switch icon-type t)
    ("-i" 0 x-handle-switch icon-type t)
    ("-iconic" 0 x-handle-iconic icon-type t)
235
    ;;("-xrm" .             x-handle-xrm-switch)
236 237 238 239 240
    ("-cr" 1 x-handle-switch cursor-color)
    ("-vb" 0 x-handle-switch vertical-scroll-bars t)
    ("-hb" 0 x-handle-switch horizontal-scroll-bars t)
    ("-bd" 1 x-handle-switch)
    ;; ("--border-width" 1 x-handle-numeric-switch border-width)
241
    ;; ("--display" 1 ns-handle-display)
242 243 244 245 246
    ("--name" 1 x-handle-name-switch)
    ("--title" 1 x-handle-switch title)
    ("--reverse-video" 0 x-handle-switch reverse t)
    ("--font" 1 x-handle-switch font)
    ("--internal-border" 1 x-handle-numeric-switch internal-border-width)
247
    ;; ("--geometry" 1 ns-handle-geometry)
248 249 250 251 252
    ("--foreground-color" 1 x-handle-switch foreground-color)
    ("--background-color" 1 x-handle-switch background-color)
    ("--mouse-color" 1 x-handle-switch mouse-color)
    ("--icon-type" 0 x-handle-switch icon-type t)
    ("--iconic" 0 x-handle-iconic)
253
    ;; ("--xrm" 1 ns-handle-xrm-switch)
254 255 256
    ("--cursor-color" 1 x-handle-switch cursor-color)
    ("--vertical-scroll-bars" 0 x-handle-switch vertical-scroll-bars t)
    ("--border-color" 1 x-handle-switch border-width))
257 258 259 260 261 262 263 264 265 266
  "Alist of NS options.
Each element has the form
  (NAME NUMARGS HANDLER FRAME-PARAM VALUE)
where NAME is the option name string, NUMARGS is the number of arguments
that the option accepts, HANDLER is a function to call to handle the option.
FRAME-PARAM (optional) is the frame parameter this option specifies,
and VALUE is the value which is given to that frame parameter
\(most options use the argument for this, so VALUE is not present).")


Roland McGrath's avatar
Roland McGrath committed
267
(defvar before-init-hook nil
268
  "Normal hook run after handling urgent options but before loading init files.")
Jim Blandy's avatar
Jim Blandy committed
269

Roland McGrath's avatar
Roland McGrath committed
270
(defvar after-init-hook nil
271 272 273 274 275 276 277 278
  "Normal hook run after initializing the Emacs session.
It is run after Emacs loads the init file, `default' library, the
abbrevs file, and additional Lisp packages (if any), and setting
the value of `after-init-time'.

There is no `condition-case' around the running of this hook;
therefore, if `debug-on-error' is non-nil, an error in one of
these functions will invoke the debugger.")
279 280 281

(defvar emacs-startup-hook nil
  "Normal hook run after loading init files and handling the command line.")
Roland McGrath's avatar
Roland McGrath committed
282

Jim Blandy's avatar
Jim Blandy committed
283
(defvar term-setup-hook nil
284 285
  "Normal hook run after loading terminal-specific Lisp code.
It also follows `emacs-startup-hook'.  This hook exists for users to set,
Jim Blandy's avatar
Jim Blandy committed
286 287 288
so as to override the definitions made by the terminal-specific file.
Emacs never sets this variable itself.")

289 290 291 292
(defvar inhibit-startup-hooks nil
  "Non-nil means don't run `term-setup-hook' and `emacs-startup-hook'.
This is because we already did so.")

Jim Blandy's avatar
Jim Blandy committed
293
(defvar keyboard-type nil
Richard M. Stallman's avatar
Richard M. Stallman committed
294
  "The brand of keyboard you are using.
295 296 297
This variable is used to define the proper function and keypad
keys for use under X.  It is used in a fashion analogous to the
environment variable TERM.")
Jim Blandy's avatar
Jim Blandy committed
298 299

(defvar window-setup-hook nil
Richard M. Stallman's avatar
Richard M. Stallman committed
300 301 302
  "Normal hook run to initialize window system display.
Emacs runs this hook after processing the command line arguments and loading
the user's init file.")
Jim Blandy's avatar
Jim Blandy committed
303

Richard M. Stallman's avatar
Richard M. Stallman committed
304
(defcustom initial-major-mode 'lisp-interaction-mode
305
  "Major mode command symbol to use for the initial `*scratch*' buffer."
306
  :type 'function
Richard M. Stallman's avatar
Richard M. Stallman committed
307
  :group 'initialization)
Jim Blandy's avatar
Jim Blandy committed
308

309
(defvar init-file-user nil
310
  "Identity of user whose init file is or was read.
311 312 313
The value is nil if `-q' or `--no-init-file' was specified,
meaning do not load any init file.

Eli Zaretskii's avatar
Eli Zaretskii committed
314 315 316 317
Otherwise, the value may be an empty string, meaning
use the init file for the user who originally logged in,
or it may be a string containing a user's name meaning
use that person's init file.
Jim Blandy's avatar
Jim Blandy committed
318

Karl Heuer's avatar
Karl Heuer committed
319
In either of the latter cases, `(concat \"~\" init-file-user \"/\")'
320
evaluates to the name of the directory where the init file was
321 322 323
looked for.

Setting `init-file-user' does not prevent Emacs from loading
324
`site-start.el'.  The only way to do that is to use `--no-site-file'.")
Jim Blandy's avatar
Jim Blandy committed
325

326
(defcustom site-run-file (purecopy "site-start")
327 328 329
  "File containing site-wide run-time initializations.
This file is loaded at run-time before `~/.emacs'.  It contains inits
that need to be in place for the entire site, but which, due to their
330
higher incidence of change, don't make sense to load into Emacs's
331
dumped image.  Thus, the run-time load order is: 1. file described in
332 333 334 335 336 337 338
this variable, if non-nil; 2. `~/.emacs'; 3. `default.el'.

Don't use the `site-start.el' file for things some users may not like.
Put them in `default.el' instead, so that users can more easily
override them.  Users can prevent loading `default.el' with the `-q'
option or by setting `inhibit-default-init' in their own init files,
but inhibiting `site-start.el' requires `--no-site-file', which
339 340 341 342 343
is less convenient.

This variable is defined for customization so as to make
it visible in the relevant context.  However, actually customizing it
is not allowed, since it would not work anyway.  The only way to set
344
this variable usefully is to set it while building and dumping Emacs."
345
  :type '(choice (const :tag "none" nil) string)
346 347
  :group 'initialization
  :initialize 'custom-initialize-default
348
  :set (lambda (_variable _value)
349
	  (error "Customizing `site-run-file' does not work")))
350

Richard M. Stallman's avatar
Richard M. Stallman committed
351
(defcustom mail-host-address nil
352 353 354
  "Name of this machine, for purposes of naming users.
If non-nil, Emacs uses this instead of `system-name' when constructing
email addresses."
Richard M. Stallman's avatar
Richard M. Stallman committed
355 356
  :type '(choice (const nil) string)
  :group 'mail)
357

358
(defcustom user-mail-address (if command-line-processed
359 360 361 362
				 (or (getenv "EMAIL")
				     (concat (user-login-name) "@"
					     (or mail-host-address
						 (system-name))))
363 364
			       ;; Empty string means "not set yet".
			       "")
365
  "Full mailing address of this user.
366
This is initialized with environment variable `EMAIL' or, as a
367
fallback, using `mail-host-address'.  This is done after your
368
init file is read, in case it sets `mail-host-address'."
Richard M. Stallman's avatar
Richard M. Stallman committed
369 370
  :type 'string
  :group 'mail)
371

Richard M. Stallman's avatar
Richard M. Stallman committed
372
(defcustom auto-save-list-file-prefix
373 374
  (cond ((eq system-type 'ms-dos)
	 ;; MS-DOS cannot have initial dot, and allows only 8.3 names
375
	 (concat user-emacs-directory "auto-save.list/_s"))
376
	(t
377
	 (concat user-emacs-directory "auto-save-list/.saves-")))
378
  "Prefix for generating `auto-save-list-file-name'.
379
This is used after reading your init file to initialize
380 381
`auto-save-list-file-name', by appending Emacs's pid and the system name,
if you have not already set `auto-save-list-file-name' yourself.
382
Directories in the prefix will be created if necessary.
383
Set this to nil if you want to prevent `auto-save-list-file-name'
Richard M. Stallman's avatar
Richard M. Stallman committed
384
from being initialized."
385 386
  :type '(choice (const :tag "Don't record a session's auto save list" nil)
		 string)
Richard M. Stallman's avatar
Richard M. Stallman committed
387
  :group 'auto-save)
388

389 390
(defvar emacs-basic-display nil)

Jim Blandy's avatar
Jim Blandy committed
391 392
(defvar init-file-debug nil)

393 394
(defvar init-file-had-error nil
  "Non-nil if there was an error loading the user's init file.")
395

396 397
(defvar normal-top-level-add-subdirs-inode-list nil)

398 399
(defvar no-blinking-cursor nil)

400 401 402
(defvar pure-space-overflow nil
  "Non-nil if building Emacs overflowed pure space.")

403
(defvar pure-space-overflow-message (purecopy "\
404
Warning Warning!!!  Pure space overflow    !!!Warning Warning
405
\(See the node Pure Storage in the Lisp manual for details.)\n"))
406

407 408 409 410 411 412
(defcustom tutorial-directory
  (file-name-as-directory (expand-file-name "tutorials" data-directory))
  "Directory containing the Emacs TUTORIAL files."
  :group 'installation
  :type 'directory
  :initialize 'custom-initialize-delay)
413

414 415 416 417 418 419 420
(defvar package--builtin-versions
  ;; Mostly populated by loaddefs.el via autoload-builtin-package-versions.
  (purecopy `((emacs . ,(version-to-list emacs-version))))
  "Alist giving the version of each versioned builtin package.
I.e. each element of the list is of the form (NAME . VERSION) where
NAME is the package name as a symbol, and VERSION is its version
as a list.")
421

422 423 424
(defun package--description-file (dir)
  (concat (let ((subdir (file-name-nondirectory
                         (directory-file-name dir))))
425
            (if (string-match "\\([^.].*?\\)-\\([0-9]+\\(?:[.][0-9]+\\|\\(?:pre\\|beta\\|alpha\\)[0-9]+\\)*\\)" subdir)
426 427 428
                (match-string 1 subdir) subdir))
          "-pkg.el"))

429
(defun normal-top-level-add-subdirs-to-load-path ()
430
  "Add all subdirectories of `default-directory' to `load-path'.
431
More precisely, this uses only the subdirectories whose names
432 433
start with letters or digits; it excludes any subdirectory named `RCS'
or `CVS', and any subdirectory that contains a file named `.nosearch'."
434
  (let (dirs
435
	attrs
436 437 438 439
	(pending (list default-directory)))
    ;; This loop does a breadth-first tree walk on DIR's subtree,
    ;; putting each subdir into DIRS as its contents are examined.
    (while pending
440
      (push (pop pending) dirs)
441 442 443
      (let* ((this-dir (car dirs))
	     (contents (directory-files this-dir))
	     (default-directory this-dir)
444 445
	     (canonicalized (if (fboundp 'w32-untranslated-canonical-name)
				(w32-untranslated-canonical-name this-dir))))
Glenn Morris's avatar
Glenn Morris committed
446 447
	;; The Windows version doesn't report meaningful inode numbers, so
	;; use the canonicalized absolute file name of the directory instead.
448 449
	(setq attrs (or canonicalized
			(nthcdr 10 (file-attributes this-dir))))
450
	(unless (member attrs normal-top-level-add-subdirs-inode-list)
451 452
	  (push attrs normal-top-level-add-subdirs-inode-list)
	  (dolist (file contents)
Glenn Morris's avatar
Glenn Morris committed
453 454 455 456 457 458 459 460 461 462
	    (and (string-match "\\`[[:alnum:]]" file)
		 ;; The lower-case variants of RCS and CVS are for DOS/Windows.
		 (not (member file '("RCS" "CVS" "rcs" "cvs")))
		 ;; Avoid doing a `stat' when it isn't necessary because
		 ;; that can cause trouble when an NFS server is down.
		 (not (string-match "\\.elc?\\'" file))
		 (file-directory-p file)
		 (let ((expanded (expand-file-name file)))
		   (or (file-exists-p (expand-file-name ".nosearch" expanded))
		       (setq pending (nconc pending (list expanded))))))))))
463
    (normal-top-level-add-to-load-path (cdr (nreverse dirs)))))
464

465
(defun normal-top-level-add-to-load-path (dirs)
Glenn Morris's avatar
Glenn Morris committed
466 467 468 469 470
  "This function is called from a subdirs.el file.
It assumes that `default-directory' is the directory in which the
subdirs.el file exists, and it adds to `load-path' the subdirs of
that directory as specified in DIRS.  Normally the elements of
DIRS are relative."
471 472 473
  (let ((tail load-path)
	(thisdir (directory-file-name default-directory)))
    (while (and tail
474 475
		;;Don't go all the way to the nil terminator.
		(cdr tail)
476 477 478 479
		(not (equal thisdir (car tail)))
		(not (and (memq system-type '(ms-dos windows-nt))
			  (equal (downcase thisdir) (downcase (car tail))))))
      (setq tail (cdr tail)))
480 481 482
    ;;Splice the new section in.
    (when tail
      (setcdr tail (append (mapcar 'expand-file-name dirs) (cdr tail))))))
483

Jim Blandy's avatar
Jim Blandy committed
484
(defun normal-top-level ()
Glenn Morris's avatar
Glenn Morris committed
485 486 487 488
  "Emacs calls this function when it first starts up.
It sets `command-line-processed', processes the command-line,
reads the initialization files, etc.
It is the default value of the variable `top-level'."
Jim Blandy's avatar
Jim Blandy committed
489 490 491
  (if command-line-processed
      (message "Back to top level.")
    (setq command-line-processed t)
492

493 494 495 496 497 498 499 500 501 502 503 504 505 506
    ;; Look in each dir in load-path for a subdirs.el file.  If we
    ;; find one, load it, which will add the appropriate subdirs of
    ;; that dir into load-path.  This needs to be done before setting
    ;; the locale environment, because the latter might need to load
    ;; some support files.
    ;; Look for a leim-list.el file too.  Loading it will register
    ;; available input methods.
    (let ((tail load-path)
          (lispdir (expand-file-name "../lisp" data-directory))
          dir)
      (while tail
        (setq dir (car tail))
        (let ((default-directory dir))
          (load (expand-file-name "subdirs.el") t t t))
Glenn Morris's avatar
Glenn Morris committed
507 508
        ;; Do not scan standard directories that won't contain a leim-list.el.
        ;; http://lists.gnu.org/archive/html/emacs-devel/2009-10/msg00502.html
509
        ;; (Except the preloaded one in lisp/leim.)
Glenn Morris's avatar
Glenn Morris committed
510 511 512
        (or (string-prefix-p lispdir dir)
            (let ((default-directory dir))
              (load (expand-file-name "leim-list.el") t t t)))
513 514 515 516 517
        ;; We don't use a dolist loop and we put this "setq-cdr" command at
        ;; the end, because the subdirs.el files may add elements to the end
        ;; of load-path and we want to take it into account.
        (setq tail (cdr tail))))

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
    ;; Set the default strings to display in mode line for end-of-line
    ;; formats that aren't native to this platform.  This should be
    ;; done before calling set-locale-environment, as the latter might
    ;; use these mnemonics.
    (cond
     ((memq system-type '(ms-dos windows-nt))
      (setq eol-mnemonic-unix "(Unix)"
	    eol-mnemonic-mac  "(Mac)"))
     (t                                   ; this is for Unix/GNU/Linux systems
      (setq eol-mnemonic-dos  "(DOS)"
	    eol-mnemonic-mac  "(Mac)")))

    (set-locale-environment nil)
    ;; Decode all default-directory's (probably, only *scratch* exists
    ;; at this point).  default-directory of *scratch* is the basis
    ;; for many other file-name variables and directory lists, so it
    ;; is important to decode it ASAP.
    (when locale-coding-system
      (save-excursion
	(dolist (elt (buffer-list))
	  (set-buffer elt)
	  (if default-directory
	      (setq default-directory
		    (decode-coding-string default-directory
					  locale-coding-system t)))))

      ;; Decode all the important variables and directory lists, now
      ;; that we know the locale's encoding.  This is because the
      ;; values of these variables are until here unibyte undecoded
      ;; strings created by build_unibyte_string.  data-directory in
      ;; particular is used to construct many other standard directory
      ;; names, so it must be decoded ASAP.
      ;; Note that charset-map-path cannot be decoded here, since we
      ;; could then be trapped in infinite recursion below, when we
      ;; load subdirs.el, because encoding a directory name might need
      ;; to load a charset map, which will want to encode
      ;; charset-map-path, which will want to load the same charset
      ;; map...  So decoding of charset-map-path is delayed until
      ;; further down below.
      (dolist (pathsym '(load-path exec-path))
	(let ((path (symbol-value pathsym)))
	  (if (listp path)
	      (set pathsym (mapcar (lambda (dir)
				     (decode-coding-string
				      dir
				      locale-coding-system t))
				path)))))
      (dolist (filesym '(data-directory doc-directory exec-directory
					installation-directory
					invocation-directory invocation-name
					source-directory
					shared-game-score-directory))
	(let ((file (symbol-value filesym)))
	  (if (stringp file)
	      (set filesym (decode-coding-string file locale-coding-system t))))))

574
    (let ((dir default-directory))
575
      (with-current-buffer "*Messages*"
576
        (messages-buffer-mode)
577 578 579 580
        ;; Make it easy to do like "tail -f".
        (set (make-local-variable 'window-point-insertion-type) t)
        ;; Give *Messages* the same default-directory as *scratch*,
        ;; just to keep things predictable.
581
	(setq default-directory dir)))
582 583 584
    ;; `user-full-name' is now known; reset its standard-value here.
    (put 'user-full-name 'standard-value
	 (list (default-value 'user-full-name)))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
585 586 587 588 589 590 591 592 593 594 595 596 597
    ;; If the PWD environment variable isn't accurate, delete it.
    (let ((pwd (getenv "PWD")))
      (and (stringp pwd)
	   ;; Use FOO/., so that if FOO is a symlink, file-attributes
	   ;; describes the directory linked to, not FOO itself.
	   (or (equal (file-attributes
		       (concat (file-name-as-directory pwd) "."))
		      (file-attributes
		       (concat (file-name-as-directory default-directory)
			       ".")))
	       (setq process-environment
		     (delete (concat "PWD=" pwd)
			     process-environment)))))
598 599 600 601 602 603 604 605 606 607
    ;; Now, that other directories were searched, and any charsets we
    ;; need for encoding them are already loaded, we are ready to
    ;; decode charset-map-path.
    (if (listp charset-map-path)
	(setq charset-map-path
	      (mapcar (lambda (dir)
			(decode-coding-string
			 dir
			 locale-coding-system t))
		      charset-map-path)))
Jim Blandy's avatar
Jim Blandy committed
608
    (setq default-directory (abbreviate-file-name default-directory))
609
    (let ((old-face-font-rescale-alist face-font-rescale-alist))
610 611 612 613
      (unwind-protect
	  (command-line)
	;; Do this again, in case .emacs defined more abbreviations.
	(setq default-directory (abbreviate-file-name default-directory))
614 615
	;; Specify the file for recording all the auto save files of this session.
	;; This is used by recover-session.
616 617 618
	(or auto-save-list-file-name
	    (and auto-save-list-file-prefix
		 (setq auto-save-list-file-name
619 620
		       ;; Under MS-DOS our PID is almost always reused between
		       ;; Emacs invocations.  We need something more unique.
621 622 623 624 625 626
		       (cond ((eq system-type 'ms-dos)
			      ;; We are going to access the auto-save
			      ;; directory, so make sure it exists.
			      (make-directory
			       (file-name-directory auto-save-list-file-prefix)
			       t)
627
			      (concat
628 629 630 631 632 633 634 635 636 637
			       (make-temp-name
				(expand-file-name
				 auto-save-list-file-prefix))
			       "~"))
			     (t
			      (expand-file-name
			       (format "%s%d-%s~"
				       auto-save-list-file-prefix
				       (emacs-pid)
				       (system-name))))))))
638 639 640 641
	(unless inhibit-startup-hooks
	  (run-hooks 'emacs-startup-hook)
	  (and term-setup-hook
	       (run-hooks 'term-setup-hook)))
642 643 644

	;; Don't do this if we failed to create the initial frame,
	;; for instance due to a dense colormap.
645 646
	(when (or frame-initial-frame
		  ;; If frame-initial-frame has no meaning, do this anyway.
647
		  (not (and initial-window-system
648
			    (not noninteractive)
649
			    (not (eq initial-window-system 'pc)))))
650 651 652 653 654 655 656 657 658 659 660 661 662

	  ;; FIXME: The user's init file may change
	  ;; face-font-rescale-alist.  However, the default face
	  ;; already has an assigned font object, which does not take
	  ;; face-font-rescale-alist into account.  For such
	  ;; situations, we ought to have a way to find all font
	  ;; objects and regenerate them; currently we do not.  As a
	  ;; workaround, we specifically reset te default face's :font
	  ;; attribute here.  See bug#1785.
	  (unless (eq face-font-rescale-alist
		      old-face-font-rescale-alist)
	    (set-face-attribute 'default nil :font (font-spec)))

663 664 665 666
	  ;; Modify the initial frame based on what .emacs puts into
	  ;; ...-frame-alist.
	  (if (fboundp 'frame-notice-user-settings)
	      (frame-notice-user-settings))
667 668 669 670
	  ;; Set the faces for the initial background mode even if
	  ;; frame-notice-user-settings didn't (such as on a tty).
	  ;; frame-set-background-mode is idempotent, so it won't
	  ;; cause any harm if it's already been done.
671
	  (if (fboundp 'frame-set-background-mode)
672
	      (frame-set-background-mode (selected-frame))))
Miles Bader's avatar
Miles Bader committed
673

674 675 676 677
	;; Now we know the user's default font, so add it to the menu.
	(if (fboundp 'font-menu-add-default)
	    (font-menu-add-default))
	(and window-setup-hook
678
	     (run-hooks 'window-setup-hook))))
679 680 681 682
    ;; Subprocesses of Emacs do not have direct access to the terminal, so
    ;; unless told otherwise they should only assume a dumb terminal.
    ;; We are careful to do it late (after term-setup-hook), although the
    ;; new multi-tty code does not use $TERM any more there anyway.
683 684 685 686 687 688 689 690 691 692 693 694 695 696
    (setenv "TERM" "dumb")
    ;; Remove DISPLAY from the process-environment as well.  This allows
    ;; `callproc.c' to give it a useful adaptive default which is either
    ;; the value of the `display' frame-parameter or the DISPLAY value
    ;; from initial-environment.
    (let ((display (frame-parameter nil 'display)))
      ;; Be careful which DISPLAY to remove from process-environment: follow
      ;; the logic of `callproc.c'.
      (if (stringp display) (setq display (concat "DISPLAY=" display))
        (dolist (varval initial-environment)
          (if (string-match "\\`DISPLAY=" varval)
              (setq display varval))))
      (when display
        (delete display process-environment)))))
697 698

;; Precompute the keyboard equivalents in the menu bar items.
699 700
;; Command-line options supported by tty's:
(defconst tty-long-option-alist
701 702 703
  '(("--name"		  . "-name")
    ("--title"		  . "-T")
    ("--reverse-video"	  . "-reverse")
704
    ("--foreground-color" . "-fg")
705 706
    ("--background-color" . "-bg")
    ("--color"		  . "-color")))
707

708
(defconst tool-bar-images-pixel-height 24
709
  "Height in pixels of images in the tool-bar.")
710

711 712 713
(defvar tool-bar-originally-present nil
  "Non-nil if tool-bars are present before user and site init files are read.")

714 715 716 717 718 719 720 721 722 723 724 725
(defvar handle-args-function-alist '((nil . tty-handle-args))
  "Functions for processing window-system dependent command-line arguments.
Window system startup files should add their own function to this
alist, which should parse the command line arguments.  Those
pertaining to the window system should be processed and removed
from the returned command line.")

(defvar window-system-initialization-alist '((nil . ignore))
  "Alist of window-system initialization functions.
Window-system startup files should add their own initialization
function to this list.  The function should take no arguments,
and initialize the window system environment to prepare for
726
opening the first frame (e.g. open a connection to an X server).")
727

728
(defun tty-handle-args (args)
Glenn Morris's avatar
Glenn Morris committed
729
  "Handle the X-like command-line arguments \"-fg\", \"-bg\", \"-name\", etc."
730
  (let (rest)
731
    (message "%S" args)
732 733
    (while (and args
		(not (equal (car args) "--")))
734 735 736
      (let* ((argi (pop args))
	     (orig-argi argi)
	     argval completion)
737 738
	;; Check for long options with attached arguments
	;; and separate out the attached option argument into argval.
739 740 741 742 743
	(when (string-match "^\\(--[^=]*\\)=" argi)
          (setq argval (substring argi (match-end 0))
                argi (match-string 1 argi)))
	(when (string-match "^--" argi)
	  (setq completion (try-completion argi tty-long-option-alist))
744 745
	  (if (eq completion t)
	      ;; Exact match for long option.
746
	      (setq argi (cdr (assoc argi tty-long-option-alist)))
747 748 749 750
	    (if (stringp completion)
		(let ((elt (assoc completion tty-long-option-alist)))
		  ;; Check for abbreviated long option.
		  (or elt
751 752
		      (error "Option `%s' is ambiguous" argi))
		  (setq argi (cdr elt)))
753
	      ;; Check for a short option.
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	      (setq argval nil
                    argi orig-argi))))
	(cond ((member argi '("-fg" "-foreground"))
	       (push (cons 'foreground-color (or argval (pop args)))
                     default-frame-alist))
	      ((member argi '("-bg" "-background"))
	       (push (cons 'background-color (or argval (pop args)))
                     default-frame-alist))
	      ((member argi '("-T" "-name"))
	       (unless argval (setq argval (pop args)))
	       (push (cons 'title
                           (if (stringp argval)
                               argval
                             (let ((case-fold-search t)
                                   i)
                               (setq argval (invocation-name))

                               ;; Change any . or * characters in name to
                               ;; hyphens, so as to emulate behavior on X.
                               (while
                                   (setq i (string-match "[.*]" argval))
                                 (aset argval i ?-))
                               argval)))
                     default-frame-alist))
	      ((member argi '("-r" "-rv" "-reverse"))
	       (push '(reverse . t)
                     default-frame-alist))
	      ((equal argi "-color")
	       (unless argval (setq argval 8)) ; default --color means 8 ANSI colors
	       (push (cons 'tty-color-mode
                           (cond
                            ((numberp argval) argval)
                            ((string-match "-?[0-9]+" argval)
                             (string-to-number argval))
                            (t (intern argval))))
                     default-frame-alist))
	      (t
               (push argi rest)))))
792
    (nconc (nreverse rest) args)))
793

794 795 796
(declare-function x-get-resource "frame.c"
		  (attribute class &optional component subclass))
(declare-function tool-bar-mode "tool-bar" (&optional arg))
797
(declare-function tool-bar-setup "tool-bar")
798

799
(defvar server-name)
800
(defvar server-process)
801

Jim Blandy's avatar
Jim Blandy committed
802
(defun command-line ()
Glenn Morris's avatar
Glenn Morris committed
803 804
  "A subroutine of `normal-top-level'.
Amongst another things, it parses the command-line arguments."
805
  (setq before-init-time (current-time)
806
	after-init-time nil
807
        command-line-default-directory default-directory)
808

809 810
  ;; Force recomputation, in case it was computed during the dump.
  (setq abbreviated-home-dir nil)
811

Roland McGrath's avatar
Roland McGrath committed
812
  ;; See if we should import version-control from the environment variable.
Jim Blandy's avatar
Jim Blandy committed
813 814
  (let ((vc (getenv "VERSION_CONTROL")))
    (cond ((eq vc nil))			;don't do anything if not set
815
	  ((member vc '("t" "numbered"))
Jim Blandy's avatar
Jim Blandy committed
816
	   (setq version-control t))
817
	  ((member vc '("nil" "existing"))
Jim Blandy's avatar
Jim Blandy committed
818
	   (setq version-control nil))
819
	  ((member vc '("never" "simple"))
Jim Blandy's avatar
Jim Blandy committed
820 821
	   (setq version-control 'never))))

Jim Blandy's avatar
Jim Blandy committed
822 823 824 825 826
  ;;! This has been commented out; I currently find the behavior when
  ;;! split-window-keep-point is nil disturbing, but if I can get used
  ;;! to it, then it would be better to eliminate the option.
  ;;! ;; Choose a good default value for split-window-keep-point.
  ;;! (setq split-window-keep-point (> baud-rate 2400))
Jim Blandy's avatar
Jim Blandy committed
827

828 829
  ;; Convert preloaded file names in load-history to absolute.
  (let ((simple-file-name
830 831
	 ;; Look for simple.el or simple.elc and use their directory
	 ;; as the place where all Lisp files live.
832 833 834
	 (locate-file "simple" load-path (get-load-suffixes)))
	lisp-dir)
    ;; Don't abort if simple.el cannot be found, but print a warning.
835 836
    ;; Although in most usage we are going to cryptically abort a moment
    ;; later anyway, due to missing required bidi data files (eg bug#13430).
837
    (if (null simple-file-name)
838 839 840 841 842 843 844 845 846 847 848
	(let ((standard-output 'external-debugging-output)
	      (lispdir (expand-file-name "../lisp" data-directory)))
	  (princ "Warning: Could not find simple.el or simple.elc")
	  (terpri)
	  (when (getenv "EMACSLOADPATH")
	    (princ "The EMACSLOADPATH environment variable is set, \
please check its value")
	    (terpri))
	  (unless (file-readable-p lispdir)
	    (princ (format "Lisp directory %s not readable?" lispdir))
	    (terpri)))
849 850 851 852 853 854 855 856 857 858
      (setq lisp-dir (file-truename (file-name-directory simple-file-name)))
      (setq load-history
	    (mapcar (lambda (elt)
		      (if (and (stringp (car elt))
			       (not (file-name-absolute-p (car elt))))
			  (cons (concat lisp-dir
					(car elt))
				(cdr elt))
			elt))
		    load-history))))
859

860
  ;; Convert the arguments to Emacs internal representation.
861
  (let ((args command-line-args))
862 863 864
    (while args
      (setcar args
	      (decode-coding-string (car args) locale-coding-system t))
865
      (pop args)))
866

Jim Blandy's avatar
Jim Blandy committed
867
  (let ((done nil)
868 869
	(args (cdr command-line-args))
	display-arg)
Jim Blandy's avatar
Jim Blandy committed
870

Jim Blandy's avatar
Jim Blandy committed
871 872 873 874
    ;; Figure out which user's init file to load,
    ;; either from the environment or from the options.
    (setq init-file-user (if noninteractive nil (user-login-name)))
    ;; If user has not done su, use current $HOME to find .emacs.
875 876
    (and init-file-user
         (equal init-file-user (user-real-login-name))
Jim Blandy's avatar
Jim Blandy committed
877
	 (setq init-file-user ""))
Jim Blandy's avatar
Jim Blandy committed
878 879 880 881

    ;; Process the command-line args, and delete the arguments
    ;; processed.  This is consistent with the way main in emacs.c
    ;; does things.
Jim Blandy's avatar
Jim Blandy committed
882
    (while (and (not done) args)
883 884 885
      (let* ((longopts '(("--no-init-file") ("--no-site-file") ("--debug-init")
                         ("--user") ("--iconic") ("--icon-type") ("--quick")
			 ("--no-blinking-cursor") ("--basic-display")))
886 887 888
             (argi (pop args))
             (orig-argi argi)
             argval)
889
	;; Handle --OPTION=VALUE format.
890
	(when (string-match "\\`\\(--[^=]*\\)=" argi)
891
	  (setq argval (substring argi (match-end 0))
892
                argi (match-string 1 argi)))
893
	(when (string-match "\\`--." orig-argi)
894
	  (let ((completion (try-completion argi longopts)))
895 896 897 898 899 900 901 902 903 904
	    (cond ((eq completion t)
		   (setq argi (substring argi 1)))
		  ((stringp completion)
		   (let ((elt (assoc completion longopts)))
		     (unless elt
		       (error "Option `%s' is ambiguous" argi))
		     (setq argi (substring (car elt) 1))))
		  (t
		   (setq argval nil
			 argi orig-argi)))))
Jim Blandy's avatar
Jim Blandy committed
905
	(cond
906 907 908 909 910
	 ;; The --display arg is handled partly in C, partly in Lisp.
	 ;; When it shows up here, we just put it back to be handled
	 ;; by `command-line-1'.
	 ((member argi '("-d" "-display"))
	  (setq display-arg (list argi (pop args))))
911
	 ((member argi '("-Q" "-quick"))
Kenichi Handa's avatar
Kenichi Handa committed
912 913
	  (setq init-file-user nil
		site-run-file nil
914
		inhibit-x-resources t))
915 916 917
	 ((member argi '("-D" "-basic-display"))
	  (setq no-blinking-cursor t
		emacs-basic-display t)
Kenichi Handa's avatar
Kenichi Handa committed
918
	  (push '(vertical-scroll-bars . nil) initial-frame-alist))
919 920 921
	 ((member argi '("-q" "-no-init-file"))
	  (setq init-file-user nil))
	 ((member argi '("-u" "-user"))
922
	  (setq init-file-user (or argval (pop args))
923
		argval nil))
924
	 ((equal argi "-no-site-file")
925
	  (setq site-run-file nil))
926
	 ((equal argi "-debug-init")
927
	  (setq init-file-debug t))
928
	 ((equal argi "-iconic")
929
	  (push '(visibility . icon) initial-frame-alist))
930 931
	 ((member argi '("-nbc" "-no-blinking-cursor"))
	  (setq no-blinking-cursor t))
932
	 ;; Push the popped arg back on the list of arguments.
933 934 935
	 (t
          (push argi args)
          (setq done t)))
936 937 938 939
	;; Was argval set but not used?
	(and argval
	     (error "Option `%s' doesn't allow an argument" argi))))

940 941 942
    ;; Re-attach the --display arg.
    (and display-arg (setq args (append display-arg args)))

Jim Blandy's avatar
Jim Blandy committed
943
    ;; Re-attach the program name to the front of the arg list.
944 945
    (and command-line-args
         (setcdr command-line-args args)))
Jim Blandy's avatar
Jim Blandy committed
946

947 948 949 950 951 952 953 954 955 956 957 958