kak-highlight/rc/highlight.kak
Lucas Schwiderski 718a7fde41
feat: Improve performances
- create a map of byte offsets to Kakoune ranges once, to avoid having to
iterate the content for every highlight event
- create a direct lookup for faces from the token index passed by the
highlighter event, rather than performing a string map lookup each time
- minimize allocations by pushing only a single string with sufficient
capacity, rather than building small strings
2023-04-18 10:35:14 +02:00

68 lines
2.1 KiB
Text

declare-option str kak_highlight_cmd "kak-highlight"
declare-option str kak_highlight_log "/tmp/kak-highlight.log"
declare-option -hidden range-specs kak_highlight_ranges
# Option to store draft of the current buffer before passing to shell.
declare-option -hidden str kak_highlight_draft
define-command kak-highlight -docstring %{
kak-highlight
Request highlighting for the current buffer
} %{
evaluate-commands -draft -no-hooks %{exec '%'; set buffer kak_highlight_draft %val{selection}}
evaluate-commands %sh{
kak_highlight_draft=$(printf '%s.' "${kak_opt_kak_highlight_draft}" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed "s/$(printf '\t')/\\\\t/g")
kak_highlight_draft=${kak_highlight_draft%.}
printf '
timestamp = %d
session = "%s"
client = "%s"
content = """
%s"""
' "${kak_timestamp}" "${kak_session}" "${kak_client}" "${kak_highlight_draft}" | ${kak_opt_kak_highlight_cmd} request
}
}
define-command kak-highlight-enable-window -docstring %{
kak-highlight-enable-window
Enable the highlighter for this window
} %{
add-highlighter window/kak_highlight_ranges ranges kak_highlight_ranges
hook window -group kak-highlight NormalIdle .* kak-highlight
hook window -group kak-highlight InsertIdle .* kak-highlight
hook window -group kak-highlight BufReload .* kak-highlight
kak-highlight
}
define-command kak-highlight-disable-window -docstring %{
kak-highlight-disable-window
Disable the highlighter for this window
} %{
remove-hooks window kak-highlight
remove-highlighter window/kak_highlight_ranges
}
define-command kak-highlight-start -docstring %{
kak-highlight-start
Start the daemon
} %{
nop %sh{
(eval "${kak_opt_kak_highlight_cmd} --log '${kak_opt_kak_highlight_log}' daemon '${kak_session}'") >/dev/null 2>&1 </dev/null &
}
}
define-command kak-highlight-stop -docstring %{
kak-highlight-stop
Stop the daemon
} %{
nop %sh{
pkill -f "${kak_opt_kak_highlight_cmd} .* daemon ${kak_session}"
}
}
hook global KakBegin .* kak-highlight-start
hook global KakEnd .* kak-highlight-stop