1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-01 16:32:54 +03:00
kakoune/rc/filetype/ledger.kak

145 lines
5.8 KiB
Plaintext
Raw Normal View History

2019-07-01 14:00:00 +03:00
# Detection
# ---------
# The .ledger suffix is not required by ledger, but the best I can do.
hook global BufCreate .*\.ledger %{
set-option buffer filetype ledger
}
# Initialization
# --------------
hook global WinSetOption filetype=ledger %{
require-module ledger
hook window InsertChar \n -group ledger-indent ledger-indent-on-new-line
hook window ModeChange pop:insert:.* -group ledger-trim-indent ledger-trim-indent
2019-07-01 14:00:00 +03:00
hook -once -always window WinSetOption filetype=.* %{
remove-hooks window ledger-.+
unset-option window static_words # Remove static completion
}
}
hook -group ledger-highlight global WinSetOption filetype=ledger %{
add-highlighter window/ledger ref ledger
hook -once -always window WinSetOption filetype=.* %{
remove-highlighter window/ledger
}
}
# Completion
# ----------
hook -group ledger-complete global WinSetOption filetype=ledger %{
set-option window static_words account note alias payee check assert eval \
default apply fixed bucket capture comment commodity format nomarket \
define end include tag test year
}
provide-module ledger %[
# Highlighters
# ------------
#
# TODO: highlight tag comments
add-highlighter shared/ledger regions
# The following highlighters implement
# https://www.ledger-cli.org/3.0/doc/ledger3.html#Transactions-and-Comments
add-highlighter shared/ledger/transaction region '^[0-9]' '^(?=\H)' group
add-highlighter shared/ledger/transaction/first_line regex \
'^([0-9].*?)\h.*?(( +|\t+);.*?)?$' 1:function 2:string
add-highlighter shared/ledger/transaction/posting regex \
'^\h+([^\h;].*?)(( +|\t+).*?)?(( +|\t+);.*?)?$' 1:type 2:value 4:string
add-highlighter shared/ledger/transaction/note regex '^\h+;[^$]*?$' 0:string
add-highlighter shared/ledger/comment region '^(;|#|%|\||\*)' '$' fill comment
# TODO: Improve
add-highlighter shared/ledger/other region '^(P|=|~)' '$' fill meta
# The following highlighters implement
# https://www.ledger-cli.org/3.0/doc/ledger3.html#Command-Directives
rc ledger: don't use region highlighter for simple keywords As reported in https://github.com/mawww/kakoune/issues/4685#issuecomment-1200530001 ledger.kak defines a region end that matches every character of the buffer. This causes performance issues for large buffers. Since the affected regions are only ever filled with a single color, just use a regex highlighter instead of a region highlighter. This improves performance when loading the file for the first time. Speedup on [example.journal.txt](https://github.com/mawww/kakoune/issues/4685#issuecomment-1193243588) $ HOME=$PWD hyperfine -w 1 'git checkout HEAD'{~,}' -- :/rc/filetype/ledger.kak && ./kak.opt example.journal.txt -e "modeline-parse; hook global NormalIdle .* quit" -ui dummy' Benchmark 1: git checkout HEAD~ -- :/rc/filetype/ledger.kak && ./kak.opt example.journal.txt -e "modeline-parse; hook global NormalIdle .* quit" -ui dummy Time (mean ± σ): 362.1 ms ± 5.1 ms [User: 336.6 ms, System: 30.2 ms] Range (min … max): 352.6 ms … 369.1 ms 10 runs Benchmark 2: git checkout HEAD -- :/rc/filetype/ledger.kak && ./kak.opt example.journal.txt -e "modeline-parse; hook global NormalIdle .* quit" -ui dummy Time (mean ± σ): 271.2 ms ± 16.7 ms [User: 252.8 ms, System: 24.0 ms] Range (min … max): 253.9 ms … 305.0 ms 10 runs Summary 'git checkout HEAD -- :/rc/filetype/ledger.kak && ./kak.opt example.journal.txt -e "modeline-parse; hook global NormalIdle .* quit" -ui dummy' ran 1.33 ± 0.08 times faster than 'git checkout HEAD~ -- :/rc/filetype/ledger.kak && ./kak.opt example.journal.txt -e "modeline-parse; hook global NormalIdle .* quit" -ui dummy'
2022-08-16 10:58:36 +03:00
add-highlighter shared/ledger/default default-region group
2019-07-01 14:00:00 +03:00
# Add highlighters for simple one-line command directives
evaluate-commands %sh{
# TODO: Is `expr` also a command directive? The documentation confuses me.
for cmd in 'apply account' 'apply fixed' 'assert' 'bucket' 'check' 'end' \
'include' 'apply tag' 'test' 'year'; do
rc ledger: don't use region highlighter for simple keywords As reported in https://github.com/mawww/kakoune/issues/4685#issuecomment-1200530001 ledger.kak defines a region end that matches every character of the buffer. This causes performance issues for large buffers. Since the affected regions are only ever filled with a single color, just use a regex highlighter instead of a region highlighter. This improves performance when loading the file for the first time. Speedup on [example.journal.txt](https://github.com/mawww/kakoune/issues/4685#issuecomment-1193243588) $ HOME=$PWD hyperfine -w 1 'git checkout HEAD'{~,}' -- :/rc/filetype/ledger.kak && ./kak.opt example.journal.txt -e "modeline-parse; hook global NormalIdle .* quit" -ui dummy' Benchmark 1: git checkout HEAD~ -- :/rc/filetype/ledger.kak && ./kak.opt example.journal.txt -e "modeline-parse; hook global NormalIdle .* quit" -ui dummy Time (mean ± σ): 362.1 ms ± 5.1 ms [User: 336.6 ms, System: 30.2 ms] Range (min … max): 352.6 ms … 369.1 ms 10 runs Benchmark 2: git checkout HEAD -- :/rc/filetype/ledger.kak && ./kak.opt example.journal.txt -e "modeline-parse; hook global NormalIdle .* quit" -ui dummy Time (mean ± σ): 271.2 ms ± 16.7 ms [User: 252.8 ms, System: 24.0 ms] Range (min … max): 253.9 ms … 305.0 ms 10 runs Summary 'git checkout HEAD -- :/rc/filetype/ledger.kak && ./kak.opt example.journal.txt -e "modeline-parse; hook global NormalIdle .* quit" -ui dummy' ran 1.33 ± 0.08 times faster than 'git checkout HEAD~ -- :/rc/filetype/ledger.kak && ./kak.opt example.journal.txt -e "modeline-parse; hook global NormalIdle .* quit" -ui dummy'
2022-08-16 10:58:36 +03:00
echo "add-highlighter shared/ledger/default/ regex '^${cmd}\b' 0:function"
2019-07-01 14:00:00 +03:00
done
}
add-highlighter shared/ledger/account region '^account' '^(?=\H)' group
add-highlighter shared/ledger/account/first_line regex '^account' 0:function
add-highlighter shared/ledger/account/note regex '^\h*note' 0:function
add-highlighter shared/ledger/account/alias regex '^\h*alias' 0:function
add-highlighter shared/ledger/account/payee regex '^\h*payee' 0:function
add-highlighter shared/ledger/account/check regex '^\h*check' 0:function
add-highlighter shared/ledger/account/assert regex '^\h*assert' 0:function
add-highlighter shared/ledger/account/eval regex '^\h*eval' 0:function
add-highlighter shared/ledger/account/default regex '^\h*default' 0:function
add-highlighter shared/ledger/alias region '^alias' '$' group
add-highlighter shared/ledger/alias/keyword regex '^alias' 0:function
add-highlighter shared/ledger/alias/key regex '^alias\h([^$=]*)=?' 1:variable
add-highlighter shared/ledger/alias/value regex '^alias\h.*?=(.*?)$' 1:value
add-highlighter shared/ledger/capture region '^capture' '$' group
add-highlighter shared/ledger/capture/keyword regex '^capture' 0:function
add-highlighter shared/ledger/capture/account regex \
'^capture\h+(.*?)( +|\t+|$)' 1:type
add-highlighter shared/ledger/capture/regex regex \
'^capture\h+.*?( +|\t+)(.*?)$' 2:value
add-highlighter shared/ledger/comment_block region '^comment' '^end comment' \
fill comment
add-highlighter shared/ledger/commodity region '^commodity' '^(?=\H)' group
add-highlighter shared/ledger/commodity/first_line regex '^commodity' 0:function
add-highlighter shared/ledger/commodity/note regex '^\h*note' 0:function
add-highlighter shared/ledger/commodity/format regex '^\h*format' 0:function
add-highlighter shared/ledger/commodity/nomarket regex '^\h*nomarket' 0:function
add-highlighter shared/ledger/commodity/alias regex '^\h*alias' 0:function
add-highlighter shared/ledger/commodity/default regex '^\h*default' 0:function
add-highlighter shared/ledger/define region '^define' '$' group
add-highlighter shared/ledger/define/keyword regex '^define' 0:function
add-highlighter shared/ledger/define/key regex '^define\h([^$=]*)=?' 1:variable
add-highlighter shared/ledger/define/value regex '^define\h.*?=(.*?)$' 1:value
add-highlighter shared/ledger/payee region '^payee' '^(?=\H)' group
add-highlighter shared/ledger/payee/first_line regex '^payee' 0:function
add-highlighter shared/ledger/payee/alias regex '^\h*alias' 0:function
add-highlighter shared/ledger/payee/uuid regex '^\h*uuid' 0:function
add-highlighter shared/ledger/tag region '^tag' '^(?=\H)' group
add-highlighter shared/ledger/tag/first_line regex '^tag' 0:function
add-highlighter shared/ledger/tag/check regex '^\h*check' 0:function
add-highlighter shared/ledger/tag/assert regex '^\h*assert' 0:function
# Commands
# --------
define-command -hidden ledger-indent-on-new-line %[
evaluate-commands -draft -itersel %[
# preserve previous line indent
try %[ execute-keys -draft <semicolon> K <a-&> ]
2019-07-01 14:00:00 +03:00
# cleanup trailing whitespaces from previous line
try %[ execute-keys -draft k x s \h+$ <ret> d ]
# indent after the first line of a transaction
try %[ execute-keys -draft kx <a-k>^[0-9]<ret> j<a-gt> ]
2019-07-01 14:00:00 +03:00
]
]
define-command -hidden ledger-trim-indent %{
try %{ execute-keys -draft <semicolon> x s ^\h+$ <ret> d }
2019-07-01 14:00:00 +03:00
}
]