Refactor register save/restore

This commit is contained in:
Tom McDonald 2014-12-22 14:09:07 +00:00
parent fd0e870abd
commit fc78e3aa68

View File

@ -1,12 +1,8 @@
function! s:exchange(x, y, reverse, expand) function! s:exchange(x, y, reverse, expand)
let reg_z = getreg('z') let reg_z = s:save_reg('z')
let reg_z_mode = getregtype('z') let reg_unnamed = s:save_reg('"')
let reg_unnamed = getreg('"') let reg_star = s:save_reg('*')
let reg_unnamed_mode = getregtype('"') let reg_plus = s:save_reg('+')
let reg_star = getreg('*')
let reg_star_mode = getregtype('*')
let reg_plus = getreg('+')
let reg_plus_mode = getregtype('+')
let selection = &selection let selection = &selection
set selection=inclusive set selection=inclusive
@ -29,19 +25,16 @@ function! s:exchange(x, y, reverse, expand)
endif endif
let &selection = selection let &selection = selection
call setreg('z', reg_z, reg_z_mode) call s:restore_reg('z', reg_z)
call setreg('"', reg_unnamed, reg_unnamed_mode) call s:restore_reg('"', reg_unnamed)
call setreg('*', reg_star, reg_star_mode) call s:restore_reg('*', reg_star)
call setreg('+', reg_plus, reg_plus_mode) call s:restore_reg('+', reg_plus)
endfunction endfunction
function! s:exchange_get(type, vis) function! s:exchange_get(type, vis)
let reg = getreg('"') let reg = s:save_reg('"')
let reg_mode = getregtype('"') let reg_star = s:save_reg('*')
let reg_star = getreg('*') let reg_plus = s:save_reg('+')
let reg_star_mode = getregtype('*')
let reg_plus = getreg('+')
let reg_plus_mode = getregtype('+')
if a:vis if a:vis
let type = a:type let type = a:type
let [start, end] = s:store_pos("'<", "'>") let [start, end] = s:store_pos("'<", "'>")
@ -68,9 +61,9 @@ function! s:exchange_get(type, vis)
let &selection = selection let &selection = selection
endif endif
let text = getreg('@') let text = getreg('@')
call setreg('"', reg, reg_mode) call s:restore_reg('"', reg)
call setreg('*', reg_star, reg_star_mode) call s:restore_reg('*', reg_star)
call setreg('+', reg_plus, reg_plus_mode) call s:restore_reg('+', reg_plus)
return [text, type, start, end] return [text, type, start, end]
endfunction endfunction
@ -111,6 +104,14 @@ function! s:exchange_clear()
endif endif
endfunction endfunction
function! s:save_reg(name)
return [getreg(a:name), getregtype(a:name)]
endfunction
function! s:restore_reg(name, reg)
call setreg(a:name, a:reg[0], a:reg[1])
endfunction
function! s:highlight(exchange) function! s:highlight(exchange)
let [text, type, start, end] = a:exchange let [text, type, start, end] = a:exchange
let regions = [] let regions = []