ledger.kak (5969B)
1 # Detection 2 # --------- 3 4 # The .ledger suffix is not required by ledger, but the best I can do. 5 hook global BufCreate .*\.ledger %{ 6 set-option buffer filetype ledger 7 } 8 9 # Initialization 10 # -------------- 11 12 hook global WinSetOption filetype=ledger %{ 13 require-module ledger 14 15 hook window InsertChar \n -group ledger-indent ledger-indent-on-new-line 16 hook window ModeChange pop:insert:.* -group ledger-trim-indent ledger-trim-indent 17 18 hook -once -always window WinSetOption filetype=.* %{ 19 remove-hooks window ledger-.+ 20 unset-option window static_words # Remove static completion 21 } 22 } 23 24 hook -group ledger-highlight global WinSetOption filetype=ledger %{ 25 add-highlighter window/ledger ref ledger 26 hook -once -always window WinSetOption filetype=.* %{ 27 remove-highlighter window/ledger 28 } 29 } 30 31 # Completion 32 # ---------- 33 34 hook -group ledger-complete global WinSetOption filetype=ledger %{ 35 set-option window static_words account note alias payee check assert eval \ 36 default apply fixed bucket capture comment commodity format nomarket \ 37 define end include tag test year 38 } 39 40 provide-module ledger %[ 41 42 # Highlighters 43 # ------------ 44 # 45 # TODO: highlight tag comments 46 47 add-highlighter shared/ledger regions 48 49 # The following highlighters implement 50 # https://www.ledger-cli.org/3.0/doc/ledger3.html#Transactions-and-Comments 51 52 add-highlighter shared/ledger/transaction region '^[0-9]' '^(?=\H)' group 53 add-highlighter shared/ledger/transaction/first_line regex \ 54 '^([0-9].*?)\h.*?(( +|\t+);.*?)?$' 1:function 2:string 55 add-highlighter shared/ledger/transaction/posting regex \ 56 '^\h+([^\h;].*?)(( +|\t+).*?)?(( +|\t+);.*?)?$' 1:type 2:value 4:string 57 add-highlighter shared/ledger/transaction/note regex '^\h+;[^$]*?$' 0:string 58 59 add-highlighter shared/ledger/comment region '^(;|#|%|\||\*)' '$' fill comment 60 61 # TODO: Improve 62 add-highlighter shared/ledger/other region '^(P|=|~)' '$' fill meta 63 64 # The following highlighters implement 65 # https://www.ledger-cli.org/3.0/doc/ledger3.html#Command-Directives 66 67 add-highlighter shared/ledger/default default-region group 68 69 # Add highlighters for simple one-line command directives 70 evaluate-commands %sh{ 71 # TODO: Is `expr` also a command directive? The documentation confuses me. 72 for cmd in 'apply account' 'apply fixed' 'assert' 'bucket' 'check' 'end' \ 73 'include' 'apply tag' 'test' 'year'; do 74 echo "add-highlighter shared/ledger/default/ regex '^${cmd}\b' 0:function" 75 done 76 } 77 78 add-highlighter shared/ledger/account region '^account' '^(?=\H)' group 79 add-highlighter shared/ledger/account/first_line regex '^account' 0:function 80 add-highlighter shared/ledger/account/note regex '^\h*note' 0:function 81 add-highlighter shared/ledger/account/alias regex '^\h*alias' 0:function 82 add-highlighter shared/ledger/account/payee regex '^\h*payee' 0:function 83 add-highlighter shared/ledger/account/check regex '^\h*check' 0:function 84 add-highlighter shared/ledger/account/assert regex '^\h*assert' 0:function 85 add-highlighter shared/ledger/account/eval regex '^\h*eval' 0:function 86 add-highlighter shared/ledger/account/default regex '^\h*default' 0:function 87 88 add-highlighter shared/ledger/alias region '^alias' '$' group 89 add-highlighter shared/ledger/alias/keyword regex '^alias' 0:function 90 add-highlighter shared/ledger/alias/key regex '^alias\h([^$=]*)=?' 1:variable 91 add-highlighter shared/ledger/alias/value regex '^alias\h.*?=(.*?)$' 1:value 92 93 add-highlighter shared/ledger/capture region '^capture' '$' group 94 add-highlighter shared/ledger/capture/keyword regex '^capture' 0:function 95 add-highlighter shared/ledger/capture/account regex \ 96 '^capture\h+(.*?)( +|\t+|$)' 1:type 97 add-highlighter shared/ledger/capture/regex regex \ 98 '^capture\h+.*?( +|\t+)(.*?)$' 2:value 99 100 add-highlighter shared/ledger/comment_block region '^comment' '^end comment' \ 101 fill comment 102 103 add-highlighter shared/ledger/commodity region '^commodity' '^(?=\H)' group 104 add-highlighter shared/ledger/commodity/first_line regex '^commodity' 0:function 105 add-highlighter shared/ledger/commodity/note regex '^\h*note' 0:function 106 add-highlighter shared/ledger/commodity/format regex '^\h*format' 0:function 107 add-highlighter shared/ledger/commodity/nomarket regex '^\h*nomarket' 0:function 108 add-highlighter shared/ledger/commodity/alias regex '^\h*alias' 0:function 109 add-highlighter shared/ledger/commodity/default regex '^\h*default' 0:function 110 111 add-highlighter shared/ledger/define region '^define' '$' group 112 add-highlighter shared/ledger/define/keyword regex '^define' 0:function 113 add-highlighter shared/ledger/define/key regex '^define\h([^$=]*)=?' 1:variable 114 add-highlighter shared/ledger/define/value regex '^define\h.*?=(.*?)$' 1:value 115 116 add-highlighter shared/ledger/payee region '^payee' '^(?=\H)' group 117 add-highlighter shared/ledger/payee/first_line regex '^payee' 0:function 118 add-highlighter shared/ledger/payee/alias regex '^\h*alias' 0:function 119 add-highlighter shared/ledger/payee/uuid regex '^\h*uuid' 0:function 120 121 add-highlighter shared/ledger/tag region '^tag' '^(?=\H)' group 122 add-highlighter shared/ledger/tag/first_line regex '^tag' 0:function 123 add-highlighter shared/ledger/tag/check regex '^\h*check' 0:function 124 add-highlighter shared/ledger/tag/assert regex '^\h*assert' 0:function 125 126 # Commands 127 # -------- 128 129 define-command -hidden ledger-indent-on-new-line %[ 130 evaluate-commands -draft -itersel %[ 131 # preserve previous line indent 132 try %[ execute-keys -draft <semicolon> K <a-&> ] 133 # cleanup trailing whitespaces from previous line 134 try %[ execute-keys -draft k x s \h+$ <ret> d ] 135 # indent after the first line of a transaction 136 try %[ execute-keys -draft kx <a-k>^[0-9]<ret> j<a-gt> ] 137 ] 138 ] 139 140 define-command -hidden ledger-trim-indent %{ 141 try %{ execute-keys -draft <semicolon> x s ^\h+$ <ret> d } 142 } 143 144 ]