From 8e691e810f8aa5ee5bc6dc5198e1b36cb68a1b04 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Thu, 10 Feb 2022 19:41:18 +0100 Subject: [PATCH] refactor!(org): backlinks buffer management BREAKING CHANGE: The +org-roam-open-buffer-on-find-file variable was renamed to +org-roam-auto-backlinks-buffer *and* is now disabled by default. When this is non-nil, it will open the *org-roam* backlinks side window when roam files are visible, and close it when they aren't. This change also makes this behavior a little more robust, but is understandably not everybody's (read: most people's) cup of tea, so it is now opt-in. --- modules/lang/org/autoload/contrib-roam2.el | 21 ++++++++++++++++++ modules/lang/org/contrib/roam2.el | 25 +++++++++++----------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/modules/lang/org/autoload/contrib-roam2.el b/modules/lang/org/autoload/contrib-roam2.el index 1618931de..34d83071d 100644 --- a/modules/lang/org/autoload/contrib-roam2.el +++ b/modules/lang/org/autoload/contrib-roam2.el @@ -66,6 +66,27 @@ If there's none, return an empty string." (directory-file-name dir))) +;; +;;; Hooks + +;;;###autoload +(defun +org-roam-manage-backlinks-buffer-h () + "Open or close roam backlinks buffer depending on visible org-roam buffers. + +Intended to be added to `doom-switch-buffer-hook' in `org-roam-find-file-hook'. +Controlled by `+org-roam-open-buffer-on-find-file'." + (when (and +org-roam-auto-backlinks-buffer + (not org-roam-capture--node) ; not for roam capture buffers + (not org-capture-mode) ; not for capture buffers + (not (bound-and-true-p +popup-buffer-mode))) + (let ((visible-p (eq 'visible (org-roam-buffer--visibility)))) + (if (cl-some #'org-roam-buffer-p (doom-visible-buffers)) + (unless visible-p + (org-roam-buffer-toggle)) + (when visible-p + (org-roam-buffer-toggle)) + (unless (doom-buffers-in-mode 'org-mode) + (remove-hook 'doom-switch-buffer-hook #'+org-roam-manage-backlinks-buffer-h)))))) ;; diff --git a/modules/lang/org/contrib/roam2.el b/modules/lang/org/contrib/roam2.el index dfb6d9c5a..87d193114 100644 --- a/modules/lang/org/contrib/roam2.el +++ b/modules/lang/org/contrib/roam2.el @@ -1,8 +1,12 @@ ;;; lang/org/contrib/roam2.el -*- lexical-binding: t; -*- ;;;###if (featurep! +roam2) -(defvar +org-roam-open-buffer-on-find-file t - "If non-nil, open the org-roam buffer when opening an org roam file.") +(defvar +org-roam-auto-backlinks-buffer nil + "If non-nil, open and close the org-roam backlinks buffer automatically. + +This ensures the backlinks buffer is always present so long as an org roam file +is visible. Once they are all closed or killed, the backlinks buffer will be +closed.") (defvar +org-roam-link-to-org-use-id 'create-if-interactive "`org-roam-directory' local value for `org-id-link-to-org-use-id'. @@ -86,18 +90,13 @@ In case of failure, fail gracefully." (setq-hook! 'org-roam-find-file-hook org-id-link-to-org-use-id +org-roam-link-to-org-use-id) - ;; Normally, the org-roam buffer doesn't open until you explicitly call - ;; `org-roam'. If `+org-roam-open-buffer-on-find-file' is non-nil, the - ;; org-roam buffer will be opened for you whenever you visit a file in - ;; `org-roam-directory'. + ;; Normally, the org-roam buffer won't open until `org-roam-buffer-toggle' is + ;; explicitly called. If `+org-roam-open-buffer-on-find-file' is non-nil, the + ;; org-roam buffer will automatically open whenever a file in + ;; `org-roam-directory' is visited and closed when no org-roam buffers remain. (add-hook! 'org-roam-find-file-hook :append - (defun +org-roam-open-with-buffer-maybe-h () - (and +org-roam-open-buffer-on-find-file - (not org-roam-capture--node) ; not for roam capture buffers - (not org-capture-mode) ; not for capture buffers - (not (bound-and-true-p +popup-buffer-mode)) - (not (eq 'visible (org-roam-buffer--visibility))) - (org-roam-buffer-toggle)))) + (defun +org-roam-enable-auto-backlinks-buffer-h () + (add-hook 'doom-switch-buffer-hook #'+org-roam-manage-backlinks-buffer-h))) (set-popup-rules! `((,(regexp-quote org-roam-buffer) ; persistent org-roam buffer