-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathorg-tidy.el
398 lines (340 loc) · 13.2 KB
/
org-tidy.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
388
389
390
391
392
393
394
395
396
397
398
;;; org-tidy.el --- A minor mode to tidy org-mode buffers -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Xuqing Jia
;; Author: Xuqing Jia <jxq@jxq.me>
;; URL: https://github.com/jxq0/org-tidy
;; Version: 0.1
;; Package-Requires: ((emacs "27.1") (dash "2.19.1"))
;; Keywords: convenience, org
;;; 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/>.
;;; Commentary:
;; A minor mode to tidy org-mode buffers.
(require 'org)
(require 'org-element)
(require 'dash)
(eval-when-compile
(require 'org))
(defun org-tidy--get-element-pos (element type)
"Get position of TYPE from ELEMENT based on org version."
(if (version< org-version "9.7")
;; 老版本使用 property
(org-element-property type element)
;; 新版本使用函数
(pcase type
(:begin (org-element-begin element))
(:end (org-element-end element))
(_ (org-element-property type element)))))
;;; Code:
(defgroup org-tidy nil
"A minor mode to tidy `org-mode' buffers."
:prefix "org-tidy-"
:group 'convenience)
(defcustom org-tidy-properties-style 'inline
"How to tidy property drawers."
:group 'org-tidy
:type '(choice
(const :tag "Show fringe bitmap" fringe)
(const :tag "Show inline symbol" inline)
(const :tag "Completely invisible" invisible)))
(defcustom org-tidy-top-property-style 'invisible
"How to tidy the topmost property drawer."
:group 'org-tidy
:type '(choice
(const :tag "Completely invisible" invisible)
(const :tag "Keep" keep)))
(defcustom org-tidy-properties-inline-symbol "♯"
"The inline symbol."
:group 'org-tidy
:type 'string)
(defcustom org-tidy-property-drawer-flag t
"Non-nil means should tidy property drawers."
:group 'org-tidy
:type '(choice
(const :tag "Tidy property drawers" t)
(const :tag "Keep property drawers" nil)))
(defcustom org-tidy-property-drawer-property-whitelist ()
"Whitelist of properties.
If set, only property drawers which contain property in this list
will be tidied."
:group 'org-tidy
:type '(repeat string))
(defcustom org-tidy-property-drawer-property-blacklist ()
"Blacklist of properties.
If set, property drawers which contain property in this list
will not be tidied."
:group 'org-tidy
:type '(repeat string))
(defcustom org-tidy-general-drawer-flag t
"Non-nil means should tidy general drawers."
:group 'org-tidy
:type '(choice
(const :tag "Tidy general drawers" t)
(const :tag "Keep general drawers" nil)))
(defcustom org-tidy-general-drawer-name-whitelist ()
"Whitelist of drawer names.
If set, only general drawers whose name is in this list
will be tidied."
:group 'org-tidy
:type '(repeat string))
(defcustom org-tidy-general-drawer-name-blacklist ()
"Blacklist of drawer names.
If set, general drawers whose name is in this list
will not be tidied."
:group 'org-tidy
:type '(repeat string))
(defcustom org-tidy-protect-overlay t
"If non-nil, org-tidy will protect the overlay by changing local-map."
:group 'org-tidy
:type 'boolean)
(defun org-tidy-protected-text-edit ()
"Keymap to protect property drawers."
(interactive)
(user-error "Property drawer is protected in org-tidy mode"))
(defvar org-tidy-properties-backspace-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "<backspace>") #'org-tidy-protected-text-edit)
map)
"Keymap to protect property drawers.")
(defvar org-tidy-properties-delete-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-d") #'org-tidy-protected-text-edit)
(define-key map (kbd "<deletechar>") #'org-tidy-protected-text-edit)
map)
"Keymap to protect property drawers.")
(defvar-local org-tidy-overlays nil
"Variable to store the regions we put an overlay on.")
(defvar-local org-tidy-toggle-state t
"Variable to control whether this buffer should be tidied.")
(define-fringe-bitmap
'org-tidy-fringe-bitmap-sharp
[#b00100100
#b00100100
#b11111111
#b00100100
#b00100100
#b11111111
#b00100100
#b00100100])
(defun org-tidy-overlay-exists (ovly-beg ovly-end)
"Check whether overlay from OVLY-BEG to OVLY-END exists."
(-filter (lambda (item)
(let* ((ov (plist-get item :ov))
(old-ovly-beg (overlay-start ov))
(old-ovly-end (overlay-end ov)))
(and (= ovly-beg old-ovly-beg)
(>= ovly-end old-ovly-end))))
org-tidy-overlays))
(defun org-tidy-make-protect-ov (backspace-beg backspace-end del-beg del-end)
"Make two read-only overlay: (BACKSPACE-BEG, BACKSPACE-END) (DEL-BEG, DEL-END)."
(let* ((backspace-ov (make-overlay backspace-beg backspace-end nil t t))
(del-ov (make-overlay del-beg del-end nil t nil)))
(overlay-put backspace-ov
'local-map org-tidy-properties-backspace-map)
(overlay-put del-ov
'local-map org-tidy-properties-delete-map)
(push (list :type 'protect :ov backspace-ov) org-tidy-overlays)
(push (list :type 'protect :ov del-ov) org-tidy-overlays)))
(defun org-tidy-property-drawer-has-key-in-list (element check-list)
"Return t if the property drawer ELEMENT contain a key in CHECK-LIST.
Otherwise return nil."
(-let* ((l (cddr element)))
(when-let* ((check-list)
(not-hit t))
(while (and l not-hit)
(-let* ((element (car l))
((type content) element))
(when (eq type 'node-property)
(if (member (plist-get content :key) check-list)
(setq not-hit nil)))
(setq l (cdr l))))
(not not-hit))))
(defun org-tidy-general-drawer-name-in-list (element check-list)
"Return t if the general drawer ELEMENT contain a key in CHECK-LIST.
Otherwise return nil."
(-let* ((content (cadr element))
(drawer-name (plist-get content :drawer-name)))
(if (member drawer-name check-list)
t)))
(defun org-tidy-should-tidy (element)
"Return whether ELEMENT should be tidied."
(-let* ((type (car element)))
(pcase type
('drawer
(and org-tidy-general-drawer-flag
(if org-tidy-general-drawer-name-whitelist
(org-tidy-general-drawer-name-in-list
element
org-tidy-general-drawer-name-whitelist)
(not (org-tidy-general-drawer-name-in-list
element
org-tidy-general-drawer-name-blacklist)))))
('property-drawer
(and org-tidy-property-drawer-flag
(if org-tidy-property-drawer-property-whitelist
(org-tidy-property-drawer-has-key-in-list
element org-tidy-property-drawer-property-whitelist)
(not (org-tidy-property-drawer-has-key-in-list
element
org-tidy-property-drawer-property-blacklist))))))))
(defun org-tidy--element-to-ov (element)
"Turn a single property ELEMENT into a plist for merge."
(let* ((should-tidy (org-tidy-should-tidy element))
(beg (org-tidy--get-element-pos element :begin))
(end (org-tidy--get-element-pos element :end))
(is-top-property (= 1 beg))
(push-ovly nil)
(display nil))
(pcase (list is-top-property org-tidy-top-property-style
org-tidy-properties-style)
(`(t invisible ,_)
(setq display 'empty push-ovly t))
(`(t keep ,_) )
(`(nil ,_ invisible)
(setq display 'empty push-ovly t))
(`(nil ,_ inline)
(setq display 'inline-symbol push-ovly t))
(`(nil ,_ fringe)
(setq display 'fringe push-ovly t)))
(when (and should-tidy push-ovly)
(list :beg beg
:end end
:is-top-property is-top-property
:display display))))
(defun org-tidy--merge-raw-ovs (raw-ovs)
"Merge adjacent RAW-OVS."
(let* ((result nil))
(while raw-ovs
(let* ((curr (car raw-ovs))
(curr-beg (plist-get curr :beg))
(curr-end (plist-get curr :end))
(last (car result))
(last-end (plist-get last :end)))
(if (and last (= curr-beg last-end))
(setf (car result) (plist-put last :end curr-end))
(push curr result)))
(setq raw-ovs (cdr raw-ovs)))
result))
(defun org-tidy--calc-ovly (merged-ovs)
"Calculate overlay and protect regions for MERGED-OVS."
(mapcar (lambda (l)
(-let* (((&plist :beg
:end
:is-top-property) l)
(ovly-beg (if is-top-property 1 (1- beg)))
(ovly-end (if is-top-property end (1- end)))
(backspace-beg (1- end))
(backspace-end end)
(del-beg (max 1 (1- beg)))
(del-end (1+ del-beg)))
(append l (list :ovly-beg ovly-beg
:ovly-end ovly-end
:backspace-beg backspace-beg
:backspace-end backspace-end
:del-beg del-beg
:del-end del-end))))
merged-ovs))
(defun org-tidy--put-overlays (ovs)
"Put overlays from OVS."
(unless (listp ovs)
(signal 'wrong-type-argument (list 'listp ovs)))
(dolist (l ovs)
(-when-let* (((&plist :ovly-beg :ovly-end :display
:backspace-beg :backspace-end
:del-beg :del-end) l)
(valid-pos (and (numberp ovly-beg)
(numberp ovly-end)
(> ovly-end ovly-beg)))
(not-exists (not (org-tidy-overlay-exists ovly-beg ovly-end)))
(ovly (ignore-errors
(make-overlay ovly-beg ovly-end nil t nil))))
(when ovly
(pcase display
('empty (overlay-put ovly 'display ""))
('inline-symbol
(overlay-put ovly 'display
(format " %s" org-tidy-properties-inline-symbol)))
('fringe
(overlay-put ovly 'display
'(left-fringe org-tidy-fringe-bitmap-sharp org-drawer))))
(push (list :type 'property :ov ovly) org-tidy-overlays)
(if org-tidy-protect-overlay
(org-tidy-make-protect-ov backspace-beg backspace-end
del-beg del-end))))))
(defun org-tidy-untidy-buffer ()
"Untidy."
(interactive)
(while org-tidy-overlays
(-let* ((item (pop org-tidy-overlays))
((&plist :type type) item))
(pcase type
('property (delete-overlay (plist-get item :ov)))
('protect (delete-overlay (plist-get item :ov)))
(_ nil)))))
(defun org-tidy-buffer ()
"Tidy."
(interactive)
(save-excursion
(condition-case err
(let* ((ast (org-element-parse-buffer))
(raw-ovs (org-element-map ast
'(property-drawer drawer)
(lambda (element)
(org-tidy--element-to-ov element)))))
(org-tidy--put-overlays
(org-tidy--calc-ovly
(org-tidy--merge-raw-ovs raw-ovs))))
(error
(message "org-tidy error: %S" err)
nil))))
(defun org-tidy-toggle ()
"Toggle between tidy and untidy."
(interactive)
(if org-tidy-toggle-state
(progn (setq org-tidy-toggle-state nil)
(org-tidy-untidy-buffer))
(progn (setq org-tidy-toggle-state t)
(org-tidy-buffer))))
(defun org-tidy-on-save ()
"Tidy buffer on save if `org-tidy-toggle-state' is t."
(interactive)
(if org-tidy-toggle-state (org-tidy-buffer)))
(defconst org-tidy-min-org-version "9.3"
"Minimum supported Org mode version.")
(defun org-tidy--check-compatibility ()
"Check if current Org mode version is compatible."
(when (version< org-version org-tidy-min-org-version)
(display-warning 'org-tidy
(format "org-tidy requires Org mode %s or later"
org-tidy-min-org-version))))
;;;###autoload
(define-minor-mode org-tidy-mode
"Automatically tidy org mode buffers."
:global nil
:group 'org-tidy
(when org-tidy-mode
(org-tidy--check-compatibility))
(if org-tidy-mode
(progn
(if (eq org-tidy-properties-style 'fringe)
(let* ((width 10))
(setq left-fringe-width width)
(set-window-fringes nil width)))
(org-tidy-buffer)
(add-hook 'before-save-hook #'org-tidy-on-save nil t))
(progn
(if (eq org-tidy-properties-style 'fringe)
(progn (setq left-fringe-width nil)
(set-window-fringes nil nil)))
(org-tidy-untidy-buffer)
(remove-hook 'before-save-hook #'org-tidy-on-save t))))
(provide 'org-tidy)
;;; org-tidy.el ends here