let g:syntastic_haskell_checkers = ['hdevtools', 'hlint']
let g:syntastic_aggregate_errors = 0
let g:syntastic_warning_symbol = "⚠"
let g:syntastic_error_symbol = "✗"
let g:syntastic_enable_signs = 1
let g:syntastic_enable_balloons = 1
let g:syntastic_enable_highlighting = 1
let g:syntastic_auto_jump = 3
let g:syntastic_auto_loc_list = 1
"let g:syntastic_haskell_ghc_mod_exec = $HOME.'/bin/ghc-mod.sh'
let g:syntastic_xml_checkers = ['xml_model']
let g:syntastic_xml_xml_model_exec = $HOME.'/bin/check-xml-model'
"set statusline+=%#warningmsg#
"set statusline+=%{SyntasticStatuslineFlag()}
"set statusline^=%{LanguageClient_statusLine()}
"set statusline+=%*
hi SyntasticWarning     ctermbg=5 ctermfg=0
hi SyntasticWarningSign ctermbg=5 ctermfg=0
" Toggle off Syntastic if there is a single Haskell file,
" since using ghci is quicker when tests must also be run.
" Note that the filetype is not already set at this point,
" hence the match on the buffer's name.
autocmd VimEnter * if match(map(getbufinfo(), "v:val.name"), '\.hs$') != -1
 \ | silent SyntasticToggleMode
 \ | endif

let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 0
let g:syntastic_check_on_wq = 0
let g:syntastic_debug = 0
let g:syntastic_enable_highlighting = 1

function! CabalCargs(args)
	let l:output = system('cabal-cargs ' . a:args)
	if v:shell_error != 0
			let l:lines = split(l:output, '\n')
			echohl ErrorMsg
			echomsg 'args: ' . a:args
			for l:line in l:lines
				echomsg l:line
			 endfor
			echohl None
			return ''
	else
	 endif
	return l:output
 endfunction
let g:cabalcargs_options = ''
function! HdevtoolsOptions()
	let l:args = CabalCargs('--format=hdevtools --sourcefile=' . shellescape(expand('%')) . ' ' . get(g:, 'cabalcargs_options', ''))
	if l:args != ''
		return l:args
	else
		let l:output = system('stack path --snapshot-pkg-db')
		if v:shell_error == 0
			" NOTE: when the "stack script" header is used
			" there is no often associated .cabal file,
			" passing a -g-package-db to stack's --snapshot-pkg-db
			" may help hdevtools to find required packages.
			return ' -g-package-db='.split(l:output, '\n')[0]
		else return ''
		 endif
	 endif
 endfunction
function! s:HlintOptions()
	return CabalCargs('--format=ghc --only=default_extensions --sourcefile=' . shellescape(expand('%')))
 endfunction
function! InitHaskellVars()
	if filereadable(expand('%')) && expand('%:t') != 'HLint.hs'
		let g:syntastic_haskell_hdevtools_args = HdevtoolsOptions() . ' -g-fno-warn-tabs'
		let g:hdevtools_options = '-g-XDatatypeContexts' . ' ' . get(g:, 'syntastic_haskell_hdevtools_args', '')
		" NOTE: DatatypeContexts is deprecated,
		" but running "hdevtools type" may fail by saying it requires it.
		let g:syntastic_haskell_hlint_args = s:HlintOptions() . '-i"Use camelCase"'
		let g:hlint_options = get(g:, 'syntastic_haskell_hlint_args', '')
	 endif
 endfunction
au FileType haskell :call InitHaskellVars()
" hdevtools
"au FileType haskell nmap <buffer> <F1> :HdevtoolsType<CR>
"au FileType haskell nmap <buffer> <silent> <F2> :HdevtoolsClear<CR>
"au FileType haskell nmap <buffer> <silent> <F3> :HdevtoolsInfo<CR>