home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / lisp / startup.el < prev    next >
Encoding:
Text File  |  1994-08-28  |  19.4 KB  |  519 lines

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