mirror of
https://github.com/chrisbarrett/nursery
synced 2025-09-20 16:10:56 -05:00
Add lib that advises backlinks buffer to build previews lazily
This commit is contained in:
@@ -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
|
||||
|
76
lisp/org-roam-lazy-previews.el
Normal file
76
lisp/org-roam-lazy-previews.el
Normal file
@@ -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 <chris@walrus.cool>
|
||||
|
||||
;; 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
;;; 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
|
Reference in New Issue
Block a user