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-file-view.el
173 lines (149 loc) · 5.58 KB
/
org-zk-file-view.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
(require 'org-zk-query)
;;; Font Faces
(defface org-zk-file-view-date-face
'((((class color) (background light)) (:foreground "#aaa"))
(((class color) (background dark)) (:foreground "#77a")))
"Face used for dates in the file view."
:group 'org-zk)
(defface org-zk-file-view-title-face
'((((class color) (background light)) (:foreground "#000" :weight bold))
(((class color) (background dark)) (:foreground "#fff" :weight bold)))
"Face used for titles in the file view."
:group 'org-zk)
(defface org-zk-file-view-keyword-face
'((((class color) (background light)) (:foreground "#070"))
(((class color) (background dark)) (:foreground "#0f0")))
"Face used for keywords in the file view."
:group 'org-zk)
(defface org-zk-file-view-collection-face
'((((class color) (background light)) (:foreground "#aa0"))
(((class color) (background dark)) (:foreground "#ff0")))
"Face used for org-zk collections in the file view."
:group 'org-zk)
;;; Tabulated List Mode
(setq org-zk-file-view-format
(vector
'("Collection" 8 t)
'("Created" 10 t)
'("Title" 50 t)))
(defun org-zk-cache-get-keyword (entry kw &optional default)
(alist-get
kw
(plist-get entry :keywords)
default
nil
#'string=))
(defun org-zk-entry-gtd-state (entry)
(alist-get
"GTD_STATE"
(plist-get entry :org-keywords)
org-zk-default-file-state nil 'string=))
;; TODO: This doesn't do anything besides checking for todos
(defun org-zk-project-p (filename entry)
(let* ((keywords (plist-get entry :org-keywords))
(state (alist-get "GTD_STATE" keywords nil nil 'string=)))
(org-zk--has-todos entry)))
(defun org-zk-file-view-tabulate-title (file)
(if (null (plist-get file :keywords))
(propertize
(plist-get file :title)
'face 'org-zk-file-view-title-face)
(concat
(propertize
(plist-get file :title)
'face 'org-zk-file-view-title-face)
" ("
(mapconcat
(lambda (kw) (propertize kw 'face 'org-zk-file-view-keyword-face))
(plist-get file :keywords) ",")
")")))
(defun org-zk-file-view-tabulate (files)
(mapcar
(lambda (file)
(list
file
(vector
(propertize
(plist-get (plist-get file :collection) :name)
'face 'org-zk-file-view-collection-face)
(propertize
(if-let ((created (plist-get file :created))) (subseq created 1 11) "")
'face 'org-zk-file-view-date-face)
(org-zk-file-view-tabulate-title file))))
files))
(defun org-zk-file-view-buffer ()
(get-buffer-create "org-zk File View"))
(defun org-zk-file-view-show (query)
(org-zk-cache-update)
(setq org-zk-file-view-filter query)
(let* ((pred (org-zk-query query org-zk-query-file-predicates))
(files (org-el-cache-select org-zk-cache pred)))
(with-current-buffer (org-zk-file-view-buffer)
(org-zk-file-view-mode)
(setq tabulated-list-format org-zk-file-view-format)
(tabulated-list-init-header)
(setq tabulated-list-entries (org-zk-file-view-tabulate files))
(setq tabulated-list-sort-key nil)
(tabulated-list-print)
(switch-to-buffer (current-buffer)))))
(define-derived-mode org-zk-file-view-mode tabulated-list-mode "org-zk Files"
"Major mode for listing org-zk files"
(hl-line-mode))
(setq org-zk-file-view-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map tabulated-list-mode-map)
(define-key map (kbd "RET") 'org-zk-file-view-open)
(define-key map (kbd "S") 'org-zk-file-view)
(define-key map (kbd "s") 'org-zk-file-view)
(define-key map (kbd "R") 'org-zk-file-view-rename)
(define-key map (kbd "k") 'org-zk-file-view-add-keyword)
(define-key map (kbd "K") 'org-zk-file-view-edit-keywords)
map))
(defun org-zk-file-view-open ()
(interactive)
(let ((file (tabulated-list-get-id)))
(find-file (plist-get file :file))))
(defun org-zk-file-view-edit-keywords ()
"Edit the keywords of the current entry"
(interactive)
(let* ((entry (tabulated-list-get-id))
(kws (split-string
(read-string
"Keywords: "
(mapconcat #'identity (plist-get entry :keywords) " "))
" " t)))
(plist-put entry :keywords kws)
(tabulated-list-set-col "Title" (org-zk-file-view-tabulate-title entry))
(org-zk-set-file-keywords (plist-get entry :file) kws)))
(defun org-zk-file-view-add-keyword ()
"Add a keyword to the current entry"
(interactive)
(let* ((entry (tabulated-list-get-id))
(kw (org-zk-read-keyword))
(kws (org-zk-sort-strings
(remove-duplicates
(cons kw (plist-get entry :keywords))
:test #'string=))))
(plist-put entry :keywords kws)
(tabulated-list-set-col "Title" (org-zk-file-view-tabulate-title entry))
(org-zk-set-file-keywords
(plist-get entry :file) kws)))
(defun org-zk-file-view-rename ()
(interactive)
(let* ((entry (tabulated-list-get-id))
(file (plist-get entry :file))
(title (org-zk-read-title (plist-get entry :title))))
(org-zk-rename-file file title)
(plist-put entry :title title)
(tabulated-list-set-col "Title" (org-zk-file-view-tabulate-title entry))))
(defun org-zk-set-file-keywords (file kws)
(org-zk-in-file file
(org-zk-keywords-set-or-add
"KEYWORDS"
(mapconcat #'identity kws " "))))
(defvar org-zk-file-view-filter ""
"Current filter of the file view")
(defun org-zk-file-view (query)
(interactive (list (read-string "Filter: " org-zk-file-view-filter)))
(org-zk-file-view-show query))
(provide 'org-zk-file-view)