-
Notifications
You must be signed in to change notification settings - Fork 3
/
buffer-switcher.kak
90 lines (84 loc) · 2.82 KB
/
buffer-switcher.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
# we assume that a buffer name will never contain newlines (not exactly true, but who cares)
face global BufferSwitcherCurrent black,green
define-command buffer-switcher %{
try %{
b *buffer-switcher*
} catch %{
eval -save-regs '"/' %{
reg / "^\Q%val{bufname}\E$"
reg dquote %val{buflist}
edit -scratch *buffer-switcher*
exec '<a-P>)<a-,>i<ret><esc>'
exec '%<a-s>'
# remove *debug* buffer
exec -draft '<a-k>^\*debug\*$<ret>d'
try %{
# select current one
exec '<a-k><ret>'
# also highlight it in green
addhl buffer/ regex "%reg{/}" 0:BufferSwitcherCurrent
} catch %{
exec gg
}
map buffer normal <ret> ': buffer-switcher-switch<ret>'
map buffer normal <esc> ': delete-buffer *buffer-switcher*<ret>'
hook global WinDisplay -once .* %{ try %{ delete-buffer *buffer-switcher* } }
}
}
}
define-command -hidden buffer-switcher-switch %{
try buffer-switcher-delete-buffers
try buffer-switcher-sort-buffers
exec ',;xH'
buffer %val{selection}
try %{ delete-buffer *buffer-switcher* }
}
# delete all buffers whose lines were removed
define-command -hidden buffer-switcher-delete-buffers %{
# print buflist, and all lines
# everything that appears only once gets removed
eval -buffer *buffer-switcher* %{
exec '%<a-s>H'
eval %sh{
{
eval set -- "$kak_quoted_buflist"
for buf do
# ignore self and debug
if [ "$buf" = '*buffer-switcher*' ]; then
:
elif [ "$buf" = '*debug*' ]; then
:
else
printf '%s\n' "$buf"
fi
done
eval set -- "$kak_quoted_selections"
for buf do
printf '%s\n' "$buf"
done
} | awk "
// {
line=\$0
if (line in line_count)
line_count[line] = line_count[line] + 1;
else
line_count[line] = 1;
}
END {
for (line in line_count)
if (line_count[line] == 1)
{
gsub(\"'\", \"''''\", line);
print(\"try 'delete-buffer ''\" line \"'' '\");
}
}"
}
}
}
# re-arrange the buflist according to the order in the *buffer-switcher*
define-command -hidden buffer-switcher-sort-buffers %{
eval -buffer *buffer-switcher* %{
exec '%<a-s>H'
arrange-buffers %val{selections}
}
}