2009-12-20 12:26:58 -05:00
|
|
|
"============================================================================
|
|
|
|
"File: c.vim
|
|
|
|
"Description: Syntax checking plugin for syntastic.vim
|
2009-12-22 06:54:24 -05:00
|
|
|
"Maintainer: Gregor Uhlenheuer <kongo2002 at gmail dot com>
|
2009-12-20 12:26:58 -05:00
|
|
|
"License: This program is free software. It comes without any warranty,
|
|
|
|
" to the extent permitted by applicable law. You can redistribute
|
|
|
|
" it and/or modify it under the terms of the Do What The Fuck You
|
|
|
|
" Want To Public License, Version 2, as published by Sam Hocevar.
|
|
|
|
" See http://sam.zoy.org/wtfpl/COPYING for more details.
|
|
|
|
"
|
|
|
|
"============================================================================
|
2009-12-21 06:50:47 -05:00
|
|
|
|
|
|
|
" in order to also check header files add this to your .vimrc:
|
|
|
|
" (this usually creates a .gch file in your source directory)
|
|
|
|
"
|
|
|
|
" let g:syntastic_c_check_header = 1
|
2010-03-25 20:09:10 -04:00
|
|
|
"
|
|
|
|
" to disable the search of included header files after special
|
|
|
|
" libraries like gtk and glib add this line to your .vimrc:
|
|
|
|
"
|
|
|
|
" let g:syntastic_c_no_include_search = 1
|
2010-03-26 18:26:47 -04:00
|
|
|
"
|
|
|
|
" alternatively you can set the buffer local variable b:syntastic_c_cflags.
|
|
|
|
" If this variable is set for the current buffer no search for additional
|
|
|
|
" libraries is done. I.e. set the variable like this:
|
|
|
|
"
|
|
|
|
" let b:syntastic_c_cflags = ' -I/usr/include/libsoup-2.4'
|
2009-12-21 06:50:47 -05:00
|
|
|
|
2009-12-20 14:08:31 -05:00
|
|
|
if exists('loaded_c_syntax_checker')
|
2009-12-20 12:26:58 -05:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let loaded_c_syntax_checker = 1
|
|
|
|
|
2009-12-20 14:08:31 -05:00
|
|
|
if !executable('gcc')
|
2009-12-20 12:26:58 -05:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
2010-04-01 10:42:30 -04:00
|
|
|
let s:save_cpo = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
2010-03-25 19:16:38 -04:00
|
|
|
" initialize handlers
|
|
|
|
function! s:Init()
|
|
|
|
let s:handlers = []
|
2010-04-01 14:12:41 -04:00
|
|
|
let s:cflags = {}
|
|
|
|
call s:RegHandler('\%(gtk\|glib\)', 's:CheckPKG',
|
|
|
|
\ ['gtk', 'gtk+-2.0', 'gtk+', 'glib-2.0', 'glib'])
|
|
|
|
call s:RegHandler('glade', 's:CheckPKG',
|
|
|
|
\ ['glade', 'libglade-2.0', 'libglade'])
|
2010-04-01 15:02:45 -04:00
|
|
|
call s:RegHandler('libsoup', 's:CheckPKG',
|
|
|
|
\ ['libsoup', 'libsoup-2.4', 'libsoup-2.2'])
|
2010-04-01 15:05:34 -04:00
|
|
|
call s:RegHandler('webkit', 's:CheckPKG',
|
|
|
|
\ ['webkit', 'webkit-1.0'])
|
2010-04-01 15:23:33 -04:00
|
|
|
call s:RegHandler('cairo', 's:CheckPKG',
|
|
|
|
\ ['cairo', 'cairo'])
|
|
|
|
call s:RegHandler('pango', 's:CheckPKG',
|
|
|
|
\ ['pango', 'pango'])
|
|
|
|
call s:RegHandler('libxml', 's:CheckPKG',
|
|
|
|
\ ['libxml', 'libxml-2.0', 'libxml'])
|
|
|
|
call s:RegHandler('freetype', 's:CheckPKG',
|
|
|
|
\ ['freetype', 'freetype2', 'freetype'])
|
2010-04-01 14:12:41 -04:00
|
|
|
call s:RegHandler('ruby', 's:CheckRuby', [])
|
|
|
|
call s:RegHandler('Python\.h', 's:CheckPython', [])
|
2010-03-25 19:16:38 -04:00
|
|
|
|
|
|
|
unlet! s:RegHandler
|
|
|
|
endfunction
|
|
|
|
|
2009-12-20 12:26:58 -05:00
|
|
|
function! SyntaxCheckers_c_GetLocList()
|
2009-12-20 13:02:08 -05:00
|
|
|
let makeprg = 'gcc -fsyntax-only %'
|
2009-12-20 12:26:58 -05:00
|
|
|
let errorformat = '%-G%f:%s:,%f:%l: %m'
|
|
|
|
|
2009-12-20 14:08:31 -05:00
|
|
|
if expand('%') =~? '.h$'
|
2009-12-20 13:19:53 -05:00
|
|
|
if exists('g:syntastic_c_check_header')
|
|
|
|
let makeprg = 'gcc -c %'
|
|
|
|
else
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2010-03-26 18:26:47 -04:00
|
|
|
if !exists('b:syntastic_c_cflags')
|
|
|
|
if !exists('g:syntastic_c_no_include_search') ||
|
|
|
|
\ g:syntastic_c_no_include_search != 1
|
|
|
|
let makeprg .= s:SearchHeaders(s:handlers)
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
let makeprg .= b:syntastic_c_cflags
|
2010-03-25 20:09:10 -04:00
|
|
|
endif
|
2010-02-08 10:44:31 -05:00
|
|
|
|
2009-12-20 12:26:58 -05:00
|
|
|
return SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat })
|
|
|
|
endfunction
|
2010-02-08 10:44:31 -05:00
|
|
|
|
2010-03-25 19:16:38 -04:00
|
|
|
" search the first 100 lines for include statements that are
|
|
|
|
" given in the s:handlers dictionary
|
|
|
|
function! s:SearchHeaders(handlers)
|
|
|
|
let includes = ''
|
2010-03-25 19:59:49 -04:00
|
|
|
let l:handlers = copy(a:handlers)
|
2010-03-25 19:42:54 -04:00
|
|
|
let files = []
|
|
|
|
|
|
|
|
" search current buffer
|
2010-03-25 19:16:38 -04:00
|
|
|
for i in range(100)
|
2010-03-25 19:59:49 -04:00
|
|
|
for handler in l:handlers
|
|
|
|
let line = getline(i)
|
2010-03-26 19:12:18 -04:00
|
|
|
if line =~# '^#include.*' . handler["regex"]
|
2010-04-01 14:12:41 -04:00
|
|
|
let includes .= call(handler["func"], handler["args"])
|
2010-03-25 19:59:49 -04:00
|
|
|
call remove(l:handlers, index(l:handlers, handler))
|
2010-03-26 19:12:18 -04:00
|
|
|
elseif line =~# '^#include\s\+"\S\+"'
|
2010-03-25 19:59:49 -04:00
|
|
|
call add(files, matchstr(line, '^#include\s\+"\zs\S\+\ze"'))
|
2010-02-08 10:44:31 -05:00
|
|
|
endif
|
|
|
|
endfor
|
2010-03-25 19:16:38 -04:00
|
|
|
endfor
|
2010-03-25 19:42:54 -04:00
|
|
|
|
|
|
|
" search included headers
|
|
|
|
for hfile in files
|
|
|
|
if hfile != ''
|
|
|
|
let filename = expand('%:p:h') . ((has('win32') || has('win64')) ?
|
|
|
|
\ '\' : '/') . hfile
|
2010-03-25 19:59:49 -04:00
|
|
|
try
|
|
|
|
let lines = readfile(filename, '', 100)
|
|
|
|
catch /E484/
|
|
|
|
continue
|
|
|
|
endtry
|
2010-03-25 19:42:54 -04:00
|
|
|
for line in lines
|
2010-03-25 19:59:49 -04:00
|
|
|
for handler in l:handlers
|
2010-03-26 19:12:18 -04:00
|
|
|
if line =~# '^#include.*' . handler["regex"]
|
2010-04-01 14:12:41 -04:00
|
|
|
let includes .= call(handler["func"], handler["args"])
|
2010-03-25 19:59:49 -04:00
|
|
|
call remove(l:handlers, index(l:handlers, handler))
|
2010-03-25 19:42:54 -04:00
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
2010-03-25 19:16:38 -04:00
|
|
|
return includes
|
|
|
|
endfunction
|
|
|
|
|
2010-04-01 14:12:41 -04:00
|
|
|
" try to find library with 'pkg-config'
|
|
|
|
" search possible libraries from first to last given
|
|
|
|
" argument until one is found
|
|
|
|
function! s:CheckPKG(name, ...)
|
2010-03-25 19:16:38 -04:00
|
|
|
if executable('pkg-config')
|
2010-04-01 14:12:41 -04:00
|
|
|
if !has_key(s:cflags, a:name)
|
|
|
|
for i in range(a:0)
|
|
|
|
let l:cflags = system('pkg-config --cflags '.a:000[i])
|
|
|
|
if v:shell_error == 0
|
|
|
|
let l:cflags = ' '.substitute(l:cflags, "\n", '', '')
|
|
|
|
let s:cflags[a:name] = l:cflags
|
|
|
|
return l:cflags
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
else
|
|
|
|
return s:cflags[a:name]
|
2010-03-25 19:16:38 -04:00
|
|
|
endif
|
2010-02-08 10:44:31 -05:00
|
|
|
endif
|
|
|
|
return ''
|
|
|
|
endfunction
|
2010-03-25 18:21:19 -04:00
|
|
|
|
2010-03-26 18:50:01 -04:00
|
|
|
" try to find the ruby headers with 'rbconfig'
|
2010-03-25 18:21:19 -04:00
|
|
|
function! s:CheckRuby()
|
|
|
|
if executable('ruby')
|
2010-03-25 19:16:38 -04:00
|
|
|
if !exists('s:ruby_flags')
|
|
|
|
let s:ruby_flags = system('ruby -r rbconfig -e '
|
|
|
|
\ . '''puts Config::CONFIG["archdir"]''')
|
|
|
|
let s:ruby_flags = substitute(s:ruby_flags, "\n", '', '')
|
|
|
|
let s:ruby_flags = ' -I' . s:ruby_flags
|
|
|
|
endif
|
|
|
|
return s:ruby_flags
|
2010-03-25 18:21:19 -04:00
|
|
|
endif
|
|
|
|
return ''
|
|
|
|
endfunction
|
2010-03-25 19:16:38 -04:00
|
|
|
|
2010-03-26 18:50:01 -04:00
|
|
|
" try to find the python headers with distutils
|
|
|
|
function! s:CheckPython()
|
|
|
|
if executable('python')
|
|
|
|
if !exists('s:python_flags')
|
|
|
|
let s:python_flags = system('python -c ''from distutils import '
|
|
|
|
\ . 'sysconfig; print sysconfig.get_python_inc()''')
|
|
|
|
let s:python_flags = substitute(s:python_flags, "\n", '', '')
|
|
|
|
let s:python_flags = ' -I' . s:python_flags
|
|
|
|
endif
|
|
|
|
return s:python_flags
|
|
|
|
endif
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2010-03-25 19:16:38 -04:00
|
|
|
" return a handler dictionary object
|
2010-04-01 14:12:41 -04:00
|
|
|
function! s:RegHandler(regex, function, args)
|
2010-03-25 19:16:38 -04:00
|
|
|
let handler = {}
|
|
|
|
let handler["regex"] = a:regex
|
2010-04-01 14:12:41 -04:00
|
|
|
let handler["func"] = function(a:function)
|
|
|
|
let handler["args"] = a:args
|
2010-03-25 19:16:38 -04:00
|
|
|
call add(s:handlers, handler)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:Init()
|
2010-04-01 10:42:30 -04:00
|
|
|
|
|
|
|
let &cpo = s:save_cpo
|
|
|
|
unlet s:save_cpo
|