Created
October 20, 2011 07:50
-
-
Save Dubhead/1300624 to your computer and use it in GitHub Desktop.
StylizeLine, take 2
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
" StylizeLine | |
" include guard | |
"if exists("g:loaded_StylizeLine") && g:loaded_StylizeLine | |
" finish | |
"endif | |
"let g:loaded_StylizeLine = 1 | |
function! StylizeLine(modifyBuffer, lineOffset) | |
let l:line = getline(line(".") + a:lineOffset) | |
" empty line | |
if match(l:line, "^\\s*$") != -1 | |
call setline(line(".") + a:lineOffset, "") | |
return "" | |
endif | |
" Skip comments and preprocessor directives. | |
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 (", "") | |
" let l:line = substitute(l:line, "^\\s*\zsfor\\s*(", "for (", "") | |
" let l:line = substitute(l:line, "^\\s*\zswhile\\s*(", "while (", "") | |
" += etc. | |
let l:line = substitute(l:line, "\\s*\\([-!~=+*/&|<>%]\\)\\s*=\\s*", " \\1= ", "g") | |
" << | |
let l:line = substitute(l:line, "\\s*<<\\s*", " << ", "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(line(".") + a:lineOffset, l:line) | |
endif | |
return l:line | |
endfunction | |
" suggested keymaps | |
noremap <M-s> :call StylizeLine(1, 0)<CR> | |
inoremap <silent> <CR> <CR> <BS><ESC>:call StylizeLine(1, -1)<CR>a | |
inoremap <silent> <M-s> <ESC>:call StylizeLine(1, 0)<CR>A | |
" eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment