2015-02-14 10:56:16 -05:00
|
|
|
let s:enable_highlighting = 1
|
|
|
|
|
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
|
|
|
|
2015-08-01 15:45:53 -04:00
|
|
|
let indent = s:get_setting('exchange_indent', 1) != 0 && a:x.type ==# 'V' && a:y.type ==# 'V'
|
2015-08-01 15:42:45 -04:00
|
|
|
|
2015-05-31 13:21:32 -04:00
|
|
|
if indent
|
2015-06-21 18:25:01 -04:00
|
|
|
let xindent = matchstr(getline(nextnonblank(a:y.start.line)), '^\s*')
|
|
|
|
let yindent = matchstr(getline(nextnonblank(a:x.start.line)), '^\s*')
|
2015-05-31 13:21:32 -04:00
|
|
|
endif
|
|
|
|
|
2015-08-01 15:19:51 -04:00
|
|
|
let view = winsaveview()
|
|
|
|
|
2015-06-21 18:25:01 -04:00
|
|
|
call s:setpos("'[", a:y.start)
|
|
|
|
call s:setpos("']", a:y.end)
|
|
|
|
call setreg('z', a:x.text, a:x.type)
|
2015-06-25 09:22:47 -04:00
|
|
|
silent execute "normal! `[" . a:y.type . "`]\"zp"
|
2013-12-06 00:26:36 -05:00
|
|
|
|
2014-04-27 10:21:27 -04:00
|
|
|
if !a:expand
|
2015-06-21 18:25:01 -04:00
|
|
|
call s:setpos("'[", a:x.start)
|
|
|
|
call s:setpos("']", a:x.end)
|
|
|
|
call setreg('z', a:y.text, a:y.type)
|
2015-06-25 09:22:47 -04:00
|
|
|
silent execute "normal! `[" . a:x.type . "`]\"zp"
|
2014-04-27 10:21:27 -04:00
|
|
|
endif
|
2013-12-06 00:26:36 -05:00
|
|
|
|
2015-05-31 13:21:32 -04:00
|
|
|
if indent
|
2015-06-21 18:25:01 -04:00
|
|
|
let xlines = 1 + a:x.end.line - a:x.start.line
|
|
|
|
let ylines = a:expand ? xlines : 1 + a:y.end.line - a:y.start.line
|
2015-06-16 21:43:26 -04:00
|
|
|
if !a:expand
|
2015-06-21 18:25:01 -04:00
|
|
|
call s:reindent(a:x.start.line, ylines, yindent)
|
2015-05-07 23:19:16 -04:00
|
|
|
endif
|
2015-06-21 18:25:01 -04:00
|
|
|
call s:reindent(a:y.start.line - xlines + ylines, xlines, xindent)
|
2015-05-07 23:19:16 -04:00
|
|
|
endif
|
|
|
|
|
2015-08-01 15:19:51 -04:00
|
|
|
call winrestview(view)
|
|
|
|
|
2015-08-01 15:21:17 -04:00
|
|
|
if !a:expand
|
|
|
|
call s:fix_cursor(a:x, a:y, a:reverse)
|
2014-01-01 11:32:54 -05:00
|
|
|
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
|
|
|
|
|
2015-08-01 15:21:17 -04:00
|
|
|
function! s:fix_cursor(x, y, reverse)
|
|
|
|
if a:reverse
|
|
|
|
call cursor(a:x.start.line, a:x.start.column)
|
|
|
|
else
|
|
|
|
if a:x.start.line == a:y.start.line
|
|
|
|
let horizontal_offset = a:x.end.column - a:y.end.column
|
|
|
|
call cursor(a:x.start.line, a:x.start.column - horizontal_offset)
|
|
|
|
else
|
|
|
|
let vertical_offset = a:x.end.line - a:y.end.line
|
|
|
|
call cursor(a:x.start.line - vertical_offset, a:x.start.column)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2015-05-07 23:19:16 -04:00
|
|
|
function! s:reindent(start, lines, new_indent)
|
2015-08-01 15:45:53 -04:00
|
|
|
if s:get_setting('exchange_indent', 1) == '=='
|
2015-05-07 23:19:16 -04:00
|
|
|
let lnum = nextnonblank(a:start)
|
|
|
|
let line = getline(lnum)
|
|
|
|
execute "silent normal! " . lnum . "G=="
|
|
|
|
let new_indent = matchstr(getline(lnum), '^\s*')
|
|
|
|
call setline(a:start, line)
|
|
|
|
else
|
|
|
|
let new_indent = a:new_indent
|
|
|
|
endif
|
|
|
|
let indent = matchstr(getline(nextnonblank(a:start)), '^\s*')
|
|
|
|
if strdisplaywidth(new_indent) > strdisplaywidth(indent)
|
|
|
|
for lnum in range(a:start, a:start + a:lines - 1)
|
|
|
|
if lnum =~ '\S'
|
|
|
|
call setline(lnum, new_indent . getline(lnum)[len(indent):])
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
elseif strdisplaywidth(new_indent) < strdisplaywidth(indent)
|
|
|
|
let can_dedent = 1
|
|
|
|
for lnum in range(a:start, a:start + a:lines - 1)
|
|
|
|
if stridx(getline(lnum), new_indent) != 0 && nextnonblank(lnum) == lnum
|
|
|
|
let can_dedent = 0
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
if can_dedent
|
|
|
|
for lnum in range(a:start, a:start + a:lines - 1)
|
|
|
|
if stridx(getline(lnum), new_indent) == 0
|
|
|
|
call setline(lnum, new_indent . getline(lnum)[len(indent):])
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
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
|
2015-06-21 18:25:01 -04:00
|
|
|
let end.column -= len(matchstr(@@, '\_.$'))
|
2014-06-24 10:33:37 -04:00
|
|
|
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("'[", "']")
|
2015-06-25 09:22:47 -04:00
|
|
|
silent execute "normal! '[V']y"
|
2014-06-24 10:33:37 -04:00
|
|
|
elseif a:type == 'block'
|
|
|
|
let type = "\<C-V>"
|
|
|
|
let [start, end] = s:store_pos("'[", "']")
|
2015-06-25 09:22:47 -04:00
|
|
|
silent execute "normal! `[\<C-V>`]y"
|
2014-06-24 10:33:37 -04:00
|
|
|
else
|
|
|
|
let type = 'v'
|
|
|
|
let [start, end] = s:store_pos("'[", "']")
|
2015-06-25 09:22:47 -04:00
|
|
|
silent execute "normal! `[v`]y"
|
2014-06-24 10:33:37 -04:00
|
|
|
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)
|
2015-06-21 18:25:01 -04:00
|
|
|
return {
|
|
|
|
\ 'text': text,
|
|
|
|
\ 'type': type,
|
|
|
|
\ 'start': start,
|
|
|
|
\ 'end': s:apply_type(end, type)
|
|
|
|
\ }
|
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)
|
2015-01-11 13:49:13 -05:00
|
|
|
" Tell tpope/vim-repeat that '.' should repeat the Exchange motion
|
|
|
|
silent! call repeat#invalidate()
|
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
|
|
|
|
|
2015-06-18 13:16:35 -04:00
|
|
|
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)
|
2015-06-18 13:04:36 -04:00
|
|
|
try
|
|
|
|
return [getreg(a:name), getregtype(a:name)]
|
|
|
|
catch /.*/
|
|
|
|
return ['', '']
|
|
|
|
endtry
|
2014-12-22 09:09:07 -05:00
|
|
|
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 regions = []
|
2015-06-21 18:25:01 -04:00
|
|
|
if a:exchange.type == "\<C-V>"
|
|
|
|
let blockstartcol = virtcol([a:exchange.start.line, a:exchange.start.column])
|
|
|
|
let blockendcol = virtcol([a:exchange.end.line, a:exchange.end.column])
|
2014-01-01 11:03:33 -05:00
|
|
|
if blockstartcol > blockendcol
|
|
|
|
let [blockstartcol, blockendcol] = [blockendcol, blockstartcol]
|
|
|
|
endif
|
2015-06-21 18:25:01 -04:00
|
|
|
let regions += map(range(a:exchange.start.line, a:exchange.end.line), '[v:val, blockstartcol, v:val, blockendcol]')
|
2014-01-01 11:03:33 -05:00
|
|
|
else
|
2015-06-21 18:25:01 -04:00
|
|
|
let [startline, endline] = [a:exchange.start.line, a:exchange.end.line]
|
|
|
|
if a:exchange.type ==# 'v'
|
|
|
|
let startcol = virtcol([a:exchange.start.line, a:exchange.start.column])
|
|
|
|
let endcol = virtcol([a:exchange.end.line, a:exchange.end.column])
|
|
|
|
elseif a:exchange.type ==# 'V'
|
2014-01-01 11:03:33 -05:00
|
|
|
let startcol = 1
|
2015-06-21 18:25:01 -04:00
|
|
|
let endcol = virtcol([a:exchange.end.line, '$'])
|
2014-01-01 11:03:33 -05:00
|
|
|
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\|$\)'
|
2015-02-14 10:56:16 -05:00
|
|
|
return matchadd('_exchange_region', pattern)
|
2014-01-01 11:03:33 -05:00
|
|
|
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
|
|
|
|
|
2015-02-14 10:56:16 -05:00
|
|
|
function! s:highlight_toggle(...)
|
|
|
|
if a:0 == 1
|
|
|
|
let s:enable_highlighting = a:1
|
|
|
|
else
|
|
|
|
let s:enable_highlighting = !s:enable_highlighting
|
|
|
|
endif
|
|
|
|
execute 'highlight link _exchange_region' (s:enable_highlighting ? 'ExchangeRegion' : 'None')
|
|
|
|
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-12 15:12:14 -05:00
|
|
|
" Compare two blockwise regions.
|
2015-06-21 18:25:01 -04:00
|
|
|
if a:x.type == "\<C-V>" && a:y.type == "\<C-V>"
|
|
|
|
if s:intersects(a:x, a:y)
|
2014-04-27 10:14:13 -04:00
|
|
|
return 'overlap'
|
2013-12-12 15:12:14 -05:00
|
|
|
endif
|
2015-06-21 18:25:01 -04:00
|
|
|
let cmp = a:x.start.column - a:y.start.column
|
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.
|
2015-06-21 18:25:01 -04:00
|
|
|
if s:compare_pos(a:x.start, a:y.start) <= 0 && s:compare_pos(a:x.end, a:y.end) >= 0
|
2014-04-27 10:21:27 -04:00
|
|
|
return 'outer'
|
2015-06-21 18:25:01 -04:00
|
|
|
elseif s:compare_pos(a:y.start, a:x.start) <= 0 && s:compare_pos(a:y.end, a:x.end) >= 0
|
2014-04-27 10:21:27 -04:00
|
|
|
return 'inner'
|
2015-06-21 18:25:01 -04:00
|
|
|
elseif (s:compare_pos(a:x.start, a:y.end) <= 0 && s:compare_pos(a:y.start, a:x.end) <= 0)
|
|
|
|
\ || (s:compare_pos(a:y.start, a:x.end) <= 0 && s:compare_pos(a:x.start, a:y.end) <= 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
|
|
|
|
|
2015-06-21 18:25:01 -04:00
|
|
|
let cmp = s:compare_pos(a:x.start, a:y.start)
|
2014-04-27 10:14:13 -04:00
|
|
|
return cmp == 0 ? 'overlap' : cmp < 0 ? 'lt' : 'gt'
|
2013-12-09 13:19:19 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:compare_pos(x, y)
|
2015-06-21 18:25:01 -04:00
|
|
|
if a:x.line == a:y.line
|
|
|
|
return a:x.column - a:y.column
|
2013-12-09 13:19:19 -05:00
|
|
|
else
|
2015-06-21 18:25:01 -04:00
|
|
|
return a:x.line - a:y.line
|
2013-12-09 13:19:19 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2015-06-21 18:25:01 -04:00
|
|
|
function! s:intersects(x, y)
|
|
|
|
if a:x.end.column < a:y.start.column || a:x.end.line < a:y.start.line
|
|
|
|
\ || a:x.start.column > a:y.end.column || a:x.start.line > a:y.end.line
|
2013-12-12 15:12:14 -05:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2015-06-21 18:25:01 -04:00
|
|
|
function! s:apply_type(pos, type)
|
2013-12-09 13:19:19 -05:00
|
|
|
let pos = a:pos
|
2015-06-21 18:25:01 -04:00
|
|
|
if a:type ==# 'V'
|
|
|
|
let pos.column = col([pos.line, '$'])
|
2013-12-09 13:19:19 -05:00
|
|
|
endif
|
|
|
|
return pos
|
2013-12-06 13:45:08 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:store_pos(start, end)
|
2015-06-21 18:25:01 -04:00
|
|
|
return [s:getpos(a:start), s:getpos(a:end)]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:getpos(mark)
|
|
|
|
let pos = getpos(a:mark)
|
|
|
|
let result = {}
|
|
|
|
return {
|
|
|
|
\ 'buffer': pos[0],
|
|
|
|
\ 'line': pos[1],
|
|
|
|
\ 'column': pos[2],
|
|
|
|
\ 'offset': pos[3]
|
|
|
|
\ }
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:setpos(mark, pos)
|
|
|
|
call setpos(a:mark, [a:pos.buffer, a:pos.line, a:pos.column, a:pos.offset])
|
2013-12-06 13:45:08 -05:00
|
|
|
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
|
|
|
|
|
2015-08-01 15:42:45 -04:00
|
|
|
function! s:get_setting(setting, default)
|
|
|
|
return get(b:, a:setting, get(g:, a:setting, a:default))
|
|
|
|
endfunction
|
|
|
|
|
2014-01-07 05:46:55 -05:00
|
|
|
highlight default link ExchangeRegion IncSearch
|
2014-01-01 11:03:33 -05:00
|
|
|
|
2015-06-25 09:22:47 -04:00
|
|
|
nnoremap <silent> <Plug>(Exchange) :<C-u>set operatorfunc=<SID>exchange_set<CR>g@
|
2013-12-22 10:05:38 -05:00
|
|
|
vnoremap <silent> <Plug>(Exchange) :<C-u>call <SID>exchange_set(visualmode(), 1)<CR>
|
|
|
|
nnoremap <silent> <Plug>(ExchangeClear) :<C-u>call <SID>exchange_clear()<CR>
|
2015-06-25 09:22:47 -04:00
|
|
|
nnoremap <silent> <Plug>(ExchangeLine) :<C-u>set operatorfunc=<SID>exchange_set<CR>g@_
|
2013-12-05 12:50:07 -05:00
|
|
|
|
2015-02-14 10:56:16 -05:00
|
|
|
command! XchangeHighlightToggle call s:highlight_toggle()
|
|
|
|
command! XchangeHighlightEnable call s:highlight_toggle(1)
|
|
|
|
command! XchangeHighlightDisable call s:highlight_toggle(0)
|
|
|
|
|
2015-02-14 11:00:42 -05:00
|
|
|
XchangeHighlightEnable
|
|
|
|
|
2015-02-14 10:38:48 -05:00
|
|
|
command! XchangeClear call s:exchange_clear()
|
2014-11-24 09:18:59 -05:00
|
|
|
|
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)')
|