set nocompatible              " be iMproved, required
filetype off                  " required

set background=dark

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

Plugin 'junegunn/goyo.vim'
Plugin 'ervandew/supertab'
Plugin 'vim-scripts/DrawIt'
Plugin 'takac/vim-hardtime'
" tpope goodness
Plugin 'tpope/vim-abolish'
Plugin 'tpope/vim-surround'
Plugin 'tpope/vim-repeat'
"" fancy ass ide stuff
Plugin 'Shougo/deoplete.nvim'
Plugin 'bling/vim-airline'
Plugin 'tpope/vim-fugitive'
Plugin 'vim-airline/vim-airline-themes'
"Plugin 'zchee/deoplete-clang'
""Plugin 'Valloric/YouCompleteMe'
"Plugin 'SirVer/ultisnips'
"Plugin 'honza/vim-snippets'
"" make it look good
"" neomutt support
Plugin 'neomutt/neomutt.vim'
"" dissaprove indented code to far
""Plugin 'dodie/vim-disapprove-deep-indentation'
" Syntax hilighting for base16 style themes
"Plugin 'plasticboy/vim-markdown'
Plugin 'rhysd/vim-grammarous'
Plugin 'chriskempson/base16-vim'

call vundle#end()            " required
filetype plugin indent on    " required
filetype plugin on

"General
syntax on
set showcmd		" Show (partial) command in status line.
"set showmatch		" Show matching brackets.
"set gdefault
set ignorecase		" Do case insensitive matching
set smartcase		" Do smart case matching
set incsearch		" Incremental search
set hlsearch
"set autowrite		" Automatically save before commands like :next and :make
"set hidden             " Hide buffers when they are abandoned
"set mouse=a		" Enable mouse usage (all modes)
set laststatus=0        " Hide the useless statusbar

set relativenumber
set number
"set spell
set ruler

"Remove annoying ESC delay
set ttimeout ttimeoutlen=20 

"Change cursor to | when in insert
let $NVIM_TUI_ENABLE_CURSOR_SHAPE = 1

"Autosave and Autoread
set autoread
set autowriteall
set noswapfile
set nobackup
au CursorHold,CursorHoldI * checktime
au CursorHold,CursorHoldI * silent! wa
set updatetime=2000

"Persisten-undo magic
set undofile

"Disable the super annoying autocomment
autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o

"Make j and k not jump when wraping
nnoremap j gj
nnoremap k gk

"set leader to comma
let mapleader = ","

"Math with qalc
nnoremap <leader>m :.!~/bin/calc<Enter>
nnoremap <leader>i :read !latestimage<Enter>
nnoremap <leader>v :!open '<cfile>'<CR>

noremap! <C-BS> <C-w>
noremap! <C-h> <C-w>

"read spaces in path's
set isfname+=32

"Set tab to two spaces
set expandtab
set shiftwidth=2
set softtabstop=2


"Make tab into another escape
"nnoremap <Tab> <Esc>
"vnoremap <Tab> <Esc>gV
"onoremap <Tab> <Esc>
"inoremap <Tab> <Esc>`^
"inoremap <S-Tab> <Tab>
set enc=utf-8

"sync paste buffer and vims register
set clipboard^=unnamed

" don't clobber up the directory with dot undo file
set undodir=~/.vim/undodir/

"disable mouse
set mouse=


"Open new file
nnoremap <Leader>o :CtrlP<CR>
nnoremap <Leader>w :w<CR>

" Hard time options
let g:hardtime_maxcount = 2
let g:hardtime_default_on = 1
let g:hardtime_showmsg = 0 
let g:hardtime_allow_different_key = 1

""Deoplete enable
let g:deoplete#enable_at_startup = 1
"let g:deoplete#sources#clang#libclang_path = '/usr/lib/libclang.so'
"let g:deoplete#sources#clang#clang_header = '/usr/lib/clang/'
"
"" supertab
let g:SuperTabDefaultCompletionType    = '<C-n>'
let g:SuperTabCrMapping                = 0

"" ultisnips
"let g:UltiSnipsSnippetsDir='~/.vim/snippets'
"let g:UltiSnipsEditSplit='vertical'
"let g:UltiSnipsExpandTrigger           = '<tab>'
"let g:UltiSnipsJumpForwardTrigger      = '<tab>'
"let g:UltiSnipsJumpBackwardTrigger     = '<s-tab>'
"
""""""""""""""""""""""""
"" CUSTOM TEXT-OBJECTS "
""""""""""""""""""""""""
"for char in [ '_', '.', ':', ',', ';', '<bar>', '/', '<bslash>', '*', '+', '%', '-', '#' ]
"    execute 'xnoremap i' . char . ' :<C-u>normal! T' . char . 'vt' . char . '<CR>'
"    execute 'onoremap i' . char . ' :normal vi' . char . '<CR>'
"    execute 'xnoremap a' . char . ' :<C-u>normal! F' . char . 'vf' . char . '<CR>'
"    execute 'onoremap a' . char . ' :normal va' . char . '<CR>'
"endfor

highlight ExtraWhitespace guibg=purple
match ExtraWhitespace /\s\+$\| \+\ze\t/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$\| \+\ze\t/
autocmd InsertEnter * match ExtraWhitespace /\s\+$%#\@<!$\| \+\ze\t/
autocmd InsertLeave * match ExtraWhitespace /\s\+$\| \+\ze\t/
autocmd BufWinLeave * call clearmatches()

"Airline symbols
let g:airline_powerline_fonts = 1

let g:vim_markdown_folding_disabled = 1

let g:airline_theme='base16'


" Modify The ColorScheme to show Italic and Bold in Markdown.
" Italicise comments and Type's
" And make Line numbers same as background

function! s:base16_customize() abort
    call Base16hi("htmlBold",    g:base16_gui0A, "", g:base16_cterm06, "", "bold", "") 
    call Base16hi("markdownBold",    g:base16_gui0A, "", g:base16_cterm06, "", "bold", "") 
    call Base16hi("markdownHeadingDelimiter",    g:base16_gui0A, "", g:base16_cterm08, "", "bold", "") 
    call Base16hi("markdownH1",    g:base16_gui0A, "", g:base16_cterm08, "", "bold", "") 
    call Base16hi("markdownH2",    g:base16_gui0A, "", g:base16_cterm08, "", "bold", "") 
    call Base16hi("htmlItalic",  g:base16_gui0E, "", g:base16_cterm06, "", "italic", "")
    call Base16hi("markdownItalic",  g:base16_gui0E, "", g:base16_cterm06, "", "italic", "")
    call Base16hi("htmlUnderline",  g:base16_gui0E, "", g:base16_cterm06, "", "underline", "")
    call Base16hi("Italic",        "", "", "", "", "italic", "")
    call Base16hi("Comment",      g:base16_gui04, "", g:base16_cterm04, "", "italic", "")

    call Base16hi("Type",         g:base16_gui0A, "", g:base16_cterm0A, "", "italic", "")
    call Base16hi("htmlArg",         "", "", "", "", "italic", "")
    call Base16hi("LineNr",        g:base16_gui03, g:base16_cterm00, g:base16_cterm03, g:base16_cterm00, "", "")
    call Base16hi("CursorLineNr",  g:base16_gui04, g:base16_gui00, g:base16_cterm04, g:base16_cterm00, "", "")
endfunction
augroup on_change_colorschema
    autocmd!
    autocmd ColorScheme * call s:base16_customize()
augroup END

if filereadable(expand("~/.vimrc_background"))
  let base16colorspace=256
  source ~/.vimrc_background
endif