├── README.md └── plugin └── uncrustify.vim /README.md: -------------------------------------------------------------------------------- 1 | # vim-uncrustify 2 | 3 | ## Installation 4 | 5 | ### With pathogen 6 | 7 | Download .tar.gz package, and extract it into ~/.vim/bundle or with `git`: 8 | 9 | ``` 10 | cd ~/.vim/bundle 11 | git clone https://github.com/Cofyc/vim-uncrustify.git 12 | ``` 13 | 14 | ### Without pathogen 15 | 16 | Simply extract package into your ~/.vim folder. 17 | 18 | ## Usage 19 | 20 | Add these in your `.vimrc` file: 21 | 22 | ``` 23 | autocmd FileType c noremap :call Uncrustify('c') 24 | autocmd FileType c vnoremap :call RangeUncrustify('c') 25 | autocmd FileType cpp noremap :call Uncrustify('cpp') 26 | autocmd FileType cpp vnoremap :call RangeUncrustify('cpp') 27 | ``` 28 | 29 | ## References 30 | 31 | - https://github.com/maksimr/vim-jsbeautify 32 | -------------------------------------------------------------------------------- /plugin/uncrustify.vim: -------------------------------------------------------------------------------- 1 | " vim-uncrustify 2 | " 3 | " Author: Yecheng Fu 4 | " 5 | 6 | " Define default "g:uncrustify_debug" {{{2 7 | if !exists("g:uncrustify_debug") 8 | let g:uncrustify_debug = 0 9 | endif 10 | 11 | " Specify path to your Uncrustify configuration file. 12 | let g:uncrustify_cfg_file_path = "auto" 13 | 14 | " Log debug message 15 | function! s:UncrustifyDebug(text) 16 | if g:uncrustify_debug 17 | echom "uncrustify: " . a:text 18 | endif 19 | endfunction 20 | 21 | " Don't forget to add Uncrustify executable to $PATH (on Unix) or 22 | " %PATH% (on Windows) for this command to work. 23 | " http://stackoverflow.com/a/15513829/288089 24 | " Restore cursor position, window position, and last search after running a 25 | " command. 26 | func! Uncrustify(...) 27 | let l:lang = get(a:000, 0, 'c') 28 | let l:start = get(a:000, 1, '1') 29 | let l:end = get(a:000, 2, '$') 30 | 31 | " Save the last search. 32 | let search = @/ 33 | 34 | " Save the current cursor position. 35 | let cursor_position = getpos('.') 36 | 37 | " Save the current window position. 38 | normal! H 39 | let window_position = getpos('.') 40 | call setpos('.', cursor_position) 41 | 42 | " Get content which need to format 43 | let content = getline(l:start, l:end) 44 | 45 | " Length of lines before formating 46 | let lines_length = len(getline(l:start, l:end)) 47 | 48 | " Write content to temporary file 49 | let l:tmpfile = tempname() . '.' . l:lang 50 | call s:UncrustifyDebug("tmpfile: " . l:tmpfile) 51 | call writefile(content, l:tmpfile) 52 | 53 | " Detect configuration file 54 | let l:cfgfile = "" 55 | if g:uncrustify_cfg_file_path == "auto" 56 | let l:cwdcfg = ".uncrustify.cfg" 57 | let l:homecfg = shellescape(fnamemodify('~/.uncrustify.cfg', ':p')) 58 | let l:gitcfg = substitute(system("git rev-parse --show-toplevel"), "\n", "", "g") . "/.uncrustify.cfg" 59 | if filereadable(l:cwdcfg) 60 | let l:cfgfile = l:cwdcfg 61 | elseif filereadable(l:gitcfg) 62 | let l:cfgfile = l:gitcfg 63 | elseif filereadable(l:homecfg) 64 | let l:cfgfile = l:homecfg 65 | endif 66 | else 67 | let l:cfgfile = g:uncrustify_cfg_file_path 68 | endif 69 | 70 | let cmd = "uncrustify -q -l " . l:lang . " -c " . l:cfgfile . " -f " . l:tmpfile . " -o " . l:tmpfile 71 | 72 | call s:UncrustifyDebug("cmd: ".cmd) 73 | call system(cmd) 74 | call s:UncrustifyDebug("shell_error: ". v:shell_error) 75 | 76 | if v:shell_error == 0 77 | let lines_uncrustify = readfile(l:tmpfile) 78 | silent exec l:start.",".l:end."j" 79 | call setline(l:start, lines_uncrustify[0]) 80 | call append(l:start, lines_uncrustify[1:]) 81 | else 82 | echom "uncrustify: failed to run, exit code: " . v:shell_error 83 | endif 84 | 85 | " Restore the last search. 86 | let @/ = search 87 | 88 | " Restore the previous window position. 89 | call setpos('.', window_position) 90 | normal! zt 91 | 92 | " Restore the previous cursor position. 93 | call setpos('.', cursor_position) 94 | 95 | " Delete the temporary file 96 | call delete(l:tmpfile) 97 | endfunc 98 | 99 | func! RangeUncrustify(language) range 100 | return call('Uncrustify', extend([a:language], [a:firstline, a:lastline])) 101 | endfunc 102 | --------------------------------------------------------------------------------