" Last change: 09.02.2026, 10:00
" erik@defiant.homedns.org

set nocp
syntax on
set number
set ignorecase
set smartcase
set nofoldenable
set spelllang=de
set nohlsearch
set noincsearch
set ruler
set tabpagemax=50
set wildmenu
set mousemodel=popup
set nohidden
set completeopt-=preview
set history=200

map <C-q> :q<CR>
map <C-s> :w<CR>
map <F7> :make<CR>

" map Shift+Cursor to same without Shift
map <S-Up> <Up>
map <S-Down> <Down>
map <S-Left> <Left>
map <S-Right> <Right>

" Alternative tab navigation
nmap <Tab> :tabnext<CR>
nmap <S-Tab> :tabprev<CR>

" http://vim.wikia.com/wiki/Recover_from_accidental_Ctrl-U
inoremap <c-u> <c-g>u<c-u>
inoremap <c-w> <c-g>u<c-w>

" search & replace in visual block
vnoremap : :s/\%V

" make
command! EnmakeC :setlocal makeprg=gcc\ -g\ -Wall\ -Wextra\ -o\ %<\ %
command! EnmakeTex :setlocal makeprg=pdflatex\ %

" http://vim.wikia.com/wiki/VimTip149
" If doing a diff. Upon writing changes to file, automatically update the differences
au BufWritePost * if &diff == 1 | :diffupdate | endif

" indent
function! FIndent2Sp()
	let oldLine=line('.')	
	setlocal tabstop=2
	setlocal shiftwidth=2
	setlocal expandtab
	retab
	normal(gg=G)
	execute ':' . oldLine	
endfunction
function! FIndent8Tab()
	let oldLine=line('.')	
	setlocal tabstop=8
	setlocal shiftwidth=8
	setlocal noexpandtab
	retab
	normal(gg=G)
	execute ':' . oldLine	
endfunction

let lReIndent = []
command! Indent2Sp call FIndent2Sp()
command! Indent8Tap call FIndent8Tab()
au BufWinEnter * if &tabstop == 2 | call add(lReIndent, expand("%:p")) | call FIndent8Tab() | endif
au BufWritePre * if index(lReIndent, expand("%:p")) >= 0 | call FIndent2Sp() | endif
au BufWritePost * if index(lReIndent, expand("%:p")) >= 0 | call FIndent8Tab() | endif

" hide special #ifdef
"
"set foldmethod=syntax
"let c_no_comment_fold=1
"let c_no_if0_fold=1
"let c_no_block_fold=1
"syn region cIfdefPC start="#ifdef PC" end="#endif" transparent fold contains=ALL

" ,a/,x 
syn region MySkip contained start="^\s*#\s*\(if\>\|ifdef\>\|ifndef\>\)" skip="\\$" end="^\s*#\s*endif\>" contains=MySkip

let g:CommentDefines = ""

hi link MyCommentOut2 MyCommentOut
hi link MySkip MyCommentOut
hi link MyCommentOut Comment

map <silent> ,a :call AddCommentDefine()<CR>
map <silent> ,x :call ClearCommentDefine()<CR>

function! AddCommentDefine()
	let g:CommentDefines = "\\(" . expand("<cword>") . "\\)"
	syn clear MyCommentOut
	syn clear MyCommentOut2
	exe 'syn region MyCommentOut start="^\s*#\s*ifdef\s\+' . g:CommentDefines . '\>" end=".\|$" contains=MyCommentOut2'
	exe 'syn region MyCommentOut2 contained start="' . g:CommentDefines . '" end="^\s*#\s*\(endif\>\|else\>\|elif\>\)" contains=MySkip'
endfunction

function! ClearCommentDefine()
	let g:ClearCommentDefine = ""
	syn clear MyCommentOut
	syn clear MyCommentOut2
endfunction

" vim -b : edit binary using xxd-format!
"augroup Binary
"	au!
"	au BufReadPost * if &bin | %!xxd
"	au BufReadPost * setlocal ft=xxd | endif
"	au BufWritePre * if &bin | %!xxd -r
"	au BufWritePre * endif
"	au BufWritePost * if &bin | %!xxd
"	au BufWritePost * setlocal nomod | endif
"augroup END

" for yankring
if has('viminfo')
	set vi^=!
endif

" open multiple files in tabs
au VimEnter * nested if &diff == 0 && restore_session == 0 | :tab all | endif
"au BufAdd * tab ball
"au BufAdd,BufNewFile,BufRead * nested tab ball

" Mark session restores
let restore_session = 0
au SessionLoadPost * nested let restore_session = 1

" WM clipboard is vim one
" http://vim.wikia.com/wiki/VimTip21
" http://vim.wikia.com/wiki/VimTip984
set clipboard^=unnamed,unnamedplus
set guioptions+=a " selection with mouse copys to clipboard
"set guioptions+=b " horiz scroll

if has("gui_running")	
	set switchbuf=usetab,newtab " Switch to tab if you can
else
	set mouse-=a

	if &term == "screen"
		" Make ctrl-* work
		set term=xterm
	elseif &term == "rxvt-unicode"
		set term=xterm
		nmap    <ESC>[5^    <C-PageUp>
		nmap    <ESC>[6^    <C-PageDown>
	endif

	if &term == "xterm"
		" xterm mostly black
		set background=dark
	endif
endif

" reload vimrc on change
au! BufWritePost .vimrc source %
au! BufWritePost _vimrc source %

augroup Shebang
	autocmd BufNewFile *.py 0put =\"#!/usr/bin/env python\<nl># -*- coding: iso-8859-15 -*-\<nl>\"|$
augroup END

" /usr/share/doc/vim/NEWS.Debian.gz
filetype indent on
filetype plugin on

" robo basic
autocmd Filetype rib setlocal filetype=basic | set fileformat=dos
" python PEP8 override
autocmd FileType python setlocal noexpandtab shiftwidth=8 softtabstop=8 tabstop=8
" ruby
let g:ruby_recommended_style = 0

" Convert Hex to Dec
" http://vim.wikia.com/wiki/VimTip27
" e.g. 5,8call Hex2Dec()" 
function! Hex2Dec()
	let lstr = getline(".")
	let hexstr = matchstr(lstr, '0x[a-fA-F0-9]\+')
	while hexstr != ""
		let hexstr = hexstr + 0
		exe 's#0x[a-fA-F0-9]\+#'.hexstr."#"
		let lstr = substitute(lstr, '0x[a-fA-F0-9]\+', hexstr, "")
		let hexstr = matchstr(lstr, '0x[a-fA-F0-9]\+')
	endwhile
endfunction
" http://vim.wikia.com/wiki/VimTip1479
function! Dec2Hex()
	let lstr = getline(".")
	let decstr = matchstr(lstr, '\<[0-9]\+\>')
	while decstr != ""
		let decstr = printf("0x%x", decstr)
		exe 's#\<[0-9]\+\>#'.decstr."#"
		let lstr = getline(".")
		let decstr = matchstr(lstr, '\<[0-9]\+\>')
	endwhile
endfunction

function! CheckFileEncoding()
	if &modified && &fileencoding != ''
		exec 'e! ++enc=' . &fileencoding
	endif
endfunction
au BufWinEnter * nested call CheckFileEncoding()

function! SetFileEncoding(charset)
	exec 'e! ++enc=' . a:charset
endfunction

function! SetJava()
	setlocal omnifunc=javacomplete#Complete 
	setlocal makeprg=ant\ -emacs
endfunction
autocmd Filetype java call SetJava()

if !exists('*FetchVimrc')
	function FetchVimrc()
		exec '!wget http://defiant.homedns.org/~erik/conf/vimrc -O $MYVIMRC'
		source $MYVIMRC
	endfunction
endif

" Fix mixed fileformat - http://vim.wikia.com/wiki/Automatically_reload_files_with_mixed_line-endings_in_DOS_fileformat
autocmd BufReadPost * nested
      \ if !exists('b:reload_dos') && !&binary && &ff=='unix' && (0 < search('\r$', 'nc')) |
      \   let b:reload_dos = 1 |
      \   e ++ff=dos |
      \ endif

if has("unix")
	" do stuff for Unix
	" Make shift-insert work like in Xterm
	map <S-Insert> <MiddleMouse>
	map! <S-Insert> <MiddleMouse>

	" maps the F1 key to Esc to disable going into Help. 
	" On some IBM ThinkPad laptops, the ESC key is above F1
	" you may keep hitting F1 and going into help!
	imap <f1> <ESC>

	" Spell
	command! Mkspellde :mkspell ~/.vim/spell/de /usr/share/myspell/dicts/de-DE

	" powertop...
	"let &guicursor = &guicursor . ",a:blinkon0"

	" Printing using kprinter
	" http://vim.wikia.com/wiki/VimTip313
	if has("gui_running")	
		set printexpr=system('kprinter5'\ .\ '\ '\ .\ v:fname_in)\ .\ delete(v:fname_in)\ +\ v:shell_error
	endif

	" Pychecker
	"autocmd BufNewFile,BufRead *.py setlocal makeprg=pychecker\-Q\ % | setlocal efm=%f:%l:%m
	" Pyflakes
	"autocmd BufNewFile,BufRead *.py setlocal makeprg=pyflakes\ % | setlocal efm=%f:%l:%m
	" Pylint
	autocmd BufNewFile,BufRead *.py setlocal makeprg=setlocal makeprg=pylint\ --output-format=parseable\ --reports=n\ % | setlocal efm=%f:%l:\ [%t]%m,%f:%l:%m

	" use xterm's bracketed paste mode to make pasting automatically enable paste mode and insert mode
	"if &term == "xterm"
	"	let &t_ti = &t_ti . "\e[?2004h"
	"	let &t_te = "\e[?2004l" . &t_te
	"	function XTermPasteBegin(ret)
	"		set pastetoggle=<Esc>[201~
	"		set paste
	"		return a:ret
	"	endfunction
	"	map <expr> <Esc>[200~ XTermPasteBegin("i")
	"	imap <expr> <Esc>[200~ XTermPasteBegin("")
	"endif
elseif has("win32")
	" do stuff for Windows

	set bs=2

	set diffexpr=MyDiff()
	function! MyDiff()
		let opt = '-a --binary '
		if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
		if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
		let arg1 = v:fname_in
		if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
		let arg2 = v:fname_new
		if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
		let arg3 = v:fname_out
		if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
		let eq = ''
		if $VIMRUNTIME =~ ' '
			if &sh =~ '\<cmd'
				let cmd = '""' . $VIMRUNTIME . '\diff"'
				let eq = '"'
			else
				let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
			endif
		else
			let cmd = $VIMRUNTIME . '\diff'
		endif
		silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
	endfunction

	" Maximize
	" http://vim.wikia.com/wiki/Maximize_or_set_initial_window_size
	au GUIEnter * simalt ~x	

	" Pychecker Windows Version
	"autocmd BufNewFile,BufRead *.py setlocal makeprg=pychecker.bat\-Q\ % | setlocal efm=%f:%l:%m
	" Pyflakes
	"autocmd BufNewFile,BufRead *.py setlocal makeprg=pyflakes.bat\ % | setlocal efm=%f:%l:%m
	" Pylint
	autocmd BufNewFile,BufRead *.py setlocal makeprg=setlocal makeprg=pylint.bat\ --output-format=parseable\ --reports=n\ % | setlocal efm=%f:%l:\ [%t]%m,%f:%l:%m

	" Use slash as separator
	set shellslash
endif

" https://github.com/tpope/vim-pathogen
runtime! autoload/pathogen.vim " needs to be called so exists() works
if exists("*pathogen#infect")
	execute pathogen#infect()
endif
let g:NERDTreeDirArrows=0
"autocmd VimEnter * NERDTree
"autocmd BufWinEnter * NERDTreeMirror
" Start NERDTree when Vim is started without file arguments.
" autocmd VimEnter * if argc() == 0 && !exists('s:std_in') | NERDTree | endif
"nnoremap <silent> <buffer> ". g:NERDTreeMapOpenInTab ." :call <SID>openInNewTab(0)<cr>:NERDTree<cr>"
" Start NERDTree when Vim starts with a directory argument.
autocmd StdinReadPre * let s:std_in=1
if &runtimepath =~ 'nerdtree'
	autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') |
	    \ execute 'NERDTree' argv()[0] | wincmd p | enew | execute 'cd '.argv()[0] | endif
	" Open the existing NERDTree on each new tab.
	autocmd BufWinEnter * if &buftype != 'quickfix' && getcmdwintype() == '' | silent NERDTreeMirror | endif
endif

" xml
let $XMLLINT_INDENT="\t"
command! Xmlformat %!xmllint --format -

" json
command! Jsonformat %!python -m json.tool

" Set working directory to the current file
command! Cdfwd :cd %:p:h
