XEmacs 21.2.45 "Thelxepeia".
[chise/xemacs-chise.git.1] / lisp / startup.el
1 ;;; startup.el --- process XEmacs shell arguments
2
3 ;; Copyright (C) 1985-1986, 1990, 1992-1997 Free Software Foundation, Inc.
4 ;; Copyright (c) 1993, 1994 Sun Microsystems, Inc.
5 ;; Copyright (C) 1995 Board of Trustees, University of Illinois
6
7 ;; Maintainer: XEmacs Development Team
8 ;; Keywords: internal, dumped
9
10 ;; This file is part of XEmacs.
11
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Synched up with: FSF 19.34.
28
29 ;;; Commentary:
30
31 ;; This file is dumped with XEmacs.
32
33 ;; -batch, -t, and -nw are processed by main() in emacs.c and are
34 ;; never seen by lisp code.
35
36 ;; -version and -help are special-cased as well: they imply -batch,
37 ;; but are left on the list for lisp code to process.
38
39 ;;; Code:
40 \f
41 (setq top-level '(normal-top-level))
42
43 (defvar command-line-processed nil "t once command line has been processed")
44
45 (defconst startup-message-timeout 12000) ; More or less disable the timeout
46 (defconst splash-frame-timeout 7) ; interval between splash frame elements
47
48 (defconst inhibit-startup-message nil
49   "*Non-nil inhibits the initial startup message.
50 This is for use in your personal init file, once you are familiar
51 with the contents of the startup message.")
52
53 ;; #### FSFmacs randomness
54 ;;(defconst inhibit-startup-echo-area-message nil
55 ;;  "*Non-nil inhibits the initial startup echo area message.
56 ;;Inhibition takes effect only if your `.emacs' file contains
57 ;;a line of this form:
58 ;; (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
59 ;;If your `.emacs' file is byte-compiled, use the following form instead:
60 ;; (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
61 ;;Thus, someone else using a copy of your `.emacs' file will see
62 ;;the startup message unless he personally acts to inhibit it.")
63
64 (defconst inhibit-default-init nil
65   "*Non-nil inhibits loading the `default' library.")
66
67 (defvar command-line-args-left nil
68   "List of command-line args not yet processed.") ; bound by `command-line'
69
70 (defvar command-line-default-directory nil
71   "Default directory to use for command line arguments.
72 This is normally copied from `default-directory' when XEmacs starts.")
73
74 (defvar before-init-hook nil
75   "Functions to call after handling urgent options but before init files.
76 The frame system uses this to open frames to display messages while
77 XEmacs loads the user's initialization file.")
78
79 (defvar after-init-hook nil
80   "*Functions to call after loading the init file.
81 The call is not protected by a condition-case, so you can set `debug-on-error'
82 in the init file, and put all the actual code on `after-init-hook'.")
83
84 (defvar term-setup-hook nil
85   "*Functions to be called after loading terminal-specific Lisp code.
86 See `run-hooks'.  This variable exists for users to set, so as to
87 override the definitions made by the terminal-specific file.  XEmacs
88 never sets this variable itself.")
89
90 (defvar keyboard-type nil
91   "The brand of keyboard you are using.
92 This variable is used to define the proper function and keypad keys
93 for use under X.  It is used in a fashion analogous to the environment
94 value TERM.")
95
96 (defvar window-setup-hook nil
97   "Normal hook run to initialize window system display.
98 XEmacs runs this hook after processing the command line arguments and loading
99 the user's init file.")
100
101 (defconst initial-major-mode 'lisp-interaction-mode
102   "Major mode command symbol to use for the initial *scratch* buffer.")
103
104 (defvar emacs-roots nil
105   "List of plausible roots of the XEmacs hierarchy.")
106
107 (defvar user-init-directory-base ".xemacs"
108   "Base of directory where user-installed init files may go.")
109
110 (defvar user-init-directory
111   (file-name-as-directory
112    (paths-construct-path (list "~" user-init-directory-base)))
113   "Directory where user-installed init files may go.")
114
115 (defvar user-init-file-base "init.el"
116   "Default name of the user init file if uncompiled.
117 This should be used for migration purposes only.")
118
119 (defvar user-init-file-base-list '("init.elc" "init.el")
120   "List of allowed init files in the user's init directory.
121 The first one found takes precedence.")
122
123 (defvar user-home-init-file-base-list
124   (append '(".emacs.elc" ".emacs.el" ".emacs")
125           (and (eq system-type 'windows-nt)
126                '("_emacs.elc" "_emacs.el" "_emacs")))
127   "List of allowed init files in the user's home directory.
128 The first one found takes precedence.")
129
130 (defvar load-home-init-file nil
131   "Non-nil if XEmacs should load the init file from the home directory.
132 Otherwise, XEmacs will offer migration to the init directory.")
133
134 (defvar load-user-init-file-p t
135   "Non-nil if XEmacs should load the user's init file.")
136
137 ;; #### called `site-run-file' in FSFmacs
138
139 (defvar site-start-file "site-start"
140   "File containing site-wide run-time initializations.
141 This file is loaded at run-time before `.emacs'.  It
142 contains inits that need to be in place for the entire site, but
143 which, due to their higher incidence of change, don't make sense to
144 load into XEmacs' dumped image.  Thus, the run-time load order is:
145
146   1. file described in this variable, if non-nil;
147   2. `.emacs';
148   3. `/path/to/xemacs/lisp/default.el'.
149
150 Don't use the `site-start.el' file for things some users may not like.
151 Put them in `default.el' instead, so that users can more easily
152 override them.  Users can prevent loading `default.el' with the `-q'
153 option or by setting `inhibit-default-init' in their own init files,
154 but inhibiting `site-start.el' requires `--no-site-file', which
155 is less convenient.")
156
157 ;;(defconst iso-8859-1-locale-regexp "8859[-_]?1"
158 ;;  "Regexp that specifies when to enable the ISO 8859-1 character set.
159 ;;We do that if this regexp matches the locale name
160 ;;specified by the LC_ALL, LC_CTYPE and LANG environment variables.")
161
162 (defcustom mail-host-address nil
163   "*Name of this machine, for purposes of naming users."
164   :type 'string
165   :group 'mail)
166
167 (defcustom user-mail-address nil
168   "*Full mailing address of this user.
169 This is initialized based on `mail-host-address',
170 after your init file is read, in case it sets `mail-host-address'."
171   :type 'string
172   :group 'mail)
173
174 (defvar init-file-debug nil)
175
176 (defvar init-file-had-error nil)
177
178 (defvar init-file-loaded nil
179   "True after the user's init file has been loaded (or suppressed with -q).
180 This will be true when `after-init-hook' is run and at all times
181 after, and will not be true at any time before.")
182
183 (defvar initial-frame-unmapped-p nil)
184
185 \f
186
187 (defvar command-switch-alist
188   '(("-help"    . command-line-do-help)
189     ("-version". command-line-do-version)
190     ("-V"       . command-line-do-version)
191     ("-funcall". command-line-do-funcall)
192     ("-f"       . command-line-do-funcall)
193     ("-e"       . command-line-do-funcall-1)
194     ("-eval"    . command-line-do-eval)
195     ("-load"    . command-line-do-load)
196     ("-l"       . command-line-do-load)
197     ("-insert"  . command-line-do-insert)
198     ("-i"       . command-line-do-insert)
199     ("-kill"    . command-line-do-kill)
200     ;; Options like +35 are handled specially.
201     ;; Window-system, site, or package-specific code might add to this.
202     ;; X11 handles its options by letting Xt remove args from this list.
203     )
204   "Alist of command-line switches.
205 Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
206 HANDLER-FUNCTION receives switch name as sole arg;
207 remaining command-line args are in the variable `command-line-args-left'.")
208
209 ;;; default switches
210 ;;; Note: these doc strings are semi-magical.
211
212 (defun command-line-do-help (arg)
213   "Print the XEmacs usage message and exit."
214   (let ((standard-output 'external-debugging-output))
215     (princ (concat "\n" (emacs-version) "\n\n"))
216     (princ
217      (if (featurep 'x)
218          (concat "When creating a window on an X display, "
219                  (emacs-name)
220                  " accepts all standard X Toolkit
221 command line options plus the following:
222   -iconname <title>     Use title as the icon name.
223   -mc <color>           Use color as the mouse color.
224   -cr <color>           Use color as the text-cursor foregound color.
225   -private              Install a private colormap.
226
227 In addition, the")
228        "The"))
229     (princ " following options are accepted:
230   -sd                   Show dump ID.  Ignored when configured without --pdump.
231   -nd                   Don't load the dump file.  Roughly like old temacs.
232                         Ignored when configured without --pdump.
233   -t <device>           Use TTY <device> instead of the terminal for input
234                         and output.  This implies the -nw option.
235   -nw                   Inhibit the use of any window-system-specific
236                         display code: use the current tty.
237   -batch                Execute noninteractively (messages go to stderr).
238   -debug-init           Enter the debugger if an error in the init file occurs.
239   -unmapped             Do not map the initial frame.
240   -no-site-file         Do not load the site-specific init file (site-start.el).
241   -no-init-file         Do not load the user-specific init file.
242   -no-early-packages    Do not process early packages.
243   -no-autoloads         Do not load global symbol files (auto-autoloads) at
244                         startup.  Also implies `-vanilla'.
245   -vanilla              Equivalent to -q -no-site-file -no-early-packages.
246   -q                    Same as -no-init-file.
247   -user-init-file <file> Use <file> as init file.
248   -user-init-directory <directory> Use <directory> as init directory.
249   -user <user>          Load user's init file instead of your own.
250   -u <user>             Same as -user.\n")
251    (let ((l command-switch-alist)
252           (insert (lambda (&rest x)
253                     (princ "  ")
254                     (let ((len 2))
255                       (while x
256                         (princ (car x))
257                         (incf len (length (car x)))
258                         (setq x (cdr x)))
259                       (when (>= len 24)
260                         (terpri) (setq len 0))
261                       (while (< len 24)
262                         (princ " ")
263                         (incf len))))))
264       (while l
265         (let ((name (car (car l)))
266               (fn (cdr (car l)))
267               doc arg cons)
268           (cond
269            ((and (symbolp fn) (get fn 'undocumented)) nil)
270            (t
271             (setq doc (documentation fn))
272             (if (member doc '(nil "")) (setq doc "(undocumented)"))
273             (cond ((string-match "\n\\(<.*>\\)\n?\\'" doc)
274                    ;; Doc of the form "The frobber switch\n<arg1> <arg2>"
275                    (setq arg (substring doc (match-beginning 1) (match-end 1))
276                          doc (substring doc 0 (match-beginning 0))))
277                   ((string-match "\n+\\'" doc)
278                    (setq doc (substring doc 0 (match-beginning 0)))))
279             (if (and (setq cons (rassq fn command-switch-alist))
280                      (not (eq cons (car l))))
281                 (setq doc (format "Same as %s." (car cons))))
282             (if arg
283                 (funcall insert name " " arg)
284               (funcall insert name))
285             (princ doc)
286             (terpri))))
287         (setq l (cdr l))))
288     (princ (concat "\
289   +N <file>             Start displaying <file> at line N.
290
291 Anything else is considered a file name, and is placed into a buffer for
292 editing.
293
294 " (emacs-name) " has an online tutorial and manuals.  Type ^Ht (Control-h t) after
295 starting XEmacs to run the tutorial.  Type ^Hi to enter the manual browser.
296 Type ^H^H^H (Control-h Control-h Control-h) to get more help options.\n")
297
298     (kill-emacs 0))))
299
300 (defun command-line-do-funcall (arg)
301   "Invoke the named lisp function with no arguments.
302 <function>"
303   (funcall (intern (pop command-line-args-left))))
304 (fset 'command-line-do-funcall-1 'command-line-do-funcall)
305 (put 'command-line-do-funcall-1 'undocumented t)
306
307 (defun command-line-do-eval (arg)
308   "Evaluate the lisp form.  Quote it carefully.
309 <form>"
310   (eval (read (pop command-line-args-left))))
311
312 (defun command-line-do-load (arg)
313   "Load the named file of Lisp code into XEmacs.
314 <file>"
315   (let ((file (pop command-line-args-left)))
316     ;; Take file from default dir if it exists there;
317     ;; otherwise let `load' search for it.
318     (if (file-exists-p (expand-file-name file))
319         (setq file (expand-file-name file)))
320     (load file nil t)))
321
322 (defun command-line-do-insert (arg)
323   "Insert file into the current buffer.
324 <file>"
325   (insert-file-contents (pop command-line-args-left)))
326
327 (defun command-line-do-kill (arg)
328   "Exit XEmacs."
329   (kill-emacs t))
330
331 (defun command-line-do-version (arg)
332   "Print version info and exit."
333   (princ (concat (emacs-version) "\n"))
334   (kill-emacs 0))
335
336 \f
337 ;;; Processing the command line and loading various init files
338
339 (defun early-error-handler (&rest debugger-args)
340   "You should probably not be using this."
341   ;; Used as the debugger during XEmacs initialization; if an error occurs,
342   ;; print some diagnostics, and kill XEmacs.
343
344   ;; output the contents of the warning buffer, since it won't be seen
345   ;; otherwise.
346   ;; #### kludge!  The call to Feval forces the pending warnings to
347   ;; get output.  There definitely needs to be a better way.
348   (let ((buffer (eval (get-buffer-create "*Warnings*"))))
349     (princ (buffer-substring (point-min buffer) (point-max buffer) buffer)
350            'external-debugging-output))
351
352   (let ((string "Initialization error")
353         (error (nth 1 debugger-args))
354         (debug-on-error nil)
355         (stream 'external-debugging-output))
356     (if (null error)
357         (princ string stream)
358       (princ (concat "\n" string ": ") stream)
359       (condition-case ()
360           (display-error error stream)
361         (error (princ "<<< error printing error message >>>" stream)))
362       (princ "\n" stream)
363       (if (memq (car-safe error) '(void-function void-variable))
364           (princ "
365         This probably means that XEmacs is picking up an old version of
366         the lisp library, or that some .elc files are not up-to-date.\n"
367                  stream)))
368     (when (not suppress-early-error-handler-backtrace)
369       (let ((print-length 1000)
370             (print-level 1000)
371             (print-escape-newlines t)
372             (print-readably nil))
373         (when (getenv "EMACSLOADPATH")
374           (princ (format "\n$EMACSLOADPATH is %s" (getenv "EMACSLOADPATH"))
375                  stream))
376         (princ (format "\nexec-directory is %S" exec-directory) stream)
377         (princ (format "\ndata-directory is %S" data-directory) stream)
378         (princ (format "\ndata-directory-list is %S" data-directory-list) stream)
379         (princ (format "\ndoc-directory is %S" doc-directory) stream)
380         (princ (format "\nload-path is %S" load-path) stream)
381         (princ "\n\n" stream)))
382     (when (not suppress-early-error-handler-backtrace)
383       (backtrace stream t)))
384   (if (fboundp 'mswindows-message-box)
385       (mswindows-message-box "Initialization error"))
386   (kill-emacs -1))
387
388 (defun normal-top-level ()
389   (if command-line-processed
390       (message "Back to top level.")
391     (setq command-line-processed t)
392     ;; Canonicalize HOME (PWD is canonicalized by init_buffer in buffer.c)
393     (let ((value (user-home-directory)))
394       (if (and value
395                (< (length value) (length default-directory))
396                (equal (file-attributes default-directory)
397                       (file-attributes value)))
398           (setq default-directory (file-name-as-directory value))))
399     (setq default-directory (abbreviate-file-name default-directory))
400     (initialize-xemacs-paths)
401
402     (startup-set-invocation-environment)
403
404     (let ((debug-paths (or debug-paths
405                            (and (getenv "EMACSDEBUGPATHS")
406                                 t))))
407
408       (setq emacs-roots (paths-find-emacs-roots invocation-directory
409                                                 invocation-name))
410
411       (if debug-paths
412           (princ (format "emacs-roots:\n%S\n" emacs-roots)
413                  'external-debugging-output))
414
415       (if (null emacs-roots)
416           (startup-find-roots-warning))
417       (startup-setup-paths emacs-roots
418                            user-init-directory
419                            inhibit-early-packages
420                            inhibit-site-lisp
421                            debug-paths)
422       (startup-setup-paths-warning))
423
424     (if (and (not inhibit-autoloads)
425              lisp-directory)
426         (load (expand-file-name (file-name-sans-extension autoload-file-name)
427                                 lisp-directory) nil t))
428
429     (if (not inhibit-autoloads)
430         (progn
431           (if (not inhibit-early-packages)
432               (packages-load-package-auto-autoloads early-package-load-path))
433           (packages-load-package-auto-autoloads late-package-load-path)
434           (packages-load-package-auto-autoloads last-package-load-path)))
435
436     (unwind-protect
437         (command-line)
438       ;; Do this again, in case the init file defined more abbreviations.
439       (setq default-directory (abbreviate-file-name default-directory))
440       ;; Specify the file for recording all the auto save files of
441       ;; this session.  This is used by recover-session.
442       (if auto-save-list-file-prefix
443           (setq auto-save-list-file-name
444                 (expand-file-name
445                  (format "%s%d-%s"
446                          auto-save-list-file-prefix
447                          (emacs-pid)
448                          (system-name)))))
449       (run-hooks 'emacs-startup-hook)
450       (and term-setup-hook
451            (run-hooks 'term-setup-hook))
452       (setq term-setup-hook nil)
453       ;;      ;; Modify the initial frame based on what the init file puts into
454       ;;      ;; ...-frame-alist.
455       (frame-notice-user-settings)
456       ;;      ;;####FSFmacs junk
457       ;;      ;; Now we know the user's default font, so add it to the menu.
458       ;;      (if (fboundp 'font-menu-add-default)
459       ;;          (font-menu-add-default))
460       (when window-setup-hook
461         (run-hooks 'window-setup-hook))
462       (setq window-setup-hook nil))
463
464     (if load-user-init-file-p
465         (maybe-migrate-user-init-file))
466     ;;####FSFmacs junk
467     ;;      (or menubar-bindings-done
468     ;;    (precompute-menubar-bindings))
469     ))
470
471 ;;####FSFmacs junk
472 ;;; Precompute the keyboard equivalents in the menu bar items.
473 ;;(defun precompute-menubar-bindings ()
474 ;;  (if (eq window-system 'x)
475 ;;      (let ((submap (lookup-key global-map [menu-bar])))
476 ;;      (while submap
477 ;;        (and (consp (car submap))
478 ;;             (symbolp (car (car submap)))
479 ;;             (stringp (car-safe (cdr (car submap))))
480 ;;             (keymapp (cdr (cdr (car submap))))
481 ;;             (x-popup-menu nil (cdr (cdr (car submap)))))
482 ;;        (setq submap (cdr submap))))))
483
484 (defun command-line-early (args)
485   ;; This processes those switches which need to be processed before
486   ;; starting up the window system.
487
488   (setq command-line-default-directory default-directory)
489
490   ;; See if we should import version-control from the environment variable.
491   (let ((vc (getenv "VERSION_CONTROL")))
492     (cond ((eq vc nil))                 ;don't do anything if not set
493           ((or (string= vc "t")
494                (string= vc "numbered"))
495            (setq version-control t))
496           ((or (string= vc "nil")
497                (string= vc "existing"))
498            (setq version-control nil))
499           ((or (string= vc "never")
500                (string= vc "simple"))
501            (setq version-control 'never))))
502
503   ;;####FSFmacs
504   ;;  (if (let ((ctype
505   ;;         ;; Use the first of these three envvars that has a nonempty value.
506   ;;         (or (let ((string (getenv "LC_ALL")))
507   ;;               (and (not (equal string "")) string))
508   ;;             (let ((string (getenv "LC_CTYPE")))
509   ;;               (and (not (equal string "")) string))
510   ;;             (let ((string (getenv "LANG")))
511   ;;               (and (not (equal string "")) string)))))
512   ;;    (and ctype
513   ;;         (string-match iso-8859-1-locale-regexp ctype)))
514   ;;      (progn
515   ;;    (standard-display-european t)
516   ;;    (require 'iso-syntax)))
517
518   (setq load-user-init-file-p (not (noninteractive)))
519
520   ;; Allow (at least) these arguments anywhere in the command line
521   (let ((new-args nil)
522         (arg      nil))
523     (while args
524       (setq arg (pop args))
525       (cond
526        ((or (string= arg "-q")
527             (string= arg "-no-init-file"))
528         (setq load-user-init-file-p nil))
529        ((string= arg "-no-site-file")
530         (setq site-start-file nil))
531        ((or (string= arg "-no-early-packages")
532             (string= arg "--no-early-packages"))
533         (setq inhibit-early-packages t))
534        ((or (string= arg "-vanilla")
535             (string= arg "--vanilla")
536             ;; Some work on this one already done in emacs.c.
537             (string= arg "-no-autoloads")
538             (string= arg "--no-autoloads"))
539         (setq load-user-init-file-p nil
540               site-start-file nil))
541        ((string= arg "-user-init-file")
542         (setq user-init-file (pop args)))
543        ((string= arg "-user-init-directory")
544         (setq user-init-directory (file-name-as-directory (pop args))))
545        ((or (string= arg "-u")
546             (string= arg "-user"))
547         (let* ((user (pop args))
548                (home-user (concat "~" user)))
549           (setq user-init-directory (file-name-as-directory
550                                      (paths-construct-path
551                                       (list home-user user-init-directory-base))))
552           (setq user-init-file
553                 (find-user-init-file user-init-directory home-user))
554           (setq custom-file
555                 (make-custom-file-name user-init-file))))
556        ((string= arg "-debug-init")
557         (setq init-file-debug t))
558        ((string= arg "-unmapped")
559         (setq initial-frame-unmapped-p t))
560        ((or (string= arg "-debug-paths")
561             (string= arg "--debug-paths"))
562         t)
563        ((or (string= arg "--") (string= arg "-"))
564         (while args
565           (push (pop args) new-args)))
566        (t (push arg new-args))))
567
568     (setq init-file-user (and load-user-init-file-p ""))
569
570     (nreverse new-args)))
571
572 (defconst initial-scratch-message "\
573 ;; This buffer is for notes you don't want to save, and for Lisp evaluation.
574 ;; If you want to create a file, first visit that file with C-x C-f,
575 ;; then enter the text in that file's own buffer.
576
577 "
578   "Initial message displayed in *scratch* buffer at startup.
579 If this is nil, no message will be displayed.")
580
581 (defun command-line ()
582   (let ((command-line-args-left (cdr command-line-args)))
583
584     (let ((debugger 'early-error-handler)
585           (debug-on-error t))
586
587       ;; Process magic command-line switches like -q and -u.  Do this
588       ;; before creating the first frame because some of these switches
589       ;; may affect that.  I think it's ok to do this before establishing
590       ;; the X connection, and maybe someday things like -nw can be
591       ;; handled here instead of down in C.
592       (setq command-line-args-left (command-line-early command-line-args-left))
593
594       ;; Setup the toolbar icon directory
595       (when (featurep 'toolbar)
596         (init-toolbar-location))
597
598       ;; Run the window system's init function.  tty is considered to be
599       ;; a type of window system for this purpose.  This creates the
600       ;; initial (non stdio) device.
601       (when (and initial-window-system (not noninteractive))
602         (funcall (intern (concat "init-"
603                                  (symbol-name initial-window-system)
604                                  "-win"))))
605
606       ;; When not in batch mode, this creates the first visible frame,
607       ;; and deletes the stdio device.
608       (frame-initialize))
609
610     ;; Reinitialize faces if necessary.  This function changes face if
611     ;; it is created during auto-autoloads loading.  Otherwise, it
612     ;; does nothing.
613     (startup-initialize-custom-faces)
614
615     ;;
616     ;; We have normality, I repeat, we have normality.  Anything you still
617     ;; can't cope with is therefore your own problem.  (And we don't need
618     ;; to kill XEmacs for it.)
619     ;;
620
621     ;;; Load init files.
622     (load-init-file)
623
624     (with-current-buffer (get-buffer "*scratch*")
625       (erase-buffer)
626       ;; (insert initial-scratch-message)
627       (set-buffer-modified-p nil)
628       (when (eq major-mode 'fundamental-mode)
629         (funcall initial-major-mode)))
630
631     ;; Load library for our terminal type.
632     ;; User init file can set term-file-prefix to nil to prevent this.
633     ;; Note that for any TTY's opened subsequently, the TTY init
634     ;; code will run this.
635     (when (and (eq 'tty (console-type))
636                (not (noninteractive)))
637       (load-terminal-library))
638
639     ;; Process the remaining args.
640     (command-line-1)
641
642     ;; it was turned on by default so that the warnings don't get displayed
643     ;; until after the splash screen.
644     (setq inhibit-warning-display nil)
645     ;; If -batch, terminate after processing the command options.
646     (when (noninteractive) (kill-emacs t))))
647
648 (defun load-terminal-library ()
649   (when term-file-prefix
650     (let ((term (getenv "TERM"))
651           hyphend)
652       (while (and term
653                   (not (load (concat term-file-prefix term) t t)))
654         ;; Strip off last hyphen and what follows, then try again
655         (if (setq hyphend (string-match "[-_][^-_]+\\'" term))
656             (setq term (substring term 0 hyphend))
657           (setq term nil))))))
658
659 (defun find-user-init-directory-init-file (&optional init-directory)
660   "Determine the user's init file if in the init directory."
661   (let ((init-directory (or init-directory user-init-directory)))
662     (catch 'found
663       (dolist (file user-init-file-base-list)
664         (let ((expanded (expand-file-name file init-directory)))
665           (when (file-readable-p expanded)
666             (throw 'found expanded)))))))
667
668 (defun find-user-home-directory-init-file (&optional home-directory)
669   "Determine the user's init file if in the home directory."
670   (let ((home-directory (or home-directory "~")))
671     (catch 'found
672       (dolist (file user-home-init-file-base-list)
673         (let ((expanded (expand-file-name file home-directory)))
674           (when (file-readable-p expanded)
675             (throw 'found expanded))))
676       nil)))
677
678 (defun find-user-init-file (&optional init-directory home-directory)
679   "Determine the user's init file."
680   (if load-home-init-file
681       (find-user-home-directory-init-file home-directory)
682     (or (find-user-init-directory-init-file init-directory)
683         (find-user-home-directory-init-file home-directory))))
684
685 (defun maybe-migrate-user-init-file ()
686   "Ask user if she wants to migrate the init file(s) to new location."
687   (if (and (not load-home-init-file)
688            (not (find-user-init-directory-init-file user-init-directory))
689            (stringp user-init-file)
690            (file-readable-p user-init-file))
691       (if (with-output-to-temp-buffer (help-buffer-name nil)
692             (progn
693               (princ "XEmacs recommends that the initialization code in
694 ")
695               (princ user-init-file)
696               (princ "
697 be migrated to the ")
698               (princ user-init-directory)
699               (princ " directory.  XEmacs can
700 perform the migration automatically.
701
702 After the migration, init.el/init.elc holds user-written
703 initialization code.  Moreover the customize settings will be in
704 custom.el.
705
706 You can undo the migration at any time with
707 M-x maybe-unmigrate-user-init-file.
708
709 If you choose not to do this now, XEmacs will not ask you this
710 question in the future.  However, you can still make XEmacs
711 perform the migration at any time with M-x migrate-user-init-file.")
712               (show-temp-buffer-in-current-frame standard-output)
713               (yes-or-no-p-minibuf (concat "Migrate init file to "
714                                            user-init-directory
715                                            "? "))))
716           (progn
717             (migrate-user-init-file)
718             (maybe-create-compatibility-dot-emacs))
719         (customize-save-variable 'load-home-init-file t))))
720
721 (defun maybe-create-compatibility-dot-emacs ()
722   "Ask user if she wants to create a .emacs compatibility file."
723   (if (with-output-to-temp-buffer (help-buffer-name nil)
724         (progn
725           (princ "The initialization code has now been migrated to the ")
726           (princ user-init-directory)
727           (princ "directory.
728
729 For backwards compatibility with, for example, older versions of XEmacs,
730 XEmacs can create a special old-style .emacs file in your home
731 directory which will load the relocated initialization code.")
732           (show-temp-buffer-in-current-frame standard-output)
733           (yes-or-no-p-minibuf "Create compatibility .emacs? ")))
734       (create-compatibility-dot-emacs)))
735
736 (defun migrate-user-init-file ()
737   "Migrate the init file from the home directory."
738   (interactive)
739   (if (not (file-exists-p user-init-directory))
740       (progn
741         (message "Creating %s directory..." user-init-directory)
742         (make-directory user-init-directory)))
743   (message "Migrating custom file...")
744   (customize-set-value 'load-home-init-file nil)
745   (custom-migrate-custom-file (make-custom-file-name user-init-file
746                                                      'force-new))
747   (message "Moving init file...")
748   (let ((new-user-init-file (expand-file-name user-init-file-base
749                                               user-init-directory)))
750     (rename-file user-init-file new-user-init-file)
751     (setq user-init-file new-user-init-file))
752   (message "Migration done."))
753
754 (defun create-compatibility-dot-emacs ()
755   "Create .emacs compatibility file for migrated setup."
756   (message "Creating .emacs compatibility file.")
757   (with-temp-file (expand-file-name ".emacs" "~")
758     (insert ";;; XEmacs backwards compatibility file\n")
759     (insert "(setq user-init-file\n")
760     (insert "      (expand-file-name \"init.el\"\n")
761     (insert "                   (expand-file-name \".xemacs\" \"~\")))\n")
762     (insert "(setq custom-file\n")
763     (insert "      (expand-file-name \"custom.el\"\n")
764     (insert "                   (expand-file-name \".xemacs\" \"~\")))\n")
765     (insert "\n")
766     (insert "(load-file user-init-file)\n")
767     (insert "(load-file custom-file)"))
768   (message "Created .emacs compatibility file."))
769
770 (defun maybe-unmigrate-user-init-file ()
771   "Possibly unmigrate the user's init and custom files."
772   (interactive)
773   (let ((dot-emacs-file-name (expand-file-name ".emacs" "~")))
774     (if (and (not load-home-init-file)
775              (or (not (file-exists-p dot-emacs-file-name))
776                  (yes-or-no-p-minibuf (concat "Overwrite " dot-emacs-file-name
777                                               "? "))))
778       (unmigrate-user-init-file dot-emacs-file-name))))
779
780 (defun unmigrate-user-init-file (&optional target-file-name)
781   "Unmigrate the user's init and custom files."
782   (interactive)
783   (let ((target-file-name
784          (or target-file-name (expand-file-name ".emacs" "~"))))
785     (rename-file user-init-file target-file-name 'ok-if-already-exists)
786     (setq user-init-file target-file-name)
787     (let ((old-custom-file custom-file))
788       (custom-migrate-custom-file target-file-name)
789       (customize-save-variable 'load-home-init-file t)
790       (delete-file old-custom-file))))
791
792 (defun load-user-init-file ()
793   "This function actually reads the init file."
794   (if (not user-init-file)
795       (setq user-init-file
796             (find-user-init-file user-init-directory)))
797   (if (and user-init-file
798            (file-readable-p user-init-file))
799       (load user-init-file t t t))
800   (if (not custom-file)
801       (setq custom-file (make-custom-file-name user-init-file)))
802   (if (and custom-file
803            (or (not user-init-file)
804                (not (string= custom-file user-init-file)))
805            (file-readable-p custom-file))
806       (load custom-file t t t))
807   (unless inhibit-default-init
808     (let ((inhibit-startup-message nil))
809       ;; Users are supposed to be told their rights.
810       ;; (Plus how to get help and how to undo.)
811       ;; Don't you dare turn this off for anyone except yourself.
812       (load "default" t t))))
813
814 ;;; Load user's init file and default ones.
815 (defun load-init-file ()
816   (run-hooks 'before-init-hook)
817
818   ;; Run the site-start library if it exists.  The point of this file is
819   ;; that it is run before .emacs.  There is no point in doing this after
820   ;; .emacs; that is useless.
821   (when site-start-file
822     (load site-start-file t t))
823
824   ;; Sites should not disable this.  Only individuals should disable
825   ;; the startup message.
826   (setq inhibit-startup-message nil)
827
828   (let (debug-on-error-from-init-file
829         debug-on-error-should-be-set
830         (debug-on-error-initial
831          (if (eq init-file-debug t) 'startup init-file-debug)))
832     (let ((debug-on-error debug-on-error-initial))
833       (if (and load-user-init-file-p init-file-debug)
834           (progn
835             ;; Do this without a condition-case if the user wants to debug.
836             (load-user-init-file))
837         (condition-case error
838             (progn
839               (if load-user-init-file-p
840                   (load-user-init-file))
841               (setq init-file-had-error nil))
842           (error
843            (message "Error in init file: %s" (error-message-string error))
844            (display-warning 'initialization
845              (format "\
846 An error has occurred while loading %s:
847
848 %s
849
850 To ensure normal operation, you should investigate the cause of the error
851 in your initialization file and remove it.  Use the `-debug-init' option
852 to XEmacs to view a complete error backtrace."
853                      user-init-file (error-message-string error))
854              'error)
855            (setq init-file-had-error t))))
856       ;; If we can tell that the init file altered debug-on-error,
857       ;; arrange to preserve the value that it set up.
858       (or (eq debug-on-error debug-on-error-initial)
859           (setq debug-on-error-should-be-set t
860                 debug-on-error-from-init-file debug-on-error)))
861     (when debug-on-error-should-be-set
862       (setq debug-on-error debug-on-error-from-init-file)))
863
864   (setq init-file-loaded t)
865
866   ;; Do this here in case the init file sets mail-host-address.
867   ;; Don't do this here unless noninteractive, it is frequently wrong. -sb
868   ;; (or user-mail-address
869   (when noninteractive
870     (setq user-mail-address (concat (user-login-name) "@"
871                                     (or mail-host-address
872                                         (system-name)))))
873
874   (run-hooks 'after-init-hook)
875   nil)
876
877 (defun load-options-file (filename)
878   "Load the file of saved options (from the Options menu) called FILENAME.
879 Currently this does nothing but call `load', but it might be redefined
880 in the future to support automatically converting older options files to
881 a new format, when variables have changed, etc."
882   (load filename))
883
884 (defun command-line-1 ()
885   (cond
886    ((null command-line-args-left)
887     (unless noninteractive
888       ;; If there are no switches to process, run the term-setup-hook
889       ;; before displaying the copyright notice; there may be some need
890       ;; to do it before doing any output.  If we're not going to
891       ;; display a copyright notice (because other options are present)
892       ;; then this is run after those options are processed.
893       (run-hooks 'term-setup-hook)
894       ;; Don't let the hook be run twice.
895       (setq term-setup-hook nil)
896
897       ;; Don't clobber a non-scratch buffer if init file
898       ;; has selected it.
899       (when (string= (buffer-name) "*scratch*")
900         (unless (or inhibit-startup-message
901                     (input-pending-p))
902           (let (tmout circ-tmout)
903             (unwind-protect
904                 ;; Guts of with-timeout
905                 (catch 'tmout
906                   (setq tmout (add-timeout startup-message-timeout
907                                            (lambda (ignore)
908                                              (condition-case nil
909                                                  (throw 'tmout t)
910                                                (error nil)))
911                                            nil))
912                   (setq circ-tmout (display-splash-frame))
913                   (or nil;; (pos-visible-in-window-p (point-min))
914                       (goto-char (point-min)))
915                   (sit-for 0)
916                   (setq unread-command-event (next-command-event)))
917               (when tmout (disable-timeout tmout))
918               (when circ-tmout (disable-timeout circ-tmout)))))
919         (with-current-buffer (get-buffer "*scratch*")
920           ;; In case the XEmacs server has already selected
921           ;; another buffer, erase the one our message is in.
922           (erase-buffer)
923           (when (stringp initial-scratch-message)
924             (insert initial-scratch-message))
925           (set-buffer-modified-p nil)))))
926
927    (t
928     ;; Command-line-options exist
929     (let ((dir command-line-default-directory)
930           (file-count 0)
931           (line nil)
932           (end-of-options nil)
933           file-p arg tem)
934       (while command-line-args-left
935         (setq arg (pop command-line-args-left))
936         (cond
937          (end-of-options
938           (setq file-p t))
939          ((setq tem (when (eq (aref arg 0) ?-)
940                       (or (assoc arg command-switch-alist)
941                           (assoc (substring arg 1)
942                                  command-switch-alist))))
943           (funcall (cdr tem) arg))
944          ((string-match "\\`\\+[0-9]+\\'" arg)
945           (setq line (string-to-int arg)))
946          ;; "- file" means don't treat "file" as a switch
947          ;;  ("+0 file" has the same effect; "-" added
948          ;;   for unixoidiality).
949          ;; This is worthless; the `unixoid' way is "./file". -jwz
950          ((or (string= arg "-") (string= arg "--"))
951           (setq end-of-options t))
952          (t
953           (setq file-p t)))
954
955         (when file-p
956           (setq file-p nil)
957           (incf file-count)
958           (setq arg (expand-file-name arg dir))
959           (cond
960            ((= file-count 1)
961             (find-file arg))
962            (noninteractive (find-file arg))
963            (t (find-file-other-window arg)))
964           (when line
965             (goto-line line)
966             (setq line nil))))))))
967
968 (defvar startup-presentation-hack-keymap
969   (let ((map (make-sparse-keymap)))
970     (set-keymap-name map 'startup-presentation-hack-keymap)
971     (define-key map '[button1] 'startup-presentation-hack)
972     (define-key map '[button2] 'startup-presentation-hack)
973     map)
974   "Putting yesterday in the future tomorrow.")
975
976 (defun startup-presentation-hack ()
977   (interactive)
978   (let ((e last-command-event))
979     (and (button-press-event-p e)
980          (setq e (extent-at (event-point e)
981                             (event-buffer e)
982                             'startup-presentation-hack))
983          (setq e (extent-property e 'startup-presentation-hack))
984          (if (consp e)
985              (apply (car e) (cdr e))
986            (while (keymapp (indirect-function e))
987              (let ((map e)
988                    (overriding-local-map (indirect-function e)))
989                (setq e (read-key-sequence
990                         (let ((p (keymap-prompt map t)))
991                           (cond ((symbolp map)
992                                  (if p
993                                      (format "%s %s " map p)
994                                    (format "%s " map)))
995                                 (p)
996                                 (t
997                                  (prin1-to-string map))))))
998                (if (and (button-release-event-p (elt e 0))
999                         (null (key-binding e)))
1000                    (setq e map)         ; try again
1001                  (setq e (key-binding e)))))
1002            (call-interactively e)))))
1003
1004 (defun startup-presentation-hack-help (e)
1005   (setq e (extent-property e 'startup-presentation-hack))
1006   (if (consp e)
1007       (format "Evaluate %S" e)
1008     (symbol-name e)))
1009
1010 (defun splash-frame-present-hack (e v)
1011   ;;   (set-extent-property e 'mouse-face 'highlight)
1012   ;;   (set-extent-property e 'keymap
1013   ;;                           startup-presentation-hack-keymap)
1014   ;;   (set-extent-property e 'startup-presentation-hack v)
1015   ;;   (set-extent-property e 'help-echo
1016   ;;                           'startup-presentation-hack-help)
1017   )
1018
1019 (defun splash-hack-version-string ()
1020   (save-excursion
1021     (save-restriction
1022       (goto-char (point-min))
1023       (re-search-forward "^XEmacs" nil t)
1024       (narrow-to-region (point-at-bol) (point-at-eol))
1025       (goto-char (point-min))
1026       (when (re-search-forward " \\[Lucid\\]" nil t)
1027         (delete-region (match-beginning 0) (match-end 0)))
1028       (when (re-search-forward "[^(][^)]*-[^)]*-" nil t)
1029         (delete-region (1+ (match-beginning 0)) (match-end 0))
1030         (insert "("))
1031       (goto-char (point-max))
1032       (search-backward " " nil t)
1033       (when (search-forward "." nil t)
1034         (delete-region (1- (point)) (point-max))))))
1035
1036 (defun splash-frame-present (l)
1037   (cond ((stringp l)
1038          (insert l))
1039         ((eq (car-safe l) 'face)
1040          ;; (face name string)
1041          (let ((p (point)))
1042            (splash-frame-present (elt l 2))
1043            (if (fboundp 'set-extent-face)
1044                (set-extent-face (make-extent p (point))
1045                                 (elt l 1)))))
1046         ((eq (car-safe l) 'key)
1047          (let* ((c (elt l 1))
1048                 (p (point))
1049                 (k (where-is-internal c nil t)))
1050            (insert (if k (key-description k)
1051                      (format "M-x %s" c)))
1052            (if (fboundp 'set-extent-face)
1053                (let ((e (make-extent p (point))))
1054                  (set-extent-face e 'bold)
1055                  (splash-frame-present-hack e c)))))
1056         ((eq (car-safe l) 'funcall)
1057          ;; (funcall (fun . args) string)
1058          (let ((p (point)))
1059            (splash-frame-present (elt l 2))
1060            (if (fboundp 'set-extent-face)
1061                (splash-frame-present-hack (make-extent p (point))
1062                                           (elt l 1)))))
1063         ((consp l)
1064          (mapcar 'splash-frame-present l))
1065         (t
1066          (error "WTF!?"))))
1067
1068 (defun startup-center-spaces (glyph)
1069   ;; Return the number of spaces to insert in order to center
1070   ;; the given glyph (may be a string or a pixmap).
1071   ;; Assume spaces are as wide as avg-pixwidth.
1072   ;; Won't be quite right for proportional fonts, but it's the best we can do.
1073   ;; Maybe the new redisplay will export something a glyph-width function.
1074   ;;; #### Yes, there is a glyph-width function but it isn't quite what
1075   ;;; #### this was expecting.  Or is it?
1076   ;; (An alternate way to get avg-pixwidth would be to use x-font-properties
1077   ;; and calculate RESOLUTION_X * AVERAGE_WIDTH / 722.7, but it's no better.)
1078
1079   ;; This function is used in about.el too.
1080   (let* ((avg-pixwidth     (round (/ (frame-pixel-width) (frame-width))))
1081          (fill-area-width  (* avg-pixwidth (- fill-column left-margin)))
1082          (glyph-pixwidth   (cond ((stringp glyph)
1083                                   (* avg-pixwidth (length glyph)))
1084                                  ;; #### the pixmap option should be removed
1085                                  ;;((pixmapp glyph)
1086                                  ;; (pixmap-width glyph))
1087                                  ((glyphp glyph)
1088                                   (glyph-width glyph))
1089                                  (t
1090                                   (error "startup-center-spaces: bad arg")))))
1091     (+ left-margin
1092        (round (/ (/ (- fill-area-width glyph-pixwidth) 2) avg-pixwidth)))))
1093
1094 (defun splash-frame-body ()
1095   `[((face (blue bold underline)
1096            "\nDistribution, copying license, warranty:\n\n")
1097      "Please visit the XEmacs website at http://www.xemacs.org !\n\n"
1098      ,@(if (featurep 'sparcworks)
1099            `( "\
1100 Sun provides support for the WorkShop/XEmacs integration package only.
1101 All other XEmacs packages are provided to you \"AS IS\".\n"
1102               ,@(let ((lang (or (getenv "LC_ALL") (getenv "LC_MESSAGES")
1103                                 (getenv "LANG"))))
1104                   (if (and
1105                        (not (featurep 'mule)) ;; Already got mule?
1106                        ;; No Mule support on tty's yet
1107                        (not (eq 'tty (console-type)))
1108                        lang ;; Non-English locale?
1109                        (not (string= lang "C"))
1110                        (not (string-match "^en" lang))
1111                        ;; Comes with Sun WorkShop
1112                        (locate-file "xemacs-mule" exec-path))
1113                       '( "\
1114 This version of XEmacs has been built with support for Latin-1 languages only.
1115 To handle other languages you need to run a Multi-lingual (`Mule') version of
1116 XEmacs, by either running the command `xemacs-mule', or by using the X resource
1117 `ESERVE*defaultXEmacsPath: xemacs-mule' when starting XEmacs from Sun WorkShop.
1118 \n")))))
1119      ((key describe-no-warranty)
1120       ": "(face (red bold) "XEmacs comes with ABSOLUTELY NO WARRANTY\n"))
1121      ((key describe-copying)
1122       ": conditions to give out copies of XEmacs\n")
1123      ((key describe-distribution)
1124       ": how to get the latest version\n")
1125      "\n--\n"
1126      (face italic "\
1127 Copyright (C) 1985-1999 Free Software Foundation, Inc.
1128 Copyright (C) 1990-1994 Lucid, Inc.
1129 Copyright (C) 1993-1997 Sun Microsystems, Inc. All Rights Reserved.
1130 Copyright (C) 1994-1996 Board of Trustees, University of Illinois
1131 Copyright (C) 1995-1996 Ben Wing\n"))
1132
1133     ((face (blue bold underline) "\nInformation, on-line help:\n\n")
1134      "XEmacs comes with plenty of documentation...\n\n"
1135      ,@(if (string-match "beta" emacs-version)
1136            `((key describe-beta)
1137              ": " (face (red bold)
1138                         "This is an Experimental version of XEmacs.\n"))
1139          `( "\n"))
1140      ((key xemacs-local-faq)
1141       ": read the XEmacs FAQ (a " (face underline "capital") " F!)\n")
1142      ((key help-with-tutorial)
1143       ": read the XEmacs tutorial (also available through the "
1144       (face bold "Help") " menu)\n")
1145      ((key help-command)
1146       ": get help on using XEmacs (also available through the "
1147       (face bold "Help") " menu)\n")
1148      ((key info) ": read the on-line documentation\n\n")
1149      ((key describe-project) ": read about the GNU project\n")
1150      ((key about-xemacs) ": see who's developing XEmacs\n"))
1151
1152     ((face (blue bold underline) "\nUseful stuff:\n\n")
1153      "Things that you should know rather quickly...\n\n"
1154      ((key find-file) ": visit a file\n")
1155      ((key save-buffer) ": save changes\n")
1156      ((key advertised-undo) ": undo changes\n")
1157      ((key save-buffers-kill-emacs) ": exit XEmacs\n"))
1158     ])
1159
1160 ;; I really hate global variables, oh well.
1161 ;(defvar xemacs-startup-logo-function nil
1162 ;  "If non-nil, function called to provide the startup logo.
1163 ;This function should return an initialized glyph if it is used.")
1164
1165 ;; This will hopefully go away when gettext is functional.
1166 (defconst splash-frame-static-body
1167   `(,(emacs-version) "\n\n"
1168     (face italic "`C-' means the control key,`M-' means the meta key\n\n")))
1169
1170
1171 (defun circulate-splash-frame-elements (client-data)
1172   (with-current-buffer (aref client-data 2)
1173     (let ((buffer-read-only nil)
1174           (elements (aref client-data 3))
1175           (indice (aref client-data 0)))
1176       (goto-char (aref client-data 1))
1177       (delete-region (point) (point-max))
1178       (splash-frame-present (aref elements indice))
1179       (set-buffer-modified-p nil)
1180       (aset client-data 0
1181             (if (= indice (- (length elements) 1))
1182                 0
1183               (1+ indice )))
1184       )))
1185
1186 ;; #### This function now returns the (possibly nil) timeout circulating the
1187 ;; splash-frame elements
1188 (defun display-splash-frame ()
1189   (let ((logo xemacs-logo)
1190         (buffer-read-only nil)
1191         (cramped-p (eq 'tty (console-type))))
1192     (unless cramped-p (insert "\n"))
1193     (indent-to (startup-center-spaces logo))
1194     (set-extent-begin-glyph (make-extent (point) (point)) logo)
1195     ;;(splash-frame-present-hack (make-extent p (point)) 'about-xemacs))
1196     (insert "\n\n")
1197     (splash-frame-present splash-frame-static-body)
1198     (splash-hack-version-string)
1199     (goto-char (point-max))
1200     (let* ((after-change-functions nil) ; no font-lock, thank you
1201            (elements (splash-frame-body))
1202            (client-data `[ 1 ,(point) ,(current-buffer) ,elements ])
1203            tmout)
1204       (if (listp  elements) ;; A single element to display
1205           (splash-frame-present (splash-frame-body))
1206         ;; several elements to rotate
1207         (splash-frame-present (aref elements 0))
1208         (setq tmout (add-timeout splash-frame-timeout
1209                                  'circulate-splash-frame-elements
1210                                  client-data splash-frame-timeout)))
1211       (set-buffer-modified-p nil)
1212       tmout)))
1213
1214 ;;  (let ((present-file
1215 ;;         #'(lambda (f)
1216 ;;             (splash-frame-present
1217 ;;            (list 'funcall
1218 ;;                  (list 'find-file-other-window
1219 ;;                        (expand-file-name f data-directory))
1220 ;;                  f)))))
1221 ;;    (insert "For customization examples, see the files ")
1222 ;;    (funcall present-file "sample.emacs")
1223 ;;    (insert " and ")
1224 ;;    (funcall present-file "sample.Xdefaults")
1225 ;;    (insert (format "\nin the directory %s." data-directory)))
1226
1227 (defun startup-set-invocation-environment ()
1228   ;; XEmacs -- Steven Baur says invocation directory is nil if you
1229   ;; try to use XEmacs as a login shell.
1230   (or invocation-directory (setq invocation-directory default-directory))
1231   (setq invocation-directory
1232         ;; don't let /tmp_mnt/... get into the load-path or exec-path.
1233         (abbreviate-file-name invocation-directory)))
1234
1235 (defun startup-setup-paths (roots user-init-directory
1236                                   &optional
1237                                   inhibit-early-packages inhibit-site-lisp
1238                                   debug-paths)
1239   "Setup all the various paths.
1240 ROOTS is a list of plausible roots of the XEmacs directory hierarchy.
1241 If INHIBIT-PACKAGES is non-NIL, don't do packages.
1242 If INHIBIT-SITE-LISP is non-NIL, don't do site-lisp.
1243 If DEBUG-PATHS is non-NIL, print paths as they are detected.
1244 It's idempotent, so call this as often as you like!"
1245
1246   (apply #'(lambda (early late last)
1247              (setq early-packages (and (not inhibit-early-packages)
1248                                        early))
1249              (setq late-packages late)
1250              (setq last-packages last))
1251          (packages-find-packages
1252           roots
1253           (packages-compute-package-locations user-init-directory)))
1254
1255   (setq early-package-load-path (packages-find-package-load-path early-packages))
1256   (setq late-package-load-path (packages-find-package-load-path late-packages))
1257   (setq last-package-load-path (packages-find-package-load-path last-packages))
1258
1259   (if debug-paths
1260       (progn
1261         (princ (format "configure-package-path:\n%S\n" configure-package-path)
1262                'external-debugging-output)
1263         (princ (format "early-packages and early-package-load-path:\n%S\n%S\n"
1264                        early-packages early-package-load-path)
1265                'external-debugging-output)
1266         (princ (format "late-packages and late-package-load-path:\n%S\n%S\n"
1267                        late-packages late-package-load-path)
1268                'external-debugging-output)
1269         (princ (format "last-packages and last-package-load-path:\n%S\n%S\n"
1270                        last-packages last-package-load-path)
1271                'external-debugging-output)))
1272
1273   (setq lisp-directory (paths-find-lisp-directory roots))
1274
1275   (if debug-paths
1276       (princ (format "lisp-directory:\n%S\n" lisp-directory)
1277              'external-debugging-output))
1278
1279   (if (featurep 'mule)
1280       (progn
1281         (setq mule-lisp-directory
1282               (paths-find-mule-lisp-directory roots
1283                                               lisp-directory))
1284         (if debug-paths
1285             (princ (format "mule-lisp-directory:\n%S\n"
1286                            mule-lisp-directory)
1287                    'external-debugging-output)))
1288     (setq mule-lisp-directory '()))
1289
1290   (setq site-directory (and (null inhibit-site-lisp)
1291                             (paths-find-site-lisp-directory roots)))
1292
1293   (if (and debug-paths (null inhibit-site-lisp))
1294       (princ (format "site-directory:\n%S\n" site-directory)
1295              'external-debugging-output))
1296
1297   (setq load-path (paths-construct-load-path roots
1298                                              early-package-load-path
1299                                              late-package-load-path
1300                                              last-package-load-path
1301                                              lisp-directory
1302                                              site-directory
1303                                              mule-lisp-directory))
1304
1305   (setq Info-directory-list
1306         (paths-construct-info-path roots
1307                                    early-packages late-packages last-packages))
1308
1309
1310   (if debug-paths
1311       (princ (format "Info-directory-list:\n%S\n" Info-directory-list)
1312              'external-debugging-output))
1313
1314   (setq exec-directory (paths-find-exec-directory roots))
1315
1316   (if debug-paths
1317       (princ (format "exec-directory:\n%s\n" exec-directory)
1318              'external-debugging-output))
1319
1320   (setq exec-path
1321         (paths-construct-exec-path roots exec-directory
1322                                    early-packages late-packages last-packages))
1323
1324   (if debug-paths
1325       (princ (format "exec-path:\n%S\n" exec-path)
1326              'external-debugging-output))
1327
1328   (setq doc-directory (paths-find-doc-directory roots))
1329
1330   (if debug-paths
1331       (princ (format "doc-directory:\n%S\n" doc-directory)
1332              'external-debugging-output))
1333
1334   (setq data-directory (paths-find-data-directory roots))
1335
1336   (if debug-paths
1337       (princ (format "data-directory:\n%S\n" data-directory)
1338              'external-debugging-output))
1339
1340   (setq data-directory-list (paths-construct-data-directory-list data-directory
1341                                                                  early-packages
1342                                                                  late-packages
1343                                                                  last-packages))
1344   (if debug-paths
1345       (princ (format "data-directory-list:\n%S\n" data-directory-list)
1346              'external-debugging-output)))
1347
1348 (defun startup-find-roots-warning ()
1349   (save-excursion
1350     (set-buffer (get-buffer-create " *warning-tmp*"))
1351     (erase-buffer)
1352     (buffer-disable-undo (current-buffer))
1353
1354     (insert "Couldn't find an obvious default for the root of the\n"
1355             "XEmacs hierarchy.")
1356
1357     (princ "\nWARNING:\n" 'external-debugging-output)
1358     (princ (buffer-string) 'external-debugging-output)))
1359
1360 (defun startup-setup-paths-warning ()
1361   (let ((warnings '()))
1362     (cond
1363      ((null (and lisp-directory exec-directory data-directory doc-directory
1364                  load-path))
1365       (save-excursion
1366         (set-buffer (get-buffer-create " *warning-tmp*"))
1367         (erase-buffer)
1368         (buffer-disable-undo (current-buffer))
1369         (if (null lisp-directory) (push "lisp-directory" warnings))
1370         (if (and (featurep 'mule)
1371                  (null mule-lisp-directory))
1372             (push "mule-lisp-directory" warnings))
1373         (if (null exec-directory) (push "exec-directory" warnings))
1374         (if (null data-directory) (push "data-directory" warnings))
1375         (if (null doc-directory)  (push "doc-directory"  warnings))
1376         (if (null load-path)      (push "load-path"      warnings))
1377
1378         (insert "Couldn't find obvious defaults for:\n")
1379         (while warnings
1380           (insert (car warnings) "\n")
1381           (setq warnings (cdr warnings)))
1382         (insert "Perhaps some directories don't exist, "
1383                 "or the XEmacs executable,\n" (concat invocation-directory
1384                                                      invocation-name)
1385                 "\nis in a strange place?")
1386
1387         (princ "\nWARNING:\n" 'external-debugging-output)
1388         (princ (buffer-string) 'external-debugging-output)
1389         (erase-buffer)
1390         t)))))
1391
1392 ;;; startup.el ends here