(feat): remove file store-link function override (#1155)

Previously we had overwritten the Org's store link function for files,
to create IDs if the point was under the headline. This has several
issues:

1. It becomes impossible to store a link to the file using `org-store-link`
2. This override is global across Org, although we had a guard in
`org-roam-store-link`
3. IDs are created during an org-capture, because org-capture calls `org-store-link`

This is unnecessary. Org provides `org-id-store-link` instead for this
purpose. Instead, we advice `org-id-new`, to queue the file for a DB
update if an ID was created.
This commit is contained in:
Jethro Kuan
2020-09-30 15:27:25 +08:00
committed by GitHub
parent 30d52e5508
commit 93d8c477fe
2 changed files with 27 additions and 32 deletions

View File

@ -456,18 +456,21 @@ If the file does not exist anymore, remove it from the cache.
If the file exists, update the cache with information." If the file exists, update the cache with information."
(setq file-path (or file-path (setq file-path (or file-path
(buffer-file-name (buffer-base-buffer)))) (buffer-file-name (buffer-base-buffer))))
(cond ((not (file-exists-p file-path)) (if (not (file-exists-p file-path))
(org-roam-db--clear-file file-path)) (org-roam-db--clear-file file-path)
((org-roam--org-roam-file-p file-path) ;; save the file before performing a database update
(org-roam--with-temp-buffer file-path (when-let ((buf (find-buffer-visiting file-path)))
(emacsql-with-transaction (org-roam-db) (with-current-buffer buf
(org-roam-db--update-meta) (save-buffer)))
(org-roam-db--update-tags) (org-roam--with-temp-buffer file-path
(org-roam-db--update-titles) (emacsql-with-transaction (org-roam-db)
(org-roam-db--update-refs) (org-roam-db--update-meta)
(when org-roam-enable-headline-linking (org-roam-db--update-tags)
(org-roam-db--update-headlines)) (org-roam-db--update-titles)
(org-roam-db--update-links)))))) (org-roam-db--update-refs)
(when org-roam-enable-headline-linking
(org-roam-db--update-headlines))
(org-roam-db--update-links)))))
(defun org-roam-db-build-cache (&optional force) (defun org-roam-db-build-cache (&optional force)
"Build the cache for `org-roam-directory'. "Build the cache for `org-roam-directory'.

View File

@ -370,8 +370,9 @@ Like `file-name-extension', but does not strip version number."
(defun org-roam--org-roam-file-p (&optional file) (defun org-roam--org-roam-file-p (&optional file)
"Return t if FILE is part of Org-roam system, nil otherwise. "Return t if FILE is part of Org-roam system, nil otherwise.
If FILE is not specified, use the current buffer's file-path." If FILE is not specified, use the current buffer's file-path."
(if-let ((path (or file (when-let ((path (or file
(buffer-file-name)))) org-roam-file-name
(buffer-file-name))))
(save-match-data (save-match-data
(and (and
(org-roam--org-file-p path) (org-roam--org-file-p path)
@ -1060,22 +1061,6 @@ citation key, for Org-ref cite links."
:where ,@conditions :where ,@conditions
:order-by (asc from)]))) :order-by (asc from)])))
(defun org-roam-store-link ()
"Store a link to an Org-roam file or heading."
(when (and (bound-and-true-p org-roam-mode)
(org-roam--org-roam-file-p))
(if (org-before-first-heading-p)
(when-let ((titles (org-roam--extract-titles)))
(org-roam-link-store-props
:type "file"
:link (format "file:%s" (abbreviate-file-name buffer-file-name))
:description (car titles)))
(let ((id (org-id-get)))
(org-id-store-link)
;; If :ID: was created, update the cache
(unless id
(org-roam-db--update-headlines))))))
(defun org-roam-id-get-file (id) (defun org-roam-id-get-file (id)
"Return the file if ID exists in the Org-roam database. "Return the file if ID exists in the Org-roam database.
Return nil otherwise." Return nil otherwise."
@ -1477,6 +1462,12 @@ When NEW-FILE-OR-DIR is a directory, we use it to compute the new file path."
(when (org-roam--org-roam-file-p new-file) (when (org-roam--org-roam-file-p new-file)
(org-roam-db--update-file new-path)))))) (org-roam-db--update-file new-path))))))
(defun org-roam--id-new-advice (&rest _args)
"Update the database if a new Org ID is created."
(when (and org-roam-enable-headline-linking
(org-roam--org-roam-file-p))
(add-to-list 'org-roam--file-update-queue (buffer-file-name))))
;;;###autoload ;;;###autoload
(define-minor-mode org-roam-mode (define-minor-mode org-roam-mode
"Minor mode for Org-roam. "Minor mode for Org-roam.
@ -1514,9 +1505,9 @@ M-x info for more information at Org-roam > Installation > Post-Installation Tas
(setq org-roam--file-update-timer (run-with-idle-timer org-roam-update-db-idle-seconds t #'org-roam--process-update-queue))) (setq org-roam--file-update-timer (run-with-idle-timer org-roam-update-db-idle-seconds t #'org-roam--process-update-queue)))
(advice-add 'rename-file :after #'org-roam--rename-file-advice) (advice-add 'rename-file :after #'org-roam--rename-file-advice)
(advice-add 'delete-file :before #'org-roam--delete-file-advice) (advice-add 'delete-file :before #'org-roam--delete-file-advice)
(advice-add 'org-id-new :after #'org-roam--id-new-advice)
(when (fboundp 'org-link-set-parameters) (when (fboundp 'org-link-set-parameters)
(when org-roam-enable-headline-linking (org-link-set-parameters "file" :face 'org-roam--file-link-face)
(org-link-set-parameters "file" :face 'org-roam--file-link-face :store #'org-roam-store-link))
(org-link-set-parameters "id" :face 'org-roam--id-link-face)) (org-link-set-parameters "id" :face 'org-roam--id-link-face))
(org-roam-db-build-cache)) (org-roam-db-build-cache))
(t (t
@ -1528,6 +1519,7 @@ M-x info for more information at Org-roam > Installation > Post-Installation Tas
(cancel-timer org-roam--file-update-timer)) (cancel-timer org-roam--file-update-timer))
(advice-remove 'rename-file #'org-roam--rename-file-advice) (advice-remove 'rename-file #'org-roam--rename-file-advice)
(advice-remove 'delete-file #'org-roam--delete-file-advice) (advice-remove 'delete-file #'org-roam--delete-file-advice)
(advice-remove 'org-id-new #'org-roam--id-new-advice)
(when (fboundp 'org-link-set-parameters) (when (fboundp 'org-link-set-parameters)
(dolist (face '("file" "id")) (dolist (face '("file" "id"))
(org-link-set-parameters face :face 'org-link))) (org-link-set-parameters face :face 'org-link)))