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 <gusbrs.2016@gmail.com>
Link: https://orgmode.org/list/87r0mp2srd.fsf@localhost
This commit is contained in:
Ihor Radchenko 2023-10-23 14:28:04 +03:00
parent 20ab29117c
commit 098f081591
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 14 additions and 7 deletions

View File

@ -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)