1
(defun switch-whitespace-mode ()
  (interactive)
  (whitespace-mode)
)

(global-set-key (kbd "<f6>") 'switch-whitespace-mode)

Through the above configuration,I failed.

  • 4
    Please include code as a code block (indented by 4 spaces). A <!-- language: lang-el --> line above the block will provide fontification. – JeanPierre Sep 11 '16 at 12:57
  • It‘s my first time to ask question. And I will adopt it next time. – moyotar Sep 12 '16 at 1:58
4

There is a set of conventions for minor modes documented in the docstring of define-minor-mode:

The defined mode command takes one optional (prefix) argument. Interactively with no prefix argument, it toggles the mode. A prefix argument enables the mode if the argument is positive, and disables it otherwise.

When called from Lisp, the mode command toggles the mode if the argument is `toggle', disables the mode if the argument is a non-positive integer, and enables the mode otherwise (including if the argument is omitted or nil or a positive integer).

In other words, M-x whitespace-mode will toggle the state of the mode, (whitespace-mode) won't. Use (whitespace-mode 'toggle) instead.

2

whitespace-mode is already an interactive function and, if called interactively, toggles the mode.

So the simplest answer to your question is:

(global-set-key (kbd "<f6>") 'whitespace-mode)
1

C-h f whitespace-mode tells you what your problem is:

whitespace-mode is an interactive autoloaded Lisp function in whitespace.el.

(whitespace-mode &optional ARG)

Toggle whitespace visualization (Whitespace mode).

With a prefix argument ARG, enable Whitespace mode if ARG is positive, and disable it otherwise. If called from Lisp, enable the mode if ARG is omitted or nil.

See also whitespace-style, whitespace-newline and whitespace-display-mappings.

The sentence I've put in bold tells you that with no argument the mode is enabled. In other words: (whitespace-mode) always enables the mode.

The doc string does not tell you, unfortunately, how to toggle the mode using Lisp. This is a bug, IMO (I've filed Emacs bug #24413 for this). The define-minor-mode call that defines this mode explicitly provides a doc string that is deficient in this way. If it had simply relied on the generic doc string provided by define-minor-mode then you would have seen this:

Toggle whitespace-mode on or off.

With a prefix argument ARG, enable Whitespace mode if ARG is positive, and disable it otherwise. If called from Lisp, enable the mode if ARG is omitted or nil, and toggle it if ARG is toggle.

@wasamasa's answer -- consult the doc of define-minor-mode -- provides this missing information: Use (whitespace-mode 'toggle). (And it lets you know that the same holds for other minor modes defined using define-minor-mode.)

  • After reading your enthusiastic explanation, I know more clearly. Thanks! – moyotar Sep 12 '16 at 1:49

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.