How to run a function before changing major modes

It looks like it change-major-mode-hook

works after the new major mode is already on. How can you run a function just before changing the main mode?

Example: buffer c is major-mode-abc

active minor-mode-xyz

and this minor mode is responsible for placing certain overlays. When changing to, major-mode-def

it is necessary to execute the function remove-overlays

if it minor-mode-xyz

was active before changing the main modes. The new one will major-mode-def

also reactivate minor-mode-xyz

and immediately draw new overlays (which is good); however, those new overlays (which I wanted to keep) are erased 'change-major-mode-hook 'remove-overlays nil t

. I would prefer the function to be remove-overlays

executed with the hook associated with minor-mode-xyz

, rather than as part of an include major-mode-def

.

+3


source to share


2 answers


You may have misunderstood your question, but I don't see any problem here, assuming your minor mode is enabled from major modes and adds its cleanup function to change-major-mode-hook

.

change-major-mode-hook

runs at the very beginning of the main mode function, first of all, including the main mode interceptors. So the function of clearing your minor mode is called before , your minor mode is re-enabled by intercepting a new major mode. If that doesn't work for you, there is probably a flaw in the implementation or configuration of your minor mode.



Alternatively, you can defer adding new overlays to post-command-hook

rather than adding them directly to the minor mode function. Define a local variable buffer and set that variable to non-nil in your minor mode function. Then add the function to buffer-local post-command-hook

which adds overlays if the variable is not null.

This ensures that overlays are not drawn before the new major mode is fully configured, including clearing any old overlays. An added benefit of this approach is that adding overlays has access to the local and directory variables of the new major mode.

+3


source


You say: "Change-major-mode change seems to have started after ...": I wonder why you think so, because it is executed earlier, ie. it does exactly what you ask (and is used for exactly this kind of situation).



+4


source







All Articles