Skip to content

Instantly share code, notes, and snippets.

@kawakami-o3
Created October 6, 2012 05:58
Show Gist options
  • Save kawakami-o3/3844156 to your computer and use it in GitHub Desktop.
Save kawakami-o3/3844156 to your computer and use it in GitHub Desktop.
インデント処理
" 実現したい機能
" * >>, << でインデント調整
" * ^で先頭へ
" * リターンで">"を自動挿入
" * 言語用自動インデント機能の挿入
" Ref.
" http://www.ibm.com/developerworks/jp/linux/library/l-vim-script-1/
" http://www.ibm.com/developerworks/jp/linux/library/l-vim-script-2/
" http://nanasi.jp/articles/code/screen/cursor.html
"
let s:code_head = "> "
let s:indent = " "
function! s:GetIndent()
return matchstr(getline('.'), '^' . s:code_head . '\s*')
endfunction
function! s:MoveHead()
call cursor(line('.'), 1+strlen(s:GetIndent()))
endfunction
function! s:ShiftRight()
execute "s/^" . s:code_head . "/" . s:code_head . s:indent . "/"
call s:MoveHead()
endfunction
function! s:ShiftLeft()
if strlen(s:GetIndent()) > strlen(s:code_head)
execute "s/^" . s:code_head . s:indent . "/" . s:code_head . "/"
call s:MoveHead()
endif
endfunction
function! s:RetWithAutoInsert()
return match(getline('.'),'^'.s:code_head) == -1 ? "\<CR>" : "\<CR>" . s:code_head
endfunction
nnoremap <silent> >> :call <SID>ShiftRight()<CR>
nnoremap <silent> << :call <SID>ShiftLeft()<CR>
nnoremap <silent> ^ :call <SID>MoveHead()<CR>
inoremap <silent> <CR> <C-R>=<SID>RetWithAutoInsert()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment