# 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
}
add-highlighter global/ show-matching

# LOCAL

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

colorscheme %sh{
	if [ -e ~/.lightmode ]; then
		echo four-colors-light
	else
		echo four-colors-dark
	fi
}

declare-user-mode file-mode
declare-user-mode options-mode

map global -docstring 'File' user 'f' ': enter-user-mode file-mode<ret>'
map global -docstring 'Buffer' user ' ' ': b '

map global -docstring 'Open (git)' file-mode 'a' ': file-open-git<ret>'
map global -docstring 'Git diff file' file-mode 'd' ': git diff %val{buffile}<ret>'
map global -docstring 'Open (fd)' file-mode 'f' ': file-open<ret>'
map global -docstring 'Open (recent)' file-mode 'r' ': file-open-recent<ret>'
map global -docstring 'Git grep' file-mode 'g' ': git grep '
map global -docstring 'Open (nnn)' file-mode 'n' ': nnn-open %val{buffile}<ret>'
map global -docstring 'Open (nnn)' file-mode 'N' ': nnn-open .<ret>'
map global -docstring 'Open/create in dir' file-mode 'c' ': open-create-in-dir<ret>'

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

map global -docstring 'LP Dark mode' options-mode 'd' ': colorscheme four-colors-dark<ret>'
map global -docstring 'LP Light mode' options-mode 'l' ': colorscheme four-colors-light<ret>'
map global -docstring 'Light mode' options-mode 'L' ': colorscheme one-light<ret>'
map global -docstring 'Dark mode' options-mode 'D' ': colorscheme one-darker<ret>'

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>'

define-command -override file-open %{
    prompt -menu -shell-script-candidates %{ fd -t f } '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-recent %{
    prompt -menu -shell-script-candidates %{ cat ~/.cache/kak-mru } 'File (recent): ' %{ edit -- %val{text} }
}

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"}/
}

# CHANGE STATUS BAR COLOR IN INSERT MODE

hook global ModeChange push:.*:insert %{
    set-face window StatusLine 'default,rgb:1c4a21'
}

hook global ModeChange pop:insert:.* %{
    unset-face window StatusLine
}

# ODIN

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

# 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

# COLORSCHEME

bundle-theme kak-one 'https://codeberg.org/raiguard/kak-one'

bundle-theme kakoune-themes 'https://codeberg.org/anhsirk0/kakoune-themes'

# FZF

bundle fzf.kak 'https://github.com/andreyorst/fzf.kak' %{
    map global -docstring 'FZF menu' user s ': fzf-mode<ret>'
    require-module fzf
    set-option global fzf_highlight_command "bat"
}

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"
}

# HARPOON

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

# EASYMOTION

bundle easymotion.kak https://git.sr.ht/~voroskoi/easymotion.kak %{
    require-module easymotion
    map global -docstring 'easy j' user <j> ': easymotion-j<ret>'
    map global -docstring 'easy k' user <k> ': easymotion-k<ret>'
    map global -docstring 'easy w' user <w> ': easymotion-w<ret>'
    map global -docstring 'easy b'user <b> ': easymotion-b<ret>'
}

# PENEIRA

bundle 'luar' 'https://github.com/gustavo-hms/luar' %{
    require-module luar
}

bundle 'peneira' 'https://github.com/gustavo-hms/peneira' %{
    require-module peneira

    define-command buffers %{
        peneira 'buffers: ' %{ eval "printf '%s\n' $kak_quoted_buflist" } %{
            buffer %arg{1}
        }
    }
    define-command peneira-git-files %{
        peneira 'git files: ' %{ git ls-files } %{ e %arg{1} }
    }
    define-command peneira-recent-files %{
        peneira 'mru files: ' %{ cat ~/.cache/kak-mru } %{ e %arg{1} }
    }

    map global -docstring 'Buffer' user ' ' ': buffers<ret>'

    declare-user-mode peneira-mode
    map global -docstring 'Peneira' user 'd' ': enter-user-mode peneira-mode<ret>'
    map global -docstring 'Git files' peneira-mode 'a' ': peneira-git-files<ret>'
    map global -docstring 'Buffers' peneira-mode 'b' ': buffers<ret>'
    map global -docstring 'Files' peneira-mode 'f' ': peneira-files<ret>'
    map global -docstring 'Files' peneira-mode 'F' ': peneira-local-files<ret>'
    map global -docstring 'Lines' peneira-mode 'l' ': peneira-lines<ret>'
    map global -docstring 'Recent files' peneira-mode 'r' ': peneira-recent-files<ret>'
}
