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