Skip to content

Commit 8e94e5e

Browse files
committed
Added smartwords plugin
1 parent 3cc4a8f commit 8e94e5e

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

.vimrc

+15
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,18 @@ function! ToggleSearchCase()
147147
endf
148148

149149
nmap ss :call ToggleSearchCase()<CR>
150+
151+
" Smart words
152+
nmap w <Plug>(smartword-w)
153+
nmap b <Plug>(smartword-b)
154+
nmap e <Plug>(smartword-e)
155+
nmap ge <Plug>(smartword-ge)
156+
157+
xmap w <Plug>(smartword-w)
158+
xmap b <Plug>(smartword-b)
159+
xmap e <Plug>(smartword-e)
160+
xmap ge <Plug>(smartword-ge)
161+
162+
nnoremap cw cw
163+
nnoremap dw dw
164+
nnoremap yw yw

autoload/smartword.vim

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

plugin/smartword.vim

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
25+
if exists('g:loaded_smartword')
26+
finish
27+
endif
28+
29+
30+
31+
32+
nnoremap <silent> <Plug>(smartword-w) :<C-u>call smartword#move('w','n')<CR>
33+
nnoremap <silent> <Plug>(smartword-b) :<C-u>call smartword#move('b','n')<CR>
34+
nnoremap <silent> <Plug>(smartword-e) :<C-u>call smartword#move('e','n')<CR>
35+
nnoremap <silent> <Plug>(smartword-ge) :<C-u>call smartword#move('ge','n')<CR>
36+
37+
xnoremap <silent> <Plug>(smartword-w) :<C-u>call smartword#move('w','v')<CR>
38+
xnoremap <silent> <Plug>(smartword-b) :<C-u>call smartword#move('b','v')<CR>
39+
xnoremap <silent> <Plug>(smartword-e) :<C-u>call smartword#move('e','v')<CR>
40+
xnoremap <silent> <Plug>(smartword-ge) :<C-u>call smartword#move('ge','v')<CR>
41+
42+
onoremap <silent> <Plug>(smartword-w) :<C-u>call smartword#move('w','o')<CR>
43+
onoremap <silent> <Plug>(smartword-b) :<C-u>call smartword#move('b','o')<CR>
44+
onoremap <silent> <Plug>(smartword-e) :<C-u>call smartword#move('e','o')<CR>
45+
onoremap <silent> <Plug>(smartword-ge) :<C-u>call smartword#move('ge','o')<CR>
46+
47+
48+
49+
50+
let g:loaded_smartword = 1
51+
52+
" __END__
53+
" vim: foldmethod=marker

0 commit comments

Comments
 (0)