forked from dominiksta/mvtn.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvtn-journal.el
349 lines (305 loc) · 14.9 KB
/
mvtn-journal.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
;;; mvtn-journal.el --- Simple journaling for mvtn -*- lexical-binding: t -*-
;;; Commentary:
;; This file may be loaded in addition to mvtn's core (mvtn.el) to provide
;; journaling functionality akin to `org-journal'. The reason `org-journal' is
;; not used directly is simply that it is hardcoded to use `org-mode', while
;; mvtn wants to also support `markdown-mode'.
;;; Code:
(require 'mvtn)
(require 'mvtn-file-helpers)
(defconst mvtn-journal-daily-timestamp "000000"
"All daily notes will be created with this timestamp.
This is a constant and should not be changed. Mvtn uses this
internally to identify daily notes. Of course, this means that
you cannot create a note at /exactly/ midnight, as that would
confuse the journal. This is reasonably unlikely to happen imo,
so this should never be a problem.")
(defcustom mvtn-journal-dir "prv/fleeting"
"The abbreviated note directory to store journal files.
See `mvtn-note-directories' and `mvtn-short-note-dir-list'."
:type 'string :group 'mvtn)
(defcustom mvtn-journal-new-daily-title "Daily note for %Y-%m-%d"
"The title for all new daily notes.
Format specifiers from `format-time-string' can be used."
:type 'string :group 'mvtn)
(defcustom mvtn-journal-new-daily-tags '("journal")
"The tags for all new daily notes."
:type '(list :value-type string) :group 'mvtn)
(defcustom mvtn-journal-file-extension-templates
(list
'("org" "#+TITLE: {title}
#+DATE: {date}
# mvtn_original_title :: {title}
# mvtn_original_id :: {timestamp}
#+STARTUP: fold\n\n")
(assoc "md" mvtn-file-extension-templates)
(assoc "" mvtn-file-extension-templates))
"Like `mvtn-file-extension-templates', expept for journal notes."
:type '(list :value-type string) :group 'mvtn)
(defcustom mvtn-journal-default-file-extension mvtn-default-file-extension
"The default file extension for new journal notes.
Also controls which template from
`mvtn-journal-file-extension-templates' is used."
:type 'string :group 'mvtn)
(defcustom mvtn-journal-entry-file-extension-templates
'(("org" "* /%H:%M/ ") ("md" "# _%H:%M_ ") ("" "# %H:%M "))
"The tags for all new daily notes."
:type '(list :value-type string) :group 'mvtn)
(defun mvtn-journal-daily-for-time (time create &optional encrypt open)
"Open the daily note for TIME, creating it if CREATE is non-nil.
TIME is defined in the emacs-internal time format. See
`current-time' for the format. A simple function to create such
a timestamp is `encode-time'. When the note does not yet exist
and ENCRYPT is non-nil, the new note will be encrypted with gpg.
If OPEN is non-nil, the file will opened. Otherwise, a buffer to
the file will be returned."
(let* ((id (format-time-string
(concat "%Y%m%d-" mvtn-journal-daily-timestamp) time))
(link (format "^^%s^^" id))
(title (format-time-string mvtn-journal-new-daily-title time))
(date (format-time-string "%Y-%m-%d" time))
(found (mvtn-link-targets link)))
(when (> (length found) 1)
(error "Multiple daily entries for one day: %s" found))
(if found
(progn
(when encrypt (warn (concat "The current daily note has already "
"been created as an unencrypted file")))
(let ((file (mvtn-expand-note-name (car found))))
(if open (find-file file) (find-file-noselect file))))
(progn
(if create
(progn
(message "Creating new daily note for %s" date)
(let ((buf
(mvtn-create-new-file
id mvtn-journal-dir title
mvtn-journal-default-file-extension
mvtn-journal-new-daily-tags
(mvtn-substitute-template
(mvtn-template-for-extension
mvtn-journal-default-file-extension
mvtn-journal-file-extension-templates)
title date id)
encrypt)))
(when open (switch-to-buffer buf))
buf))
nil)))))
(defun mvtn-journal-open-daily-past-relative (n &optional encrypt)
"Open the daily note for N days in the past.
If ENCRYPT is specified and the note does not yet exist, if will
be encrypted with gpg."
(interactive)
(let* ((target-time (seconds-to-time (- (time-to-seconds)
(* 60 (* 60 (* 24 n)))))))
(mvtn-journal-daily-for-time target-time t encrypt t)))
(defun mvtn-journal-new-entry-for-time (time text &optional encrypt)
"Insert TEXT at a new entry at TIME in the daily note for TIME.
If ENCRYPT is specified and the daily note for TIME does not yet
exist, if will be encrypted with gpg."
(mvtn-journal-daily-for-time time t encrypt t)
(goto-char (point-max))
(insert "\n"
(format-time-string (mvtn-template-for-extension
mvtn-journal-default-file-extension
mvtn-journal-entry-file-extension-templates)
time)
text)
(when (fboundp 'evil-append) (evil-append 0)))
;;;###autoload
(defun mvtn-journal-new-entry (&optional encrypt)
"Open the journal for today and add a new entry for the current time.
If ENCRYPT is specified and the daily note for TIME does not yet
exist, if will be encrypted with gpg."
(interactive "P")
(mvtn-journal-new-entry-for-time (current-time) "" encrypt))
;;;###autoload
(defun mvtn-journal-new-quick-entry (text &optional encrypt)
"Create a new journal entry for the current time from TEXT.
In interactive use, TEXT will be read from the minibuffer. Like
`mvtn-journal-new-entry', but does not open the file in the
foreground. If ENCRYPT is specified and the daily note for TIME
does not yet exist, if will be encrypted with gpg."
(interactive "MJournal Entry: \nP")
(save-window-excursion
(mvtn-journal-new-entry-for-time (current-time) text encrypt)
(when (fboundp 'evil-normal-state) (evil-normal-state))
(save-buffer)))
;;;###autoload
(defun mvtn-journal-new-entry-yesterday (&optional encrypt)
"Open yesterdays daily note.
If ENCRYPT is specified and the note does not yet exist, if will
be encrypted with gpg."
(interactive)
(mvtn-journal-open-daily-past-relative 1 encrypt))
;; ----------------------------------------------------------------------
;; autojournal
;; ----------------------------------------------------------------------
;;;###autoload
(defun mvtn-journal-autojournal-set-feature (feature enable)
"Enable or disable an mvtn autojournaling feature.
Autojournaling refers to the concept of automatically adding
entries to your journal on certain events. For example, if you
use `magit', you can put
\(mvtn-journal-autojournal-set-feature 'git-commit t)
in your init file to enable automatically logging all git commits
to your journal.
If ENABLE is non-nil, the feature will be enabled, otherwise
disabled.
Available values for FEATURE are:
- 'git-commit (see `mvtn-journal-autojournal-git-commit')
- 'note-changed (see `mvtn-journal-autojournal-note-changed')
- 'org-clock (see `mvtn-journal-autojournal-org-clock')"
(let* ((suffix (if enable "-enable" "-disable")))
(funcall (intern (concat "mvtn-journal-autojournal-"
(symbol-name feature) suffix)))))
;; git-commit
;; ----------------------------------------------------------------------
(defcustom mvtn-journal-autojournal-git-commit-format
"Commit in repo \"%s\"\n\n- Commit Summary: %s\n- Full repo path: %s\n"
"The format for autojournal entries for git commits.
The first %s will be replaced by the repository, the second by
the commit summary and the third with the full path to the
repository. See `mvtn-journal-autojournal-set-feature' and
`mvtn-journal-autojournal-git-commit'."
:type 'string :group 'mvtn)
(declare-function magit-toplevel "ext:magit")
(declare-function magit-git-string "ext:magit")
(declare-function magit-format-rev-summary "ext:magit")
(defun mvtn-journal-autojournal-git-commit ()
"Record a commit made with magit to the mvtn journal.
Expects to be called using `git-commit-post-finish-hook'.
Catches errors and ignores them because the ability to commit
seems more important than journaling."
(condition-case nil
(let* ((repo (abbreviate-file-name (magit-toplevel)))
(short-repo (file-name-nondirectory (directory-file-name repo)))
(rev (magit-git-string "rev-parse" "HEAD"))
(summary (substring-no-properties (magit-format-rev-summary rev))))
(mvtn-journal-new-quick-entry
(format mvtn-journal-autojournal-git-commit-format
short-repo summary repo)))
(error nil)))
(defun mvtn-journal-autojournal-git-commit-enable ()
"Enable mvtn autojournal for git commits."
(add-hook 'git-commit-post-finish-hook 'mvtn-journal-autojournal-git-commit))
(defun mvtn-journal-autojournal-git-commit-disable ()
"Disable mvtn autojournal for git commits."
(remove-hook 'git-commit-post-finish-hook 'mvtn-journal-autojournal-git-commit))
;; note-changed
;; ----------------------------------------------------------------------
(defcustom mvtn-journal-autojournal-note-changed-format
"Edited note: %s\n\nedit:%s %s\n"
"The format for autojournal entries for note edits.
The first %s will be replaced by the note name, the second by a
the current timestamp and the third link to the note. See
`mvtn-journal-autojournal-set-feature' and
`mvtn-journal-autojournal-note-changed'."
:type 'string :group 'mvtn)
(defun mvtn-journal-autojournal-note-changed ()
"Record every edit of any mvtn note to the mvtn journal.
Expects to be called using `after-save-hook'. Catches errors and
ignores them because the ability to save a file seems more
important than journaling."
(condition-case nil
(let* ((id+name (mvtn--extract-note-identity buffer-file-name t))
(link (format "%s" id+name))
(name (substring link 16))
(today (format-time-string "%Y%m%d"))
(match (format "^%s-%s" today mvtn-journal-daily-timestamp)))
;; ignore the current journal note to avoid an infinite loop
(if (not (string-match-p match id+name))
(mvtn-journal-new-quick-entry
(format mvtn-journal-autojournal-note-changed-format
name (format-time-string "%H:%M:%S") link))))
(error nil)))
(defun mvtn-journal-autojournal-note-changed-enable ()
"Enable mvtn autojournal for mvtn note edits."
(add-hook 'mvtn-minor-mode-hook
(lambda () (add-hook 'before-save-hook
'mvtn-journal-autojournal-note-changed 0 t))))
(defun mvtn-journal-autojournal-note-changed-disable ()
"Disable mvtn autojournal for mvtn note edits."
(remove-hook 'mvtn-minor-mode-hook
(lambda () (add-hook 'before-save-hook
'mvtn-journal-autojournal-note-changed 0 t))))
;; org-clock
;; ----------------------------------------------------------------------
(defcustom mvtn-journal-autojournal-org-clock-in-note-format
"org-clock-in\n\nclock-in:%s: %s in %s\n"
"The format for autojournal entries for org clock in events in mvtn notes.
The first %s will be replaced by current timestamp, the second by
the org task and the third by a link to the note. See
`mvtn-journal-autojournal-set-feature' and
`mvtn-journal-autojournal-note-changed'."
:type 'string :group 'mvtn)
(defcustom mvtn-journal-autojournal-org-clock-out-note-format
"org-clock-out\n\nclock-out:%s: %s in %s\n"
"The format for autojournal entries for org clock out events in mvtn notes.
The first %s will be replaced by current timestamp, the second by
the org task and the third by a link to the note. See
`mvtn-journal-autojournal-set-feature' and
`mvtn-journal-autojournal-note-changed'."
:type 'string :group 'mvtn)
(defcustom mvtn-journal-autojournal-org-clock-in-file-format
"org-clock\n\nclock-in:%s: %s in %s\n"
"The format for autojournal entries for org clock in events regular files.
The first %s will be replaced by current timestamp, the second by
the org task and the third by the full file path. See
`mvtn-journal-autojournal-set-feature' and
`mvtn-journal-autojournal-note-changed'."
:type 'string :group 'mvtn)
(defcustom mvtn-journal-autojournal-org-clock-out-file-format
"org-clock\n\nclock-out:%s: %s in %s\n"
"The format for autojournal entries for org clock out events regular files.
The first %s will be replaced by current timestamp, the second by
the org task and the third by the full file path. See
`mvtn-journal-autojournal-set-feature' and
`mvtn-journal-autojournal-note-changed'."
:type 'string :group 'mvtn)
(defvar org-clock-current-task)
(defun mvtn-journal-autojournal-org-clock (in)
"Record an org clock in or out event to the mvtn journal.
If IN is non-nil, a clock-in event is logged. Is IN is nil, a
clock-out event is logged. Catches errors and ignores them
because the ability to clock in or out seems more important than
journaling that event."
(condition-case nil
(let* ((task-name (substring-no-properties org-clock-current-task))
(file (abbreviate-file-name buffer-file-name))
(file-short (file-name-nondirectory buffer-file-name))
(is-note (string-match-p mvtn--link-regexp (format "^^%s^^" file-short)))
(id+name (if is-note (mvtn--extract-note-identity
buffer-file-name t)
nil))
(link (if is-note (format "\"%s\"" id+name)))
(time (format-time-string "%H:%M:%S"))
(text (if in
(if is-note
(format mvtn-journal-autojournal-org-clock-in-note-format
time task-name link)
(format mvtn-journal-autojournal-org-clock-in-file-format
time task-name file))
(if is-note
(format mvtn-journal-autojournal-org-clock-out-note-format
time task-name link)
(format mvtn-journal-autojournal-org-clock-out-file-format
time task-name file)))))
(mvtn-journal-new-quick-entry text))
(error nil)))
(defun mvtn-journal-autojournal-org-clock-in ()
"Call (`mvtn-journal-autojournal-org-clock' t)."
(mvtn-journal-autojournal-org-clock t))
(defun mvtn-journal-autojournal-org-clock-out ()
"Call (`mvtn-journal-autojournal-org-clock' nil)."
(mvtn-journal-autojournal-org-clock nil))
(defun mvtn-journal-autojournal-org-clock-enable ()
"Enable mvtn autojournal for org clock."
(add-hook 'org-clock-in-hook 'mvtn-journal-autojournal-org-clock-in)
(add-hook 'org-clock-out-hook 'mvtn-journal-autojournal-org-clock-out))
(defun mvtn-journal-autojournal-org-clock-disable ()
"Disable mvtn autojournal for org clock."
(remove-hook 'org-clock-in-hook 'mvtn-journal-autojournal-org-clock-in)
(remove-hook 'org-clock-out-hook 'mvtn-journal-autojournal-org-clock-out))
(provide 'mvtn-journal)
;;; mvtn-journal.el ends here