Say, I want to bind a key to turn some mode on/off. I guess, the function I need should look something like
(defun toggle-some-mode ()
(if (some-mode-is-active)
(some-mode 0)
(some-mode 1)))
(global-set-key (kbd "some key") 'toggle-some-mode)
The question is what does this some-mode-is-active look like? How could I know mode is active/inactive? I don't believe there's no standard function for such a useful case.
Any ideas?
Just use some-mode to toggle, i.e., (global-set-key (kbd "some key") 'some-mode). The argument of some-mode should be optional and if the argument is not present in the function call the function toggles the mode by default.
About your question how to detect whether mode is active: If some-mode is defined by define-minor-mode it exists as function and as variable. The variable some-mode evaluates to t if the mode is active and nil otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With