|
| 1 | +" textobj-indent - Text objects for indented blocks of lines |
| 2 | +" Version: 0.0.3 |
| 3 | +" Copyright (C) 2009 kana <http://whileimautomaton.net/> |
| 4 | +" License: MIT license {{{ |
| 5 | +" Permission is hereby granted, free of charge, to any person obtaining |
| 6 | +" a copy of this software and associated documentation files (the |
| 7 | +" "Software"), to deal in the Software without restriction, including |
| 8 | +" without limitation the rights to use, copy, modify, merge, publish, |
| 9 | +" distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | +" permit persons to whom the Software is furnished to do so, subject to |
| 11 | +" the following conditions: |
| 12 | +" |
| 13 | +" The above copyright notice and this permission notice shall be included |
| 14 | +" in all copies or substantial portions of the Software. |
| 15 | +" |
| 16 | +" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 | +" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | +" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | +" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 | +" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 | +" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 | +" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | +" }}} |
| 24 | +" Interface "{{{1 |
| 25 | +function! textobj#indent#select_a() "{{{2 |
| 26 | + return s:select(!0, 'same-or-deep') |
| 27 | +endfunction |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +function! textobj#indent#select_i() "{{{2 |
| 33 | + return s:select(!!0, 'same-or-deep') |
| 34 | +endfunction |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +function! textobj#indent#select_same_a() "{{{2 |
| 40 | + return s:select(!0, 'same') |
| 41 | +endfunction |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +function! textobj#indent#select_same_i() "{{{2 |
| 47 | + return s:select(!!0, 'same') |
| 48 | +endfunction |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +" Misc. "{{{1 |
| 58 | +" Constants "{{{2 |
| 59 | +let s:EMPTY_LINE = -1 |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +function! s:select(include_empty_lines_p, block_border_type) "{{{2 |
| 65 | + " Check the indentation level of the current or below line. |
| 66 | + let cursor_linenr = line('.') |
| 67 | + let base_linenr = cursor_linenr |
| 68 | + while !0 |
| 69 | + let base_indent = s:indent_level_of(base_linenr) |
| 70 | + if base_indent != s:EMPTY_LINE || base_linenr == line('$') |
| 71 | + break |
| 72 | + endif |
| 73 | + let base_linenr += 1 |
| 74 | + endwhile |
| 75 | + |
| 76 | + " Check the end of a block. |
| 77 | + let end_linenr = base_linenr + 1 |
| 78 | + while end_linenr <= line('$') |
| 79 | + let end_indent = s:indent_level_of(end_linenr) |
| 80 | + if s:block_border_p(end_indent, base_indent, |
| 81 | + \ a:include_empty_lines_p, a:block_border_type) |
| 82 | + break |
| 83 | + endif |
| 84 | + let end_linenr += 1 |
| 85 | + endwhile |
| 86 | + let end_linenr -= 1 |
| 87 | + |
| 88 | + " Check the start of a block. |
| 89 | + let start_linenr = base_linenr |
| 90 | + while 1 <= start_linenr |
| 91 | + let start_indent = s:indent_level_of(start_linenr) |
| 92 | + if s:block_border_p(start_indent, base_indent, |
| 93 | + \ a:include_empty_lines_p, a:block_border_type) |
| 94 | + break |
| 95 | + endif |
| 96 | + let start_linenr -= 1 |
| 97 | + endwhile |
| 98 | + let start_linenr += 1 |
| 99 | + if line('$') < start_linenr |
| 100 | + let start_linenr = line('$') |
| 101 | + endif |
| 102 | + |
| 103 | + " Select the cursor line only |
| 104 | + " if <Plug>(textobj-indent-i) is executed in the last empty lines. |
| 105 | + if ((!a:include_empty_lines_p) |
| 106 | + \ && start_linenr == end_linenr |
| 107 | + \ && start_indent == s:EMPTY_LINE) |
| 108 | + let start_linenr = cursor_linenr |
| 109 | + let end_linenr = cursor_linenr |
| 110 | + endif |
| 111 | + |
| 112 | + return ['V', |
| 113 | + \ [0, start_linenr, 1, 0], |
| 114 | + \ [0, end_linenr, len(getline(end_linenr)) + 1, 0]] |
| 115 | +endfunction |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +function! s:indent_level_of(linenr) "{{{2 |
| 121 | + let _ = getline(a:linenr) |
| 122 | + if _ == '' |
| 123 | + return s:EMPTY_LINE |
| 124 | + else |
| 125 | + return indent(a:linenr) |
| 126 | + endif |
| 127 | +endfunction |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +function! s:block_border_p(indent,base_indent,include_empty_lines_p,type) "{{{2 |
| 133 | + if a:type ==# 'same-or-deep' |
| 134 | + return a:include_empty_lines_p |
| 135 | + \ ? a:indent != s:EMPTY_LINE && a:indent < a:base_indent |
| 136 | + \ : a:indent == s:EMPTY_LINE || a:indent < a:base_indent |
| 137 | + elseif a:type ==# 'same' |
| 138 | + return a:include_empty_lines_p |
| 139 | + \ ? a:indent != s:EMPTY_LINE && a:indent != a:base_indent |
| 140 | + \ : a:indent == s:EMPTY_LINE || a:indent != a:base_indent |
| 141 | + else |
| 142 | + echoerr 'Unexpected type:' string(a:type) |
| 143 | + return 0 |
| 144 | + endif |
| 145 | +endfunction |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +" __END__ "{{{1 |
| 155 | +" vim: foldmethod=marker |
0 commit comments