It depends on which version(s) of Emacs you are using (or targeting). Iff you are exclusively using Emacs 24+ then you can safely use variant A:
* Incompatible Lisp Changes in Emacs 24.1
** Passing a nil argument to a minor mode function call now ENABLES
the minor mode unconditionally. This is so that you can write e.g.
(add-hook 'text-mode-hook 'foo-...
In Emacs Lisp, if foo is a symbol, then 'foo and #'foo are completely equivalent. The latter form (with #') is preferred when foo is a function, as it documents the fact that it is intended to be funcalled.
Your two forms are therefore completely equivalent, and the one with #' is preferred.
Edit: as pointed out by Malabarba, this is not quite true: #' on ...
A lazy init is a good init.
When to use with-eval-after-load
with-eval-after-load is loaded once when a certain feature or file is first loaded, so a keymap-change clearly goes inside one of these. Not the least because the keymap may not be known at init-time [try something like (define-key message-mode-map (kbd "C-c f") 'Footnote-add-footnote) in ...
Alternatively, use a local hook:
(lambda () (add-hook 'before-save-hook MY-HOOK-FUNC nil 'local)))
This adds MY-HOOK-FUNC to the buffer-local before-save-hook of each C++
Mode buffer, or any buffer in a mode derived of C++ Mode.
I find this more elegant that explicitly checking major-mode, and it has the
advantage that ...
I don't have any experience with the use-package macro, but in principle, you could use the third argument to remove-hook which tells it to remove the function only from the local hook:
(remove-hook 'before-save-hook 'delete-trailing-whitespace t)
Here's the docstring for reference:
(remove-hook HOOK FUNCTION &optional LOCAL)
Remove from the ...
In Emacs terminology, these are two different steps:
Associate files with the .ts extension with the major mode typescript-mode.
Run the function tss-setup-current-buffer when Typescript mode starts.
To choose which major mode to use for certain file names, add an entry to the variable auto-mode-alist. Put the following line in your init file:
You can use directory-local variables to make Emacs' source files read-only by default. (See also C-hig (emacs) Directory Variables RET).
Create a file called .dir-locals.el at the root of the directory tree you wish to protect, with the following contents:
((nil . ((eval . (view-mode 1)))))
Edit: Michał Politowski points out in the comments that enabling ...
See the Startup Summary section of the Emacs Lisp manual for a detailed description of the things that happen when you start Emacs.
The emacs-startup-hook runs later than the after-init-hook.
One key difference is that there may be command-line options processed after the after-init-hook and before the emacs-startup-hook. (Command-line options are ...
There is no easy way to know exactly what a single key press will do.
If you see additional behavior always check the common hooks. See the list here: http://www.gnu.org/software/emacs/manual/html_node/elisp/Standard-Hooks.html
In most cases the important ones are:
One fairly common way of restricting the action of a hook to a particular major mode is to check the value of major-mode.
For example, this hook prints a message before saving buffers using c++-mode:
(defun my-c++-mode-before-save-hook ()
(when (eq major-mode 'c++-mode)
(message "It's never too early to start saving (C++ code)!")))
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 already contains what you are adding, while add-to-list cannot control buffer-local status of the symbol you are modifying.
The question you meant to ask
I'm guessing that you are not really looking for a way to "execute the hook only once". I'm guessing that you are looking for a way to execute that particular function only once, whenever the hook is run.
The conventional, and simple, answer to that question is for your function to remove itself from the hook, after carrying out the one-time action that ...
You can use kill-emacs-hook which is run when emacs is quit 'normally'.
Hook run when kill-emacs is called. Since kill-emacs may be
invoked when the terminal is disconnected (or in other similar
situations), functions placed on this hook should not expect to be
able to interact with the user. To ask for confirmation, see
Here's a simple setup that you can modify that allows you to sync org files to your Raspberry Pi, based on your bash script (untested, of course, because I have neither a Pi nor your script). Modify the function with your script name. It uses shell-command-to-string to put the command line output somewhere; you could also just use shell-command if that's ...
What you are looking for is find-file-hook:
(add-hook 'find-file-hook 'my-project-hook)
(defun my-project-hook ()
(when (string= (file-name-extension buffer-file-name) "ts")
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 just needs to be the same lambda form as the one you added!
(lambda () (local-set-key (kbd "C-c l") 'some-fancy-function)...
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 hook, ediff-prepare-buffer-hook which is run after buffers A, B, & C are set up:
(add-hook 'ediff-prepare-buffer-hook #'...
In Emacs 24.4 you should be able to use
(add-hook 'focus-out-hook #'turn-on-my-jabber-sound)
(add-hook 'focus-in-hook #'turn-off-my-jabber-sound)
(defun turn-on-my-jabber-sound ()
;; t for `on'
(setq my-jabber-sound 't))
(defun turn-off-my-jabber-sound ()
;; nil for `off'
(setq my-jabber-sound 'nil))
Then test if my-jabber-sound is t or nil when ...
Why would you define a new command that does exactly what bar-minor-mode does?
Starting with 24.1, all of these are completely equivalent, so just use the less redundant one: option A
(add-hook 'foo-mode-hook #'bar-minor-mode)
Here's one more way to do it that has some advantages,
assuming that you're editing your own config, not a package that
(defun custom-foo-hook ()
(define-key foo-mode-map "C-c C-b" #'foobar))
The advantage is that everything is stored ...
global-hl-line-mode sets variable global-hl-line-mode to t. The global mode is separate from the non-global (hl-line-mode).
The following will disable it in the selected mode:
(add-hook 'eshell-mode-hook (lambda ()
(add-hook 'before-save-hook #'clang-format-buffer nil t)
This is the right way to add a function locally to a hook.
This will set before-save-hook as a buffer local variable ignoring everything that I set globally.
No, it won't. The global value will still exist, and run-hooks will run both values.
before-save-hook is no exception here, try it out and ...
Firstly you need to define what "saved manually" actually means. It could cover multiple commands, to my mind. M-x apropos-command RET save\|write RET probably includes all the typical cases.
Once you know which commands you're interested in, you could simply test the this-command variable in your hook function, to see whether its value is a member of your ...
You can use cl-some to apply a list of predicates to a value:
(cl-some (lambda (p) (funcall p "some value"))
'(vectorp listp stringp))
I suppose you could wrap this in a function. Although I don't love this name, say:
(defun cl-emos (preds v)
"Are any PREDS true for V.
This is the \"opposite\" of `cl-some', which sees if one pred is
true for ...
Dunno what Evil might change in the picture (try without Evil, to see if there is a difference).
But if you are interested in the buffer name of a buffer visiting a file then what you should use is buffer-file-name, not just buffer-name. Specifically, (file-name-nondirectory (buffer-file-name)), if you want only the non-dir part. (buffer-name is for any ...
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 inserting or by deleting a chunk of text (or text-properties). Be aware that some commands work by applying several modifications to the buffer, so those hooks can ...
You could try:
(defun my-run-fkpiawh ()
(remove-hook 'pre-command-hook #'my-run-fkpiawh)
(run-with-idle-timer 1200 t (lambda ()
After which you can use add functions to first-keypress-in-a-...