summaryrefslogtreecommitdiff
path: root/hsys-org-roam.el
blob: f06c74a40afb5ed87bea66ad12aebd3987007b32 (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
;;; hsys-org.el --- GNU Hyperbole support functions for Org Roam  -*- lexical-binding: t; -*-
;;
;; Author:       Bob Weiner
;;
;; Orig-Date:    26-Feb-23 at 11:20:15 by Bob Weiner
;; Last-Mod:      7-Mar-26 at 22:38:37 by Bob Weiner
;;
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; Copyright (C) 2023-2025  Free Software Foundation, Inc.
;; See the "HY-COPY" file for license information.
;;
;; This file is part of GNU Hyperbole.

;;; Commentary:
;;
;;   The autoloaded function, `hsys-org-roam-consult-grep', uses
;;   consult-grep to do a full-text search over notes included
;;   into the user's Org Roam database.
;;
;;   Use `org-roam-migrate-wizard' to import any Org note files and
;;   assign them UUIDs required for indexing by Org Roam.

;;; Code:
;;; ************************************************************************
;;; Other required Elisp libraries
;;; ************************************************************************

(require 'package) ;; Always keep this first
(require 'hypb)

;;; ************************************************************************
;;; Public declarations
;;; ************************************************************************

(defvar consult-org-roam-grep-func)
(defvar hsys-org-at-tags-p)
(defvar org-agenda-buffer-tmp-name)
(defvar org-agenda-files)
(defvar org-roam-directory)
(declare-function hsys-org-at-tags-p "hsys-org")
(declare-function hypb:require-package "hypb")
(declare-function org-roam-db-autosync-mode "ext:org-roam")

;;; ************************************************************************
;;; Public functions
;;; ************************************************************************

;;;###autoload
(defun hsys-org-roam-consult-grep ()
  "Prompt for search terms and run consult grep over `org-roam-directory'.
Actual grep function used is given by the variable,
`consult-org-roam-grep-func'."
  (interactive)
  (hypb:require-package 'consult-org-roam)
  (let ((grep-func (when (and (boundp 'consult-org-roam-grep-func)
			      (fboundp consult-org-roam-grep-func))
		     consult-org-roam-grep-func)))
    (if grep-func
	(funcall grep-func org-roam-directory)
      (error "(hsys-org-roam-consult-grep): `%s' is an invalid function"
	     consult-org-roam-grep-func))))

(defun hsys-org-roam-directory-at-tags-p (&optional at-tag-flag)
  "Return non-nil if point is in an `org-roam-directory' buffer and at Org tags."
  (and (featurep 'org-roam)
       (or at-tag-flag (hsys-org-at-tags-p))
       (and (hypb:buffer-file-name)
	    (string-prefix-p (expand-file-name org-roam-directory)
			     (hypb:buffer-file-name)))))

;;;###autoload
(defun hsys-org-roam-tags-view (&optional todo-only match view-buffer-name)
  "Prompt for colon-separated Org Roam tags and display matching headlines.
With optional prefix arg TODO-ONLY, limit matches to Org Roam
todo items only.  With optional MATCH, an Org tags match selector
string, e.g. \":tag1:tag2:tag3:\", match to sections that contain
or inherit all of these tags, regardless of tag order.  With
optional VIEW-BUFFER-NAME, use that rather than the default,
\"*Org Roam Tags*\"."
  (interactive "P")
  (require 'org-agenda)
  (hypb:require-package 'org-roam)
  (let* ((org-agenda-files (list org-roam-directory))
	 (org-agenda-buffer-name (or view-buffer-name "*Org Roam Tags*"))
	 ;; `org-tags-view' is mis-written to require setting this next
	 ;; tmp-name or it will not properly name the displayed buffer.
	 (org-agenda-buffer-tmp-name org-agenda-buffer-name))
    ;; This prompts for the tags to match and uses `org-agenda-files'.
    (org-tags-view todo-only match)
    (when (equal (buffer-name) org-agenda-buffer-name)
      ;; Set up {C-u r} redo cmd
      (let (buffer-read-only)
	(put-text-property (point-min) (point-max) 'org-redo-cmd
			   `(hsys-org-roam-tags-view
			       ,todo-only
			       nil
			       ,org-agenda-buffer-name)))
      (forward-line 2))))

(provide 'hsys-org-roam)

;;; hsys-org-roam.el ends here