1

Have written this function to change the style for global-whitespace-mode. I describe the problem as follows. Suppose one calls (space-glow "tab-trail") followed by (space-glow "allvis"). But trying to do (space-glow "tab-trail") again, the style does not take effect.

(defun space-glow (form)
  "Global visualisation of tabs and trailing spaces."
  (interactive
   (list
    (let* ( (cseq '("allvis" "tab-trail" "disable")) )
      (completing-read " Space_glow: " cseq nil t "tab-trail"))))
  
  (require 'whitespace)

  (pcase form
    ("tab-trail"
     (setq whitespace-style '(face tab-mark tabs trailing))
     (global-whitespace-mode 1))
    ("allvis"
     (setq whitespace-style '(face tabs spaces trailing lines
            space-before-tab newline indentation empty
            space-after-tab space-mark tab-mark newline-mark))
     (global-whitespace-mode 1))
    ("disable"
     (global-whitespace-mode 0))) )

1 Answer 1

0

Toggle the mode to have a new style take effect.

Here I just unconditionally turned off the mode to start with. You can simplify the code for the "disable" case accordingly, if you like.

(defun space-glow (form)
  "Global visualisation of tabs and trailing spaces."
  (interactive
   (list
    (let* ( (cseq '("allvis" "tab-trail" "disable")) )
      (completing-read " Space_glow: " cseq nil t "tab-trail"))))
  (require 'whitespace)
  (global-whitespace-mode 0) ; <========= Turn it off first.
  (pcase form
    ("tab-trail"
     (setq whitespace-style '(face tab-mark tabs trailing))
     (global-whitespace-mode 1))
    ("allvis"
     (setq whitespace-style
       '(face tabs spaces trailing lines
          space-before-tab newline indentation empty
          space-after-tab space-mark tab-mark newline-mark))
     (global-whitespace-mode 1))
    ("disable"
     (global-whitespace-mode 0))) )
4
  • Is it safe to call (global-whitespace-mode 0) when a mode might not be available? whitespace-mode is a mode that is automatically available in emacs, correct?
    – Dilna
    Jul 20, 2022 at 1:36
  • I don't understand your comment question. I put that after your require.
    – Drew
    Jul 20, 2022 at 2:32
  • I understand the fix now. Because require imports (loads) the appropriate functionalities, then the value for whitespace-style can be set and used by the feature that uses it. Is it customary to require disabling a mode to reset a customisation variable? Are there packages that react dynamically to changes, and would it be the preferred behaviour?
    – Dilna
    Jul 20, 2022 at 2:57
  • If depends on what setting the option does and what enabling the mode does. In this case, just changing the variable value without toggling the mode didn't do what you wanted.
    – Drew
    Jul 20, 2022 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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