Refactored toggleMapping

This commit is contained in:
Dhruva Sagar
2014-05-30 11:49:13 +05:30
parent 9336261063
commit e7e806916f
2 changed files with 46 additions and 76 deletions

View File

@@ -32,90 +32,56 @@ function! s:SetBufferOptDefault(opt, val) "{{{2
endif endif
endfunction endfunction
function! s:Map(map, to, mode)
if !hasmapto(a:map, a:mode)
for l:mode in split(a:mode, '.\zs')
execute l:mode . 'map <buffer>' a:to a:map
endfor
endif
endfunction
function! s:UnMap(map, mode)
if !empty(maparg(a:map, a:mode))
for mode in split(a:mode, '.\zs')
execute l:mode . 'unmap <buffer>' a:map
endfor
endif
endfunction
function! s:ToggleMapping() "{{{2 function! s:ToggleMapping() "{{{2
let separator_map = g:table_mode_separator let separator_map = g:table_mode_separator
" '|' is a special character, we need to map <Bar> instead " '|' is a special character, we need to map <Bar> instead
if g:table_mode_separator ==# '|' | let separator_map = '<Bar>' | endif if g:table_mode_separator ==# '|' | let separator_map = '<Bar>' | endif
if tablemode#IsActive() if tablemode#IsActive()
execute 'inoremap <silent> <buffer>' separator_map call s:Map('<Plug>(table-mode-tableize)', separator_map, 'i')
\ separator_map . '<Esc>:call tablemode#TableizeInsertMode()<CR>a' call s:Map('<Plug>(table-mode-motion-up)', '{<Bar>', 'n')
call s:Map('<Plug>(table-mode-motion-down)', '}<Bar>', 'n')
call s:Map('<Plug>(table-mode-motion-left)', '[<Bar>', 'n')
call s:Map('<Plug>(table-mode-motion-right)', ']<Bar>', 'n')
if !hasmapto('<Plug>(table-mode-motion-up)', 'n') call s:Map('<Plug>(table-mode-cell-text-object-a)', 'a<Bar>', 'ox')
nmap <buffer> {<Bar> <Plug>(table-mode-motion-up) call s:Map('<Plug>(table-mode-cell-text-object-i)', 'i<Bar>', 'ox')
endif
if !hasmapto('<Plug>(table-mode-motion-down)', 'n') call s:Map('<Plug>(table-mode-realign)', '<Leader>tr', 'n')
nmap <buffer> }<Bar> <Plug>(table-mode-motion-down) call s:Map('<Plug>(table-mode-delete-row)', '<Leader>tdd', 'n')
endif call s:Map('<Plug>(table-mode-delete-column)', '<Leader>tdc', 'n')
if !hasmapto('<Plug>(table-mode-motion-left)', 'n') call s:Map('<Plug>(table-mode-add-formula)', '<Leader>tfa', 'n')
nmap <buffer> [<Bar> <Plug>(table-mode-motion-left) call s:Map('<Plug>(table-mode-eval-formula)', '<Leader>tfe', 'n')
endif call s:Map('<Plug>(table-mode-echo-cell)', '<Leader>t?', 'n')
if !hasmapto('<Plug>(table-mode-motion-right)', 'n')
nmap <buffer> ]<Bar> <Plug>(table-mode-motion-right)
endif
if !hasmapto('<Plug>(table-mode-cell-text-object-a)', 'o')
omap <buffer> a<Bar> <Plug>(table-mode-cell-text-object-a)
xmap <buffer> a<Bar> <Plug>(table-mode-cell-text-object-a)
endif
if !hasmapto('<Plug>(table-mode-cell-text-object-i)', 'o')
omap <buffer> i<Bar> <Plug>(table-mode-cell-text-object-i)
xmap <buffer> i<Bar> <Plug>(table-mode-cell-text-object-i)
endif
if !hasmapto('<Plug>(table-mode-realign)', 'n')
nmap <buffer> <Leader>tr <Plug>(table-mode-realign)
endif
if !hasmapto('<Plug>(table-mode-delete-row)', 'n')
nmap <buffer> <Leader>tdd <Plug>(table-mode-delete-row)
endif
if !hasmapto('<Plug>(table-mode-delete-column)', 'n')
nmap <buffer> <Leader>tdc <Plug>(table-mode-delete-column)
endif
if !hasmapto('<Plug>(table-mode-add-formula)', 'n')
nmap <buffer> <Leader>tfa <Plug>(table-mode-add-formula)
endif
if !hasmapto('<Plug>(table-mode-eval-formula)', 'n')
nmap <buffer> <Leader>tfe <Plug>(table-mode-eval-formula)
endif
if !hasmapto('<Plug>(table-mode-echo-cell)', 'n')
nmap <buffer> <Leader>t? <Plug>(table-mode-echo-cell)
endif
else else
if !empty(maparg(separator_map, 'i')) call s:UnMap(separator_map, 'i')
execute "iunmap <silent> <buffer> " . separator_map call s:UnMap('{<Bar>', 'n')
endif call s:UnMap('}<Bar>', 'n')
if !empty(maparg('{<Bar>', 'n')) call s:UnMap('[<Bar>', 'n')
nunmap <buffer> {<Bar> call s:UnMap(']<Bar>', 'n')
endif call s:UnMap('a<Bar>', 'o')
if !empty(maparg('}<Bar>', 'n')) call s:UnMap('i<Bar>', 'o')
nunmap <buffer> }<Bar> call s:UnMap('<Leader>tdd', 'n')
endif call s:UnMap('<Leader>tdc', 'n')
if !empty(maparg('[<Bar>', 'n')) call s:UnMap('<Leader>tfa', 'n')
nunmap <buffer> [<Bar> call s:UnMap('<Leader>tfe', 'n')
endif call s:UnMap('<Leader>t?', 'n')
if !empty(maparg(']<Bar>', 'n'))
nunmap <buffer> ]<Bar>
endif
if !empty(maparg('a<Bar>', 'o'))
ounmap <buffer> a<Bar>
endif
if !empty(maparg('i<Bar>', 'o'))
ounmap <buffer> i<Bar>
endif
if !empty(maparg('<Leader>tdd', 'n'))
nunmap <buffer> <Leader>tdd
endif
if !empty(maparg('<Leader>tdc', 'n'))
nunmap <buffer> <Leader>tdc
endif
if !empty(maparg('<Leader>tfa', 'n'))
nunmap <buffer> <Leader>tfa
endif
if !empty(maparg('<Leader>tfe', 'n'))
nunmap <buffer> <Leader>tfe
endif
if !empty(maparg('<Leader>t?', 'n'))
nunmap <buffer> <Leader>t?
endif
endif endif
endfunction endfunction

View File

@@ -92,6 +92,10 @@ command! TableAddFormula call tablemode#spreadsheet#formula#Add()
command! TableModeRealign call tablemode#table#Realign('.') command! TableModeRealign call tablemode#table#Realign('.')
command! TableEvalFormulaLine call tablemode#spreadsheet#formula#EvaluateFormulaLine() command! TableEvalFormulaLine call tablemode#spreadsheet#formula#EvaluateFormulaLine()
" '|' is a special character, we need to map <Bar> instead
if g:table_mode_separator ==# '|' | let separator_map = '<Bar>' | endif
execute 'inoremap <silent> <Plug>(table-mode-tableize)' separator_map . '<Esc>:call tablemode#TableizeInsertMode()<CR>a'
nnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR> nnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR>
xnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR> xnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR>
xnoremap <silent> <Plug>(table-mode-tableize-delimiter) :<C-U>call tablemode#TableizeByDelimiter()<CR> xnoremap <silent> <Plug>(table-mode-tableize-delimiter) :<C-U>call tablemode#TableizeByDelimiter()<CR>