summaryrefslogtreecommitdiff
path: root/config/kak/kakrc
blob: 02213b7894468fb16afe12228fb524a3062d0faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# --- 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|org) %{
    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 = ':lsp-formatting<ret>' -docstring "LSP Format"
map global user w '|fmt <ret>' -docstring "Wrap to 80 columns"
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

set-option global autocomplete prompt
set-option global autoinfo onkey

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

hook global ModeChange (push|pop):insert:.* %{
    set-face global PrimaryCursor      rgb:FFFFFF,rgb:000000+F
}

hook global ModeChange (push|pop):.*:insert %{
    set-face global PrimaryCursor      default,default+Bc
}
# 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|.org) %{ spell }

# Wrap for org and markdown
hook global WinCreate filetype=(markdown|.org) %{ autowrap-enable }

# 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)
      echo "$kak_main_reg_dquote" | xclip -selection clipboard -i  > /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  }
}