# OPTIONS

set-option global startup_info_version 20260412
set-option global ui_options terminal_assistant=none

hook global WinCreate .* %{
    add-highlighter window/number-lines number-lines -relative -hlcursor
}

hook global BufCreate .*\.templ %{ set buffer filetype templ }
hook global BufCreate .*\.html.tmpl %{ set buffer filetype gotmpl }

hook global WinCreate ^[^*]+$ %{editorconfig-load}

# LOCAL

nop %sh{
	mkdir -p "$kak_config/autoload"
	[ -e "$kak_config/autoload/stdlib" ] || ln -s "$kak_runtime/autoload" "$kak_config/autoload/stdlib"
}

map global -docstring 'Buffer' user ' ' ': buffer '

declare-user-mode file-mode
map global -docstring 'Search/open' user 's' ': enter-user-mode file-mode<ret>'

map global -docstring 'Open (fd)' file-mode 'a' ': file-open<ret>'
map global -docstring 'Open (fd -H)' file-mode 'h' ': file-open-with-hidden<ret>'
map global -docstring 'Open (recent)' file-mode 'r' ': file-open-recent<ret>'
map global -docstring 'Open (nnn)' file-mode 'd' ': nnn-open %val{buffile}<ret>'
map global -docstring 'Open (nnn)' file-mode 'D' ': nnn-open .<ret>'
map global -docstring 'Open/create in dir' file-mode 'c' ': open-create-in-dir<ret>'
map global -docstring 'ctags file symbols' file-mode 's' ': ctags-file-symbols<ret>'
map global -docstring 'ctags project symbols' file-mode 'w' ': ctags-project-symbols<ret>'
map global -docstring 'Grep' file-mode 'g' ':grep '
map global -docstring 'Grep next match' file-mode 'n' ': grep-next-match<ret>'
map global -docstring 'Grep previous match' file-mode 'p' ': grep-prev-match<ret>'
map global -docstring 'Grep current file' file-mode 'l' ':grep-file '
map global -docstring 'Grep current file' file-mode 'L' ': grep-file %val{selection}<ret>'
map global -docstring 'Delete buffer' file-mode 'x' ': delete-buffer '
map global -docstring 'REPL send selection' file-mode 'u' ': repl-send-text<ret>'
map global -docstring 'REPL send paragraph' file-mode 'U' ': repl-send-paragraph<ret>'

declare-user-mode fossil-mode
map global -docstring 'Fossil' user 'f' ': enter-user-mode fossil-mode<ret>'

map global -docstring 'Open file' fossil-mode 'a' ': file-open-fossil<ret>'
map global -docstring 'Grep' fossil-mode 'g' ':fossil-grep '
map global -docstring 'ctags project symbols' fossil-mode 'w' ': ctags-project-fossil-symbols<ret>'

declare-user-mode git-mode
map global -docstring 'Git' user 'g' ': enter-user-mode git-mode<ret>'

map global -docstring 'Open file' git-mode 'a' ': file-open-git<ret>'
map global -docstring 'Blame' git-mode 'b' ': git blame<ret>'
map global -docstring 'Blame' git-mode 'B' ': git blame-jump<ret>'
map global -docstring 'Diff file' git-mode 'd' ': git diff %val{buffile}<ret>'
map global -docstring 'Diff' git-mode 'D' ': git diff<ret>'
map global -docstring 'Grep' git-mode 'g' ':git grep '
map global -docstring 'Grep' git-mode 'G' ': git grep %val{selection}<ret>'
map global -docstring 'Log' git-mode 'l' ': git log -- %val{buffile}<ret>'
map global -docstring 'Log' git-mode 'L' ': git log<ret>'
map global -docstring 'Show branch' git-mode 'c' ': git show-branch<ret>'
map global -docstring 'Status' git-mode 't' ': git status<ret>'
map global -docstring 'Show diff' git-mode 's' ': git-show-diff<ret>'
map global -docstring 'Show' git-mode 'S' ': git show<ret>'
map global -docstring 'Hide diff' git-mode 'h' ': git-hide-diff<ret>'
map global -docstring 'Next hunk' git-mode 'n' ': git next-hunk<ret>'
map global -docstring 'Prev hunk' git-mode 'p' ': git prev-hunk<ret>'

declare-user-mode options-mode
map global -docstring 'Options' user 'o' ': enter-user-mode options-mode<ret>'

map global -docstring 'Five Dark mode' options-mode 'd' ': colorscheme five-colors-dark<ret>'
map global -docstring 'Five Light mode' options-mode 'l' ': colorscheme five-colors-light<ret>'

declare-user-mode make-mode
map global -docstring 'Make' user 'k' ': enter-user-mode make-mode<ret>'

map global -docstring 'Make' make-mode 'a' ':make '
map global -docstring 'Next' make-mode 'n' ': make-next-error<ret>'
map global -docstring 'Previous' make-mode 'p' ': make-previous-error<ret>'
map global -docstring 'General messages' make-mode 'g' ': set window make_error_pattern "^([^:\n]+):(\d+):(?:(\d+):)? ([^\n]+)?"<ret>'

declare-user-mode code-mode
map global -docstring 'Code' user 'c' ': enter-user-mode code-mode<ret>'

map global -docstring 'Comment block' code-mode 'b' ': comment-block<ret>'
map global -docstring 'Comment line' code-mode 'l' ': comment-line<ret>'
map global -docstring 'Format buffer' code-mode 'f' ': format-buffer<ret>'

evaluate-commands %sh{
    if [ -n "$WAYLAND_DISPLAY" ]; then
        echo "
            map global -docstring 'Paste clipboard (append)' user 'p' '<a-!> wl-paste -n<ret>'
            map global -docstring 'Paste clipboard (insert)' user 'P' '! wl-paste -n<ret>'
            map global -docstring 'Yank to clipboard' user 'y' '<a-|> wl-copy<ret>'
        "
    elif [ -n "$DISPLAY" ]; then
        echo "
            map global -docstring 'Paste clipboard (append)' user 'p' '<a-!> xsel -bo<ret>'
            map global -docstring 'Paste clipboard (insert)' user 'P' '! xsel -bo<ret>'
            map global -docstring 'Yank to clipboard' user 'y' '<a-|> xsel -bi<ret>'
        "
    fi
}

map global -docstring 'Replace jump mode' user 'j' ': enter_jump_mode_with_replace_select_mode<ret>'
map global -docstring 'Extend jump mode' user 'e' ': enter_jump_mode_with_extend_select_mode<ret>'
map global -docstring 'Append jump mode' user 'a' ': enter_jump_mode_with_append_select_mode<ret>'

define-command -override file-open %{
    prompt -menu -shell-script-candidates %{ fd -t f } 'File: ' %{ edit -- %val{text} }
}

define-command -override file-open-with-hidden %{
    prompt -menu -shell-script-candidates %{ fd -t f -H } 'File: ' %{ edit -- %val{text} }
}

define-command -override file-open-git %{
    prompt -menu -shell-script-candidates %{ git ls-files } 'File (git): ' %{ edit -- %val{text} }
}

define-command -override file-open-fossil %{
    prompt -menu -shell-script-candidates %{ fossil ls } 'File (fossil): ' %{ edit -- %val{text} }
}

define-command -override fossil-grep -params 1- %{
    evaluate-commands %sh{
        printf '%s ' grep "$@" -- $(fossil ls)
    }
}

define-command -override file-open-recent %{
    prompt -menu -shell-script-candidates %{ cat ~/.cache/kak-mru } 'File (recent): ' %{ edit -- %val{text} }
}

define-command -override grep-file -params 1- %{
    grep %arg{@} -- %val{buffile}
}

define-command -override -params 1 nnn-open %{
    terminal nnn-kak-edit %arg{1} %val{session} %val{client}
}

define-command -override open-create-in-dir %{
    execute-keys ': edit ' %sh{dirname "$kak_buffile"}/
}

define-command -override repl-send-paragraph %{
    execute-keys -draft '<a-i>p: repl-send-text<ret>'
}

define-command git-log-show-current-commit %{
    execute-keys ';<a-/>^commit\s[0-9a-f]+<ret>b'
    git show %val{selection}
}

define-command -override git-show-diff %{
    hook buffer -group git-show-diff NormalIdle .* %{ git update-diff }
    git show-diff
}

define-command -override git-hide-diff %{
    remove-hooks buffer git-show-diff
    git hide-diff
}

define-command -override ctags-file-symbols %{
    prompt -menu -shell-script-candidates %{
        ctags -uo - -x "$kak_buffile" | awk -f "$kak_config/scripts/filesymbols.awk"
    } 'File symbols: ' %{
        evaluate-commands %sh{ awk -f "$kak_config/scripts/filesymbolscmd.awk" # "$kak_text" }
    }
}

define-command -override ctags-project-symbols %{
    prompt -menu -shell-script-candidates %{
        git ls-files | xargs wc | awk '$1 > 0 && $3/$1 < 1024 { print $4 }' | xargs ctags -uo - -x | awk -f "$kak_config/scripts/projectsymbols.awk"
    } 'Project symbols: ' %{
        evaluate-commands %sh{ awk -f "$kak_config/scripts/projectsymbolscmd.awk" # "$kak_text" }
    }
}

define-command -override ctags-project-fossil-symbols %{
    prompt -menu -shell-script-candidates %{
        fossil ls | xargs wc | awk '$1 > 0 && $3/$1 < 1024 { print $4 }' | xargs ctags -uo - -x | awk -f "$kak_config/scripts/projectsymbols.awk"
    } 'Project symbols: ' %{
        evaluate-commands %sh{ awk -f "$kak_config/scripts/projectsymbolscmd.awk" # "$kak_text" }
    }
}

set-face global JumpLabel 'rgb:faf8f5,rgb:af8842+F'

# CHANGE CURSOR IN INSERT MODE

hook global ModeChange (push|pop):.*:insert %{
    set-face window PrimarySelection rgb:000000,green+F
    set-face window SecondarySelection rgb:000000,blue+F
    set-face window PrimaryCursor rgb:000000,bright-green+bF
    set-face window SecondaryCursor rgb:000000,bright-blue+bF
    set-face window PrimaryCursorEol rgb:000000,bright-red+bF
    set-face window SecondaryCursorEol rgb:000000,bright-yellow+bF
}

# Undo colour changes when we leave insert mode.
hook global ModeChange (push|pop):insert:.* %{
    unset-face window PrimarySelection
    unset-face window SecondarySelection
    unset-face window PrimaryCursor
    unset-face window SecondaryCursor
    unset-face window PrimaryCursorEol
    unset-face window SecondaryCursorEol
}

# ODIN

hook global BufSetOption filetype=odin %{
    set-option buffer formatcmd 'odinfmt -stdin'
}

hook global BufSetOption filetype=go %{
    hook -group buf-write-pre-fmt buffer BufWritePre .* %{
        lsp-formatting-sync
    }
}

# LSP

eval %sh{kak-lsp}
lsp-enable

set-option global modelinefmt "%opt{lsp_modeline} %opt{modelinefmt}"

map global user l ': enter-user-mode lsp<ret>' -docstring 'LSP mode'

map global goto d <esc>:lsp-definition<ret> -docstring 'LSP definition'
map global goto r <esc>:lsp-references<ret> -docstring 'LSP references'
map global goto y <esc>:lsp-type-definition<ret> -docstring 'LSP type definition'

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 f '<a-semicolon>lsp-object Function Method<ret>' -docstring 'LSP function or method'
map global object t '<a-semicolon>lsp-object Class Interface Module Namespace Struct<ret>' -docstring 'LSP class or module'
map global object d '<a-semicolon>lsp-diagnostic-object error warning<ret>' -docstring 'LSP errors and warnings'
map global object D '<a-semicolon>lsp-diagnostic-object error<ret>' -docstring 'LSP errors'

map global lsp p '<esc>: lsp-find-error --previous error<ret>'   -docstring 'find previous error'

# BUNDLE

source "%val{config}/bundle/kak-bundle/rc/kak-bundle.kak"
bundle-noload kak-bundle https://codeberg.org/jdugan6240/kak-bundle

# RECENT

hook global BufCreate [^*].* %{
    nop %sh{
        mru=~/.cache/kak-mru
            echo "$kak_buffile" | awk '!seen[$0]++' - "$mru" | sponge "$mru"
}}

# MATCH

bundle match 'https://github.com/kmafeni04/match.kak' %{
    declare-user-mode match
    map global user m ': enter-user-mode match<ret>' -docstring 'Match'
    map global match m m -docstring 'Match next matching pair'
    map global match i ': match-inside<ret>' -docstring 'Match inside object'
    map global match a ': match-around<ret>' -docstring 'Match around object'
    map global match n ': match-next<ret>' -docstring 'Match next object'
    map global match p ': match-prev<ret>' -docstring 'Match previous object'
    map global match s ': match-surround-add<ret>' -docstring 'Surround selection with character'
    map global match d ': match-surround-delete<ret>' -docstring "Delete selection's surrounding character"
    map global match r ': match-surround-replace<ret>' -docstring "Replace selection's surrounding character"
    map global match u ': match-surround-undo<ret>' -docstring "Undo"
    map global match U ': match-surround-redo<ret>' -docstring "Redo"
}

# PICKERS

bundle pickers https://codeberg.org/kmafeni04/pickers.kak %{
    declare-user-mode pickers
    map global user u ':enter-user-mode pickers<ret>' -docstring 'pickers mode'
    map global pickers b ':pickers-buffer<ret>' -docstring 'Open fuzzy buffer picker'
    map global pickers i ':pickers-buffer-info<ret>' -docstring 'Show currently open buffers'
    map global pickers a ':pickers-buffer-alternate<ret>' -docstring 'Switch to previously opened buffer'
    map global pickers f ':pickers-file<ret>' -docstring 'Open fuzzy file picker in git root or current working directory'
    map global pickers n ':pickers-file /home/lupan/notes/<ret>' -docstring 'Open fuzzy file picker in notes directory'
    map global pickers o ':pickers-file-cbd<ret>' -docstring "Open fuzzy file picker in current buffer's directory"
    map global pickers g ':pickers-grep<ret>' -docstring "Open grep picker in git root"
    map global pickers G "<dquote>/y:pickers-grep<ret>" -docstring "Open grep picker in git root with selection as init"
    map global pickers <a-g> ':pickers-grep-cbd<ret>' -docstring "Open grep picker in buffer's directory"
    map global pickers <a-G> "<dquote>/y:pickers-grep-cbd<ret>" -docstring "Open grep picker in buffer's directory with selection as init"

    hook global WinCreate \*pickers-grep\* %{
      map window normal <ret> ':pickers-grep-open<ret>'
      map window normal <a-ret> "%%s<ret>)" -docstring "Select all matched grep text"
      alias window write pickers-grep-write
      alias window w pickers-grep-write
    }
    hook global WinCreate \*pickers-file\* %{
      map window normal <ret> ':pickers-file-open<ret>'
    }
    hook global WinCreate \*pickers-buffer\* %{
      map window normal <ret> ':pickers-buffer-open<ret>'
    }
}

# HARPOON

bundle kak-harpoon 'https://codeberg.org/raiguard/kak-harpoon' %{
    harpoon-add-bindings
}

# TREE SITTER

eval %sh{ kak-tree-sitter -dks --init $kak_session }

map global -docstring 'Tree-sitter' user 't' ': enter-user-mode tree-sitter<ret>'

define-command -override tree-sitter-user-after-highlighter %{
    add-highlighter -override buffer/show-matching show-matching
}

# LOCAL

define-command set-dark-or-light-colorscheme %{
    colorscheme %sh{
    	if [ -e ~/.lightmode ]; then
    		echo five-colors-light
    	else
    		echo five-colors-dark
    	fi
    }
}

set-dark-or-light-colorscheme
