This repository has been archived by the owner on Jun 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
overstrike.kak
103 lines (87 loc) · 3.14 KB
/
overstrike.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
##
## overstrike.kak by lenormf
## Overstrike markup parser
##
declare-option -hidden range-specs overstrike_ranges
set-face global OverstrikeBold default+b
set-face global OverstrikeUnderline default+u
evaluate-commands %sh{
# https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
ansi_index_to_color() {
case "${1}" in
0) echo "black";;
1) echo "red";;
2) echo "green";;
3) echo "yellow";;
4) echo "blue";;
5) echo "magenta";;
6) echo "cyan";;
7) echo "white";;
*) echo "default";;
esac
}
ansi_to_face() {
capacity="$1"
color_fg='default'
color_bg='default'
attributes='e'
descriptors=$(expr "${capacity}" : '^.\[\([0-9]*\(;[0-9]*\)*\)m$')
if [ $? -eq 0 ]; then
# TODO: support bright colors, whenever kakoune supports them as well
for code in $(printf %s "${descriptors}" | tr ';' '\n' | sed 's/ +//g'); do
code=$((code))
case "${code}" in
1) attributes="${attributes}b";;
3) attributes="${attributes}i";;
4) attributes="${attributes}u";;
5) attributes="${attributes}B";;
7) attributes="${attributes}r";;
30|31|32|33|34|35|36|37) color_fg=$(ansi_index_to_color $((code - 30)));;
40|41|42|43|44|45|46|47) color_bg=$(ansi_index_to_color $((code - 40)));;
*) printf 'Unsupported code: %s\n' "${code}" >&2;;
esac
done
else
printf 'Unsupported capacity format: %s\n' "${capacity}" >&2
exit 1
fi
printf %s\\n "${color_fg},${color_bg}+${attributes}"
}
if [ -n "${LESS_TERMCAP_md}" ] && [ -n "${LESS_TERMCAP_us}" ]; then
face_bold=$(ansi_to_face "${LESS_TERMCAP_md}")
face_underline=$(ansi_to_face "${LESS_TERMCAP_us}")
if [ -n "${face_bold}" ]; then
printf 'set-face global OverstrikeBold "%s"\n' "${face_bold}"
fi
if [ -n "${face_underline}" ]; then
printf 'set-face global OverstrikeUnderline "%s"\n' "${face_underline}"
fi
fi
}
define-command -hidden overstrike-parse-bold %{
eval -draft %{
try %{
exec '%1s(.\x08).<ret>d<a-m>'
update-option window overstrike_ranges
eval -itersel %{
set -add window overstrike_ranges "%val{selection_desc}|OverstrikeBold"
}
}
}
}
define-command overstrike-parse -docstring "Parse the contents of the current buffer and convert the overstrike escapes to ranges" %{
set window overstrike_ranges "%val{timestamp}"
eval -draft %{
try %{
exec '%1s(_\x08).<ret>d<a-m>'
update-option window overstrike_ranges
overstrike-parse-bold
eval -itersel %{
set -add window overstrike_ranges "%val{selection_desc}|OverstrikeUnderline"
}
} catch %{
overstrike-parse-bold
}
}
addhl ranges overstrike_ranges
}