map <F8>  :make C=1 <CR>
"-Wno-unused-parameter
"-Wsuggest-attribute=pure -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wpadded
"-Wconversion
map <F9>  :make EXTRA_CFLAGS="-fstrict-aliasing -fstrict-overflow -Wstrict-overflow=4 -Wextra -Winit-self -Wwrite-strings -Wformat=2 -Wunsafe-loop-optimizations -Wlogical-op -Wshadow -Wsign-compare -Wno-unused-parameter -Wpadded" <CR>
map <F10> :make EXTRA_CFLAGS="-Wno-unused-but-set-variable" <CR>

func! ToggleTags()
        let l:t = substitute(bufname("%"), '.*/\zs', '.', '') . ".tags"
        if (filereadable(l:t))
                if count(tagfiles(), "tags")
                        let &l:tags = l:t
                else
                        let &l:tags = l:t . ",tags"
                endif
        endif
endfunc

map <expr> <F12> ToggleTags()

let s:ctags = "ctags --langmap=C++: --langmap=C:.c.h --languages=C --fields=+S --c-kinds=-m"
"let s:ctags = s:ctags . ' --c-kinds=+p --fields=+iaS --extra=+q'

func! FixCscope()
        if !cscope_connection()
                cscope add cscope.out
        endif
endfunc

func! MakeTags()
        let l:t = substitute(bufname("%"), '.*/\zs', '.', '') . ".tags"
        "let l:t = substitute(expand("%"), '\([^/]*\)$', '.\1.tags', '')

        let l:ctags = s:ctags . ' --c-kinds=+p -f ' . l:t

        if &filetype == "c" && getftime(bufname("%")) > getftime(l:t)
                redir => l:include
                silent! checkpath!
                redir END

                let l:includes = split(l:include, "\n")
                let l:includes[0] = expand("%")

                call filter(l:includes, 'v:val !~ "\\(NOT FOUND\\|(Already listed)\\| -->\\)"')
                call system(l:ctags . ' '. join(l:includes) . " &")
        endif
endfunc

func! MakeLinuxTags()
        let l:dirs = "arch/x86 block crypto drivers fs include init ipc kernel lib mm net security virt"
        let l:ctags = s:ctags . " -R "
        let l:cscope = "cscope -R -b -q -k -s "

        if &filetype == "c" && getftime(bufname("%")) > getftime("tags") + 600
                call system(l:cscope . '"' . l:dirs . '"' . " &")
                call system(l:ctags  . l:dirs . " &")
        endif
endfunc

func! LinuxStuff()
        if !filereadable("Kbuild") || !filereadable("Makefile")
                return
        endif

        set path=.,include,arch/x86/include

        if executable("gcc-4.6")
                set makeprg=make\ O=.linux\ CC=gcc-4.6\ %<.o
        elseif executable("gcc-4.5")
                set makeprg=make\ O=.linux\ CC=gcc-4.5\ %<.o
        else
                set makeprg=make\ O=.linux\ %<.o
        endif

        call MakeLinuxTags()

        augroup LinuxStuff
                au!
                au BufWritePost         * call MakeLinuxTags()
        augroup end
endfunc

call LinuxStuff()

func! Old(...)
        setlocal splitright
        let l:fname = exists("b:fname") ? b:fname : bufname("%")
        let l:n = exists("a:1") ? a:1 : "HEAD"

        let l:cmd = "read! git show " . l:n . ":" . l:fname
        let l:ftype = &filetype
        let l:rev = system("git rev-parse --verify --short " . l:n)
        let l:rev = strpart(l:rev, 0, stridx(l:rev, "\n")) . " " . l:n
        vnew
        exe l:cmd

        setlocal buftype=nofile
        setlocal bufhidden=wipe
        setlocal noswapfile
        setlocal nomodifiable
        let b:fname = l:fname
        let &l:statusline=l:rev . "%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P"
        let &l:filetype=l:ftype
endfunc

func! Diff(...)
        call Old(exists("a:1") ? a:1 : "HEAD")

        au BufDelete <buffer> diffoff!
        au BufDelete <buffer> set foldmethod=syntax
        "au BufDelete <buffer> foldclose!
        diffthis
        wincmd p
        diffthis
endfunc

command! -nargs=? Diff call Diff(<f-args>)
command! -nargs=? Old  call Old(<f-args>)

augroup vimrc
        au!
        au BufWritePost ~/.vimrc        source ~/.vimrc
        au BufRead,BufWritePost         * call FixCscope()
        au BufRead,BufWritePost         * call MakeTags()
        au BufRead                      * call ToggleTags()
        au BufRead                      * call LinuxStuff()
        au InsertEnter                  * nohl
augroup end

BcacheWiki: Vimrc (last edited 2011-08-01 16:19:25 by Kent)