-
Notifications
You must be signed in to change notification settings - Fork 5
/
evil-vimish-fold.el
135 lines (114 loc) · 4.69 KB
/
evil-vimish-fold.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
;;; evil-vimish-fold.el --- Integrate vimish-fold with evil
;; Copyright (c) 2015 Alex Murray
;;
;; Author: Alex Murray <[email protected]>
;; Maintainer: Alex Murray <[email protected]>
;; URL: https://github.com/alexmurray/evil-vimish-fold
;; Version: 0.3
;; Package-Requires: ((emacs "24.4") (evil "1.0.0") (vimish-fold "0.2.0"))
;; 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/>.
;;; Commentary:
;;
;; Integrate `vimish-fold' with `evil'.
;;
;; Provides bindings to create and delete folds via "zf" and "zd" respectively,
;; and provides integration of usual vim fold commands via `vimish-fold`.
;; Also supports navigation between folds using "zj" / "zk" respectively.
;;
;;; Code:
(require 'evil)
(require 'vimish-fold)
(require 'cl-lib)
(defvar evil-vimish-fold-target-modes '(prog-mode)
"Major modes in which `evil-vimish-fold-mode' should be activated.
This is used by `global-evil-vimish-fold-mode'.")
(defvar evil-vimish-fold-mode-lighter " zf"
"Mode lighter for evil-vimish-fold Mode.")
(evil-define-operator evil-vimish-fold/create (beg end)
"Create a fold from the current region.
See also `evil-delete-fold'."
(when vimish-fold-mode
(vimish-fold beg end)))
(evil-define-operator evil-vimish-fold/create-line (beg end)
"Create a fold from the current region.
See also `evil-delete-fold'."
:motion evil-line
(interactive "<r>")
(when vimish-fold-mode
(vimish-fold beg end)))
(evil-define-command evil-vimish-fold/delete ()
"Delete a fold under point.
See also `evil-create-fold'."
(evil-fold-action evil-fold-list :delete))
(evil-define-command evil-vimish-fold/delete-all ()
"Delete all folds."
(when vimish-fold-mode
(vimish-fold-delete-all)))
(evil-define-motion evil-vimish-fold/next-fold (count)
"Go to the start of the next fold."
:type inclusive
(when vimish-fold-mode
(unless (numberp count)
(setq count 1))
(dotimes (_ count nil)
(vimish-fold-next-fold))))
(evil-define-motion evil-vimish-fold/previous-fold (count)
"Go to the start of the previous fold."
:type inclusive
(when vimish-fold-mode
(unless (numberp count)
(setq count 1))
(dotimes (_ count nil)
(vimish-fold-previous-fold))))
;;;###autoload
(define-minor-mode evil-vimish-fold-mode
"Evil-vimish-fold-mode."
:lighter evil-vimish-fold-mode-lighter
:keymap (let ((map (make-sparse-keymap)))
(evil-define-key 'normal map "zj" 'evil-vimish-fold/next-fold)
(evil-define-key 'motion map "zj" 'evil-vimish-fold/next-fold)
(evil-define-key 'normal map "zk" 'evil-vimish-fold/previous-fold)
(evil-define-key 'motion map "zk" 'evil-vimish-fold/previous-fold)
(evil-define-key 'motion map "zd" 'evil-vimish-fold/delete)
(evil-define-key 'normal map "zE" 'evil-vimish-fold/delete-all)
(evil-define-key 'motion map "zf" 'evil-vimish-fold/create)
(evil-define-key 'motion map "zF" 'evil-vimish-fold/create-line)
map)
(vimish-fold-mode (if evil-vimish-fold-mode 1 -1))
(if evil-vimish-fold-mode
(add-to-list 'evil-fold-list
`((vimish-fold-mode)
:delete vimish-fold-delete
:open-all vimish-fold-unfold-all
:close-all vimish-fold-refold-all
:toggle vimish-fold-toggle
:open vimish-fold-unfold
:open-rec nil
:close vimish-fold-refold))
(setq evil-fold-list (cl-remove-if
#'(lambda (e) (eq (caar e) 'vimish-fold-mode))
evil-fold-list))))
;;;###autoload
(define-globalized-minor-mode global-evil-vimish-fold-mode
evil-vimish-fold-mode turn-on-evil-vimish-fold-mode)
;;;###autoload
(defun turn-on-evil-vimish-fold-mode ()
(when (apply 'derived-mode-p evil-vimish-fold-target-modes)
(evil-vimish-fold-mode 1)))
;;;###autoload
(defun turn-off-evil-vimish-fold-mode ()
"Turn off `evil-vimish-fold-mode'."
(interactive)
(evil-vimish-fold-mode -1))
(provide 'evil-vimish-fold)
;;; evil-vimish-fold.el ends here