From 8869fba83d2939ce32e21be39ebc182c8a414851 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sat, 11 Apr 2015 15:46:45 +0200 Subject: [PATCH 1/2] Fix slowdown by moving overlays less often. Now a new function `hl-paren-initiate-highlight` is in `post-command-hook` instead of `hl-paren-highlight` itself. The new function uses a timer to skip calls to `hl-paren-highlight` in case those come faster than about one in a quarter of a second. That easily happens when scrolling by pressing and holding `C-n`. Fixes issue #8. In contrast to the subject of the issue, I don't use an idle timer but a normal one to ensure that the hl-paren updates are immediately visible from a user's point of view. --- highlight-parentheses.el | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/highlight-parentheses.el b/highlight-parentheses.el index 0597721..eeee89f 100644 --- a/highlight-parentheses.el +++ b/highlight-parentheses.el @@ -96,6 +96,9 @@ Color attributes might be overriden by `hl-paren-colors' and This is used to prevent analyzing the same context over and over.") (make-variable-buffer-local 'hl-paren-last-point) +(defvar hl-paren-timer nil + "A timer initiating the movement of the `hl-paren-overlays'.") + (defun hl-paren-highlight () "Highlight the parentheses around point." (unless (= (point) hl-paren-last-point) @@ -109,13 +112,18 @@ This is used to prevent analyzing the same context over and over.") (cdr overlays)) (move-overlay (pop overlays) pos1 (1+ pos1)) (when (setq pos2 (scan-sexps pos1 1)) - (move-overlay (pop overlays) (1- pos2) pos2) - )) + (move-overlay (pop overlays) (1- pos2) pos2))) (error nil)) (goto-char pos)) (dolist (ov overlays) (move-overlay ov 1 1))))) +(defun hl-paren-initiate-highlight () + "Move the `hl-paren-overlays' after a short fraction of time." + (when hl-paren-timer + (cancel-timer hl-paren-timer)) + (setq hl-paren-timer (run-at-time 0.23 nil #'hl-paren-highlight))) + ;;;###autoload (define-minor-mode highlight-parentheses-mode "Minor mode to highlight the surrounding parentheses." @@ -123,10 +131,10 @@ This is used to prevent analyzing the same context over and over.") (mapc 'delete-overlay hl-paren-overlays) (kill-local-variable 'hl-paren-overlays) (kill-local-variable 'hl-paren-last-point) - (remove-hook 'post-command-hook 'hl-paren-highlight t) + (remove-hook 'post-command-hook 'hl-paren-initiate-highlight t) (when highlight-parentheses-mode (hl-paren-create-overlays) - (add-hook 'post-command-hook 'hl-paren-highlight nil t))) + (add-hook 'post-command-hook 'hl-paren-initiate-highlight nil t))) ;;;###autoload (define-globalized-minor-mode global-highlight-parentheses-mode From 5e1fa9d60cc1e2c8d71d04f09058060ae51a9560 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sun, 12 Apr 2015 08:01:34 +0200 Subject: [PATCH 2/2] Make overlays front-advancing Without that, when inserting text at (foo bar|) where | indicates point, the inserted text will be made part of the overlay initially until hl-paren-highlight is called the next time and thus have 'face hl-paren-face. This hasn't really be needed when hl-paren-highlight has been called from pre-command-hook, but now with a more lazy approach to moving overlays it's very important. --- highlight-parentheses.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlight-parentheses.el b/highlight-parentheses.el index eeee89f..1159b0f 100644 --- a/highlight-parentheses.el +++ b/highlight-parentheses.el @@ -156,7 +156,7 @@ This is used to prevent analyzing the same context over and over.") (setq attributes (plist-put attributes :background (car bg)))) (pop bg) (dotimes (i 2) ;; front and back - (push (make-overlay 0 0) hl-paren-overlays) + (push (make-overlay 0 0 nil t) hl-paren-overlays) (overlay-put (car hl-paren-overlays) 'face attributes))) (setq hl-paren-overlays (nreverse hl-paren-overlays))))