-
Notifications
You must be signed in to change notification settings - Fork 7
/
evil-indent-plus.el
191 lines (162 loc) · 8.48 KB
/
evil-indent-plus.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
;;; evil-indent-plus.el --- Evil textobjects based on indentation -*- lexical-binding: t; -*-
;; Original evil-indent-textobject.el Copyright (C) 2013 Michael Markert
;; Modifications Copyright (C) 2014-2015 Eivind Fonn
;;
;; Author: Eivind Fonn <evfonn@gmail.com>
;; Version: 1.0.1
;; Keywords: convenience evil
;; URL: http://github.com/TheBB/evil-indent-plus
;; Package-Requires: ((evil "0") (cl-lib "0.5"))
;;
;; 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 2 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Adds new text objects:
;;
;; ii, ai: Block of text with same or higher indentation
;; iI, aI: Block of text with same or higher indentation, including the first line
;; above with smaller indentation
;; iJ, aJ: Block of text with same or higher indentation, including the first lines
;; above and below with smaller indentation
;;; Code:
(require 'cl-lib)
(require 'evil)
(defvar evil-indent-plus--base nil)
(defun evil-indent-plus--chomp (str)
"Chomp leading and tailing whitespace from STR."
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'" str)
(setq str (replace-match "" t t str)))
str)
(defun evil-indent-plus--empty-line-p ()
(string= "" (evil-indent-plus--chomp (thing-at-point 'line))))
(defun evil-indent-plus--not-empty-line-p ()
(not (evil-indent-plus--empty-line-p)))
(defun evil-indent-plus--geq-p ()
(>= (current-indentation) evil-indent-plus--base))
(defun evil-indent-plus--geq-or-empty-p ()
(or (evil-indent-plus--empty-line-p) (evil-indent-plus--geq-p)))
(defun evil-indent-plus--g-p ()
(> (current-indentation) evil-indent-plus--base))
(defun evil-indent-plus--g-or-empty-p ()
(or (evil-indent-plus--empty-line-p) (evil-indent-plus--g-p)))
(defun evil-indent-plus--seek (start direction before skip predicate)
"Seeks forward (if direction is 1) or backward (if direction is -1) from start, until predicate
fails. If before is nil, it will return the first line where predicate fails, otherwise it returns
the last line where predicate holds."
(save-excursion
(goto-char start)
(goto-char (point-at-bol))
(let ((bnd (if (> 0 direction)
(point-min)
(point-max)))
(pt (point)))
(when skip (forward-line direction))
(cl-loop while (and (/= (point) bnd) (funcall predicate))
do (progn
(when before (setq pt (point-at-bol)))
(forward-line direction)
(unless before (setq pt (point-at-bol)))))
pt)))
(defun evil-indent-plus--same-indent-range (&optional point)
"Return the point at the begin and end of the text block with the same (or greater) indentation.
If `point' is supplied and non-nil it will return the begin and end of the block surrounding point."
(save-excursion
(when point
(goto-char point))
(let ((evil-indent-plus--base (current-indentation))
(begin (point))
(end (point)))
(setq begin (evil-indent-plus--seek begin -1 t nil 'evil-indent-plus--geq-or-empty-p))
(setq begin (evil-indent-plus--seek begin 1 nil nil 'evil-indent-plus--g-or-empty-p))
(setq end (evil-indent-plus--seek end 1 t nil 'evil-indent-plus--geq-or-empty-p))
(setq end (evil-indent-plus--seek end -1 nil nil 'evil-indent-plus--empty-line-p))
(list begin end evil-indent-plus--base))))
(defun evil-indent-plus--up-indent-range (&optional point)
(let* ((range (evil-indent-plus--same-indent-range point))
(evil-indent-plus--base (cl-third range))
(begin (evil-indent-plus--seek (cl-first range)
-1 nil nil
'evil-indent-plus--geq-or-empty-p)))
(list begin (cl-second range) evil-indent-plus--base)))
(defun evil-indent-plus--up-down-indent-range (&optional point)
(let* ((range (evil-indent-plus--same-indent-range point))
(evil-indent-plus--base (cl-third range))
(begin (evil-indent-plus--seek (cl-first range)
-1 nil nil
'evil-indent-plus--geq-or-empty-p))
(end (evil-indent-plus--seek (cl-second range)
1 nil nil
'evil-indent-plus--geq-or-empty-p)))
(list begin end evil-indent-plus--base)))
(defun evil-indent-plus--linify (range)
(let ((nbeg (save-excursion (goto-char (cl-first range)) (point-at-bol)))
(nend (save-excursion (goto-char (cl-second range)) (point-at-eol))))
(evil-range nbeg nend 'line)))
(defun evil-indent-plus--extend (range)
(let ((begin (cl-first range))
(end (cl-second range))
nend)
(setq nend (evil-indent-plus--seek end 1 t t 'evil-indent-plus--empty-line-p))
(when (= nend end)
(setq begin (evil-indent-plus--seek begin -1 t t 'evil-indent-plus--empty-line-p)))
(list begin nend)))
;;;###autoload (autoload 'evil-indent-plus-i-indent "evil-indent-plus" nil t)
(evil-define-text-object evil-indent-plus-i-indent (&optional count beg end type)
"Text object describing the block with the same (or greater) indentation as the current line,
skipping empty lines."
:type line
(evil-indent-plus--linify (evil-indent-plus--same-indent-range)))
;;;###autoload (autoload 'evil-indent-plus-a-indent "evil-indent-plus" nil t)
(evil-define-text-object evil-indent-plus-a-indent (&optional count beg end type)
"Text object describing the block with the same (or greater) indentation as the current line,
skipping empty lines."
:type line
(evil-indent-plus--linify (evil-indent-plus--extend (evil-indent-plus--same-indent-range))))
;;;###autoload (autoload 'evil-indent-plus-i-indent-up "evil-indent-plus" nil t)
(evil-define-text-object evil-indent-plus-i-indent-up (&optional count beg end type)
"Text object describing the block with the same (or greater) indentation as the current line,
and the line above, skipping empty lines."
:type line
(evil-indent-plus--linify (evil-indent-plus--up-indent-range)))
;;;###autoload (autoload 'evil-indent-plus-a-indent-up "evil-indent-plus" nil t)
(evil-define-text-object evil-indent-plus-a-indent-up (&optional count beg end type)
"Text object describing the block with the same (or greater) indentation as the current line,
and the line above, skipping empty lines."
:type line
(evil-indent-plus--linify (evil-indent-plus--extend (evil-indent-plus--up-indent-range))))
;;;###autoload (autoload 'evil-indent-plus-i-indent-up-down "evil-indent-plus" nil t)
(evil-define-text-object evil-indent-plus-i-indent-up-down (&optional count beg end type)
"Text object describing the block with the same (or greater) indentation as the current line,
and the line above and below, skipping empty lines."
:type line
(evil-indent-plus--linify (evil-indent-plus--up-down-indent-range)))
;;;###autoload (autoload 'evil-indent-plus-a-indent-up-down "evil-indent-plus" nil t)
(evil-define-text-object evil-indent-plus-a-indent-up-down (&optional count beg end type)
"Text object describing the block with the same (or greater) indentation as the current line,
and the line above and below, skipping empty lines."
:type line
(evil-indent-plus--linify (evil-indent-plus--extend (evil-indent-plus--up-down-indent-range))))
;;;###autoload
(defun evil-indent-plus-default-bindings ()
"Set the default evil-indent-plus keybindings."
(define-key evil-inner-text-objects-map "i" 'evil-indent-plus-i-indent)
(define-key evil-outer-text-objects-map "i" 'evil-indent-plus-a-indent)
(define-key evil-inner-text-objects-map "I" 'evil-indent-plus-i-indent-up)
(define-key evil-outer-text-objects-map "I" 'evil-indent-plus-a-indent-up)
(define-key evil-inner-text-objects-map "J" 'evil-indent-plus-i-indent-up-down)
(define-key evil-outer-text-objects-map "J" 'evil-indent-plus-a-indent-up-down))
(provide 'evil-indent-plus)
;;; evil-indent-plus.el ends here