15
votes
Accepted
add-to-list vs add-hook?
The question you asked
The biggest difference is the last argument:
local for add-hook and
compare-fn for add-to-list.
This means that you have no control over how add-hook decides whether it ...
15
votes
How to avail of `:hook` using use-package?
As others said I think https://github.com/jwiegley/use-package#hooks
is pretty clear. :hook replaces add-hook and creates autoloads for
you. Your example with :hook would be:
(use-package web-mode
:...
11
votes
Accepted
Prevent folding org files opened by ediff
It sounds like your goal is to have org-mode files always show everything when in ediff. The simplest solution is probably to avoid the local variables approach and just put show-all in the relevant ...
Dan♦
- 32.9k
10
votes
Buffer-local after-save-hook?
This is standard functionality built into add-hook:
add-hook is a compiled Lisp function in ‘subr.el’.
(add-hook HOOK FUNCTION &optional APPEND LOCAL)
Add to the value of HOOK the ...
10
votes
Accepted
How to automatically remove a hook provided by a minor mode after disabling that mode?
The minor mode's hook is called both when activating and deactivating the minor mode, so how 'bout
(add-hook 'my-minor-mode-name-hook
(lambda ()
(if my-minor-mode-name
...
9
votes
Accepted
Run function after specific snippet
You can make snippet-specific bindings with # expand-env:
# -*- mode: snippet -*-
# name: ipdb
# key: ipdb
# expand-env: ((yas-after-exit-snippet-hook #'bm-toggle))
# --
import ipdb; ipdb.set_trace()
...
8
votes
Accepted
(la)tex-mode-hook doesn't work?
If you have AUCTeX installed, then you're presumably using AUCTeX's modes, so the hooks you need to use have names of the form TeX-mode-hook and LaTeX-mode-hook (note the capitalization).
8
votes
Accepted
Different themes in different modes
AFAIK you can't do this with the 'theme' system. I'm pretty sure themes are global.
You can have buffer-local face remapping however, and that could take place in a mode hook, so I think there's a ...
8
votes
Accepted
How to replace focus-out-hook with after-focus-change-function in emacs 27?
You could try using something like:
(add-function :after after-focus-change-function #'your-function-here)
So, in your case, something like this should do what you are after:
(add-function :after ...
8
votes
Accepted
Add hooks for a file type based on org-mode
(define-derived-mode hy-mode org-mode "Hy Mode"
"Major mode for editing *.hy files."
do-whatever-you-want-here)
(add-to-list 'auto-mode-alist '("\\.hy\\'" . hy-mode))...
7
votes
Accepted
List hooks that will run after command
I'm pretty sure it's impossible to know a priori what hooks a command will run; as others have mentioned, this smells a lot like the halting problem.
But it's certainly possible to actually execute ...
7
votes
Accepted
Is there a hook I can use to run my function when point moves?
I think post-command-hook is what you wish to use.
It is run every time a command has been run. And a command is basically any interactive action, such as clicking a mouse button, typing a letter (...
7
votes
Accepted
How to track user changes in the buffer?
To react to buffer changes, you generally want to use after-change-functions (or sometimes before-change-functions).
These hooks are run everytime some part of the buffer is modified, either by ...
7
votes
Accepted
Looking for something like a `first-keypress-in-a-while-hook`
You could try:
(defun my-run-fkpiawh ()
(remove-hook 'pre-command-hook #'my-run-fkpiawh)
(run-hooks 'first-keypress-in-a-while-hook))
(run-with-idle-timer 1200 t (lambda ()
...
7
votes
Accepted
Is there a hook that runs for all read only buffers
If you want this to act in any buffer, not just a file-visiting buffer then find-file-hook is not appropriate. (You said "all buffers", but you also spoke of editable/non-editable "files".)
If you ...
7
votes
Accepted
Hook with quoted lambda as an argument
(add-hook 'coffee-mode-hook '(lambda () (coffee-custom)))
Definitely not recommended.
Quoting lambdas like that is not good practice.
As you say, (add-hook 'coffee-mode-hook #'coffee-custom) would ...
6
votes
Looking for something like a `first-keypress-in-a-while-hook`
I guess a solution of your problem is setting a flag with an idle timer, https://www.gnu.org/software/emacs/manual/html_node/elisp/Idle-Timers.html
and checking/unsetting it with post-self-insert-...
6
votes
add-to-list vs add-hook?
In some cases you can use either function to add an element to a list. That you can do that does not mean that you should, however.
The recommendation by Emacs is to use only add-hook for a hook. ...
6
votes
Accepted
Is there a hook that runs when exiting read-only-mode?
Like all minor mode hooks, read-only-mode-hook runs when entering or leaving read-only mode. So you only need to make the setting of the cursor a bit smarter, probably by checking the value of buffer-...
6
votes
Hook that fires any time ANY file is opened?
find-file-hook
Documentation:
List of functions to be called after a buffer is loaded from a file.
The buffer’s local variables (if any) will have been processed before the
functions are called.
6
votes
Accepted
How to run a 'prog-mode-hook' after more specific hooks?
You can use after-change-major-mode-hook:
(add-hook 'after-change-major-mode-hook 'my-after-change-major-mode-prog-mode)
(defun my-after-change-major-mode-prog-mode ()
"Custom `after-change-major-...
6
votes
In org mode, how can I make a post-capture hook run only for certain capture templates?
See my answer here: https://emacs.stackexchange.com/a/48566/18296
Here’s the example code from it:
(defun tina/test-finalize ()
(let ((key (plist-get org-capture-plist :key))
(desc (plist-...
6
votes
Accepted
Check if current user undoing/redoing?
C-hv undo-in-progress
Documentation:
Non-nil while performing an undo.
Some change-hooks test this variable to do something different.
Bearing in mind that "redoing" is really still "undoing" in ...
5
votes
Accepted
save a particular buffer without prompting on emacs exit?
When Emacs exits, it saves buffers by calling save-some-buffers. This is the function that does the prompting. You can turn off the prompting in a buffer by setting the buffer-local variable buffer-...
5
votes
Accepted
Is there a way to detect change of monitor in Emacs / Elisp?
One way to do this is to save the original monitor for each frame (using frame-monitor-attributes) and then run a timer, where you check the current monitor.
If you run an idle timer and run it a few ...
5
votes
Accepted
post-command hook for org-cut-subtree
I typed M-x find-function RET org-cut-subtree RET and saw that it was just a few lines of code that calls org-copy-subtree. Then, I typed M-x find-function RET org-copy-subtree RET and looked through ...
5
votes
Accepted
how to disable minor mode (enabled using prog-mode-hook) for a particular major mode
This can be done very easily with hooks for major-modes like so:
(add-hook 'python-mode-hook (lambda ()
(highlight-indent-guides-mode -1)))
If for some reason this did ...
5
votes
Accepted
How to make *Messages* use visual-line-mode?
None of those hooks will apply because the buffer (and its mode) has already been created long before your add-hook happens.
I suggest you add
(with-current-buffer "*Messages*"
(visual-line-mode))
...
5
votes
Accepted
Detect interactive use of `kill-buffer` in `kill-buffer-hook`
Test the variable this-command to see whether it is kill-buffer
5
votes
Only enable auto-revert-mode for the current buffer
Since Emacs 24.4, auto-revert-mode does not poll anymore. Instead, it uses file notifications from your underlying OS. There shouldn't be any performance problem; people have reported 600+ buffers in ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
hooks × 409org-mode × 37
minor-mode × 24
init-file × 21
buffers × 17
major-mode × 17
elisp × 15
use-package × 13
key-bindings × 11
evil × 11
window × 10
advice × 10
magit × 9
debugging × 9
frames × 9
themes × 9
spacemacs × 7
python × 7
files × 7
buffer-local × 7
latex × 6
org-agenda × 6
faces × 6
company-mode × 6
minibuffer × 6