|
| 1 | +" smartword - Smart motions on words |
| 2 | +" Version: 0.0.2 |
| 3 | +" Copyright (C) 2008 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! smartword#move(motion_command, mode) "{{{2 |
| 26 | + let exclusive_adjustment_p = 0 |
| 27 | + if a:mode ==# 'o' && (a:motion_command ==# 'e' || a:motion_command ==# 'ge') |
| 28 | + if exists('v:motion_force') " if it's possible to check o_v and others: |
| 29 | + if v:motion_force == '' |
| 30 | + " inclusive |
| 31 | + normal! v |
| 32 | + elseif v:motion_force ==# 'v' |
| 33 | + " User-defined motion is always exclusive and o_v forces it inclusve. |
| 34 | + " So here use Visual mode to behave this motion as an exclusive one. |
| 35 | + normal! v |
| 36 | + let exclusive_adjustment_p = !0 |
| 37 | + else " v:motion_force ==# 'V' || v:motion_force ==# "\<C-v>" |
| 38 | + " linewise or blockwise |
| 39 | + " -- do nothing because o_V or o_CTRL-V will override the motion type. |
| 40 | + endif |
| 41 | + else " if it's not possible: |
| 42 | + " inclusive -- the same as the default style of "e" and "ge". |
| 43 | + " BUGS: o_v and others are ignored. |
| 44 | + normal! v |
| 45 | + endif |
| 46 | + endif |
| 47 | + if a:mode == 'v' |
| 48 | + normal! gv |
| 49 | + endif |
| 50 | + |
| 51 | + call s:move(a:motion_command, v:count1) |
| 52 | + |
| 53 | + if exclusive_adjustment_p |
| 54 | + execute "normal! \<Esc>" |
| 55 | + if getpos("'<") == getpos("'>") " no movement - select empty area. |
| 56 | + " FIXME: But how to select nothing? Because o_v was given, so at least |
| 57 | + " 1 character will be the target of the pending operator. |
| 58 | + else |
| 59 | + let original_whichwrap = &whichwrap |
| 60 | + set whichwrap=h |
| 61 | + normal! `> |
| 62 | + normal! h |
| 63 | + if col('.') == col('$') " FIXME: 'virtualedit' with onemore |
| 64 | + normal! h |
| 65 | + endif |
| 66 | + if a:motion_command ==# 'ge' |
| 67 | + " 'ge' is backward motion, |
| 68 | + " so that it's necessary to specify the target text in this way. |
| 69 | + normal! v`< |
| 70 | + endif |
| 71 | + let &whichwrap = original_whichwrap |
| 72 | + endif |
| 73 | + endif |
| 74 | +endfunction |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +" Misc. "{{{1 |
| 84 | +function! s:current_char(pos) "{{{2 |
| 85 | + return getline(a:pos[1])[a:pos[2] - 1] |
| 86 | +endfunction |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +function! s:move(motion_command, times) "{{{2 |
| 92 | + for i in range(v:count1) |
| 93 | + let curpos = [] " dummy |
| 94 | + let newpos = [] " dummy |
| 95 | + while !0 |
| 96 | + let curpos = newpos |
| 97 | + execute 'normal!' a:motion_command |
| 98 | + let newpos = getpos('.') |
| 99 | + |
| 100 | + if s:current_char(newpos) =~# '\k' |
| 101 | + break |
| 102 | + endif |
| 103 | + if curpos == newpos " No more word - stop. |
| 104 | + return |
| 105 | + endif |
| 106 | + endwhile |
| 107 | + endfor |
| 108 | + return |
| 109 | +endfunction |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +" __END__ "{{{1 |
| 119 | +" vim: foldmethod=marker |
0 commit comments