| Misha Brukman | 831ad84 | 2005-04-24 17:05:04 +0000 | [diff] [blame] | 1 | " LLVM coding guidelines conformance for VIM | 
| Dan Gohman | 1b9dd24 | 2010-02-26 21:38:04 +0000 | [diff] [blame] | 2 | " $Revision$ | 
| Misha Brukman | ac7b456 | 2009-01-02 16:26:14 +0000 | [diff] [blame] | 3 | " | 
|  | 4 | " Maintainer: The LLVM Team, http://llvm.org | 
| Misha Brukman | 831ad84 | 2005-04-24 17:05:04 +0000 | [diff] [blame] | 5 | " WARNING:    Read before you source in all these commands and macros!  Some | 
| Misha Brukman | f1ed8ed | 2005-05-12 21:41:48 +0000 | [diff] [blame] | 6 | "             of them may change VIM behavior that you depend on. | 
| Misha Brukman | ac7b456 | 2009-01-02 16:26:14 +0000 | [diff] [blame] | 7 | " | 
|  | 8 | " You can run VIM with these settings without changing your current setup with: | 
|  | 9 | " $ vim -u /path/to/llvm/utils/vim/vimrc | 
|  | 10 |  | 
|  | 11 | " It's VIM, not VI | 
|  | 12 | set nocompatible | 
| Misha Brukman | 831ad84 | 2005-04-24 17:05:04 +0000 | [diff] [blame] | 13 |  | 
| Misha Brukman | 831ad84 | 2005-04-24 17:05:04 +0000 | [diff] [blame] | 14 | " A tab produces a 2-space indentation | 
| Dan Gohman | 2dca828 | 2009-01-04 00:05:43 +0000 | [diff] [blame] | 15 | set softtabstop=2 | 
| Misha Brukman | 831ad84 | 2005-04-24 17:05:04 +0000 | [diff] [blame] | 16 | set shiftwidth=2 | 
|  | 17 | set expandtab | 
|  | 18 |  | 
| Dan Gohman | 94fa92b | 2010-02-26 21:24:46 +0000 | [diff] [blame] | 19 | " Highlight trailing whitespace and lines longer than 80 columns. | 
|  | 20 | highlight LongLine ctermbg=DarkYellow guibg=DarkYellow | 
| Misha Brukman | ac7b456 | 2009-01-02 16:26:14 +0000 | [diff] [blame] | 21 | highlight WhitespaceEOL ctermbg=DarkYellow guibg=DarkYellow | 
| Dan Gohman | 94fa92b | 2010-02-26 21:24:46 +0000 | [diff] [blame] | 22 | if v:version >= 702 | 
|  | 23 | " Lines longer than 80 columns. | 
|  | 24 | au BufWinEnter * let w:m0=matchadd('LongLine', '\%>80v.\+', -1) | 
|  | 25 |  | 
|  | 26 | " Whitespace at the end of a line. This little dance suppresses | 
| Dan Gohman | b53daba | 2010-02-26 21:45:37 +0000 | [diff] [blame] | 27 | " whitespace that has just been typed. | 
| Dan Gohman | 94fa92b | 2010-02-26 21:24:46 +0000 | [diff] [blame] | 28 | au BufWinEnter * let w:m1=matchadd('WhitespaceEOL', '\s\+$', -1) | 
|  | 29 | au InsertEnter * call matchdelete(w:m1) | 
|  | 30 | au InsertEnter * let w:m2=matchadd('WhitespaceEOL', '\s\+\%#\@<!$', -1) | 
|  | 31 | au InsertLeave * call matchdelete(w:m2) | 
|  | 32 | au InsertLeave * let w:m1=matchadd('WhitespaceEOL', '\s\+$', -1) | 
|  | 33 | else | 
|  | 34 | au BufRead,BufNewFile * syntax match LongLine /\%>80v.\+/ | 
|  | 35 | au InsertEnter * syntax match WhitespaceEOL /\s\+\%#\@<!$/ | 
|  | 36 | au InsertLeave * syntax match WhitespaceEOL /\s\+$/ | 
|  | 37 | endif | 
| Misha Brukman | ac7b456 | 2009-01-02 16:26:14 +0000 | [diff] [blame] | 38 |  | 
| Dan Gohman | 455be5a | 2009-01-21 21:30:25 +0000 | [diff] [blame] | 39 | " Enable filetype detection | 
|  | 40 | filetype on | 
|  | 41 |  | 
| Misha Brukman | f1ed8ed | 2005-05-12 21:41:48 +0000 | [diff] [blame] | 42 | " Optional | 
|  | 43 | " C/C++ programming helpers | 
| Dan Gohman | 455be5a | 2009-01-21 21:30:25 +0000 | [diff] [blame] | 44 | augroup csrc | 
|  | 45 | au! | 
|  | 46 | autocmd FileType *      set nocindent smartindent | 
|  | 47 | autocmd FileType c,cpp  set cindent | 
|  | 48 | augroup END | 
| Dan Gohman | be631a3 | 2009-01-04 18:59:55 +0000 | [diff] [blame] | 49 | " Set a few indentation parameters. See the VIM help for cinoptions-values for | 
|  | 50 | " details.  These aren't absolute rules; they're just an approximation of | 
|  | 51 | " common style in LLVM source. | 
| Dan Gohman | d5ae136 | 2010-01-09 17:15:21 +0000 | [diff] [blame] | 52 | set cinoptions=:0,g0,(0,Ws,l1 | 
| Misha Brukman | f1ed8ed | 2005-05-12 21:41:48 +0000 | [diff] [blame] | 53 | " Add and delete spaces in increments of `shiftwidth' for tabs | 
|  | 54 | set smarttab | 
|  | 55 |  | 
| Misha Brukman | ac7b456 | 2009-01-02 16:26:14 +0000 | [diff] [blame] | 56 | " Highlight syntax in programming languages | 
|  | 57 | syntax on | 
|  | 58 |  | 
| Misha Brukman | 831ad84 | 2005-04-24 17:05:04 +0000 | [diff] [blame] | 59 | " LLVM Makefiles can have names such as Makefile.rules or TEST.nightly.Makefile, | 
|  | 60 | " so it's important to categorize them as such. | 
|  | 61 | augroup filetype | 
| Misha Brukman | ac7b456 | 2009-01-02 16:26:14 +0000 | [diff] [blame] | 62 | au! BufRead,BufNewFile *Makefile* set filetype=make | 
| Misha Brukman | 831ad84 | 2005-04-24 17:05:04 +0000 | [diff] [blame] | 63 | augroup END | 
|  | 64 |  | 
|  | 65 | " In Makefiles, don't expand tabs to spaces, since we need the actual tabs | 
|  | 66 | autocmd FileType make set noexpandtab | 
|  | 67 |  | 
|  | 68 | " Useful macros for cleaning up code to conform to LLVM coding guidelines | 
|  | 69 |  | 
|  | 70 | " Delete trailing whitespace and tabs at the end of each line | 
| Misha Brukman | 93bd1ca | 2009-01-08 02:17:30 +0000 | [diff] [blame] | 71 | command! DeleteTrailingWs :%s/\s\+$// | 
| Misha Brukman | 831ad84 | 2005-04-24 17:05:04 +0000 | [diff] [blame] | 72 |  | 
|  | 73 | " Convert all tab characters to two spaces | 
| Misha Brukman | ac7b456 | 2009-01-02 16:26:14 +0000 | [diff] [blame] | 74 | command! Untab :%s/\t/  /g | 
| Dan Gohman | 3155533 | 2009-01-21 21:47:51 +0000 | [diff] [blame] | 75 |  | 
|  | 76 | " Enable syntax highlighting for LLVM files. To use, copy | 
|  | 77 | " utils/vim/llvm.vim to ~/.vim/syntax . | 
|  | 78 | augroup filetype | 
|  | 79 | au! BufRead,BufNewFile *.ll     set filetype=llvm | 
|  | 80 | augroup END | 
|  | 81 |  | 
|  | 82 | " Enable syntax highlighting for tablegen files. To use, copy | 
|  | 83 | " utils/vim/tablegen.vim to ~/.vim/syntax . | 
|  | 84 | augroup filetype | 
|  | 85 | au! BufRead,BufNewFile *.td     set filetype=tablegen | 
|  | 86 | augroup END | 
| Dan Gohman | 94fa92b | 2010-02-26 21:24:46 +0000 | [diff] [blame] | 87 |  | 
| Bill Wendling | 0615dbe | 2013-02-28 06:43:24 +0000 | [diff] [blame] | 88 | " Enable syntax highlighting for reStructuredText files. To use, copy | 
|  | 89 | " rest.vim (http://www.vim.org/scripts/script.php?script_id=973) | 
|  | 90 | " to ~/.vim/syntax . | 
|  | 91 | augroup filetype | 
|  | 92 | au! BufRead,BufNewFile *.rst     set filetype=rest | 
|  | 93 | augroup END | 
|  | 94 |  | 
| Dan Gohman | 94fa92b | 2010-02-26 21:24:46 +0000 | [diff] [blame] | 95 | " Additional vim features to optionally uncomment. | 
|  | 96 | "set showcmd | 
|  | 97 | "set showmatch | 
|  | 98 | "set showmode | 
|  | 99 | "set incsearch | 
|  | 100 | "set ruler | 
| Dan Gohman | f34728a | 2010-08-26 18:12:22 +0000 | [diff] [blame] | 101 |  | 
| Dan Gohman | 5faac39 | 2010-10-26 23:24:54 +0000 | [diff] [blame] | 102 | " Clang code-completion support. This is somewhat experimental! | 
| Dan Gohman | f34728a | 2010-08-26 18:12:22 +0000 | [diff] [blame] | 103 |  | 
| Dan Gohman | 7e64c98 | 2010-08-27 15:15:31 +0000 | [diff] [blame] | 104 | " A path to a clang executable. | 
|  | 105 | let g:clang_path = "clang++" | 
| Dan Gohman | f34728a | 2010-08-26 18:12:22 +0000 | [diff] [blame] | 106 |  | 
|  | 107 | " A list of options to add to the clang commandline, for example to add | 
|  | 108 | " include paths, predefined macros, and language options. | 
|  | 109 | let g:clang_opts = [ | 
|  | 110 | \ "-x","c++", | 
|  | 111 | \ "-D__STDC_LIMIT_MACROS=1","-D__STDC_CONSTANT_MACROS=1", | 
|  | 112 | \ "-Iinclude" ] | 
|  | 113 |  | 
|  | 114 | function! ClangComplete(findstart, base) | 
|  | 115 | if a:findstart == 1 | 
|  | 116 | " In findstart mode, look for the beginning of the current identifier. | 
|  | 117 | let l:line = getline('.') | 
|  | 118 | let l:start = col('.') - 1 | 
|  | 119 | while l:start > 0 && l:line[l:start - 1] =~ '\i' | 
|  | 120 | let l:start -= 1 | 
|  | 121 | endwhile | 
|  | 122 | return l:start | 
|  | 123 | endif | 
|  | 124 |  | 
|  | 125 | " Get the current line and column numbers. | 
|  | 126 | let l:l = line('.') | 
|  | 127 | let l:c = col('.') | 
|  | 128 |  | 
|  | 129 | " Build a clang commandline to do code completion on stdin. | 
|  | 130 | let l:the_command = shellescape(g:clang_path) . | 
|  | 131 | \ " -cc1 -code-completion-at=-:" . l:l . ":" . l:c | 
|  | 132 | for l:opt in g:clang_opts | 
|  | 133 | let l:the_command .= " " . shellescape(l:opt) | 
|  | 134 | endfor | 
|  | 135 |  | 
|  | 136 | " Copy the contents of the current buffer into a string for stdin. | 
|  | 137 | " TODO: The extra space at the end is for working around clang's | 
|  | 138 | " apparent inability to do code completion at the very end of the | 
|  | 139 | " input. | 
|  | 140 | " TODO: Is it better to feed clang the entire file instead of truncating | 
|  | 141 | " it at the current line? | 
|  | 142 | let l:process_input = join(getline(1, l:l), "\n") . " " | 
|  | 143 |  | 
|  | 144 | " Run it! | 
|  | 145 | let l:input_lines = split(system(l:the_command, l:process_input), "\n") | 
|  | 146 |  | 
|  | 147 | " Parse the output. | 
|  | 148 | for l:input_line in l:input_lines | 
|  | 149 | " Vim's substring operator is annoyingly inconsistent with python's. | 
|  | 150 | if l:input_line[:11] == 'COMPLETION: ' | 
|  | 151 | let l:value = l:input_line[12:] | 
|  | 152 |  | 
|  | 153 | " Chop off anything after " : ", if present, and move it to the menu. | 
|  | 154 | let l:menu = "" | 
|  | 155 | let l:spacecolonspace = stridx(l:value, " : ") | 
|  | 156 | if l:spacecolonspace != -1 | 
|  | 157 | let l:menu = l:value[l:spacecolonspace+3:] | 
|  | 158 | let l:value = l:value[:l:spacecolonspace-1] | 
|  | 159 | endif | 
|  | 160 |  | 
| Dan Gohman | fa4a470 | 2010-08-27 15:16:09 +0000 | [diff] [blame] | 161 | " Chop off " (Hidden)", if present, and move it to the menu. | 
|  | 162 | let l:hidden = stridx(l:value, " (Hidden)") | 
|  | 163 | if l:hidden != -1 | 
|  | 164 | let l:menu .= " (Hidden)" | 
|  | 165 | let l:value = l:value[:l:hidden-1] | 
|  | 166 | endif | 
|  | 167 |  | 
| Dan Gohman | ca15841 | 2010-08-27 15:16:40 +0000 | [diff] [blame] | 168 | " Handle "Pattern". TODO: Make clang less weird. | 
| Dan Gohman | f34728a | 2010-08-26 18:12:22 +0000 | [diff] [blame] | 169 | if l:value == "Pattern" | 
|  | 170 | let l:value = l:menu | 
|  | 171 | let l:pound = stridx(l:value, "#") | 
|  | 172 | " Truncate the at the first [#, <#, or {#. | 
|  | 173 | if l:pound != -1 | 
|  | 174 | let l:value = l:value[:l:pound-2] | 
|  | 175 | endif | 
|  | 176 | endif | 
|  | 177 |  | 
|  | 178 | " Filter out results which don't match the base string. | 
|  | 179 | if a:base != "" | 
|  | 180 | if l:value[:strlen(a:base)-1] != a:base | 
|  | 181 | continue | 
|  | 182 | end | 
|  | 183 | endif | 
|  | 184 |  | 
|  | 185 | " TODO: Don't dump the raw input into info, though it's nice for now. | 
|  | 186 | " TODO: The kind string? | 
|  | 187 | let l:item = { | 
|  | 188 | \ "word": l:value, | 
|  | 189 | \ "menu": l:menu, | 
|  | 190 | \ "info": l:input_line, | 
|  | 191 | \ "dup": 1 } | 
|  | 192 |  | 
|  | 193 | " Report a result. | 
|  | 194 | if complete_add(l:item) == 0 | 
|  | 195 | return [] | 
|  | 196 | endif | 
|  | 197 | if complete_check() | 
|  | 198 | return [] | 
|  | 199 | endif | 
|  | 200 |  | 
|  | 201 | elseif l:input_line[:9] == "OVERLOAD: " | 
|  | 202 | " An overload candidate. Use a crazy hack to get vim to | 
|  | 203 | " display the results. TODO: Make this better. | 
|  | 204 | let l:value = l:input_line[10:] | 
|  | 205 | let l:item = { | 
|  | 206 | \ "word": " ", | 
|  | 207 | \ "menu": l:value, | 
|  | 208 | \ "info": l:input_line, | 
|  | 209 | \ "dup": 1} | 
|  | 210 |  | 
|  | 211 | " Report a result. | 
|  | 212 | if complete_add(l:item) == 0 | 
|  | 213 | return [] | 
|  | 214 | endif | 
|  | 215 | if complete_check() | 
|  | 216 | return [] | 
|  | 217 | endif | 
|  | 218 |  | 
|  | 219 | endif | 
|  | 220 | endfor | 
|  | 221 |  | 
|  | 222 |  | 
|  | 223 | return [] | 
|  | 224 | endfunction ClangComplete | 
|  | 225 |  | 
| Dan Gohman | 5faac39 | 2010-10-26 23:24:54 +0000 | [diff] [blame] | 226 | " This to enables the somewhat-experimental clang-based | 
|  | 227 | " autocompletion support. | 
|  | 228 | set omnifunc=ClangComplete |