summaryrefslogtreecommitdiff
path: root/config/kak/kakrc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--config/kak/kakrc248
1 files changed, 248 insertions, 0 deletions
diff --git a/config/kak/kakrc b/config/kak/kakrc
new file mode 100644
index 0000000..26b0a88
--- /dev/null
+++ b/config/kak/kakrc
@@ -0,0 +1,248 @@
+# --- PLUGINS
+source "~/.config/kak/smarttab.kak"
+source "~/.config/kak/palette.kak"
+source "~/.config/kak/auto-pairs.kak"
+source "~/.config/kak/snippets.kak"
+source "~/.config/kak/phantom-selection.kak"
+
+# --- Auto pairs
+enable-auto-pairs
+
+# --- LSP & FORMATTING
+# https://discuss.kakoune.com/t/sane-kak-lsp-config-hook/2019
+hook global WinSetOption filetype=(rust|python|nim|go|javascript|typescript|c|cpp|ocaml|haskell|markdown|elixir|nix) %{
+ set global lsp_hover_anchor true
+ lsp-enable-window
+ lsp-auto-hover-insert-mode-enable
+}
+hook global WinSetOption filetype=(eml|markdown) %{
+ spell
+}
+hook global BufSetOption filetype=(javascript|typescript) %{
+ set-option buffer formatcmd "prettier --stdin-filepath=%val{buffile}"
+ hook buffer BufWritePre .* %{format}
+}
+
+hook global BufSetOption filetype=(javascript|typescript|elixir|haskell|rust|markdown|c|cpp) %{
+ lsp-auto-signature-help-enable
+}
+
+hook global BufSetOption filetype=(terraform|ocaml|elixir|nix) %{
+ hook buffer BufWritePre .* %{lsp-formatting-sync}
+}
+
+eval %sh{kak-lsp --kakoune -s $kak_session}
+lsp-enable
+
+# --- GREP
+set-option global grepcmd 'rg --column'
+
+# --- Line number
+add-highlighter global/ number-lines
+
+# --- COMMANDS
+define-command fd -docstring "find files" -params 1 %{ edit %arg{1} }
+complete-command fd shell-script-candidates %{ fd -t f }
+
+map global goto f '<esc>:prompt -menu -shell-script-candidates %{ fd -t f } fd: %{ edit %val{text} }<ret>' -docstring "Goto file"
+
+define-command z -docstring "Change Directory (Z)" -params 1 %{ change-directory %sh{zoxide query $1} }
+complete-command z shell-script-candidates %{ zoxide query -l }
+
+
+# --- KEYMAPS
+map global user l %{:enter-user-mode lsp<ret>} -docstring "LSP mode"
+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'
+map global object h '<a-semicolon>lsp-diagnostic-object<ret>' -docstring 'LSP errors'
+map global user k ':lsp-hover<ret>' -docstring 'LSP errors'
+map global normal \; ":"
+map global insert <a-k> "<a-;>:lsp-signature-help<ret>"
+
+# --- Hunks
+declare-user-mode hunk
+
+map global hunk ] ':git next-hunk<ret>' -docstring 'Git hunk next'
+map global hunk [ ':git prev-hunk<ret>' -docstring 'Git hunk prev'
+map -docstring 'Goto hunk' global user h ': enter-user-mode hunk<ret>'
+
+# --- Surround
+
+declare-user-mode surround
+
+define-command declare-surrounding-pair -params 4 -docstring 'declare-surrounding-pair <description> <alias> <opening> <closing>: declare surrounding pair' %{
+ map -docstring %arg{1} global surround %arg{2} "Z\i%arg{3}<esc>\a%arg{4}<esc>Hz"
+ map -docstring %arg{1} global surround %arg{3} "Z\i%arg{3}<esc>\a%arg{4}<esc>Hz"
+ map -docstring %arg{1} global surround %arg{4} "Z\i%arg{3}<esc>\a%arg{4}<esc>Hz"
+}
+
+declare-surrounding-pair 'parenthesis block' b ( )
+declare-surrounding-pair 'brace block' B { }
+declare-surrounding-pair 'bracket block' r [ ]
+declare-surrounding-pair 'quotation' \' \' \'
+declare-surrounding-pair 'double quotation' \" \" \"
+declare-surrounding-pair 'angle block' a <lt> <gt>
+
+map -docstring 'enter surround mode' global user c ': enter-user-mode surround<ret>'
+set-option -add global ui_options terminal_padding_char=.
+set-option -add global ui_options terminal_assistant=off
+
+# --- Git
+hook global WinCreate .* %{git show-diff}
+
+hook global BufWritePost .* %{git update-diff}
+
+declare-user-mode conflict-resolve
+map -docstring 'enter conflict resolution' global user u ': enter-user-mode conflict-resolve<ret>'
+
+map global object m %{c^[<lt>=]{4\,}[^\n]*\n,^[<gt>=]{4\,}[^\n]*\n<ret>} -docstring 'conflict markers'
+define-command conflict-use-1 %{
+ evaluate-commands -draft %{
+ execute-keys <a-h>h/^<lt>{4}<ret><a-x>d
+ execute-keys h/^={4}<ret>j
+ execute-keys -with-maps <a-a>m
+ execute-keys d
+ }
+} -docstring "resolve a conflict by using the first version"
+define-command conflict-use-2 %{
+ evaluate-commands -draft %{
+ execute-keys j
+ execute-keys -with-maps <a-a>m
+ execute-keys dh/^>{4}<ret><a-x>d
+ }
+} -docstring "resolve a conflict by using the second version"
+map global conflict-resolve a conflict-use-1 -docstring "resolve conflict using first version"
+map global conflict-resolve b conflict-use-2 -docstring "resolve conflict using second version"
+
+# --- VISUAL
+
+colorscheme simple
+
+# Width of a tab
+set-option global tabstop 2
+
+# Indent with 4 spaces
+set-option global indentwidth 2
+
+# Use spaces instead of tabs
+hook global WinCreate .* %{ expandtab }
+
+# Enable spelling for markdown
+hook global WinCreate filetype=(markdown) %{ spell }
+
+# Softwrap long lines
+add-highlighter global/ wrap -word -indent
+
+# Show git branch in statusline
+declare-option -docstring "name of the git branch holding the current buffer" \
+ str modeline_git_branch
+
+hook global WinCreate .* %{
+ hook window NormalIdle .* %{ evaluate-commands %sh{
+ branch=$(cd "$(dirname "${kak_buffile}")" && git rev-parse --abbrev-ref HEAD 2>/dev/null)
+ if [ -n "${branch}" ]; then
+ printf 'set window modeline_git_branch %%{%s}' "${branch}"
+ fi
+ } }
+}
+
+hook global WinCreate .* %{ evaluate-commands %sh{
+ is_work_tree=$(cd "$(dirname "${kak_buffile}")" && git rev-parse --is-inside-work-tree 2>/dev/null)
+ if [ "${is_work_tree}" = 'true' ]; then
+ printf 'set-option window modelinefmt %%{%s}' "%opt{modeline_git_branch} ${kak_opt_modelinefmt}"
+ fi
+}}
+
+## Yanking writes to wayland
+hook global RegisterModified '"' %{ nop %sh{
+ case $(uname) in
+ Linux)
+ wl-copy -n "$kak_main_reg_dquote" > /dev/null 2>&1 & ;;
+ Darwin)
+ printf "%s" "$kak_main_reg_dquote" | pbcopy ;;
+ esac
+}}
+
+
+# --- CODING ASSISTANT
+declare-user-mode assistant
+
+# Chatgpt
+map global assistant -docstring "Replace selection with code assistant's answer" r '<a-!>tee /tmp/kak-tmp-code.txt; cat /tmp/kak-tmp-code.txt | chatgpt "You are a code generator.\nWriting comments is forbidden.\nWriting test code is forbidden.\nWriting English explanations is forbidden.\nMax 10 lines\nWrap new code in markdown code block\nComplete the $kak_bufname code\n" | filter-code-blocks<ret>'
+
+map global assistant -docstring "Ask chatgpt about the selection!" q '<a-|>(tee /tmp/chatgpt.txt; echo "\nWhat is this code in my $kak_bufname file?" >> /tmp/chatgpt.txt)<ret>:info -title "chatgpt" "%sh{cat /tmp/chatgpt.txt | xargs -I _ chatgpt -q _ --set-max-tokens 500}"<ret>'
+
+
+# Ollama
+map global assistant -docstring "Replace selection with code assistant's answer" o '<a-!>tee /tmp/kak-tmp-code.txt;echo "You are a code generator.\nWriting comments is forbidden.\nWriting test code is forbidden.\nWriting English explanations is forbidden.\nMax 10 lines\nContinue this $kak_bufname code:\n" > /tmp/kak-gpt-prompt.txt;cat /tmp/kak-tmp-code.txt >> /tmp/kak-gpt-prompt.txt;ollama-cli -p -s -f /tmp/kak-gpt-prompt.txt<ret>'
+
+map -docstring 'enter assistant mode' global user a ': enter-user-mode assistant<ret>'
+
+
+hook global BufSetOption filetype=(ocaml) %{
+ map buffer assistant -docstring "Add rapper definitions to selected code" u '<a-|>tee /tmp/ocaml-rapper.txt;ocaml-rapper-helper /tmp/ocaml-rapper.txt<ret>'
+}
+
+
+# --- SNIPPETS
+
+map global insert <a-f> "<esc>: phantom-selection-iterate-next<ret>i"
+map global insert <a-F> "<esc>: phantom-selection-iterate-prev<ret>i"
+map global user f ":phantom-selection-select-all; phantom-selection-clear<ret>,"
+
+hook global BufSetOption filetype=(ocaml) %{
+ set buffer snippets %opt{snippets} # keep global snippets (if any)
+ set -add buffer snippets 'Import Html libs' 'imp-h' %{ snippets-insert %{let open Dream_html in
+let open HTML in
+ }
+ }
+ set -add buffer snippets '[letform-] Create Web Form' 'letform-' \
+ %{ phantom-selection-clear ; snippets-insert %{let form ~csrf req =
+ match%lwt Dream.form ~csrf req with
+ | `Ok [ ("${}", ${}); ] ->
+ Lwt_result.return (${})
+ | e -> Lwt_result.fail (`Form e)
+ ${}
+}; phantom-selection-add-selection ; phantom-selection-iterate-next }
+
+ set -add buffer snippets '[form-n] Create user form (base)' 'form-n' \
+ %{ phantom-selection-clear ; snippets-insert %{null
+ [ h3 [] [txt "${}"]
+ ; form
+ [ id "${}"; action "/${}"; method_ `POST ]
+ [ csrf_tag req
+ ; br []
+ ]
+ ]
+}; phantom-selection-add-selection ; phantom-selection-iterate-next }
+
+ set -add buffer snippets '[form-tf] Create user form (text field)' 'form-tf' \
+ %{phantom-selection-clear ; snippets-insert %{; label [ for_ "${}" ] [ txt "${}" ]
+ ; input [ id "${}"; name "${}"; type_ "text" ]
+ ; br []
+}; phantom-selection-add-selection ; phantom-selection-iterate-next }
+
+ set -add buffer snippets '[form-b] Create user form (submit)' 'form-b' \
+ %{ snippets-insert %{; button [type_ "submit"] [ txt "${}" ]
+}; }
+
+ set -add buffer snippets '[let-e] Expect test' 'let-e' \
+ %{ phantom-selection-clear ; snippets-insert %{let%expect_test "${}" =
+ Printf.printf ;
+ [%expect {| |}] |> Lwt.return
+ }; phantom-selection-add-selection ; phantom-selection-iterate-next }
+
+ set -add buffer snippets '[let-c] controller' 'let-c' \
+ %{ phantom-selection-clear ; snippets-insert %{let controller ?(csrf=true) =
+ User.Session.auth_middleware
+ @@ fun req ->
+ match%lwt logic ~csrf req with
+ | Ok ${} -> ${}
+ | Error e -> Components.Errors.handle log e
+ }; phantom-selection-add-selection ; phantom-selection-iterate-next }
+}