From f9228ce31969cab9ca46ad4890e82e2ea7de3738 Mon Sep 17 00:00:00 2001 From: "Samuel W. Flint" Date: Sun, 4 Dec 2022 21:55:47 -0600 Subject: [PATCH] (feat): add support for filtering backlinks. (#2247) * Add support for filtering backlinks. See #1043. * Add documentation for backlinks filter --- doc/org-roam.org | 13 +++++++++++++ org-roam-mode.el | 18 ++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/doc/org-roam.org b/doc/org-roam.org index 2d1618e..7575b18 100644 --- a/doc/org-roam.org +++ b/doc/org-roam.org @@ -644,6 +644,19 @@ rendering reference links), set ~org-roam-mode-sections~ as follows: org-roam-reflinks-section)) #+end_src +The backlinks section ~org-roam-backlinks-section~ also supports a +predicate to filter backlinks, ~:show-backlink-p~. This can be used +as follows: + +#+begin_src emacs-lisp + (defun my-org-roam-show-backlink-p (backlink) + (not (member "daily" (org-roam-node-tags (org-roam-backlink-source-node backlink))))) + + (setq org-roam-mode-sections + '((org-roam-backlinks-section :unique t :show-backlink-p my/org-roam-show-backlink-p) + org-roam-reflinks-section)) +#+end_src + ** Configuring the Org-roam buffer display Org-roam does not control how the pop-up buffer is displayed: this is left to diff --git a/org-roam-mode.el b/org-roam-mode.el index d23bc79..7ca7bba 100644 --- a/org-roam-mode.el +++ b/org-roam-mode.el @@ -514,19 +514,25 @@ Sorts by title." (string< (org-roam-node-title (org-roam-backlink-source-node a)) (org-roam-node-title (org-roam-backlink-source-node b)))) -(cl-defun org-roam-backlinks-section (node &key (unique nil)) +(cl-defun org-roam-backlinks-section (node &key (unique nil) (show-backlink-p nil)) "The backlinks section for NODE. When UNIQUE is nil, show all positions where references are found. -When UNIQUE is t, limit to unique sources." +When UNIQUE is t, limit to unique sources. + +When SHOW-BACKLINK-P is not null, only show backlinks for which +this predicate is not nil." (when-let ((backlinks (seq-sort #'org-roam-backlinks-sort (org-roam-backlinks-get node :unique unique)))) (magit-insert-section (org-roam-backlinks) (magit-insert-heading "Backlinks:") (dolist (backlink backlinks) - (org-roam-node-insert-section - :source-node (org-roam-backlink-source-node backlink) - :point (org-roam-backlink-point backlink) - :properties (org-roam-backlink-properties backlink))) + (when (or (null show-backlink-p) + (and (not (null show-backlink-p)) + (funcall show-backlink-p backlink))) + (org-roam-node-insert-section + :source-node (org-roam-backlink-source-node backlink) + :point (org-roam-backlink-point backlink) + :properties (org-roam-backlink-properties backlink)))) (insert ?\n)))) ;;;; Reflinks