Merge pull request #36 from wilywampa/reindent

Add option to re-indent text after exchange
This commit is contained in:
Tom McDonald 2015-06-18 12:38:34 -04:00
commit 5a8e725a64
2 changed files with 59 additions and 0 deletions

View File

@ -75,6 +75,14 @@ g:exchange_no_mappings ~
If this variable is defined, the default mappings will not be created.
*g:exchange_indent*
g:exchange_indent ~
If this variable is defined and non-zero, linewise exchanges will swap
indents. Alternatively, setting the option to '==' will re-indent the
exchanged text with |==|. This behavior can be configured per buffer by
setting b:exchange_indent.
ISSUES AND TODO *exchange-issues*
*exchange-todo*

View File

@ -8,6 +8,14 @@ function! s:exchange(x, y, reverse, expand)
let selection = &selection
set selection=inclusive
let indent = (exists("b:exchange_indent") ? (b:exchange_indent !~ 0) :
\ (exists("g:exchange_indent") && (g:exchange_indent !~ 0)))
\ && a:x[1] ==# 'V' && a:y[1] ==# 'V'
if indent
let xindent = matchstr(getline(nextnonblank(a:y[2][1])), '^\s*')
let yindent = matchstr(getline(nextnonblank(a:x[2][1])), '^\s*')
endif
call setpos("'[", a:y[2])
call setpos("']", a:y[3])
call setreg('z', a:x[0], a:x[1])
@ -20,6 +28,15 @@ function! s:exchange(x, y, reverse, expand)
silent exe "normal! `[" . a:x[1] . "`]\"zp"
endif
if indent
let xlines = 1 + a:x[3][1] - a:x[2][1]
let ylines = a:expand ? xlines : 1 + a:y[3][1] - a:y[2][1]
if !a:expand
call s:reindent(a:x[2][1], ylines, yindent)
endif
call s:reindent(a:y[2][1] - xlines + ylines, xlines, xindent)
endif
if a:reverse
call cursor(a:x[2][1], a:x[2][2])
else
@ -33,6 +50,40 @@ function! s:exchange(x, y, reverse, expand)
call s:restore_reg('+', reg_plus)
endfunction
function! s:reindent(start, lines, new_indent)
if (exists('b:exchange_indent') ? b:exchange_indent : g:exchange_indent) == '=='
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
function! s:exchange_get(type, vis)
let reg = s:save_reg('"')
let reg_star = s:save_reg('*')