filetype off
set nocompatible

call plug#begin('~/.vim/plugged')
" General vim stuff
Plug 'wincent/terminus'
Plug 'godlygeek/tabular'

" F#
Plug 'fsharp/vim-fsharp'

" Perl6/Raku
Plug 'vim-perl/vim-perl6', { 'for': 'perl6' }

" Rust
Plug 'rust-lang/rust.vim', { 'for': 'rust' }

" Fish shell
Plug 'dag/vim-fish'

" Rusty Object Notation
Plug 'ron-rs/ron.vim'

" HJSON
Plug 'hjson/vim-hjson'

" Dhall
Plug 'vmchale/dhall-vim'

" Vimwiki
Plug 'vimwiki/vimwiki'

" Godotscript v3
Plug 'clktmr/vim-gdscript3'

" Python
Plug 'ambv/black'
Plug 'vim-scripts/indentpython.vim'

" TOML
Plug 'cespare/vim-toml'

" Elixir
Plug 'elixir-editors/vim-elixir'

" Jinja + HTML templating
Plug 'lepture/vim-jinja'

" Haskell
Plug 'neovimhaskell/haskell-vim'
Plug 'alx741/vim-hindent'

call plug#end()

filetype plugin indent on
let python_highlight_all=1
"let g:rustfmt_options = '--edition 2018'
"let c_no_curly_error=1
syntax enable " syntax highlighting
colorscheme badwolf2

"let g:cargo_makeprg_params = '--color=never'
let g:hindent_on_save = 0

"set statusline+=%#warningmsg#
"set statusline+=%{SyntasticStatuslineFlag()}
"set statusline+=%*

au BufRead,BufNewFile *.hj setlocal filetype=hjson
au BufRead,BufNewFile *.md setlocal textwidth=100
au BufRead,BufNewFile *.yaml,*.yml setlocal shiftwidth=2 tabstop=2
"au BufRead,BufNewFile *.rs setlocal textwidth=100 colorcolumn=100
au BufRead,BufNewFile /tmp/jrnl*.txt setlocal textwidth=100 colorcolumn=100 formatoptions-=t columns=100 wrap
au BufRead,BufNewFile *.py setlocal colorcolumn=100 textwidth=100
au BufRead,BufNewFile *.adoc setlocal colorcolumn=100 textwidth=100
au FileType make setlocal noexpandtab shiftwidth=4 tabstop=4
au FileType haskell setlocal shiftwidth=2 tabstop=2

au FileType rust compiler cargo
au FileType rust nnoremap cc :make check<cr>
au FileType rust nnoremap cn :cn<cr>
au FileType rust nnoremap cp :cp<cr>
au FileType rust nnoremap co :copen<cr>
au FileType rust nnoremap cq :cclose<cr>
au BufRead,BufNewFile *.lalrpop setlocal ft=rust
au BufRead,BufNewFile *html,*.j2 setlocal ft=jinja

" Help navigation
" Enter to go to the topic under cursor
au Filetype help nnoremap <buffer> <cr> <c-]>
" Backspace to go to the previous topic
au Filetype help nnoremap <buffer> <bs> <c-t>
" S for the next subject
au Filetype help nnoremap <buffer> s /\|\zs\S\+\ze\|<cr>
au Filetype help nnoremap <buffer> S ?\|\zs\S\+\ze\|<cr>
" Doesn't cause statements after `case: {` to be crazy-indented
au Filetype cpp setlocal cinoptions=l1g0

" Store swapfiles in ~/.vim/swap/
set directory=$HOME/.vim/swap/
" Store wiki in ~/.vim/wiki
let g:vimwiki_list = [{'path': '~/.vim/wiki', 'syntax': 'markdown'}]

" Tabbing/indenting
set nu                  " line numbers
set relativenumber
set shiftwidth=4        " 4 space tabs
set tabstop=4           " tabs print as 4 spaces
set ruler
set expandtab           " spaces > tabs
set smarttab
set autoindent
set smartindent
set nowrap
set cursorline
set wildmenu            " tab menu for files
set lazyredraw          " redraw only when we need to - useful for macros
highlight clear CursorLineNr " For some reason, the line number column(s) of the current line sometimes get underlined and it's distracting
"set showmatch           " jumps to matching [({})]
set incsearch           " search as characters are entered
set hlsearch            " highlight matches
set mouse=a
set scrolloff=10
set timeoutlen=175 ttimeoutlen=175
set modeline modelines=5

set linebreak           " If the line goes off the screen, break the line at the `breakat` characters
set showbreak=+++       " Use this string to show that the line was broken at the end of the line

" Move vertically by visual line
nnoremap j gj
nnoremap k gk
" Hack to fix the # character insert
inoremap # X<BS>#
nnoremap 4i i

" Color stuff
set t_Co=256            " 256 colors support
set colorcolumn=100,120     " 100, and 120 character markers
" This sets up 24-bit true color support - don't fuck these up
set t_8f=[38;2;%lu;%lu;%lum
set t_8b=[48;2;%lu;%lu;%lum
set termguicolors

"
" Keymappings
"
let mapleader = "-"
inoremap <c-d> <esc>ddi
" Upper/lower case keymaps
inoremap <c-u> <esc>wbvwU<esc>i
inoremap <c-l> <esc>wbvwu<esc>i
nnoremap <c-u> <esc>wbvwU<esc>
nnoremap <c-l> <esc>wbvwu<esc>
vnoremap <s-k> k
" Set up paste-replacement to yank into the blackhole register
vnoremap p "_dP

"=====[ Correct common mistypings in-the-fly ]=======================

iab    retrun  return
iab     pritn  print
iab       teh  the
iab      liek  like
iab  liekwise  likewise
iab      Pelr  Perl
iab      pelr  perl
iab        ;t  't
iab      moer  more
iab  previosu  previous
iab   virutal  virtual
iab    unwarp  unwrap
iab    unrwap  unwrap
iab  compilie  compile
iab compilier  compiler

" Clear a line
nnoremap <leader>c ddO
" Edit vimrc file
nnoremap <leader>ev :tabnew $HOME/.vimrc<cr>
" Source vimrc file
nnoremap <leader>sv :source $HOME/.vimrc<cr>
" Tabular extension
nnoremap <leader>t :Tab /<C-R>=getline('.')[col('.')-1]<cr><cr>

" Tab control
nnoremap tt :tabnew<cr>
nnoremap <c-h> :tabp<cr>
nnoremap <c-l> :tabn<cr>

nnoremap Q @@

" Quicksave
nnoremap <leader>w :update<cr>
nnoremap <leader>s :update<cr>

"
" Code folding
"
set foldlevelstart=4
set foldmethod=manual
nnoremap <leader>f za
