Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle on/off particular mode in Emacs?

Tags:

emacs

elisp

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?

like image 973
avli Avatar asked Oct 31 '25 13:10

avli


1 Answers

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.

like image 54
Tobias Avatar answered Nov 04 '25 11:11

Tobias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!