capnp.kak (3554B)
1 # https://capnproto.org/ 2 3 hook global BufCreate .*[.](capnp) %{ 4 set-option buffer filetype capnp 5 } 6 7 # Initialization 8 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 9 10 hook global WinSetOption filetype=capnp %{ 11 require-module capnp 12 13 set-option window static_words %opt{capnp_static_words} 14 15 hook window ModeChange pop:insert:.* -group capnp-trim-indent capnp-trim-indent 16 hook window InsertChar .* -group capnp-indent capnp-indent-on-char 17 hook window InsertChar \n -group capnp-indent capnp-indent-on-new-line 18 19 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window capnp-.+ } 20 } 21 22 hook -group capnp-highlight global WinSetOption filetype=capnp %{ 23 add-highlighter window/capnp ref capnp 24 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/capnp } 25 } 26 27 provide-module capnp %@ 28 29 # Highlighters 30 # ‾‾‾‾‾‾‾‾‾‾‾‾ 31 32 add-highlighter shared/capnp regions 33 add-highlighter shared/capnp/code default-region group 34 35 add-highlighter shared/capnp/line_comment region '#' '$' fill comment 36 add-highlighter shared/capnp/string region '"' (?<!\\)(\\\\)*" fill string 37 38 add-highlighter shared/capnp/code/ regex '(?i)\b0b[01]+l?\b' 0:value 39 add-highlighter shared/capnp/code/ regex '(?i)\b0x[\da-f]+l?\b' 0:value 40 add-highlighter shared/capnp/code/ regex '(?i)\b0o?[0-7]+l?\b' 0:value 41 add-highlighter shared/capnp/code/ regex '(?i)\b([1-9]\d*|0)l?\b' 0:value 42 add-highlighter shared/capnp/code/ regex '\b\d+[eE][+-]?\d+\b' 0:value 43 add-highlighter shared/capnp/code/ regex '(\b\d+)?\.\d+\b' 0:value 44 add-highlighter shared/capnp/code/ regex '\b\d+\.' 0:value 45 46 evaluate-commands %sh{ 47 builtin_types="Void Bool Text Data List union group Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64 Float32 Float64" 48 declarations="struct union enum interface const annotation" 49 keywords="using extends import" 50 values="true false inf" 51 52 join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } 53 54 printf %s\\n "declare-option str-list capnp_static_words $(join "${builtin_types}" ' ') $(join "${declarations}" ' ') $(join "${keywords}" ' ') $(join "${values}" ' ')" 55 56 printf %s\\n "add-highlighter shared/capnp/code/ regex '\b($(join "${builtin_types}" '|'))\b' 0:type" 57 printf %s\\n "add-highlighter shared/capnp/code/ regex '\b($(join "${declarations}" '|'))\b' 0:keyword" 58 printf %s\\n "add-highlighter shared/capnp/code/ regex '\b($(join "${keywords}" '|'))\b' 0:keyword" 59 printf %s\\n "add-highlighter shared/capnp/code/ regex '\b($(join "${values}" '|'))\b' 0:value" 60 } 61 62 # Commands 63 # ‾‾‾‾‾‾‾‾ 64 65 define-command -hidden capnp-trim-indent %{ 66 # remove trailing white spaces 67 try %{ execute-keys -draft -itersel x s \h+$ <ret> d } 68 } 69 70 define-command -hidden capnp-indent-on-char %< 71 evaluate-commands -draft -itersel %< 72 # align closer token to its opener when alone on a line 73 try %< execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m <a-S> 1<a-&> > 74 > 75 > 76 77 define-command -hidden capnp-indent-on-new-line %< 78 evaluate-commands -draft -itersel %< 79 # preserve previous line indent 80 try %{ execute-keys -draft <semicolon> K <a-&> } 81 # filter previous line 82 try %{ execute-keys -draft k : capnp-trim-indent <ret> } 83 # indent after lines ending with opener token 84 try %< execute-keys -draft k x <a-k> [[{]\h*$ <ret> j <a-gt> > 85 # deindent closer token(s) when after cursor 86 try %< execute-keys -draft x <a-k> ^\h*[}\]] <ret> gh / [}\]] <ret> m <a-S> 1<a-&> > 87 > 88 > 89 90 @