(feat): Allow one file to have multiple roam_key statements (#1215)

This commit is contained in:
Kisaragi Hiu
2020-11-07 16:33:31 +09:00
committed by GitHub
parent 440461a90b
commit c6797cbd75
8 changed files with 120 additions and 62 deletions

View File

@ -50,7 +50,7 @@
(declare-function org-roam--org-roam-file-p "org-roam")
(declare-function org-roam--extract-titles "org-roam")
(declare-function org-roam--extract-ref "org-roam")
(declare-function org-roam--extract-refs "org-roam")
(declare-function org-roam--extract-tags "org-roam")
(declare-function org-roam--extract-ids "org-roam")
(declare-function org-roam--extract-links "org-roam")
@ -274,34 +274,35 @@ Returns the number of rows inserted."
rows)
(length rows)))
(defun org-roam-db--insert-ref (&optional update-p)
"Update the ref of the current buffer into the cache.
(defun org-roam-db--insert-refs (&optional update-p)
"Update the refs of the current buffer into the cache.
If UPDATE-P is non-nil, first remove the ref for the file in the database."
(let ((file (or org-roam-file-name (buffer-file-name))))
(let ((file (or org-roam-file-name (buffer-file-name)))
(count 0))
(when update-p
(org-roam-db-query [:delete :from refs
:where (= file $s1)]
file))
(if-let ((ref (org-roam--extract-ref)))
(let ((key (cdr ref))
(type (car ref)))
(condition-case nil
(progn
(org-roam-db-query
[:insert :into refs :values $v1]
(list (vector key file type)))
1)
(error
(lwarn '(org-roam) :error
(format "Duplicate ref %s in:\n\nA: %s\nB: %s\n\nskipping..."
key
file
(caar (org-roam-db-query
[:select file :from refs
:where (= ref $v1)]
(vector key)))))
0)))
0)))
(when-let ((refs (org-roam--extract-refs)))
(dolist (ref refs)
(let ((key (cdr ref))
(type (car ref)))
(condition-case nil
(progn
(org-roam-db-query
[:insert :into refs :values $v1]
(list (vector key file type)))
(cl-incf count))
(error
(lwarn '(org-roam) :error
(format "Duplicate ref %s in:\n\nA: %s\nB: %s\n\nskipping..."
key
file
(caar (org-roam-db-query
[:select file :from refs
:where (= ref $v1)]
(vector key))))))))))
count))
(defun org-roam-db--insert-links (&optional update-p)
"Update the file links of the current buffer in the cache.
@ -473,7 +474,7 @@ If the file exists, update the cache with information."
(org-roam-db--insert-meta 'update)
(org-roam-db--insert-tags 'update)
(org-roam-db--insert-titles 'update)
(org-roam-db--insert-ref 'update)
(org-roam-db--insert-refs 'update)
(when org-roam-enable-headline-linking
(org-roam-db--insert-ids 'update))
(org-roam-db--insert-links 'update)))))
@ -538,7 +539,7 @@ If FORCE, force a rebuild of the cache from scratch."
(setq link-count (+ link-count (org-roam-db--insert-links)))
(setq tag-count (+ tag-count (org-roam-db--insert-tags)))
(setq title-count (+ title-count (org-roam-db--insert-titles)))
(setq ref-count (+ ref-count (org-roam-db--insert-ref))))
(setq ref-count (+ ref-count (org-roam-db--insert-refs))))
(file-error
(setq org-roam-files (remove file org-roam-files))
(org-roam-db--clear-file file)