mirror of
https://github.com/org-roam/org-roam
synced 2025-09-24 16:30:55 -05:00
deduplicate async/non-async functions (#72)
Signed-off-by: Jethro Kuan <jethrokuan95@gmail.com>
This commit is contained in:
220
org-roam.el
220
org-roam.el
@@ -161,7 +161,6 @@ If called interactively, then PARENTS is non-nil."
|
|||||||
(unless org-roam-cache-initialized
|
(unless org-roam-cache-initialized
|
||||||
(user-error "The Org-Roam caches aren't built! Please run org-roam--build-cache-async"))
|
(user-error "The Org-Roam caches aren't built! Please run org-roam--build-cache-async"))
|
||||||
nil)))
|
nil)))
|
||||||
|
|
||||||
(defun org-roam--find-files (dir)
|
(defun org-roam--find-files (dir)
|
||||||
"Return all org-roam files in `DIR'."
|
"Return all org-roam files in `DIR'."
|
||||||
(if (file-exists-p dir)
|
(if (file-exists-p dir)
|
||||||
@@ -181,6 +180,65 @@ If called interactively, then PARENTS is non-nil."
|
|||||||
(setq result (cons (file-truename file) result)))))
|
(setq result (cons (file-truename file) result)))))
|
||||||
result)))
|
result)))
|
||||||
|
|
||||||
|
(defun org-roam--parse-content (&optional file-path)
|
||||||
|
"Parse the current buffer, and return a list of items for processing."
|
||||||
|
(org-element-map (org-element-parse-buffer) 'link
|
||||||
|
(lambda (link)
|
||||||
|
(let ((type (org-element-property :type link))
|
||||||
|
(path (org-element-property :path link))
|
||||||
|
(start (org-element-property :begin link)))
|
||||||
|
(when (and (string= type "file")
|
||||||
|
(string= (file-name-extension path) "org"))
|
||||||
|
(goto-char start)
|
||||||
|
(let* ((element (org-element-at-point))
|
||||||
|
(content (or (org-element-property :raw-value element)
|
||||||
|
(buffer-substring
|
||||||
|
(or (org-element-property :content-begin element)
|
||||||
|
(org-element-property :begin element))
|
||||||
|
(or (org-element-property :content-end element)
|
||||||
|
(org-element-property :end element))))))
|
||||||
|
(list :from (or file-path
|
||||||
|
(file-truename (buffer-file-name (current-buffer))))
|
||||||
|
:to (file-truename (expand-file-name path org-roam-directory))
|
||||||
|
:content (string-trim content))))))))
|
||||||
|
|
||||||
|
(cl-defun org-roam--insert-item (item &key forward backward)
|
||||||
|
"Insert ITEM into FORWARD and BACKWARD cache.
|
||||||
|
|
||||||
|
ITEM is of the form: (:from from-path :to to-path :content preview-content)."
|
||||||
|
(pcase-let ((`(:from ,p-from :to ,p-to :content ,content) item))
|
||||||
|
;; Build forward-links
|
||||||
|
(let ((links (gethash p-from forward)))
|
||||||
|
(if links
|
||||||
|
(puthash p-from
|
||||||
|
(if (member p-to links)
|
||||||
|
links
|
||||||
|
(cons p-to links)) forward)
|
||||||
|
(puthash p-from (list p-to) forward)))
|
||||||
|
;; Build backward-links
|
||||||
|
(let ((contents-hash (gethash p-to backward)))
|
||||||
|
(if contents-hash
|
||||||
|
(if-let ((contents-list (gethash p-from contents-hash)))
|
||||||
|
(let ((updated (cons content contents-list)))
|
||||||
|
(puthash p-from updated contents-hash)
|
||||||
|
(puthash p-to contents-hash backward))
|
||||||
|
(progn
|
||||||
|
(puthash p-from (list content) contents-hash)
|
||||||
|
(puthash p-to contents-hash backward)))
|
||||||
|
(let ((contents-hash (make-hash-table :test #'equal)))
|
||||||
|
(puthash p-from (list content) contents-hash)
|
||||||
|
(puthash p-to contents-hash backward))))))
|
||||||
|
|
||||||
|
(defun org-roam--extract-title ()
|
||||||
|
"Extract the title from `BUFFER'."
|
||||||
|
(org-element-map
|
||||||
|
(org-element-parse-buffer)
|
||||||
|
'keyword
|
||||||
|
(lambda (kw)
|
||||||
|
(when (string= (org-element-property :key kw) "TITLE")
|
||||||
|
(org-element-property :value kw)))
|
||||||
|
:first-match t))
|
||||||
|
|
||||||
(defun org-roam--find-all-files ()
|
(defun org-roam--find-all-files ()
|
||||||
"Return all org-roam files."
|
"Return all org-roam files."
|
||||||
(org-roam--find-files (file-truename org-roam-directory)))
|
(org-roam--find-files (file-truename org-roam-directory)))
|
||||||
@@ -213,6 +271,7 @@ If `ABSOLUTE', return the absolute file-path. Else, return the relative file-pat
|
|||||||
(s (s-join "_" s)))
|
(s (s-join "_" s)))
|
||||||
s))
|
s))
|
||||||
|
|
||||||
|
|
||||||
;;; Creating org-roam files
|
;;; Creating org-roam files
|
||||||
(defun org-roam--populate-title (file &optional title)
|
(defun org-roam--populate-title (file &optional title)
|
||||||
"Populate title line for FILE using TITLE, if provided.
|
"Populate title line for FILE using TITLE, if provided.
|
||||||
@@ -311,95 +370,30 @@ Optionally pass it the title, for a smart file name."
|
|||||||
(require 'subr-x) ; temp-fix
|
(require 'subr-x) ; temp-fix
|
||||||
(require 'cl-lib)
|
(require 'cl-lib)
|
||||||
,(async-inject-variables "org-roam-directory")
|
,(async-inject-variables "org-roam-directory")
|
||||||
|
(defalias 'org-roam--find-files ,(symbol-function 'org-roam--find-files))
|
||||||
|
(defalias 'org-roam--parse-content ,(symbol-function 'org-roam--parse-content))
|
||||||
|
(defalias 'org-roam--insert-item ,(symbol-function 'org-roam--insert-item))
|
||||||
|
(defalias 'org-roam--extract-title ,(symbol-function 'org-roam--extract-title))
|
||||||
(let ((backward-links (make-hash-table :test #'equal))
|
(let ((backward-links (make-hash-table :test #'equal))
|
||||||
(forward-links (make-hash-table :test #'equal))
|
(forward-links (make-hash-table :test #'equal))
|
||||||
(file-titles (make-hash-table :test #'equal)))
|
(file-titles (make-hash-table :test #'equal)))
|
||||||
(cl-labels ((org-roam--find-files
|
(let* ((org-roam-files (org-roam--find-files org-roam-directory))
|
||||||
(dir)
|
(file-items (mapcar (lambda (file)
|
||||||
(if (file-exists-p dir)
|
|
||||||
(let ((files (directory-files dir t "." t))
|
|
||||||
(dir-ignore-regexp (concat "\\(?:"
|
|
||||||
"\\."
|
|
||||||
"\\|\\.\\."
|
|
||||||
"\\)$"))
|
|
||||||
result)
|
|
||||||
(dolist (file files)
|
|
||||||
(cond
|
|
||||||
((file-directory-p file)
|
|
||||||
(when (not (string-match dir-ignore-regexp file))
|
|
||||||
(setq result (append (org-roam--find-files file) result))))
|
|
||||||
((and (file-readable-p file)
|
|
||||||
(string= (file-name-extension file) "org"))
|
|
||||||
(setq result (cons (file-truename file) result)))))
|
|
||||||
result)))
|
|
||||||
(org-roam--parse-content
|
|
||||||
(file)
|
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(insert-file-contents file)
|
(insert-file-contents file)
|
||||||
(with-current-buffer (current-buffer)
|
(org-roam--parse-content file))) org-roam-files)))
|
||||||
(org-element-map (org-element-parse-buffer) 'link
|
(dolist (items file-items)
|
||||||
(lambda (link)
|
(dolist (item items)
|
||||||
(let ((type (org-element-property :type link))
|
(org-roam--insert-item
|
||||||
(path (org-element-property :path link))
|
item
|
||||||
(start (org-element-property :begin link)))
|
:forward forward-links
|
||||||
(when (and (string= type "file")
|
:backward backward-links)))
|
||||||
(string= (file-name-extension path) "org"))
|
|
||||||
(goto-char start)
|
|
||||||
(let* ((element (org-element-at-point))
|
|
||||||
(content (or (org-element-property :raw-value element)
|
|
||||||
(buffer-substring
|
|
||||||
(or (org-element-property :content-begin element)
|
|
||||||
(org-element-property :begin element))
|
|
||||||
(or (org-element-property :content-end element)
|
|
||||||
(org-element-property :end element))))))
|
|
||||||
(list :from file
|
|
||||||
:to (file-truename (expand-file-name path org-roam-directory))
|
|
||||||
:content (string-trim content))))))))))
|
|
||||||
(org-roam--process-items
|
|
||||||
(items)
|
|
||||||
(mapcar
|
|
||||||
(lambda (item)
|
|
||||||
(pcase-let ((`(:from ,p-from :to ,p-to :content ,content) item))
|
|
||||||
;; Build forward-links
|
|
||||||
(let ((links (gethash p-from forward-links)))
|
|
||||||
(if links
|
|
||||||
(puthash p-from
|
|
||||||
(if (member p-to links)
|
|
||||||
links
|
|
||||||
(cons p-to links)) forward-links)
|
|
||||||
(puthash p-from (list p-to) forward-links)))
|
|
||||||
;; Build backward-links
|
|
||||||
(let ((contents-hash (gethash p-to backward-links)))
|
|
||||||
(if contents-hash
|
|
||||||
(if-let ((contents-list (gethash p-from contents-hash)))
|
|
||||||
(let ((updated (cons content contents-list)))
|
|
||||||
(puthash p-from updated contents-hash)
|
|
||||||
(puthash p-to contents-hash backward-links))
|
|
||||||
(progn
|
|
||||||
(puthash p-from (list content) contents-hash)
|
|
||||||
(puthash p-to contents-hash backward-links)))
|
|
||||||
(let ((contents-hash (make-hash-table :test #'equal)))
|
|
||||||
(puthash p-from (list content) contents-hash)
|
|
||||||
(puthash p-to contents-hash backward-links))))))
|
|
||||||
items))
|
|
||||||
(org-roam--extract-title
|
|
||||||
()
|
|
||||||
(org-element-map
|
|
||||||
(org-element-parse-buffer)
|
|
||||||
'keyword
|
|
||||||
(lambda (kw)
|
|
||||||
(when (string= (org-element-property :key kw) "TITLE")
|
|
||||||
(org-element-property :value kw)))
|
|
||||||
:first-match t)))
|
|
||||||
(let ((org-roam-files (org-roam--find-files org-roam-directory)))
|
|
||||||
(mapcar #'org-roam--process-items
|
|
||||||
(mapcar #'org-roam--parse-content org-roam-files))
|
|
||||||
(mapcar (lambda (file)
|
(mapcar (lambda (file)
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(insert-file-contents file)
|
(insert-file-contents file)
|
||||||
(when-let ((title (org-roam--extract-title)))
|
(when-let ((title (org-roam--extract-title)))
|
||||||
(puthash file title file-titles))))
|
(puthash file title file-titles))))
|
||||||
org-roam-files)))
|
org-roam-files))
|
||||||
(list
|
(list
|
||||||
:forward forward-links
|
:forward forward-links
|
||||||
:backward backward-links
|
:backward backward-links
|
||||||
@@ -411,56 +405,6 @@ Optionally pass it the title, for a smart file name."
|
|||||||
(setq org-roam-cache-initialized t)
|
(setq org-roam-cache-initialized t)
|
||||||
(message "Org-roam cache built!"))))
|
(message "Org-roam cache built!"))))
|
||||||
|
|
||||||
(defun org-roam--insert-item (item)
|
|
||||||
"Insert `ITEM' into org-roam caches.
|
|
||||||
|
|
||||||
`ITEM' is of the form: (:from from-path :to to-path :content preview-content)
|
|
||||||
|
|
||||||
Before calling this function, `org-roam-cache' should be already populated."
|
|
||||||
(pcase-let ((`(:from ,p-from :to ,p-to :content ,content) item))
|
|
||||||
;; Build forward-links
|
|
||||||
(let ((links (gethash p-from org-roam-forward-links-cache)))
|
|
||||||
(if links
|
|
||||||
(puthash p-from
|
|
||||||
(if (member p-to links)
|
|
||||||
links
|
|
||||||
(cons p-to links)) org-roam-forward-links-cache)
|
|
||||||
(puthash p-from (list p-to) org-roam-forward-links-cache)))
|
|
||||||
;; Build backward-links
|
|
||||||
(let ((contents-hash (gethash p-to org-roam-backward-links-cache)))
|
|
||||||
(if contents-hash
|
|
||||||
(if-let ((contents-list (gethash p-from contents-hash)))
|
|
||||||
(let ((updated (cons content contents-list)))
|
|
||||||
(puthash p-from updated contents-hash)
|
|
||||||
(puthash p-to contents-hash org-roam-backward-links-cache))
|
|
||||||
(progn
|
|
||||||
(puthash p-from (list content) contents-hash)
|
|
||||||
(puthash p-to contents-hash org-roam-backward-links-cache)))
|
|
||||||
(let ((contents-hash (make-hash-table :test #'equal)))
|
|
||||||
(puthash p-from (list content) contents-hash)
|
|
||||||
(puthash p-to contents-hash org-roam-backward-links-cache))))))
|
|
||||||
|
|
||||||
(defun org-roam--parse-content ()
|
|
||||||
"Parse the current buffer, and return a list of items for processing."
|
|
||||||
(org-element-map (org-element-parse-buffer) 'link
|
|
||||||
(lambda (link)
|
|
||||||
(let ((type (org-element-property :type link))
|
|
||||||
(path (org-element-property :path link))
|
|
||||||
(start (org-element-property :begin link)))
|
|
||||||
(when (and (string= type "file")
|
|
||||||
(string= (file-name-extension path) "org"))
|
|
||||||
(goto-char start)
|
|
||||||
(let* ((element (org-element-at-point))
|
|
||||||
(content (or (org-element-property :raw-value element)
|
|
||||||
(buffer-substring
|
|
||||||
(or (org-element-property :content-begin element)
|
|
||||||
(org-element-property :begin element))
|
|
||||||
(or (org-element-property :content-end element)
|
|
||||||
(org-element-property :end element))))))
|
|
||||||
(list :from (file-truename (buffer-file-name (current-buffer)))
|
|
||||||
:to (file-truename (expand-file-name path org-roam-directory))
|
|
||||||
:content (string-trim content))))))))
|
|
||||||
|
|
||||||
(defun org-roam--clear-cache ()
|
(defun org-roam--clear-cache ()
|
||||||
"Remove any related links to the file.
|
"Remove any related links to the file.
|
||||||
|
|
||||||
@@ -494,7 +438,10 @@ This is equivalent to removing the node from the graph."
|
|||||||
;; Insert new items
|
;; Insert new items
|
||||||
(let ((items (org-roam--parse-content)))
|
(let ((items (org-roam--parse-content)))
|
||||||
(dolist (item items)
|
(dolist (item items)
|
||||||
(org-roam--insert-item item)))
|
(org-roam--insert-item
|
||||||
|
item
|
||||||
|
:forward org-roam-forward-links-cache
|
||||||
|
:backward org-roam-backward-links-cache)))
|
||||||
;; Rerender buffer
|
;; Rerender buffer
|
||||||
(org-roam--maybe-update-buffer :redisplay t)))
|
(org-roam--maybe-update-buffer :redisplay t)))
|
||||||
|
|
||||||
@@ -516,16 +463,6 @@ This is equivalent to removing the node from the graph."
|
|||||||
(org-roam--new-file-named (format-time-string "%Y-%m-%d" time))))
|
(org-roam--new-file-named (format-time-string "%Y-%m-%d" time))))
|
||||||
|
|
||||||
;;; Org-roam buffer updates
|
;;; Org-roam buffer updates
|
||||||
(defun org-roam--extract-title ()
|
|
||||||
"Extract the title from `BUFFER'."
|
|
||||||
(org-element-map
|
|
||||||
(org-element-parse-buffer)
|
|
||||||
'keyword
|
|
||||||
(lambda (kw)
|
|
||||||
(when (string= (org-element-property :key kw) "TITLE")
|
|
||||||
(org-element-property :value kw)))
|
|
||||||
:first-match t))
|
|
||||||
|
|
||||||
(defun org-roam-update (file-path)
|
(defun org-roam-update (file-path)
|
||||||
"Show the backlinks for given org file for file at `FILE-PATH'."
|
"Show the backlinks for given org file for file at `FILE-PATH'."
|
||||||
(org-roam--ensure-cache-built)
|
(org-roam--ensure-cache-built)
|
||||||
@@ -678,7 +615,6 @@ This needs to be quick/infrequent, because this is run at
|
|||||||
(call-process org-roam-graphviz-executable nil 0 nil temp-dot "-Tsvg" "-o" temp-graph)
|
(call-process org-roam-graphviz-executable nil 0 nil temp-dot "-Tsvg" "-o" temp-graph)
|
||||||
(call-process org-roam-graph-viewer nil 0 nil temp-graph)))
|
(call-process org-roam-graph-viewer nil 0 nil temp-graph)))
|
||||||
|
|
||||||
|
|
||||||
(provide 'org-roam)
|
(provide 'org-roam)
|
||||||
|
|
||||||
;;; org-roam.el ends here
|
;;; org-roam.el ends here
|
||||||
|
Reference in New Issue
Block a user