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

View File

@ -1046,14 +1046,21 @@ citation key, for Org-ref cite links."
:where ,@conditions
:order-by (asc source)])))
(defun org-roam-id-get-file (id)
"Return the file if ID exists in the Org-roam database.
(defun org-roam-id-get-file (id &optional strict)
"Return the file if ID exists.
When STRICT is non-nil, only consider Org-roam's database.
Return nil otherwise."
(caar (org-roam-db-query [:select [file]
:from ids
:where (= id $s1)
:limit 1]
id)))
(or (caar (org-roam-db-query [:select [file]
:from ids
:where (= id $s1)
:limit 1]
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)
"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).
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."
(let ((file (or (org-roam-id-get-file id)
(unless strict (org-id-find-id-file id)))))
(let ((file (org-roam-id-get-file id strict)))
(when file
(let ((existing-buf (find-buffer-visiting file))
(res (org-id-find-id-in-file id file markerp)))
@ -1238,17 +1244,15 @@ file."
(org-roam--org-roam-file-p)))
(custom (or (and in-note org-roam-link-use-custom-faces)
(eq org-roam-link-use-custom-faces 'everywhere))))
(cond ((and custom
(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)
(cond ((and (org-roam--in-buffer-p)
(org-roam--backlink-to-current-p))
'org-roam-link-current)
((and custom
(org-roam-id-get-file id))
(org-roam-id-get-file id t))
'org-roam-link)
((and custom
(not (org-roam-id-get-file id)))
'org-roam-link-invalid)
(t
'org-link)))))

View File

@ -45,7 +45,7 @@
(pcase (benchmark-run 1 (org-roam-db-build-cache t))
(`(,time ,gcs ,time-in-gc)
(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"
(pcase (benchmark-run 1 (org-roam-db-build-cache))
(`(,time ,gcs ,time-in-gc)