-
Notifications
You must be signed in to change notification settings - Fork 1
/
zettel2.el
156 lines (130 loc) · 5.31 KB
/
zettel2.el
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
;;; zettel2.el --- Helpers for note organizing -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Wojciech Siewierski
;; Author: Wojciech Siewierski
;; URL: https://github.com/vifon/zettel2
;; Keywords: outlines, org-mode, convenience
;; Version: 0.9
;; Package-Requires: ((emacs "28.1") (org "9.5"))
;; 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:
;; Helpers for note organization.
;; Install with:
;; (use-package zettel2
;; :straight (:host github :repo "vifon/zettel2"
;; :files (:defaults "graph.pl"))
;; :mode (("/\\.deft/[^/]+\\.org\\'" . zettel2-mode)
;; ("/zettels?/[^/]+\\.org\\'" . zettel2-mode))
;; :config (require 'zettel2-link))
;;; Code:
(require 'zettel2-frontmatter)
(require 'xref)
(defgroup zettel2 nil
"Helpers for note organization."
:group 'outlines)
(defconst zettel2-id-time-format "%Y%m%dT%H%M%S"
"The prefix used for the filenames of created files.
Passed to `format-time-string'.")
(defconst zettel2-id-regexp
(rx (= 8 digit) "T" (= 6 digit)))
(defun zettel2-all-notes (&optional directory)
"List all the valid notes in DIRECTORY or the current directory."
(directory-files
(or directory default-directory)
nil
(rx bos
(regexp zettel2-id-regexp)
"--"
(+ (not "/"))
".org"
eos)))
(defun zettel2-all-tags (&optional directory)
"List all the tags present in DIRECTORY in lexicographical order."
(sort (seq-uniq
(mapcan (lambda (file)
(split-string
(replace-regexp-in-string ".*__\\([^.]+\\)\\.org"
"\\1"
file)
"_"))
(zettel2-all-notes directory)))
#'string<))
(defun zettel2-get-note-by-id (id)
"Find a file by the note ID."
(pcase (directory-files default-directory nil
(rx bos (literal id) "--" (* anything) ".org" eos))
(`(,file) file)
('() (error "No file with ID: %S" id))
(file-list (error "Conflicting file IDs: %S" file-list))))
(defun zettel2-file-id (file)
"Extract the note ID from FILE."
(string-match (rx (or bos "/")
(group (regexp zettel2-id-regexp))
"--")
file)
(match-string 1 file))
(defun zettel2-sanitize-name (name &optional tags)
"Compute a valid filename for a new note named NAME tagged with TAGS."
(format "%s--%s%s.org"
(format-time-string zettel2-id-time-format)
(replace-regexp-in-string "[^a-zA-Z0-9]+" "-" (downcase name))
(if tags
(concat "__" (string-join tags "_"))
"")))
;;;###autoload
(defun zettel2-create-note (title &optional tags)
"Create a new note with a given TITLE and TAGS."
(interactive
(list
(read-from-minibuffer "Title: ")
(completing-read-multiple "Tags: "
(zettel2-all-tags))))
(let ((file-name (zettel2-sanitize-name title tags)))
(find-file-other-window file-name)
(with-current-buffer (get-file-buffer file-name)
(insert (format-spec zettel2-frontmatter-template
(zettel2-frontmatter-format-spec
(zettel2-file-id file-name)
title)))
(goto-char (point-max)))))
(defun zettel2-backrefs ()
"Show the notes referencing this one."
(interactive)
(let ((backrefs-buffer-name "*zettel-backrefs*")
(xrefs (xref-matches-in-files
(concat ":"
(regexp-quote (zettel2-file-id buffer-file-name))
"\\(--\\|]\\)")
(mapcar #'expand-file-name (zettel2-all-notes))))
(file (file-relative-name buffer-file-name)))
(unless xrefs
(user-error "No references to `%s'" file))
(with-current-buffer (get-buffer-create backrefs-buffer-name)
(grep-mode)
(toggle-truncate-lines 1)
(let ((inhibit-read-only t))
(erase-buffer)
(insert (format-message "# Backrefs for `%s':\n\n" file))
(apply #'insert (mapcar
(lambda (xref)
(let ((loc (xref-match-item-location xref)))
(format "%s:%s:%s\n"
(file-relative-name
(xref-location-group loc))
(xref-location-line loc)
(substring-no-properties
(xref-item-summary xref)))))
xrefs)))
(goto-char (point-min))
(display-buffer (current-buffer)))
(setq next-error-last-buffer (get-buffer backrefs-buffer-name))))
(provide 'zettel2)
;;; zettel2.el ends here