1
0
Fork 0
Browse Source

Add smart tabbing

master
Gavin Howard 2 years ago
parent
commit
520e9c33cc
Signed by: gavin
GPG Key ID: C08038BDF280D33E
  1. 52
      autoload/spacealign.vim
  2. 15
      plugin/vim-spacealign.vim

52
autoload/spacealign.vim

@ -5,6 +5,14 @@ function! spacealign#isCommentOrString()
return synIDattr(synID(line("."), col("."), 0), "name") =~? "string\|comment"
endfunction
function! spacealign#isWhitespace(nbr)
return a:nbr == 32 || a:nbr == 9 || a:nbr == 11 || a:nbr == 12
endfunction
function! spacealign#charAtPos(line, col)
return char2nr(matchstr(a:line, '\%' . a:col . 'c.'))
endfunction
function! spacealign#findTabs(lnum, cin, curpos, linestop, ignoreempty)
let line = getline(a:lnum)
@ -103,6 +111,50 @@ function! spacealign#align(first, last, ignoreempty)
endfunction
function! spacealign#insertTab(lnum)
let tabstp = &tabstop
let linestop = 0
let usetab = 1
let savecursor = getcurpos()
let curpos = deepcopy(savecursor)
let curpos[2] = 1
let scol = savecursor[2]
let idx = savecursor[2] - 1
let line = getline(a:lnum)
let cin = cindent(a:lnum)
let [expectedtabs, linestop] = spacealign#findTabs(a:lnum, cin, curpos, 0, 1)
let ntabs = len(matchstr(line, "^[\t]*", 0))
let nspaces = len(matchstr(line, "^[\t]*[ ]*", 0)) - ntabs
let retval = len(line) > savecursor[2]
if (ntabs > expectedtabs && savecursor[2] >= ntabs) ||
\ (ntabs == expectedtabs && savecursor[2] > ntabs)
let s = strpart(s:spaces, 0, tabstp)
if retval
let line = strpart(line, 0, idx) . s . strpart(line, idx + 1)
let scol += tabstp
else
let line = strpart(line, 0, idx) . s
let scol += tabstp - 1
endif
call setline(a:lnum, line)
elseif retval
let scol += 1
endif
let savecursor[2] = scol
call setpos('.', savecursor)
return retval
endfunction
function! spacealign#aligncomments(first, last)
let et = &expandtab

15
plugin/vim-spacealign.vim

@ -10,14 +10,24 @@ function! s:SpaceAlignLine()
let l = line('.')
call spacealign#align(l, l, 1)
execute "normal! ^"
let dec = char2nr(matchstr(getline('.'), '\%' . col('.') . 'c.'))
if dec == 32 || dec == 9 || dec == 11 || dec == 12
let dec = spacealign#charAtPos(getline('.'), col('.'))
if spacealign#isWhitespace(dec)
startinsert!
else
startinsert
endif
endfunction
function! s:SpaceAlignTab()
let ncol = col('.') + 1
let doins = spacealign#insertTab(line('.'))
if doins
startinsert
else
startinsert!
endif
endfunction
command! -range SpaceAlign <line1>,<line2>call s:SpaceAlign()
command! -range SA <line1>,<line2>call s:SpaceAlign()
command! -range SAlign <line1>,<line2>call s:SpaceAlign()
@ -28,3 +38,4 @@ command! -range SAlignComment <line1>,<line2>call s:SpaceAlignComment()
autocmd FileType c,cpp,idl,yao,yvm exe 'inoremap <buffer> <silent> <CR> <CR><ESC>:call <SID>SpaceAlignLine()<CR>'
autocmd FileType c,cpp,idl,yao,yvm exe 'nnoremap <buffer> <silent> o o<ESC>:call <SID>SpaceAlignLine()<CR>'
autocmd FileType c,cpp,idl,yao,yvm exe 'nnoremap <buffer> <silent> O O<ESC>:call <SID>SpaceAlignLine()<CR>'
autocmd FileType c,cpp,idl,yao,yvm exe 'inoremap <buffer> <silent> <TAB> <TAB><ESC>:call <SID>SpaceAlignTab()<CR>'

Loading…
Cancel
Save