set nocompatible           " Vim defaults rather than vi ones. Keep at top.

set undodir=$VIMDOTDIR/undo
set directory=$VIMDOTDIR/swap
set backupdir=$VIMDOTDIR/backup
set viewdir=$VIMDOTDIR/view
set viminfo+='1000,n$VIMDOTDIR/viminfo
set runtimepath=$XDG_CONFIG_HOME/vim,$VIMRUNTIME,$XDG_CONFIG_HOME/vim/after

" Install vim-plug if not found
if empty(glob($VIMDOTDIR.'/autoload/plug.vim'))
  silent !curl -fLo ${VIMDOTDIR}/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
endif

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

" Specify a directory for plugins
call plug#begin($VIMDOTDIR.'/plugged')
  Plug 'elzr/vim-json'
  Plug 'catppuccin/vim', { 'as': 'catppuccin' }
call plug#end()

set backspace=2            " Make the backspace behave as most applications.
set autoindent             " Use current indent for new lines.
set display=lastline       " Show as much of the line as will fit.
set wildmenu               " Better tab completion in the commandline.
set wildmode=list:longest  " List all matches and complete to the longest match.
set showcmd                " Show (partial) command in bottom-right.
set expandtab              " Use spaces instead of tabs for indentation.
set smarttab               " Backspace removes 'shiftwidth' worth of spaces.
set number                 " Show line numbers.
set laststatus=2           " Always show the statusline.
set ruler                  " Show the ruler in the statusline.
set textwidth=80           " Wrap at n characters.
set incsearch              " Jump to search match while typing.
set hlsearch               " Highlight the last used search pattern.
set nrformats-=octal       " Remove octal support from 'nrformats'.
set tabstop=2              " Size of a Tab character.
set shiftwidth=2           " Use same value as 'tabstop'.
set softtabstop=2          " Use same value as 'shiftwidth'.
set termguicolors              " Enable true color support
color catppuccin_mocha         " Use Catppuccin Mocha color scheme
set listchars=tab:»·,trail:·,eol:¤,nbsp:ʽ " Define invisible chars
set wrap linebreak         " Softwrap long lines at window border, don't break words
set showbreak=            " Indent softwrapped lines with unbreakable space

" Filetype-specific settings
augroup filetypes
  autocmd!
  autocmd FileType markdown set cursorline
  autocmd BufNewFile,BufRead *.md set filetype=markdown
  autocmd FileType make set noexpandtab
  autocmd FileType python set softtabstop=4 tabstop=4 shiftwidth=4 textwidth=79
augroup end

" Go to the last cursor location when opening a file.
augroup jump
  autocmd!
  autocmd BufReadPost *
    \  if line("'\"") > 1 && line("'\"") <= line("$") && &ft !~# 'commit'
      \| exe 'normal! g`"'
    \| endif
augroup end

" Clean trailing whitespace.
fun! s:trim_whitespace()
  let l:save = winsaveview()
  keeppatterns %s/\s\+$//e
  call winrestview(l:save)
endfun
command! TrimWhitespace call s:trim_whitespace()
