1 "--------------------------------------------------------------------------------
3 " Copyright (c) 2010 Michael Smith <msmith@msmith.id.au>
5 " http://github.com/michaeljsmith/vim-indent-object
7 " Permission is hereby granted, free of charge, to any person obtaining a copy
8 " of this software and associated documentation files (the "Software"), to
9 " deal in the Software without restriction, including without limitation the
10 " rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 " sell copies of the Software, and to permit persons to whom the Software is
12 " furnished to do so, subject to the following conditions:
14 " The above copyright notice and this permission notice shall be included in
15 " all copies or substantial portions of the Software.
17 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 "--------------------------------------------------------------------------------
27 " Mappings excluding line below.
28 onoremap <silent>ai :<C-u>cal <Sid>HandleTextObjectMapping(0, 0, 0, [line("."), line("."), col("."), col(".")])<CR>
29 onoremap <silent>ii :<C-u>cal <Sid>HandleTextObjectMapping(1, 0, 0, [line("."), line("."), col("."), col(".")])<CR>
30 vnoremap <silent>ai :<C-u>cal <Sid>HandleTextObjectMapping(0, 0, 1, [line("'<"), line("'>"), col("'<"), col("'>")])<CR><Esc>gv
31 vnoremap <silent>ii :<C-u>cal <Sid>HandleTextObjectMapping(1, 0, 1, [line("'<"), line("'>"), col("'<"), col("'>")])<CR><Esc>gv
33 " Mappings including line below.
34 onoremap <silent>aI :<C-u>cal <Sid>HandleTextObjectMapping(0, 1, 0, [line("."), line("."), col("."), col(".")])<CR>
35 onoremap <silent>iI :<C-u>cal <Sid>HandleTextObjectMapping(1, 1, 0, [line("."), line("."), col("."), col(".")])<CR>
36 vnoremap <silent>aI :<C-u>cal <Sid>HandleTextObjectMapping(0, 1, 1, [line("'<"), line("'>"), col("'<"), col("'>")])<CR><Esc>gv
37 vnoremap <silent>iI :<C-u>cal <Sid>HandleTextObjectMapping(1, 1, 1, [line("'<"), line("'>"), col("'<"), col("'>")])<CR><Esc>gv
44 function! <Sid>TextObject(inner, incbelow, vis, range, count)
46 " Record the current state of the visual region.
49 " Detect if this is a completely new visual selction session.
51 let new_vis = new_vis || s:l0 != a:range[0]
52 let new_vis = new_vis || s:l1 != a:range[1]
53 let new_vis = new_vis || s:c0 != a:range[2]
54 let new_vis = new_vis || s:c1 != a:range[3]
61 " Repeatedly increase the scope of the selection.
66 " Look for the minimum indentation in the current visual region.
68 let idnt_invalid = 1000
69 let idnt = idnt_invalid
71 if !(getline(l) =~ "^\\s*$")
72 let idnt = min([idnt, indent(l)])
77 " Keep track of where the range should be expanded to.
83 " If we are highlighting only blank lines, we may not have found a
84 " valid indent. In this case we need to look for the next and previous
85 " non blank lines and check which of those has the largest indent.
86 if idnt == idnt_invalid
88 let pnb = prevnonblank(s:l0)
90 let idnt = max([idnt, indent(pnb)])
93 let nnb = nextnonblank(s:l0)
95 let idnt = max([idnt, indent(nnb)])
98 " If we are in whitespace at the beginning of a block, skip over
99 " it when we are selecting the range. Similarly, if we are in
100 " whitespace at the end, ignore it.
101 if idnt > indent(pnb)
104 if idnt > indent(nnb)
109 " Search backward for the first line with less indent than the target
110 " indent (skipping blank lines).
111 let blnk = getline(l_1) =~ "^\\s*$"
112 while l_1 > 0 && ((idnt == 0 && !blnk) || (idnt != 0 && (blnk || indent(l_1) >= idnt)))
117 let blnk = getline(l_1) =~ "^\\s*$"
120 " Search forward for the first line with more indent than the target
121 " indent (skipping blank lines).
122 let line_cnt = line("$")
123 let blnk = getline(l2) =~ "^\\s*$"
124 while l2 <= line_cnt && ((idnt == 0 && !blnk) || (idnt != 0 && (blnk || indent(l2) >= idnt)))
129 let blnk = getline(l2) =~ "^\\s*$"
132 " Determine which of these extensions to include. Include neither if
133 " we are selecting an 'inner' object. Exclude the bottom unless are
134 " told to include it.
135 let idnt2 = max([indent(l_1), indent(l2)])
136 if indent(l_1) < idnt2 || a:inner
139 if indent(l2) < idnt2 || a:inner || !a:incbelow
142 let l_1 = max([l_1, 1])
143 let l2 = min([l2, line("$")])
145 " Extend the columns to the start and end.
146 " If inner is selected, set the final cursor pos to the start
147 " of the text in the line.
150 let c_1 = match(getline(l_1), "\\c\\S") + 1
152 let c2 = len(getline(l2))
157 " Make sure there's no change if we haven't really made a
158 " significant change in linewise mode - this makes sure that
159 " we can iteratively increase selection in linewise mode.
160 if itr_cnt == 0 && vismode ==# 'V' && s:l0 == l_1 && s:l1 == l2
165 " Check whether the visual region has changed.
167 let chg = chg || s:l0 != l_1
168 let chg = chg || s:l1 != l2
169 let chg = chg || s:c0 != c_1
170 let chg = chg || s:c1 != c2
172 if vismode ==# 'V' && new_vis
182 " If there was no change, then don't decrement the count (it didn't
183 " count because it didn't do anything).
187 " Since this didn't work, push the selection back one char. This
188 " will have the effect of getting the enclosing block. Do it at
189 " the beginning rather than the end - the beginning is very likely
190 " to be only one indentation level different.
195 let s:c0 = len(getline(s:l0))
202 " Apply the range we have found. Make sure to use the current visual mode.
203 call cursor(s:l0, s:c0)
204 exe "normal! " . vismode
205 call cursor(s:l1, s:c1)
208 " Update these static variables - we need to keep these up-to-date between
209 " invocations because it's the only way we can detect whether it's a new
210 " visual mode. We need to know if it's a new visual mode because otherwise
211 " if there's a single line block in visual line mode and we select it with
212 " "V", we can't tell whether it's already been selected using Vii.
214 let s:l0 = line("'<")
215 let s:l1 = line("'>")
222 function! <Sid>HandleTextObjectMapping(inner, incbelow, vis, range)
223 call <Sid>TextObject(a:inner, a:incbelow, a:vis, a:range, v:count1)