aboutsummaryrefslogtreecommitdiffstats
path: root/zettelkasten.el
blob: 69ca56b7cb90261bd291f23a5be396fc0d825d28 (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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
;;; Zettelkasten --- Helper functions to organise notes.
;;; Commentary:
;;;
;;; Used to organise notes using the Zettelkasten method.
;;;
;;; Copyright (C) 2020  Yann Herklotz
;;;
;;; 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/>.
;;;
;;; Code:

(defgroup zettelkasten nil
  "Helper to work with zettelkasten notes."
  :group 'applications)

(defcustom zettelkasten-prefix [(control ?c) ?k]
  "Prefix key to use for Zettelkasten commands in Zettelkasten minor mode.
The value of this variable is checked as part of loading Zettelkasten mode.
After that, changing the prefix key requires manipulating keymaps."
  :type 'key-sequence
  :group 'zettelkasten)

(defcustom zettelkasten-directory "~/Dropbox/org/zettelkasten"
  "Main zettelkasten directory."
  :type 'string
  :group 'zettelkasten)

(defcustom zettelkasten-file-format "%y%W%u%%02d"
  "Format for new zettelkasten files.

For supported options, please consult `format-time-string'."
  :type 'string
  :group 'zettelkasten)

(defcustom zettelkasten-link-format "[[./%2$s.%3$s][%1$s]]"
  "Zettelkasten link format."
  :type 'string
  :group 'zettelkasten)

(defcustom zettelkasten-extension "org"
  "Default extension for notes."
  :type 'string
  :group 'zettelkasten)

(defun zettelkasten-make-filename (note)
  "Make a filename using the default directory and the NOTE passed to it."
  (expand-file-name (concat zettelkasten-directory "/"
                            note "." zettelkasten-extension)))

(defun zettelkasten-filename-to-id (filename)
  "Convert FILENAME to id."
  (string-match (format "\\([0-9]*\\)\\.%s\\'" zettelkasten-extension) filename)
  (match-string 1 filename))

(defun zettelkasten-note-regexp (note regexp &optional num)
  "Return the REGEXP first match in the NOTE.

Return the NUMth match.  If NUM is nil, return the 0th match."
  (with-temp-buffer
    (insert-file-contents-literally
     (zettelkasten-make-filename note))
    (when (re-search-forward regexp nil t)
      (match-string (if num num 0)))))

(defun zettelkasten-display-id-title (note)
  "Dispaly the NOTE's title and id."
  (format "%s: %s" note (zettelkasten--get-note-title note)))

(defun zettelkasten-match-link (current note)
  "Return t if the link to CURRENT is in NOTE."
  (when (zettelkasten-note-regexp
         note
         (let ((zk-link-format (replace-regexp-in-string
                                "\\\\\\$" "$"
                                (regexp-quote zettelkasten-link-format))))
           (format zk-link-format
                   ".*" current
                   zettelkasten-extension)))
    note))

(defun zettelkasten-list-notes-by-id ()
  "Return all the ids that are currently available."
  (mapcar 'zettelkasten-filename-to-id
          (directory-files
           (expand-file-name zettelkasten-directory) nil
           (format "\\.%s$" zettelkasten-extension) t)))

(defun zettelkasten-list-notes-grep ()
  "Return all the ids and titles of notes in the `zettelkasten-directory'."
  (shell-command (concat "grep -i \"#+TITLE:\" " zettelkasten-directory "/*"))
  (let (match-list morelines current-string matched-string)
    (with-current-buffer "*Shell Command Output*"
      (set-buffer "*Shell Command Output*")
      (setq morelines t)
      (goto-char 1)
      (while morelines
        (setq current-string
              (buffer-substring-no-properties
               (line-beginning-position)
               (line-end-position)))
        (when (string-match
               (format "\\([0-9]*\\)\\.%s:#\\+TITLE: \\(.*\\)"
                       zettelkasten-extension)
               current-string)
          (setq matched-string (concat (match-string 1 current-string) ": "
                                       (match-string 2 current-string)))
          (setq match-list (append match-list (list matched-string))))
        (setq morelines (= 0 (forward-line 1)))))
    (kill-buffer "*Shell Command Output*")
    match-list))

(defun zettelkasten-list-notes ()
  "Return all the ids and titles of notes in the `zettelkasten-directory'."
  (mapcar 'zettelkasten-display-id-title
          (zettelkasten-list-notes-by-id)))

(defun zettelkasten-find-new-note-name (note)
  "Iterate on ITERATION until a usable file based on NOTE is found."
  (let ((iteration 0)
        (maxcount 10000))
    (while (and (< iteration maxcount)
                (file-exists-p
                 (zettelkasten-make-filename (format note iteration))))
      (setq iteration (+ iteration 1)))
    (format note iteration)))

(defun zettelkasten-generate-note-name ()
  "Create the new note name."
  (zettelkasten-find-new-note-name
   (format-time-string zettelkasten-file-format)))

(defun zettelkasten-get-id (note)
  "Return the id for a NOTE."
  (string-match "\\([0-9]*\\)" note)
  (match-string 1 note))

(defun zettelkasten-format-link (note)
  "Format a link to a NOTE."
  (format zettelkasten-link-format
          (zettelkasten--get-note-title note)
          (zettelkasten-get-id note)
          zettelkasten-extension))

(defun zettelkasten-add-link-to-parent (note parent)
  "Add a link to NOTE from a PARENT."
  (with-temp-file (zettelkasten-make-filename parent)
    (insert-file-contents-literally (zettelkasten-make-filename parent))
    (goto-char (point-max))
    (insert (concat "\n" (zettelkasten-format-link note)))))

(defun zettelkasten-create-new-note-ni (title &optional parent)
  "Create a new note based on the TITLE and it's optional PARENT note.

If PARENT is nil, it will not add a link from a PARENT."
  (let ((note (zettelkasten-generate-note-name)))
    (when parent
      (zettelkasten-add-link-to-parent note (zettelkasten-get-id parent)))
    (find-file (zettelkasten-make-filename note))
    (insert (concat "#+TITLE: " title
                    (format-time-string "\n#+DATE: %c\n#+TAGS:\n\n")))
    (save-buffer)))

(defun zettelkasten-create-new-note (prefix)
  "Create a new zettelkasten.

If PREFIX is used, or if the `zettelkasten-directory' is empty,
does not create a parent.

Also see `zettelkasten-create-new-note-ni' for more information."
  (interactive "P")
  (let ((title (read-string "Note title: "))
        (notes (zettelkasten-list-notes)))
    (zettelkasten-create-new-note-ni
     title
     (unless (or prefix (not notes))
       (completing-read "Parent note: " notes nil 'match)))))

(defun zettelkasten-insert-link (note)
  "Insert a link to another NOTE in the current note."
  (interactive
   (list (completing-read "Notes: "
                          (zettelkasten-list-notes) nil 'match)))
  (insert (zettelkasten-format-link note)))

(defun zettelkasten-find-parents (note)
  "Find the parents of the NOTE."
  (delete
   nil
   (mapcar #'(lambda (el) (zettelkasten-match-link
                           note el))
           (zettelkasten-list-notes-by-id))))

(defun zettelkasten--get-note-title (note)
  "Return the title of the NOTE."
  (zettelkasten-note-regexp note "#\\+TITLE: \\(.*\\)" 1))

(defun zettelkasten-org-export-preprocessor (backend)
  "A preprocessor for zettelkasten directories, using the BACKEND.

Adds information such as backlinks to the `org-mode' files before
publishing."
  (let ((notes (zettelkasten-find-parents
                (zettelkasten-filename-to-id (buffer-file-name)))))
    (when notes
      (save-excursion
        (goto-char (point-max))
        (insert
         (mapconcat 'identity (append
          '("\n* Backlinks\n")
           (mapcar
            #'(lambda
                (el)
                (concat "- " (zettelkasten-format-link el) "\n"))
            notes)) ""))))))

(defun zettelkasten-open-parent (&optional note)
  "Find the parent notes to the NOTE that is given.

The format of the NOTE is anything that can be ready by
  `zettelkasten-get-id'."
  (interactive)
  (let* ((act-note
          (if note (zettelkasten-get-id note)
            (zettelkasten-filename-to-id (buffer-file-name))))
         (selected (completing-read
                    "Notes: "
                    (delete
                     nil
                     (mapcar #'(lambda (el)
                                 (zettelkasten-display-id-title
                                  (zettelkasten-match-link act-note el)))
                             (zettelkasten-list-notes-by-id)))
                    nil 'match)))
    (find-file (zettelkasten-make-filename (zettelkasten-get-id selected)))))

(defun zettelkasten-open-note (note)
  "Open an existing NOTE, searching by title and id."
  (interactive
   (list (completing-read "Notes: "
                          (zettelkasten-list-notes) nil 'match)))
  (find-file (zettelkasten-make-filename (zettelkasten-get-id note))))

(defvar zettelkasten-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "i" 'zettelkasten-insert-link)
    (define-key map "n" 'zettelkasten-create-new-note)
    (define-key map "p" 'zettelkasten-open-parent)
    (define-key map "o" 'zettelkasten-open-note)
    map))

(defvar zettelkasten-minor-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map zettelkasten-prefix zettelkasten-mode-map)
    map)
  "Keymap used for binding footnote minor mode.")

(define-minor-mode zettelkasten-mode
  "Enable the keymaps to be used with zettelkasten."
  :lighter " zettelkasten"
  :keymap zettelkasten-minor-mode-map
  :global t)

(provide 'zettelkasten)

;;; zettelkasten.el ends here