Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a custom Emacs hook in a generic way

Tags:

emacs

elisp

I want to run some code after loading my custom file, but I want to do it in a generic fashion. The easy way would be to just have a list of functions that I append to and then execute each function in a list afterwards, but I wanted to see if I could do it as a hook. Something like:

(run-hooks 'bw-after-custom-load-hook)

and do this each time I want to add to it:

(add-hook 'bw-after-custom-load-hook (lambda () 'something))

Is this basically how hooks work? All documentation I can find only seems to add stuff to existing hooks provided by modes.

like image 915
Brad Wright Avatar asked Dec 27 '11 12:12

Brad Wright


1 Answers

I worked it out (should have tried before posting):

;; add my custom hook
(defvar bw-after-custom-load-hook nil
  "Hook called after the custom file is loaded")

then in another file:

;; but load it after custom has loaded, so it's marked safe
(add-hook 'bw-after-custom-load-hook
      (lambda ()
        (load-theme 'solarized-dark)))

then we load custom and call the hook:

;; Load custom file last
(setq custom-file (concat dotfiles-dir "custom.el"))
(load custom-file 'noerror)

;; load my custom hooks
(run-hooks 'bw-after-custom-load-hook)
like image 168
Brad Wright Avatar answered Oct 22 '22 21:10

Brad Wright