" Install vim-plug if not found
if empty(glob('~/.config/vim/autoload/plug.vim'))
  silent !curl -fLo ~/.config/vim/autoload/plug.vim --create-dirs
        \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
endif
if empty(glob('~/.local/share/nvim/site/autoload/plug.vim'))
  silent !curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
       https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
endif


set laststatus=2 "lightline requires it?
"Setup Plugins
call plug#begin()

"*********************
" General Plugins
"*********************

"distraction free writing
Plug 'junegunn/goyo.vim' 

"ranger - file manager
Plug 'francoiscabrol/ranger.vim' 

" Easymotion
Plug 'easymotion/vim-easymotion'
let g:EasyMotion_smartcase = 1

" Signify - show vcs changes
if has('nvim') || has('patch-8.0.902')
  Plug 'mhinz/vim-signify'
  set updatetime=100
else
  Plug 'mhinz/vim-signify', { 'tag': 'legacy' }
endif

" Line
Plug 'itchyny/lightline.vim'

" LSP Server
Plug 'autozimu/LanguageClient-neovim', {
    \ 'branch': 'next',
    \ 'do': 'bash install.sh',
    \ }

" FZF
Plug 'junegunn/fzf'

" Async.vim - backport nvim async to vim
Plug 'prabirshrestha/async.vim'

"Auto add delimiters
Plug 'Raimondi/delimitMate'

"Linting
"Plug 'dense-analysis/ale'

" Commenting
Plug 'tpope/vim-commentary'

"Copy using OSC52
Plug 'ojroques/vim-oscyank', {'branch': 'main'}

Plug 'scrooloose/nerdtree'
Plug 'jistr/vim-nerdtree-tabs'
Plug 'vim-scripts/grep.vim'
Plug 'vim-scripts/CSApprox'
Plug 'majutsushi/tagbar'
Plug 'Yggdroot/indentLine'



"*********************
" Language Specific
"*********************

" c
Plug 'vim-scripts/c.vim', {'for': ['c', 'cpp']}

" go
Plug 'fatih/vim-go', {'do': ':GoInstallBinaries', 'for': 'go'}

" html
Plug 'hail2u/vim-css3-syntax', {'for': ['html','css']}
Plug 'gko/vim-coloresque'
Plug 'skammer/vim-css-color'
Plug 'tpope/vim-haml', {'for': 'html'}
Plug 'mattn/emmet-vim', {'for': 'html'}

" javascript
"" Javascript Bundle
Plug 'jelera/vim-javascript-syntax'

" latex
if has('nvim')
        Plug 'lervag/vimtex', {'for': 'latex'}
  let g:tex_flavor='latex'
  let g:vimtex_view_method='zathura'
  let g:vimtex_quickfix_mode=0
  set conceallevel=1
  let g:tex_conceal='abdmg'
endif

" python
Plug 'davidhalter/jedi-vim', {'for': 'python' }
Plug 'raimon49/requirements.txt.vim', {'for': 'requirements'}

" ruby
Plug 'tpope/vim-rails'
Plug 'tpope/vim-rake'
Plug 'tpope/vim-projectionist'
Plug 'thoughtbot/vim-rspec'
Plug 'ecomba/vim-ruby-refactoring', {'tag': 'main'}

" rust
" Code completion for rust using Racer
Plug 'racer-rust/vim-racer', {'for': 'rust'}
Plug 'rust-lang/rust.vim', {'for': 'rust'}
if has('nvim')
  Plug 'simrat39/rust-tools.nvim', {'for': 'rust'}
  Plug 'Saecki/crates.nvim', {'for': 'rust'}
endif

" toml
Plug 'cespare/vim-toml'

" typescript
Plug 'leafgarland/typescript-vim', {'for': 'typescript'}
Plug 'HerringtonDarkholme/yats.vim', {'for': 'typescript'}

"Git
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-rhubarb' " required by fugitive to :Gbrowse

call plug#end()

"****************
" Plugin Setup
"****************

" Run PlugInstall Vif there are missing plugins
autocmd VimEnter * if len(filter(values(g:plugs), '!isdirectory(v:val.dir)'))
      \| PlugInstall --sync | source $MYVIMRC
      \| endif

" OSCYank setup
if exists('##TextYankPost')
  autocmd TextYankPost * if v:event.operator is 'y' && v:event.regname is '' | execute 'OSCYankVisual"' | endif
endif


" status line
set statusline=%F%m%r%h%w%=(%{&ff}/%Y)\ (line\ %l\/%L,\ col\ %c)\
if exists("*fugitive#statusline")
  set statusline+=%{fugitive#statusline()}
endif

" Goyo Keybind
map <leader>g :Goyo \| set linebreak<CR>


"****************
" Vim Setup
"****************

set nocompatible
filetype on
filetype plugin on
filetype indent on
syntax on " syntax higlighting
set hidden "enable hidden buffers
set noshowmode "conflicts with airline
set mouse=a "mouse anywhere
set number "line numbers
set relativenumber "relative line numbers
" search
set hlsearch "highlight search
set ignorecase "case insensitive?
set smartcase "only case insensitive for lowercase
set incsearch

set showmatch " show matching brace
" indentations
set autoindent "auto indent
set smartindent "smart indent
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=0

set ruler
" Autocomplete
set wildmode=longest:list:full
set wildmenu
set encoding=utf-8
set ttyfast

" w!! to sudo save
cmap w!! !sudo tee % > /dev/null

" Search mappings: These will make it so that going to the next one in a
" search will center on the line it's found in.
nnoremap n nzzzv
nnoremap N Nzzzv

"" no one is really happy until you have this shortcuts
cnoreabbrev W! w!
cnoreabbrev Q! q!
cnoreabbrev Qall! qall!
cnoreabbrev Wq wq
cnoreabbrev Wa wa
cnoreabbrev wQ wq
cnoreabbrev WQ wq
cnoreabbrev W w
cnoreabbrev Q q
cnoreabbrev Qall qall