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

From: Nick Helm <[email-masked]>
Date: Mon, 15 May 2017 12:26:14 +1200

> Did you try running whitespace-tests with this change, and if so, did
> they pass?

Yes, all tests pass.

> If the tests pass, would you mind to please add a new test for this
> bug?

I've written a new test and confirmed it catches this problem. I also
tweaked the original patch to handle another instance of the same bug.

Patches for whitespace.el and whitespace-tests.el are attached.

--- whitespace-tests-a.el	2017-05-12 23:11:38.000000000 +1200
+++ whitespace-tests-b.el	2017-05-15 01:55:43.000000000 +1200
@@ -47,6 +47,31 @@
     (should (equal (whitespace-tests--cleanup-string "a  \n\t \n\n")
                    "a  \n"))))
 
+(ert-deftest whitespace-tests-display-tables ()
+  "Test whitespace stores and restores the buffer display table - bug26892."
+  (with-temp-buffer
+    (whitespace-mode -1) ; turn off in case global ws mode is active
+    (let ((whitespace-style '(space-mark tab-mark newline-mark))
+          (whitespace-display-mappings '((space-mark   32 [183] [46])
+                                         (space-mark  160 [164] [95])
+                                         (newline-mark 10 [36 10])
+                                         (tab-mark      9 [187 9] [92 9])))
+          (buffer-display-table nil))
+      ;test the display table actually changes 
+      (should-not (equal nil
+                         (progn (whitespace-mode 1)
+                                buffer-display-table)))
+      ;test the display table restores correctly
+      (should (equal nil
+                     (progn (whitespace-mode -1)
+                            buffer-display-table)))
+      ;test the stored display table is preserved
+      (should (equal nil
+                     (progn (whitespace-mode 1)
+                            (whitespace-mode 1)
+                            (whitespace-mode -1)
+                            buffer-display-table))))))
+
 (provide 'whitespace-tests)
 
 ;;; whitespace-tests.el ends here
--- whitespace-a.el	2017-05-14 14:12:59.000000000 +1200
+++ whitespace-b.el	2017-05-15 01:11:30.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 (or whitespace-mode 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