emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* An org-follow-mode following org/org-roam links
@ 2024-10-21 14:56 Sébastien Lerique
  2024-10-26 17:59 ` Ihor Radchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Sébastien Lerique @ 2024-10-21 14:56 UTC (permalink / raw)
  To: emacs-orgmode

Hello all,

First of all thanks to everybody for org (file, agenda, and much more),
such an amazing tool!

I'm trying to add a minor "org-follow-mode" somewhere between
"org-agenda-follow-mode" and "org-roam-ui-follow-mode"
<https://github.com/org-roam/> (request also posted on
<https://org-roam.discourse.group/t/advice-for-an-org-follow-mode-following-org-org-roam-links/3631>).
My intuition so far was to start from a copy of "org-agenda-follow-mode"
(and subcommands), adapting it to an org file being the main window.

--8<---------------cut here---------------start------------->8---
(defvar org-follow-mode nil)

(defvar org-pre-follow-window-conf nil)

(defun org-marked-tree-to-indirect-buffer (arg)
  ;; TODO needed?
  ;; (interactive "P")
  ;; (org-agenda-check-no-diary)
  (let* ((marker
          ;;(or
          (org-get-at-bol 'org-marker)
	  ;; (org-agenda-error)
          ;; )
          )
	 (buffer (marker-buffer marker))
	 (pos (marker-position marker)))
    (with-current-buffer buffer
      (save-excursion
	(goto-char pos)
	(org-tree-to-indirect-buffer arg))))
  ;; TODO needed?
  ;; (setq org-agenda-last-indirect-buffer org-last-indirect-buffer)
  )

(defcustom org-follow-indirect nil
  "Non-nil means `org-follow-mode' displays only the current item's
tree, in an indirect buffer."
  :group 'org
  :version "29.4"
  :type 'boolean)

(defun org-do-context-action ()
  "Show outline path and, maybe, follow mode window."
  (let ((m (org-get-at-bol 'org-marker)))
    (when (and (markerp m) (marker-buffer m))
      (and org-follow-mode
           (if org-follow-indirect
               (let ((org-indirect-buffer-display 'other-window))
                 ;; TODO or org-marked-tree-to-indirect-buffer from above
                 (org-tree-to-indirect-buffer nil))
             ;; TODO needed?
             ;; (org-agenda-show)
             ;; TODO
             ;; (and org-agenda-show-outline-path
             ;;      (org-with-point-at m (org-display-outline-path org-agenda-show-outline-path)))
             )))))

(defun org-follow-mode ()
  "Toggle follow mode in a buffer."
  (interactive)
  (unless org-follow-mode
    (setq org-pre-follow-window-conf
	  (current-window-configuration)))
  (setq org-follow-mode (not org-follow-mode))
  (unless org-follow-mode
    (set-window-configuration org-pre-follow-window-conf))
  ;; TODO needed?
  ;; (org-set-mode-name)
  (org-do-context-action)
  (message "Follow mode is %s"
	   (if org-follow-mode "on" "off")))
--8<---------------cut here---------------end--------------->8---

Now this obviously doesn't work, primarily because I have no
`org-marker` defined as no agenda line is selected.

I then went over the "org-roam-ui-follow-mode" code, little of which I
could use for this as most of the follow-mode looks simpler, directly
using the network instead of having to manage windows.

Would anyone have some advice to set this up? How to replace the use of
`marker` and read the current org position instead?

Thanks again for reading, and best to y'all
-- 
Sébastien Lerique
https://slvh.fr/


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: An org-follow-mode following org/org-roam links
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Ihor Radchenko @ 2024-10-26 17:59 UTC (permalink / raw)
  To: Sébastien Lerique; +Cc: emacs-orgmode

Sébastien Lerique <sl@eauchat.org> writes:

> ...
> Now this obviously doesn't work, primarily because I have no
> `org-marker` defined as no agenda line is selected.
>
> I then went over the "org-roam-ui-follow-mode" code, little of which I
> could use for this as most of the follow-mode looks simpler, directly
> using the network instead of having to manage windows.
>
> Would anyone have some advice to set this up? How to replace the use of
> `marker` and read the current org position instead?

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.

Hope it helps.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: An org-follow-mode following org/org-roam links
  2024-10-26 17:59 ` Ihor Radchenko
@ 2024-10-30 19:19   ` Sébastien Lerique
  0 siblings, 0 replies; 3+ messages in thread
From: Sébastien Lerique @ 2024-10-30 19:19 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode


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/


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-30 19:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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

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