From 93d8c477fe1800a26a41d60e6fbec56e29ab9894 Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Wed, 30 Sep 2020 15:27:25 +0800 Subject: [PATCH] (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. --- org-roam-db.el | 27 +++++++++++++++------------ org-roam.el | 32 ++++++++++++-------------------- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/org-roam-db.el b/org-roam-db.el index e82ea41..a428e02 100644 --- a/org-roam-db.el +++ b/org-roam-db.el @@ -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." (setq file-path (or file-path (buffer-file-name (buffer-base-buffer)))) - (cond ((not (file-exists-p file-path)) - (org-roam-db--clear-file file-path)) - ((org-roam--org-roam-file-p file-path) - (org-roam--with-temp-buffer file-path - (emacsql-with-transaction (org-roam-db) - (org-roam-db--update-meta) - (org-roam-db--update-tags) - (org-roam-db--update-titles) - (org-roam-db--update-refs) - (when org-roam-enable-headline-linking - (org-roam-db--update-headlines)) - (org-roam-db--update-links)))))) + (if (not (file-exists-p file-path)) + (org-roam-db--clear-file file-path) + ;; save the file before performing a database update + (when-let ((buf (find-buffer-visiting file-path))) + (with-current-buffer buf + (save-buffer))) + (org-roam--with-temp-buffer file-path + (emacsql-with-transaction (org-roam-db) + (org-roam-db--update-meta) + (org-roam-db--update-tags) + (org-roam-db--update-titles) + (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) "Build the cache for `org-roam-directory'. diff --git a/org-roam.el b/org-roam.el index e6a6318..a53080c 100644 --- a/org-roam.el +++ b/org-roam.el @@ -370,8 +370,9 @@ Like `file-name-extension', but does not strip version number." (defun org-roam--org-roam-file-p (&optional file) "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-let ((path (or file - (buffer-file-name)))) + (when-let ((path (or file + org-roam-file-name + (buffer-file-name)))) (save-match-data (and (org-roam--org-file-p path) @@ -1060,22 +1061,6 @@ citation key, for Org-ref cite links." :where ,@conditions :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) "Return the file if ID exists in the Org-roam database. 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) (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 (define-minor-mode org-roam-mode "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))) (advice-add 'rename-file :after #'org-roam--rename-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 org-roam-enable-headline-linking - (org-link-set-parameters "file" :face 'org-roam--file-link-face :store #'org-roam-store-link)) + (org-link-set-parameters "file" :face 'org-roam--file-link-face) (org-link-set-parameters "id" :face 'org-roam--id-link-face)) (org-roam-db-build-cache)) (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)) (advice-remove 'rename-file #'org-roam--rename-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) (dolist (face '("file" "id")) (org-link-set-parameters face :face 'org-link)))