[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: An org-follow-mode following org/org-roam links
From: |
Sébastien Lerique |
Subject: |
Re: An org-follow-mode following org/org-roam links |
Date: |
Wed, 30 Oct 2024 20:19:35 +0100 |
On 26 Oct 2024 at 17:59, Ihor Radchenko <yantar92@posteo.net> wrote:
> You can do something like (save-excursion (org-back-to-heading t)
> (point-marker))
> This will move point back to the beginning of current heading, return
> marker corresponding to that point, and move back.
>
Indeed, thanks to
<https://org-roam.discourse.group/t/advice-for-an-org-follow-mode-following-org-org-roam-links/3631>
the final setup is the following, and works grand:
--8<---------------cut here---------------start------------->8---
(defvar sl/org-follow-preview-buffer nil)
(defun sl/org-follow-link ()
"Automatically open the first link in the current line.
When it is a file to open, the buffer will open in \"other\"
window."
(interactive)
(when (and (member this-command '(previous-line next-line
forward-char backward-char
left-char right-char
org-previous-link org-next-link))
;; This second condition may be part of the minor-mode function
(derived-mode-p 'org-mode))
(save-excursion
;; `cl-letf' is meant to ensure `file-file-other-window' to be used for
;; opening a file by temporarily changing the value of
;; `org-link-frame-setup' during evaluating the body.
(cl-letf (((alist-get 'file org-link-frame-setup)
#'find-file-other-window))
(let ((buffers-before-follow)
(win (selected-window))
(link
(when
(or (org-in-regexp org-link-any-re)
(re-search-forward org-link-any-re
(line-end-position) :t))
;; It seems `org-element-link-parser' must be located at
;; the beginning of the link at point.
(goto-char (match-beginning 0))
(org-element-link-parser))))
(when link
(when sl/org-follow-preview-buffer
(kill-buffer sl/org-follow-preview-buffer)
(setq sl/org-follow-preview-buffer nil))
(setq buffers-before-follow (buffer-list))
(org-link-open link)
;; Current buffer is the link target
(unless (member (current-buffer) buffers-before-follow)
(rename-buffer (concat "preview: " (buffer-name)))
(setq sl/org-follow-preview-buffer (current-buffer))))
(select-window win))))))
(define-minor-mode sl/org-follow-link-minor-mode
"Automatically open the first link in the current line.
When it is a file to open, the buffer will open in \"other\"
window."
:lighter " sfl" ;; The indicator for the mode line.
(if sl/org-follow-link-minor-mode
(add-hook 'post-command-hook #'sl/org-follow-link nil :local)
(remove-hook 'post-command-hook #'sl/org-follow-link :local)))
--8<---------------cut here---------------end--------------->8---
Best
--
Sébastien Lerique
https://slvh.fr/