Turn org-roam-backlinks-mode into a minor mode (#355)

This change makes `org-roam-open-at-point` non-interactive and adds it to
`org-open-at-point-functions`.  As a consequence, `org-open-at-point` will try
to visit an Org-roam link or preview text, before falling back to the default
behavior of Org mode.

Fixes #312.
This commit is contained in:
Johann Klähn 2020-03-27 06:06:44 +01:00 committed by GitHub
parent 3e0a49de03
commit de36d15d0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1152,45 +1152,54 @@ Applies `org-roam-link-face' if PATH corresponds to a Roam file."
;;;; org-roam-backlinks-mode
(defvar org-roam-backlinks-mode-map
(let ((km (make-sparse-keymap)))
(define-key km [mouse-1] 'org-roam-open-at-point)
(define-key km (kbd "RET") 'org-roam-open-at-point)
km)
(let ((map (make-sparse-keymap)))
(define-key map [mouse-1] 'org-open-at-point)
(define-key map (kbd "RET") 'org-open-at-point)
map)
"Keymap for `org-roam-backlinks-mode'.")
(define-derived-mode org-roam-backlinks-mode org-mode "Backlinks"
"Major mode for the `org-roam-buffer'.
(define-minor-mode org-roam-backlinks-mode
"Minor mode for the `org-roam-buffer'.
\\{org-roam-backlinks-mode-map}")
\\{org-roam-backlinks-mode-map}"
:lighter " Backlinks"
:keymap org-roam-backlinks-mode-map
(if org-roam-backlinks-mode
(add-hook 'org-open-at-point-functions
'org-roam-open-at-point nil 'local)
(remove-hook 'org-open-at-point-functions
'org-roam-open-at-point 'local)))
(defun org-roam-open-at-point ()
"Open a link at point.
"Open an Org-roam link or visit the text previewed at point.
When point is on an Org-roam link, open the link in the Org-roam window.
When point is on the Org-roam preview text, open the link in the Org-roam
window, and navigate to the point.
If item at point is not Org-roam specific, default to Org behaviour."
(interactive)
(let ((context (org-element-context)))
(catch 'ret
;; Org-roam link
This function hooks into `org-open-at-point' via `org-open-at-point-functions'."
(cond
;; Org-roam link
((let* ((context (org-element-context))
(type (org-element-property :type context))
(path (org-element-property :path context)))
(when (and (eq (org-element-type context) 'link)
(string= "file" (org-element-property :type context))
(org-roam--org-roam-file-p (file-truename (org-element-property :path context))))
(org-roam--find-file (org-element-property :path context))
(string= "file" type)
(org-roam--org-roam-file-p (file-truename path)))
(org-roam--find-file path)
(org-show-context)
(throw 'ret t))
;; Org-roam preview text
(when-let ((file-from (get-text-property (point) 'file-from))
(p (get-text-property (point) 'file-from-point)))
(org-roam--find-file file-from)
(goto-char p)
(org-show-context)
(throw 'ret t))
;; Default to default org behaviour
(org-open-at-point))))
t)))
;; Org-roam preview text
((when-let ((file-from (get-text-property (point) 'file-from))
(p (get-text-property (point) 'file-from-point)))
(org-roam--find-file file-from)
(goto-char p)
(org-show-context)
t))
;; If called via `org-open-at-point', fall back to default behavior.
nil))
(defun org-roam--find-file (file)
"Open FILE in the window `org-roam' was called from."
@ -1227,7 +1236,9 @@ If item at point is not Org-roam specific, default to Org behaviour."
(cons '(file . org-roam--find-file) org-link-frame-setup))
(let ((inhibit-read-only t))
(erase-buffer)
(unless (eq major-mode 'org-roam-backlinks-mode)
(unless (eq major-mode 'org-mode)
(org-mode))
(unless org-roam-backlinks-mode
(org-roam-backlinks-mode))
(make-local-variable 'org-return-follows-link)
(setq org-return-follows-link t)