startup.el 89.3 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1 2
;;; startup.el --- process Emacs shell arguments

3
;; Copyright (C) 1985, 1986, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5
;;   Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
6

Eric S. Raymond's avatar
Eric S. Raymond committed
7
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
8
;; Keywords: internal
Eric S. Raymond's avatar
Eric S. Raymond committed
9

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

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

;; 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
23
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Jim Blandy's avatar
Jim Blandy committed
24

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

Kenichi Handa's avatar
Kenichi Handa committed
27 28 29
;; 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
30

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

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

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

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

(defcustom initial-buffer-choice nil
  "Buffer to show after starting Emacs.
44
If the value is nil and `inhibit-startup-screen' is nil, show the
45 46 47
startup screen.  If the value is string, visit the specified file or
directory using `find-file'.  If t, open the `*scratch*' buffer."
  :type '(choice
48
	  (const     :tag "Startup screen" nil)
49
	  (directory :tag "Directory" :value "~/")
50
	  (file      :tag "File" :value "~/.emacs")
51 52 53
	  (const     :tag "Lisp scratch buffer" t))
  :version "23.1"
  :group 'initialization)
Richard M. Stallman's avatar
Richard M. Stallman committed
54

55
(defcustom inhibit-startup-screen nil
56 57
  "Non-nil inhibits the startup screen.

58 59
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
60 61
  :type 'boolean
  :group 'initialization)
Jim Blandy's avatar
Jim Blandy committed
62

63 64
(defvaralias 'inhibit-splash-screen 'inhibit-startup-screen)
(defvaralias 'inhibit-startup-message 'inhibit-startup-screen)
65

66
(defvar startup-screen-inhibit-startup-screen nil)
67

Richard M. Stallman's avatar
Richard M. Stallman committed
68
(defcustom inhibit-startup-echo-area-message nil
69
  "Non-nil inhibits the initial startup echo area message.
Richard M. Stallman's avatar
Richard M. Stallman committed
70 71
Setting this variable takes effect
only if you do it with the customization buffer
Karl Heuer's avatar
Karl Heuer committed
72
or if your `.emacs' file contains a line of this form:
73
 (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
74 75
If your `.emacs' file is byte-compiled, use the following form instead:
 (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
76
Thus, someone else using a copy of your `.emacs' file will see
Richard M. Stallman's avatar
Richard M. Stallman committed
77 78 79 80
the startup message unless he personally acts to inhibit it."
  :type '(choice (const :tag "Don't inhibit")
		 (string :tag "Enter your user name, to inhibit"))
  :group 'initialization)
81

Richard M. Stallman's avatar
Richard M. Stallman committed
82
(defcustom inhibit-default-init nil
83
  "Non-nil inhibits loading the `default' library."
Richard M. Stallman's avatar
Richard M. Stallman committed
84 85
  :type 'boolean
  :group 'initialization)
Jim Blandy's avatar
Jim Blandy committed
86

87
(defcustom inhibit-startup-buffer-menu nil
88
  "Non-nil inhibits display of buffer list when more than 2 files are loaded."
89 90 91
  :type 'boolean
  :group 'initialization)

92
(defvar command-switch-alist nil
Jim Blandy's avatar
Jim Blandy committed
93 94
  "Alist of command-line switches.
Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
Kenichi Handa's avatar
Kenichi Handa committed
95 96
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
97

98 99 100
(defvar command-line-args-left nil
  "List of command-line args not yet processed.")

101 102 103 104 105 106
(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.")

Jim Blandy's avatar
Jim Blandy committed
107 108 109
(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
110
`argi' (the current argument) and `command-line-args-left' (the remaining
Jim Blandy's avatar
Jim Blandy committed
111
arguments).  The function should return non-nil only if it recognizes and
Richard M. Stallman's avatar
Richard M. Stallman committed
112 113
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
114

115 116 117 118
(defvar command-line-default-directory nil
  "Default directory to use for command line arguments.
This is normally copied from `default-directory' when Emacs starts.")

119 120
;;; This is here, rather than in x-win.el, so that we can ignore these
;;; options when we are not using X.
121
(defconst command-line-x-option-alist
122 123 124
  '(("-bw" 1 x-handle-numeric-switch border-width)
    ("-d" 1 x-handle-display)
    ("-display" 1 x-handle-display)
125
    ("-name" 1 x-handle-name-switch)
126 127
    ("-title" 1 x-handle-switch title)
    ("-T" 1 x-handle-switch title)
128 129 130 131 132 133
    ("-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)
134 135 136
    ("-fs" 0 x-handle-initial-switch fullscreen fullboth)
    ("-fw" 0 x-handle-initial-switch fullscreen fullwidth)
    ("-fh" 0 x-handle-initial-switch fullscreen fullheight)
137
    ("-mm" 0 x-handle-initial-switch fullscreen maximized)
138
    ("-ib" 1 x-handle-numeric-switch internal-border-width)
139
    ("-g" 1 x-handle-geometry)
140
    ("-lsp" 1 x-handle-numeric-switch line-spacing)
141
    ("-geometry" 1 x-handle-geometry)
142 143 144 145 146
    ("-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)
147
    ("-nbi" 0 x-handle-switch icon-type nil)
148 149 150 151 152 153 154 155
    ("-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)
156
    ("--name" 1 x-handle-name-switch)
157
    ("--title" 1 x-handle-switch title)
158 159
    ("--reverse-video" 0 x-handle-switch reverse t)
    ("--font" 1 x-handle-switch font)
160 161 162
    ("--fullscreen" 0 x-handle-initial-switch fullscreen fullboth)
    ("--fullwidth" 0 x-handle-initial-switch fullscreen fullwidth)
    ("--fullheight" 0 x-handle-initial-switch fullscreen fullheight)
163
    ("--maximized" 0 x-handle-initial-switch fullscreen maximized)
164
    ("--internal-border" 1 x-handle-numeric-switch internal-border-width)
165
    ("--geometry" 1 x-handle-geometry)
166 167 168
    ("--foreground-color" 1 x-handle-switch foreground-color)
    ("--background-color" 1 x-handle-switch background-color)
    ("--mouse-color" 1 x-handle-switch mouse-color)
169
    ("--no-bitmap-icon" 0 x-handle-no-bitmap-icon)
170 171 172 173
    ("--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)
174
    ("--line-spacing" 1 x-handle-numeric-switch line-spacing)
175
    ("--border-color" 1 x-handle-switch border-color)
176 177
    ("--smid" 1 x-handle-smid)
    ("--parent-id" 1 x-handle-parent-id))
178 179 180 181 182 183 184 185 186
  "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).")

187 188 189
(defconst command-line-ns-option-alist
  '(("-NSAutoLaunch" 1 ns-ignore-1-arg)
    ("-NXAutoLaunch" 1 ns-ignore-1-arg)
190
    ("-macosx" 0 ignore)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    ("-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)
    ("-name" 1 ns-handle-name-switch)
    ("-title" 1 ns-handle-switch title)
    ("-T" 1 ns-handle-switch title)
    ("-r" 0 ns-handle-switch reverse t)
    ("-rv" 0 ns-handle-switch reverse t)
    ("-reverse" 0 ns-handle-switch reverse t)
    ("-fn" 1 ns-handle-switch font)
    ("-font" 1 ns-handle-switch font)
    ("-ib" 1 ns-handle-numeric-switch internal-border-width)
    ;;("-g" .               x-handle-geometry)
    ;;("-geometry" .        x-handle-geometry)
    ("-fg" 1 ns-handle-switch foreground-color)
    ("-foreground" 1 ns-handle-switch foreground-color)
    ("-bg" 1 ns-handle-switch background-color)
    ("-background" 1 ns-handle-switch background-color)
;    ("-ms" 1 ns-handle-switch mouse-color)
    ("-itype" 0 ns-handle-switch icon-type t)
    ("-i" 0 ns-handle-switch icon-type t)
    ("-iconic" 0 ns-handle-iconic icon-type t)
    ;;("-xrm" .             x-handle-xrm-switch)
    ("-cr" 1 ns-handle-switch cursor-color)
    ("-vb" 0 ns-handle-switch vertical-scroll-bars t)
    ("-hb" 0 ns-handle-switch horizontal-scroll-bars t)
225 226
    ("-bd" 1 ns-handle-switch)
    ;; ("--border-width" 1 ns-handle-numeric-switch border-width)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    ;; ("--display" 1 ns-handle-display)
    ("--name" 1 ns-handle-name-switch)
    ("--title" 1 ns-handle-switch title)
    ("--reverse-video" 0 ns-handle-switch reverse t)
    ("--font" 1 ns-handle-switch font)
    ("--internal-border" 1 ns-handle-numeric-switch internal-border-width)
    ;; ("--geometry" 1 ns-handle-geometry)
    ("--foreground-color" 1 ns-handle-switch foreground-color)
    ("--background-color" 1 ns-handle-switch background-color)
    ("--mouse-color" 1 ns-handle-switch mouse-color)
    ("--icon-type" 0 ns-handle-switch icon-type t)
    ("--iconic" 0 ns-handle-iconic)
    ;; ("--xrm" 1 ns-handle-xrm-switch)
    ("--cursor-color" 1 ns-handle-switch cursor-color)
    ("--vertical-scroll-bars" 0 ns-handle-switch vertical-scroll-bars t)
    ("--border-color" 1 ns-handle-switch border-width))
  "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
253
(defvar before-init-hook nil
254
  "Normal hook run after handling urgent options but before loading init files.")
Jim Blandy's avatar
Jim Blandy committed
255

Roland McGrath's avatar
Roland McGrath committed
256
(defvar after-init-hook nil
257 258 259 260 261 262 263
  "Normal hook run after loading the init files, `~/.emacs' and `default.el'.
There is no `condition-case' around the running of these functions;
therefore, if you set `debug-on-error' non-nil in `.emacs',
an error in one of these functions will invoke the debugger.")

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

Jim Blandy's avatar
Jim Blandy committed
265
(defvar term-setup-hook nil
266 267
  "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
268 269 270
so as to override the definitions made by the terminal-specific file.
Emacs never sets this variable itself.")

271 272 273 274
(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
275
(defvar keyboard-type nil
Richard M. Stallman's avatar
Richard M. Stallman committed
276
  "The brand of keyboard you are using.
277 278 279
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
280 281

(defvar window-setup-hook nil
Richard M. Stallman's avatar
Richard M. Stallman committed
282 283 284
  "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
285

Richard M. Stallman's avatar
Richard M. Stallman committed
286
(defcustom initial-major-mode 'lisp-interaction-mode
287
  "Major mode command symbol to use for the initial `*scratch*' buffer."
288
  :type 'function
Richard M. Stallman's avatar
Richard M. Stallman committed
289
  :group 'initialization)
Jim Blandy's avatar
Jim Blandy committed
290

291
(defvar init-file-user nil
Jim Blandy's avatar
Jim Blandy committed
292
  "Identity of user whose `.emacs' file is or was read.
293 294 295
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
296 297 298 299
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
300

Karl Heuer's avatar
Karl Heuer committed
301 302
In either of the latter cases, `(concat \"~\" init-file-user \"/\")'
evaluates to the name of the directory where the `.emacs' file was
303 304 305
looked for.

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

Richard M. Stallman's avatar
Richard M. Stallman committed
308
(defcustom site-run-file "site-start"
309 310 311
  "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
312
higher incidence of change, don't make sense to load into Emacs's
313
dumped image.  Thus, the run-time load order is: 1. file described in
314 315 316 317 318 319 320
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
321 322 323 324 325
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
326
this variable usefully is to set it while building and dumping Emacs."
327
  :type '(choice (const :tag "none" nil) string)
328 329 330 331
  :group 'initialization
  :initialize 'custom-initialize-default
  :set '(lambda (variable value)
	  (error "Customizing `site-run-file' does not work")))
332

Richard M. Stallman's avatar
Richard M. Stallman committed
333
(defcustom mail-host-address nil
334
  "Name of this machine, for purposes of naming users."
Richard M. Stallman's avatar
Richard M. Stallman committed
335 336
  :type '(choice (const nil) string)
  :group 'mail)
337

338
(defcustom user-mail-address (if command-line-processed
339 340 341 342
				 (or (getenv "EMAIL")
				     (concat (user-login-name) "@"
					     (or mail-host-address
						 (system-name))))
343 344
			       ;; Empty string means "not set yet".
			       "")
345
  "Full mailing address of this user.
346
This is initialized with environment variable `EMAIL' or, as a
347
fallback, using `mail-host-address'.  This is done after your
348
init file is read, in case it sets `mail-host-address'."
Richard M. Stallman's avatar
Richard M. Stallman committed
349 350
  :type 'string
  :group 'mail)
351

Richard M. Stallman's avatar
Richard M. Stallman committed
352
(defcustom auto-save-list-file-prefix
353 354
  (cond ((eq system-type 'ms-dos)
	 ;; MS-DOS cannot have initial dot, and allows only 8.3 names
355
	 (concat user-emacs-directory "auto-save.list/_s"))
356
	(t
357
	 (concat user-emacs-directory "auto-save-list/.saves-")))
358 359 360 361
  "Prefix for generating `auto-save-list-file-name'.
This is used after reading your `.emacs' file to initialize
`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.
362
Directories in the prefix will be created if necessary.
363
Set this to nil if you want to prevent `auto-save-list-file-name'
Richard M. Stallman's avatar
Richard M. Stallman committed
364
from being initialized."
365 366
  :type '(choice (const :tag "Don't record a session's auto save list" nil)
		 string)
Richard M. Stallman's avatar
Richard M. Stallman committed
367
  :group 'auto-save)
368

Kenichi Handa's avatar
Kenichi Handa committed
369 370
(defvar emacs-quick-startup nil)

371 372
(defvar emacs-basic-display nil)

Jim Blandy's avatar
Jim Blandy committed
373 374
(defvar init-file-debug nil)

375 376
(defvar init-file-had-error nil
  "Non-nil if there was an error loading the user's init file.")
377

378 379
(defvar normal-top-level-add-subdirs-inode-list nil)

380 381
(defvar no-blinking-cursor nil)

382 383
(defvar default-frame-background-mode)

384 385 386
(defvar pure-space-overflow nil
  "Non-nil if building Emacs overflowed pure space.")

387 388 389 390
(defvar pure-space-overflow-message "\
Warning Warning!!!  Pure space overflow    !!!Warning Warning
\(See the node Pure Storage in the Lisp manual for details.)\n")

391
(defvar tutorial-directory nil
392 393
  "Directory containing the Emacs TUTORIAL files.")

394 395 396 397 398
;; Get correct value in a dumped, installed Emacs.
(eval-at-startup
 (setq tutorial-directory (file-name-as-directory
                           (expand-file-name "tutorials" data-directory))))

399
(defun normal-top-level-add-subdirs-to-load-path ()
400 401
  "Add all subdirectories of current directory to `load-path'.
More precisely, this uses only the subdirectories whose names
402 403
start with letters or digits; it excludes any subdirectory named `RCS'
or `CVS', and any subdirectory that contains a file named `.nosearch'."
404
  (let (dirs
405
	attrs
406 407 408 409
	(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
410
      (push (pop pending) dirs)
411 412 413
      (let* ((this-dir (car dirs))
	     (contents (directory-files this-dir))
	     (default-directory this-dir)
414 415
	     (canonicalized (if (fboundp 'untranslated-canonical-name)
				(untranslated-canonical-name this-dir))))
416 417 418 419 420
	;; The Windows version doesn't report meaningful inode
	;; numbers, so use the canonicalized absolute file name of the
	;; directory instead.
	(setq attrs (or canonicalized
			(nthcdr 10 (file-attributes this-dir))))
421
	(unless (member attrs normal-top-level-add-subdirs-inode-list)
422 423
	  (push attrs normal-top-level-add-subdirs-inode-list)
	  (dolist (file contents)
424
	    ;; The lower-case variants of RCS and CVS are for DOS/Windows.
425 426
	    (unless (member file '("." ".." "RCS" "CVS" "rcs" "cvs"))
	      (when (and (string-match "\\`[[:alnum:]]" file)
427 428 429
			 ;; Avoid doing a `stat' when it isn't necessary
			 ;; because that can cause trouble when an NFS server
			 ;; is down.
430 431 432
			 (not (string-match "\\.elc?\\'" file))
			 (file-directory-p file))
		(let ((expanded (expand-file-name file)))
433 434
		  (unless (file-exists-p (expand-file-name ".nosearch"
							   expanded))
435
		    (setq pending (nconc pending (list expanded)))))))))))
436
    (normal-top-level-add-to-load-path (cdr (nreverse dirs)))))
437

Richard M. Stallman's avatar
Richard M. Stallman committed
438 439 440 441 442
;; 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.
443
(defun normal-top-level-add-to-load-path (dirs)
444 445 446
  (let ((tail load-path)
	(thisdir (directory-file-name default-directory)))
    (while (and tail
447 448
		;;Don't go all the way to the nil terminator.
		(cdr tail)
449 450 451 452
		(not (equal thisdir (car tail)))
		(not (and (memq system-type '(ms-dos windows-nt))
			  (equal (downcase thisdir) (downcase (car tail))))))
      (setq tail (cdr tail)))
453 454 455
    ;;Splice the new section in.
    (when tail
      (setcdr tail (append (mapcar 'expand-file-name dirs) (cdr tail))))))
456

Jim Blandy's avatar
Jim Blandy committed
457 458 459 460
(defun normal-top-level ()
  (if command-line-processed
      (message "Back to top level.")
    (setq command-line-processed t)
461
    (let ((dir default-directory))
462
      (with-current-buffer "*Messages*"
463 464 465 466
        ;; 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.
467
	(setq default-directory dir)))
468 469 470
    ;; `user-full-name' is now known; reset its standard-value here.
    (put 'user-full-name 'standard-value
	 (list (default-value 'user-full-name)))
Karl Heuer's avatar
Karl Heuer committed
471 472 473
    ;; For root, preserve owner and group when editing files.
    (if (equal (user-uid) 0)
	(setq backup-by-copying-when-mismatch t))
474 475 476
    ;; 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,
477 478
    ;; Look for a leim-list.el file too.  Loading it will register
    ;; available input methods.
479 480 481 482 483 484 485 486 487 488 489
    (let ((tail load-path) dir)
      (while tail
        (setq dir (car tail))
        (let ((default-directory dir))
          (load (expand-file-name "subdirs.el") t t t))
        (let ((default-directory dir))
          (load (expand-file-name "leim-list.el") t t t))
        ;; 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))))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
490 491 492 493 494 495 496 497 498 499 500 501 502
    ;; 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)))))
Jim Blandy's avatar
Jim Blandy committed
503
    (setq default-directory (abbreviate-file-name default-directory))
504
    (let ((old-face-font-rescale-alist face-font-rescale-alist))
505 506 507 508
      (unwind-protect
	  (command-line)
	;; Do this again, in case .emacs defined more abbreviations.
	(setq default-directory (abbreviate-file-name default-directory))
509 510
	;; Specify the file for recording all the auto save files of this session.
	;; This is used by recover-session.
511 512 513
	(or auto-save-list-file-name
	    (and auto-save-list-file-prefix
		 (setq auto-save-list-file-name
514 515
		       ;; Under MS-DOS our PID is almost always reused between
		       ;; Emacs invocations.  We need something more unique.
516 517 518 519 520 521
		       (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)
522
			      (concat
523 524 525 526 527 528 529 530 531 532
			       (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))))))))
533 534 535 536
	(unless inhibit-startup-hooks
	  (run-hooks 'emacs-startup-hook)
	  (and term-setup-hook
	       (run-hooks 'term-setup-hook)))
537 538 539

	;; Don't do this if we failed to create the initial frame,
	;; for instance due to a dense colormap.
540 541
	(when (or frame-initial-frame
		  ;; If frame-initial-frame has no meaning, do this anyway.
542
		  (not (and initial-window-system
543
			    (not noninteractive)
544
			    (not (eq initial-window-system 'pc)))))
545 546 547 548 549 550 551 552 553 554 555 556 557

	  ;; 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)))

558 559 560 561
	  ;; Modify the initial frame based on what .emacs puts into
	  ;; ...-frame-alist.
	  (if (fboundp 'frame-notice-user-settings)
	      (frame-notice-user-settings))
562 563 564 565
	  ;; 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.
566
	  (if (fboundp 'frame-set-background-mode)
567
	      (frame-set-background-mode (selected-frame))))
Miles Bader's avatar
Miles Bader committed
568

569 570 571 572
	;; 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
573
	     (run-hooks 'window-setup-hook))))
574 575 576 577
    ;; 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.
578 579 580 581 582 583 584 585 586 587 588 589 590 591
    (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)))))
592 593

;; Precompute the keyboard equivalents in the menu bar items.
594 595
;; Command-line options supported by tty's:
(defconst tty-long-option-alist
596 597 598
  '(("--name"		  . "-name")
    ("--title"		  . "-T")
    ("--reverse-video"	  . "-reverse")
599
    ("--foreground-color" . "-fg")
600 601
    ("--background-color" . "-bg")
    ("--color"		  . "-color")))
602

603
(defconst tool-bar-images-pixel-height 24
604
  "Height in pixels of images in the tool-bar.")
605

606 607 608
(defvar tool-bar-originally-present nil
  "Non-nil if tool-bars are present before user and site init files are read.")

609 610 611 612 613 614 615 616 617 618 619 620
(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
621
opening the first frame (e.g. open a connection to an X server).")
622

623
;; Handle the X-like command-line arguments "-fg", "-bg", "-name", etc.
624
(defun tty-handle-args (args)
625
  (let (rest)
626
    (message "%S" args)
627 628
    (while (and args
		(not (equal (car args) "--")))
629 630 631
      (let* ((argi (pop args))
	     (orig-argi argi)
	     argval completion)
632 633
	;; Check for long options with attached arguments
	;; and separate out the attached option argument into argval.
634 635 636 637 638
	(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))
639 640
	  (if (eq completion t)
	      ;; Exact match for long option.
641
	      (setq argi (cdr (assoc argi tty-long-option-alist)))
642 643 644 645
	    (if (stringp completion)
		(let ((elt (assoc completion tty-long-option-alist)))
		  ;; Check for abbreviated long option.
		  (or elt
646 647
		      (error "Option `%s' is ambiguous" argi))
		  (setq argi (cdr elt)))
648
	      ;; Check for a short option.
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
	      (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)))))
    (nreverse rest)))
688

689 690 691
(declare-function x-get-resource "frame.c"
		  (attribute class &optional component subclass))
(declare-function tool-bar-mode "tool-bar" (&optional arg))
692
(declare-function tool-bar-setup "tool-bar")
693

694
(defvar server-name)
695
(defvar server-process)
696

Jim Blandy's avatar
Jim Blandy committed
697
(defun command-line ()
698
  (setq before-init-time (current-time)
699
	after-init-time nil
700
        command-line-default-directory default-directory)
701

702
  ;; Choose a reasonable location for temporary files.
703
  (custom-reevaluate-setting 'temporary-file-directory)
704
  (custom-reevaluate-setting 'small-temporary-file-directory)
705
  (custom-reevaluate-setting 'auto-save-file-name-transforms)
706 707 708
  (custom-reevaluate-setting 'abbrev-file-name)
  ;; Force recomputation, in case it was computed during the dump.
  (setq abbreviated-home-dir nil)
709

Roland McGrath's avatar
Roland McGrath committed
710
  ;; See if we should import version-control from the environment variable.
Jim Blandy's avatar
Jim Blandy committed
711 712
  (let ((vc (getenv "VERSION_CONTROL")))
    (cond ((eq vc nil))			;don't do anything if not set
713
	  ((member vc '("t" "numbered"))
Jim Blandy's avatar
Jim Blandy committed
714
	   (setq version-control t))
715
	  ((member vc '("nil" "existing"))
Jim Blandy's avatar
Jim Blandy committed
716
	   (setq version-control nil))
717
	  ((member vc '("never" "simple"))
Jim Blandy's avatar
Jim Blandy committed
718 719
	   (setq version-control 'never))))

Jim Blandy's avatar
Jim Blandy committed
720 721 722 723 724
  ;;! 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
725

726 727 728
  ;; Set the default strings to display in mode line for
  ;; end-of-line formats that aren't native to this platform.
  (cond
729
   ((memq system-type '(ms-dos windows-nt))
730 731 732 733 734
    (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)")))
735

736 737
  ;; Make sure window system's init file was loaded in loadup.el if
  ;; using a window system.
738
  (condition-case error
739 740 741
    (unless noninteractive
      (if (and initial-window-system
	       (not (featurep
742 743
		     (intern
		      (concat (symbol-name initial-window-system) "-win")))))
744 745 746
	  (error "Unsupported window system `%s'" initial-window-system))
      ;; Process window-system specific command line parameters.
      (setq command-line-args
747 748 749 750
	    (funcall
	     (or (cdr (assq initial-window-system handle-args-function-alist))
		 (error "Unsupported window system `%s'" initial-window-system))
	     command-line-args))
751
      ;; Initialize the window system. (Open connection, etc.)
752 753 754
      (funcall
       (or (cdr (assq initial-window-system window-system-initialization-alist))
	   (error "Unsupported window system `%s'" initial-window-system))))
755
    ;; If there was an error, print the error message and exit.
756
    (error
757 758 759 760 761
     (princ
      (if (eq (car error) 'error)
	  (apply 'concat (cdr error))
	(if (memq 'file-error (get (car error) 'error-conditions))
	    (format "%s: %s"
762 763 764
                    (nth 1 error)
                    (mapconcat (lambda (obj) (prin1-to-string obj t))
                               (cdr (cdr error)) ", "))
765
	  (format "%s: %s"
766 767 768
                  (get (car error) 'error-message)
                  (mapconcat (lambda (obj) (prin1-to-string obj t))
                             (cdr error) ", "))))
769
      'external-debugging-output)
770
     (terpri 'external-debugging-output)
771
     (setq initial-window-system nil)
772
     (kill-emacs)))
Jim Blandy's avatar
Jim Blandy committed
773

774 775
  (set-locale-environment nil)

776 777
  ;; Convert preloaded file names in load-history to absolute.
  (let ((simple-file-name
778 779
	 ;; Look for simple.el or simple.elc and use their directory
	 ;; as the place where all Lisp files live.
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
	 (locate-file "simple" load-path (get-load-suffixes)))
	lisp-dir)
    ;; Don't abort if simple.el cannot be found, but print a warning.
    (if (null simple-file-name)
	(progn
	  (princ "Warning: Could not find simple.el nor simple.elc"
		 'external-debugging-output)
	  (terpri 'external-debugging-output))
      (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))))
798

799 800 801 802 803
  ;; Convert the arguments to Emacs internal representation.
  (let ((args (cdr command-line-args)))
    (while args
      (setcar args
	      (decode-coding-string (car args) locale-coding-system t))
804
      (pop args)))
805

Jim Blandy's avatar
Jim Blandy committed
806 807 808
  (let ((done nil)
	(args (cdr command-line-args)))

Jim Blandy's avatar
Jim Blandy committed
809 810 811 812
    ;; 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.
813 814
    (and init-file-user
         (equal init-file-user (user-real-login-name))
Jim Blandy's avatar
Jim Blandy committed
815
	 (setq init-file-user ""))
Jim Blandy's avatar
Jim Blandy committed
816 817 818 819

    ;; 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
820
    (while (and (not done) args)
821 822 823
      (let* ((longopts '(("--no-init-file") ("--no-site-file") ("--debug-init")
                         ("--user") ("--iconic") ("--icon-type") ("--quick")
			 ("--no-blinking-cursor") ("--basic-display")))
824 825 826
             (argi (pop args))
             (orig-argi argi)
             argval)
827
	;; Handle --OPTION=VALUE format.
828
	(when (string-match "\\`\\(--[^=]*\\)=" argi)
829
	  (setq argval (substring argi (match-end 0))
830
                argi (match-string 1 argi)))
831
	(when (string-match "\\`--." orig-argi)
832 833 834 835 836 837 838 839
	  (let ((completion (try-completion argi longopts)))
	    (if (eq completion t)
		(setq argi (substring argi 1))
	      (if (stringp completion)
		  (let ((elt (assoc completion longopts)))
		    (or elt
			(error "Option `%s' is ambiguous" argi))
		    (setq argi (substring (car elt) 1)))
840 841
		(setq argval nil
                      argi orig-argi)))))
Jim Blandy's avatar
Jim Blandy committed
842
	(cond
843
	 ((member argi '("-Q" "-quick"))
Kenichi Handa's avatar
Kenichi Handa committed
844 845
	  (setq init-file-user nil
		site-run-file nil
846 847 848 849
		emacs-quick-startup t))
	 ((member argi '("-D" "-basic-display"))
	  (setq no-blinking-cursor t
		emacs-basic-display t)
Kenichi Handa's avatar
Kenichi Handa committed
850
	  (push '(vertical-scroll-bars . nil) initial-frame-alist))
851 852 853
	 ((member argi '("-q" "-no-init-file"))
	  (setq init-file-user nil))
	 ((member argi '("-u" "-user"))
854
	  (setq init-file-user (or argval (pop args))
855
		argval nil))
856
	 ((equal argi "-no-site-file")
857
	  (setq site-run-file nil))
858
	 ((equal argi "-debug-init")
859
	  (setq init-file-debug t))
860
	 ((equal argi "-iconic")
861
	  (push '(visibility . icon) initial-frame-alist))
862
	 ((member argi '("-icon-type" "-i" "-itype"))
863
	  (push '(icon-type . t) default-frame-alist))
864 865
	 ((member argi '("-nbc" "-no-blinking-cursor"))
	  (setq no-blinking-cursor t))
866
	 ;; Push the popped arg back on the list of arguments.
867 868 869
	 (t
          (push argi args)
          (setq done t)))
870 871 872 873
	;; Was argval set but not used?
	(and argval
	     (error "Option `%s' doesn't allow an argument" argi))))

Jim Blandy's avatar
Jim Blandy committed
874
    ;; Re-attach the program name to the front of the arg list.
875 876
    (and command-line-args
         (setcdr command-line-args args)))
Jim Blandy's avatar
Jim Blandy committed
877

878 879
  (run-hooks 'before-init-hook)

880 881
  ;; Under X Window, this creates the X frame and deletes the terminal frame.
  (unless (daemonp)
882
    (frame-initialize))
883

884
  ;; Turn off blinking cursor if so specified in X resources.  This is here
Jan Djärv's avatar
Jan Djärv committed
885
  ;; only because all other settings of no-blinking-cursor are here.
886 887
  (unless (or noninteractive
	      emacs-basic-display
888
	      (and (memq window-system '(x w32 ns))
889 890 891 892
		   (not (member (x-get-resource "cursorBlink" "CursorBlink")
				'("off" "false")))))
    (setq no-blinking-cursor t))

893 894 895 896 897
  ;; If frame was created with a menu bar, set menu-bar-mode on.
  (unless (or noninteractive
	      emacs-basic-display
              (and (memq initial-window-system '(x w32))
                   (<= (frame-parameter nil 'menu-bar-lines) 0)))
898
    (menu-bar-mode 1))
899

900 901 902 903 904 905 906 907 908 909 910
  (unless (or noninteractive (not (fboundp 'tool-bar-mode)))
    ;; Set up the tool-bar.  Do this even in tty frames, so that there
    ;; is a tool-bar if Emacs later opens a graphical frame.
    (if (or emacs-basic-display
	    (and (numberp (frame-parameter nil 'tool-bar-lines))
		 (<= (frame-parameter nil 'tool-bar-lines) 0)))
	;; On a graphical display with the toolbar disabled via X
	;; resources, set up the toolbar without enabling it.
	(tool-bar-setup)
      ;; Otherwise, enable tool-bar-mode.
      (tool-bar-mode 1)))
911

912
  ;; Can't do this init in defcustom because the relevant variables
913 914
  ;; are not set.
  (custom-reevaluate-setting 'blink-cursor-mode)
915
  (custom-reevaluate-setting 'tooltip-mode)
916
  (custom-reevaluate-setting 'global-font-lock-mode)
917 918
  (custom-reevaluate-setting 'mouse-wheel-down-event)
  (custom-reevaluate-setting 'mouse-wheel-up-event)
919
  (custom-reevaluate-setting 'file-name-shadow-mode)
920
  (custom-reevaluate-setting 'send-mail-function)
921
  (custom-reevaluate-setting 'focus-follows-mouse)
922
  (custom-reevaluate-setting 'global-auto-composition-mode)
Dan Nicolaescu's avatar