summaryrefslogtreecommitdiff
path: root/config/kak/kakrc
blob: 895be8f3e44082dd1dff6843cfee74d80a7e8d5c (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# --- 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"
source "~/.config/kak/expand-region.kak"

# --- Auto pairs
enable-auto-pairs

# --- Make 
hook global BufSetOption filetype=(go) %{
	set-option buffer makecmd "go build"
}

# --- 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 BufCreate .*\.(njk|tmpl) %{ set buffer filetype html }
hook global WinSetOption filetype=(html) %{
	set-option buffer tabstop 2
}
hook global WinSetOption filetype=(eml|markdown|org) %{
    spell
}
hook global BufSetOption filetype=(javascript|typescript|html) %{
  set-option buffer formatcmd "prettier --stdin-filepath=%val{buffile}"
  hook buffer BufWritePre .* %{format}
}

hook global BufSetOption filetype=(go) %{
	set-option global make_error_pattern "^([^:\n]+):(\d+):(?:(\d+):)?"
}

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'

# --- Tab complete

hook global InsertCompletionShow .* %{
  map window insert <tab> <c-n>
  map window insert <s-tab> <c-p>
}

hook global InsertCompletionHide .* %{
  unmap window insert <tab> <c-n>
  unmap window insert <s-tab> <c-p>
}

# --- Line number
add-highlighter global/ number-lines -separator ' '
 
# --- 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  }

define-command select-angle-bracket  %{ execute-keys <a-i>c<gt>,<lt><ret> } -docstring "Select content within angle bracket"

# --- 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 l '<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 a '<a-semicolon>select-angle-bracket<ret>' -docstring 'XML Tag'
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>"
map global user m     ':make<ret>' -docstring "Run make"
map global normal + ":expand<ret>"

# --- Hunks
declare-user-mode hunk


map global hunk s ':git apply --cached<ret>:git update-diff<ret>' -docstring 'Stage hunk at cursor'
map global hunk r ':git apply --reverse<ret>:git update-diff<ret>' -docstring 'Remove hunk at cursor'
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 'stars' * * *
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 / THEME
 
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
}

# Show TODO comments
hook -group todohighlight global BufCreate .* %{
    add-highlighter buffer/todo-highlight regex "%opt{comment_line}[ \t]*\b(TODO|XXX|FIXME|MAYBE)\b" 1:default+bu@comment
}

# Width of a tab
set-option global tabstop 8

# Indent with 4 spaces
set-option global indentwidth 0

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

hook global RegisterModified '"' %{ nop %sh{
  printf %s "$kak_main_reg_dquote" | xsel --input --clipboard
}}

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=(go) %{

	set buffer snippets %opt{snippets} # keep global snippets (if any)
	set -add buffer snippets 'err != nil' 'err@' %{ snippets-insert %{if err != nil {
	${}
}
	}}

}

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

# TAGS