/
org-heading-checkbox.el
125 lines (101 loc) · 3.79 KB
/
org-heading-checkbox.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
;;; org-heading-checkbox.el --- A minor mode to fake checkboxes in org-headings. -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Arthur Miller
;; Author: Arthur Miller <arthur.miller@live.com>
;; Keywords:
;; 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:
;;
;;; Code:
(defvar ohc--enabled-re "^[ \t]*\\*+.*?[ \t]*\\[x\\]")
(defvar ohc--disabled-re "^[ \t]*\\*+.*?[ \t]*\\[ \\]")
(defvar ohc--checkbox-re "^[ \t]*\\*+.*?\\[[ x]\\]")
(defvar org-heading-checkbox-enabled-hooks nil)
(defvar org-heading-checkbox-disabled-hooks nil)
(defvar ohc--mode-on nil)
(defun ohc--mode-on ()
(setq ohc--mode-on t))
(defun ohc--mode-off ()
(setq ohc--mode-on nil))
(defun ohc--heading-checkbox-p ()
"Return t if this is a heading with a checkbox."
(save-excursion
(beginning-of-line)
(looking-at ohc--checkbox-re)))
(defun ohc--checkbox-enabled-p ()
"Return t if point is at a heading with an enabed checkbox."
(save-excursion
(beginning-of-line)
(looking-at "^[ \t]*\\*+.*?\\[x\\]")))
(defun ohc--checkbox-disabled-p ()
"Return t if point is at a heading with a disabeled checkbox."
(save-excursion
(beginning-of-line)
(looking-at "^[ \t]*\\*+.*?\\[ \\]")))
(defun ohc--checkbox-enable ()
"Disable checkbox for heading at point."
(interactive)
(when (ohc--checkbox-enabled-p)
(save-excursion
(beginning-of-line)
(when (re-search-forward "\\[ \\]" (line-end-position) t 1)
(replace-match "[x]")
(run-hooks 'org-heading-checkbox-enabled-hooks)))))
(defun ohc--checkbox-disable ()
"Disable checkbox for heading at point."
(interactive)
(when (ohc--checkbox-enabled-p)
(save-excursion
(beginning-of-line)
(when (re-search-forward "\\[x\\]" (line-end-position) t 1)
(replace-match "[ ]")
(run-hooks 'org-heading-disabled-hooks)))))
(defun ohc--checkbox-toggle ()
"Toggle state of checkbox at heading under the point."
(interactive)
(save-excursion
(beginning-of-line)
(cond ((looking-at ohc--enabled-re)
(when (re-search-forward "\\[x\\]" (line-end-position) t 1)
(replace-match "[ ]")
(run-hooks 'org-heading-checkbox-disabled-hooks)))
((looking-at ohc--disabled-re)
(when (re-search-forward "\\[ \\]" (line-end-position) t 1)
(replace-match "[x]")
(run-hooks 'org-heading-checkbox-enabled-hooks)))
(t (error "Not at org-heading-checkbox line.")))))
(defun ohc--shiftup ()
(interactive)
(if (ohc--heading-checkbox-p)
(ohc--checkbox-toggle)
(org-shiftup)))
(defun ohc--shiftdown ()
(interactive)
(if (ohc--heading-checkbox-p)
(ohc--checkbox-toggle)
(org-shiftdown)))
(defvar org-heading-checkbox-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [S-up] #'ohc--shiftup)
(define-key map [S-down] #'ohc--shiftdown)
map)
"Keymap used in `org-heading-checkbox-mode'.")
;;;###autoload
(define-minor-mode org-heading-checkbox-mode
""
:global nil :lighter nil
(unless (derived-mode-p 'org-mode)
(error "Not in org-mode."))
(cond (org-heading-checkbox-mode
(ohc--mode-on))
(t (ohc--mode-off))))
(provide 'org-heading-checkbox)
;;; org-heading-checkbox.el ends here