mirror of
https://github.com/org-roam/org-roam
synced 2025-08-01 12:17:21 -05:00
Code cleanup
This commit is contained in:
58
org-roam.el
58
org-roam.el
@ -1,3 +1,9 @@
|
|||||||
|
;;; org-roam.el --- Roam Research replica with Org-mode and Deft
|
||||||
|
|
||||||
|
;;; Commentary:
|
||||||
|
;;
|
||||||
|
|
||||||
|
;;; Code:
|
||||||
(require 'deft)
|
(require 'deft)
|
||||||
(require 'async)
|
(require 'async)
|
||||||
|
|
||||||
@ -73,7 +79,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)
|
||||||
"Return a list of links from an Org BUFFER."
|
"Return a list of links from 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)
|
||||||
@ -84,7 +90,7 @@ Valid states are 'visible, 'exists and 'none."
|
|||||||
path))))))
|
path))))))
|
||||||
|
|
||||||
(defun org-roam-add-backlink (hash link_a link_b)
|
(defun org-roam-add-backlink (hash link_a link_b)
|
||||||
"Adds a backlink link_a <- link_b to hash table `hash'."
|
"Add a backlink LINK_A <- LINK_B to hash-table HASH."
|
||||||
(let* ((item (gethash link_a hash))
|
(let* ((item (gethash link_a hash))
|
||||||
(updated (if item
|
(updated (if item
|
||||||
(if (member link_b item)
|
(if (member link_b item)
|
||||||
@ -93,35 +99,6 @@ Valid states are 'visible, 'exists and 'none."
|
|||||||
(list link_b))))
|
(list link_b))))
|
||||||
(puthash link_a updated hash)))
|
(puthash link_a updated hash)))
|
||||||
|
|
||||||
(defun org-roam-build-backlinks ()
|
|
||||||
"Builds the backlink hash table, saving it into `org-roam-hash-backlinks'."
|
|
||||||
(interactive)
|
|
||||||
(let ((backlinks (make-hash-table)))
|
|
||||||
(mapcar (lambda (file)
|
|
||||||
(with-temp-buffer
|
|
||||||
(insert-file-contents file)
|
|
||||||
(mapcar (lambda (link)
|
|
||||||
(let* ((item (gethash link backlinks))
|
|
||||||
(updated (if item
|
|
||||||
(if (member (file-name-nondirectory
|
|
||||||
file) item)
|
|
||||||
item
|
|
||||||
(cons (file-name-nondirectory
|
|
||||||
file) item))
|
|
||||||
(list (file-name-nondirectory
|
|
||||||
file)))))
|
|
||||||
(puthash link updated backlinks)))
|
|
||||||
(with-current-buffer (current-buffer)
|
|
||||||
(org-element-map (org-element-parse-buffer) 'link
|
|
||||||
(lambda (link)
|
|
||||||
(let ((type (org-element-property :type link))
|
|
||||||
(path (org-element-property :path link)))
|
|
||||||
(when (and (string= type "file")
|
|
||||||
(string= (file-name-extension path) "org"))
|
|
||||||
path))))))))
|
|
||||||
(deft-find-all-files))
|
|
||||||
(setq org-roam-hash-backlinks backlinks)))
|
|
||||||
|
|
||||||
(defun org-roam-build-backlinks-async ()
|
(defun org-roam-build-backlinks-async ()
|
||||||
"Builds the backlink hash table asychronously, saving it into `org-roam-hash-backlinks'."
|
"Builds the backlink hash table asychronously, saving it into `org-roam-hash-backlinks'."
|
||||||
(interactive)
|
(interactive)
|
||||||
@ -173,12 +150,12 @@ Valid states are 'visible, 'exists and 'none."
|
|||||||
(deft-open-file file)))
|
(deft-open-file file)))
|
||||||
|
|
||||||
(defun org-roam-today ()
|
(defun org-roam-today ()
|
||||||
"Creates the file for today."
|
"Create the file for today."
|
||||||
(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 (file)
|
(defun org-roam-update (file)
|
||||||
"Show the backlinks for the current org-buffer."
|
"Show the backlinks for given org file `FILE'."
|
||||||
(unless (string= org-roam-current-file file)
|
(unless (string= org-roam-current-file file)
|
||||||
(let ((backlinks (gethash file org-roam-hash-backlinks)))
|
(let ((backlinks (gethash file org-roam-hash-backlinks)))
|
||||||
(with-current-buffer org-roam-buffer
|
(with-current-buffer org-roam-buffer
|
||||||
@ -195,7 +172,10 @@ Valid states are 'visible, 'exists and 'none."
|
|||||||
(setq org-roam-current-file file))
|
(setq org-roam-current-file file))
|
||||||
|
|
||||||
(defun org-roam ()
|
(defun org-roam ()
|
||||||
"Initialize org-roam."
|
"Initialize `org-roam'.
|
||||||
|
1. Setup to auto-update `org-roam-buffer' with the correct information.
|
||||||
|
2. Starts the timer to asynchronously build backlinks.
|
||||||
|
3. Pops up the window `org-roam-buffer' accordingly."
|
||||||
(interactive)
|
(interactive)
|
||||||
(add-hook 'post-command-hook 'org-roam-update-buffer)
|
(add-hook 'post-command-hook 'org-roam-update-buffer)
|
||||||
(unless org-roam-update-timer
|
(unless org-roam-update-timer
|
||||||
@ -207,18 +187,24 @@ Valid states are 'visible, 'exists and 'none."
|
|||||||
('none (org-roam-setup-buffer))))
|
('none (org-roam-setup-buffer))))
|
||||||
|
|
||||||
(defun org-roam-stop ()
|
(defun org-roam-stop ()
|
||||||
|
"Cancels auto-building of backlinks."
|
||||||
(interactive)
|
(interactive)
|
||||||
(when org-roam-update-timer
|
(when org-roam-update-timer
|
||||||
(cancel-timer org-roam-update-timer)
|
(cancel-timer org-roam-update-timer)
|
||||||
(setq org-roam-update-timer nil)))
|
(setq org-roam-update-timer nil)))
|
||||||
|
|
||||||
(defun org-roam-setup-buffer ()
|
(defun org-roam-setup-buffer ()
|
||||||
"Sets up the org-roam buffer at the side."
|
"Setup the `org-roam' buffer at the `org-roam-position'."
|
||||||
(display-buffer-in-side-window
|
(display-buffer-in-side-window
|
||||||
(get-buffer-create org-roam-buffer)
|
(get-buffer-create org-roam-buffer)
|
||||||
`((side . ,org-roam-position))))
|
`((side . ,org-roam-position))))
|
||||||
|
|
||||||
(defun org-roam-update-buffer ()
|
(defun org-roam-update-buffer ()
|
||||||
|
"Update `org-roam-buffer' with the necessary information.
|
||||||
|
This needs to be quick/infrequent, because this is run at
|
||||||
|
`post-command-hook'. This is achieved by only checking Org files
|
||||||
|
that are amongst deft files, and `org-roam' not already
|
||||||
|
displaying information for the correct file."
|
||||||
(interactive)
|
(interactive)
|
||||||
(when (and (eq major-mode 'org-mode)
|
(when (and (eq major-mode 'org-mode)
|
||||||
(not (string= org-roam-current-file (buffer-file-name (current-buffer))))
|
(not (string= org-roam-current-file (buffer-file-name (current-buffer))))
|
||||||
@ -226,3 +212,5 @@ Valid states are 'visible, 'exists and 'none."
|
|||||||
(org-roam-update (file-name-nondirectory (buffer-file-name (current-buffer))))))
|
(org-roam-update (file-name-nondirectory (buffer-file-name (current-buffer))))))
|
||||||
|
|
||||||
(provide 'org-roam)
|
(provide 'org-roam)
|
||||||
|
|
||||||
|
;;; org-roam.el ends here
|
||||||
|
Reference in New Issue
Block a user