Subject: 25.2; whitespace-mode does not turn off correctly

From: Nick Helm <[email-masked]>
Date: Fri, 12 May 2017 11:34:33 +1200

Emacs -Q
C-u 20 SPC RET RET RET RET   ; type some random whitespace
M-x global-whitespace-mode
M-x whitespace-mode
M-x whitespace-mode

The last command should toggle local whitespace-mode off, but note that the
whitespace display characters are not properly removed from the current
buffer.

I had a look at whitespace.el and I think the problem lies in
`whitespace-display-char-on'. Among other things, this function stores the
buffer display table and replaces it with a temporary display table to show
whitespace chars. The original is restored when the mode is turned off.

However, with global-whitespace-mode active, there is already a temporary
display table in place, which whitespace-mode incorrectly picks up and stores,
clobbering the original display table in the process. When whitespace-mode is
turned off, it restores the temporary display table set by
global-whitespace-mode, which leads to the problem.

Here's a little patch to check if global-whitespace-mode is active before
storing the display table. I've tried this for a few days with no obvious
problems.

--- whitespace-a.el	2017-05-12 00:59:34.000000000 +1200
+++ whitespace-b.el	2017-05-12 01:02:26.000000000 +1200
@@ -2496,9 +2496,10 @@
     (let (vecs vec)
       ;; Remember whether a buffer has a local display table.
       (unless whitespace-display-table-was-local
-	(setq whitespace-display-table-was-local t
-	      whitespace-display-table
-	      (copy-sequence buffer-display-table))
+	(setq whitespace-display-table-was-local t)
+        (unless global-whitespace-mode
+ 	      (setq whitespace-display-table
+	      (copy-sequence buffer-display-table)))
 	;; Assure `buffer-display-table' is unique
 	;; when two or more windows are visible.
 	(setq buffer-display-table

In GNU Emacs 25.2.1 (x86_64-apple-darwin13.4.0, NS appkit-1265.21 Version 10.9.5 (Build 13F1911))
 of 2017-04-22 built on builder10-9.porkrind.org
Windowing system distributor 'Apple', version 10.3.1504
Configured using:
 'configure --with-ns '--enable-locallisppath=/Library/Application
 Support/Emacs/${version}/site-lisp:/Library/Application
 Support/Emacs/site-lisp' --with-modules'

Configured features:
NOTIFY ACL GNUTLS LIBXML2 ZLIB TOOLKIT_SCROLL_BARS NS MODULES

Important settings:
  value of $LANG: en_NZ.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Messages

Minor modes in effect:
  recentf-mode: t
  savehist-mode: t
  ivy-mode: t
  global-eldoc-mode: t
  mouse-wheel-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  transient-mark-mode: t

Recent messages:
Loading /Users/nick/.emacs.d/config/mc/mc-run-once.list...done
Emacs start-up time: 1.0 seconds
For information about GNU Emacs and the GNU system, type C-h C-a.
Loading /Users/nick/.emacs.d/config/recentf/recentf.history...done
Cleaning up the recentf list...done (0 removed)
user-error: The mark is not set now, so there is no region
s-b is undefined [2 times]

Load-path shadows:
/Users/nick/.emacs.d/lisp/org/ox hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox
/Users/nick/.emacs.d/lisp/org/ox-texinfo hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-texinfo
/Users/nick/.emacs.d/lisp/org/ox-publish hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-publish
/Users/nick/.emacs.d/lisp/org/ox-org hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-org
/Users/nick/.emacs.d/lisp/org/ox-odt hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-odt
/Users/nick/.emacs.d/lisp/org/ox-md hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-md
/Users/nick/.emacs.d/lisp/org/ox-man hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-man
/Users/nick/.emacs.d/lisp/org/ox-latex hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-latex
/Users/nick/.emacs.d/lisp/org/ox-icalendar hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-icalendar
/Users/nick/.emacs.d/lisp/org/ox-html hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-html
/Users/nick/.emacs.d/lisp/org/ox-beamer hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-beamer
/Users/nick/.emacs.d/lisp/org/ox-ascii hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ox-ascii
/Users/nick/.emacs.d/lisp/org/org hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org
/Users/nick/.emacs.d/lisp/org/org-w3m hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-w3m
/Users/nick/.emacs.d/lisp/org/org-version hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-version
/Users/nick/.emacs.d/lisp/org/org-timer hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-timer
/Users/nick/.emacs.d/lisp/org/org-table hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-table
/Users/nick/.emacs.d/lisp/org/org-src hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-src
/Users/nick/.emacs.d/lisp/org/org-rmail hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-rmail
/Users/nick/.emacs.d/lisp/org/org-protocol hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-protocol
/Users/nick/.emacs.d/lisp/org/org-plot hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-plot
/Users/nick/.emacs.d/lisp/org/org-pcomplete hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-pcomplete
/Users/nick/.emacs.d/lisp/org/org-mouse hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-mouse
/Users/nick/.emacs.d/lisp/org/org-mobile hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-mobile
/Users/nick/.emacs.d/lisp/org/org-mhe hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-mhe
/Users/nick/.emacs.d/lisp/org/org-macs hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-macs
/Users/nick/.emacs.d/lisp/org/org-macro hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-macro
/Users/nick/.emacs.d/lisp/org/org-loaddefs hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-loaddefs
/Users/nick/.emacs.d/lisp/org/org-list hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-list
/Users/nick/.emacs.d/lisp/org/org-irc hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-irc
/Users/nick/.emacs.d/lisp/org/org-install hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-install
/Users/nick/.emacs.d/lisp/org/org-inlinetask hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-inlinetask
/Users/nick/.emacs.d/lisp/org/org-info hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-info
/Users/nick/.emacs.d/lisp/org/org-indent hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-indent
/Users/nick/.emacs.d/lisp/org/org-id hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-id
/Users/nick/.emacs.d/lisp/org/org-habit hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-habit
/Users/nick/.emacs.d/lisp/org/org-gnus hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-gnus
/Users/nick/.emacs.d/lisp/org/org-footnote hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-footnote
/Users/nick/.emacs.d/lisp/org/org-feed hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-feed
/Users/nick/.emacs.d/lisp/org/org-faces hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-faces
/Users/nick/.emacs.d/lisp/org/org-eshell hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-eshell
/Users/nick/.emacs.d/lisp/org/org-entities hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-entities
/Users/nick/.emacs.d/lisp/org/org-element hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-element
/Users/nick/.emacs.d/lisp/org/org-docview hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-docview
/Users/nick/.emacs.d/lisp/org/org-datetree hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-datetree
/Users/nick/.emacs.d/lisp/org/org-ctags hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-ctags
/Users/nick/.emacs.d/lisp/org/org-crypt hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-crypt
/Users/nick/.emacs.d/lisp/org/org-compat hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-compat
/Users/nick/.emacs.d/lisp/org/org-colview hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-colview
/Users/nick/.emacs.d/lisp/org/org-clock hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-clock
/Users/nick/.emacs.d/lisp/org/org-capture hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-capture
/Users/nick/.emacs.d/lisp/org/org-bibtex hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-bibtex
/Users/nick/.emacs.d/lisp/org/org-bbdb hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-bbdb
/Users/nick/.emacs.d/lisp/org/org-attach hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-attach
/Users/nick/.emacs.d/lisp/org/org-archive hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-archive
/Users/nick/.emacs.d/lisp/org/org-agenda hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/org-agenda
/Users/nick/.emacs.d/lisp/org/ob hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob
/Users/nick/.emacs.d/lisp/org/ob-tangle hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-tangle
/Users/nick/.emacs.d/lisp/org/ob-table hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-table
/Users/nick/.emacs.d/lisp/org/ob-sqlite hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-sqlite
/Users/nick/.emacs.d/lisp/org/ob-sql hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-sql
/Users/nick/.emacs.d/lisp/org/ob-shen hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-shen
/Users/nick/.emacs.d/lisp/org/ob-screen hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-screen
/Users/nick/.emacs.d/lisp/org/ob-scheme hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-scheme
/Users/nick/.emacs.d/lisp/org/ob-scala hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-scala
/Users/nick/.emacs.d/lisp/org/ob-sass hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-sass
/Users/nick/.emacs.d/lisp/org/ob-ruby hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-ruby
/Users/nick/.emacs.d/lisp/org/ob-ref hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-ref
/Users/nick/.emacs.d/lisp/org/ob-R hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-R
/Users/nick/.emacs.d/lisp/org/ob-python hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-python
/Users/nick/.emacs.d/lisp/org/ob-plantuml hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-plantuml
/Users/nick/.emacs.d/lisp/org/ob-picolisp hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-picolisp
/Users/nick/.emacs.d/lisp/org/ob-perl hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-perl
/Users/nick/.emacs.d/lisp/org/ob-org hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-org
/Users/nick/.emacs.d/lisp/org/ob-octave hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-octave
/Users/nick/.emacs.d/lisp/org/ob-ocaml hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-ocaml
/Users/nick/.emacs.d/lisp/org/ob-mscgen hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-mscgen
/Users/nick/.emacs.d/lisp/org/ob-maxima hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-maxima
/Users/nick/.emacs.d/lisp/org/ob-matlab hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-matlab
/Users/nick/.emacs.d/lisp/org/ob-makefile hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-makefile
/Users/nick/.emacs.d/lisp/org/ob-lob hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-lob
/Users/nick/.emacs.d/lisp/org/ob-lisp hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-lisp
/Users/nick/.emacs.d/lisp/org/ob-lilypond hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-lilypond
/Users/nick/.emacs.d/lisp/org/ob-ledger hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-ledger
/Users/nick/.emacs.d/lisp/org/ob-latex hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-latex
/Users/nick/.emacs.d/lisp/org/ob-keys hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-keys
/Users/nick/.emacs.d/lisp/org/ob-js hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-js
/Users/nick/.emacs.d/lisp/org/ob-java hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-java
/Users/nick/.emacs.d/lisp/org/ob-io hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-io
/Users/nick/.emacs.d/lisp/org/ob-haskell hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-haskell
/Users/nick/.emacs.d/lisp/org/ob-gnuplot hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-gnuplot
/Users/nick/.emacs.d/lisp/org/ob-fortran hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-fortran
/Users/nick/.emacs.d/lisp/org/ob-exp hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-exp
/Users/nick/.emacs.d/lisp/org/ob-eval hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-eval
/Users/nick/.emacs.d/lisp/org/ob-emacs-lisp hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-emacs-lisp
/Users/nick/.emacs.d/lisp/org/ob-dot hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-dot
/Users/nick/.emacs.d/lisp/org/ob-ditaa hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-ditaa
/Users/nick/.emacs.d/lisp/org/ob-css hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-css
/Users/nick/.emacs.d/lisp/org/ob-core hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-core
/Users/nick/.emacs.d/lisp/org/ob-comint hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-comint
/Users/nick/.emacs.d/lisp/org/ob-clojure hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-clojure
/Users/nick/.emacs.d/lisp/org/ob-calc hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-calc
/Users/nick/.emacs.d/lisp/org/ob-C hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-C
/Users/nick/.emacs.d/lisp/org/ob-awk hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-awk
/Users/nick/.emacs.d/lisp/org/ob-asymptote hides /Users/nick/Desktop/Emacs.app/Contents/Resources/lisp/org/ob-asymptote

Features:
(shadow face-remap emacsbug colir color recentf tree-widget wid-edit
time em-term term disp-table ehelp esh-opt esh-ext mu4e desktop frameset
mu4e-speedbar speedbar sb-image ezimage dframe mu4e-main mu4e-context
mu4e-view cal-menu calendar cal-loaddefs epa derived browse-url comint
ansi-color mu4e-headers mu4e-compose mu4e-draft mu4e-actions rfc2368
smtpmail sendmail mu4e-mark mu4e-message html2text mu4e-proc mu4e-utils
doc-view subr-x image-mode mu4e-lists mu4e-vars message format-spec
rfc822 mml mml-sec epg epg-config mm-decode mm-bodies mm-encode
mail-parse rfc2231 rfc2047 rfc2045 ietf-drums mailabbrev mail-utils
gmm-utils mailheader hl-line cl mu4e-meta dired-x dired savehist smex
ido seq counsel jka-compr esh-util etags xref project ring swiper ivy
delsel ivy-overlay ffap url-parse auth-source cl-seq eieio byte-opt
bytecomp byte-compile cl-extra cconv eieio-core cl-macs gnus-util
mm-util help-fns help-mode mail-prsvr password-cache url-vars
multiple-cursors mc-hide-unmatched-lines-mode mc-separate-operations
rectangular-region-mode mc-mark-pop mc-mark-more mc-cycle-cursors
mc-edit-lines multiple-cursors-core advice rect nh-posmark nh-resize
nh-macdict markdown-mode thingatpt noutline outline easy-mmode easymenu
iso-transl nh-tax nh-move-lines gv flyspell ispell linum plain-theme
edmacro kmacro cl-loaddefs pcase cl-lib time-date mule-util tooltip
eldoc electric uniquify ediff-hook vc-hooks lisp-float-type mwheel
ns-win ucs-normalize term/common-win tool-bar dnd fontset image
regexp-opt fringe tabulated-list newcomment elisp-mode lisp-mode
prog-mode register page menu-bar rfn-eshadow timer select scroll-bar
mouse jit-lock font-lock syntax facemenu font-core frame cl-generic cham
georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese charscript case-table epa-hook
jka-cmpr-hook help simple abbrev minibuffer cl-preloaded nadvice
loaddefs button faces cus-face macroexp files text-properties overlay
sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote kqueue cocoa ns multi-tty
make-network-process emacs)

Memory information:
((conses 16 291871 101436)
 (symbols 48 29250 0)
 (miscs 40 60 296)
 (strings 32 44032 118873)
 (string-bytes 1 1279925)
 (vectors 16 44379)
 (vector-slots 8 762963 80102)
 (floats 8 452 191)
 (intervals 56 708 164)
 (buffers 976 19))