1
0
Fork 0
emacs-config/oni-org-roam.el
Tom Willemse cff931be7e Speed up ‘oni-org-roam--books-to-read’
By doing more of the filtering in SQL rather than Emacs Lisp it got a bit
faster.
2023-08-01 11:02:56 -07:00

128 lines
4.7 KiB
EmacsLisp
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; 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: 2023.0728.155508
;; 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/"))
(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))))
;;; Taken from/inspired by
;;; https://magnus.therning.org/2021-07-23-keeping-todo-items-in-org-roam-v2.html
(defun oni-org-roam-update-todo-files (&rest _)
"Set org-agenda-files to all roam files with a task in them."
(setq org-agenda-files (seq-uniq (append org-agenda-files (oni-org-roam--todo-files)))))
(defun oni-org-roam--todo-files ()
"Get a list of all the files in the org-roam database with tasks in them."
(let ((nodes (seq-filter #'oni-org-roam--todo-node-p (org-roam-node-list))))
(seq-uniq (seq-map #'org-roam-node-file nodes))))
(defun oni-org-roam--todo-node-p (node)
"Predicate to check whether or not NODE is task."
(let ((state (org-roam-node-todo node)))
(and (not (null state))
(not (string= state "FINISHED")))))
(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)))
(add-hook 'window-state-change-hook #'oni-org-roam-auto-toggle-roam-buffer)
(advice-add 'org-agenda :before #'oni-org-roam-update-todo-files)
;;; 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)))
(org-roam-db-autosync-mode)
(provide 'oni-org-roam)
;;; oni-org-roam.el ends here