-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreorg-dynamic-bullets.el
329 lines (278 loc) · 10.8 KB
/
reorg-dynamic-bullets.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
;;; reorg-dynamic-bullets.el --- Orgmode dynamic bullets -*- lexical-binding: t; -*-
;;; Code:
;;;; Requirements
(require 'org)
(require 'org-element)
(require 'outline)
;;;; Faces
(defface reorg-dynamic-bullets-face '((t (:foreground "gray")))
"Bullet face"
:group 'reorg-dynamic-bullets)
;; (defface reorg-dynamic-bullets-clone-face '((t (:foreground "red")))
;; "Clone face"
;; :group 'reorg-dynamic-bullets)
;;;; Customization
(defcustom reorg-dynamic-bullets-update-triggers
'((outline-cycle . reorg-dynamic-bullets--org-cycle-hook-func))
"Alist in the form '((hook-or-function . refresh-function)))
where refresh-function is added as a hook or advice to hook-or-function.
Refresh function should be one of the `reorg-dynamic-bullets--fontify' functions
below."
:type 'alist
:group 'reorg-dynamic-bullets)
(defcustom reorg-dynamic-bullets-folded-body-text-bullet "▶"
"Bullet for a folded node with text in its body."
:type 'string
:group 'reorg-dynamic-bullets)
(defcustom reorg-dynamic-bullets-folded-no-body-text-bullet "▷"
"Bullet for folded node with no text in its body."
:type 'string
:group 'reorg-dynamic-bullets)
(defcustom reorg-dynamic-bullets-unfolded-body-text-bullet "▼"
"Bullet for an unfolded node with text in its body."
:type 'string
:group 'reorg-dynamic-bullets)
(defcustom reorg-dynamic-bullets-unfolded-no-body-text-bullet "▽"
"Bullet for an unfolded node with no text in its body."
:type 'string
:group 'reorg-dynamic-bullets)
(defcustom reorg-dynamic-bullets-leaf-body-text-bullet "▬"
"Bullet for a leaf node with body text."
:type 'string
:group 'reorg-dynamic-bullets)
(defcustom reorg-dynamic-bullets-leaf-no-body-text-bullet "▭"
"Bullet for a leaf node with no body text."
:type 'string
:group 'reorg-dynamic-bullets)
(defcustom reorg-dynamic-bullets-refresh-func
#'reorg-dynamic-bullets--refresh-with-text-props
"Function to refresh bullets. Must take
two arguments: BEG and END representing the region
to be refreshed. Two options are:
`reorg-dynamic-bullets--refresh-with-compose-region',
`reorg-dynamic-bullets--refresh-with-text-props', or
`reorg-dynamic-bullets--refresh-with-font-lock'."
:type 'function
:group 'reorg-dynamic-bullets)
;;;; Variables
(defvar reorg-dynamic-bullets--idle-timer nil
"Idle timer to update heading bullets.")
;;;; Constants
(defconst reorg-dynamic-bullets--heading-re "^\\(?1:\\*+\\) "
"Outline heading regexp.")
;; (defconst reorg-dynamic-bullets--font-lock-keyword
;; `((,reorg-dynamic-bullets--heading-re
;; (1 (list 'face 'reorg-dynamic-bullets-face
;; 'display (reorg-dynamic-bullets--create-heading-bullet)))))
;; "Font-lock keyword to fontify heading stars.")
(defconst reorg-dynamic-bullets--font-lock-keyword
`((,reorg-dynamic-bullets--heading-re
(1 (progn (compose-region (match-beginning 1) (match-end 1)
(reorg-dynamic-bullets--create-heading-bullet))
nil))))
"Font-lock keyword to fontify heading stars.")
;;;; Minor mode
(define-minor-mode reorg-dynamic-bullets-mode
"Display orgmode trees."
nil
" dbullets"
nil
(if reorg-dynamic-bullets-mode
(progn
(reorg-dynamic-bullets--add-all-hooks-and-advice)
;;(cl-pushnew 'display font-lock-extra-managed-props)
;;(font-lock-add-keywords nil reorg-dynamic-bullets--font-lock-keyword)
(reorg-dynamic-bullets--fontify-buffer))
;; (setq reorg-dynamic-bullets--idle-timer
;; (run-with-idle-timer 1 'repeat #'reorg-dynamic-bullets--fontify-heading)))
;; (font-lock-remove-keywords nil reorg-dynamic-bullets--font-lock-keyword)
(reorg-dynamic-bullets--add-all-hooks-and-advice 'remove)
(reorg-dynamic-bullets--fontify (point-min) (point-max) 'remove)
;; (cancel-timer reorg-dynamic-bullets--idle-timer)
;; (font-lock-flush (point-min) (point-max))
;; (font-lock-ensure (point-min) (point-max))
))
;;;; Functions
;;;;; Outline position predicates
(defun reorg-dynamic-bullets--heading-folded-p ()
"Is the current heading folded?"
(and (reorg--get-prop 'reorg-branch)
(not
(reorg--get-next-visible-child))))
;;;;; Creating prefix strings
(defun reorg-dynamic-bullets--children-p ()
"Does the current heading have children?"
(reorg--get-next-child))
;; (defun reorg-dynamic-bullets--create-heading-bullet ()
;; "Create a string to be displayed in lieu of the headings' leading stars."
;; (let ((folded (reorg-dynamic-bullets--heading-folded-p))
;; (children (reorg--get-next-child))
;; (body (reorg--get-prop 'body)))
;; (propertize
;; (cond ((and children folded body)
;; reorg-dynamic-bullets-folded-body-text-bullet)
;; ((and children folded)
;; reorg-dynamic-bullets-folded-no-body-text-bullet)
;; ((and children body)
;; reorg-dynamic-bullets-unfolded-body-text-bullet)
;; (children
;; reorg-dynamic-bullets-unfolded-no-body-text-bullet)
;; (body
;; reorg-dynamic-bullets-leaf-body-text-bullet)
;; (t
;; reorg-dynamic-bullets-leaf-no-body-text-bullet))
;; 'face
;; 'reorg-dynamic-bullets-face)))
(defun reorg-dynamic-bullets--create-heading-bullet ()
"Create a string to be displayed in lieu of the headings' leading stars."
(let (;;(branch (reorg--get-prop 'reorg-branch))
(branch (and (reorg--get-prop 'reorg-branch)
(reorg--get-next-child)))
(folded (reorg-dynamic-bullets--heading-folded-p))
(body nil))
;; (body (reorg--get-prop 'body)))
(propertize
(cond ((and branch folded body)
(or (reorg--get-prop 'folded-bullet)
(reorg--get-prop 'bullet)
reorg-dynamic-bullets-folded-body-text-bullet))
((and branch folded)
(or (reorg--get-prop 'folded-bullet)
(reorg--get-prop 'bullet)
reorg-dynamic-bullets-folded-no-body-text-bullet))
((and branch body)
(or (reorg--get-prop 'unfolded-bullet)
(reorg--get-prop 'bullet)
reorg-dynamic-bullets-unfolded-no-body-text-bullet))
(branch
(or (reorg--get-prop 'unfolded-bullet)
(reorg--get-prop 'bullet)
reorg-dynamic-bullets-unfolded-no-body-text-bullet))
(body
(or (reorg--get-prop 'leaf-bullet)
(reorg--get-prop 'bullet)
reorg-dynamic-bullets-leaf-body-text-bullet))
(t
(or (reorg--get-prop 'leaf-bullet)
(reorg--get-prop 'bullet)
reorg-dynamic-bullets-leaf-no-body-text-bullet)))
'face
'reorg-dynamic-bullets-face)))
;;;;; Refreshing display
(defun reorg-dynamic-bullets--refresh-with-compose-region (beg end &optional remove)
"refresh with compose region"
(decompose-region beg end)
(compose-region beg end (reorg-dynamic-bullets--create-heading-bullet)))
(defun reorg-dynamic-bullets--refresh-with-text-props (beg end &optional remove)
"Refresh all bullets from BEG to END."
;; This appears to a bit faster than using
;; (font-lock-fontify-region beg end).
(remove-text-properties beg end '(display nil))
(unless remove
(put-text-property beg
end
'display
(reorg-dynamic-bullets--create-heading-bullet))))
(defun reorg-dynamic-bullets--refresh-with-font-lock (beg end &optional remove)
;; NOTE: REMOVE is not used right now. But it is used for
;; `reorg-dynamic-bullets--refresh-with-text-props', above.
"Refresh all bullets from BEG to END using
`font-lock-fontify-region'."
(font-lock-fontify-region beg end))
(defun reorg-dynamic-bullets--fontify (beg end &optional remove)
"Fontify only the leading stars from BEG to END.
All fontifying functions use this function as their base.
This function searches the region for the headline regexp and calls
`reorg-dynamic-bullets-refresh-func' to act on the matches."
(let ((search-invisible nil))
(save-match-data
(save-excursion
(goto-char beg)
(while (and (let ((b (point))
(e (when (re-search-forward
reorg-dynamic-bullets--heading-re
(point-at-eol)
t)
(match-end 1))))
(when (and b e)
(funcall reorg-dynamic-bullets-refresh-func
b e))
(reorg--goto-next-visible-heading))
(< (point) end)))))
(run-hooks 'reorg--navigation-hook)))
(defun reorg-dynamic-bullets--fontify-buffer (&rest _)
"Fontify the entire buffer."
(reorg-dynamic-bullets--fontify (point-min) (point-max)))
(defun reorg-dynamic-bullets--fontify-tree (&rest _)
"Fontify the entire tree from root to last leaf."
(when-let* ((beg (point))
;; (if (= 1 level)
;; (progn (beginning-of-line)
;; (point))
;; (reorg--get-root))
(end (or (reorg--get-next-sibling)
(reorg--get-next-parent)
(point-max))))
(reorg-dynamic-bullets--fontify beg end)))
(defun reorg-dynamic-bullets--fontify-heading (&rest _)
"Fontify the current heading only."
(save-excursion
(reorg-dynamic-bullets--fontify (point-at-bol)
(point-at-eol))))
;; (defun reorg-dynamic-bullets--fontify-heading-and-previous-sibling (&rest _)
;; "Fontify the current heading and previous sibling."
;; (let ((beg (save-excursion (or (outline-get-last-sibling)
;; (outline-previous-heading))
;; (point)))
;; (end (line-beginning-position 2)))
;; (reorg-dynamic-bullets--fontify beg end)))
(defun reorg-dynamic-bullets--fontify-heading-and-parent (&rest _)
"Fontify the current heading only."
(let ((beg (reorg--get-parent))
(end (reorg--get-next-visibile-heading)))
(reorg-dynamic-bullets--fontify beg end)))
;; (defun reorg-dynamic-bullets--fontify-children (&rest _)
;; "Fontify current heading to last child."
;; (save-excursion
;; (when (outline-back-to-heading)
;; (let ((beg (point-at-bol))
;; (end (save-excursion
;; (outline-end-of-subtree)
;; (point))))
;; (reorg-dynamic-bullets--fontify beg end)))))
(defun reorg-dynamic-bullets--org-cycle-hook-func ()
"Called after `org-cyle'."
(reorg-dynamic-bullets--fontify-tree))
;;;; Hooks and advice
(defun reorg-dynamic-bullets--add-hook-or-advice (hook-or-func
func
&optional remove)
"If HOOK-OR-FUNC is a hook, add FUNC as a local hook.
If HOOK-OF-FUNC is a function, add FUNC as advice after HOOK-OR-FUNC.
if REMOVE is non-nil, remove the hook or advice."
(pcase hook-or-func
((pred (functionp))
(if remove
(advice-remove hook-or-func func)
(advice-add hook-or-func :after func)))
((pred (lambda (sym)
(let ((name (symbol-name sym)))
(and (>= (length name) 6)
(string=
"-hook"
(substring name -5 (length name)))))))
(if remove
(remove-hook hook-or-func func t)
(add-hook hook-or-func func nil t)))))
(defun reorg-dynamic-bullets--add-all-hooks-and-advice (&optional remove)
"Add hooks and advice to all members of
`reorg-dynamic-bullets-update-triggers.'"
(cl-loop for (hook-or-func . func) in reorg-dynamic-bullets-update-triggers
when (and hook-or-func func)
do (reorg-dynamic-bullets--add-hook-or-advice
hook-or-func
func
remove)))
;;;; Footer
(provide 'reorg-dynamic-bullets)
;;; reorg-dynamic-bullets.el ends here