emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Sébastien Lerique" <sl@eauchat.org>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode@gnu.org
Subject: Re: An org-follow-mode following org/org-roam links
Date: Wed, 30 Oct 2024 20:19:35 +0100	[thread overview]
Message-ID: <87ldy5o0u0.fsf@eauchat.org> (raw)
In-Reply-To: <87a5eqvj7p.fsf@localhost> (Ihor Radchenko's message of "Sat, 26 Oct 2024 17:59:22 +0000")


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/


      reply	other threads:[~2024-10-30 19:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-21 14:56 An org-follow-mode following org/org-roam links Sébastien Lerique
2024-10-26 17:59 ` Ihor Radchenko
2024-10-30 19:19   ` Sébastien Lerique [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ldy5o0u0.fsf@eauchat.org \
    --to=sl@eauchat.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).