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