This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
org-themis.el
327 lines (266 loc) · 10.5 KB
/
org-themis.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
;;; org-themis.el --- Experimental project management mode for org-mode
;; Copyright (C) 2015 Zachary Elliott
;;
;; Authors: Zachary Elliott <[email protected]>
;; Maintainer: Zachary Elliott <[email protected]>
;; URL: http:/zellio/org-themis
;; Created: 2015-11-20
;; Version: 0.4.1
;; Keywords: org-mode, elisp, project
;; Package-Requires: ((cl-lib "0.4"))
;; This file is not part of GNU Emacs.
;;; Commentary:
;;
;;; License:
;; 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 GNU Emacs; see the file COPYING. If not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
;; USA.
;;; Code:
(require 'cl-lib)
(defconst org-themis-version "0.4.1"
"`org-themis' version.")
(defgroup org-themis nil
"`org-themis' application group."
:group 'applications
:link '(url-link :tag "Website for org-themis"
"https:/zellio/org-themis")
:prefix "org-themis-")
(defcustom org-themis-project-root (expand-file-name "projects" "~")
"Parent directory for `org-themis' projects.
This is used for automated project generation in
`org-themis-add-project' and `org-themis-generate-project'
methods."
:type 'string
:group 'org-themis)
(defcustom org-themis-data-file
(expand-file-name ".org-themis.dat" org-themis-project-root)
"`org-themis' data file.
The file contains the internal configurations for `org-themis' as
well as the project database. I would recommend either putting it
in a place where it is backed up or NOT EDITING IT BY HAND."
:type 'string
:group 'org-themis)
(defun org-themis--load-data ()
"Unmarshal operational data from the `org-themis-data-file'."
(if (file-exists-p org-themis-data-file)
(with-temp-buffer
(insert-file-contents org-themis-data-file)
(read (current-buffer)))
'()))
(defun org-themis--save-data ()
"Marshal operational data to the `org-themis-data-file'."
(with-temp-buffer
(insert
";; -*- mode: lisp; coding: utf-8 -*-\n"
";; org-themis data file\n"
";; DO NOT EDIT THIS FILE BY HAND, IT WILL BREAK\n"
"\n"
"((meta\n")
(dolist (arg org-themis-meta-alist)
(insert " " (prin1-to-string arg) "\n"))
(delete-char -1)
(insert
")\n"
" (projects")
(dolist (project org-themis-project-alist)
(newline)
(insert " (" (prin1-to-string (car project)) "\n")
(dolist (arg (cdr project))
(insert " " (prin1-to-string arg) "\n"))
(delete-char -1)
(insert ")"))
(insert "))")
(write-file org-themis-data-file nil)))
(defvar org-themis-data (org-themis--load-data)
"Internal operational data-structure for `org-themis'.")
(defvar org-themis-meta-alist
(cdr (assoc 'meta org-themis-data))
"Settings alist derived from `org-themis-data'.")
(defun org-themis--get-meta-value (label)
"Get, by LABEL, a VALUE from the `org-themis-meta-alist'."
(cdr (assoc label org-themis-meta-alist)))
(defun org-themis--set-meta-value (label value)
"Set, by LABEL, a VALUE from the `org-themis-meta-alist'."
(let ((meta-pair (assoc label org-themis-meta-alist)))
(if meta-pair
(setcdr meta-pair value)
(setq
org-themis-meta-alist
(push (cons label value) org-themis-meta-alist)))))
(defvar org-themis-project-alist
(cdr (assoc 'projects org-themis-data))
"Project alist derived from `org-themis-data'.")
(defvar org-themis-project-data nil
"Operational data for the current `org-themis' project.
Set the `org-themis--add-project' method for more information
what that data looks like.")
(defvar org-themis-project
(let* ((project (org-themis--get-meta-value 'current-project))
(data (assoc project org-themis-project-alist)))
(when (and project data)
(setq org-themis-project project)
(setq org-themis-project-data data))
project)
"Symbolic name of current `org-themis' project.")
(dolist (tag '(name root scratch journal))
(eval
`(defun ,(intern (format "org-themis--project-%s" tag)) ()
,(format "Auto generated accessor method for the %s value." tag)
(cdr (assoc (quote ,tag) (cdr org-themis-project-data))))
))
(defun org-themis--list-projects ()
"List of known `org-themis' projects.
This value is not cached."
(mapcar #'car org-themis-project-alist))
(defun org-themis--completing-read-project-selector ()
"Completing read from the `org-themis' project list.
Value is interned and returned as a symbol."
(list
(intern
(completing-read "Project name: " (org-themis--list-projects)))))
(defun org-themis--set-project (name data)
"Set the current `org-themis' project NAME and DATA.
This method does not validate that it has been passed a valid
project. It just sets the values directly and updates the
`org-themis-meta-alist' value."
(setq org-themis-project name)
(setq org-themis-project-data data)
(org-themis--set-meta-value 'current-project name))
(defun org-themis-set-project-interactive (name)
"Set the current `org-themis' project to valid project.
Uses list of projects for completing read call to set the current
project. This updates the `org-themis-project' and
`org-themis-project-data'. This function validates the NAME
value and then uses the `org-themis--set-project' method.
Example:
(org-themis-set-project-interactive NAME)"
(interactive (org-themis--completing-read-project-selector))
(let ((data (assoc name org-themis-project-alist)))
(if data
(org-themis--set-project name data)
(error "No project named %s found" name))))
(defun org-themis-set-project ()
"Wrapper around the `org-themis-set-project-interactive' method.
This method exists so that `org-themis-mode-map' has pretty
names. This method sets the project, updates the minor mode
lighter, and writes the `org-themis-data-file'."
(interactive)
(command-execute 'org-themis-set-project-interactive)
(org-themis--update-minor-mode-lighter)
(org-themis--save-data))
(defun org-themis--add-project (name root scratch journal)
"Add a project alist to the `org-themis-project-alist'."
(push
(cons name
(list
(cons 'name (symbol-name name))
(cons 'root root)
(cons 'scratch scratch)
(cons 'journal journal)))
org-themis-project-alist))
(defun org-themis-add-project-interactive (name)
"Add a valid project alist the `org-themis-alist'.
This methods ensures that NAME is not already a project.
Additionally it generates default values.
Defaults:
ROOT: Defaults to `org-themis-project-root'/NAME
SCRATCH: scratch.org
JOURNAL: journal.org"
(interactive "SProject Name: ")
(if (null (assoc name org-themis-project-alist))
(org-themis--add-project
name
(expand-file-name (symbol-name name) org-themis-project-root)
"scratch.org"
"journal.org")
(error "Name %s already in use" name)))
(defun org-themis-add-project ()
"Wrapper around the `org-themis-add-project-interactive' method.
This method exists so that `org-themis-mode-map' has pretty
names. This method adds a new project and writes the
`org-themis-data-file'."
(interactive)
(command-execute 'org-themis-add-project-interactive)
(org-themis--save-data))
(defun org-themis--remove-project (name)
"Remove a project alist by NAME from `org-themis-project-alist'."
(setq
org-themis-project-alist
(cl-remove-if
(lambda (x) (eql name (car x)))
org-themis-project-alist)))
(defun org-themis-remove-project-interactive (name)
"Remove a project alist by NAME from `org-themis-project-alist'."
(interactive (org-themis--completing-read-project-selector))
(org-themis--remove-project name))
(defun org-themis-remove-project ()
"Wrapper around `org-themis-remove-project-interactive'.
This method exists so that `org-themis-mode-map' has pretty
names. This method removes a project and writes the
`org-themis-data-file'."
(interactive)
(command-execute 'org-themis-remove-project-interactive)
(org-themis--save-data))
(defun org-themis-find-project-file (&optional file)
"Run the find-file function within an `org-themis' project.
If the value FILE is provided it is assumed to be located at the
current project root. Otherwise a file is searched for starting
at the current project root."
(interactive)
(let ((default-directory (format "%s/" (org-themis--project-root))))
(if file
(find-file (expand-file-name file default-directory))
(command-execute 'find-file))))
(defun org-themis-find-project-root ()
"Method open the current project root."
(interactive)
(org-themis-find-project-file "."))
(defun org-themis-find-project-journal ()
"Method open the current project journal."
(interactive)
(org-themis-find-project-file (org-themis--project-journal)))
(defun org-themis-find-project-scratch ()
"Method open the current project scratch."
(interactive)
(org-themis-find-project-file (org-themis--project-scratch)))
(defvar org-themis-minor-mode-lighter
(if org-themis-project
(format " ot:%s" org-themis-project)
" ot")
"Current `org-themis' minor mode lighter.
Facilitates tracking and auotmatic updating of the lighter for
project changes.")
(defun org-themis--update-minor-mode-lighter ()
"Update the minor mode lighter to reflect the current project."
(let ((lighter (if org-themis-project
(format " ot:%s" org-themis-project)
" ot")))
(setq org-themis-minor-mode-lighter lighter)
(setcdr
(assoc 'org-themis-mode minor-mode-alist)
(list lighter))))
(defvar org-themis-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c /") 'org-themis-find-project-file)
(define-key map (kbd "C-c !") 'org-themis-set-project)
(define-key map (kbd "C-c +") 'org-themis-add-project)
(define-key map (kbd "C-c -") 'org-themis-remove-project)
map)
"Keymap for `org-themis' minor mode.")
;;;###autoload
(define-minor-mode org-themis-mode
"Experimental project management mode for `org-mode'"
:lighter org-themis-minor-mode-lighter
:keymap org-themis-mode-map
:group 'org-themis)
(provide 'org-themis)
;;; org-themis.el ends here