1 ;;; startup.el --- process SXEmacs shell arguments
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
8 ;; Maintainer: SXEmacs Development Team
9 ;; Keywords: internal, dumped
11 ;; This file is part of SXEmacs.
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.
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.
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/>.
26 ;;; Synched up with: FSF 19.34.
30 ;; This file is dumped with SXEmacs.
32 ;; -batch, -t, and -nw are processed by main() in emacs.c and are
33 ;; never seen by lisp code.
35 ;; -version and -help are special-cased as well: they imply -batch,
36 ;; but are left on the list for lisp code to process.
40 (setq top-level '(normal-top-level))
42 (defvar command-line-processed nil "t once command line has been processed")
44 (defconst startup-message-timeout 12000) ; More or less disable the timeout
45 (defconst splash-frame-timeout 10) ; interval between splash frame elements
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.")
52 (defconst inhibit-default-init nil
53 "*Non-nil inhibits loading the `default' library.")
55 (defvar command-line-args-left nil
56 "List of command-line args not yet processed.") ; bound by `command-line'
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.")
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.")
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'.")
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.")
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
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.")
89 (defconst initial-major-mode 'lisp-interaction-mode
90 "Major mode command symbol to use for the initial *scratch* buffer.")
92 (defvar emacs-roots nil
93 "List of plausible roots of the SXEmacs hierarchy.")
95 (defun find-user-init-directory ()
96 "Find and set the user's init directory.
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
103 If the legacy init directory, \"~/.sxemacs\" exists, return that.
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
108 (let* ((legacy (getenv "SXE_USE_LEGACY"))
109 (xdg (getenv "XDG_CONFIG_HOME"))
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)))
120 (setq user-init-directory (file-name-as-directory legacydir))
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
129 (lwarn 'multi-initd nil
130 "Multiple init directories found:
135 See `display-warning-suppressed-classes' to suppress this warning"
136 locations user-init-directory))))
138 (defvar user-init-directory ""
139 "Directory where user-installed init files may go.
141 See: `find-user-init-directory'.")
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.")
147 (defvar load-user-init-file-p t
148 "Non-nil if SXEmacs should load the user's init file.")
150 ;; #### called `site-run-file' in FSFmacs
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:
159 1. file described in this variable, if non-nil;
161 3. `/path/to/sxemacs/lisp/default.el'.
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.")
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.")
175 (defcustom mail-host-address nil
176 "*Name of this machine, for purposes of naming users."
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'."
187 (defvar init-file-debug nil)
189 (defvar init-file-had-error nil)
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.")
196 (defvar initial-frame-unmapped-p nil)
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.
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'.")
224 ;;; Note: these doc strings are semi-magical.
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"))
232 (concat "When creating a window on an X display, "
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.
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)
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)
274 (incf len (length (car x)))
277 (terpri) (setq len 0))
282 (let ((name (car (car l)))
286 ((and (symbolp fn) (get fn 'undocumented)) nil)
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))))
300 (funcall insert name " " arg)
301 (funcall insert name))
305 (princ (concat "+N <file> Start displaying <file> at line N.
307 Anything else is considered a file name, and is placed into a buffer for
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")
316 (defun command-line-do-funcall (arg)
317 "Invoke the named lisp function with no arguments.
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)
323 (defun command-line-do-eval (arg)
324 "Evaluate the lisp form. Quote it carefully.
326 (eval (read (pop command-line-args-left))))
328 (defun command-line-do-load (arg)
329 "Load the named file of Lisp code into SXEmacs.
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)))
338 (defun command-line-do-insert (arg)
339 "Insert file into the current buffer.
341 (insert-file-contents (pop command-line-args-left)))
343 (defun command-line-do-kill (arg)
347 (defun command-line-do-version (arg)
348 "Print version info and exit."
349 (princ (concat (emacs-version) "\n"))
353 ;;; Processing the command line and loading various init files
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.
360 ;; output the contents of the warning buffer, since it won't be seen
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))
368 (let ((string "Initialization error")
369 (error (nth 1 debugger-args))
371 (stream 'external-debugging-output))
373 (princ string stream)
374 (princ (concat "\n" string ": ") stream)
376 (display-error error stream)
377 (error (princ "<<< error printing error message >>>" stream)))
379 (if (memq (car-safe error) '(void-function void-variable))
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"
384 (when (not suppress-early-error-handler-backtrace)
385 (let ((print-length 1000)
387 (print-escape-newlines t)
388 (print-readably nil))
389 (when (getenv "EMACSLOADPATH")
390 (princ (format "\n$EMACSLOADPATH is %s" (getenv "EMACSLOADPATH"))
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)))
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)))
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)
421 (startup-set-invocation-environment)
423 (let ((debug-paths (or debug-paths
424 (and (getenv "EMACSDEBUGPATHS")
427 (setq emacs-roots (paths-find-emacs-roots invocation-directory
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))
437 (if (null emacs-roots)
438 (startup-find-roots-warning))
439 (startup-setup-paths emacs-roots
441 inhibit-early-packages
443 (startup-setup-paths-warning))
445 ;; we set up a separate function, so that a user can later decide
446 ;; to load the autoloads
447 (startup-load-autoloads)
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
462 auto-save-list-file-prefix
465 (run-hooks 'emacs-startup-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)
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))))
485 ;; FSF calls precompute-menubar-bindings. We don't mix menubars
489 (defvar forced-user-init-directory nil
490 "Non-nil when `user-init-directory' is set on cmd line.
492 Internal variable, DO NOT USE.")
494 (defun command-line-early (args)
495 ;; This processes those switches which need to be processed before
496 ;; starting up the window system.
498 (setq command-line-default-directory default-directory)
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))))
513 (setq load-user-init-file-p (not (noninteractive)))
515 ;; Allow (at least) these arguments anywhere in the command line
519 (setq arg (pop args))
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
552 inhibit-early-packages
554 (unless inhibit-early-packages
555 (unless inhibit-autoloads
556 (packages-load-package-auto-autoloads
557 early-package-load-path)))
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)
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
587 inhibit-early-packages
589 (unless inhibit-early-packages
590 (unless inhibit-autoloads
591 (packages-load-package-auto-autoloads
592 early-package-load-path)))
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"))
606 ((or (string= arg "--") (string= arg "-"))
608 (push (pop args) new-args)))
609 (t (push arg new-args))))
611 ;; obsolete, initialize for backward compatibility
612 (with-obsolete-variable 'init-file-user
613 (setq init-file-user (and load-user-init-file-p "")))
615 (if (and debug-paths forced-user-init-directory)
617 (princ (format "user-init-directory:\n%S\n"
619 'external-debugging-output)
620 (princ (format "lisp-initd-dir:\n\%S\n" lisp-initd-dir)
621 'external-debugging-output)))
623 (nreverse new-args)))
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.
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
634 ;; For Lisp evaluation, type an expression, move to the end and hit C-j.
638 "Initial message displayed in *scratch* buffer at startup.
639 If this is nil, no message will be displayed.")
641 (defun command-line ()
642 (let ((command-line-args-left (cdr command-line-args)))
644 (let ((debugger 'early-error-handler)
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))
654 ;; Setup the toolbar icon directory
655 (when (featurep 'toolbar)
656 (init-toolbar-location))
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)
666 ;; When not in batch mode, this creates the first visible frame,
667 ;; and deletes the stdio device.
670 ;; Reinitialize faces if necessary. This function changes face if
671 ;; it is created during auto-autoloads loading. Otherwise, it
673 (startup-initialize-custom-faces)
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.)
684 (with-current-buffer (get-buffer "*scratch*")
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))
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))
700 ;; Process the remaining args.
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))))
709 (defun load-terminal-library ()
710 "Load the terminal initialization library"
711 (when term-file-prefix
712 (let ((term (getenv "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))))))))
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)))
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)))))))
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))
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)
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
749 (if (and user-init-file
750 (file-readable-p user-init-file))
751 (load user-init-file t t t))
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))))
764 ;;; Load user's init file and default ones.
765 (defun load-init-file ()
767 (require 'const-aliases)
769 (run-hooks 'before-init-hook)
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))
777 ;; Sites should not disable this. Only individuals should disable
778 ;; the startup message.
779 (setq inhibit-startup-message nil)
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)
790 ;; Do this without a condition-case if the user wants to debug.
791 (load-user-init-file))
792 (condition-case error
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))
799 (message "Error in init file: %s" (error-message-string error))
800 (display-warning 'initialization
801 (format "An error has occurred while loading %s:
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))
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)))
819 (setq init-file-loaded t)
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
825 (setq user-mail-address (concat (user-login-name) "@"
826 (or mail-host-address
829 (run-hooks 'after-init-hook)
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."
839 (defun command-line-1 ()
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)
852 ;; Don't clobber a non-scratch buffer if init file
854 (when (string= (buffer-name) "*scratch*")
855 (unless (or inhibit-startup-message
857 (let (tmout circ-tmout)
859 ;; Guts of with-timeout
861 (setq tmout (add-timeout startup-message-timeout
867 (setq circ-tmout (display-splash-frame))
868 (or nil;; (pos-visible-in-window-p (point-min))
869 (goto-char (point-min)))
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.
878 (when (stringp initial-scratch-message)
879 (insert initial-scratch-message))
880 (set-buffer-modified-p nil)))))
883 ;; Command-line-options exist
884 (let ((dir command-line-default-directory)
889 (while command-line-args-left
890 (setq arg (pop command-line-args-left))
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))
913 (setq arg (expand-file-name arg dir))
917 (noninteractive (find-file arg))
918 (t (find-file-other-window arg)))
921 (setq line nil))))))))
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)
929 "Putting yesterday in the future tomorrow.")
931 (defun startup-presentation-hack ()
933 (let ((e last-command-event))
934 (and (button-press-event-p e)
935 (setq e (extent-at (event-point e)
937 'startup-presentation-hack))
938 (setq e (extent-property e 'startup-presentation-hack))
940 (apply (car e) (cdr e))
941 (while (keymapp (indirect-function e))
943 (overriding-local-map (indirect-function e)))
944 (setq e (read-key-sequence
945 (let ((p (keymap-prompt map t)))
948 (format "%s %s " map p)
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)))))
959 (defun startup-presentation-hack-help (e)
960 (setq e (extent-property e 'startup-presentation-hack))
962 (format "Evaluate %S" e)
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)
974 (defun splash-frame-present (l)
977 ((eq (car-safe l) 'face)
978 ;; (face name string)
980 (splash-frame-present (elt l 2))
981 (if (fboundp 'set-extent-face)
982 (set-extent-face (make-extent p (point))
984 ((eq (car-safe l) 'key)
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)
997 (splash-frame-present (elt l 2))
998 (if (fboundp 'set-extent-face)
999 (splash-frame-present-hack (make-extent p (point))
1002 (mapcar 'splash-frame-present l))
1004 (backtrace 'external-debugging-output)
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.)
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
1025 ;; (pixmap-width glyph))
1027 (glyph-width glyph))
1029 (error "startup-center-spaces: bad arg")))))
1031 (round (/ (/ (- fill-area-width glyph-pixwidth) 2) avg-pixwidth)))))
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"))
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.
1049 - To foster a user and developer friendly project environment.
1051 - And, above all, to have fun doing it.\n"
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"))
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"))
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")
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"))
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"))
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.")
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")))
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)
1109 (if (= indice (1- (length elements)))
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))
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 ])
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)
1141 ;; (let ((present-file
1143 ;; (splash-frame-present
1145 ;; (list 'find-file-other-window
1146 ;; (expand-file-name f data-directory))
1148 ;; (insert "For customization examples, see the files ")
1149 ;; (funcall present-file "sample.emacs")
1151 ;; (funcall present-file "sample.Xdefaults")
1152 ;; (insert (format "\nin the directory %s." data-directory)))
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)))
1162 (defvar Info-directory-list)
1164 (defun startup-setup-paths (roots userdir
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
1176 (let ((allargs command-line-args)
1177 (initdargs '("-u" "-user" "--user" "-user-init-directory"
1178 "--user-init-directory")))
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))))
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)))))
1195 (setq user-packages-topdir (packages-find-user-topdir))
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)
1203 (setq late-packages late)
1204 (setq last-packages last))
1205 (packages-find-packages
1207 (packages-compute-package-locations user-packages-topdir)))
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))
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)))
1230 (setq lisp-directory (paths-find-lisp-directory roots))
1233 (princ (format "lisp-directory:\n%S\n" lisp-directory)
1234 'external-debugging-output))
1236 (if (featurep 'mule)
1238 (setq mule-lisp-directory
1239 (paths-find-mule-lisp-directory roots
1242 (princ (format "mule-lisp-directory:\n%S\n"
1243 mule-lisp-directory)
1244 'external-debugging-output)))
1245 (setq mule-lisp-directory '()))
1247 (setq ffi-lisp-directory
1248 (when (fboundp #'ffi-defun)
1249 (paths-find-ffi-lisp-directory roots
1253 (setq module-directory (paths-find-module-directory roots))
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))
1263 (setq load-path (paths-construct-load-path roots
1264 early-package-load-path
1265 late-package-load-path
1266 last-package-load-path
1270 ffi-lisp-directory))
1272 (setq Info-directory-list
1273 (paths-construct-info-path roots
1274 early-packages late-packages last-packages))
1278 (princ (format "Info-directory-list:\n%S\n" Info-directory-list)
1279 'external-debugging-output))
1281 (setq exec-directory (paths-find-exec-directory roots))
1284 (princ (format "exec-directory:\n%s\n" exec-directory)
1285 'external-debugging-output))
1288 (paths-construct-exec-path roots exec-directory
1289 early-packages late-packages last-packages))
1292 (princ (format "exec-path:\n%S\n" exec-path)
1293 'external-debugging-output))
1295 (setq doc-directory (paths-find-doc-directory roots))
1298 (princ (format "doc-directory:\n%S\n" doc-directory)
1299 'external-debugging-output))
1301 (setq data-directory (paths-find-data-directory roots))
1304 (princ (format "data-directory:\n%S\n" data-directory)
1305 'external-debugging-output))
1307 (setq data-directory-list
1308 (paths-construct-data-directory-list data-directory
1313 (princ (format "data-directory-list:\n%S\n" data-directory-list)
1314 'external-debugging-output)))
1316 (defun startup-find-roots-warning ()
1318 (set-buffer (get-buffer-create " *warning-tmp*"))
1320 (buffer-disable-undo (current-buffer))
1322 (insert "Couldn't find an obvious default for the root of the\n"
1323 "SXEmacs hierarchy.")
1325 (princ "\nWARNING:\n" 'external-debugging-output)
1326 (princ (buffer-string) 'external-debugging-output)))
1328 (defun startup-setup-paths-warning ()
1329 (let ((warnings '()))
1331 ((null (and lisp-directory exec-directory data-directory doc-directory
1334 (set-buffer (get-buffer-create " *warning-tmp*"))
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))
1346 (insert "Couldn't find obvious defaults for:\n")
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?")
1355 (princ "\nWARNING:\n" 'external-debugging-output)
1356 (princ (buffer-string) 'external-debugging-output)
1361 (defun startup-load-autoloads ()
1362 "Load autoloads from known locations."
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)))
1370 (load (expand-file-name aalfile lisp-directory) nil t)
1373 ;; just load them all
1377 (load (expand-file-name (concat "lisp/" aalfile) root) nil t)
1381 (when (featurep 'mule)
1382 (load (expand-file-name aalfile
1383 (expand-file-name "mule" lisp-directory))
1385 (when (featurep 'modules)
1386 (load (expand-file-name aalfile module-directory) t t))
1388 (when (fboundp #'ffi-defun)
1389 (load (expand-file-name aalfile
1390 (expand-file-name "ffi" lisp-directory))
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))
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)))
1407 ;;; startup.el ends here