unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#56103: [PATCH] whitespace: Redraw if indentation or line length changes
@ 2022-06-20  5:44 Richard Hansen
  2022-06-20  7:40 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 2+ messages in thread
From: Richard Hansen @ 2022-06-20  5:44 UTC (permalink / raw)
  To: 56103


[-- Attachment #1.1.1: Type: text/plain, Size: 668 bytes --]

Attached patch:

     whitespace: Redraw if indentation or line length changes

     * lisp/whitespace.el (whitespace-color-on): Convert the indentation
     matcher from a static regular expression to a function so that changes
     to `indent-tabs-mode' and `tab-width' are picked up the next time
     `font-lock-flush' runs.
     (whitespace--indentation-matcher): The new function matcher.
     (whitespace--variable-watcher): New variable watcher that calls
     `font-lock-flush' if `whitespace-mode' is enabled for the buffer.
     (whitespace--watched-vars): List of variables to watch.
     (whitespace-unload-function): Un-watch the variables.

[-- Attachment #1.1.2: 0001-whitespace-Redraw-if-indentation-or-line-length-chan.patch --]
[-- Type: text/x-patch, Size: 3751 bytes --]

From 3c276adca2435d045110883c0e6cfc73c72cf12c Mon Sep 17 00:00:00 2001
From: Richard Hansen <rhansen@rhansen.org>
Date: Mon, 20 Jun 2022 01:04:29 -0400
Subject: [PATCH] whitespace: Redraw if indentation or line length changes

* lisp/whitespace.el (whitespace-color-on): Convert the indentation
matcher from a static regular expression to a function so that changes
to `indent-tabs-mode' and `tab-width' are picked up the next time
`font-lock-flush' runs.
(whitespace--indentation-matcher): The new function matcher.
(whitespace--variable-watcher): New variable watcher that calls
`font-lock-flush' if `whitespace-mode' is enabled for the buffer.
(whitespace--watched-vars): List of variables to watch.
(whitespace-unload-function): Un-watch the variables.
---
 lisp/whitespace.el | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/lisp/whitespace.el b/lisp/whitespace.el
index 7ee8a46cec..45e7ce683c 100644
--- a/lisp/whitespace.el
+++ b/lisp/whitespace.el
@@ -2112,16 +2112,7 @@ whitespace-color-on
        ,@(when (or (memq 'indentation whitespace-active-style)
                    (memq 'indentation::tab whitespace-active-style)
                    (memq 'indentation::space whitespace-active-style))
-           `((,(cond
-                ((memq 'indentation whitespace-active-style)
-                 ;; Show indentation SPACEs (indent-tabs-mode).
-                 (whitespace-indentation-regexp))
-                ((memq 'indentation::tab whitespace-active-style)
-                 ;; Show indentation SPACEs (SPACEs).
-                 (whitespace-indentation-regexp 'tab))
-                ((memq 'indentation::space whitespace-active-style)
-                 ;; Show indentation SPACEs (TABs).
-                 (whitespace-indentation-regexp 'space)))
+           `((,#'whitespace--indentation-matcher
               1 whitespace-indentation t)))
        ,@(when (memq 'big-indent whitespace-active-style)
            ;; Show big indentation.
@@ -2356,6 +2347,28 @@ whitespace-post-command-hook
         (font-lock-flush ostart (overlay-end whitespace-point--used))
         (delete-overlay whitespace-point--used))))))
 
+
+(defun whitespace--indentation-matcher (limit)
+  "Indentation matcher for `font-lock-keywords'.
+This matcher is a function instead of a static regular expression
+so that the next call to `font-lock-flush' picks up any changes
+to `indent-tabs-mode' and `tab-width'."
+  (re-search-forward
+   (whitespace-indentation-regexp
+    (cond
+     ((memq 'indentation whitespace-active-style) nil)
+     ((memq 'indentation::tab whitespace-active-style) 'tab)
+     ((memq 'indentation::space whitespace-active-style) 'space)))
+   limit t))
+
+
+(defun whitespace--variable-watcher (_symbol _newval _op buffer)
+  "Variable watcher that calls `font-lock-flush' for BUFFER."
+  (when buffer
+    (with-current-buffer buffer
+      (when whitespace-mode
+        (font-lock-flush)))))
+
 \f
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
@@ -2468,9 +2481,16 @@ whitespace-warn-read-only
 \f
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+(defvar whitespace--watched-vars
+  '(fill-column indent-tabs-mode tab-width whitespace-line-column))
+
+(dolist (var whitespace--watched-vars)
+  (add-variable-watcher var #'whitespace--variable-watcher))
 
 (defun whitespace-unload-function ()
   "Unload the whitespace library."
+  (dolist (var whitespace--watched-vars)
+    (remove-variable-watcher var #'whitespace--variable-watcher))
   (global-whitespace-mode -1)
   ;; be sure all local whitespace mode is turned off
   (save-current-buffer
-- 
2.36.1


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#56103: [PATCH] whitespace: Redraw if indentation or line length changes
  2022-06-20  5:44 bug#56103: [PATCH] whitespace: Redraw if indentation or line length changes Richard Hansen
@ 2022-06-20  7:40 ` Lars Ingebrigtsen
  0 siblings, 0 replies; 2+ messages in thread
From: Lars Ingebrigtsen @ 2022-06-20  7:40 UTC (permalink / raw)
  To: Richard Hansen; +Cc: 56103

Richard Hansen <rhansen@rhansen.org> writes:

>     whitespace: Redraw if indentation or line length changes

Thanks; pushed to Emacs 29.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-20  7:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-20  5:44 bug#56103: [PATCH] whitespace: Redraw if indentation or line length changes Richard Hansen
2022-06-20  7:40 ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).