-
Notifications
You must be signed in to change notification settings - Fork 7
/
org-dynamic-bullets.el
387 lines (321 loc) · 12.8 KB
/
org-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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
;;; org-dynamic-bullets.el --- Orgmode dynamic bullets -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Jeff Filipovits
;; Author: Jeff Filipovits <[email protected]>
;; Url: https://github.com/legalnonsense/org-dynamic-bullets
;; Version: 0.0.1
;; Package-Requires: ((emacs "26.1") (org "9.0"))
;;
;; Keywords: Org, outline
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Replace leading stars with icons indicating whether
;; a heading is folded and whether it has text in its
;; body. Provide vertical lines in the left margin
;; indicating the depth of the outline.
;;; 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;;; Installation and usage
;; 1. Put this file in your load-path.
;; 2. (require 'org-dynamic-bullets)
;; 3. (org-dynamic-bullets-mode)
;;;; Tips
;; You can customize settings in the `org-dynamic-bullets' group.
;; This package pairs well with `org-visual-indent'.
;;; 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
;;;; Requirements
(require 'org)
(require 'org-element)
;;;; Faces
(defface org-dynamic-bullets-face '((t (:foreground "gray")))
"Bullet face"
:group 'org-dynamic-bullets)
;;;; Customization
(defcustom org-dynamic-bullets-update-triggers
'((org-cycle-hook . org-dynamic-bullets--org-cycle-hook-func)
(org-insert-heading . org-dynamic-bullets--fontify-heading-and-previous-sibling)
(org-promote . org-dynamic-bullets--fontify-heading-and-previous-sibling)
(org-demote . org-dynamic-bullets--fontify-heading-and-previous-sibling)
(org-promote-subtree . org-dynamic-bullets--fontify-heading-and-previous-sibling)
(org-demote-subtree . org-dynamic-bullets--fontify-heading-and-previous-sibling))
;; TODO: I do not know the most efficient way to ensure
;; bullets are redrawn. This seems to be working
;; and is efficient enough that it does not slow
;; me down. There are a number of other org functions
;; that could potentially be used here:
;;
;; org-show-all
;; org-show-entry
;; org-show-subtree
;; org-show-context
;; org-show-siblings
;; org-show-children
"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 `org-dynamic-bullets--fontify' functions
below."
:type 'alist
:group 'org-dynamic-bullets)
(defcustom org-dynamic-bullets-folded-body-text-bullet "▶"
"Bullet for a folded node with text in its body."
:type 'string
:group 'org-dynamic-bullets)
(defcustom org-dynamic-bullets-folded-no-body-text-bullet "▷"
"Bullet for folded node with no text in its body."
:type 'string
:group 'org-dynamic-bullets)
(defcustom org-dynamic-bullets-unfolded-body-text-bullet "▼"
"Bullet for an unfolded node with text in its body."
:type 'string
:group 'org-dynamic-bullets)
(defcustom org-dynamic-bullets-unfolded-no-body-text-bullet "▽"
"Bullet for an unfolded node with no text in its body."
:type 'string
:group 'org-dynamic-bullets)
(defcustom org-dynamic-bullets-leaf-body-text-bullet "■"
"Bullet for a leaf node with body text."
:type 'string
:group 'org-dynamic-bullets)
(defcustom org-dynamic-bullets-leaf-no-body-text-bullet "□"
"Bullet for a leaf node with no body text."
:type 'string
:group 'org-dynamic-bullets)
(defcustom org-dynamic-bullets-refresh-func
#'org-dynamic-bullets--refresh-with-compose-region
"Function to refresh bullets. Must take
two arguments: BEG and END representing the region
to be refreshed. Two options are:
`org-dynamic-bullets--refresh-with-compose-region',
`org-dynamic-bullets--refresh-with-text-props', or
`org-dynamic-bullets--refresh-with-font-lock'."
:type 'function
:group 'org-dynamic-bullets)
;;;; Variables
(defvar org-dynamic-bullets--idle-timer nil
"Idle timer to update heading bullets.")
;;;; Constants
(defconst org-dynamic-bullets--heading-re "^\\(?1:\\*+\\) "
"Outline heading regexp.")
;; (defconst org-dynamic-bullets--font-lock-keyword
;; `((,org-dynamic-bullets--heading-re
;; (1 (list 'face 'org-dynamic-bullets-face
;; 'display (org-dynamic-bullets--create-heading-bullet)))))
;; "Font-lock keyword to fontify heading stars.")
(defconst org-dynamic-bullets--font-lock-keyword
`((,org-dynamic-bullets--heading-re
(1 (progn (compose-region (match-beginning 1) (match-end 1)
(org-dynamic-bullets--create-heading-bullet))
nil))))
"Font-lock keyword to fontify heading stars.")
;;;; Minor mode
;;;###autoload
(define-minor-mode org-dynamic-bullets-mode
"Display orgmode trees."
nil
" dbullets"
nil
(if org-dynamic-bullets-mode
(progn
(when (fboundp 'org-bullets-mode)
(org-bullets-mode -1))
(when (fboundp 'org-superstar-mode)
(org-superstar-mode -1))
(org-dynamic-bullets--add-all-hooks-and-advice)
(cl-pushnew 'display font-lock-extra-managed-props)
(font-lock-add-keywords nil org-dynamic-bullets--font-lock-keyword)
(org-dynamic-bullets--fontify-buffer)
(setq org-dynamic-bullets--idle-timer
(run-with-idle-timer 1 'repeat #'org-dynamic-bullets--fontify-heading)))
(font-lock-remove-keywords nil org-dynamic-bullets--font-lock-keyword)
(org-dynamic-bullets--add-all-hooks-and-advice 'remove)
(org-with-wide-buffer
(org-dynamic-bullets--fontify (point-min) (point-max) 'remove))
(cancel-timer org-dynamic-bullets--idle-timer)
(font-lock-flush (point-min) (point-max))
(font-lock-ensure (point-min) (point-max))))
;;;; Functions
;;;;; Outline position predicates
(defun org-dynamic-bullets--has-children-p ()
"Does the current node have any children?"
(save-excursion (org-goto-first-child)))
(defun org-dynamic-bullets--heading-folded-p ()
"Is the current heading folded?"
(save-excursion
(org-back-to-heading)
(end-of-line)
(if (fboundp 'org-fold-folded-p)
(org-fold-folded-p)
(outline-invisible-p))))
(defun org-dynamic-bullets--body-p ()
"Does the current heading have text in its body? \"Body\" is
defined as any text, including property drawers, following
the heading and before the next heading."
nil)
;; As of emacs 29 there is a bug here.
;; (cddr (car (org-element--parse-elements
;; (point)
;; (save-excursion (or (outline-next-heading)
;; (point-max)))
;; nil nil nil nil nil))))
;;;;; Creating prefix strings
(defun org-dynamic-bullets--create-heading-bullet ()
"Create a string to be displayed in lieu of the headings' leading stars."
(let ((children (org-dynamic-bullets--has-children-p))
(folded (org-dynamic-bullets--heading-folded-p))
(body (org-dynamic-bullets--body-p)))
(cond ((and children folded body)
org-dynamic-bullets-folded-body-text-bullet)
((and children folded)
org-dynamic-bullets-folded-no-body-text-bullet)
((and children body)
org-dynamic-bullets-unfolded-body-text-bullet)
(children
org-dynamic-bullets-unfolded-no-body-text-bullet)
(body
org-dynamic-bullets-leaf-body-text-bullet)
(t
org-dynamic-bullets-leaf-no-body-text-bullet))))
;;;;; Refreshing display
(defun org-dynamic-bullets--refresh-with-compose-region (beg end &optional remove)
(if remove
(decompose-region beg end)
(compose-region beg end (org-dynamic-bullets--create-heading-bullet))))
(defun org-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
(org-dynamic-bullets--create-heading-bullet))))
(defun org-dynamic-bullets--refresh-with-font-lock (beg end &optional remove)
;; NOTE: REMOVE is not used right now. But it is used for
;; `org-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 org-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
`org-dynamic-bullets-refresh-func' to act on the matches."
(when org-dynamic-bullets-mode
(unless (org-before-first-heading-p)
(org-with-wide-buffer
(save-excursion
(save-match-data
(goto-char beg)
(while
(re-search-forward org-dynamic-bullets--heading-re end t)
(save-excursion
(funcall org-dynamic-bullets-refresh-func
(match-beginning 1)
(match-end 1))))))))))
(defun org-dynamic-bullets--fontify-buffer (&rest _)
"Fontify the entire buffer."
(org-dynamic-bullets--fontify (point-min) (point-max)))
(defun org-dynamic-bullets--fontify-tree (&rest _)
"Fontify the entire tree from root to last leaf."
(when-let* ((level (org-current-level))
(beg (save-excursion (if (= 1 level)
(progn (beginning-of-line)
(point))
(while (org-up-heading-safe))
(point))))
(end (save-excursion (outline-end-of-subtree)
(point))))
(org-dynamic-bullets--fontify beg end)))
(defun org-dynamic-bullets--fontify-heading (&rest _)
"Fontify the current heading only."
(save-excursion
(org-dynamic-bullets--fontify (point-at-bol)
(point-at-eol))))
(defun org-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)))
(org-dynamic-bullets--fontify beg end)))
(defun org-dynamic-bullets--fontify-heading-and-parent (&rest _)
"Fontify the current heading only."
(let ((beg (save-excursion (org-up-heading-safe)
(point)))
(end (save-excursion (org-next-visible-heading 1)
(point))))
(org-dynamic-bullets--fontify beg end)))
(defun org-dynamic-bullets--fontify-children (&rest _)
"Fontify current heading to last child."
(save-excursion
(when (org-back-to-heading)
(let ((beg (point-at-bol))
(end (save-excursion
(outline-end-of-subtree)
(point))))
(org-dynamic-bullets--fontify beg end)))))
(defun org-dynamic-bullets--org-cycle-hook-func (state)
"Called after `org-cyle'."
(pcase state
((or `overview
`contents
`showall)
(org-dynamic-bullets--fontify-buffer))
((or `folded
`children
`subtree)
(org-dynamic-bullets--fontify-tree))
(_ nil)))
;;;; Hooks and advice
(defun org-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 org-dynamic-bullets--add-all-hooks-and-advice (&optional remove)
"Add hooks and advice to all members of
`org-dynamic-bullets-update-triggers.'"
(cl-loop for (hook-or-func . func) in org-dynamic-bullets-update-triggers
when (and hook-or-func func)
do (org-dynamic-bullets--add-hook-or-advice
hook-or-func
func
remove)))
;;;; Footer
(provide 'org-dynamic-bullets)
;;; org-dynamic-bullets.el ends here