Skip to content

Commit

Permalink
Allow custom prefixes with list argument (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed Aug 21, 2014
1 parent 69d0f16 commit 03f7dc7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ When the line contains multiple occurrences of the characters, you can forward
the visual selection by repeating `a=`, or move backward with `aa=`. Both
mappings can be preceded by a count. Refer to the test cases for the details.

To define mappings with different prefixes other than `a` and `aa`, you can
pass an optional list containing forward prefix and backward prefix to
`after_object#enable` call as follows:

```vim
autocmd VimEnter * call after_object#enable([']', '['], '=', ':')
```

17 changes: 13 additions & 4 deletions autoload/after_object.vim
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ function! s:esc(c)
return substitute(substitute(a:c, ' ', '<space>', 'g'), '|', '<bar>', 'g')
endfunction

function! s:parse_args(args)
let lists = filter(copy(a:args), 'type(v:val) == type([])')
let chars = filter(copy(a:args), 'type(v:val) == type("")')
return [get(lists, '-1', ['a', 'aa']), chars]
endfunction

function! after_object#enable(...)
for c in a:000
for [p, b] in items({ 'a': 0, 'aa': 1 })
let [prefixes, chars] = s:parse_args(a:000)
let prefixes = map(prefixes[0 : 1], '[v:val, v:key]')
for c in chars
for [p, b] in prefixes
for [m, v] in items({ 'x': 1, 'o': 0 })
execute printf(
\ '%snoremap <silent> %s%s :<c-u>call <sid>after(%s, v:count1, %d, %d)<cr>',
Expand All @@ -88,8 +96,9 @@ function! after_object#enable(...)
endfunction

function! after_object#disable(...)
for c in a:000
for p in ['a', 'aa']
let [prefixes, chars] = s:parse_args(a:000)
for c in chars
for p in prefixes[0 : 1]
for m in ['x', 'o']
execute printf('%sunmap <silent> %s%s', m, p, s:esc(c))
endfor
Expand Down
39 changes: 39 additions & 0 deletions test/after_object.vader
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,42 @@ Expect:
banana = blue
right.

------------------------
~ Custom prefix ~
------------------------

Given (=s):
apple = apple = apple = apple

Before (Define after-objects with custom prefixes):
call after_object#enable([']', '['], '=', ':')
Do (Using custom prefix):
v]=]=]=[=U

Expect:
apple = apple = APPLE = APPLE

Before (You can put prefix list anywhere in the argument list):
call after_object#enable('=', ':', ['<c-a>', '-'])
Do (Using custom prefix):
v\<c-a>=\<c-a>=\<c-a>=-=U

Expect:
apple = apple = APPLE = APPLE

Before (Remove mappings with after_object#disable call):
call after_object#disable('=', ':', ['<c-a>', '-'])
Do (Mappings with custom prefixes are gone):
v\<c-a>=\<c-a>=\<c-a>=-=U

Expect:
apple = apple = apple = apple

Before (Remove mappings with after_object#disable call w/o list argument):
call after_object#disable('=', ':')
Do (Mappings are gone):
va=a=a=aa=U

Expect:
apple = apple = apple = apple

0 comments on commit 03f7dc7

Please sign in to comment.