This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
org-zk-keywords.el
130 lines (115 loc) · 3.73 KB
/
org-zk-keywords.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
(require 'rx)
(defvar org-zk-keywords-re
(rx bol "#" "+" (group (+ (or alpha "_" "-"))) ":" (* blank) (group (+ any)) eol))
(defun org-zk-keywords-get (key)
"Get the value for keyword KEY of the current file."
(let (res)
(save-match-data
(save-excursion
(goto-char (point-min))
(while (and (null res) (looking-at org-zk-keywords-re))
(if (string-equal (match-string 1) key)
(setq res (match-string 2)))
(forward-line))))
res))
(defun org-zk-keywords-set-or-add (key value)
"Sets the value of keyword KEY if it already exists,
if not, adds an entry at the end of the files keywords."
(let (found)
(save-match-data
(save-excursion
(goto-char (point-min))
(while (looking-at org-zk-keywords-re)
(if (string-equal (match-string 1) key)
(progn
(goto-char (match-beginning 2))
(kill-line)
(insert value)
(setq found t))
(forward-line)))
(unless found
(insert (format "#+%s: %s\n" key value)))))))
(defun org-zk-keywords--parse-list (value)
(split-string value " " t))
(defun org-zk-keywords--format-list (list)
(mapconcat #'identity list " "))
(defun org-zk-sort-strings (list)
"Sort a list of strings"
(sort list (lambda (a b) (string< a b))))
(defun org-zk-keywords-list-add (key element)
"Add element to a list-keyword KEY if it already exists,
if not, adds an entry at the end of the files keywords."
(let (found)
(save-match-data
(save-excursion
(goto-char (point-min))
(while (looking-at org-zk-keywords-re)
(if (string-equal (match-string 1) key)
(let* ((beg (match-beginning 2))
(list-old (org-zk-keywords--parse-list (match-string 2)))
(list-new
(org-zk-sort-strings
(remove-duplicates
(cons element list-old)
:test #'string=))))
(progn
(goto-char beg)
(kill-line)
(insert (org-zk-keywords--format-list list-new))
(setq found t)))
(forward-line)))
(unless found
(insert (format "#+%s: %s\n" key element)))))))
(defun org-zk-keywords-add (key value)
"Adds a keyword #+KEY: VALUE at the end of the files keywords."
(save-match-data
(save-excursion
(goto-char (point-min))
(while (looking-at org-zk-keywords-re)
(forward-line))
(insert (format "#+%s: %s\n" key value)))))
(defun org-zk-keywords-delete (key)
"Deletes all keywords with KEY.
Returns T if at least one keyword was deleted, NIL if not."
(let (found)
(save-match-data
(save-excursion
(goto-char (point-min))
(while (looking-at org-zk-keywords-re)
(if (string-equal (match-string 1) key)
(progn (kill-line)
(setq found t))
(forward-line)))
found))))
(defmacro org-zk-def-keyword (key values)
"Macro for registering keyword-setting functions"
(let ((name
(->>
(downcase key)
(replace-regexp-in-string "_" "-")
(format "org-zk-set-%s")
intern)))
`(defun ,name (value)
(interactive
(list (ivy-completing-read
(format "%s: " ,key)
,values)))
(org-zk-keywords-set-or-add ,key value)
)))
;; TODO: Use the same as (org-todo-keywords)
(org-zk-def-keyword
"GTD_STATE"
'("active"
"someday"
"planning"
"cancelled"
"done"))
(org-zk-def-keyword
"STABILITY"
'("stable"
"wip"))
(org-zk-def-keyword
"FC_STATE"
'("suspended"
"active"))
(provide 'org-zk-keywords)