# ------------------
# Editor Settings
# ------------------

# Disables startup message showing
set-option global startup_info_version 30000000

set-option global tabstop 4
set-option global indentwidth 4
set-option global scrolloff 5,3

declare-option str kakrc_path "~/system/nate/dotfiles/kak/kakrc"
declare-option str sway_conf_path "~/system/nate/dotfiles/sway/config.d/default"
declare-option str shell_config "~/system/nate/dotfiles/.zshrc"

colorscheme catppuccin_macchiato

# relative line numbers
hook global BufCreate .* %{
  add-highlighter buffer/ number-lines -hlcursor
}

# Git Gutters
hook global WinCreate .* %{ evaluate-commands %sh{
  if [ $kak_buffile != $kak_bufname ] && git ls-files --error-unmatch "$kak_buffile" > /dev/null 2>&1; then
    echo "git show-diff"
  fi
}}

# Write on unfocus
hook global FocusOut .* %{ try %{
  write
}}

hook global BufWritePost .* %{ evaluate-commands 'git update-diff' }
hook global BufReload .* %{ evaluate-commands 'git update-diff' }

# Add default,red and bold style to these regex matches
add-highlighter global/ regex \b(TODO|FIXME|XXX|NOTE)\b 0:default,red+rb

# ------------------
# Key bindings
# ------------------

# Remap hjkl navigation to colemak-dh layout
map global normal n h
map global normal e k
map global normal i j
map global normal o l

map global normal N H
map global normal E K
map global normal I J
map global normal O L

# Remap insert command
map global normal h i

# Insert line above or below
map global normal l o
map global normal L O

# No alt, remap
map global normal <c-f> <a-f>  -docstring 'same as [f] but in the other direction'
map global normal <c-t> <a-t>  -docstring 'same as [t] but in the other direction'
map global normal <c-n> <a-h>  -docstring 'select to beginning of line'
map global normal <c-o> <a-l>  -docstring 'select to end of line'
map global normal <c-s> <a-s>  -docstring 'split selections on line boundaries'
map global normal <c-S> <a-S>  -docstring 'select first and last character of each selection'
map global normal '<c-;>' '<a-;>'  -docstring 'flip direction of each selection'
map global normal <c-:> <a-:>  -docstring 'ensure selections are in forward direction (cursor then anchor)'
map global normal <c-,> <a-,>  -docstring 'clear the main selection'

# Scrolling remap
map global normal <c-e> <pageup> -docstring 'Scroll screen up'
map global normal <c-i> <pagedown> -docstring 'Scroll screen down'

map global normal <c-/> ':comment-line<ret>' -docstring 'comment selected lines'

# Ctrl-s to save
map global normal <c-s> ':w<ret>' -docstring 'write buffer'
# Ctrl-x to save and exit
map global normal <c-x> ':w<ret>:db<ret>' -docstring 'write close buffer'

# Colemak goto remappings
map global goto n h -docstring 'line start'
map global goto o l -docstring 'line end'
map global goto e k -docstring 'buffer top'
map global goto i j -docstring 'buffer bottom'

map global goto h n
map global goto l o
map global goto k e
map global goto j i

# Remap view keys
map global view n h
map global view o l
map global view e k
map global view i j

map global normal ( ': buffer-previous<ret>' -docstring 'prev buffer'
map global normal ) ': buffer-next<ret>' -docstring 'next buffer'

# map global normal [ ( -docstring 'rotate main selection'
    
# map global normal ] ) -docstring 'rotate main selection backwards'
# Make x select <param> lines up and down
def -params 1 extend-line-down %{
  exec "<a-:>%arg{1}J"
}

def -params 1 extend-line-up %{
  exec "<a-:><a-;>%arg{1}K<a-;>"
  try %{
    exec -draft ';<a-K>\n<ret>'
    exec X
  }
  exec '<a-;><a-X>'
}


map global normal x ':extend-line-down %val{count}<ret>'
map global normal X ':extend-line-up %val{count}<ret>'

map global normal k n -docstring 'next result'
map global normal K N -docstring 'next result add selection'

map global normal <a-k> <c-n> -docstring 'prev result'
map global normal <a-K> <c-N> -docstring 'prev result add selection'

map global insert <down> <c-n> -docstring 'next completion'
map global insert <up> <c-p> -docstring 'prev completion'

# ------------------
# User mode Bindings
# ------------------
# Accessed with <space>
declare-user-mode git
map global user g ': enter-user-mode git<ret>' -docstring "Git mode"
map global user p '<a-!>wl-paste -n<ret>' -docstring 'paste from system clip board'
hook global RegisterModified '"' %{ nop %sh{
  printf %s "$kak_main_reg_dquote" | wl-copy > /dev/null 2>&1 &
}}

# Open kakrc, double quotes needed to ecsape the kakrc_path option
map global user o ":edit %opt{kakrc_path}<ret>" -docstring 'open kakrc'
map global user z ":edit %opt{shell_config}<ret>" -docstring 'open shell config'
map global user S ":edit %opt{sway_conf_path}<ret>" -docstring 'open wm config'
# Git Mode Bindings
map global git d '<esc>:git show-diff<ret>' -docstring "Show diff"

# -----------------
# LSP Configuration
# -----------------

# plug "kak-lsp/kak-lsp" do %{
#   cargo install --locked --force --path .
#   mkdir -p ~/.config/kak-lsp
#   cp -n kak-lsp.toml ~/.config/kak-lsp/
# } config %{

  # enable inlay hints
  # set global lsp_diagnostic_line_error_sign '║'
  # set global lsp_diagnostic_line_warning_sign '┊'

define-command lsp-restart -docstring 'restart lsp server' %{ lsp-stop; lsp-start }
map global insert <tab> '<a-;>:try lsp-snippets-select-next-placeholders catch %{ execute-keys -with-hooks <lt>tab> }<ret>' -docstring 'Select next snippet placeholder'
map global object a '<a-semicolon>lsp-object<ret>' -docstring 'LSP any symbol'
map global object <a-a> '<a-semicolon>lsp-object<ret>' -docstring 'LSP any symbol'
map global object e '<a-semicolon>lsp-object Function Method<ret>' -docstring 'LSP function or method'
map global object k '<a-semicolon>lsp-object Class Interface Struct<ret>' -docstring 'LSP class interface or struct'
map global object d '<a-semicolon>lsp-diagnostic-object --include-warnings<ret>' -docstring 'LSP errors and warnings'
map global object D '<a-semicolon>lsp-diagnostic-object<ret>' -docstring 'LSP errors'

eval %sh{kak-lsp --kakoune -s $kak_session}  # Not needed if you load it with plug.kak.
hook global WinSetOption filetype=(rust|python|go|javascript|typescript|c|cpp|dart|zig|nix|ocaml) %{
    lsp-enable-window
    map global user l ': enter-user-mode lsp<ret>' -docstring 'lsp mode'
    map global user h ': enter-user-mode lsp<ret>h' -docstring 'lsp help'
    map global user a ': enter-user-mode lsp<ret>a' -docstring 'lsp code action'
    set-option global lsp_hover_anchor true
    lsp-inlay-hints-enable global
    lsp-inlay-diagnostics-enable global
}

hook global KakEnd .* lsp-exit
  # load lsp for supported filetypes
  # set-option global lsp_cmd "kak-lsp -s %val{session} -vvv --log /tmp/kak-lsp.log"
  # hook global WinSetOption filetype=(rust|python|go|javascript|typescript|c|cpp|zig|dart|ruby) %{
  #   lsp-enable-window
  #   # lsp-auto-hover-enable
  #   map global user l ': enter-user-mode lsp<ret>' -docstring 'lsp mode'
  #   map global user h ': enter-user-mode lsp<ret>h' -docstring 'lsp help'
  #   map global user a ': enter-user-mode lsp<ret>a' -docstring 'lsp code action'
  #   set-option global lsp_hover_anchor false
  # }


# }


# -----------------
# Other Plugins
# -----------------

# fzf file picker
map global goto f '<esc>:prompt -shell-script-candidates %{ fd --type f --hidden } file: %{ edit %val{text} }<ret>' -docstring "file"
map global goto b '<esc>:prompt -buffer-completion buffer: %{ buffer %val{text} }<ret>' -docstring "buffer"

map global user b ':fzf-mode<ret>b' -docstring 'fzf current buffers'
map global user / ':fzf-mode<ret>g' -docstring 'fzf search in project'

map global normal <c-m> ':surround<ret>'