1

I'd like to disable whitespace-mode while in evil insert state. The following hooks work, but require refreshing all font colors with (font-lock-fontify-buffer), and even then- sometimes it fails to refresh properly to show trailing whitespace.

While this example uses evil-mode, similar logic applies to any other hook.

(defun i42/enter-insert-mode ()
  (whitespace-mode -1)
  (font-lock-fontify-buffer))
(defun i42/exit-insert-mode ()
  (whitespace-mode 1)
  (font-lock-fontify-buffer))
(add-hook 'evil-insert-state-entry-hook 'i42/enter-insert-mode)
(add-hook 'evil-insert-state-exit-hook  'i42/exit-insert-mode)

Is there a better way to toggle white space mode?

  • 1
    Just out of curiosity, why don't you want it on in insert state? – Dan Dec 7 '16 at 17:30
  • Does redraw-display or redisplay fix this issue? These functions should redraw the display and possibly fix your problem. – DoMiNeLa10 Dec 7 '16 at 17:38
  • If you manually disable whitespace-mode without using evil, does the screen refresh properly without any extra steps? – lawlist Dec 7 '16 at 18:59
  • @Dan, because I have a bright color set for trailing space which is distracting when entering text. Checked redraw-display, redraw & manually toggling whitespace-mode - none of them properly refresh the background color of trailing space, found a workaround, temporarily change the theme color - will post as a possible answer. – ideasman42 Dec 7 '16 at 20:26
1

One workaround I'd found is to temporarily change the face background color.

;; Don't show trailing space in insert mode.
(defvar i42/whitespace-trailing-bg (face-attribute 'whitespace-trailing :background))
(defun i42/enter-insert-mode ()
  (set-face-attribute 'whitespace-trailing nil :background nil :foreground nil))
(defun i42/exit-insert-mode ()
  (set-face-attribute 'whitespace-trailing nil :background i42/whitespace-trailing-bg :foreground nil))

(add-hook 'evil-insert-state-entry-hook 'i42/enter-insert-mode)
(add-hook 'evil-insert-state-exit-hook  'i42/exit-insert-mode)

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.