From 9b173af80d53b36acb382966a7dd9525cd97fbc7 Mon Sep 17 00:00:00 2001 From: Chris Barrett Date: Mon, 29 Aug 2022 20:28:49 +1200 Subject: [PATCH] Add lib that advises backlinks buffer to build previews lazily --- Readme.org | 4 ++ lisp/org-roam-lazy-previews.el | 76 ++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 lisp/org-roam-lazy-previews.el diff --git a/Readme.org b/Readme.org index 66bcac6..2840872 100644 --- a/Readme.org +++ b/Readme.org @@ -62,6 +62,10 @@ and provides integrations with org-capture. ** SPIKE [[file:lisp/org-roam-refill-previews.el][org-roam-refill-previews]] /(spike)/ Refill previews in the backlinks buffer so they fit the window. +** SPIKE [[file:lisp/org-roam-lazy-previews.el][org-roam-lazy-previews]] /(spike)/ +Compute previews lazily for much better performance in buffers with many +backlinks or reflinks. + * Installation Most packages should be manually installable via =package.el=, assuming you have [[https://melpa.org/#/getting-started][MELPA]] set up. But honestly, you're better off just cloning this repo and putting diff --git a/lisp/org-roam-lazy-previews.el b/lisp/org-roam-lazy-previews.el new file mode 100644 index 0000000..7ecfd4d --- /dev/null +++ b/lisp/org-roam-lazy-previews.el @@ -0,0 +1,76 @@ +;;; org-roam-lazy-previews.el --- Make previews in org-roam buffer lazy for better performance -*- lexical-binding: t; -*- + +;; Copyright (C) 2022 Chris Barrett + +;; Author: Chris Barrett + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; Changes previews in the org-roam buffer to be lazily computed, improving +;; responsiveness in buffers with many backlinks and reflinks. +;; +;; Example configuration: +;; +;; (use-package org-roam-lazy-previews +;; :after org-roam +;; :demand t) + +;;; Code: + +(require 'cl-lib) +(require 'magit-section) +(require 'org) +(require 'org-roam) +(require 'subr-x) + +(autoload 'org-roam-review-insert-preview "org-roam-review") + +(defgroup org-roam-lazy-previews nil + "Change org-roam node previews to be lazy for performance." + :group 'productivity + :prefix "org-roam-lazy-previews-") + +(defcustom org-roam-lazy-previews-title-formatter #'org-roam-lazy-previews-custom-title-formatter + "A function to format a node's title for the backlinks buffer. + +It is passed the same arguments as +`org-roam-node-insert-section', and should return a string." + :group 'org-roam-lazy-previews + :type 'function) + +(cl-defun org-roam-lazy-previews-custom-title-formatter (&key source-node properties &allow-other-keys) + (let* ((outline (when-let* ((outline (plist-get properties :outline))) + (mapconcat #'org-link-display-format outline " > "))) + (title (org-roam-node-title source-node))) + (concat (propertize title 'font-lock-face 'org-roam-title) + (when (and outline (not (equal title outline))) + (format " > %s" (propertize outline 'font-lock-face 'org-roam-olp)))))) + +(define-advice org-roam-node-insert-section (:override (&rest args) lazy-previews) + (cl-destructuring-bind (&key source-node point &allow-other-keys) args + (magit-insert-section section (org-roam-node-section (org-roam-node-id source-node) t) + (magit-insert-heading (apply org-roam-lazy-previews-title-formatter args)) + (oset section node source-node) + ;; KLUDGE: Mofified macro-expansion of `magit-insert-section-body' that + ;; avoids unsetting the parent section's keymap. + (oset section washer + (lambda () + (org-roam-review-insert-preview source-node :point point) + (magit-section-maybe-remove-visibility-indicator section)))))) + +(provide 'org-roam-lazy-previews) + +;;; org-roam-lazy-previews.el ends here