Search code examples
elispemacs24

How can I call a function when a mode is disabled in elisp?


I am trying to set a variable to true when entering a certain mode and set it to nil when the mode exists. How can I make it using hooks?


Solution

  • Normally, each mode has a corresponding hook, which is invoked at both entering and leaving the mode. You can tell the difference between entering and leaving by checking the corresponding mode variable. If it is not set - you are leaving the mode, otherwise you are entering.

    A rough sketch of what you need to do:

    (add-hook 'the-mode-hook
       (lambda () 
          (if the-mode
              (setq your-variable whatever-value)
            (setq your-variable nil))))