This page looks best with JavaScript enabled

Configure org-roam v2 to update database only when idle

 ·  ☕ 2 min read  ·  ✍️ Charl P. Botha

org-roam v2, which was recently released, removed the update database on idle functionality (which I coincidentally contributed) as part of its simplification.

I agree with this removal, as I think it makes the most sense to keep the org-roam core as simple as possible.

That being said, being able to have the org-roam database update only when Emacs is idle can be helpful for some folks, like me, who C-x C-s quite often and notice a few hundred millisecond blocking delay on files of a thousand or more lines.

Fortunately, Emacs makes this sort of behaviourable modification straight-forward.

In this post, in fact right below these words, I present to you some code you can add to your init.el configuration to enable database-update-only-on-idle for org-roam v2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
;; https://orgmode-exocortex.com/2021/07/22/configure-org-roam-v2-to-update-database-only-when-idle/
;; use this code entirely at your own risk
(with-eval-after-load "org-roam"
  ;; queue for files that will be updated in org-roam-db when emacs is idle
  (setq org-roam-db-update-queue (list))
  ;; save the original update function;
  (setq orig-update-file (symbol-function 'org-roam-db-update-file))
  ;; then redefine the db update function to add the filename to a queue
  (defun org-roam-db-update-file (&optional file-path)
    ;; do same logic as original to determine current file-path if not passed as arg
    (setq file-path (or file-path (buffer-file-name (buffer-base-buffer))))
    (message "org-roam: scheduling update of %s" file-path)
    (if (not (memq file-path org-roam-db-update-queue))
        (push file-path org-roam-db-update-queue)))

  ;; this function will be called when emacs is idle for a few seconds
  (defun org-roam-db-idle-update-files ()
    ;; go through queued filenames one-by-one and update db
    ;; if we're not idle anymore, stop. will get rest of queue next idle.
    (while (and org-roam-db-update-queue (current-idle-time))
      ;; apply takes function var and list
      (apply orig-update-file (list (pop org-roam-db-update-queue)))))

  ;; we'll only start updating db if we've been idle for this many seconds
  (run-with-idle-timer 5 t #'org-roam-db-idle-update-files))
Share on

Charl P. Botha
WRITTEN BY
Charl P. Botha
Engineer