3

I wrote a function to enable some minor modes when another minor mode is enabled:

(defun prose-mode()
  (display-line-numbers-mode)
  (variable-pitch-mode))

I added prose-mode as a hook for olivetti-mode:

(add-hook 'olivetti-mode-hook 'prose-mode)

The hook is working fine and prose-mode gets ivoked on M-x olivetti-mode.

However, when I do that again to disable the mode, the line numbers don't reappear. variable-pitch-mode is toggling and I get my monospaced font back.

In general, is this the right way to do what I'm trying to do? Inside prose-mode, should I check for the current minor mode using a conditional statement to enable/disable the other modes?

  • If I do understand correctly, you're loading some stuff using the hook, which is run only when activated. – Muihlinn May 6 at 12:01
  • 1
    Elisp tag shouldn't used here, please, check elisp tag info – Muihlinn May 6 at 12:05
  • @Muihlinn I am enabling other minor modes using the hook, which is run only when activated. – sixter May 6 at 12:14
  • 1
    I meant that disabling olivetti-mode won't run the hook, if you mean that the line numbers doesn't toggle when you run again prose-mode you should use (display-line-numbers-mode 'toggle) instead. – Muihlinn May 6 at 12:19
  • 1
    The function name prose-mode is misleading, as it's is not a minor mode, minor mode is usually defined via define-minor-mode, a better name is sixter-olivetti-mode-setup (?) and you can detect if the olivetti mode is on or off via the variable olivetti-mode. – xuchunyang May 6 at 12:37
2

As xuchunyang noted in comments, your shouldn't use prose-mode to name a function that doesn't define a mode, so you could define your function like this:

(defun sixter-olivetti-mode-setup()
 "Load additional stuff with olivetti-mode."
  (display-line-numbers-mode 'toggle)
  (variable-pitch-mode)
)

Note that display-line-numbers-mode, accepts an optional argument to specify what you want to do. toggle is what your need in your case. You can ask for its documentation typing C-h f display-line-numbers-mode.

Then add it to the hook as you already did:

(add-hook 'olivetti-mode-hook 'sixter-olivetti-mode-setup)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.