emacsdot-emacs

Add hook to default mode when using emacs -q -l


I have been loading emacs with emacs -q -l "init.el" quite a bit and was trying to enable auto-complete in my scratch buffer. I was struggling to figure out why it wasn't working but realized it must have to do with the order of operations when emacs is loaded like this - a quick test with the following init file:

(package-initialize)
(require 'auto-complete)
(ac-config-default)
(add-hook 'lisp-interaction-mode-hook
      '(lambda ()
         (auto-complete-mode t)))

shows completion working as I would like when calling emacs as normal from the command line. But if I call it as emacs -q -l init.el there is no dropdown completion.

Question: How can I get this hook to run?

I've tried variations on after-init-hook but none seem to work.


Solution

  • The following analysis is based on startup.el of the master branch: https://github.com/emacs-mirror/emacs/blob/master/lisp/startup.el

    As I understand the question, it seeks an answer as to when the command line option -l aka --load FILE is run as compared to when the *scratch* buffer is initialized with the initial-major-mode that by default is lisp-interaction-mode.

    Based on the sequence of events defined within startup.el, the -l or --load options are taken into consideration at line 2381 of the function command-line-1.

    The function command-line-1 runs at line 1366 of startup.el, which is subsequent to the after-init-hook at line 1344 and subsequent to the *scratch* buffer being initialized with the initial-major-mode at line 1350.

    To the extent that the original poster would like to rely upon loading a file manually using the -l or --load option, then functions being assigned to the lisp-interaction-mode-hook will not be seen at line 1350 because they do not exist until command-line-1 runs at line 1366. One option the original poster may wish to consider would be the following: (with-current-buffer "*scratch*" (lisp-interaction-mode)) after the auto-complete-mode has been added to the lisp-interaction-mode-hook.