" TODO: Move these settings to vimrc when after switching to vim8 packages
scriptencoding 'utf-8'

" Copy indent from current line
set autoindent

" Make <BS> work in a sane way
set backspace=indent,eol,start

" Insert blank same in front of line according to 'shiftwidth'.
set smarttab

if !has('nvim') && &ttimeoutlen == -1
  " Time in miliseconds to wait for a key core or mapping
  set ttimeout
  set ttimeoutlen=100
endif

" TODO: These might be irrelevant with vim-airline
" Show 2 line status
set laststatus=2
" Show the line and colum number of a cursor position
set ruler

" Enhanced command line completion
set wildmenu
" Command line history
if &history < 1000
  set history=1000
endif

" Display as much as possible of the last line
set display+=lastline

if &listchars ==# 'eol:$'
  " TODO: Review this
  set listchars=tab:>\ ,trail:-,extends:>,precedes:<,nbsp:+
endif

if v:version > 703 || v:version == 703 && has('patch541')
  " Delete comment character when joining commented lines
  set formatoptions+=j
endif

" Enable visually highlighting listchars
set list
" Non breaking space:
" * Circled Reverse Solidus (U+29B8, utf-8: E2 A6 B8)
set listchars=nbsp:⦸
" Trailing space:
" * Middle Dot (U+00B7, utf-8: C2 B7)
set listchars+=trail:·
" Tab stop:
" * White Right-Pointing Small Triangle (U+25B9, utf-8: E2 96 B9)
" * Box Drawings Light Triple Dash Horizontal (U+2504, utf-8: E2 94 84)
set listchars+=tab:▹┄
" Line extended beyond screen when nowrap:
" * Right-Pointing Double Angle Quotation Mark (U+00BB, utf-8: C2 BB)
set listchars+=extends:»
" Line preceded beyond screen when nowrap:
" * Left-Pointing Double Angle Quotation Mark (U+00AB, utf-8: C2 AB)
set listchars+=precedes:«

" Wrap lines which are longer than screen width
set wrap
if has('linebreak')
  " Break wrapped lines at a character in breakat
  set linebreak
  " Downwards Arrow With Tip Rightwards (U+21B3, utf-8: E2 86 B3)
  let &showbreak='↳ '
  " Use same highlight group as listchars for showbreak
  set highlight+=@:SpecialKey
endif

" TODO: spellcapcheck
" TODO: virtualedit=block
" TODO: whichwrap=b,h,l,s,<,>,[,],~

" Don't show completeopt preview buffer
if has('insert_expand')
  set completeopt-=preview
endif

" Set window title to titlestring
if has('title')
  set title
endif

" Don't show mode in command line
set noshowmode

" Show relative line numbers & current line number
set number
if exists('+relativenumber')
  set relativenumber
endif

" Keep cursor from buffer edges
set scrolloff=8
set sidescrolloff=5

" Turn backup off
set nobackup noswapfile
if has('writebackup')
  set nowritebackup
endif

" Change info file location
if has('viminfo') && has('unix')
  let s:cache_dir = expand('~/.cache/vim')
  if !isdirectory(s:cache_dir)
    call mkdir(s:cache_dir)
  endif
  execute 'set viminfofile='.s:cache_dir.'/info'
endif

" Use Unix as standard file type
set fileformats=unix,mac,dos

" Allow : in filenames to allow gf to specify line numbers
set isfname-=:

" Highlight search matches & show search matches while typing
set hlsearch incsearch
" Set ignore search case unless mixed
set ignorecase smartcase

" Allow buffers with changes to be hidden
set hidden

" Open new splits on the other side of the axis
if has('windows')
  set splitbelow
endif
if has('vertsplit')
  set splitright
endif

" Use existing windows and tabs when jumping to errors
set switchbuf=usetab

" Indicates a fast terminal connection
set ttyfast

" Set syntax as default fold method
if has('folding')
  set foldmethod=syntax foldlevel=20
endif

" Automatically read/write changes to files
set autoread
set autowrite

" Don't add 2 spaces after end of sentence
set nojoinspaces

" Enable all mouse features
set mouse=a

" Format text
"   r - insert comment leader on 'o' and 'O'
"   q - allow formatting with 'gq'
set formatoptions+=rq

" Enable modeline
set modeline

" Don't redraw during execution macros, registers, commands, etc.
set lazyredraw

" When in diff mode, use the patience algorithm for more readable diffs.
if &diff
  set diffopt+=algorithm:patience
endif

" Allow color schemes to do bright colors without forcing bold
if &t_Co == 8 && $TERM !~# '^linux\|^Eterm'
  set t_Co=16
endif

" Change cursor dependant on current mode
if has('cursorshape') && has('unix') && !has('gui_running')
  if $TMUX ==# '' && $ITERM_PROFILE !=# ''
    let &t_SI = "\<Esc>]50;CursorShape=1\x7"
    let &t_EI = "\<Esc>]50;CursorShape=0\x7"
  else
    let &t_SI = "\<Esc>[6 q"
    let &t_EI = "\<Esc>[2 q"
  endif
  let &t_SR = &t_SI
endif