aboutsummaryrefslogtreecommitdiff
path: root/dot_config/kak/kakrc
blob: 4b1157222c610d71446ab5f5f6513af40f9c89b5 (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
#colorscheme catppuccin_latte

# Lines numbers and relative numbers
add-highlighter global/ number-lines -relative

# Highlight matching pair
add-highlighter global/ show-matching

# Wrap on word boundaries
add-highlighter global/ wrap -indent -word

# Highlight trailing whitespace
add-highlighter global/ regex \h+$ 0:Error

set-option global scrolloff 5,5

map global normal <c-v> ":comment-line<ret>"

# System clipboard bindings
map global user y "<a-|>cp.sh<ret>" -docstring "[Y]ank to system clipboard"
map global user p "|put.sh<ret>" -docstring "[P]ut from system clipboard"

### A note on the /dev/null black magic ###
# %sh is synchronous by design. It will wait for:
# 1. The shell process to exit.
# 2. All pipes (stdin, stdout, stderr) to close.
#
# The final `&` forks the process, satisfying the first condition, but all
# pipes are left open. So, we need to "close" them:
# - `> /dev/null` to close stdout
# - `2>&1` redirects stderr to stdout, which in this case is `/dev/null` - closed
# - `< /dev/null` closes stdin
# Now, as far as kakoune is concerned, the process has finished and all pipes
# are closed, so control immediately returns to the client.
define-command -docstring "Compile the current document" compile %{
    nop %sh{ {
        compiler $"{kak_buffile}"
    } > /dev/null 2>&1 < /dev/null & }
}
map global user c -docstring "[C]ompile the current file" ":compile<ret>"

map global user = -docstring "Format current buffer with :format" ":format<ret>"

hook global BufCreate .*\.[Rr]md %{ set-option buffer filetype markdown }

# Pattern for communicating with kak via it's session socket
define-command sockettest %{
    nop %sh{ {
        sleep 10
        echo "eval -client '$kak_client' 'echo sleep ended'" |
            kak -p ${kak_session}
    } > /dev/null 2>&1 < /dev/null & }
}

# fzf file chooser
define-command fzf_file %{
    nop %sh{
        # Create a temporary file for storage
        foot -T 'foot_tmp' dash -c '
            next=$(fzf --height=100% --layout=default --preview "highlight -O ansi {}")
            printf "eval -client %s %%{ edit %s }\n" "$kak_client" "$next" |
                kak -p "$kak_session"
        ' > /dev/null 2>&1 < /dev/null &
    }
}
map global user f -docstring "Browse [f]iles with fzf" ":fzf_file<ret>"

# fzf buffer chooser
define-command fzf_buffer %{
    nop %sh{
        # Create a temporary file for storage
        foot -T 'foot_tmp' dash -c '
            next=$(printf "%s\n" "$kak_buflist" | tr " " "\n" |
                fzf --height=100% --layout=default --preview "highlight -O ansi {}")
            printf "eval -client %s %%{ buffer %s }\n" "$kak_client" "$next" |
                kak -p "$kak_session"
        ' > /dev/null 2>&1 < /dev/null &
    }
}
map global user b -docstring "Browse [b]uffers with fzf" ":fzf_buffer<ret>"

define-command fifotest %{
    evaluate-commands %sh{
         # Create a temporary fifo for communication
         output=$(mktemp -d -t kak-temp-XXXXXXXX)/fifo
         mkfifo ${output}
         # run command detached from the shell
         { fzf > ${output} } > /dev/null 2>&1 < /dev/null &
         # Open the file in Kakoune and add a hook to remove the fifo
         echo "edit! -fifo ${output} *buffer-name*
               hook buffer BufClose .* %{ nop %sh{ rm -r $(dirname ${output})} }"
    }
}


# Trick to make compiler run async
#hook global BufWritePost .*\.(md|tex|ms)$ %{
#    nop %sh{ {
#        compiler $"{kak_buffile}"
#    } > /dev/null 2>&1 < /dev/null & }
#}
# Simpler version
# hook global BufWritePost .*\.(md|tex)$ %{
#     echo -debug %sh{compiler "${kak_buffile}"}
# }

# Filetypes with two-space indentation
hook global BufCreate .*\.(js|html|ts|tsx|hs)$ %{
    # Map tab key to `>` in normal mode -- inserts <indentwidth> spaces
    map global insert <tab> '<a-;><a-gt>'
    set-option buffer indentwidth 2
}
# Filetypes with four-space indentation
hook global BufCreate .*\.(py|json|jsonc)$ %{
    map global insert <tab> '<a-;><a-gt>'
    set-option buffer indentwidth 4
}

eval %sh{kak-lsp}
hook global WinSetOption filetype=(haskell|rust|python|go|javascript|typescript|c|cpp) %{
    lsp-enable-window
}

remove-hooks global lsp-filetype-python
hook -group lsp-filetype-python global BufSetOption filetype=python %{
    # set-option buffer lsp_servers %{
    #     [pylsp]
    #     root_globs = ["requirements.txt", "setup.py", "pyproject.toml", ".git", ".hg"]
    #     settings_section = "_"
    #     [pylsp.settings._]
    #     # See https://github.com/python-lsp/python-lsp-server#configuration
    #     # pylsp.configurationSources = ["flake8"]
    #     pylsp.plugins.jedi_completion.include_params = true
    # }
    set-option buffer lsp_servers %{
        [pyright-langserver]
        root_globs = ["requirements.txt", "setup.py", "pyproject.toml", "pyrightconfig.json", ".git", ".hg"]
        args = ["--stdio"]
    }
    # set-option -add buffer lsp_servers %{
    #     [ruff]
    #     args = ["server", "--quiet"]
    #     root_globs = ["requirements.txt", "setup.py", "pyproject.toml", ".git", ".hg"]
    #     settings_section = "_"
    #     [ruff.settings._.globalSettings]
    #     organizeImports = true
    #     fixAll = true
    # }
}

hook -group lsp-filetype-javascript global BufSetOption filetype=(?:javascript|typescript) %{
    set-option buffer lsp_servers %{
        [vtsls]
        root_globs = ["package.json", "tsconfig.json", "jsconfig.json", ".git", ".hg"]
        args = ["--stdio"]
        settings_section = "_"
        [vtsls.settings._]
        quotePreference = "single"
        # typescript.format.semicolons = "insert"
    }
    #set-option buffer lsp_servers %{
    #    [typescript-language-server]
    #    root_globs = ["package.json", "tsconfig.json", "jsconfig.json", ".git", ".hg"]
    #    args = ["--stdio"]
    #    settings_section = "_"
    #    [typescript-language-server.settings._]
    #    # quotePreference = "double"
    #    # typescript.format.semicolons = "insert"
    #}
    # set-option buffer lsp_servers %{
    #     [deno]
    #     root_globs = ["package.json", "tsconfig.json", ".git", ".hg"]
    #     args = ["lsp"]
    #     settings_section = "deno"
    #     [deno.settings.deno]
    #     enable = true
    #     lint = true
    # }
    # set-option buffer lsp_servers %opt{lsp_server_biome}
    # set-option buffer lsp_servers %{
    #     [eslint-language-server]
    #     root_globs = [".eslintrc", ".eslintrc.json"]
    #     args = ["--stdio"]
    #     workaround_eslint = true
    #     [eslint-language-server.settings]
    #     codeActionsOnSave = { mode = "all", "source.fixAll.eslint" = true }
    #     format = { enable = true }
    #     quiet = false
    #     rulesCustomizations = []
    #     run = "onType"
    #     validate = "on"
    #     experimental = {}
    #     problems = { shortenToSingleLine = false }
    #     codeAction.disableRuleComment = { enable = true, location = "separateLine" }
    #     codeAction.showDocumentation = { enable = false }
    # }
    # set-option buffer lsp_servers %{
    #     [tailwindcss-language-server]
    #     root_globs = ["tailwind.*"]
    #     args = ["--stdio"]
    #     [tailwindcss-language-server.settings.tailwindCSS]
    #     editor = {}
    # }
}

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 user h ':lsp-hover<ret>' -docstring 'LSP hover'

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