Skip to content

Instantly share code, notes, and snippets.

@Dubhead
Created October 18, 2011 03:59
Show Gist options
  • Save Dubhead/1294577 to your computer and use it in GitHub Desktop.
Save Dubhead/1294577 to your computer and use it in GitHub Desktop.
StylizeLine
" 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
@Dubhead
Copy link
Author

Dubhead commented Oct 20, 2011

いろいろ改良→ https://gist.github.com/1300624

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment