From e1b099a62ff3c718d1bd8a100f516b3dff035dd2 Mon Sep 17 00:00:00 2001 From: Karthik Chikmagalur Date: Sun, 4 Jun 2023 22:00:25 -0700 Subject: expand-region-core: Fix excursions when expensive expand-region-core.el (er--expand-region-1, er--save-excursion): When `er-save-excursion` is expensive, such as when using Org-mode's `org-save-outline-visibility`, calling it repeatedly for each function in `er/try-expand-list' is prohibitive. Fix by only calling it once around the expansion trials, and using `save-mark-and-excursion` around each expansion function. --- expand-region-core.el | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/expand-region-core.el b/expand-region-core.el index c239fd1..1f60dbe 100644 --- a/expand-region-core.el +++ b/expand-region-core.el @@ -72,8 +72,7 @@ `(save-excursion ,@body)))) (defmacro er--save-excursion (&rest body) - `(let ((action (lambda () - (save-mark-and-excursion ,@body)))) + `(let ((action (lambda () ,@body))) (if er/save-mode-excursion (funcall er/save-mode-excursion action) (funcall action)))) @@ -109,17 +108,18 @@ moving point or mark as little as possible." (skip-chars-forward er--space-str) (setq start (point))) - (while try-list - (er--save-excursion - (ignore-errors - (funcall (car try-list)) - (when (and (region-active-p) - (er--this-expansion-is-better start end best-start best-end)) - (setq best-start (point)) - (setq best-end (mark)) - (when (and er--show-expansion-message (not (minibufferp))) - (message "%S" (car try-list)))))) - (setq try-list (cdr try-list))) + (er--save-excursion + (while try-list + (ignore-errors + (save-mark-and-excursion + (funcall (car try-list)) + (when (and (region-active-p) + (er--this-expansion-is-better start end best-start best-end)) + (setq best-start (point)) + (setq best-end (mark)) + (when (and er--show-expansion-message (not (minibufferp))) + (message "%S" (car try-list)))))) + (setq try-list (cdr try-list)))) (setq deactivate-mark nil) ;; if smart cursor enabled, decide to put it at start or end of region: -- cgit v1.0