summaryrefslogtreecommitdiffstatshomepage
path: root/nroam-unlinked.el
blob: b989de367ffe4a7a9622c632ee41b3e43620aad8 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
;;; nroam-unlinked.el --- Unlinked references section for nroam.el  -*- lexical-binding: t; -*-

;; Copyright (C) 2021  Nicolas Petton

;; Author: Nicolas Petton <nico@petton.fr>

;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This library provides an unlinked references nroam section for org-roam
;; buffers.

;;; Code:
(require 'org-roam)
(require 'nroam-utils)
(declare-function nroam-register-section "nroam.el")
(declare-function nroam-update "nroam.el")

(defvar-local nroam-unlinked-show-references nil
  "When non-nil, show unlinked references for the current buffer.")

(defun nroam-unlinked-register-section ()
  "Register `nroam-unlinked-section' as a section in nroam."
  (nroam-register-section #'nroam-unlinked-section))

(defun nroam-unlinked-section ()
  "Insert `org-roam' unlinked references for the current buffer."
  (nroam-unlinked--insert-heading)
  (if nroam-unlinked-show-references
      (nroam-unlinked--insert-references)
    (nroam-unlinked--insert-toggle-button)))

(defun nroam-unlinked--insert-heading ()
  "Insert the heading for unlinked references."
  (insert "* Unlinked references                      :noexport:\n"))

(defun nroam-unlinked--insert-references ()
  "Insert unlinked references for the current buffer."
  (let (content)
    (save-window-excursion
      (org-roam-unlinked-references)
      (let ((buf (current-buffer)))
        (goto-char (point-min))
        (search-forward "* Unlinked References\n" nil t)
        (setq content (buffer-substring (point) (point-max)))
        (kill-buffer buf)))
    (insert content)))

(defun nroam-unlinked--insert-toggle-button ()
  "Insert a button to show unlinked references."
  (let ((beg (point)))
    (insert "[Show unlinked references]")
    (make-text-button beg (point) 'action #'nroam-unlinked--show-references)
    (insert "\n")))

(defun nroam-unlinked--show-references (&rest _)
  "Search for and show unlinked references."
  (let ((inhibit-read-only t)
        (pos (point-at-bol)))
    (setq nroam-unlinked-show-references t)
    (nroam-update)
    (goto-char pos)
    (org-back-to-heading)))

(provide 'nroam-unlinked)
;;; nroam-unlinked.el ends here