]> git.rmz.io Git - dotfiles.git/commitdiff
vim: google c++ indent style for cpp + syntax foldmethod
authorSamir Benmendil <samir.benmendil@gmail.com>
Wed, 4 Jun 2014 04:35:47 +0000 (05:35 +0100)
committerSamir Benmendil <samir.benmendil@gmail.com>
Wed, 4 Jun 2014 04:35:47 +0000 (05:35 +0100)
vim/ftplugin/cpp.vim [new file with mode: 0644]
vim/indent/cpp.vim [new file with mode: 0644]

diff --git a/vim/ftplugin/cpp.vim b/vim/ftplugin/cpp.vim
new file mode 100644 (file)
index 0000000..16ffc97
--- /dev/null
@@ -0,0 +1 @@
+setlocal foldmethod=syntax
diff --git a/vim/indent/cpp.vim b/vim/indent/cpp.vim
new file mode 100644 (file)
index 0000000..5484695
--- /dev/null
@@ -0,0 +1,69 @@
+"
+" VIM C++ Indent file.
+" Inspired by Google's C++ Style guide:
+" http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
+"
+
+if exists("b:did_indent")
+    finish
+endif
+let b:did_indent = 1
+
+
+function! GoogleCppIndent()
+    let l:cline_num = line('.')
+
+    let l:orig_indent = cindent(l:cline_num)
+
+    if l:orig_indent == 0 | return 0 | endif
+
+    let l:pline_num = prevnonblank(l:cline_num - 1)
+    let l:pline = getline(l:pline_num)
+    if l:pline =~# '^\s*template' | return l:pline_indent | endif
+
+    if l:orig_indent != &shiftwidth | return l:orig_indent | endif
+
+    let l:in_comment = 0
+    let l:pline_num = prevnonblank(l:cline_num - 1)
+    while l:pline_num > -1
+        let l:pline = getline(l:pline_num)
+        let l:pline_indent = indent(l:pline_num)
+
+        if l:in_comment == 0 && l:pline =~ '^.\{-}\(/\*.\{-}\)\@<!\*/'
+            let l:in_comment = 1
+        elseif l:in_comment == 1
+            if l:pline =~ '/\*\(.\{-}\*/\)\@!'
+                let l:in_comment = 0
+            endif
+        elseif l:pline_indent == 0
+            if l:pline !~# '\(#define\)\|\(^\s*//\)\|\(^\s*{\)'
+                if l:pline =~# '^\s*namespace.*'
+                    return 0
+                else
+                    return l:orig_indent
+                endif
+            elseif l:pline =~# '\\$'
+                return l:orig_indent
+            endif
+        else
+            return l:orig_indent
+        endif
+
+        let l:pline_num = prevnonblank(l:pline_num - 1)
+    endwhile
+
+    return l:orig_indent
+endfunction
+
+setlocal shiftwidth=2
+setlocal tabstop=2
+setlocal softtabstop=2
+setlocal expandtab
+setlocal textwidth=80
+
+setlocal cindent
+setlocal cinoptions=h1,l1,g1,t0,i4,+4,(0,w1,W4
+
+setlocal indentexpr=GoogleCppIndent()
+
+let b:undo_indent = "setl sw< ts< sts< et< tw< wrap< cin< cino< inde<"