Reputation: 47051
For example, I put some codes within python-mode-hook
, and the codes will be run when:
(1) Every time I open a python file *.py
(2) Only the first time that python-mode
was triggered
Does anyone have ideas that which answer is right?
Thanks!
Upvotes: 3
Views: 217
Reputation: 73274
If you want to look at the code, then note that the vast majority of major modes are constructed with the define-derived-mode
macro (modes can simply supply nil
for the parent
if the new mode does not actually derive from another one, in order to still get the same benefits and standard behaviours from using the macro).
So M-x find-function
RET define-derived-mode
RET
You'll see that the very last thing that happens when the mode is invoked is (run-mode-hooks ',hook)
(where ,hook
in this instance will expand to the name of the mode with a -hook
suffix).
So every time a buffer enables that mode (meaning that the mode function defined by that macro is executed), the mode's hook is also run (as well the hooks for any parent/ancestor modes -- the interesting delay-mode-hooks
and run-mode-hooks
interaction means that none of the hooks are run until all the other processing has been completed).
Upvotes: 4
Reputation: 16440
There is no general rule about when hooks run: this is controlled solely by the code which defines the hook. However, there are general conventions that are followed among which is this one:
foo-mode
is a major mode of some sort, then foo-mode-hook
will run each time foo-mode
is enabled in a buffer.In particular, as has already been pointed out, the functions in python-mode-hook
run every time a .py
file is loaded.
Upvotes: 6
Reputation: 21172
It will run every time you open a python file.
Then python mode will be triggered for this file buffer and your hook will be executed.
You can easily test this by writing a simple hook:
(add-hook 'python-mode-hook (lambda () (message "python mode hook")))
Upvotes: 3