(mouse-save-then-kill, mouse-secondary-save-then-kill): Use the kill-new
[bpt/emacs.git] / lisp / startup.el
1 ;;; startup.el --- process Emacs shell arguments
2
3 ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: internal
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ; These are processed only at the beginning of the argument list.
27 ; -batch execute noninteractively (messages go to stdout,
28 ; variable noninteractive set to t)
29 ; This option must be the first in the arglist.
30 ; Processed by `main' in emacs.c -- never seen by lisp
31 ; -t file Specify to use file rather than stdin/stdout
32 ; as the terminal.
33 ; This option must be the first in the arglist.
34 ; Processed by `main' in emacs.c -- never seen by lisp
35 ; -nw Inhibit the use of any window-system-specific display
36 ; code; use the current virtual terminal.
37 ; This option must be the first in the arglist.
38 ; Processed by `main' in emacs.c -- never seen by lisp
39 ; -q load no init file
40 ; -no-init-file same
41 ; -u user load user's init file
42 ; -user user same
43 ; -debug-init Don't catch errors in init file; let debugger run.
44
45 ; These are processed in the order encountered.
46 ; -f function execute function
47 ; -funcall function same
48 ; -l file load file
49 ; -load file same
50 ; -insert file same
51 ; file visit file
52 ; -kill kill (exit) emacs
53
54 ;;; Code:
55
56 (setq top-level '(normal-top-level))
57
58 (defvar command-line-processed nil "t once command line has been processed")
59
60 (defconst inhibit-startup-message nil
61 "*Non-nil inhibits the initial startup message.
62 This is for use in your personal init file, once you are familiar
63 with the contents of the startup message.")
64
65 (defconst inhibit-startup-echo-area-message nil
66 "*Non-nil inhibits the initial startup echo area message.
67 Inhibition takes effect only if your `.emacs' file contains
68 a line of this form:
69 (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
70 If your `.emacs' file is byte-compiled, use the following form instead:
71 (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
72 Thus, someone else using a copy of your `.emacs' file will see
73 the startup message unless he personally acts to inhibit it.")
74
75 (defconst inhibit-default-init nil
76 "*Non-nil inhibits loading the `default' library.")
77
78 (defconst command-switch-alist nil
79 "Alist of command-line switches.
80 Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
81 HANDLER-FUNCTION receives switch name as sole arg;
82 remaining command-line args are in the variable `command-line-args-left'.")
83
84 (defvar command-line-args-left nil
85 "List of command-line args not yet processed.")
86
87 (defvar command-line-functions nil ;; lrs 7/31/89
88 "List of functions to process unrecognized command-line arguments.
89 Each function should access the dynamically bound variables
90 `argi' (the current argument) and `command-line-args-left' (the remaining
91 arguments). The function should return non-nil only if it recognizes and
92 processes `argi'. If it does so, it may consume successive arguments by
93 altering `command-line-args-left' to remove them.")
94
95 (defvar command-line-default-directory nil
96 "Default directory to use for command line arguments.
97 This is normally copied from `default-directory' when Emacs starts.")
98
99 (defvar before-init-hook nil
100 "Functions to call after handling urgent options but before init files.
101 The frame system uses this to open frames to display messages while
102 Emacs loads the user's initialization file.")
103
104 (defvar after-init-hook nil
105 "Functions to call after loading the init file (`~/.emacs').
106 The call is not protected by a condition-case, so you can set `debug-on-error'
107 in `.emacs', and put all the actual code on `after-init-hook'.")
108
109 (defvar term-setup-hook nil
110 "Functions to be called after loading terminal-specific Lisp code.
111 See `run-hooks'. This variable exists for users to set,
112 so as to override the definitions made by the terminal-specific file.
113 Emacs never sets this variable itself.")
114
115 (defvar keyboard-type nil
116 "The brand of keyboard you are using.
117 This variable is used to define
118 the proper function and keypad keys for use under X. It is used in a
119 fashion analogous to the environment value TERM.")
120
121 (defvar window-setup-hook nil
122 "Normal hook run to initialize window system display.
123 Emacs runs this hook after processing the command line arguments and loading
124 the user's init file.")
125
126 (defconst initial-major-mode 'lisp-interaction-mode
127 "Major mode command symbol to use for the initial *scratch* buffer.")
128
129 (defvar init-file-user nil
130 "Identity of user whose `.emacs' file is or was read.
131 The value is nil if no init file is being used; otherwise, it may be either
132 the null string, meaning that the init file was taken from the user that
133 originally logged in, or it may be a string containing a user's name.
134
135 In either of the latter cases, `(concat \"~\" init-file-user \"/\")'
136 evaluates to the name of the directory where the `.emacs' file was
137 looked for.")
138
139 (defvar site-run-file "site-start"
140 "File containing site-wide run-time initializations.
141 This file is loaded at run-time before `~/.emacs'. It contains inits
142 that need to be in place for the entire site, but which, due to their
143 higher incidence of change, don't make sense to load into emacs'
144 dumped image. Thus, the run-time load order is: 1. file described in
145 this variable, if non-nil; 2. `~/.emacs'; 3. `default.el'.")
146
147 (defconst iso-8859-1-locale-regexp "8859[-_]?1"
148 "Regexp that specifies when to enable the ISO 8859-1 character set.
149 We do that if this regexp matches the locale name
150 specified by the LC_ALL, LC_CTYPE and LANG environment variables.")
151
152 (defvar user-mail-address nil
153 "Full mailing address of this user.")
154
155 (defvar init-file-debug nil)
156
157 (defvar init-file-had-error nil)
158
159 (defun normal-top-level ()
160 (if command-line-processed
161 (message "Back to top level.")
162 (setq command-line-processed t)
163 (if (not (eq system-type 'vax-vms))
164 (progn
165 ;; If the PWD environment variable isn't accurate, delete it.
166 (let ((pwd (getenv "PWD")))
167 (and (stringp pwd)
168 ;; Use FOO/., so that if FOO is a symlink, file-attributes
169 ;; describes the directory linked to, not FOO itself.
170 (or (equal (file-attributes
171 (concat (file-name-as-directory pwd) "."))
172 (file-attributes
173 (concat (file-name-as-directory default-directory)
174 ".")))
175 (setq process-environment
176 (delete (concat "PWD=" pwd)
177 process-environment)))))))
178 (setq default-directory (abbreviate-file-name default-directory))
179 (setq user-mail-address (concat (user-login-name) "@" (system-name)))
180 (let ((menubar-bindings-done nil))
181 (unwind-protect
182 (command-line)
183 ;; Do this again, in case .emacs defined more abbreviations.
184 (setq default-directory (abbreviate-file-name default-directory))
185 (run-hooks 'emacs-startup-hook)
186 (and term-setup-hook
187 (run-hooks 'term-setup-hook))
188 ;; Modify the initial frame based on what .emacs puts into
189 ;; ...-frame-alist.
190 (if (fboundp 'frame-notice-user-settings)
191 (frame-notice-user-settings))
192 ;; Now we know the user's default font, so add it to the menu.
193 (if (fboundp 'font-menu-add-default)
194 (font-menu-add-default))
195 (and window-setup-hook
196 (run-hooks 'window-setup-hook))
197 (or menubar-bindings-done
198 (precompute-menubar-bindings))))))
199
200 ;; Precompute the keyboard equivalents in the menu bar items.
201 (defun precompute-menubar-bindings ()
202 (if (eq window-system 'x)
203 (let ((submap (lookup-key global-map [menu-bar])))
204 (while submap
205 (and (consp (car submap))
206 (symbolp (car (car submap)))
207 (stringp (car-safe (cdr (car submap))))
208 (keymapp (cdr (cdr (car submap))))
209 (x-popup-menu nil (cdr (cdr (car submap)))))
210 (setq submap (cdr submap))))))
211
212 (defun command-line ()
213 (setq command-line-default-directory default-directory)
214
215 ;; See if we should import version-control from the environment variable.
216 (let ((vc (getenv "VERSION_CONTROL")))
217 (cond ((eq vc nil)) ;don't do anything if not set
218 ((or (string= vc "t")
219 (string= vc "numbered"))
220 (setq version-control t))
221 ((or (string= vc "nil")
222 (string= vc "existing"))
223 (setq version-control nil))
224 ((or (string= vc "never")
225 (string= vc "simple"))
226 (setq version-control 'never))))
227
228 (if (let ((ctype
229 ;; Use the first of these three envvars that has a nonempty value.
230 (or (let ((string (getenv "LC_ALL")))
231 (and (not (equal string "")) string))
232 (let ((string (getenv "LC_CTYPE")))
233 (and (not (equal string "")) string))
234 (let ((string (getenv "LANG")))
235 (and (not (equal string "")) string)))))
236 (and ctype
237 (string-match iso-8859-1-locale-regexp ctype)))
238 (progn
239 (standard-display-european t)
240 (require 'iso-syntax)))
241
242 ;;! This has been commented out; I currently find the behavior when
243 ;;! split-window-keep-point is nil disturbing, but if I can get used
244 ;;! to it, then it would be better to eliminate the option.
245 ;;! ;; Choose a good default value for split-window-keep-point.
246 ;;! (setq split-window-keep-point (> baud-rate 2400))
247
248 ;; Read window system's init file if using a window system.
249 (condition-case error
250 (if (and window-system (not noninteractive))
251 (load (concat term-file-prefix
252 (symbol-name window-system)
253 "-win")
254 ;; Every window system should have a startup file;
255 ;; barf if we can't find it.
256 nil t))
257 ;; If we can't read it, print the error message and exit.
258 (error
259 (princ
260 (if (eq (car error) 'error)
261 (apply 'concat (cdr error))
262 (if (memq 'file-error (get (car error) 'error-conditions))
263 (format "%s: %s"
264 (nth 1 error)
265 (mapconcat '(lambda (obj) (prin1-to-string obj t))
266 (cdr (cdr error)) ", "))
267 (format "%s: %s"
268 (get (car error) 'error-message)
269 (mapconcat '(lambda (obj) (prin1-to-string obj t))
270 (cdr error) ", "))))
271 'external-debugging-output)
272 (setq window-system nil)
273 (kill-emacs)))
274
275 (let ((done nil)
276 (args (cdr command-line-args)))
277
278 ;; Figure out which user's init file to load,
279 ;; either from the environment or from the options.
280 (setq init-file-user (if noninteractive nil (user-login-name)))
281 ;; If user has not done su, use current $HOME to find .emacs.
282 (and init-file-user (string= init-file-user (user-real-login-name))
283 (setq init-file-user ""))
284
285 ;; Process the command-line args, and delete the arguments
286 ;; processed. This is consistent with the way main in emacs.c
287 ;; does things.
288 (while (and (not done) args)
289 (let ((argi (car args)))
290 (cond
291 ((or (string-equal argi "-q")
292 (string-equal argi "-no-init-file"))
293 (setq init-file-user nil
294 args (cdr args)))
295 ((or (string-equal argi "-u")
296 (string-equal argi "-user"))
297 (setq args (cdr args)
298 init-file-user (car args)
299 args (cdr args)))
300 ((string-equal argi "-no-site-file")
301 (setq site-run-file nil
302 args (cdr args)))
303 ((string-equal argi "-debug-init")
304 (setq init-file-debug t
305 args (cdr args)))
306 (t (setq done t)))))
307
308 ;; Re-attach the program name to the front of the arg list.
309 (setcdr command-line-args args))
310
311 ;; Under X Windows, this creates the X frame and deletes the terminal frame.
312 (if (fboundp 'face-initialize)
313 (face-initialize))
314 (if (fboundp 'frame-initialize)
315 (frame-initialize))
316 ;; If frame was created with a menu bar, set menu-bar-mode on.
317 (if (and (eq window-system 'x)
318 (> (cdr (assq 'menu-bar-lines (frame-parameters))) 0))
319 (menu-bar-mode t))
320
321 (run-hooks 'before-init-hook)
322
323 ;; Run the site-start library if it exists. The point of this file is
324 ;; that it is run before .emacs. There is no point in doing this after
325 ;; .emacs; that is useless.
326 (if site-run-file
327 (load site-run-file t t))
328
329 ;; Sites should not disable this. Only individuals should disable
330 ;; the startup message.
331 (setq inhibit-startup-message nil)
332
333 ;; Load that user's init file, or the default one, or none.
334 (let (debug-on-error-from-init-file
335 debug-on-error-should-be-set
336 (debug-on-error-initial
337 (if (eq init-file-debug t) 'startup init-file-debug)))
338 (let ((debug-on-error debug-on-error-initial)
339 ;; This function actually reads the init files.
340 (inner
341 (function
342 (lambda ()
343 (if init-file-user
344 (progn
345 (setq user-init-file
346 (cond
347 ((eq system-type 'ms-dos)
348 (concat "~" init-file-user "/_emacs"))
349 ((eq system-type 'vax-vms)
350 "sys$login:.emacs")
351 (t
352 (concat "~" init-file-user "/.emacs"))))
353 (load user-init-file t t t)
354 (or inhibit-default-init
355 (let ((inhibit-startup-message nil))
356 ;; Users are supposed to be told their rights.
357 ;; (Plus how to get help and how to undo.)
358 ;; Don't you dare turn this off for anyone
359 ;; except yourself.
360 (load "default" t t)))))))))
361 (if init-file-debug
362 ;; Do this without a condition-case if the user wants to debug.
363 (funcall inner)
364 (condition-case error
365 (progn
366 (funcall inner)
367 (setq init-file-had-error nil))
368 (error (message "Error in init file: %s%s%s"
369 (get (car error) 'error-message)
370 (if (cdr error) ": " "")
371 (mapconcat 'prin1-to-string (cdr error) ", "))
372 (setq init-file-had-error t))))
373 ;; If we can tell that the init file altered debug-on-error,
374 ;; arrange to preserve the value that it set up.
375 (or (eq debug-on-error debug-on-error-initial)
376 (setq debug-on-error-should-be-set t
377 debug-on-error-from-init-file debug-on-error)))
378 (if debug-on-error-should-be-set
379 (setq debug-on-error debug-on-error-from-init-file)))
380
381 (run-hooks 'after-init-hook)
382
383 ;; If *scratch* exists and init file didn't change its mode, initialize it.
384 (if (get-buffer "*scratch*")
385 (save-excursion
386 (set-buffer "*scratch*")
387 (if (eq major-mode 'fundamental-mode)
388 (funcall initial-major-mode))))
389 ;; Load library for our terminal type.
390 ;; User init file can set term-file-prefix to nil to prevent this.
391 (and term-file-prefix (not noninteractive) (not window-system)
392 (let ((term (getenv "TERM"))
393 hyphend)
394 (while (and term
395 (not (load (concat term-file-prefix term) t t)))
396 ;; Strip off last hyphen and what follows, then try again
397 (if (setq hyphend (string-match "[-_][^-_]+$" term))
398 (setq term (substring term 0 hyphend))
399 (setq term nil)))))
400
401 ;; Process the remaining args.
402 (command-line-1 (cdr command-line-args))
403
404 ;; If -batch, terminate after processing the command options.
405 (if noninteractive (kill-emacs t)))
406
407 (defun command-line-1 (command-line-args-left)
408 (or noninteractive (input-pending-p) init-file-had-error
409 (and inhibit-startup-echo-area-message
410 (let ((buffer (get-buffer-create " *temp*")))
411 (prog1
412 (condition-case nil
413 (save-excursion
414 (set-buffer buffer)
415 (insert-file-contents user-init-file)
416 (re-search-forward
417 (concat
418 "([ \t\n]*setq[ \t\n]+"
419 "inhibit-startup-echo-area-message[ \t\n]+"
420 (regexp-quote
421 (prin1-to-string
422 (if (string= init-file-user "")
423 (user-login-name)
424 init-file-user)))
425 "[ \t\n]*)")
426 nil t))
427 (error nil))
428 (kill-buffer buffer))))
429 (message (if (eq (key-binding "\C-h\C-p") 'describe-project)
430 "For information about the GNU Project and its goals, type C-h C-p."
431 (substitute-command-keys
432 "For information about the GNU Project and its goals, type \\[describe-project]."))))
433 (if (null command-line-args-left)
434 (cond ((and (not inhibit-startup-message) (not noninteractive)
435 ;; Don't clobber a non-scratch buffer if init file
436 ;; has selected it.
437 (string= (buffer-name) "*scratch*")
438 (not (input-pending-p)))
439 ;; If there are no switches to process, we might as well
440 ;; run this hook now, and there may be some need to do it
441 ;; before doing any output.
442 (and term-setup-hook
443 (run-hooks 'term-setup-hook))
444 ;; Don't let the hook be run twice.
445 (setq term-setup-hook nil)
446
447 ;; It's important to notice the user settings before we
448 ;; display the startup message; otherwise, the settings
449 ;; won't take effect until the user gives the first
450 ;; keystroke, and that's distracting.
451 (if (fboundp 'frame-notice-user-settings)
452 (frame-notice-user-settings))
453
454 (and window-setup-hook
455 (run-hooks 'window-setup-hook))
456 (setq window-setup-hook nil)
457 ;; Do this now to avoid an annoying delay if the user
458 ;; clicks the menu bar during the sit-for.
459 (precompute-menubar-bindings)
460 (setq menubar-bindings-done t)
461 (unwind-protect
462 (progn
463 (insert (emacs-version)
464 "
465 Copyright (C) 1994 Free Software Foundation, Inc.\n\n")
466 ;; If keys have their default meanings,
467 ;; use precomputed string to save lots of time.
468 (if (and (eq (key-binding "\C-h") 'help-command)
469 (eq (key-binding "\C-xu") 'advertised-undo)
470 (eq (key-binding "\C-x\C-c") 'save-buffers-kill-emacs)
471 (eq (key-binding "\C-h\C-c") 'describe-copying)
472 (eq (key-binding "\C-h\C-d") 'describe-distribution)
473 (eq (key-binding "\C-h\C-w") 'describe-no-warranty)
474 (eq (key-binding "\C-ht") 'help-with-tutorial))
475 (insert
476 "Type C-h for help; C-x u to undo changes. (`C-' means use CTRL key.)
477 To kill the Emacs job, type C-x C-c.
478 Type C-h t for a tutorial on using Emacs.
479 Type C-h i to enter Info, which you can use to read GNU documentation.
480
481 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details.
482 You may give out copies of Emacs; type C-h C-c to see the conditions.
483 Type C-h C-d for information on getting the latest version.")
484 (insert (substitute-command-keys
485 "Type \\[help-command] for help; \\[advertised-undo] to undo changes. (`C-' means use CTRL key.)
486 To kill the Emacs job, type \\[save-buffers-kill-emacs].
487 Type \\[help-with-tutorial] for a tutorial on using Emacs.
488 Type \\[info] to enter Info, which you can use to read GNU documentation.
489
490 GNU Emacs comes with ABSOLUTELY NO WARRANTY; type \\[describe-no-warranty] for full details.
491 You may give out copies of Emacs; type \\[describe-copying] to see the conditions.
492 Type \\[describe-distribution] for information on getting the latest version.")))
493 (set-buffer-modified-p nil)
494 (sit-for 120))
495 (save-excursion
496 ;; In case the Emacs server has already selected
497 ;; another buffer, erase the one our message is in.
498 (set-buffer (get-buffer "*scratch*"))
499 (erase-buffer)
500 (set-buffer-modified-p nil)))))
501 (let ((dir command-line-default-directory)
502 (file-count 0)
503 first-file-buffer
504 (line 0))
505 (while command-line-args-left
506 (let ((argi (car command-line-args-left))
507 tem)
508 (setq command-line-args-left (cdr command-line-args-left))
509 (cond ((setq tem (assoc argi command-switch-alist))
510 (funcall (cdr tem) argi))
511 ((or (string-equal argi "-f") ;what the manual claims
512 (string-equal argi "-funcall")
513 (string-equal argi "-e")) ; what the source used to say
514 (setq tem (intern (car command-line-args-left)))
515 (setq command-line-args-left (cdr command-line-args-left))
516 (funcall tem))
517 ((or (string-equal argi "-l")
518 (string-equal argi "-load"))
519 (let ((file (car command-line-args-left)))
520 ;; Take file from default dir if it exists there;
521 ;; otherwise let `load' search for it.
522 (if (file-exists-p (expand-file-name file))
523 (setq file (expand-file-name file)))
524 (load file nil t))
525 (setq command-line-args-left (cdr command-line-args-left)))
526 ((string-equal argi "-insert")
527 (or (stringp (car command-line-args-left))
528 (error "filename omitted from `-insert' option"))
529 (insert-file-contents (car command-line-args-left))
530 (setq command-line-args-left (cdr command-line-args-left)))
531 ((string-equal argi "-kill")
532 (kill-emacs t))
533 ((string-match "^\\+[0-9]+\\'" argi)
534 (setq line (string-to-int argi)))
535 (t
536 ;; We have almost exhausted our options. See if the
537 ;; user has made any other command-line options available
538 (let ((hooks command-line-functions);; lrs 7/31/89
539 (did-hook nil))
540 (while (and hooks
541 (not (setq did-hook (funcall (car hooks)))))
542 (setq hooks (cdr hooks)))
543 (if (not did-hook)
544 ;; Ok, presume that the argument is a file name
545 (progn
546 (setq file-count (1+ file-count))
547 (cond ((= file-count 1)
548 (setq first-file-buffer
549 (find-file (expand-file-name argi dir))))
550 (t
551 (find-file-other-window (expand-file-name argi dir))))
552 (or (zerop line)
553 (goto-line line))
554 (setq line 0))))))))
555 ;; If 3 or more files visited, and not all visible,
556 ;; show user what they all are.
557 (if (> file-count 2)
558 (or (get-buffer-window first-file-buffer)
559 (progn (other-window 1)
560 (buffer-menu)))))))
561
562 ;;; startup.el ends here