Search code examples
vimclang-complete

Use an alternate .clang_complete file


The clang_complete plugin by default reads compiler flags from files in vim's path variable. This is not necessarily desirable, since path is used for other purposes, and it means potentially having to tinker with the variable, track the location of .clang_complete files, etc. It would be much simpler if there were, e.g., an option to just specify the files to include.

There isn't, but there's this:

clang_complete-auto_user_options
g:clang_auto_user_options

Set sources for user options passed to clang. Available sources are:

   [...]

{anything} else will be treaded as a custom option source in the following manner: clang_complete will try to load the autoload-function named getopts#{anything}#getopts, which then will be able to modify b:clang_user_options variable. See help on autoload if you don't know what it is.

So I've specified:

let g:clang_auto_user_options = "clangcompl"

And created a file autoload/getopts/clangcompl.vim with a function:

function clangcompl#getopts ()

This function loads, but clang_complete doesn't use it and I get the error:

Error detected while processing function <SNR>14_ClangCompleteInit..LoadUserOptions:
line   20:
E117: Unknown function: getopts#clangcompl#getopts

This is my first foray into vimscript, so any tips are appreciated. Where have I gone wrong?


Solution

  • You need to include the relative path, not just the file and function name, in the declaration:

    function getopts#clangcompl#getopts ()
            let files = expand("~/.vim/clang_opts/*", 0, 1)
            for f in files
                    let opts = readfile(f)
                    for o in opts
                            let b:clang_user_options .= " ".o
                    endfor
            endfor
    endfunction
    

    This will use anything in ~/.vim/clang_opts as a .clang_complete file.