execute pathogen#infect()
" Helptags

" VIM CORE
set nocompatible
set autochdir
set notitle
set shm+=I
set path+=.,**
set mouse=v
set laststatus=2
set keywordprg="man"
set backspace=indent,eol,start
set ttimeout
set ttimeoutlen=50
set number
set cursorline
set scrolloff=3
set spelllang=de,en
syntax on
filetype plugin indent on
set mmp=5000
set spelllang=de,en
set re=1
let mapleader = ","

set formatprg=par\ -w80qie

" * and # are only highlighting
" n searches in the direction of * or #
nnoremap * *``
nnoremap # #``
nnoremap n <S-n>

" INDENTATION
set noautoindent
set nosmartindent
set nocindent

" DIFF
set formatoptions=crolj

" SEARCH
set hlsearch
set ignorecase
set smartcase

" SPACES (TABS)
set tabstop=8
set shiftwidth=4
let &softtabstop=&shiftwidth
set expandtab
set smarttab
set textwidth=72

set list
set listchars=tab:»·,trail:•
set fillchars=vert:\|,eob:\ 

set showcmd
set clipboard=unnamedplus

" C specific highlighting
let c_space_errors=1
let c_gnu=1
let c_comment_strings=1
let c_curly_error=1
"set makeprg=cc\ -o\ %:r\ %
set autowrite

" TEMPORARY FILES
set undofile
set undodir=~/.local/vim/undo//
set backup
set backupcopy=yes
set backupdir=~/.local/vim/backup//
set directory=~/.local/vim/swapfiles//
set updatetime=2000
set updatecount=100

" TAB LINE
set tabline=%!Tabline()
function! Tabline() abort
    let l:line = ''
    for l:i in range(1, tabpagenr('$'))
        if l:i == tabpagenr()
            let l:line .= '%#TabLineSel#'
        else
            let l:line .= '%#TabLine#'
        endif
        let l:label = fnamemodify(
            \ bufname(tabpagebuflist(l:i)[tabpagewinnr(l:i) - 1]), ':t' )
        let l:line .= '  ' . l:label . '  '
    endfor
    let l:line .= '%#TabLineFill#'
    return l:line
endfunction

" CREATE DIRECTORIES
silent execute '!mkdir -p ~/.local/vim/backup ~/.local/vim/undo ~/.local/vim/swapfiles'
silent execute 'cs add ~/.dev/cscope.out'
set csprg=cscope\ -C
set cst
set cspc=9

" LOOK N FEEL
set t_Co=256
set t_ut=
set background=dark
let g:is_posix=1

" HIGHLIGHT SPECIAL WORDS
match ErrorMsg '\(TODO:\|FIXME\|XXX\|workaround\|WTF\|: error:.*\|\s\+$\| \+\ze\t\)'

" MY THEME
hi TabLine        ctermbg=234  ctermfg=240  cterm=NONE
hi TabLineFill    ctermbg=234  ctermfg=240  cterm=NONE
hi TabLineSel     ctermbg=234  ctermfg=249  cterm=NONE
hi Comment        ctermbg=NONE ctermfg=249  cterm=italic
hi Constant       ctermbg=NONE ctermfg=251  cterm=italic
hi CursorLine     ctermbg=NONE ctermfg=NONE cterm=NONE
hi CursorLineNr   ctermbg=NONE ctermfg=NONE cterm=NONE
hi Directory      ctermbg=NONE ctermfg=NONE cterm=NONE
hi Error          ctermbg=NONE ctermfg=NONE cterm=NONE
hi ErrorMsg       ctermbg=NONE ctermfg=196  cterm=NONE
hi Folded         ctermbg=NONE ctermfg=246  cterm=inverse
hi Identifier     ctermbg=NONE ctermfg=NONE cterm=NONE
hi Identifier     ctermbg=NONE ctermfg=NONE cterm=NONE
hi Ignore         ctermbg=NONE ctermfg=NONE cterm=NONE
hi IncSearch      ctermbg=NONE ctermfg=251  cterm=inverse
hi LineNr         ctermbg=NONE ctermfg=240  cterm=NONE
hi MatchParen     ctermbg=NONE ctermfg=251  cterm=bold
hi NonText        ctermbg=NONE ctermfg=NONE cterm=NONE
hi Normal         ctermbg=NONE ctermfg=NONE cterm=NONE
hi Pmenu          ctermbg=NONE ctermfg=251  cterm=inverse
hi PmenuSel       ctermbg=NONE ctermfg=251  cterm=bold
hi PreProc        ctermbg=NONE ctermfg=251  cterm=NONE
hi Search         ctermbg=NONE ctermfg=251  cterm=inverse
hi SignColumn     ctermbg=NONE ctermfg=NONE cterm=NONE
hi Special        ctermbg=NONE ctermfg=NONE cterm=NONE
hi SpecialComment ctermbg=NONE ctermfg=NONE cterm=NONE
hi SpecialKey     ctermbg=NONE ctermfg=239  cterm=NONE
hi Statement      ctermbg=NONE ctermfg=NONE cterm=NONE
hi Statement      ctermbg=NONE ctermfg=251  cterm=NONE
hi String         ctermbg=NONE ctermfg=251  cterm=NONE
hi TODO           ctermbg=NONE ctermfg=246  cterm=italic
hi Title          ctermbg=NONE ctermfg=246  cterm=bold
hi Type           ctermbg=NONE ctermfg=NONE cterm=NONE
hi Underlined     ctermbg=NONE ctermfg=251  cterm=underline
hi VisualNOS      ctermbg=NONE ctermfg=246  cterm=NONE
hi WarningMsg     ctermbg=NONE ctermfg=NONE cterm=NONE
hi Wildmenu       ctermbg=NONE ctermfg=NONE cterm=NONE

" VIMDIFF COLORS
hi DiffAdd    ctermbg=22 ctermfg=NONE cterm=NONE
hi DiffDelete ctermbg=234 ctermfg=NONE cterm=NONE
hi DiffChange ctermbg=234 ctermfg=NONE cterm=NONE
hi DiffText   ctermbg=NONE ctermfg=NONE cterm=NONE

" SPLIT AND FOLD COLORS
hi VertSplit  ctermbg=NONE ctermfg=246 cterm=NONE
hi FoldColumn ctermbg=NONE ctermfg=251 cterm=NONE
hi Folded     ctermbg=233 ctermfg=251 cterm=NONE

" SPELL COLOR
hi SpellBad ctermbg=NONE ctermfg=124

" C+PgUp/PgDown
nnoremap  gn :next<CR>
nnoremap  gp :prev<CR>

" NAVIGATE ON VISUAL LINES (SOFT WRAP)
imap <silent> <Down> <C-o> gj
imap <silent> <Up> <C-o> gk
nmap <silent> <Down> gj
nmap <silent> <Up> gk

" USE ARROW / VIM KEYS INSIDE OMNIBOX
inoremap <expr> j pumvisible() ? '<C-n>' : 'j'
inoremap <expr> k pumvisible() ? '<C-p>' : 'k'
inoremap <expr> <Down> pumvisible() ? '<C-n>' : '<Down>'
inoremap <expr> <Up> pumvisible() ? '<C-p>' : '<Up>'

" NETRW
let g:netrw_banner       = 0
let g:netrw_browse_split = 3
let g:netrw_liststyle    = 3
let g:netrw_ctags        = "ectags"
let g:netrw_silent       = 1

" STATUSLINE, AIRLINE/POWERLINE IS FOR NOOBS
set statusline=%#VisualNOS#
set statusline+=%F\ (%{&filetype})
set statusline+=%=
set statusline+=%m%r%h%w%q
set statusline+=\ %{&fileformat}/%{&fileencoding?&fileencoding:&encoding}
set statusline+=\ %b/0x%B
set statusline+=\ %c\ %p%%
set statusline+=\ 

" ITALIC FIX
set t_ZH=[3m
set t_ZR=[23m

" OPEN FILE
nnoremap <C-t> :Texplore<CR>

" TAGS FILE
set tags="~/.dev/tags"
nnoremap gt :cs find 1 <C-R>=expand("<cword>")<CR><CR>
nnoremap gs :cs find 4 <C-R>=expand("<cword>")<CR><CR>
nnoremap gb <c-o>
nnoremap gh :FSHere<CR>

nnoremap oo :browse oldfiles<CR>
nnoremap XX :qall!<CR>

" PLUGIN: TAGBAR
nmap <F12> :TagbarToggle<CR>
let g:tagbar_ctags_bin = "ectags"

" PLUGIN: EASY ALIGN
xmap ga <Plug>(EasyAlign)
nmap ga <Plug>(EasyAlign)

" Compile Stuff
nnoremap mw :!cd /home/sdk/website && make<CR>
nnoremap mm :call MyMake()<CR>
nnoremap mr :!%:p:r<CR>
function MyMake()
    :silent !clear
    if filereadable("Makefile")
        :w! | !make
    else
        :w! | !cc -Wall -g -O0 -o %:p:r -lsndio %:p
    endif
endfunction

" CUSTOM: NO CLUTTER PASTE MODE
nnoremap <silent> <Tab> :call TogglePaste()<CR>
let g:paste=0
function! TogglePaste()
    if (g:paste == 0)
    :set paste nolist nonumber |
        \ echo "-- PASTE --"
    let g:paste=1
else
    :set nopaste list number |
        \ echo "-- NOPASTE --"
    let g:paste=0
endif
endfunction

" CUSTOM: COPY ACROSS VIM INSTANCES
vnoremap vy :call VisualYank()<CR>
nnoremap vp :call VisualPaste()<CR>
let s:temp_file_path = $HOME . '/.cache/vim-snap'
function! VisualYank()
    call writefile(getline(getpos("'<")[1],
                \ getpos("'>")[1]), s:temp_file_path)
endfunction
function! VisualPaste()
    let file_msg = readfile(s:temp_file_path)
    call append(line("."), file_msg)
endfunction

" CUSTOM: HTMLENCODE WITHOUT PRE
function! BlogEncode()
    :%s/>/\&gt;/g | %s/</\&lt;/g | %s/&lt;pre&gt;/<pre>/g | %s/&lt;\/pre&gt;/<\/pre>/g
endfunction

augroup mystuff
    au!
    au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
    au BufRead,BufNewFile *.h,*.c set filetype=c.doxygen
    au BufRead,BufNewFile Makefile,/usr/src/*.c,/usr/src/*.h,*.gmk setl sw=8 sts=8 noet
    au BufWritePost vimrc so ~/.vim/vimrc
augroup end
