(fix): fix id-face killing buffers (#1218)

org-roam-id-get-file no longer falls back onto the current buffer: It queries the org-roam database, and optionally org-id-locations, and declares that it does not exist if it isn't in either.
This commit is contained in:
Jethro Kuan
2020-10-27 00:25:39 +08:00
committed by GitHub
parent ac2044b84b
commit 0d235686f4
3 changed files with 55 additions and 52 deletions

View File

@ -167,53 +167,52 @@ ORIG-PATH is the path where the CONTENT originated."
"file"))) "file")))
(dolist (backlink bls) (dolist (backlink bls)
(pcase-let ((`(,file-from _ ,props) backlink)) (pcase-let ((`(,file-from _ ,props) backlink))
(insert (propertize (org-roam-buffer-expand-links (plist-get props :content) file-from) (insert (if-let ((content (plist-get props :content)))
'help-echo "mouse-1: visit backlinked note" (propertize (org-roam-buffer-expand-links content file-from)
'file-from file-from 'help-echo "mouse-1: visit backlinked note"
'file-from-point (plist-get props :point))) 'file-from file-from
(insert "\n\n")))))) 'file-from-point (plist-get props :point))
"")
"\n\n"))))))
(insert "\n\n* No ref backlinks!")))) (insert "\n\n* No ref backlinks!"))))
(defun org-roam-buffer--insert-backlinks () (defun org-roam-buffer--insert-backlinks ()
"Insert the org-roam-buffer backlinks string for the current buffer." "Insert the org-roam-buffer backlinks string for the current buffer."
(if-let* ((file-path (buffer-file-name org-roam-buffer--current)) (let (props file-from)
(titles (with-current-buffer org-roam-buffer--current (if-let* ((file-path (buffer-file-name org-roam-buffer--current))
(org-roam--extract-titles))) (titles (with-current-buffer org-roam-buffer--current
(backlinks (org-roam--get-backlinks (push file-path titles))) (org-roam--extract-titles)))
(grouped-backlinks (--group-by (nth 0 it) backlinks))) (backlinks (org-roam--get-backlinks (push file-path titles)))
(progn (grouped-backlinks (--group-by (nth 0 it) backlinks)))
(insert (let ((l (length backlinks))) (progn
(format "\n\n* %d %s\n" (insert (let ((l (length backlinks)))
l (org-roam-buffer--pluralize "Backlink" l)))) (format "\n\n* %d %s\n"
(dolist (group grouped-backlinks) l (org-roam-buffer--pluralize "Backlink" l))))
(let ((file-from (car group)) (dolist (group grouped-backlinks)
(bls (mapcar (lambda (row) (setq file-from (car group))
(nth 2 row)) (cdr group)))) (setq props (mapcar (lambda (row) (nth 2 row)) (cdr group)))
(setq props (seq-sort-by (lambda (p) (plist-get p :point)) #'< props))
(insert (format "** %s\n" (insert (format "** %s\n"
(org-roam-format-link file-from (org-roam-format-link file-from
(org-roam--get-title-or-slug file-from) (org-roam--get-title-or-slug file-from)
"file"))) "file")))
;; Sort backlinks according to time of occurrence in buffer (dolist (prop props)
(setq bls (seq-sort-by (lambda (bl)
(plist-get bl :point))
#'<
bls))
(dolist (props bls)
(insert "*** " (insert "*** "
(if-let ((outline (plist-get props :outline))) (if-let ((outline (plist-get prop :outline)))
(-> outline (-> outline
(string-join " > ") (string-join " > ")
(org-roam-buffer-expand-links file-from)) (org-roam-buffer-expand-links file-from))
"Top") "Top")
"\n" "\n"
(propertize (if-let ((content (plist-get prop :content)))
(s-trim (s-replace "\n" " " (propertize
(org-roam-buffer-expand-links (plist-get props :content) file-from))) (s-trim (s-replace "\n" " " (org-roam-buffer-expand-links content file-from)))
'help-echo "mouse-1: visit backlinked note" 'help-echo "mouse-1: visit backlinked note"
'file-from file-from 'file-from file-from
'file-from-point (plist-get props :point)) 'file-from-point (plist-get prop :point))
"\n\n"))))) "")
(insert "\n\n* No backlinks!"))) "\n\n"))))
(insert "\n\n* No backlinks!"))))
(defun org-roam-buffer-update () (defun org-roam-buffer-update ()
"Update the `org-roam-buffer'." "Update the `org-roam-buffer'."

View File

@ -1046,14 +1046,21 @@ citation key, for Org-ref cite links."
:where ,@conditions :where ,@conditions
:order-by (asc source)]))) :order-by (asc source)])))
(defun org-roam-id-get-file (id) (defun org-roam-id-get-file (id &optional strict)
"Return the file if ID exists in the Org-roam database. "Return the file if ID exists.
When STRICT is non-nil, only consider Org-roam's database.
Return nil otherwise." Return nil otherwise."
(caar (org-roam-db-query [:select [file] (or (caar (org-roam-db-query [:select [file]
:from ids :from ids
:where (= id $s1) :where (= id $s1)
:limit 1] :limit 1]
id))) id))
(and (not strict)
(progn
(unless org-id-locations (org-id-locations-load))
(or (and org-id-locations
(hash-table-p org-id-locations)
(gethash id org-id-locations)))))))
(defun org-roam-id-find (id &optional markerp strict keep-buffer-p) (defun org-roam-id-find (id &optional markerp strict keep-buffer-p)
"Return the location of the entry with the id ID. "Return the location of the entry with the id ID.
@ -1061,8 +1068,7 @@ When MARKERP is non-nil, return a marker pointing to the headline.
Otherwise, return a cons formatted as \(file . pos). Otherwise, return a cons formatted as \(file . pos).
When STRICT is non-nil, only consider Org-roams database. When STRICT is non-nil, only consider Org-roams database.
When KEEP-BUFFER-P is non-nil, keep the buffers navigated by Org-roam open." When KEEP-BUFFER-P is non-nil, keep the buffers navigated by Org-roam open."
(let ((file (or (org-roam-id-get-file id) (let ((file (org-roam-id-get-file id strict)))
(unless strict (org-id-find-id-file id)))))
(when file (when file
(let ((existing-buf (find-buffer-visiting file)) (let ((existing-buf (find-buffer-visiting file))
(res (org-id-find-id-in-file id file markerp))) (res (org-id-find-id-in-file id file markerp)))
@ -1238,17 +1244,15 @@ file."
(org-roam--org-roam-file-p))) (org-roam--org-roam-file-p)))
(custom (or (and in-note org-roam-link-use-custom-faces) (custom (or (and in-note org-roam-link-use-custom-faces)
(eq org-roam-link-use-custom-faces 'everywhere)))) (eq org-roam-link-use-custom-faces 'everywhere))))
(cond ((and custom (cond ((and (org-roam--in-buffer-p)
(not (org-roam-id-get-file id))
(not (and (eq org-roam-link-use-custom-faces 'everywhere)
(org-id-find id))))
'org-roam-link-invalid)
((and (org-roam--in-buffer-p)
(org-roam--backlink-to-current-p)) (org-roam--backlink-to-current-p))
'org-roam-link-current) 'org-roam-link-current)
((and custom ((and custom
(org-roam-id-get-file id)) (org-roam-id-get-file id t))
'org-roam-link) 'org-roam-link)
((and custom
(not (org-roam-id-get-file id)))
'org-roam-link-invalid)
(t (t
'org-link))))) 'org-link)))))

View File

@ -45,7 +45,7 @@
(pcase (benchmark-run 1 (org-roam-db-build-cache t)) (pcase (benchmark-run 1 (org-roam-db-build-cache t))
(`(,time ,gcs ,time-in-gc) (`(,time ,gcs ,time-in-gc)
(message "Elapsed time: %fs (%fs in %d GCs)" time time-in-gc gcs) (message "Elapsed time: %fs (%fs in %d GCs)" time time-in-gc gcs)
(expect time :to-be-less-than 90)))) (expect time :to-be-less-than 100))))
(it "builds quickly without change" (it "builds quickly without change"
(pcase (benchmark-run 1 (org-roam-db-build-cache)) (pcase (benchmark-run 1 (org-roam-db-build-cache))
(`(,time ,gcs ,time-in-gc) (`(,time ,gcs ,time-in-gc)