aboutsummaryrefslogtreecommitdiffstats
path: root/oni-org-roam.el
blob: 289da059f422bf944eeb032c64860258581c1c22 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
;;; oni-org-roam.el --- Configuration for org-roam   -*- lexical-binding: t; -*-

;; Copyright (C) 2021  Tom Willemse

;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords: local
;; Version: 2024.0212.234817
;; Package-Requires: (oni-org org-roam)

;; 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:

;; Configuration specific to ‘org-roam’. Add the following to your init file if
;; you want to use this configuration:
;;
;;     (with-eval-after-load 'org-roam (require 'oni-org-roam))

;;; Code:

(require 'org-roam)
(require 'seq)

(setq org-roam-completion-everywhere t)
(setq org-roam-directory (expand-file-name "~/documents/exocortex-novus/reference/"))

(add-hook 'org-roam-mode-hook 'visual-line-mode)

(mkdir org-roam-directory t)

(defun oni-org-roam-auto-toggle-roam-buffer (&optional _)
  "Show the ‘org-roam-buffer' if the current buffer is part of Roam."
  (if (or (string-prefix-p org-roam-directory (buffer-file-name))
          (string= (buffer-name) org-roam-buffer))
      (unless (get-buffer-window org-roam-buffer)
        (org-roam-buffer-toggle))
    (when (and (get-buffer-window org-roam-buffer)
               (not (window-minibuffer-p)))
      (org-roam-buffer-toggle))))

(defun oni-org-roam--books-to-read ()
  (org-roam-db-query (concat "select properties, file, title, group_concat(tag) as node_tags "
                             "from nodes "
                             "left join tags on node_id=id "
                             "group by id "
                             "having node_tags like '%%\"book\"%%' "
                             "and node_tags like '%%\"unread\"%%' "
                             "order by title")))

(defun oni-org-roam-list-books-to-read ()
  (interactive)
  (let ((buffer (get-buffer-create "*books-to-read*")))
    (with-current-buffer buffer
      (erase-buffer)
      (save-excursion
        (mapc (lambda (item) (insert "- [["(cadr item) "]["
                                     (caddr item) "]]\n"))
              (oni-org-roam--books-to-read))
        (org-mode)
        (view-mode)))
    (switch-to-buffer buffer)))

(defun oni-org-roam-random-book-to-read ()
  (interactive)
  (find-file (cadr (seq-random-elt (oni-org-roam--books-to-read)))))

(add-to-list 'display-buffer-alist
             `(,(rx string-start "*org-roam*" string-end)
               display-buffer-in-side-window
               (side . right)
               (slot . 0)
               (window-width . 66)))

;;; These two properties are used a lot when working in ‘org-roam'. ‘ROAM_REFS'
;;; is used to specify a URL that (if pointed to) should be considered a
;;; reference to a particular note. ‘ROAM_ALIASES' sets up additional names for
;;; a note.
(add-to-list 'org-default-properties "ROAM_REFS")
(add-to-list 'org-default-properties "ROAM_ALIASES")

;;;###autoload
(setq org-roam-v2-ack t)

(setq org-roam-node-display-template
      (concat "${title:*} "
              (propertize "${tags:10}" 'face 'org-tag)))

;; Only display unique references in the org-roam buffer.
(setq org-roam-mode-sections
      (cons '(org-roam-backlinks-section :unique t)
            (delete 'org-roam-backlinks-section org-roam-mode-sections)))

;; Show only the current element, not the whole heading for the org-roam-buffer
;; preview.
(defun oni-org-roam-preview-element-function ()
  (let ((element (org-element-at-point)))
    (buffer-substring-no-properties (org-element-property :begin element)
                                    (org-element-property :end element))))

(setq org-roam-preview-function #'oni-org-roam-preview-element-function)

(org-roam-db-autosync-mode)

(provide 'oni-org-roam)
;;; oni-org-roam.el ends here