summaryrefslogtreecommitdiffhomepage
path: root/lib/org/jao-org-notes.el
blob: 3c3a134c9c068e38d40e7f5a4fc70f6f9bcad56c (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
;;; jao-org-notes.el --- A simple system for org note taking  -*- lexical-binding: t; -*-

;; Copyright (C) 2020, 2021  jao

;; Author: jao <mail@jao.io>
;; Keywords: tools

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

;; A note per file

;;; Code:

(require 'org)

(defvar jao-org-notes-dir (expand-file-name "notes" org-directory))

(defun jao-org-notes--cat ()
  (let* ((cats (seq-difference (directory-files jao-org-notes-dir)
                               '("." ".." "attic")))
         (cat (completing-read "Top level category: " cats)))
    (cond ((file-exists-p (expand-file-name cat jao-org-notes-dir)) cat)
          ((yes-or-no-p "New category, create?") cat)
          (t (jao-roam--cat)))))

(defun jao-org-notes--insert-title ()
  (let ((cat (jao-org-notes--cat))
        (title (read-string "Title: ")))
    (when (not (string-empty-p title))
      (let* ((base (replace-regexp-in-string " +" "-" (downcase title)))
             (fname (expand-file-name (concat cat "/" base ".org")
                                      jao-org-notes-dir))
             (exists? (file-exists-p fname)))
        (find-file fname)
        (when (not exists?)
          (insert "#+title: " title "\n")
          t)))))

(defun jao-org-notes--insert-tags ()
  (let ((ts (completing-read-multiple "Tags: "
                                      (org-global-tags-completion-table))))
    (insert "#+filetags:" ":" (mapconcat 'identity ts ":") ":\n")))

(defun jao-org-notes--insert-date ()
  (insert "#+date: ")
  (org-insert-time-stamp (current-time))
  (insert "\n"))

(defun jao-org-notes--template (k)
  `(,k "Note" plain (file jao-org-notes-open) "* %a "))

(defvar jao-org-notes--rg
  (concat "rg --null --line-buffered --color=ansi --max-columns=250"
          " --no-heading --line-number --smart-case"
          " . -e \"^(#.(title|(file|roam_)tags): .*)ARG\" OPTS"))

(defun jao-org-notes--clean-match (m)
  (cons (replace-regexp-in-string ":[0-9]+:#\\+\\(roam_\\|file\\)?\\(title\\|tags\\):"
                                  " (\\2)"
                                  (car m))
        (cdr m)))

(defun jao-org-notes--matches (lines)
  (let ((ms (consult--grep-matches lines)))
    (mapcar #'jao-org-notes--clean-match ms)))

(defun jao-org-notes--lookup (_ cands cand)
  (cadr (assoc cand cands)))

(defvar jao-org-notes--grep-history nil)

(defun jao-org--grep ()
  (let ((default-directory jao-org-notes-dir))
    (consult--read
     (consult--async-command jao-org-notes--rg
       (consult--async-transform jao-org-notes--matches))
     :prompt "Search notes: "
     :lookup #'jao-org-notes--lookup
     :initial consult-async-default-split
     :add-history (concat consult-async-default-split (thing-at-point 'symbol))
     :require-match t
     :category 'jao-org-notes-lookup
     :history '(:input jao-org-notes--grep-history)
     :sort nil)))

;;;###autoload
(defun jao-org-notes-consult ()
  "Search notes directory for tags and titles."
  (interactive)
  (when-let (f (jao-org--grep))
    (find-file f)))

;;;###autoload
(defun jao-org-notes-open ()
  (interactive)
  (when (jao-org-notes--insert-title)
    (jao-org-notes--insert-date)
    (jao-org-notes--insert-tags)
    (insert "#+link: "))
  (save-buffer)
  (buffer-file-name))

;;;###autoload
(defun jao-org-notes-setup (mnemonic)
  (setq org-capture-templates
        (add-to-list 'org-capture-templates (jao-org-notes--template mnemonic)))
  (add-to-list 'org-agenda-files jao-org-notes-dir)
  (when (fboundp 'org-capture-upgrade-templates)
    (org-capture-upgrade-templates org-capture-templates)))

;;;###autoload
(defun jao-org-notes-backlinks ()
  (interactive)
  (consult-ripgrep jao-org-notes-dir (regexp-quote (buffer-name))))

(provide 'jao-org-notes)
;;; jao-org-notes.el ends here