Hot answers tagged

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 ...
sds's user avatar
  • 6,104
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 :...
dakra's user avatar
  • 406
12 votes
Accepted

how to unbind a mode hook without restarting

Using function symbols certainly makes this easier (and this is one of the reasons why I would recommend always using that approach). You can still use remove-hook with the lambda form, though -- it ...
phils's user avatar
  • 50.4k
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's user avatar
  • 32.9k
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 ...
Stefan's user avatar
  • 26.3k
9 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 ...
phils's user avatar
  • 50.4k
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() ...
npostavs's user avatar
  • 9,143
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).
Stefan's user avatar
  • 26.3k
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 ...
phils's user avatar
  • 50.4k
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 ...
Manuel Uberti's user avatar
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))...
phils's user avatar
  • 50.4k
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 ...
Aaron Harris's user avatar
  • 2,674
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 (...
Harald Hanche-Olsen's user avatar
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 ...
Stefan's user avatar
  • 26.3k
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 () ...
Stefan's user avatar
  • 26.3k
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 ...
Drew's user avatar
  • 77k
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 ...
phils's user avatar
  • 50.4k
6 votes
Accepted

what is info-mode-hook

The built-in Info-mode uses a combination of initial uppercase and/or all lowercase letters for the various variables/functions/faces. There may be a rhyme or reason, but it quite frankly escapes me ...
lawlist's user avatar
  • 19.1k
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-...
Vera Johanna's user avatar
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. ...
Drew's user avatar
  • 77k
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-...
NickD's user avatar
  • 29.2k
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.
matteol's user avatar
  • 1,978
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-...
phils's user avatar
  • 50.4k
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-...
Tina Russell's user avatar
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 ...
phils's user avatar
  • 50.4k
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-...
Gilles 'SO- stop being evil''s user avatar
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 ...
Lindydancer's user avatar
  • 6,115
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 ...
lawlist's user avatar
  • 19.1k
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 ...
Chakravarthy Raghunandan's user avatar
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)) ...
Stefan's user avatar
  • 26.3k

Only top scored, non community-wiki answers of a minimum length are eligible