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 ()
766 (run-hooks 'before-init-hook)
768 ;; Run the site-start library if it exists. The point of this file is
769 ;; that it is run before .emacs. There is no point in doing this after
770 ;; .emacs; that is useless.
771 (when site-start-file
772 (load site-start-file t t))
774 ;; Sites should not disable this. Only individuals should disable
775 ;; the startup message.
776 (setq inhibit-startup-message nil)
778 (let (debug-on-error-from-init-file
779 debug-on-error-should-be-set
780 (debug-on-error-initial
781 (if (eq init-file-debug t) 'startup init-file-debug)))
782 (let ((debug-on-error debug-on-error-initial))
783 ;; #### I believe this test is incorrect, it leads to custom-file
784 ;; (at least) being undefined
785 (if (and load-user-init-file-p init-file-debug)
787 ;; Do this without a condition-case if the user wants to debug.
788 (load-user-init-file))
789 (condition-case error
791 ;; #### probably incorrect, see comment above
792 (if load-user-init-file-p
793 (load-user-init-file))
794 (setq init-file-had-error nil))
796 (message "Error in init file: %s" (error-message-string error))
797 (display-warning 'initialization
798 (format "An error has occurred while loading %s:
802 To ensure normal operation, you should investigate the cause of the error
803 in your initialization file and remove it. Use the `-debug-init' option
804 to SXEmacs to view a complete error backtrace."
805 user-init-file (error-message-string error))
807 (setq init-file-had-error t))))
808 ;; If we can tell that the init file altered debug-on-error,
809 ;; arrange to preserve the value that it set up.
810 (or (eq debug-on-error debug-on-error-initial)
811 (setq debug-on-error-should-be-set t
812 debug-on-error-from-init-file debug-on-error)))
813 (when debug-on-error-should-be-set
814 (setq debug-on-error debug-on-error-from-init-file)))
816 (setq init-file-loaded t)
818 ;; Do this here in case the init file sets mail-host-address.
819 ;; Don't do this here unless noninteractive, it is frequently wrong. -sb
820 ;; (or user-mail-address
822 (setq user-mail-address (concat (user-login-name) "@"
823 (or mail-host-address
826 (run-hooks 'after-init-hook)
829 (defun load-options-file (filename)
830 "Load the file of saved options (from the Options menu) called FILENAME.
831 Currently this does nothing but call `load', but it might be redefined
832 in the future to support automatically converting older options files to
833 a new format, when variables have changed, etc."
836 (defun command-line-1 ()
838 ((null command-line-args-left)
839 (unless noninteractive
840 ;; If there are no switches to process, run the term-setup-hook
841 ;; before displaying the copyright notice; there may be some need
842 ;; to do it before doing any output. If we're not going to
843 ;; display a copyright notice (because other options are present)
844 ;; then this is run after those options are processed.
845 (run-hooks 'term-setup-hook)
846 ;; Don't let the hook be run twice.
847 (setq term-setup-hook nil)
849 ;; Don't clobber a non-scratch buffer if init file
851 (when (string= (buffer-name) "*scratch*")
852 (unless (or inhibit-startup-message
854 (let (tmout circ-tmout)
856 ;; Guts of with-timeout
858 (setq tmout (add-timeout startup-message-timeout
864 (setq circ-tmout (display-splash-frame))
865 (or nil;; (pos-visible-in-window-p (point-min))
866 (goto-char (point-min)))
868 (setq unread-command-event (next-command-event)))
869 (when tmout (disable-timeout tmout))
870 (when circ-tmout (disable-timeout circ-tmout)))))
871 (with-current-buffer (get-buffer "*scratch*")
872 ;; In case the SXEmacs server has already selected
873 ;; another buffer, erase the one our message is in.
875 (when (stringp initial-scratch-message)
876 (insert initial-scratch-message))
877 (set-buffer-modified-p nil)))))
880 ;; Command-line-options exist
881 (let ((dir command-line-default-directory)
886 (while command-line-args-left
887 (setq arg (pop command-line-args-left))
891 ((setq tem (when (eq (aref arg 0) ?-)
892 (or (assoc arg command-switch-alist)
893 (assoc (substring arg 1)
894 command-switch-alist))))
895 (funcall (cdr tem) arg))
896 ((string-match #r"\`\+[0-9]+\'" arg)
897 (setq line (string-to-int arg)))
898 ;; "- file" means don't treat "file" as a switch
899 ;; ("+0 file" has the same effect; "-" added
900 ;; for unixoidiality).
901 ;; This is worthless; the `unixoid' way is "./file". -jwz
902 ((or (string= arg "-") (string= arg "--"))
903 (setq end-of-options t))
910 (setq arg (expand-file-name arg dir))
914 (noninteractive (find-file arg))
915 (t (find-file-other-window arg)))
918 (setq line nil))))))))
920 (defvar startup-presentation-hack-keymap
921 (let ((map (make-sparse-keymap)))
922 (set-keymap-name map 'startup-presentation-hack-keymap)
923 (define-key map '[button1] 'startup-presentation-hack)
924 (define-key map '[button2] 'startup-presentation-hack)
926 "Putting yesterday in the future tomorrow.")
928 (defun startup-presentation-hack ()
930 (let ((e last-command-event))
931 (and (button-press-event-p e)
932 (setq e (extent-at (event-point e)
934 'startup-presentation-hack))
935 (setq e (extent-property e 'startup-presentation-hack))
937 (apply (car e) (cdr e))
938 (while (keymapp (indirect-function e))
940 (overriding-local-map (indirect-function e)))
941 (setq e (read-key-sequence
942 (let ((p (keymap-prompt map t)))
945 (format "%s %s " map p)
949 (prin1-to-string map))))))
950 (if (and (button-release-event-p (elt e 0))
951 (null (key-binding e)))
952 (setq e map) ; try again
953 (setq e (key-binding e)))))
954 (call-interactively e)))))
956 (defun startup-presentation-hack-help (e)
957 (setq e (extent-property e 'startup-presentation-hack))
959 (format "Evaluate %S" e)
962 (defun splash-frame-present-hack (e v)
963 ;; (set-extent-property e 'mouse-face 'highlight)
964 ;; (set-extent-property e 'keymap
965 ;; startup-presentation-hack-keymap)
966 ;; (set-extent-property e 'startup-presentation-hack v)
967 ;; (set-extent-property e 'help-echo
968 ;; 'startup-presentation-hack-help)
971 (defun splash-frame-present (l)
974 ((eq (car-safe l) 'face)
975 ;; (face name string)
977 (splash-frame-present (elt l 2))
978 (if (fboundp 'set-extent-face)
979 (set-extent-face (make-extent p (point))
981 ((eq (car-safe l) 'key)
984 (k (where-is-internal c nil t)))
985 (insert (if k (key-description k)
986 (format "M-x %s" c)))
987 (if (fboundp 'set-extent-face)
988 (let ((e (make-extent p (point))))
989 (set-extent-face e 'bold)
990 (splash-frame-present-hack e c)))))
991 ((eq (car-safe l) 'funcall)
992 ;; (funcall (fun . args) string)
994 (splash-frame-present (elt l 2))
995 (if (fboundp 'set-extent-face)
996 (splash-frame-present-hack (make-extent p (point))
999 (mapcar 'splash-frame-present l))
1001 (backtrace 'external-debugging-output)
1004 (defun startup-center-spaces (glyph)
1005 ;; Return the number of spaces to insert in order to center
1006 ;; the given glyph (may be a string or a pixmap).
1007 ;; Assume spaces are as wide as avg-pixwidth.
1008 ;; Won't be quite right for proportional fonts, but it's the best we can do.
1009 ;; Maybe the new redisplay will export something a glyph-width function.
1010 ;;; #### Yes, there is a glyph-width function but it isn't quite what
1011 ;;; #### this was expecting. Or is it?
1012 ;; (An alternate way to get avg-pixwidth would be to use x-font-properties
1013 ;; and calculate RESOLUTION_X * AVERAGE_WIDTH / 722.7, but it's no better.)
1015 ;; This function is used in about.el too.
1016 (let* ((avg-pixwidth (round (/ (frame-pixel-width) (frame-width))))
1017 (fill-area-width (* avg-pixwidth (- fill-column left-margin)))
1018 (glyph-pixwidth (cond ((stringp glyph)
1019 (* avg-pixwidth (length glyph)))
1020 ;; #### the pixmap option should be removed
1022 ;; (pixmap-width glyph))
1024 (glyph-width glyph))
1026 (error "startup-center-spaces: bad arg")))))
1028 (round (/ (/ (- fill-area-width glyph-pixwidth) 2) avg-pixwidth)))))
1030 (defun splash-frame-body ()
1031 `[((face (blue bold underline)
1032 "\nDistribution, copying license, warranty:\n\n")
1033 "Please visit the SXEmacs website at http://www.sxemacs.org !\n\n"
1034 ((key describe-no-warranty)
1035 ": "(face (red bold) "SXEmacs comes with ABSOLUTELY NO WARRANTY\n"))
1036 ((key describe-copying)
1037 ": conditions to give out copies of SXEmacs\n")
1038 ((key describe-distribution)
1039 ": how to get the latest version\n")
1040 ((face (blue bold underline) "\nOur Mission is:\n\n"))
1042 - To provide the Open Source community with a text editing and
1043 development environment that is based on XEmacs and is 2nd to
1044 none in regards to stability, features, and innovation.
1046 - To foster a user and developer friendly project environment.
1048 - And, above all, to have fun doing it.\n"
1051 Copyright (C) 2004 - 2015 Steve Youngs\n"))
1052 ; Copyright (C) 1985-2001 Free Software Foundation, Inc.
1053 ; Copyright (C) 1990-1994 Lucid, Inc.
1054 ; Copyright (C) 1993-1997 Sun Microsystems, Inc. All Rights Reserved.
1055 ; Copyright (C) 1994-1996 Board of Trustees, University of Illinois
1056 ; Copyright (C) 1995-2001 Ben Wing\n"))
1058 ((face (blue bold underline) "\nInformation, on-line help:\n\n")
1059 "SXEmacs comes with plenty of documentation...\n\n"
1060 ,@(if emacs-beta-version
1061 `((key describe-beta)
1062 ": " (face (red bold)
1063 "This is an Experimental version of SXEmacs.\n"))
1065 ((key sxemacs-local-faq)
1066 ": read the SXEmacs FAQ (a " (face underline "capital") " F!)\n")
1067 ((key help-with-tutorial)
1068 ": read the SXEmacs tutorial (also available through the "
1069 (face bold "Help") " menu)\n")
1071 ": get help on using SXEmacs (also available through the "
1072 (face bold "Help") " menu)\n")
1073 ((key info) ": read the on-line documentation\n\n")
1074 ((key about-sxemacs) ": see who's developing SXEmacs\n"))
1076 ((face (blue bold underline) "\nUseful stuff:\n\n")
1077 "Things that you should learn rather quickly...\n\n"
1078 ((key find-file) ": visit a file\n")
1079 ((key save-buffer) ": save changes\n")
1080 ((key advertised-undo) ": undo changes\n")
1081 ((key save-buffers-kill-emacs) ": exit SXEmacs\n"))
1084 ;; I really hate global variables, oh well.
1085 ;(defvar xemacs-startup-logo-function nil
1086 ; "If non-nil, function called to provide the startup logo.
1087 ;This function should return an initialized glyph if it is used.")
1089 ;; This will hopefully go away when gettext is functional.
1090 (defconst splash-frame-static-body
1091 `(,(emacs-version) "\n\n"
1092 (face italic "`C-' means the control key,`M-' means the meta key\n\n")))
1095 (defun circulate-splash-frame-elements (client-data)
1096 (with-current-buffer (aref client-data 2)
1097 (let ((buffer-read-only nil)
1098 (elements (aref client-data 3))
1099 (indice (aref client-data 0)))
1100 (goto-char (aref client-data 1))
1101 (delete-region (point) (point-max))
1102 (splash-frame-present (aref elements indice))
1103 (goto-char (point-min))
1104 (set-buffer-modified-p nil)
1106 (if (= indice (1- (length elements)))
1111 ;; #### This function now returns the (possibly nil) timeout circulating the
1112 ;; splash-frame elements
1113 (defun display-splash-frame ()
1114 (let ((logo sxemacs-logo)
1115 (buffer-read-only nil)
1116 (cramped-p (eq 'tty (console-type))))
1117 (unless cramped-p (insert "\n"))
1118 (indent-to (startup-center-spaces logo))
1119 (set-extent-begin-glyph (make-extent (point) (point)) logo)
1120 ;;(splash-frame-present-hack (make-extent p (point)) 'about-sxemacs))
1122 (splash-frame-present splash-frame-static-body)
1123 (goto-char (point-max))
1124 (let* ((after-change-functions nil) ; no font-lock, thank you
1125 (elements (splash-frame-body))
1126 (client-data `[ 1 ,(point) ,(current-buffer) ,elements ])
1128 (if (listp elements) ;; A single element to display
1129 (splash-frame-present (splash-frame-body))
1130 ;; several elements to rotate
1131 (splash-frame-present (aref elements 0))
1132 (setq tmout (add-timeout splash-frame-timeout
1133 'circulate-splash-frame-elements
1134 client-data splash-frame-timeout)))
1135 (set-buffer-modified-p nil)
1138 ;; (let ((present-file
1140 ;; (splash-frame-present
1142 ;; (list 'find-file-other-window
1143 ;; (expand-file-name f data-directory))
1145 ;; (insert "For customization examples, see the files ")
1146 ;; (funcall present-file "sample.emacs")
1148 ;; (funcall present-file "sample.Xdefaults")
1149 ;; (insert (format "\nin the directory %s." data-directory)))
1151 (defun startup-set-invocation-environment ()
1152 ;; XEmacs -- Steven Baur says invocation directory is nil if you
1153 ;; try to use XEmacs as a login shell.
1154 (or invocation-directory (setq invocation-directory default-directory))
1155 (setq invocation-directory
1156 ;; don't let /tmp_mnt/... get into the load-path or exec-path.
1157 (abbreviate-file-name invocation-directory)))
1159 (defvar Info-directory-list)
1161 (defun startup-setup-paths (roots userdir
1163 inhibit-early-packages debug-paths)
1164 "Setup all the various paths.
1165 ROOTS is a list of plausible roots of the SXEmacs directory hierarchy.
1166 USERDIR is the user's init directory, possibly computed.
1167 If INHIBIT-EARLY-PACKAGES is non-NIL, don't do user packages.
1168 If DEBUG-PATHS is non-NIL, print paths as they are detected. It's
1169 idempotent, so call this as often as you like!"
1170 ;; Maybe find a good candidate for user-init-directory, but only if
1171 ;; SXEmacs was started without any command line arg that would set
1173 (let ((allargs command-line-args)
1174 (initdargs '("-u" "-user" "--user" "-user-init-directory"
1175 "--user-init-directory")))
1177 (or (member t (mapfam #'string= initdargs allargs :mode 'comb))
1178 forced-user-init-directory)
1179 (find-user-init-directory)
1180 (setq userdir user-init-directory)
1181 (setq lisp-initd-dir (file-name-as-directory
1182 (paths-construct-path
1183 (list userdir lisp-initd-basename))))
1186 (princ (format "user-init-directory:\n%S\n"
1187 user-init-directory)
1188 'external-debugging-output)
1189 (princ (format "lisp-initd-dir:\n\%S\n" lisp-initd-dir)
1190 'external-debugging-output)))))
1192 (setq user-packages-topdir (packages-find-user-topdir))
1194 (princ (format "user-packages-topdir: \n%S\n"
1195 user-packages-topdir)
1196 'external-debugging-output))
1197 (apply #'(lambda (early late last)
1198 (setq early-packages (and (not inhibit-early-packages)
1200 (setq late-packages late)
1201 (setq last-packages last))
1202 (packages-find-packages
1204 (packages-compute-package-locations user-packages-topdir)))
1206 (setq early-package-load-path
1207 (packages-find-package-load-path early-packages)
1208 late-package-load-path
1209 (packages-find-package-load-path late-packages)
1210 last-package-load-path
1211 (packages-find-package-load-path last-packages))
1215 (princ (format "configure-package-path:\n%S\n" configure-package-path)
1216 'external-debugging-output)
1217 (princ (format "early-packages and early-package-load-path:\n%S\n%S\n"
1218 early-packages early-package-load-path)
1219 'external-debugging-output)
1220 (princ (format "late-packages and late-package-load-path:\n%S\n%S\n"
1221 late-packages late-package-load-path)
1222 'external-debugging-output)
1223 (princ (format "last-packages and last-package-load-path:\n%S\n%S\n"
1224 last-packages last-package-load-path)
1225 'external-debugging-output)))
1227 (setq lisp-directory (paths-find-lisp-directory roots))
1230 (princ (format "lisp-directory:\n%S\n" lisp-directory)
1231 'external-debugging-output))
1233 (if (featurep 'mule)
1235 (setq mule-lisp-directory
1236 (paths-find-mule-lisp-directory roots
1239 (princ (format "mule-lisp-directory:\n%S\n"
1240 mule-lisp-directory)
1241 'external-debugging-output)))
1242 (setq mule-lisp-directory '()))
1244 (setq ffi-lisp-directory
1245 (when (fboundp #'ffi-defun)
1246 (paths-find-ffi-lisp-directory roots
1250 (setq module-directory (paths-find-module-directory roots))
1252 (princ (format "module-directory:\n%S\n" module-directory)
1253 'external-debugging-output))
1254 (setq site-module-directory (and (null inhibit-site-modules)
1255 (paths-find-site-module-directory roots)))
1256 (if (and debug-paths (null inhibit-site-modules))
1257 (princ (format "site-module-directory:\n%S\n" site-module-directory)
1258 'external-debugging-output))
1260 (setq load-path (paths-construct-load-path roots
1261 early-package-load-path
1262 late-package-load-path
1263 last-package-load-path
1267 ffi-lisp-directory))
1269 (setq Info-directory-list
1270 (paths-construct-info-path roots
1271 early-packages late-packages last-packages))
1275 (princ (format "Info-directory-list:\n%S\n" Info-directory-list)
1276 'external-debugging-output))
1278 (setq exec-directory (paths-find-exec-directory roots))
1281 (princ (format "exec-directory:\n%s\n" exec-directory)
1282 'external-debugging-output))
1285 (paths-construct-exec-path roots exec-directory
1286 early-packages late-packages last-packages))
1289 (princ (format "exec-path:\n%S\n" exec-path)
1290 'external-debugging-output))
1292 (setq doc-directory (paths-find-doc-directory roots))
1295 (princ (format "doc-directory:\n%S\n" doc-directory)
1296 'external-debugging-output))
1298 (setq data-directory (paths-find-data-directory roots))
1301 (princ (format "data-directory:\n%S\n" data-directory)
1302 'external-debugging-output))
1304 (setq data-directory-list
1305 (paths-construct-data-directory-list data-directory
1310 (princ (format "data-directory-list:\n%S\n" data-directory-list)
1311 'external-debugging-output)))
1313 (defun startup-find-roots-warning ()
1315 (set-buffer (get-buffer-create " *warning-tmp*"))
1317 (buffer-disable-undo (current-buffer))
1319 (insert "Couldn't find an obvious default for the root of the\n"
1320 "SXEmacs hierarchy.")
1322 (princ "\nWARNING:\n" 'external-debugging-output)
1323 (princ (buffer-string) 'external-debugging-output)))
1325 (defun startup-setup-paths-warning ()
1326 (let ((warnings '()))
1328 ((null (and lisp-directory exec-directory data-directory doc-directory
1331 (set-buffer (get-buffer-create " *warning-tmp*"))
1333 (buffer-disable-undo (current-buffer))
1334 (if (null lisp-directory) (push "lisp-directory" warnings))
1335 (if (and (featurep 'mule)
1336 (null mule-lisp-directory))
1337 (push "mule-lisp-directory" warnings))
1338 (if (null exec-directory) (push "exec-directory" warnings))
1339 (if (null data-directory) (push "data-directory" warnings))
1340 (if (null doc-directory) (push "doc-directory" warnings))
1341 (if (null load-path) (push "load-path" warnings))
1343 (insert "Couldn't find obvious defaults for:\n")
1345 (insert (car warnings) "\n")
1346 (setq warnings (cdr warnings)))
1347 (insert "Perhaps some directories don't exist, "
1348 "or the SXEmacs executable,\n"
1349 (concat invocation-directory invocation-name)
1350 "\nis in a strange place?")
1352 (princ "\nWARNING:\n" 'external-debugging-output)
1353 (princ (buffer-string) 'external-debugging-output)
1358 (defun startup-load-autoloads ()
1359 "Load autoloads from known locations."
1361 (when (and (not inhibit-autoloads)
1362 (or lisp-directory module-directory))
1363 ;; ordinary auto-autoloads in lisp/
1364 (let ((aalfile (file-name-sans-extension autoload-file-name)))
1367 (load (expand-file-name aalfile lisp-directory) nil t)
1370 ;; just load them all
1374 (load (expand-file-name (concat "lisp/" aalfile) root) nil t)
1378 (when (featurep 'mule)
1379 (load (expand-file-name aalfile
1380 (expand-file-name "mule" lisp-directory))
1382 (when (featurep 'modules)
1383 (load (expand-file-name aalfile module-directory) t t))
1385 (when (fboundp #'ffi-defun)
1386 (load (expand-file-name aalfile
1387 (expand-file-name "ffi" lisp-directory))
1390 (unless inhibit-autoloads
1391 (unless inhibit-early-packages
1392 (packages-load-package-auto-autoloads early-package-load-path))
1393 (if (and (not inhibit-early-packages) (not warn-early-package-shadows))
1394 (let ((early-path (mapcar 'file-basename early-package-load-path))
1396 (mapc (lambda (path)
1397 (unless (member (file-basename path) early-path)
1398 (setq late-load-path (append late-load-path (list path)))))
1399 late-package-load-path)
1400 (packages-load-package-auto-autoloads late-load-path))
1401 (packages-load-package-auto-autoloads late-package-load-path))
1402 (packages-load-package-auto-autoloads last-package-load-path)))
1404 ;;; startup.el ends here