emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: run/load a lisp script before user init file


From: Josh
Subject: Re: run/load a lisp script before user init file
Date: Tue, 8 Oct 2013 12:34:26 -0700

On Tue, Oct 8, 2013 at 7:50 AM, Jarek Czekalski <address@hidden> wrote:
> My attempts to establish a portable way to do that may be
> annoying. I can understand that. So I finish this thread. My
> conclusion is:
>
> The site-start design is a mistake. That's why all installers say:
> add "..."  to your init file. That's the way I'm going to choose
> too. Until someone revises the design and comes up with something
> comfortable. Debian folks would do that, but apparently they
> decided that it's easier to create own solution, than to fight for
> the improvement in core.

If you choose to use init files alone, you may be able to take advantage
of the order in which Emacs searches for init files.  The manual[0]
documents three init files that are checked, but _not_ the order in which
Emacs looks for them (this is arguably a documentation bug, but be aware
that this order could change since the documentation currently makes no
promises about it).  For the moment though, a quick command-line
experiment here in Bash[1] showed that Emacs looks first for ~/.emacs.el,
then for ~/.emacs, and finally for ~/.emacs.d/init.el.  You could place a
minimal bootstrap init file in ~/.emacs.el which would do nothing but
load Emacspeak if possible and then immediately try to load the main init
file where users' configuration would be stored.  By making an
appropriate assignment to user-init-file it preserves users' ability to
store customization settings in their own init files where your package
need never touch them.  It is perfectly safe to create and remove this
bootstrap file when installing and uninstalling your package so long as
you verify that it does not yet exist before creating it and that its
contents remain unchanged before deleting it.  After uninstallation of
your package and removal of the bootstrap code in ~/.emacs.el, all of the
contents of your users' ~/.emacs and/or ~/.emacs.d/init.el will remain
intact and Emacs will find them normally after failing to find the
deleted ~/.emacs.el.  Here is an example of what I'm suggesting (lightly
tested):

;; add emacspeak to `load-path' here if necessary
(message "Loading Emacspeak...%s"
         (if (load "emacspeak-loader" :noerror) "success!" "FAILED!"))

;; Fall back to this file if neither ~/.emacs nor ~/.emacs.d/init.el
;; could be found, though creating one of those may be preferable to
;; avoid the possibility of user customization ending up here and
;; complicating uninstallation of your package.
(let ((fallback-user-init-file user-init-file))
  (if (string-equal user-init-file (expand-file-name "~/.emacs.el"))
      (unless (load (setq user-init-file
                          (expand-file-name "~/.emacs"))
                    :noerror nil :nosuffix)
        (unless (load (setq user-init-file
                            (expand-file-name "~/.emacs.d/init.el"))
                      :noerror nil :nosuffix)
          (setq user-init-file fallback-user-init-file)))
    (error "init bootstrap code found in %s but must reside in ~/.emacs.el"
           user-init-file)))

;; (message "final user-init-file: %s" user-init-file)
;; -> /path/to/HOME/.emacs, or if not found
;;    /path/to/HOME/.emacs.d/init.el, or if not found
;;    /path/to/HOME/.emacs.el (this file)

HTH,
Josh                      

[0] (info "(elisp) Init File")
[1] ( fakehome="/tmp/fakehome"; if [ -d $fakehome ] ; then echo "$fakehome exists, aborting" ; exit 1 ; else export HOME="$fakehome"; mkdir -p ${HOME}/.emacs.d && cd && for f in .emacs .emacs.el .emacs.d/init.el ; do echo "(message \"$f\")" >$f ; done ; emacs -nw --eval '(message "user-init-file: %s" user-init-file)' ; fi )


reply via email to

[Prev in Thread] Current Thread [Next in Thread]