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

bundle kakoune-lsp "https://github.com/kakoune-lsp/kakoune-lsp" %{
    set-option global lsp_diagnostic_line_error_sign 
    set-option global lsp_diagnostic_line_hint_sign ?
    set-option global lsp_diagnostic_line_info_sign ℹ
    set-option global lsp_diagnostic_line_warning_sign 

    set-option global lsp_hover_anchor true

    # kakoune-lsp doesn't have a command to enable auto-hover per buffer,
    # only supporting enabling auto-hover globally,
    # which causes issues in filetypes that don't have lsp set up.
    # So, we need to define one ourselves.
    define-command lsp-auto-hover-enable-buffer %{
        remove-hooks buffer lsp-auto-hover
        hook -group lsp-auto-hover buffer NormalIdle .* %{ lsp-check-auto-hover lsp-hover }
    }

    # Convenience commands for enabling lsp features
    define-command lsp-semantic-tokens-enable %{
        hook window -group semantic-tokens BufReload .* lsp-semantic-tokens
        hook window -group semantic-tokens NormalIdle .* lsp-semantic-tokens
        hook window -group semantic-tokens InsertIdle .* lsp-semantic-tokens
        hook -once -always window WinSetOption filetype=.* %{
            remove-hooks window semantic-tokens
        }
    }
    define-command lsp-enable-buffer %{
        lsp-enable-window
        lsp-auto-hover-enable-buffer
        lsp-inlay-diagnostics-enable buffer
        lsp-inlay-hints-enable buffer
    }

    # We want to be explicit about lsp server settings per filetype
    # So, we are disabling the default lsp server hooks
    remove-hooks global lsp-filetype-.*

    set global lsp_debug true
}
bundle-install-hook kakoune-lsp %{
    CARGO_PROFILE_RELEASE_LTO=false cargo install --locked --force --path .
}

bundle-theme everforest.kak "https://codeberg.org/jdugan6240/everforest.kak"

bundle luar "https://github.com/gustavo-hms/luar" %@
    require-module luar

    # Luar's highlighters are very subtly broken so we need to replace them.
    # These aren't perfect either, as they assume the "lua" or "fennel" command invocation is the first thing in the line,
    # but whatever - when both "lua" and "fennel" can appear in many contexts, it's hard to be robust without a true parser.
    remove-highlighter shared/kakrc/code/lua
    remove-highlighter shared/kakrc/lua1
    remove-highlighter shared/kakrc/lua2
    remove-highlighter shared/kakrc/lua3
    remove-highlighter shared/kakrc/lua4
    remove-highlighter shared/kakrc/code/fennel
    remove-highlighter shared/kakrc/fennel1
    remove-highlighter shared/kakrc/fennel2
    remove-highlighter shared/kakrc/fennel3
    remove-highlighter shared/kakrc/fennel4
    add-highlighter shared/kakrc/code/lua regex (^\s*)lua(?:(?=\s)|\z) 0:keyword
    add-highlighter shared/kakrc/lua1 region -recurse '\{' '^\s*lua\s(([\s{}\w%/$-|''"])* )?%\{\K' '\}' ref lua
    add-highlighter shared/kakrc/lua2 region -recurse '\(' '^\s*lua\s(([\s{}\w%/$-|''"])* )?%\(\K' '\)' ref lua
    add-highlighter shared/kakrc/lua3 region -recurse '\[' '^\s*lua\s(([\s{}\w%/$-|''"])* )?%\[\K' '\]' ref lua
    add-highlighter shared/kakrc/lua4 region -recurse '<' '^\s*lua\s(([\s{}\w%/$-|''"])* )?%<\K' '>' ref lua
    add-highlighter shared/kakrc/code/fennel regex (^\s*)fennel(?:(?=\s)|\z) 0:keyword
    add-highlighter shared/kakrc/fennel1 region -recurse '\{' '^\s*fennel\s(([\s{}\w%/$-|''"])* )?%\{\K' '\}' ref fennel
    add-highlighter shared/kakrc/fennel2 region -recurse '\(' '^\s*fennel\s(([\s{}\w%/$-|''"])* )?%\(\K' '\)' ref fennel
    add-highlighter shared/kakrc/fennel3 region -recurse '\[' '^\s*fennel\s(([\s{}\w%/$-|''"])* )?%\[\K' '\]' ref fennel
    add-highlighter shared/kakrc/fennel4 region -recurse '<' '^\s*fennel\s(([\s{}\w%/$-|''"])* )?%<\K' '>' ref fennel
@

bundle kakoune-focus "https://github.com/caksoylar/kakoune-focus" %{
    map global user <space> ':focus-toggle<ret>' -docstring "toggle selections focus"
}

bundle kakship https://github.com/eburghar/kakship %{
    kakship-enable
}
bundle-install-hook kakship %{
    cargo install --force --path . --root ~/.local
    if [ ! -e $kak_config/starship.toml ]; then
        cp starship.toml $kak_config/
    fi
}
bundle-cleaner kakship %{
    rm ~/.local/bin/kakship
}

bundle-customload kak-tree-sitter https://git.sr.ht/~hadronized/kak-tree-sitter %{
    evaluate-commands %sh{ kak-tree-sitter -dks --init $kak_session --with-highlighting -vvvvv }

    # kak-tree-sitter sometimes gets out of sync during editing, especially with large files
    # so, whenever we get back to normal mode, force an update
    hook global ModeChange .*:normal %{
        tree-sitter-buffer-update
    }
}
bundle-install-hook kak-tree-sitter %{
    cargo install --force --path kak-tree-sitter
    cargo install --force --path ktsctl
}
bundle-cleaner kak-tree-sitter %{
    cargo uninstall kak-tree-sitter
    cargo uninstall ktsctl
}

bundle kakeidoscope https://git.sr.ht/~orchid/kakeidoscope %{
    #hook global WinCreate .* kakeidoscope-enable-window
}
bundle-install-hook kakeidoscope %{
    cargo install --force --path .
}
bundle-cleaner kakeidoscope %{
    cargo uninstall kakeidoscope
}

bundle-noload kak-rainbower %{
    # This is the last commit of kak-rainbower that worked
    git clone https://github.com/crizan/kak-rainbower
    cd kak-rainbower
    git reset --hard 692c196650edd97da9ed6c275bb9b261630d063d
}%{
    set-option global rainbow_mode 0
    hook global WinCreate .* %{
        rainbow-enable-window
    }
}
bundle-install-hook kak-rainbower %{
    c++ rc/rainbower.cpp -o rc/rainbower
}
bundle-updater kak-rainbower %{
    # This is the last commit of kak-rainbower that worked
    #git reset --hard 692c196650edd97da9ed6c275bb9b261630d063d
    git reset --hard frederick-the-great
}

bundle kak-rainbow https://github.com/Bodhizafa/kak-rainbow %{
    hook global WinCreate .* %{
        rainbow-enable-window
    }
}

bundle pairs.kak https://github.com/42xel/pairs.kak %{
    pairs_enable
}

bundle parinfer-rust https://github.com/eraserhd/parinfer-rust %{
    hook global WinSetOption filetype=(clojure|fennel|lisp|picolisp|racket|scheme) %{
        parinfer-enable-window -smart
    }
}
bundle-install-hook parinfer-rust %{
    cargo install --force --path .
}
bundle-cleaner parinfer-rust %{
    cargo uninstall parinfer-rust
}

# Automatically clean any unregistered plugins
bundle-clean
