if !has('vim9script') ||  v:version < 900
    " Needs Vim version 9.0 and above
    finish
endif

vim9script

g:mapleader = "\<Space>"
g:maplocalleader = "\<Space>" # meant for certain file types
map <BS> <Leader>

# To make undercurl work in iterm2 (:h E436, :h t_Cs)
&t_Cs = "\e[4:3m"
&t_Ce = "\e[4:0m"

# Disable bracketed paste (otherwise pasting does not work in popup)
# https://github.com/vim/vim/issues/11766
# &t_BE = ""
# &t_BD = "\e[?2004l"
# exec "set t_PS=\e[200~"
# exec "set t_PE=\e[201~"

# Cursor shape changes to show which mode you are in (:h t_SI)
# 6=beam, 4=underline, 2=block
&t_SI = "\e[6 q" #SI = INSERT mode
&t_SR = "\e[4 q" #SR = REPLACE mode
&t_EI = "\e[2 q" #EI = NORMAL mode (ALL ELSE)
# reset the cursor on start
autocmd VimEnter,VimResume * silent execute '!echo -ne "\e[2 q"' | redraw!

# Format usin 'gq'. :h fo-table
set formatoptions=qjl

# Defaults
source $VIMRUNTIME/defaults.vim
# disable message from 'defaults.vim' when entering cmdwin
autocmd! vimHints

# NOTE: $VIMRUNTIME/ftplugin/python.vim sets tabstop, shiftwidth, etc.

# Turn on for Noname/scratch files and non-help files
# 'listchars' is not local to buffer, only local to window
autocmd FileType * if &ft =~ '\v(help|markdown)' | setlocal listchars=tab:\ \ ,trail:\  | else | setlocal listchars=tab:→·,trail:~ | endif
autocmd BufEnter * if &ft == null_string | setlocal listchars=tab:\ \ ,trail:\  | endif
# autocmd BufEnter,FileType * :echom &ft expand('<amatch>') expand('<afile>') 'here'
set list

# set fillchars=vert:│,fold:۰,diff:·
if &background == 'dark'
    set fillchars=vert:│,fold:۰,diff:·,stl:─,stlnc:═
endif
set clipboard=unnamed # Always use the system clipboard
set mouse= # XXX: Disable mouse (set by defaults.vim). Needed by macos terminal app.
set number # line numbering
# set relativenumber
set hls # highlight search
set lbr # line break
set hidden # buffer becomes hidden (not unloaded) when it is abandoned (ex. help buffer)
set nojoinspaces # suppress inserting two spaces between sentences
set shortmess+=I # disable startup message
set report=0 # show yank confirmation even if 1 or 2 lines yanked
set showmatch # show matching braces when text indicator is over them
set ignorecase # case-sensitive search
set smartcase # smart search
set infercase # when doing <c-n/p> completion, respect case
set splitbelow # open new split panes to right
set splitright # open new split panes to bottom
set breakindent # wrapped line will continue visually indented
set smarttab
set spellsuggest=best,10 # set maximum number of suggestions listed top 10 items:
set foldmethod=indent
set foldnestmax=1
set nofoldenable # do not do folding when you open file
set complete-=i # disable completing keywords from included files (e.g., #include in C)
set signcolumn=yes # always show column for lsp diagnostics etc
set dictionary+=/usr/share/dict/words
set whichwrap+=<,>,h,l # make arrows and h, l, push cursor to next line
# set pumheight=7 # max number of items in popup menu (pmenu)
set virtualedit=block # allows selection of rectangular text in visual block mode
set wildignore+=.gitignore,*.swp,*.zwc,tags
set path=.,,
set laststatus=2 # always show statusline
# set laststatus=0
# set rulerformat=%80(%=%f\ %h%m%r\ %-6y\ %-5.(%l,%c%V%)\ %P%)

# XXX: job_start does not pass --exclude={foo, bar} correctly to shell (because
# job_start does not spawn a shell, it executes command directly). single quotes
# also don't work, need double quotes. but all of these work for :grep
# set grepprg=grep\ --color=never\ -RESIHin\ --exclude="*.git*"\ --exclude="*.swp"\ --exclude="*.zwc"\ --exclude-dir=plugged
# dot directories like .git are automatically excluded
# '$*' is a placeholder to replace typed pattern
set grepprg=grep\ --color=never\ -RESIHin\ $*
# make grep behave like vimgrep
# set grepprg=internal

# enable folding at headers in markdown
g:markdown_folding = 1

syntax on # turn on syntax highlighting

# if executable("ug")
#     set grepprg=ug\ -HnjR\ --no-heading
#     set grepformat=%f:%l:%c:%m,%f:%l:%m
# elseif executable("rg")
#     set grepprg=rg\ --vimgrep\ --no-heading\ --smart-case\ --follow
#     set grepformat=%f:%l:%c:%m,%f:%l:%m
# elseif executable("ag")
#     set grepprg=ag\ --vimgrep
#     set grepformat=%f:%l:%c:%m
# endif

#--------------------
# Plugins
#--------------------

# This loads the "matchit" plugin; It makes the % command more powerful, but
# bracket matching gets much slower. Vim's default bracket matching does not
# avoid commented brackets through. and this plugin avoids that.
if has('syntax') && has('eval')
    packadd! matchit
endif

# Suppress netrw banner
g:netrw_banner = 0

if v:version >= 901 && !getcompletion('comment', 'packadd')->empty()
    packadd comment
endif

# Download plug.vim if it doesn't exist yet
if empty(glob('~/.vim/autoload/plug.vim'))
    silent !curl -fLo ~/.vim/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)')) > 0
            \| PlugInstall --sync | source $MYVIMRC
            \| endif

if has('gui')  # MacVim
    plug#begin("~/.local/share/vim-plug/")
    # plug#begin()
    # Plug '~/git/scope.vim'
    Plug '~/git/devdocs.vim'
    Plug '~/git/vimbits'
    Plug '~/git/bufline.vim'
    Plug '~/git/vimsuggest'
    plug#end()
else
    plug#begin("~/.local/share/vim-plug/")
    # Make sure you use single quotes
    Plug 'noahfrederick/vim-noctu'
    Plug 'letorbi/vim-colors-modern-borland'
    Plug 'airblade/vim-gitgutter'
    # Plug 'lacygoill/vim9asm'
    Plug 'yegappan/lsp'
    # Plug '~/git/lsp'
    Plug 'rafamadriz/friendly-snippets'
    Plug 'hrsh7th/vim-vsnip'
    Plug 'hrsh7th/vim-vsnip-integ'
    Plug 'vim-python/python-syntax'
    # colorschemes
    # Plug 'cocopon/iceberg.vim'
    # Plug 'dracula/vim'
    #
    # Plug '~/git/declutter.vim'
    # Plug 'girishji/declutter.vim'
    Plug '~/git/bufline.vim'
    # Plug 'girishji/bufline.vim'
    # Plug '~/git/scope.vim'
    Plug '~/git/vimsuggest'
    Plug '~/git/vimcomplete'
    # Plug 'girishji/vimcomplete'
    Plug '~/git/ngram-complete.vim'
    # Plug 'girishji/ngram-complete.vim'
    Plug '~/git/vimbits'
    # Plug 'girishji/vimbits'
    # Plug 'girishji/pythondoc.vim', {'for': 'python'}
    # Plug '~/git/pythondoc.vim', {'for': 'python'}
    Plug '~/git/pythondoc.vim'
    Plug '~/git/devdocs.vim'
    # Plug 'girishji/devdocs.vim'
    plug#end()
endif

g:python_highlight_all = 1
g:pythondoc_h_expand = 1
# g:vimbits_vim9cmdline = true
g:vimcomplete_tab_enable = 1

# vim: ts=8 sts=4 sw=4 et fdm=marker