mirror of
https://github.com/org-roam/org-roam
synced 2025-08-17 13:33:31 -05:00
Add automatic update of org-roam buffer
This commit is contained in:
70
org-roam.el
70
org-roam.el
@@ -14,7 +14,7 @@
|
|||||||
"Org-roam buffer name.")
|
"Org-roam buffer name.")
|
||||||
|
|
||||||
(defcustom org-roam-position 'right
|
(defcustom org-roam-position 'right
|
||||||
"Position of org-roam buffer.
|
"Position of `org-roam' buffer.
|
||||||
|
|
||||||
Valid values are
|
Valid values are
|
||||||
* left,
|
* left,
|
||||||
@@ -24,7 +24,10 @@ Valid values are
|
|||||||
:group 'org-roam)
|
:group 'org-roam)
|
||||||
|
|
||||||
(defvar org-roam-hash-backlinks nil
|
(defvar org-roam-hash-backlinks nil
|
||||||
"Cache containing backlinks for org-roam buffers.")
|
"Cache containing backlinks for `org-roam' buffers.")
|
||||||
|
|
||||||
|
(defvar org-roam-current-file nil
|
||||||
|
"The current file being shown in the `org-roam' buffer.")
|
||||||
|
|
||||||
(define-inline org-roam-current-visibility ()
|
(define-inline org-roam-current-visibility ()
|
||||||
"Return whether the current visibility state of the org-roam buffer.
|
"Return whether the current visibility state of the org-roam buffer.
|
||||||
@@ -37,7 +40,7 @@ Valid states are 'visible, 'exists and 'none."
|
|||||||
(t 'none))))
|
(t 'none))))
|
||||||
|
|
||||||
(defun org-roam-insert (file-name)
|
(defun org-roam-insert (file-name)
|
||||||
"Finds a file, inserts it as a link with the base file name as the link name."
|
"Find file `FILE-NAME', insert it as a link with the base file name as the link name."
|
||||||
(interactive (list (completing-read "File: " (deft-find-all-files-no-prefix))))
|
(interactive (list (completing-read "File: " (deft-find-all-files-no-prefix))))
|
||||||
(let ((org-link-file-type 'relative))
|
(let ((org-link-file-type 'relative))
|
||||||
(org-insert-link nil (concat "file:" (concat deft-directory file-name))
|
(org-insert-link nil (concat "file:" (concat deft-directory file-name))
|
||||||
@@ -60,7 +63,7 @@ Valid states are 'visible, 'exists and 'none."
|
|||||||
3)))
|
3)))
|
||||||
|
|
||||||
(defun org-roam-get-links-from-buffer (buffer)
|
(defun org-roam-get-links-from-buffer (buffer)
|
||||||
"Returns a list of links from an Org buffer."
|
"Return a list of links from an Org BUFFER."
|
||||||
(with-current-buffer buffer
|
(with-current-buffer buffer
|
||||||
(org-element-map (org-element-parse-buffer) 'link
|
(org-element-map (org-element-parse-buffer) 'link
|
||||||
(lambda (link)
|
(lambda (link)
|
||||||
@@ -72,7 +75,7 @@ Valid states are 'visible, 'exists and 'none."
|
|||||||
|
|
||||||
|
|
||||||
(defun org-roam-build-backlinks ()
|
(defun org-roam-build-backlinks ()
|
||||||
(interactive)
|
"Builds the backlink hash table, saving it into `org-roam-hash-backlinks'."
|
||||||
(message "building backlinks...")
|
(message "building backlinks...")
|
||||||
(let ((backlinks (ht)))
|
(let ((backlinks (ht)))
|
||||||
(-map (lambda (file)
|
(-map (lambda (file)
|
||||||
@@ -92,8 +95,8 @@ Valid states are 'visible, 'exists and 'none."
|
|||||||
(setq org-roam-hash-backlinks backlinks)))
|
(setq org-roam-hash-backlinks backlinks)))
|
||||||
|
|
||||||
(defun org-roam-new-file-named (slug)
|
(defun org-roam-new-file-named (slug)
|
||||||
"Create a new file named SLUG.
|
"Create a new file named `SLUG'.
|
||||||
SLUG is the short file name, without a path or a file extension."
|
`SLUG' is the short file name, without a path or a file extension."
|
||||||
(interactive "sNew filename (without extension): ")
|
(interactive "sNew filename (without extension): ")
|
||||||
(let ((file (deft-absolute-filename slug)))
|
(let ((file (deft-absolute-filename slug)))
|
||||||
(unless (file-exists-p file)
|
(unless (file-exists-p file)
|
||||||
@@ -107,40 +110,45 @@ SLUG is the short file name, without a path or a file extension."
|
|||||||
(interactive)
|
(interactive)
|
||||||
(org-roam-new-file-named (format-time-string "%Y-%m-%d" (current-time))))
|
(org-roam-new-file-named (format-time-string "%Y-%m-%d" (current-time))))
|
||||||
|
|
||||||
(defun org-roam-update ()
|
(defun org-roam-update (file)
|
||||||
(interactive)
|
(interactive)
|
||||||
"Show the backlinks for the current org-buffer."
|
"Show the backlinks for the current org-buffer."
|
||||||
(unless org-roam-hash-backlinks
|
(unless (string= org-roam-current-file file)
|
||||||
(org-roam-build-backlinks))
|
(unless org-roam-hash-backlinks
|
||||||
(let* ((file (file-name-nondirectory (buffer-file-name (current-buffer))))
|
(org-roam-build-backlinks))
|
||||||
(backlinks (ht-get org-roam-hash-backlinks file)))
|
(let ((backlinks (ht-get org-roam-hash-backlinks file)))
|
||||||
(with-current-buffer org-roam-buffer
|
(with-current-buffer org-roam-buffer
|
||||||
(read-only-mode -1)
|
(read-only-mode -1)
|
||||||
(erase-buffer)
|
(erase-buffer)
|
||||||
(org-mode)
|
(org-mode)
|
||||||
(make-local-variable 'org-return-follows-link)
|
(make-local-variable 'org-return-follows-link)
|
||||||
(setq org-return-follows-link t)
|
(setq org-return-follows-link t)
|
||||||
(insert (format "Backlinks for %s:\n\n" file))
|
(insert (format "Backlinks for %s:\n\n" file))
|
||||||
(-map (lambda (link)
|
(-map (lambda (link)
|
||||||
(insert (format "- [[file:%s][%s]]\n" (expand-file-name link deft-directory) link))
|
(insert (format "- [[file:%s][%s]]\n" (expand-file-name link deft-directory) link))
|
||||||
) backlinks)
|
) backlinks)
|
||||||
(read-only-mode +1))))
|
(read-only-mode +1))))
|
||||||
|
(setq org-roam-current-file file))
|
||||||
|
|
||||||
(defun org-roam ()
|
(defun org-roam ()
|
||||||
"Initialize org-roam."
|
"Initialize org-roam."
|
||||||
(interactive)
|
(interactive)
|
||||||
|
(add-hook 'post-command-hook 'org-roam-update-buffer)
|
||||||
(pcase (org-roam-current-visibility)
|
(pcase (org-roam-current-visibility)
|
||||||
('visible (org-roam-update))
|
('visible (delete-window (get-buffer-window org-roam-buffer)))
|
||||||
('exists (org-roam-split))
|
('exists (org-roam-setup-buffer))
|
||||||
('none (org-roam-split))))
|
('none (org-roam-setup-buffer))))
|
||||||
|
|
||||||
(defun org-roam-split ()
|
|
||||||
(interactive)
|
|
||||||
(org-roam-setup-buffer)
|
|
||||||
(org-roam-update))
|
|
||||||
|
|
||||||
(defun org-roam-setup-buffer ()
|
(defun org-roam-setup-buffer ()
|
||||||
|
"Sets up the org-roam buffer at the side."
|
||||||
(-> (get-buffer-create org-roam-buffer)
|
(-> (get-buffer-create org-roam-buffer)
|
||||||
(display-buffer-in-side-window `((side . ,org-roam-position)))))
|
(display-buffer-in-side-window `((side . ,org-roam-position)))))
|
||||||
|
|
||||||
|
(defun org-roam-update-buffer ()
|
||||||
|
(interactive)
|
||||||
|
(when (and (eq major-mode 'org-mode)
|
||||||
|
(not (string= org-roam-current-file (buffer-file-name (current-buffer))))
|
||||||
|
(member (buffer-file-name (current-buffer)) (deft-find-all-files)))
|
||||||
|
(org-roam-update (file-name-nondirectory (buffer-file-name (current-buffer))))))
|
||||||
|
|
||||||
(provide 'org-roam)
|
(provide 'org-roam)
|
||||||
|
Reference in New Issue
Block a user