-
Notifications
You must be signed in to change notification settings - Fork 2
/
browser.kak
364 lines (335 loc) · 12.2 KB
/
browser.kak
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
declare-option -hidden str files_plugin_path %sh{ dirname "$kak_source" }
declare-option -hidden str files_browse_buffer 'files-browser'
declare-option -hidden str files_selection_buffer 'files-selections'
declare-option -hidden str files_markers "*/=>@|"
declare-option -hidden str files_disabled_keys "i I a A r R p P d <a-d> ! <a-!> | <a-|> <gt> <a-gt> <lt> <a-lt>"
declare-option -hidden bool files_show_hidden true
declare-option -hidden bool files_directories_first true
declare-option -hidden bool files_long_format false
declare-option -hidden str files_sorting "name"
declare-option -hidden bool files_sorting_reverse false
declare-option -hidden str files_sorting_opts "none name size time version extension"
declare-option -hidden str files_selection_quoting "backslash"
declare-option -hidden str files_quoting_opts "none backslash quotes double_quotes"
declare-option -hidden str files_options_with_setters "show_hidden directories_first long_format sorting_reverse"
declare-option -hidden str files_togglable_options "show_hidden directories_first long_format"
declare-option -hidden str files_cwd
declare-option -hidden int files_browse_buffer_counter 0
declare-option -hidden line-specs files_long_format_gutter
declare-option -hidden str files_editor_client
define-command -hidden files-ls %{
execute-keys %sh{
cmd="$kak_opt_files_plugin_path/nice-ls.sh\
'$kak_opt_files_cwd' '$kak_opt_files_show_hidden' '$kak_opt_files_directories_first' '$kak_opt_files_sorting' '$kak_opt_files_sorting_reverse'"
echo "%%d!$cmd<ret>d"
}
evaluate-commands %{ try %{
execute-keys gjx_
evaluate-commands %sh{
echo set-option buffer files_long_format_gutter $kak_timestamp "$kak_selection"
}
execute-keys xdgk
}}
}
define-command -docstring 'create new filebrowser in kakoune buffer' files-new-browser -params 0..1 %{
edit -scratch "*%opt{files_browse_buffer}-%opt{files_browse_buffer_counter}*"
set-option buffer filetype %opt{files_browse_buffer}
set-option -add global files_browse_buffer_counter 1
files-set-cwd %sh{
[ -n "$1" ] && echo "$1" || pwd
}
}
define-command -docstring 'create new filebrowser opening files in current client' files-new-tied-browser -params 0..1 %{
evaluate-commands -draft %sh{
echo new
echo files-new-browser "$@"
echo set-option buffer files_editor_client "$kak_client"
}
}
define-command -hidden files-create-hl %{
add-highlighter shared/files-filetypes group
add-highlighter shared/files-filetypes/ regex '(?S)^(.*)/@?$' 1:blue
add-highlighter shared/files-filetypes/ regex '(?S)^(.*)\*@?$' 1:green
add-highlighter shared/files-filetypes/ regex '(?S)^(.*)\|@?$' 1:yellow
add-highlighter shared/files-filetypes/ regex '(?S)^(.*)=@?$' 1:magenta
add-highlighter shared/files-filetypes/ regex '@$' 0:cyan
}
define-command -docstring 'manually redraw filebrowser buffer' files-redraw-browser -params 0..1 %{
evaluate-commands %sh{
if [ "$1" = false ]; then
echo "files-ls"
exit
fi
echo "files-select-current-entry
evaluate-commands -save-regs e %{
set-register e %reg{.}
files-ls
files-focus-entry %reg{e}
}"
}
remove-highlighter buffer/long-format
evaluate-commands %sh{
if [ "$kak_opt_files_long_format" = "true" ]; then
echo add-highlighter buffer/long-format flag-lines Default files_long_format_gutter
fi
}
}
define-command -hidden files-focus-entry -params 1 %{
try %{ execute-keys "/^\Q%arg{1}\E[%opt{files_markers}]*$<ret>gi" }
}
define-command -hidden files-generate-ls-option-setters %{ evaluate-commands %sh{
for opt in $kak_opt_files_options_with_setters; do
echo "\
define-command -params 1 files-set-$opt %{
set-option buffer files_$opt %arg{1}
files-redraw-browser
}"
done
}}
define-command -hidden files-generate-ls-option-togglers %{ evaluate-commands %sh{
for opt in $kak_opt_files_togglable_options; do
echo "\
define-command files-toggle-$opt %{ evaluate-commands %sh{
if [ \"\$kak_opt_files_$opt\" = true ]; then
echo files-set-$opt false
else
echo files-set-$opt true
fi
}}
"
done
}}
define-command -docstring 'set browser cwd' files-set-cwd -params 1 %{
evaluate-commands %sh{
if [ ! -d "$1" ]; then
echo "fail not a directory"
fi
if ! cd "$1"; then
echo "fail can't cd to directory"
fi
echo "set-option buffer files_cwd '$(pwd)'"
}
files-redraw-browser false
}
define-command -docstring 'go up in hierarchy' files-cd-parent %{ evaluate-commands %sh{
current_dir="$(basename "$kak_opt_files_cwd")"
echo "files-set-cwd '$(dirname "$kak_opt_files_cwd")'"
echo "files-focus-entry '$current_dir'"
}}
define-command -docstring %{
cd or open file
supports batching, you can open multiple files
} files-cd %{ evaluate-commands -save-regs rsep %{ try %{
set-register s ''
set-register e ''
set-register p ''
evaluate-commands %sh{
echo "$kak_selections_length" | grep -v ' ' >/dev/null && echo "set-register s true"
}
evaluate-commands -itersel %{
evaluate-commands -draft %{
files-full-path-of-choice
}
evaluate-commands %sh{
target="$kak_reg_r"
can_cd="$kak_reg_s"
if [ -d "$target" ]; then
if [ -n "$can_cd" ]; then
echo "files-set-cwd '$target'"
echo "fail found dir"
fi
else
echo "evaluate-commands -draft -try-client '$kak_opt_files_editor_client' %{ edit '$target' }"
echo "set-register e '$target'"
fi
}
}
evaluate-commands -draft %{
execute-keys <space>
files-full-path-of-choice
evaluate-commands %sh{
primary="$kak_reg_r"
[ -f "$primary" ] && echo "set-register p '$primary'"
}
}
evaluate-commands %sh{
focus_file="$kak_reg_e"
primary="$kak_reg_p"
if [ -n "$primary" ]; then
focus_file="$primary"
fi
[ -n "$focus_file" ] &&
echo "evaluate-commands -try-client '$kak_opt_files_editor_client' %{ edit '$focus_file' }" &&
echo "fail opening file"
}
evaluate-commands %{
execute-keys <space>
files-full-path-of-choice
evaluate-commands %sh{
primary="$kak_reg_r"
echo "files-set-cwd '$primary'"
}
}
}}}
hook global BufSetOption "filetype=%opt{files_browse_buffer}" %{
add-highlighter buffer/ ref files-filetypes
# files-disable-keys
hook buffer NormalIdle ".*" %{
info -title %opt{files_browse_buffer} %sh{
printf "%-20s\n" "$kak_opt_files_cwd$([ "$kak_opt_files_cwd" != "/" ] && echo "/" )"
rp="$(realpath "$kak_opt_files_cwd")"
if [ "$rp" != "$kak_opt_files_cwd" ]; then
echo "realpath: $rp/"
fi
echo -n "sorting: $kak_opt_files_sorting"
[ $kak_opt_files_sorting_reverse = "true" ] && echo -n "(rev)"
echo
echo -n "options: "
[ $kak_opt_files_show_hidden = "true" ] && echo -n "show_hidden "
[ $kak_opt_files_directories_first = "true" ] && echo -n "dir_first "
echo
echo -n "format: "
[ $kak_opt_files_long_format = "true" ] && echo -n "long" || echo -n "short"
echo
}
}
}
define-command -docstring 'change filebrowser path to realpath' files-cd-browser-to-realpath %{
set-option buffer files_cwd %sh{echo "$(realpath "$kak_opt_files_cwd")"}
}
define-command -docstring 'change server cwd to current filebrowser path' files-cd-server-to-browser %{
change-directory %opt{files_cwd}
}
define-command -docstring 'change filebrowser path to session pwd' files-cd-browser-to-server %{
files-set-cwd %sh{pwd}
}
define-command -docstring 'goto selections buffer' files-focus-selections %{
edit -scratch "*%opt{files_selection_buffer}*"
}
hook global BufCreate "\*%opt{files_selection_buffer}\*" %{
set-option buffer filetype %opt{files_selection_buffer}
}
define-command -hidden files-select-current-entry %{
execute-keys ";xH"
try %{
execute-keys "s[%opt{files_markers}]+$<ret>"
execute-keys "<a-;>h<a-h>"
}
}
define-command -hidden files-add-to-selection -params 1 %{ evaluate-commands -draft %{
files-focus-selections
execute-keys "gj"
try %{
execute-keys "x<a-k>^.+$<ret>"
execute-keys "o<esc>"
}
evaluate-commands %sh{
sel="$1"
if [ -d "$sel" ] && [ "$sel" != "/" ]; then
sel="$sel/"
fi
case "$kak_opt_files_selection_quoting" in
"quotes")
sel="'$sel'"
;;
"double_quotes")
sel="\"$sel\""
;;
"backslash")
sel="$(echo "$sel" | sed -E 's_[[:space:]]|\$|\*|\||\\|~|\(|\)|\{|\}|"|'\''_\\&_g')"
;;
esac
echo execute-keys "%{i$sel<esc>}"
}
}}
define-command -hidden files-full-path-of-choice %{
files-select-current-entry
evaluate-commands %sh{
cwd="$kak_opt_files_cwd"
choice="$kak_selection"
if [ "$cwd" != "/" ]; then
target="$kak_opt_files_cwd/$choice"
else
target="/$choice"
fi
echo "set-register r '$target'"
}
}
define-command -docstring %{
add entry under cursor to selections buffer
supports batching, all lines with cursor will be added
can do auto quoting
} files-add-entry-to-selection %{ evaluate-commands -draft -save-regs r %{
evaluate-commands -itersel %{
files-full-path-of-choice
files-add-to-selection %reg{r}
}
}}
define-command -docstring 'add cwd to selections buffer' files-add-cwd-to-selection %{
files-add-to-selection %opt{files_cwd}
}
define-command -docstring %sh{
echo set sorting
echo options: $kak_opt_files_sorting_opts
echo default: $kak_opt_files_sorting
echo can be reversed
} files-set-sorting -params 1 \
-menu -shell-script-candidates %{
for e in $kak_opt_files_sorting_opts; do
echo $e
done
} %{
evaluate-commands %sh{
if echo "$kak_opt_files_sorting_opts" | grep -w "$1" > /dev/null; then
echo "set-option buffer files_sorting $1"
echo "files-redraw-browser"
else
echo "fail unknown sorting option"
fi
}
}
define-command -docstring %sh{
echo set quoting
echo options: $kak_opt_files_quoting_opts
echo default: $kak_opt_files_selection_quoting
} files-set-selection-quoting -params 1 \
-menu -shell-script-candidates %{
for e in $kak_opt_files_quoting_opts; do
echo $e
done
} %{
evaluate-commands %sh{
if echo "$kak_opt_files_quoting_opts" | grep -w "$1" > /dev/null; then
echo "set-option global files_selection_quoting $1"
else
echo "fail unknown quoting option"
fi
}
}
define-command -docstring 'create filebrowser with cursor on current file' files-open-file-in-browser %{
evaluate-commands %sh{
path="$kak_buffile"
echo "files-new-browser '$(dirname "$path")'"
echo "files-focus-entry '$(basename "$path")'"
}
}
define-command -docstring 'create tied filebrowser with cursor on current file' files-open-file-in-tied-browser %{
evaluate-commands -draft %sh{
path="$kak_buffile"
echo new %\{ \
files-new-browser "'$(dirname "$path")'"\; \
set-option buffer files_editor_client "$kak_client"\; \
files-focus-entry "'$(basename "$path")'"\; \
\}
}
}
define-command -docstring 'spawn terminal in filesbrowser cwd(using kcr)' files-open-in-terminal %{ evaluate-commands -save-regs a %{
set-register e %sh{pwd}
files-cd-server-to-browser
connect-terminal
change-directory %reg{e}
}}
files-generate-ls-option-setters
# files-generate-getters
files-create-hl
files-generate-ls-option-togglers