Questions tagged [hooks]

Hooks are an important mechanism for customizing Emacs. A hook is a Lisp variable which holds a list of functions, to be called on some well-defined occasion.

Filter by
Sorted by
Tagged with
29
votes
3answers
7k views

Proper way to enable minor mode

I have foo-mode and would enable bar-minor-mode for it. So which way is more common and preferable? A (add-hook 'foo-mode-hook 'bar-minor-mode) B (add-hook 'foo-mode-hook (lambda () ...
25
votes
1answer
2k views

What is the difference between ' and #' in front of a symbol?

I'm a little new to Emacs. When looking at some of the configurations, I found there are two types command in "add-hook". (add-hook 'LaTeX-mode-hook #'LaTeX-math-mode) and (add-hook 'LaTeX-mode-...
19
votes
2answers
1k views

Is there fully-automatic fill-paragraph-mode for code comments?

I'm looking for a minor mode to keep paragraphs filled at all times while typing (similar to what aggressive-indent-mode does for indentation). It also needs to be smart enough to only fill comments (...
16
votes
1answer
5k views

Remove hooks for specific modes

I want to delete trailing whitespace on save for every mode except org-mode. In my .emacs, I have the following line: (add-hook 'before-save-hook 'delete-trailing-whitespace) I use the use-package ...
16
votes
2answers
565 views

How to find out what a key sequence really does

From time to time I observe unexpected behavior when editing text. My first recourse is usually to use C-h k to find out what functions are being called by a given key sequence. However, sometimes ...
15
votes
2answers
8k views

Run command when opening a file of a specific filetype

I'm trying to get a lisp script to run some commands for me when I open a file of a specific filetype. I know that I'm working in the correct init file because if I remove the theme from it, emacs has ...
15
votes
3answers
2k views

Is there any way to run a hook function only once?

The context I'm using the after-make-frame-functions hook to load properly the themes in an emacs client/server configuration. Specifically this is the code snippet that I use to make that (based in ...
13
votes
3answers
5k views

before-save-hook for cc-mode

I want to customize cc-mode to execute a function before saving. One solution would be to modify cc-mode keymap to bind C-x C-s to a function which performs the work I need and then saves it. My ...
13
votes
2answers
1k views

Where should I include a lazy initialization?

I want to add a specific key binding to latex-mode: (define-key latex-mode-map (kbd "<f6>") 'my-latex-defun) At the same time I want to set it only when the specific mode is loaded. So I ...
13
votes
1answer
3k views

Execute external script upon save when in a certain mode?

When I'm in org-mode, I want Emacs to execute a bash script I wrote whenever I hit C-x s to save. The script automatically syncs the file I am saving to my Raspberry Pi. It expects the file name as ...
10
votes
5answers
1k views

How to enter view-only mode when browsing Emacs source code from help?

When I browse Emacs help for functions via C-h f, I often want to peek into the Elisp/C implementation. I want to enter view-mode automatically when I access source code this way to avoid unnecessary ...
9
votes
1answer
3k views

What's the difference between after-init-hook and emacs-startup-hook

Seems there are several hooks about actions on Emacs initialization. However I'm not sure what their differences are, and a brief Googling yields little information. Is it like emacs-startup-hook is ...
8
votes
1answer
151 views

Quitting Emacs and re-enabeling touchpad

I have the below elisp code in my emacs config which disables my touchpad, so it won't annoy me when writing. ;; disable touchpad when on emacs (defun turn-off-mouse (&optional frame) (...
8
votes
2answers
1k views

Disable hl-line-mode only for eshell and ansi-term

I have global-hl-line-mode setup in my init file: (use-package hl-line :init (global-hl-line-mode 1)) Is it possible to disable it when I am in eshell and ansi-term? I tried adding a hook like ...
8
votes
1answer
1k views

What is a hook?

Just to clarify... For example, in this doc: A hook is a Lisp variable which holds a list of functions, to be called on some well-defined occasion. (This is called running the hook.) The ...
8
votes
1answer
798 views

How to hook into buffer modified state?

Such as in the built-in variable mode-line-modified which displays an asterisk in the mode line if the buffer is modified. Specifically, I want to turn hl-line-mode off when the buffer is modified ...
8
votes
2answers
933 views

How to activate semantic-mode for specific major modes only?

I've seen this question a lot. But none of the replies fixed my issue: the minor mode stays on in others modes. So far, I have tried: (use-package stickyfunc-enhance :config ;; (add-to-list '...
7
votes
3answers
174 views

What's a good way to have a hook that runs for every file in a given directory?

What is a good way to assign a function to run when I open any file in a specified directory? Just like a hook but based on the location of a file rather than a major mode. It would be nice if I ...
7
votes
2answers
573 views

Looking for something like a `first-keypress-in-a-while-hook`

Suppose that I have an Emacs session running, and that I step away from my computer for, say, 20 minutes. Upon returning to my Emacs session, at some point I will press some key while the Emacs ...
7
votes
2answers
632 views

Prevent folding org files opened by ediff

Folding gets in the way when merging two org files using ediff, so I'm trying to disable all folding in org-mode buffers created by ediff. Non-ediff folding behavior should remain unaffected. ...
7
votes
2answers
168 views

Is there a hook that runs for all read only buffers

I want to enable show-trailing-whitespace for editable file and disable show-trailing-whitespace for read only files. I've tried (defun my/read-only-whitespace () (setq-local show-trailing-...
7
votes
2answers
804 views

Hook that indicates when the cursor has been moved by scrolling

Inspired by this question: Let Emacs move the cursor off-screen, I am considering writing a minor mode that will keep the cursor in a fixed position that is not affected by scrolling operations. I ...
7
votes
1answer
216 views

How to abort saving in a before-save-hook function?

If, in a function run from before-save-hook, I want to abort the saving of a file, so that the file is not saved and the user is signaled, how can I do that? Is it possible (without rebinding C-xC-s ...
7
votes
1answer
761 views

Is there a universal 'after-theme-load' hook?

I would like to set several face variables for use with any theme in my personal init-file. Is there a hook I could use to insure it is theme independent?
6
votes
2answers
2k views

add-to-list vs add-hook?

What is the difference between add-to-list vs add-hook? For example I see in progmodes/make-mode.el: (add-hook 'completion-at-point-functions #'makefile-completions-at-point nil t) ...
6
votes
3answers
792 views

how to unbind a mode hook without restarting

I had set up a custom hook for a mode, but I now realize that I want to use the mode without my changes and without my hook. So, I want the mode to behave as if I never set the hook in the first place,...
6
votes
2answers
5k views

How to run an `after-save-hook` only when the buffer has been saved manually

This question is inspired by https://emacs.stackexchange.com/a/5947/. Assume that I have a function like (defun do-some-actions () () (..put a copy of the current file in a specific folder...) ) ...
6
votes
3answers
134 views

`query-replace` leaves potential match to be replaced at bottom of window

If I have a long document that I run query-replace (or any other number of commands) in, as I'm saying y or n to replace that match or not, the current potential item to be replaced is always at the ...
6
votes
3answers
590 views

Focus-hook: attenuate colours when losing focus

How can I use the focus hooks to attenuate all colours in visible buffers when the Emacs frame loses focus? I tried using the following code: (set-frame-parameter (selected-frame) 'alpha '(100 80)) ...
6
votes
1answer
829 views

Run hook on new buffer

I am trying to create something like uniquely, which renames the buffer to the full path within the projectile project. I have the code working, however, I can't seem to figure out how to get it to ...
6
votes
2answers
245 views

Programmatically controlling the order of functions within a hook

I am looking for an example, please, to programmatically control the order of functions within a particular hook: First in time, which is easy -- i.e., don't append using add-hook. Last in time, ...
6
votes
1answer
2k views

List hooks that will run after command

Is there a way to lookup which hooks will run when a given command is called? For example, for major modes it is pretty easy to guess that activating python-mode will run python-mode-hook. But ...
5
votes
3answers
671 views

Turn a non prog-mode derived major-mode into a prog-mode derived major-mode

erlang-mode is not a derived prog-mode. Is there any way to make it part of the prog-mode-hook ?
5
votes
4answers
259 views

Is there a standard function that applies a list of predicates to a value until success?

I look for compacting expression like: (let ( (val (complex-expr ...)) ) (or (pred1 val) (pred2 val) ...(predN val))) Instead I like something: (apply-some (complex-expr ...) '(pred1 pred2 ... ...
5
votes
2answers
542 views

In org mode, how can I make a post-capture hook run only for certain capture templates?

I have several capture templates. With one of them, I want an Elisp function to transform the contents before saving it. I know of org-capture-prepare-finalize-hook, but this will run for every ...
5
votes
1answer
2k views

How to avail of `:hook` using use-package?

I've wrote this, and it works: (use-package web-mode :ensure t :mode (("\\.php$" . web-mode) ("\\.html$" . web-mode)) :preface (defun dg/web-mode-hook()) :config (add-hook 'web-...
5
votes
1answer
233 views

How to automatically remove a hook provided by a minor mode after disabling that mode?

I want to write a minor which evaluates some code in the buffer after saving a file. I activate after-save-hook for this mode: (add-hook 'my-minor-mode-name-hook #'(lambda () (add-hook 'after-save-...
5
votes
3answers
652 views

Call a function before a specific major mode starts

I have a setup function that I want to run before I use pdb/gud to debug a Python script. I know about gud-mode-hook which runs after the debugger starts (too late) and change-major-mode-hook which, ...
5
votes
2answers
195 views

How to ensure lines are always displayed in groups of 3?

I am writing a major-mode which displays a list to the user. It is similar to tabulated-list-mode, except each entry takes up 3 lines, as opposed to just one: 0 0 Ctrl-} key in emacs 5m ...
5
votes
2answers
278 views

Detect when a key is rebound to another command

First, some context: I am in the process of debugging a strange interaction between smartparens and Org Speed Keys: After editing an .org file for a while, specific speed keys for navigating the ...
5
votes
1answer
621 views

How to deal with 'add-hook' in .emacs when it's reloaded more than once?

I have some add-hook functions in my .emacs similar to below, Send a notification when a pomodoro is completed: (add-hook 'org-pomodoro-finished-hook (lambda () (message "Pomodoro finished") ...
5
votes
1answer
688 views

(la)tex-mode-hook doesn't work?

I was playing around with my .emacs, and I cannot figure out why the following does not work with .tex files: (add-hook 'tex-mode-hook (lambda () (show-paren-mode t))) On the other hand, the ...
5
votes
1answer
244 views

Make Emacs diff files before asking to save

When I run save-buffers-kill-emacs or projectile-kill-buffers I get asked to save changed buffers. But for me a lot of these buffers do not differ from the file on disk. Can I make Emacs diff the ...
5
votes
2answers
378 views

Using different theme on tramp buffers (and buffer-related themes in general)?

Is it possible to configure emacs so it uses different theme for tramp buffers than for defaults? Say, I use twilight by default, but while editing sth via ssh I'd like to switch to pastels-on-dark... ...
5
votes
1answer
739 views

What is the difference between setq-mode-local and setq-local

I don't understand how setq-mode-local works (mode-local is a package from cedet). The macro's documentation says: setq-mode-local is a Lisp macro in `mode-local.el'. (setq-mode-local MODE &...
5
votes
1answer
188 views

How to suppress a minor mode's key binding in only certain major modes?

Suppose that I use minor mode MIN with a whole bunch of major modes. In one of those major modes, call it MAJ1, MIN's binding for a specific key K is undesirable; I would therefore like to suppress ...
5
votes
2answers
647 views

Different themes in different modes

I would like to do the following: load some theme when working on a particular kind of source file (say, Haskell), but elsewhere use a different "default" theme. I naively imagine that this is ...
5
votes
2answers
247 views

Detect if mouse button is held down?

I have some code that runs in a hook that I want to change to only execute only if the left mouse button is not currently held down. I don't see any obvious functions or variables that let me directly ...
5
votes
1answer
89 views

Why (lambda () (run-hooks 'foo)) instead of just 'foo?

I am studying the following code snippet taken from prelude-c.el: ;; taken from prelude-c.el (defun prelude-makefile-mode-defaults () (whitespace-toggle-options '(tabs)) (setq indent-tabs-mode t)...
5
votes
0answers
88 views

Get task duration from old timestamps

Assuming I have a task where I store a timestamp for every taskchange I do: * DONE Extremely important dummy task :categorywhatevs: - State "DONE" from "IN PROGRESS" [2015-06-08 Mo 14:...

1
2 3 4 5 6