blob: 0489888c024f11e9624fe85a81dca8b801a4e52f (
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
|
;;; ss-capture.el --- Capture configuration -*- lexical-binding: t; -*-
;;; Commentary:
;; Journal capture helpers and capture templates.
;;; Code:
(require 'calendar)
(require 'org)
(require 'org-capture)
(require 'ss-core)
(require 'ss-org)
(defun ss-journal-capture-time ()
"Return the effective timestamp for the current journal capture."
(or org-overriding-default-time
(org-capture-get :default-time)
(current-time)))
(defun ss-journal-calendar-date (&optional time)
"Return TIME as a Gregorian date list for datetree helpers."
(calendar-gregorian-from-absolute
(time-to-days (or time (current-time)))))
(defun ss-journal-year-heading (&optional time)
"Return the journal year heading text for TIME."
(format-time-string "%Y" (or time (current-time))))
(defun ss-journal-day-heading (&optional time)
"Return the journal day heading text for TIME."
(format-time-string "%Y-%m-%d %A" (or time (current-time))))
(defun ss-journal-find-or-create-heading (level heading)
"Move to HEADING at LEVEL, creating it when missing."
(goto-char (point-min))
(if (re-search-forward
(format "^%s %s$"
(make-string level ?*)
(regexp-quote heading))
nil t)
(goto-char (match-beginning 0))
(goto-char (point-max))
(unless (bolp)
(insert "\n"))
(insert (make-string level ?*) " " heading "\n")
(forward-line -1))
(org-back-to-heading t))
(defun ss-journal-goto-date (&optional time create)
"Move to TIME's journal date heading.
When CREATE is non-nil, create the datetree entry when missing."
(goto-char (point-min))
(if create
(let ((year-heading (ss-journal-year-heading time))
(day-heading (ss-journal-day-heading time)))
(ss-journal-find-or-create-heading 1 year-heading)
(save-restriction
(org-narrow-to-subtree)
(ss-journal-find-or-create-heading 2 day-heading))
t)
(when (re-search-forward
(format "^\\*\\* %s$"
(regexp-quote
(ss-journal-day-heading (or time (current-time)))))
nil t)
(goto-char (match-beginning 0))
t)))
(defun ss-journal-ensure-day-sections ()
"Ensure the standard section headings exist under the current journal day."
(org-back-to-heading t)
(let ((section-level (1+ (org-outline-level))))
(save-excursion
(save-restriction
(org-narrow-to-subtree)
(dolist (section ss-journal-section-headings)
(goto-char (point-min))
(forward-line 1)
(unless (re-search-forward
(format "^%s %s$"
(make-string section-level ?*)
(regexp-quote section))
nil t)
(goto-char (point-max))
(unless (bolp)
(insert "\n"))
(insert (make-string section-level ?*) " " section "\n")))))))
(defun ss-journal-goto-section (section &optional time)
"Move to SECTION beneath TIME's journal date, creating structure as needed."
(unless (member section ss-journal-section-headings)
(user-error "Unknown journal section: %s" section))
(ss-journal-goto-date time 'create)
(ss-journal-ensure-day-sections)
(let ((section-level (1+ (org-outline-level)))
position)
(save-restriction
(org-narrow-to-subtree)
(goto-char (point-min))
(when (re-search-forward
(format "^%s %s$"
(make-string section-level ?*)
(regexp-quote section))
nil t)
(setq position (match-beginning 0))))
(unless position
(user-error "Journal section not found: %s" section))
(goto-char position)
(org-back-to-heading t)))
(defun ss-journal-capture-target (section)
"Select SECTION under today's journal datetree entry for capture."
(set-buffer (find-file-noselect (ss-require-existing-file ss-journal-file)))
(widen)
(ss-journal-goto-section section (ss-journal-capture-time)))
(defun ss-capture--denote-templates ()
"Return Denote-backed capture templates when Denote is enabled."
(when (ss-feature-enabled-p 'denote)
`(("n" "Denote")
("nn" "Generic" plain
(file denote-last-path)
(function
(lambda ()
(denote-org-capture-with-prompts :title :keywords :subdirectory)))
:no-save t
:immediate-finish nil
:kill-buffer t
:jump-to-captured t)
("np" "Project" plain
(file denote-last-path)
(function
(lambda ()
(ss-denote-capture-in-directory
ss-org-projects-directory '("project") :title :keywords :subdirectory)))
:no-save t
:immediate-finish nil
:kill-buffer t
:jump-to-captured t)
("na" "Area" plain
(file denote-last-path)
(function
(lambda ()
(ss-denote-capture-in-directory
ss-org-areas-directory nil :title :keywords :subdirectory)))
:no-save t
:immediate-finish nil
:kill-buffer t
:jump-to-captured t)
("nr" "Resource" plain
(file denote-last-path)
(function
(lambda ()
(ss-denote-capture-in-directory
ss-org-resources-directory nil :title :keywords :subdirectory)))
:no-save t
:immediate-finish nil
:kill-buffer t
:jump-to-captured t))))
(defun ss-capture-setup ()
"Initialize capture templates."
(setq org-capture-templates
(append
'(("j" "Journal")
("jt" "Task" entry
(function (lambda () (ss-journal-capture-target "Tasks")))
"* TODO %?")
("jn" "Note" entry
(function (lambda () (ss-journal-capture-target "Notes")))
"* %?")
("jm" "Meeting" entry
(function (lambda () (ss-journal-capture-target "Meetings")))
"* <%<%Y-%m-%d %H:%M>> %?"))
(ss-capture--denote-templates))))
(provide 'ss-capture)
;;; ss-capture.el ends here
|