2014-04-27 10:21:27 -04:00
|
|
|
function! s:exchange(x, y, reverse, expand)
|
2014-12-22 09:09:07 -05:00
|
|
|
let reg_z = s:save_reg('z')
|
|
|
|
let reg_unnamed = s:save_reg('"')
|
|
|
|
let reg_star = s:save_reg('*')
|
|
|
|
let reg_plus = s:save_reg('+')
|
2014-04-26 08:25:46 -04:00
|
|
|
let selection = &selection
|
|
|
|
set selection=inclusive
|
2013-12-06 00:26:36 -05:00
|
|
|
|
2014-04-08 17:09:22 -04:00
|
|
|
call setpos("'[", a:y[2])
|
|
|
|
call setpos("']", a:y[3])
|
2014-02-04 12:39:30 -05:00
|
|
|
call setreg('z', a:x[0], a:x[1])
|
2014-04-08 17:09:22 -04:00
|
|
|
silent exe "normal! `[" . a:y[1] . "`]\"zp"
|
2013-12-06 00:26:36 -05:00
|
|
|
|
2014-04-27 10:21:27 -04:00
|
|
|
if !a:expand
|
|
|
|
call setpos("'[", a:x[2])
|
|
|
|
call setpos("']", a:x[3])
|
|
|
|
call setreg('z', a:y[0], a:y[1])
|
|
|
|
silent exe "normal! `[" . a:x[1] . "`]\"zp"
|
|
|
|
endif
|
2013-12-06 00:26:36 -05:00
|
|
|
|
2014-01-01 11:32:54 -05:00
|
|
|
if a:reverse
|
|
|
|
call cursor(a:x[2][1], a:x[2][2])
|
|
|
|
else
|
|
|
|
call cursor(a:y[2][1], a:y[2][2])
|
|
|
|
endif
|
|
|
|
|
2014-04-26 08:25:46 -04:00
|
|
|
let &selection = selection
|
2014-12-22 09:09:07 -05:00
|
|
|
call s:restore_reg('z', reg_z)
|
|
|
|
call s:restore_reg('"', reg_unnamed)
|
|
|
|
call s:restore_reg('*', reg_star)
|
|
|
|
call s:restore_reg('+', reg_plus)
|
2013-12-06 00:26:36 -05:00
|
|
|
endfunction
|
|
|
|
|
2013-12-06 13:45:08 -05:00
|
|
|
function! s:exchange_get(type, vis)
|
2014-12-22 09:09:07 -05:00
|
|
|
let reg = s:save_reg('"')
|
|
|
|
let reg_star = s:save_reg('*')
|
|
|
|
let reg_plus = s:save_reg('+')
|
2013-12-06 00:26:36 -05:00
|
|
|
if a:vis
|
|
|
|
let type = a:type
|
|
|
|
let [start, end] = s:store_pos("'<", "'>")
|
2014-06-24 10:33:37 -04:00
|
|
|
silent normal! gvy
|
|
|
|
if &selection ==# 'exclusive' && start != end
|
|
|
|
let end[2] -= len(matchstr(@@, '\_.$'))
|
|
|
|
endif
|
2013-12-06 00:26:36 -05:00
|
|
|
else
|
2014-06-24 10:33:37 -04:00
|
|
|
let selection = &selection
|
|
|
|
let &selection = 'inclusive'
|
|
|
|
if a:type == 'line'
|
|
|
|
let type = 'V'
|
|
|
|
let [start, end] = s:store_pos("'[", "']")
|
|
|
|
silent exe "normal! '[V']y"
|
|
|
|
elseif a:type == 'block'
|
|
|
|
let type = "\<C-V>"
|
|
|
|
let [start, end] = s:store_pos("'[", "']")
|
|
|
|
silent exe "normal! `[\<C-V>`]y"
|
|
|
|
else
|
|
|
|
let type = 'v'
|
|
|
|
let [start, end] = s:store_pos("'[", "']")
|
|
|
|
silent exe "normal! `[v`]y"
|
|
|
|
endif
|
|
|
|
let &selection = selection
|
2013-12-06 00:26:36 -05:00
|
|
|
endif
|
2013-12-11 10:19:25 -05:00
|
|
|
let text = getreg('@')
|
2014-12-22 09:09:07 -05:00
|
|
|
call s:restore_reg('"', reg)
|
|
|
|
call s:restore_reg('*', reg_star)
|
|
|
|
call s:restore_reg('+', reg_plus)
|
2013-12-06 00:26:36 -05:00
|
|
|
return [text, type, start, end]
|
2013-12-05 12:50:07 -05:00
|
|
|
endfunction
|
|
|
|
|
2013-12-06 13:45:08 -05:00
|
|
|
function! s:exchange_set(type, ...)
|
|
|
|
if !exists('b:exchange')
|
|
|
|
let b:exchange = s:exchange_get(a:type, a:0)
|
2014-01-01 11:03:33 -05:00
|
|
|
let b:exchange_matches = s:highlight(b:exchange)
|
2013-12-06 13:45:08 -05:00
|
|
|
else
|
|
|
|
let exchange1 = b:exchange
|
|
|
|
let exchange2 = s:exchange_get(a:type, a:0)
|
2014-01-01 11:32:54 -05:00
|
|
|
let reverse = 0
|
2014-04-27 10:21:27 -04:00
|
|
|
let expand = 0
|
2013-12-06 13:45:08 -05:00
|
|
|
|
|
|
|
let cmp = s:compare(exchange1, exchange2)
|
2014-04-27 10:14:13 -04:00
|
|
|
if cmp == 'overlap'
|
2013-12-09 13:19:19 -05:00
|
|
|
echohl WarningMsg | echo "Exchange aborted: overlapping text" | echohl None
|
|
|
|
return s:exchange_clear()
|
2014-04-27 10:21:27 -04:00
|
|
|
elseif cmp == 'outer'
|
|
|
|
let [expand, reverse] = [1, 1]
|
|
|
|
let [exchange1, exchange2] = [exchange2, exchange1]
|
|
|
|
elseif cmp == 'inner'
|
|
|
|
let expand = 1
|
2014-04-27 10:14:13 -04:00
|
|
|
elseif cmp == 'gt'
|
2014-01-01 11:32:54 -05:00
|
|
|
let reverse = 1
|
2013-12-06 13:45:08 -05:00
|
|
|
let [exchange1, exchange2] = [exchange2, exchange1]
|
|
|
|
endif
|
|
|
|
|
2014-04-27 10:21:27 -04:00
|
|
|
call s:exchange(exchange1, exchange2, reverse, expand)
|
2013-12-06 13:45:08 -05:00
|
|
|
call s:exchange_clear()
|
|
|
|
endif
|
2013-12-05 12:50:07 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:exchange_clear()
|
2013-12-06 00:26:36 -05:00
|
|
|
unlet! b:exchange
|
2014-01-01 11:03:33 -05:00
|
|
|
if exists('b:exchange_matches')
|
|
|
|
call s:highlight_clear(b:exchange_matches)
|
|
|
|
unlet b:exchange_matches
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-12-22 09:09:07 -05:00
|
|
|
function! s:save_reg(name)
|
|
|
|
return [getreg(a:name), getregtype(a:name)]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:restore_reg(name, reg)
|
2014-12-22 09:10:56 -05:00
|
|
|
silent! call setreg(a:name, a:reg[0], a:reg[1])
|
2014-12-22 09:09:07 -05:00
|
|
|
endfunction
|
|
|
|
|
2014-01-01 11:03:33 -05:00
|
|
|
function! s:highlight(exchange)
|
|
|
|
let [text, type, start, end] = a:exchange
|
|
|
|
let regions = []
|
|
|
|
if type == "\<C-V>"
|
|
|
|
let blockstartcol = virtcol([start[1], start[2]])
|
|
|
|
let blockendcol = virtcol([end[1], end[2]])
|
|
|
|
if blockstartcol > blockendcol
|
|
|
|
let [blockstartcol, blockendcol] = [blockendcol, blockstartcol]
|
|
|
|
endif
|
|
|
|
let regions += map(range(start[1], end[1]), '[v:val, blockstartcol, v:val, blockendcol]')
|
|
|
|
else
|
|
|
|
let [startline, endline] = [start[1], end[1]]
|
|
|
|
if type ==# 'v'
|
|
|
|
let startcol = virtcol([startline, start[2]])
|
|
|
|
let endcol = virtcol([endline, end[2]])
|
|
|
|
elseif type ==# 'V'
|
|
|
|
let startcol = 1
|
|
|
|
let endcol = virtcol([end[1], '$'])
|
|
|
|
endif
|
|
|
|
let regions += [[startline, startcol, endline, endcol]]
|
|
|
|
endif
|
|
|
|
return map(regions, 's:highlight_region(v:val)')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:highlight_region(region)
|
|
|
|
let pattern = '\%'.a:region[0].'l\%'.a:region[1].'v\_.\{-}\%'.a:region[2].'l\(\%>'.a:region[3].'v\|$\)'
|
|
|
|
return matchadd('ExchangeRegion', pattern)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:highlight_clear(match)
|
|
|
|
for m in a:match
|
|
|
|
silent! call matchdelete(m)
|
|
|
|
endfor
|
2013-12-05 12:50:07 -05:00
|
|
|
endfunction
|
|
|
|
|
2013-12-09 13:19:19 -05:00
|
|
|
" Return < 0 if x comes before y in buffer,
|
|
|
|
" = 0 if x and y overlap in buffer,
|
|
|
|
" > 0 if x comes after y in buffer
|
2013-12-06 13:45:08 -05:00
|
|
|
function! s:compare(x, y)
|
2013-12-09 13:19:19 -05:00
|
|
|
let [xs, xe, xm, ys, ye, ym] = [a:x[2], a:x[3], a:x[1], a:y[2], a:y[3], a:y[1]]
|
|
|
|
let xe = s:apply_mode(xe, xm)
|
|
|
|
let ye = s:apply_mode(ye, ym)
|
|
|
|
|
2013-12-12 15:12:14 -05:00
|
|
|
" Compare two blockwise regions.
|
|
|
|
if xm == "\<C-V>" && ym == "\<C-V>"
|
|
|
|
if s:intersects(xs, xe, ys, ye)
|
2014-04-27 10:14:13 -04:00
|
|
|
return 'overlap'
|
2013-12-12 15:12:14 -05:00
|
|
|
endif
|
|
|
|
let cmp = xs[2] - ys[2]
|
2014-04-27 10:14:13 -04:00
|
|
|
return cmp <= 0 ? 'lt' : 'gt'
|
2013-12-12 15:12:14 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
" TODO: Compare a blockwise region with a linewise or characterwise region.
|
|
|
|
" NOTE: Comparing blockwise with characterwise has one exception:
|
|
|
|
" When the characterwise region spans only one line, it is like blockwise.
|
|
|
|
|
|
|
|
" Compare two linewise or characterwise regions.
|
2014-04-27 10:21:27 -04:00
|
|
|
if s:compare_pos(xs, ys) <= 0 && s:compare_pos(xe, ye) >= 0
|
|
|
|
return 'outer'
|
|
|
|
elseif s:compare_pos(ys, xs) <= 0 && s:compare_pos(ye, xe) >= 0
|
|
|
|
return 'inner'
|
|
|
|
elseif (s:compare_pos(xs, ye) <= 0 && s:compare_pos(ys, xe) <= 0) || (s:compare_pos(ys, xe) <= 0 && s:compare_pos(xs, ye) <= 0)
|
2013-12-09 13:19:19 -05:00
|
|
|
" x and y overlap in buffer.
|
2014-04-27 10:14:13 -04:00
|
|
|
return 'overlap'
|
2013-12-09 13:19:19 -05:00
|
|
|
endif
|
|
|
|
|
2014-04-27 10:14:13 -04:00
|
|
|
let cmp = s:compare_pos(xs, ys)
|
|
|
|
return cmp == 0 ? 'overlap' : cmp < 0 ? 'lt' : 'gt'
|
2013-12-09 13:19:19 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:compare_pos(x, y)
|
|
|
|
if a:x[1] == a:y[1]
|
|
|
|
return a:x[2] - a:y[2]
|
|
|
|
else
|
|
|
|
return a:x[1] - a:y[1]
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-12-12 15:12:14 -05:00
|
|
|
function! s:intersects(xs, xe, ys, ye)
|
|
|
|
if a:xe[2] < a:ys[2] || a:xe[1] < a:ys[1] || a:xs[2] > a:ye[2] || a:xs[1] > a:ye[1]
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-12-09 13:19:19 -05:00
|
|
|
function! s:apply_mode(pos, mode)
|
|
|
|
let pos = a:pos
|
2013-12-09 20:40:00 -05:00
|
|
|
if a:mode ==# 'V'
|
2013-12-09 13:19:19 -05:00
|
|
|
let pos[2] = col([pos[1], '$'])
|
|
|
|
endif
|
|
|
|
return pos
|
2013-12-06 13:45:08 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:store_pos(start, end)
|
|
|
|
return [getpos(a:start), getpos(a:end)]
|
|
|
|
endfunction
|
|
|
|
|
2013-12-21 22:51:50 -05:00
|
|
|
function! s:create_map(mode, lhs, rhs)
|
|
|
|
if !hasmapto(a:rhs, a:mode)
|
|
|
|
execute a:mode.'map '.a:lhs.' '.a:rhs
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-01-07 05:46:55 -05:00
|
|
|
highlight default link ExchangeRegion IncSearch
|
2014-01-01 11:03:33 -05:00
|
|
|
|
2013-12-22 10:05:38 -05:00
|
|
|
nnoremap <silent> <Plug>(Exchange) :<C-u>set opfunc=<SID>exchange_set<CR>g@
|
|
|
|
vnoremap <silent> <Plug>(Exchange) :<C-u>call <SID>exchange_set(visualmode(), 1)<CR>
|
|
|
|
nnoremap <silent> <Plug>(ExchangeClear) :<C-u>call <SID>exchange_clear()<CR>
|
|
|
|
nnoremap <silent> <Plug>(ExchangeLine) :<C-u>set opfunc=<SID>exchange_set<CR>g@_
|
2013-12-05 12:50:07 -05:00
|
|
|
|
2014-11-24 09:18:59 -05:00
|
|
|
command! ExchangeClear call s:exchange_clear()
|
|
|
|
|
2013-12-10 11:43:08 -05:00
|
|
|
if exists('g:exchange_no_mappings')
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
2013-12-22 10:05:38 -05:00
|
|
|
call s:create_map('n', 'cx', '<Plug>(Exchange)')
|
2014-06-24 10:28:41 -04:00
|
|
|
call s:create_map('x', 'X', '<Plug>(Exchange)')
|
2013-12-22 10:05:38 -05:00
|
|
|
call s:create_map('n', 'cxc', '<Plug>(ExchangeClear)')
|
|
|
|
call s:create_map('n', 'cxx', '<Plug>(ExchangeLine)')
|