set background=dark
set hlsearch
set autowrite
"set t_BE=
set cinoptions=:0,(0
filetype plugin indent on

" highlight trailing whitespace
let c_space_errors=1
" highlight WhitespaceEOL ctermbg=red guibg=red
" match WhitespaceEOL /\s\+$/

if has('mouse')
	set mouse=a
endif

if filereadable("build.ninja")
	set makeprg=ninja\ -j16
endif

if filereadable("build/build.ninja")
	set makeprg=ninja\ -j16\ -C\ build
endif

if filereadable("kernel/pid.c")
	set makeprg=kernel-build\ -d\ @
	set path+=/home/xslaby/linux/include/
	set path+=/home/xslaby/linux/include/uapi/
	set path+=/home/xslaby/linux/arch/x86/include/
	set path+=/home/xslaby/linux/arch/x86/include/uapi/
	set path+=/home/xslaby/build/bu/include/
	set path+=/home/xslaby/build/bu/arch/x86/include/

	if filereadable("/home/xslaby/build/bu/cscope.out")
		set nocsverb
		cs add /home/xslaby/build/bu/cscope.out
		set csverb
	endif
endif

set tags=tags;/

function InsertInterpreter(interp)
	call append(0, "#!/usr/bin/" . a:interp)
endfun

function SetExecutableBit(file)
	checktime
	exec "au FileChangedShell " . a:file . " :echo"
	setlocal autoread
	silent execute "!chmod a+x " . a:file
	checktime
	exec "au! FileChangedShell " . a:file
endfun

augroup filetype
	au! BufRead,BufNewFile _service	set filetype=xml
	au! BufRead,BufNewFile *.g	set filetype=antlr3
	au! BufRead,BufNewFile *.i	set filetype=c
	au! BufRead,BufNewFile *.ll	set filetype=llvm
	au! BufRead,BufNewFile *.td	set filetype=tablegen
	if filereadable("/usr/share/slaby-scripts/skeleton/skeleton.c")
		au BufNewFile CMakeLists.txt	:0r /usr/share/slaby-scripts/skeleton/CMakeLists.txt
		au BufNewFile meson.build	:0r /usr/share/slaby-scripts/skeleton/meson.build
		au BufNewFile meson.options	:0r /usr/share/slaby-scripts/skeleton/meson.options
		au BufNewFile *.c		:0r /usr/share/slaby-scripts/skeleton/skeleton.c
		au BufNewFile *.cpp		:0r /usr/share/slaby-scripts/skeleton/skeleton.cpp
		au BufNewFile *.pl		:0r /usr/share/slaby-scripts/skeleton/skeleton.pl
	endif
	au BufNewFile *.py		call InsertInterpreter("python3")
	au BufNewFile *.sh		call InsertInterpreter("bash")
	au BufNewFile *.{pl,py,sh}	call SetExecutableBit(expand("<afile>:p"))
augroup END

"autocmd FileType c set cin
"autocmd FileType gitcommit set tw=70 "nocin
"autocmd FileType gitcommit 1

imap <F3> <Esc>:wN<CR>
map <F3> :wN<CR>
imap <F4> <Esc>:wn<CR>
map <F4> :wn<CR>
imap <F5> <Esc>l[si
map <F5> [s
imap <F6> <Esc>l]si
map <F6> ]s
imap <F7> <Esc>:mak<CR>
map <F7> :mak<CR>
imap <C-F7> <Esc>:mak!<CR>
map <C-F7> :mak!<CR>
map <F11> :cp<CR>
map <F12> :cn<CR>
imap <C-K> <Esc>lc<End>
imap <C-U> <Esc>ld<Home>
map <C-K> d<End>
map <C-U> d<Home>
map <C-N> ?^@@<CR>V/^@@\\|\\%$<CR>:s/^[- ]//n<CR>
map <C-M> ?^@@<CR>V/^@@\\|\\%$<CR>:s/^[+ ]//n<CR>
