-
Notifications
You must be signed in to change notification settings - Fork 8
/
org-evil-list.el
146 lines (121 loc) · 4.85 KB
/
org-evil-list.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
;;; org-evil-list.el --- org-evil list manipulation.
;; Copyright (C) 2017-2019 Ben Moon
;; Author: Ben Moon <software@guiltydolphin.com>
;; 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/>.
;;; Commentary:
;;;
;;; Defines Evil functionality for working with lists in Org-mode.
;;;
;;; Code:
(require 'evil)
(require 'org-evil-core)
(require 'org-list)
(org-evil--define-regional-minor-mode org-evil-list-mode
"Minor mode active when in an Org list."
(org-in-item-p)
:keymap (make-sparse-keymap))
(defun org-evil-list-open-item-above ()
"Insert a new item above the current item and switch to Insert state."
(interactive)
(org-beginning-of-item)
(org-insert-item)
(evil-insert-state 1))
(defun org-evil-list-open-item-or-insert-above (insert)
"With prefix argument INSERT, perform `org-evil-list-open-item-above'.
Otherwise, perform `evil-open-above'."
(interactive "P")
(if insert
(org-evil-list-open-item-above)
(evil-open-above 1)))
(defun org-evil-list-open-item-below ()
"Insert a new item below the current item and switch to Insert state."
(interactive)
(org-end-of-item)
(org-insert-item)
(evil-insert-state 1))
(defun org-evil-list-open-item-or-insert-below (insert)
"With prefix argument INSERT, perform `org-evil-list-open-item-below'.
Otherwise, perform `evil-open-below'."
(interactive "P")
(if insert
(org-evil-list-open-item-below)
(evil-open-below 1)))
(evil-define-motion org-evil-list-beginning-of-item ()
"Move to the beginning of the current item."
:type exclusive
(org-beginning-of-item)
(re-search-forward
(regexp-quote
(org-list-get-bullet (point) (org-list-struct)))))
(evil-define-motion org-evil-list-beginning-of-next-item (count)
"Move to the beginning of the next item.
If optional COUNT is specified then move that many items down."
:type line
(--dotimes (or count 1)
(org-evil--save-point-on-error (org-next-item))
(org-evil-list-beginning-of-item)))
(evil-define-motion org-evil-list-beginning-of-previous-item (count)
"Move to the beginning of the previous item.
If optional COUNT is specified then move that many items up."
:type line
(--dotimes (or count 1)
(org-evil--save-point-on-error (org-previous-item))
(org-evil-list-beginning-of-item)))
(defun org-evil-list--full-item-region (beg end)
"Return the start of the first item touched by BEG and the end of the last item touched by END."
(list (save-excursion (goto-char beg) (org-list-get-item-begin))
(save-excursion (goto-char end) (org-end-of-item))))
(defmacro org-evil-list--with-items-region (beg end &rest body)
"With all items between BEG and END, execute BODY.
The current region is expanded to cover all items between BEG and END.
If BEG or END are NIL, no region is assumed and nothing happens."
(declare (indent 2) (debug t))
`(let ((beg ,beg) (end ,end))
(if (and beg end)
(-let* (((beg end) (org-evil-list--full-item-region beg end)))
(evil-with-active-region beg end ,@body))
,@body)))
(evil-define-operator org-evil-list-outdent-item-tree
(beg end &optional count)
"Outdent the current list item and its children."
:type block
:motion nil
(interactive "<r><c>")
(org-evil-list--with-items-region beg end
(let* ((count (or count 1))
(indenter (if (>= count 0) 'org-outdent-item-tree 'org-indent-item-tree))
(count (abs count)))
(--dotimes count (funcall indenter)))))
(evil-define-operator org-evil-list-indent-item-tree
(beg end &optional count)
"Indent the current list item and its children."
:type block
:motion nil
(interactive "<r><c>")
(org-evil-list--with-items-region beg end
(let* ((count (or count 1))
(indenter (if (>= count 0) 'org-indent-item-tree 'org-outdent-item-tree))
(count (abs count)))
(--dotimes count (funcall indenter)))))
(org-evil--define-key 'motion 'org-evil-list-mode
"(" 'org-evil-list-beginning-of-previous-item
")" 'org-evil-list-beginning-of-next-item
"^" 'org-evil-list-beginning-of-item)
(org-evil--define-key 'normal 'org-evil-list-mode
"<" 'org-evil-list-outdent-item-tree
">" 'org-evil-list-indent-item-tree
"O" 'org-evil-list-open-item-or-insert-above
"o" 'org-evil-list-open-item-or-insert-below)
(provide 'org-evil-list)
;;; org-evil-list.el ends here