Added ykutw plugin.
[profile.git] / .vim / plugin / ykutw.vim
1 " Vim global plugin for making cw consistent with dw, yw, et al
2 " Licence:     The MIT License (MIT)
3 " {{{ Copyright (c) 2014 Aristotle Pagaltzis <pagaltzis@gmx.de>
4
5 " Permission is hereby granted, free of charge, to any person obtaining a copy
6 " of this software and associated documentation files (the "Software"), to deal
7 " in the Software without restriction, including without limitation the rights
8 " to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 " copies of the Software, and to permit persons to whom the Software is
10 " furnished to do so, subject to the following conditions:
11
12 " The above copyright notice and this permission notice shall be included in
13 " all copies or substantial portions of the Software.
14
15 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 " OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 " THE SOFTWARE.
22 " }}}
23
24 if exists('g:loaded_ykutw') || v:version < 700 | finish | endif
25 let g:loaded_ykutw = 1
26
27 function s:DoWordMotion(wordmotion, endmotion)
28         let before = line('.')
29         execute 'normal! v'.v:count1.a:wordmotion
30
31         " when the cursor wraps, we must check whether it went too far
32         if line('.') != before
33                 " try backing up to the end of the previous word
34                 " and then see if we stay on the same line
35                 let target = winsaveview()
36                 let before = line('.')
37                 exe 'normal! g'.a:endmotion
38                 if line('.') != before
39                         " we are now at the end of the word at the end of previous line,
40                         " which is exactly where we want to be
41                         return
42                 else
43                         " we were (almost) in the right place, so go back there
44                         call winrestview(target)
45                 endif
46         endif
47
48         " visual selections are inclusive; to avoid erasing the first char
49         " of the following word, we must back off one column
50         execute 'normal! h'
51 endfunction
52
53 onoremap w :<C-U>call <SID>DoWordMotion('w','e')<CR>
54 onoremap W :<C-U>call <SID>DoWordMotion('W','E')<CR>
55
56 " vim: fdm=marker fmr={{{,}}}