From 098f0815916fcfd88b24d7a0a842c3b294b4383d Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Mon, 23 Oct 2023 14:28:04 +0300 Subject: [PATCH] org-open-at-point: Preserve point unless opening link moves the point * lisp/org.el (org-open-at-point): Preserve point when opening links. When a link opened moves point in current buffer, do move the point as needed by the link. When multiple links that are opened move point in current buffer, move point to the last such link. Fix searching for the links when opening _multiple_ the links is requested. Reported-by: Gustavo Barros Link: https://orgmode.org/list/87r0mp2srd.fsf@localhost --- lisp/org.el | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index bda64bb6c..fbaf655d8 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -8495,13 +8495,20 @@ a link." (org-attach-reveal-in-emacs) (org-attach-reveal)))) (`(,links . ,links-end) - (dolist (link (if (stringp links) (list links) links)) - (search-forward link nil links-end) - (goto-char (match-beginning 0)) - ;; When opening file link, current buffer may be - ;; altered. - (save-current-buffer - (org-open-at-point arg)))))))) + (let ((link-marker (make-marker)) + (last-moved-marker (point-marker))) + (dolist (link (if (stringp links) (list links) links)) + (search-forward link nil links-end) + (goto-char (match-beginning 0)) + (move-marker link-marker (point)) + (save-excursion + (org-open-at-point arg) + (unless (equal (point-marker) link-marker) + (move-marker last-moved-marker (point-marker))))) + ;; If any of the links moved point in current buffer, + ;; move to the point corresponding to such latest link. + ;; Otherwise, restore the original point position. + (goto-char last-moved-marker))))))) ;; On a footnote reference or at definition's label. ((or (eq type 'footnote-reference) (and (eq type 'footnote-definition)