Add buffer-mode-hooks system
This commit is contained in:
parent
a3a2ae5dbd
commit
d6b11259f3
@ -321,6 +321,30 @@ Running a shell command as sudo:
|
||||
(shell-command command)))
|
||||
#+END_SRC
|
||||
|
||||
** Buffer switch hooks
|
||||
I want to be able to run code whenever I switch to a buffer running certain modes. The code to run is stored in a alist mapping mode names to lists of code to run (stored as a raw data structure to be eval'ed):
|
||||
#+BEGIN_SRC emacs-lisp
|
||||
(defvar buffer-mode-hooks '())
|
||||
#+END_SRC
|
||||
|
||||
To add a new hook, push the code to run onto the correct list:
|
||||
#+BEGIN_SRC emacs-lisp
|
||||
(defun add-buffer-mode-hook (mode fn)
|
||||
(if-let ((existing-entry (assoc mode buffer-mode-hooks)))
|
||||
(push fn (cdr existing-entry))
|
||||
(let ((new-entry `(,mode . (,fn))))
|
||||
(push new-entry buffer-mode-hooks))))
|
||||
#+END_SRC
|
||||
|
||||
Whenever the buffer changes, look up the major-mode to see if there is any code to run:
|
||||
#+BEGIN_SRC emacs-lisp
|
||||
(defun run-buffer-mode-hooks ()
|
||||
(when-let ((entry (assoc major-mode buffer-mode-hooks)))
|
||||
(dolist (fn (cdr entry))
|
||||
(funcall fn))))
|
||||
|
||||
(add-hook 'buffer-list-update-hook #'run-buffer-mode-hooks)
|
||||
#+END_SRC
|
||||
* Customization File
|
||||
I don't want anything to write to my init.el, so save customizations in a separate file:
|
||||
#+BEGIN_SRC emacs-lisp
|
||||
|
Loading…
Reference in New Issue
Block a user