branch: elpa/highlight-parentheses commit 8869fba83d2939ce32e21be39ebc182c8a414851 Author: Tassilo Horn <t...@gnu.org> Commit: Tassilo Horn <t...@gnu.org>
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