From 7c4bf1e9c32c6fe2575ad8a6bd013a0b92659395 Mon Sep 17 00:00:00 2001 From: Matus Goljer Date: Sun, 12 Feb 2023 13:21:55 +0100 Subject: [PATCH] fix: preserve point before widening and re-narrowing Some languages can narrow to a region not including point when invoking narrow-to-defun. This will cause the cursor to jump around when the restriction is restored. To prevent this, remember also the original point. --- highlight-thing.el | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/highlight-thing.el b/highlight-thing.el index 28f3047..995f0d1 100644 --- a/highlight-thing.el +++ b/highlight-thing.el @@ -225,15 +225,16 @@ functionality." (defun highlight-thing-buffer-do (buf regex) (with-current-buffer buf - (save-restriction - (widen) - (cond ((highlight-thing-should-narrow-to-defun-p) - (narrow-to-defun)) - ((highlight-thing-should-narrow-to-region-p) - (let ((bounds (highlight-thing-narrow-bounds))) - (narrow-to-region (car bounds) (cdr bounds))))) - (highlight-thing-call-highlight-regexp regex) - (when highlight-thing-exclude-thing-under-point (highlight-thing-remove-overlays-at-point regex))))) + (save-excursion + (save-restriction + (widen) + (cond ((highlight-thing-should-narrow-to-defun-p) + (narrow-to-defun)) + ((highlight-thing-should-narrow-to-region-p) + (let ((bounds (highlight-thing-narrow-bounds))) + (narrow-to-region (car bounds) (cdr bounds))))) + (highlight-thing-call-highlight-regexp regex) + (when highlight-thing-exclude-thing-under-point (highlight-thing-remove-overlays-at-point regex)))))) (defun highlight-thing-call-highlight-regexp (regex) (unless (string= "" regex)