Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
junegunn committed Aug 20, 2014
0 parents commit 22d9713
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: vim

before_script: |
git clone https://github.com/junegunn/vader.vim.git
script: |
vim -Nu <(cat << VIMRC
set rtp+=vader.vim
set rtp+=.
VIMRC) -c 'Vader! test/*' > /dev/null
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
vim-after-object ![travis-ci](https://travis-ci.org/junegunn/vim-after-object.svg?branch=master)
================

Defines text objects to target text *after* the designated characters.

Installation
------------

Using [vim-plug](https://github.com/junegunn/vim-plug):

```vim
Plug 'junegunn/vim-after-object'
```

Setting up
----------

vim-after-object does not define any mappings by default.
You have to enable mappings that you want.

```vim
" Define 'after text objects' on VimEnter
autocmd VimEnter * call after_object#enable('=', '-', ':', '#', ' ')
```

Usage
-----

```ruby
# va= visual after =
# ca= change after =
# da= delete after =
# ya= yank after =
apple = 'juice'
```

When the line contains multiple occurrences of the characters, you can forward
the visual selection by repeating `a=`, or move backward with `A=`. Both
mappings can be preceded by a count. Refer to the test cases for the details.

104 changes: 104 additions & 0 deletions autoload/after_object.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
" Copyright (c) 2014 Junegunn Choi
"
" MIT License
"
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the
" "Software"), to deal in the Software without restriction, including
" without limitation the rights to use, copy, modify, merge, publish,
" distribute, sublicense, and/or sell copies of the Software, and to
" permit persons to whom the Software is furnished to do so, subject to
" the following conditions:
"
" The above copyright notice and this permission notice shall be
" included in all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
" NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
" LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

if exists('g:loaded_after_object')
finish
endif
let g:loaded_after_object = 1

let s:cpo_save = &cpo
set cpo&vim

function! s:after(str, cnt, vis, bw)
let pos = getpos('.')
let col = a:vis ? col("'<") : col('.')
let line = getline('.')
let parts = split(line, '\V'.a:str.'\zs', 1)

try
if len(parts) == 1
throw 'exit'
endif

for i in reverse(range(1, a:cnt))
let len = 0
let idx = 0
for part in parts[0 : (a:bw ? -2 : -3)]
let len += len(part)
if len > col - 1
break
endif
let idx += 1
endfor

if a:bw
let idx = max([idx - 2, 0])
endif
let col = len(join(parts[0 : idx], '')) + (i > 1 && a:cnt > 1)
endfor

let rest = line[col : -1]
if empty(rest)
throw 'exit'
else
let idx = max([match(rest, '\S'), 0])
execute 'normal! '.(col + idx + 1).'|v$h'
endif
catch 'exit'
call setpos('.', pos)
if a:vis
normal! gv
endif
endtry
endfunction

function! s:esc(c)
" TODO: anything else?
return substitute(substitute(a:c, ' ', '<space>', 'g'), '|', '<bar>', 'g')
endfunction

function! after_object#enable(...)
for c in a:000
for [p, b] in items({ 'a': 0, 'A': 1 })
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>',
\ m, p, s:esc(c), string(s:esc(c)), v, b)
endfor
endfor
endfor
endfunction

function! after_object#disable(...)
for c in a:000
for p in ['a', 'A']
for m in ['x', 'o']
execute printf('%sunmap <silent> %s%s', m, p, s:esc(c))
endfor
endfor
endfor
endfunction

let &cpo = s:cpo_save
unlet s:cpo_save

108 changes: 108 additions & 0 deletions test/after_object.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
Execute (Setup):
call after_object#enable('=', ':')

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

Before:
let @q = 'da=j'
Do (da=):
100@q

Expect (Deleted after =):
apple=
apple=
apple=
apple =
apple =
apple =

Before:
let @q = "va=cx\<esc>j"
Do (va=cx):
100@q

Expect (x after =):
apple=x
apple= x
apple= x
apple =x
apple = x
apple = x

Given (:):
apple:apple
apple: apple
apple: apple
apple :apple
apple : apple
apple : apple

Before:
let @q = 'da:j'
Do (da:):
100@q

Expect (Deleted after :):
apple:
apple:
apple:
apple :
apple :
apple :

Before:
let @q = 'dA:j'
Do (dA:):
100@q

Expect (Deleted after :):
apple:
apple:
apple:
apple :
apple :
apple :

------------------------
~ Multiple occurrences ~
------------------------

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

Before:
Do (a=a=):
gUa=
gua=
gUa=

Expect:
apple = APPLE = apple = APPLE

Do (A=A=A=):
$v
A=
A=
d

Expect:
apple =

Do (With count (2)):
2da=

Expect:
apple = apple =

Do (With count (3)):
3da=

Expect:
apple = apple = apple =

0 comments on commit 22d9713

Please sign in to comment.