-
Notifications
You must be signed in to change notification settings - Fork 71
/
evil-org.el
818 lines (740 loc) · 32 KB
/
evil-org.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
;;; evil-org.el --- evil keybindings for org-mode
;; Copyright (C) 2012-2017 by Somelauw
;; Maintainer: Somelauw
;; Original-author: Edward Tjörnhammar
;; URL: https://github.com/Somelauw/evil-org-mode.git
;; Git-Repository: git://github.com/Somelauw/evil-org-mode.git
;; Created: 2012-06-14
;; Forked-since: 2017-02-12
;; Version: 1.0.3
;; Package-Requires: ((emacs "24.4") (evil "1.0"))
;; Keywords: evil vim-emulation org-mode key-bindings presets
;; This file is not part of GNU Emacs
;; 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/>.
;; Minimal config
;; (add-hook 'org-mode-hook 'evil-org-mode)
;;; Commentary:
;;
;; Known Bugs:
;; See, https://github.com/Somelauw/evil-org-mode/issues
;;
;;; Code:
(eval-when-compile
(require 'let-alist))
(require 'cl-lib)
(require 'evil)
(require 'org)
(require 'org-element)
(require 'org-table)
(defgroup evil-org nil
"Provides integration of org-mode and evil."
:group 'org
:prefix "evil-org-")
;;; Customizations
(defcustom evil-org-key-theme
(if (bound-and-true-p evil-disable-insert-state-bindings)
'(navigation textobjects additional calendar)
'(navigation insert textobjects additional calendar))
"Which key themes to enable.
If you use this variable, you should call `evil-org-set-key-theme' with zero
arguments."
:group 'evil-org
:type '(set (const navigation)
(const insert)
(const return)
(const textobjects)
(const additional)
(const shift)
(const todo)
(const heading)
(const calendar)))
(defcustom evil-org-movement-bindings
'((up . "k")
(down . "j")
(left . "h")
(right . "l"))
"AList of normal keys to use for arrows.
This can be used by non-qwerty users who don't use hjkl."
:group 'evil-org
:type '(alist :key-type symbol :value-type string)
:options '(up down left right))
(defcustom evil-org-use-additional-insert nil
"Whether additional keybindings should also be available in insert mode."
:group 'evil-org
:type 'boolean)
(defcustom evil-org-special-o/O '(table-row item)
"When o and O should be special.
This makes them continue item lists and table rows.
By default, o and O are bound to ‘evil-org-open-above’ and ‘evil-org-open-below’."
:group 'evil-org
:type '(set (const table-row) (const item)))
(defcustom evil-org-retain-visual-state-on-shift nil
"Whether < and > should retain selection when used in visual state."
:group 'evil-org
:type 'boolean)
(defcustom evil-org-want-hybrid-shift t
"Whether HJKL should fall back on default bindings if not on heading/item.
This variable only takes effect when shift keytheme is enabled and should be set
before calling `evil-org-set-keytheme'."
:group 'evil-org
:type 'boolean)
;;; Variable declarations
(defvar browse-url-generic-program)
(defvar browse-url-generic-args)
(defvar evil-disable-insert-state-bindings)
(defvar org-capture-mode-map)
;;;###autoload
(define-minor-mode evil-org-mode
"Buffer local minor mode for evil-org"
:init-value nil
:lighter " EvilOrg"
:group 'evil-org
)
(with-eval-after-load 'evil-surround
(add-to-list 'evil-surround-operator-alist '(evil-org-delete . delete)))
(defun evil-org-eol-call (fun &rest arguments)
"Go to end of line and call provided function.
FUN function callback
Optional argument ARGUMENTS arguments to pass to FUN."
(declare (obsolete 'evil-org-define-eol-command "0.9.4"))
(end-of-visible-line)
(apply fun arguments)
(evil-insert nil))
(defun evil-org-bol-call (fun &rest arguments)
"Go to beginning of line and call provided function.
FUN function callback
Optional argument ARGUMENTS arguments to pass to FUN."
(declare (obsolete 'evil-org-define-bol-command "0.9.4"))
(beginning-of-line)
(apply fun arguments)
(evil-insert nil))
;;; motion declarations
(evil-declare-motion 'org-beginning-of-line)
(evil-declare-motion 'org-end-of-line)
(evil-declare-motion 'org-backward-sentence)
(evil-declare-motion 'org-forward-sentence)
(evil-declare-motion 'org-backward-paragraph)
(evil-declare-motion 'org-forward-paragraph)
;; heading
(evil-declare-motion 'org-backward-heading-same-level)
(evil-declare-motion 'org-forward-heading-same-level)
(evil-declare-motion 'org-previous-visible-heading)
(evil-declare-motion 'org-next-visible-heading)
;; elements
(evil-declare-motion 'org-backward-element)
(evil-declare-motion 'org-forward-element)
(evil-declare-motion 'org-up-element)
(evil-declare-motion 'org-down-element)
;; items
(evil-declare-motion 'org-previous-item)
(evil-declare-motion 'org-next-item)
(evil-declare-motion 'org-beginning-of-item)
(evil-declare-motion 'org-end-of-item)
(evil-declare-motion 'org-beginning-of-item-list)
(evil-declare-motion 'org-end-of-item-list)
;; blocks
(evil-declare-motion 'org-previous-block)
(evil-declare-motion 'org-next-block)
;; table
(evil-declare-motion 'org-table-previous-row)
(evil-declare-motion 'org-table-next-row)
(evil-declare-motion 'org-table-previous-field)
(evil-declare-motion 'org-table-next-field)
(evil-declare-motion 'org-table-beginning-of-field)
(evil-declare-motion 'org-table-end-of-field)
;;; non-repeatible
(evil-declare-change-repeat 'org-cycle)
(evil-declare-change-repeat 'org-shifttab)
(evil-declare-change-repeat 'org-table-end-of-field)
;;; new motions
(evil-define-motion evil-org-forward-sentence (count)
"In a table go to next cell, otherwise go to next sentence."
:type exclusive
:jump t
(interactive "p")
(if (org-at-table-p)
(org-table-end-of-field count)
(evil-forward-sentence-begin count)))
(evil-define-motion evil-org-backward-sentence (count)
"In a table go to previous cell, otherwise go to previous sentence."
:type exclusive
:jump t
(interactive "p")
(if (org-at-table-p)
(org-table-beginning-of-field count)
(evil-backward-sentence-begin count)))
(evil-define-motion evil-org-top ()
"Find the nearest one-star heading."
:type exclusive
:jump t
(while (org-up-heading-safe)))
(evil-define-motion evil-org-end-of-line (&optional n)
"Like evil-org-end-of-line but makes org-special-ctrl-a work in evil."
(when (and org-special-ctrl-a/e
evil-move-cursor-back
(not evil-move-beyond-eol)
(memq evil-state '(normal visual operator))
(not (invisible-p (line-end-position)))
(= (point) (1- (line-end-position))))
(forward-char))
(org-end-of-line n))
(defalias 'evil-org-beginning-of-line 'org-beginning-of-line)
;;; insertion commands
(defun evil-org-insert-line (count)
"Insert at beginning of line.
If ‘org-special-ctrl-a/e’ insertion will be done after heading and item markers.
The insertion will be repeated COUNT times."
(interactive "p")
(if (org-at-heading-or-item-p)
;; Manipulate org-beginning-of-line to become special
(progn (beginning-of-line)
(org-beginning-of-line nil)
(evil-insert count))
(evil-insert-line count)))
(defun evil-org-append-line (count)
"Append at end of line before ellipses if present.
If ‘org-special-ctrl-a/e’ insert before tags on headlines.
The insertion will be repeated COUNT times."
(interactive "p")
(if (org-at-heading-p)
;; Manipulate org-end-of-line to become special
(progn (end-of-line)
(org-end-of-line nil)
(evil-insert count))
(evil-append-line count)))
(defun evil-org-open-below (count)
"Clever insertion of org item.
Argument COUNT number of lines to insert.
The behavior in items and tables can be controlled using ‘evil-org-special-o/O’.
Passing in any prefix argument, executes the command without special behavior."
(interactive "P")
(cond ((and (memq 'table-row evil-org-special-o/O) (org-at-table-p))
(org-table-insert-row '(4))
(evil-insert nil))
((and (memq 'item evil-org-special-o/O) (org-at-item-p)
(progn (end-of-visible-line)
(org-insert-item (org-at-item-checkbox-p))))
(evil-insert nil))
((evil-open-below count))))
(defun evil-org-open-above (count)
"Clever insertion of org item.
Argument COUNT number of lines to insert.
The behavior in items and tables can be controlled using ‘evil-org-special-o/O’.
Passing in any prefix argument, executes the command without special behavior."
(interactive "P")
(cond ((and (memq 'table-row evil-org-special-o/O) (org-at-table-p))
(org-table-insert-row)
(evil-insert nil))
((and (memq 'item evil-org-special-o/O) (org-at-item-p)
(progn (beginning-of-line)
(org-insert-item (org-at-item-checkbox-p))))
(evil-insert nil))
((evil-open-above count))))
(defun evil-org-return (arg)
"Like `org-return', but continues items and tables like `evil-open-below'.
Pressing return twice cancels the continuation of the itemlist or table.
If ARG is set it will not cancel the continuation.
The behavior of this function can be controlled using `evil-org-special-o/O’."
(interactive "P")
(cond ((and (not arg) (evil-org--empty-element-p))
(delete-region (line-beginning-position) (line-end-position)))
((eolp)
(if (bolp)
(call-interactively #'org-return)
(call-interactively #'evil-org-open-below)))
('otherwise
(call-interactively #'org-return-indent))))
(defun evil-org--empty-element-p ()
"Return if pointer is on an empty element."
(cond ((org-at-table-p)
(let* ((rows (cl-remove 'hline (org-table-to-lisp)))
(row (nth (1- (org-table-current-line)) rows)))
(cl-every 'string-empty-p row)))
((org-at-item-p)
(string-match-p "^[[:space:]]*\\([+-]\\|[[:digit:]]+[.)]\\)[[:space:]]*\\(::[[:space:]]*\\)?$"
(thing-at-point 'line)))))
;; other
(defun evil-org-shift-fallback-command ()
"Call the default evil command that is bound the currently pressed key."
(when (and evil-mode evil-org-mode evil-org-want-hybrid-shift)
(let ((key (this-command-keys)))
(when-let ((command (or (lookup-key evil-normal-state-map key)
(lookup-key evil-motion-state-map key))))
(when (commandp command)
(call-interactively command)
t)))))
(defmacro evil-org-define-eol-command (cmd)
"Return a function that executes CMD at eol and then enters insert state.
eol stands for end of line.
For many org functions such as `org-insert-heading', this creates a heading below the current line."
(let ((newcmd (intern (concat "evil-org-" (symbol-name cmd) "-below"))))
`(progn
(defun ,newcmd ()
,(concat "Call `" (symbol-name cmd) "' at end of line and go to insert mode.")
(interactive)
(end-of-visible-line)
(call-interactively #',cmd)
(evil-insert nil))
#',newcmd)))
(defmacro evil-org-define-bol-command (cmd)
"Return a function that executes CMD at bol and then enters insert state.
bol stands for beginning of line.
For many org functions such as `org-insert-heading', this creates a heading above the current line."
(let ((newcmd (intern (concat "evil-org-" (symbol-name cmd) "-above"))))
`(progn
(defun ,newcmd ()
,(concat "Call `" (symbol-name cmd) "' at beginning of line and go to insert mode.")
(interactive)
(beginning-of-line)
(call-interactively #',cmd)
(evil-insert nil))
#',newcmd)))
;;; operators
(evil-define-operator evil-org-> (beg end count)
"Demote, indent, move column right.
In items or headings, demote heading/item.
In code blocks, indent lines
In tables, move column to the right."
:move-point nil
(interactive "<r><vc>")
(when (null count) (setq count 1))
(cond
;; Work with subtrees and headings
((org-with-limited-levels
(or (org-at-heading-p)
(save-excursion (goto-char beg) (org-at-heading-p))))
(if (> count 0)
(org-map-region 'org-do-demote beg end)
(org-map-region 'org-do-promote beg end)))
;; Shifting table columns
((and (org-at-table-p)
(save-excursion
(goto-char beg)
(<= (line-beginning-position) end (line-end-position))))
(evil-org-table-move-column beg end count))
;; Work with items
((and (org-at-item-p)
(<= end (save-excursion (org-end-of-item-list))))
(evil-org-indent-items beg end count))
;; Default indentation
(t
;; special casing tables
(when (and (not (region-active-p)) (org-at-table-p))
(setq beg (min beg (org-table-begin)))
(setq end (max end (org-table-end))))
(evil-shift-right beg end count)))
(when (and evil-org-retain-visual-state-on-shift (evil-visual-state-p))
(evil-normal-state)
(evil-visual-restore)))
(evil-define-operator evil-org-< (beg end count)
"Promote, dedent, move column left.
In items or headings, promote heading/item.
In code blocks, indent lines
In tables, move column to the left."
(interactive "<r><vc>")
(evil-org-> beg end (- (or count 1))))
(defun evil-org-indent-items (beg end count)
"Indent all selected items in itemlist.
Argument BEG Begin of subtree items to indent.
Argument END End of subtree items to indent.
Argument COUNT if negative, items are dedented instead."
(when (null count) (setq count 1))
(let* ((struct (save-excursion (goto-char beg) (org-list-struct)))
(region-p (region-active-p)))
;; special case: indenting all items
(if (and struct org-list-automatic-rules (not region-p)
(= (point-at-bol) (org-list-get-top-point struct)))
(org-list-indent-item-generic count nil struct)
;; indenting selected items
(save-excursion
(when region-p (deactivate-mark))
(set-mark beg)
(goto-char end)
(org-list-indent-item-generic count t struct)))))
(defun evil-org-table-move-column (beg end arg)
"Move org table column.
Argument BEG, first column
Argument END, second column
If ARG > 0, move column BEG to END.
If ARG < 0, move column END to BEG"
(let* ((text (buffer-substring beg end))
(n-cells-selected (max 1 (cl-count ?| text)))
(n-columns-to-move (* n-cells-selected (abs arg)))
(move-left-p (< arg 0)))
(goto-char (if move-left-p end beg))
(dotimes (_ n-columns-to-move) (org-table-move-column move-left-p))))
(evil-define-operator evil-org-delete-char (count beg end type register)
"Combine evil-delete-char with org-delete-char"
:motion evil-forward-char
(interactive "p<R><x>")
(if (evil-visual-state-p) ; No special support for visual state
(evil-delete-char beg end type register)
(evil-set-register ?- (filter-buffer-substring beg end))
(evil-yank beg end type register)
(org-delete-char count)))
(evil-define-operator evil-org-delete-backward-char (count beg end type register)
"Combine evil-delete-backward-char with org-delete-backward-char"
:motion evil-backward-char
(interactive "p<R><x>")
(if (evil-visual-state-p) ; No special support for visual state
(evil-delete-backward-char beg end type register)
(evil-set-register ?- (filter-buffer-substring beg end))
(evil-yank beg end type register)
(org-delete-char count)))
(evil-define-operator evil-org-delete (beg end type register yank-handler)
"Like evil-delete, but realigns tags and numbered lists."
(interactive "<R><x><y>")
(let ((renumber-lists-p (or (< beg (line-beginning-position))
(> end (line-end-position)))))
(evil-delete beg end type register yank-handler)
(cond ((and renumber-lists-p (org-at-item-p))
(org-list-repair))
((org-at-heading-p)
(org-fix-tags-on-the-fly)))))
(defun evil-org-generic-open-links (beg end incog)
"Open org mode links in visual selection.
Argument BEG beginning of region.
Argument END end of region.
Argument INCOG whether to open in incognito mode."
(progn
(save-excursion
(goto-char beg)
(catch 'break
(while t
(org-next-link)
;; break from outer loop when there are no more
;; org links
(when (or (not (< (point) end))
(not (null org-link-search-failed)))
(throw 'break 0))
(if (not (null incog))
(evil-org-open-incognito)
(let ((browse-url-generic-args '("")))
(org-open-at-point '(16)))))))))
(defun evil-org-open-incognito ()
"Open link in new incognito window."
(interactive)
(let* ((new-arg
;; decide which incognito settings to use depending on the browser
(cond ((not (null (string-match "^.*\\(iceweasel\\|firefox\\).*$" browse-url-generic-program))) "--private-window")
((not (null (string-match "^.*\\(chrome\\|chromium\\).*$" browse-url-generic-program))) "--incognito")
(t "")))
(old-b (list browse-url-generic-args " "))
(browse-url-generic-args (add-to-ordered-list 'old-b new-arg 0)))
(org-open-at-point '(16))))
(evil-define-operator evil-org-open-links (beg end &optional count)
"Open links in visual selection.
If a prefix argument is given, links are opened in incognito mode."
:keep-visual t
:move-point nil
(interactive "<r><vc>")
(evil-org-generic-open-links beg end (not (null count))))
(evil-define-operator evil-org-open-links-incognito (beg end)
"Open links in visual selection in incognito mode."
:keep-visual t
:move-point nil
(interactive "<r>")
(evil-org-generic-open-links beg end t))
;;; text-objects
(defun evil-org-select-an-element (element)
"Select an org ELEMENT."
(list (min (region-beginning) (org-element-property :begin element))
(org-element-property :end element)))
(defun evil-org-select-inner-element (element)
"Select inner org ELEMENT."
(let ((type (org-element-type element))
(begin (org-element-property :begin element))
(end (org-element-property :end element))
(contents-begin (org-element-property :contents-begin element))
(contents-end (org-element-property :contents-end element))
(post-affiliated (org-element-property :post-affiliated element))
(post-blank (org-element-property :post-blank element)))
(cond ((or (string-suffix-p "-block" (symbol-name type))
(memq type '(latex-environment)))
;; Special case on block types (thanks Nicolas Goaziou)
(list (org-with-point-at post-affiliated (line-beginning-position 2))
(org-with-point-at end (line-beginning-position (- post-blank)))))
((memq type '(verbatim code))
(list (1+ begin) (- end post-blank 1)))
('otherwise
(list (or contents-begin post-affiliated begin)
(or contents-end
;; Prune post-blank lines from :end element
(org-with-point-at end
;; post-blank is charwise for objects and linewise for elements
(if (memq type org-element-all-objects)
(- end post-blank)
(line-end-position (- post-blank))))))))))
(defun evil-org-parent (element)
"Find a parent or nearest heading of ELEMENT."
(or (org-element-property :parent element)
(save-excursion
(goto-char (org-element-property :begin element))
(if (org-with-limited-levels (org-at-heading-p))
(org-up-heading-safe)
(org-with-limited-levels (org-back-to-heading)))
(org-element-at-point))))
(evil-define-text-object evil-org-an-object (count beg end type)
"An org object.
Matches urls and table cells."
(when (null end) (setq end (point)))
(when (null beg) (setq beg (point)))
(let* ((first (org-element-context))
(element first))
;; select next object on repetitive presses
(goto-char end)
(when (<= (org-element-property :end element) end)
(setq element (org-element-context)))
(dotimes (_ (1- count))
(goto-char (org-element-property :end element))
(setq element (org-element-context)))
(evil-org-select-an-element element)))
(evil-define-text-object evil-org-inner-object (count &optional beg end type)
"Select an org object.
Matches urls and table cells."
(evil-org-select-inner-element (org-element-context)))
(evil-define-text-object evil-org-an-element (count &optional beg end type)
"An org element.
Includes paragraphs, table rows and code blocks.
"
(let* ((first (org-element-at-point))
(element first))
(when (and end (>= end (org-element-property :end element)))
(org-forward-element)
(setq element (org-element-at-point)))
(dotimes (_ (1- count))
(org-forward-element)
(setq element (org-element-at-point)))
(evil-org-select-an-element element)))
(evil-define-text-object evil-org-inner-element (count &optional beg end type)
"Inner org element.
Includes paragraphs, table rows and code blocks.
"
(evil-org-select-inner-element (org-element-at-point)))
(evil-define-text-object evil-org-a-greater-element (count &optional beg end type)
"A greater (recursive) org element.
Includes tables, list items and subtrees."
:type line
(when (null count) (setq count 1))
(save-excursion
(when beg (goto-char beg))
(let ((element (org-element-at-point)))
(when (or (not (memq (cl-first element) org-element-greater-elements))
(and end (>= end (org-element-property :end element))))
(setq element (evil-org-parent element)))
(dotimes (_ (1- count))
(setq element (evil-org-parent element)))
(evil-org-select-an-element element))))
(evil-define-text-object evil-org-inner-greater-element (count &optional beg end type)
"Inner greater (recursive) org element.
Includes tables, list items and subtrees."
(when (null count) (setq count 1))
(save-excursion
(when beg (goto-char beg))
(let ((element (org-element-at-point)))
(unless (memq (cl-first element) org-element-greater-elements)
(setq element (evil-org-parent element)))
(dotimes (_ (1- count))
(setq element (evil-org-parent element)))
(evil-org-select-inner-element element))))
(evil-define-text-object evil-org-a-subtree (count &optional beg end type)
"An org subtree."
:type line
(when (null count) (setq count 1))
(org-with-limited-levels
(cond ((org-at-heading-p) (beginning-of-line))
((org-before-first-heading-p) (user-error "Not in a subtree"))
(t (outline-previous-visible-heading 1))))
(when count (while (and (> count 1) (org-up-heading-safe)) (cl-decf count)))
(evil-org-select-an-element (org-element-at-point)))
(evil-define-text-object evil-org-inner-subtree (count &optional beg end type)
"Inner org subtree."
:type line
(when (null count) (setq count 1))
(org-with-limited-levels
(cond ((org-at-heading-p) (beginning-of-line))
((org-before-first-heading-p) (user-error "Not in a subtree"))
(t (outline-previous-visible-heading 1))))
(when count (while (and (> count 1) (org-up-heading-safe)) (cl-decf count)))
(evil-org-select-inner-element (org-element-at-point)))
;;; Keythemes
(defun evil-org--populate-base-bindings ()
"Bindings that are always available."
(evil-define-key 'motion 'evil-org-mode
(kbd "0") 'evil-org-beginning-of-line
(kbd "$") 'evil-org-end-of-line
(kbd ")") 'evil-org-forward-sentence
(kbd "(") 'evil-org-backward-sentence
(kbd "}") 'org-forward-paragraph
(kbd "{") 'org-backward-paragraph)
(evil-define-key 'normal 'evil-org-mode
(kbd "I") 'evil-org-insert-line
(kbd "A") 'evil-org-append-line
(kbd "o") 'evil-org-open-below
(kbd "O") 'evil-org-open-above
(kbd "d") 'evil-org-delete
(kbd "x") 'evil-org-delete-char
(kbd "X") 'evil-org-delete-backward-char
(kbd "<C-return>") (evil-org-define-eol-command
org-insert-heading-respect-content)
(kbd "<C-S-return>") (evil-org-define-eol-command
org-insert-todo-heading-respect-content))
(evil-define-key '(normal visual) 'evil-org-mode
(kbd "<tab>") 'org-cycle
(kbd "g TAB") 'org-cycle
(kbd "<backtab>") 'org-shifttab
(kbd "<") 'evil-org-<
(kbd ">") 'evil-org->))
(defun evil-org--populate-textobjects-bindings ()
"Text objects."
(evil-define-key '(visual operator) 'evil-org-mode
"ae" 'evil-org-an-object
"ie" 'evil-org-inner-object
"aE" 'evil-org-an-element
"iE" 'evil-org-inner-element
"ir" 'evil-org-inner-greater-element
"ar" 'evil-org-a-greater-element
"aR" 'evil-org-a-subtree
"iR" 'evil-org-inner-subtree))
(defun evil-org--populate-insert-bindings ()
"Define insert mode bindings."
(evil-define-key 'insert 'evil-org-mode
(kbd "C-t") 'org-metaright
(kbd "C-d") 'org-metaleft))
(defun evil-org--populate-navigation-bindings ()
"Configures gj/gk/gh/gl for navigation."
(let-alist evil-org-movement-bindings
;; Other evil packages can override normal state and visual state bindings.
;; Thus, it is necessary to bind keys on motion, normal, and visual states.
(evil-define-key '(motion normal visual) 'evil-org-mode
(kbd (concat "g" .left)) 'org-up-element
(kbd (concat "g" .right)) 'org-down-element
(kbd (concat "g" .up)) 'org-backward-element
(kbd (concat "g" .down)) 'org-forward-element
(kbd (concat "g" (capitalize .left))) 'evil-org-top)))
(defun evil-org--populate-additional-bindings ()
"Bindings with meta and control."
(let-alist evil-org-movement-bindings
(let ((state (if evil-org-use-additional-insert
'(normal visual insert)
'(normal visual))))
(evil-define-key state 'evil-org-mode
(kbd (concat "M-" .left)) 'org-metaleft
(kbd (concat "M-" .right)) 'org-metaright
(kbd (concat "M-" .up)) 'org-metaup
(kbd (concat "M-" .down)) 'org-metadown
(kbd (concat "M-" (capitalize .left))) 'org-shiftmetaleft
(kbd (concat "M-" (capitalize .right))) 'org-shiftmetaright
(kbd (concat "M-" (capitalize .up))) 'org-shiftmetaup
(kbd (concat "M-" (capitalize .down))) 'org-shiftmetadown
(kbd (concat "C-S-" .left)) 'org-shiftcontrolleft
(kbd (concat "C-S-" .right)) 'org-shiftcontrolright
(kbd (concat "C-S-" .up)) 'org-shiftcontrolup
(kbd (concat "C-S-" .down)) 'org-shiftcontroldown))))
(defun evil-org--populate-shift-bindings ()
"Shift bindings that conflict with evil bindings."
(let-alist evil-org-movement-bindings
(evil-define-key 'normal 'evil-org-mode
(capitalize .left) 'org-shiftleft
(capitalize .right) 'org-shiftright
(capitalize .down) 'org-shiftdown
(capitalize .up) 'org-shiftup)
;; Make shift keys fall back on the keys they have replaced
(when evil-org-want-hybrid-shift
(dolist (hook '(org-shiftleft-final-hook
org-shiftright-final-hook
org-shiftdown-final-hook
org-shiftup-final-hook))
(add-hook hook #'evil-org-shift-fallback-command 'append)))))
(defun evil-org--populate-todo-bindings ()
"Bindings for easy todo insertion."
(evil-define-key 'normal 'evil-org-mode
(kbd "t") 'org-todo
(kbd "T") (evil-org-define-eol-command org-insert-todo-heading)
(kbd "M-t") (evil-org-define-eol-command org-insert-todo-subheading)))
(defun evil-org--populate-heading-bindings ()
"Bindings for easy heading insertion."
(evil-define-key 'normal 'evil-org-mode
(kbd "O") (evil-org-define-eol-command org-insert-heading)
(kbd "M-o") (evil-org-define-eol-command org-insert-subheading)))
(defun evil-org--populate-calendar-bindings ()
"Bindings for easy date selection."
(define-key org-read-date-minibuffer-local-map
(kbd "C-f") (lambda () (interactive)
(org-eval-in-calendar
'(calendar-scroll-left-three-months 1))))
(define-key org-read-date-minibuffer-local-map
(kbd "C-b") (lambda () (interactive)
(org-eval-in-calendar
'(calendar-scroll-right-three-months 1))))
(let-alist evil-org-movement-bindings
(define-key org-read-date-minibuffer-local-map
(kbd (concat "M-" .left)) (lambda () (interactive)
(org-eval-in-calendar '(calendar-backward-day 1))))
(define-key org-read-date-minibuffer-local-map
(kbd (concat "M-" .right)) (lambda () (interactive)
(org-eval-in-calendar '(calendar-forward-day 1))))
(define-key org-read-date-minibuffer-local-map
(kbd (concat "M-" .up)) (lambda () (interactive)
(org-eval-in-calendar '(calendar-backward-week 1))))
(define-key org-read-date-minibuffer-local-map
(kbd (concat "M-" .down)) (lambda () (interactive)
(org-eval-in-calendar '(calendar-forward-week 1))))
(define-key org-read-date-minibuffer-local-map
(kbd (concat "M-" (capitalize .left))) (lambda () (interactive)
(org-eval-in-calendar '(calendar-backward-month 1))))
(define-key org-read-date-minibuffer-local-map
(kbd (concat "M-" (capitalize .right))) (lambda () (interactive)
(org-eval-in-calendar '(calendar-forward-month 1))))
(define-key org-read-date-minibuffer-local-map
(kbd (concat "M-" (capitalize .up))) (lambda () (interactive)
(org-eval-in-calendar '(calendar-backward-year 1))))
(define-key org-read-date-minibuffer-local-map
(kbd (concat "M-" (capitalize .down))) (lambda () (interactive)
(org-eval-in-calendar '(calendar-forward-year 1))))))
(defun evil-org-set-key-theme (&optional theme)
"Select what keythemes to enable.
Optional argument THEME list of themes. See evil-org-key-theme for a list of values."
(dolist (state evil-minor-mode-keymaps-alist)
;; Remove evil-org-mode keymaps in evil minor-mode keymaps
(setcdr state (assq-delete-all 'evil-org-mode (cdr state))))
(evil-org--populate-base-bindings)
(let ((theme (or theme evil-org-key-theme)))
(when (memq 'navigation theme) (evil-org--populate-navigation-bindings))
(when (memq 'insert theme) (evil-org--populate-insert-bindings))
(when (memq 'return theme)
(evil-define-key '(insert emacs) 'evil-org-mode
(kbd "RET") 'evil-org-return))
(when (memq 'textobjects theme) (evil-org--populate-textobjects-bindings))
(when (memq 'additional theme) (evil-org--populate-additional-bindings))
(when (memq 'shift theme) (evil-org--populate-shift-bindings))
(when (memq 'todo theme) (evil-org--populate-todo-bindings))
(when (memq 'heading theme) (evil-org--populate-heading-bindings))
(when (memq 'calendar theme) (evil-org--populate-calendar-bindings))))
;;; vim-like confirm/abort for capture and src
;;; Taken from mwillsey (Max Willsey) on https://github.com/syl20bnr/spacemacs/pull/7400
(with-eval-after-load 'org-capture
(define-key org-capture-mode-map [remap evil-save-and-close] 'org-capture-finalize)
(define-key org-capture-mode-map [remap evil-save-modified-and-close] 'org-capture-finalize)
(define-key org-capture-mode-map [remap evil-quit] 'org-capture-kill))
(with-eval-after-load 'org-src
(define-key org-src-mode-map [remap evil-save-and-close] 'org-edit-src-exit)
(define-key org-src-mode-map [remap evil-save-modified-and-close] 'org-edit-src-exit)
(define-key org-src-mode-map [remap evil-quit] 'org-edit-src-abort))
(with-eval-after-load 'org-table
(define-key org-table-fedit-map [remap evil-save-and-close] 'org-table-fedit-finish)
(define-key org-table-fedit-map [remap evil-save-modified-and-close] 'org-table-fedit-finish)
(define-key org-table-fedit-map [remap evil-quit] 'org-table-fedit-abort))
;; Set customizable theme
(evil-org-set-key-theme evil-org-key-theme)
(provide 'evil-org)
;;; evil-org.el ends here