-
Notifications
You must be signed in to change notification settings - Fork 8
/
org-evil-motion.el
208 lines (175 loc) · 7.22 KB
/
org-evil-motion.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
;;; org-evil-motion.el --- org-evil general motion.
;; Copyright (C) 2016-2019 Ben Moon
;; Author: Ben Moon <[email protected]>
;; 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 moving around in general Org mode.
;;;
;;; Code:
(require 'dash)
(require 'evil)
(org-evil--define-minor-mode org-evil-motion-mode
"Minor-mode for moving around in Org files."
t
:keymap (make-sparse-keymap))
(defun org-evil-motion--check-in-headings ()
"Signal a user error if not within a heading hierarchy."
(when (org-before-first-heading-p) (user-error "Before first heading")))
(evil-define-motion org-evil-motion-forward-heading
(count)
"Move forward by COUNT headings at the same level (default 1).
If there are no more headings at the same level, attempt to move to
the next higher heading."
(let ((count (or count 1)))
(--dotimes count
(if (and (org-evil-motion--last-heading-same-level-p) (org-evil-motion--heading-has-parent-p))
(if (save-excursion (org-evil-motion-up-heading) (not (org-evil-motion--last-heading-same-level-p)))
(progn (org-evil-motion-up-heading) (org-evil-motion-forward-heading))
(error "No more forward headings"))
(if (not (org-evil-motion--last-heading-same-level-p))
(org-forward-heading-same-level 1)
(error "No more forward headings"))))))
(evil-define-motion org-evil-motion-backward-heading
(count)
"Move backward by COUNT headings at the same level (default 1).
If there are no previous headings at the same level, attempt to move to
the previous higher heading.
Move to the current heading if not on a heading."
(org-evil-motion--check-in-headings)
(let ((count (or count 1)))
(--dotimes count
(if (org-evil-motion--first-heading-same-level-p)
(if (org-evil-motion--heading-has-parent-p)
(org-evil-motion-up-heading)
(if (org-at-heading-p)
(error "Already at first heading")
(org-evil-motion-up-heading)))
(if (not (org-evil-motion--first-heading-same-level-p))
(if (org-at-heading-p)
(org-backward-heading-same-level 1)
(org-evil-motion-up-heading))
(error "No more previous headings"))))))
(defun org-evil-motion--last-heading-same-level-p ()
"Return T if the current heading is the last child of its parents."
(save-excursion
(when (ignore-errors (org-back-to-heading))
(let ((header-point (point)))
(org-forward-heading-same-level 1 t)
(= (point) header-point)))))
(defun org-evil-motion--first-heading-same-level-p ()
"Return T if the current heading is the first child of its parents."
(save-excursion
(ignore-errors (progn (org-back-to-heading) (org-first-sibling-p)))))
(defun org-evil-motion--heading-has-parent-p ()
"Return non-NIL if the current heading has a parent."
(save-excursion (ignore-errors (org-up-heading-safe))))
(evil-define-motion org-evil-motion-up-heading
(count)
"Move up COUNT parent headings.
Jump to the current heading if not already upon it."
:type line
(let ((count (or count 1)))
(unless (org-at-heading-p) (progn (org-back-to-heading) (setq count (1- count))))
(--dotimes count (org-up-heading-all 1))))
(evil-define-motion org-evil-motion-up-heading-top
(count)
"Move up to the COUNTth level parent heading.
Move to the parent-most heading by default.
Move to the current heading if COUNT is greater than the parent level."
:type line
(let ((count (or count 1))
(level (org-current-level)))
(when level
(if (<= level count) (org-back-to-heading)
(org-up-heading-all (- level count))))))
;;; Blocks
(evil-define-motion org-evil-motion-forward-block-begin
(count)
"Move forward to the beginning of the COUNTth next block."
:type line
(--dotimes (or count 1) (org-evil--save-point-on-error (org-next-block 1))))
(evil-define-motion org-evil-motion-backward-block-begin
(count)
"Move backward to the beginning of the COUNTth previous block."
:type line
(--dotimes (or count 1) (org-evil--save-point-on-error (org-previous-block 1))))
(defun org-evil-in-block-p ()
"Non-nil when point belongs to a block."
(let* ((case-fold-search t)
(blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
"^[ \t]*#\\+end_.*")))
blockp))
(org-evil--define-regional-minor-mode org-evil-block-mode
"Minor-mode active when in an Org block."
(org-evil-in-block-p)
:keymap (make-sparse-keymap))
(defun org-evil-block-boundaries ()
"Return the '(START . END) position of the current block."
(or (org-evil-in-block-p) (user-error "Not in a block")))
(defun org-evil-block-beginning-of-block ()
"Go to the beginning of the current block."
(interactive)
(goto-char (car (org-evil-block-boundaries))))
(defun org-evil-block-end-of-block ()
"Go to the end of the current block."
(interactive)
(goto-char (cdr (org-evil-block-boundaries))))
(defun org-evil-block-beginning-of-content ()
"Go to the start of the current block's content."
(interactive)
(org-evil-block-beginning-of-block)
(while (org-at-block-p)
(forward-line))
(point))
(defun org-evil-block-end-of-content ()
"Go to the end of the current block's content."
(interactive)
(org-evil-block-end-of-block)
(forward-line -1)
(end-of-line)
(point))
(defun org-evil-block-content-boundaries ()
"Return the '(START . END) boundaries of the content for the current block."
(cons (save-excursion (org-evil-block-beginning-of-content))
(save-excursion (org-evil-block-end-of-content))))
(evil-define-text-object org-evil-block-inner-block (count)
"Select inner block (the content)."
:type 'line
(interactive "<c>")
(-cons-to-list (org-evil-block-content-boundaries)))
(evil-define-text-object org-evil-block-a-block (count)
"Select a block."
:type 'line
(interactive "<c>")
(-cons-to-list (org-evil-block-boundaries)))
(org-evil--define-key 'motion 'org-evil-block-mode
"(" 'org-evil-block-beginning-of-block
")" 'org-evil-block-end-of-block)
;; Have to loop through as it looks like the text objects
;; don't configure correctly when binding multiple states
;; at once.
(dolist (mode '(operator visual))
(org-evil--define-key mode 'org-evil-block-mode
"ib" 'org-evil-block-inner-block
"ab" 'org-evil-block-a-block))
(org-evil--define-key 'motion 'org-evil-motion-mode
"[[" 'org-evil-motion-backward-block-begin
"]]" 'org-evil-motion-forward-block-begin
"gH" 'org-evil-motion-up-heading-top
"gh" 'org-evil-motion-up-heading
"{" 'org-evil-motion-backward-heading
"}" 'org-evil-motion-forward-heading)
(provide 'org-evil-motion)
;;; org-evil-motion.el ends here