Created
October 18, 2011 03:59
-
-
Save Dubhead/1294577 to your computer and use it in GitHub Desktop.
StylizeLine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" smartchr 的な動作は改行時にまとめてやれば済むでしょ、と思って簡単に作ってみた。 | |
" どうも実用的にできなかったのでgistに晒して終わりにする。 | |
function! StylizeLine(modifyBuffer) | |
let l:line = getline(".") | |
" Skip if comment line or preprocessor directive. | |
if (match(l:line, "^\\s*#") != -1) || (match(l:line, "^\\s*\/\/") != -1) | |
return l:line | |
endif | |
" single spaces before and after =+/|% | |
let l:line = substitute(l:line, "\\s*\\([=+/|%]\\)\\s*", " \\1 ", "g") | |
" single space before -!~ | |
let l:line = substitute(l:line, "\\s*\\([-!~]\\)", " \\1", "g") | |
" increment, and, or, equal, not equal | |
let l:line = substitute(l:line, "+\\s*+", "++", "g") | |
let l:line = substitute(l:line, "&\\s*&", "&&", "g") | |
let l:line = substitute(l:line, "|\\s*|", "||", "g") | |
let l:line = substitute(l:line, "=\\s*=", "==", "g") | |
let l:line = substitute(l:line, "!\\s*=", "!=", "g") | |
" if, for, while | |
let l:line = substitute(l:line, "^\\s*\zsif\\s*(", "if (", "g") | |
let l:line = substitute(l:line, "^\\s*\zsfor\\s*(", "for (", "g") | |
let l:line = substitute(l:line, "^\\s*\zswhile\\s*(", "while (", "g") | |
" += etc. | |
let l:line = substitute(l:line, "\\s*\\([-!~=+*/&|<>%]\\)\\s*=\\s*", " \\1= ", "g") | |
" semicolon and comma | |
let l:line = substitute(l:line, "\\s*\\([;,]\\)\\s*", "\\1 ", "g") | |
" spaces at EOL | |
let l:line = substitute(l:line, "\\s\\+$", "", "") | |
if a:modifyBuffer != 0 | |
call setline(".", l:line) | |
endif | |
return l:line | |
endfunction | |
" キーマップ例 (keymap suggestions) | |
noremap <M-s> :call StylizeLine(1)<CR> | |
inoremap <silent> <CR> <ESC>:call StylizeLine(1)<CR>o | |
inoremap <silent> <M-s> <ESC>:call StylizeLine(1)<CR>A |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
要改良: 空行で改行するとインデントが消える。