kdl.kak (7194B)
1 # https://kdl.dev 2 # This supports both kdl v1 and v2. 3 4 hook global BufCreate .*\.kdl %{ 5 set-option buffer filetype kdl 6 } 7 8 # Initialization 9 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 10 11 hook global WinSetOption filetype=kdl %{ 12 require-module kdl 13 14 hook window ModeChange pop:insert:.* -group kdl-trim-indent kdl-trim-indent 15 hook window InsertChar .* -group kdl-indent kdl-indent-on-char 16 hook window InsertChar \n -group kdl-indent kdl-indent-on-new-line 17 18 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window kdl-.+ } 19 } 20 21 hook -group kdl-highlight global WinSetOption filetype=kdl %{ 22 add-highlighter window/kdl ref kdl 23 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/kdl } 24 } 25 26 provide-module kdl %@ 27 28 # Highlighters 29 # ‾‾‾‾‾‾‾‾‾‾‾‾ 30 31 add-highlighter shared/kdl regions 32 add-highlighter shared/kdl/code default-region group 33 34 # Slashdash (/-) comments are annoying to highlight properly without a proper parser due to the fact that they can comment 35 # out any kdl construct. 36 # The below is an approximation, and there are almost certainly edge cases missed. 37 add-highlighter shared/kdl/slashdash_entire_node_with_child region -recurse \{ ^[\s]*/-\N+\{ \} fill comment 38 add-highlighter shared/kdl/slashdash_child region -recurse \{ /-\s*\{ \} fill comment 39 add-highlighter shared/kdl/slashdash_node region ^[\s]*/-\N+ $ fill comment 40 add-highlighter shared/kdl/slashdash_string region '/-\s*"' (?<!\\)(\\\\)*" fill comment 41 add-highlighter shared/kdl/slashdash_triple_string region '/-\s*"""' '(?<!\\)(?:\\\\)*"""' fill string 42 add-highlighter shared/kdl/slashdash_raw_string region -match-capture '/-\s*r"(#+)' '"(#+)' fill comment 43 add-highlighter shared/kdl/slashdash_raw_string2 region -match-capture '/-\s*"(#+)' '"(#+)' fill comment 44 add-highlighter shared/kdl/slashdash_builtin_value region /-\s+(true|false|null) \s fill comment 45 add-highlighter shared/kdl/slashdash_binary region /-\s*0b[01_]+ \s fill comment 46 add-highlighter shared/kdl/slashdash_octal region /-\s*0o[0-7_]+ \s fill comment 47 add-highlighter shared/kdl/slashdash_hex region /-\s*0x[a-fA-F0-9_]+ \s fill comment 48 add-highlighter shared/kdl/slashdash_decimal region /-\s*[0-9-+][0-9_]* \s fill comment 49 add-highlighter shared/kdl/slashdash_float region /-\s*[0-9-+][0-9_]*\.[0-9_]+ \s fill comment 50 add-highlighter shared/kdl/slashdash_float_exp region /-\s*[0-9-+][0-9_]*(\.[0-9_]+)?[eE][-+]?[0-9_]+ \s fill comment 51 add-highlighter shared/kdl/slashdash_prop_string region '/-\s*[\u000021-\u00FFFF]+="' (?<!\\)(\\\\)*" fill comment 52 add-highlighter shared/kdl/slashdash_prop_triple_string region '/-\s*[\u000021-\u00FFFF]+="""' '(?<!\\)(\\\\)*"""' fill comment 53 add-highlighter shared/kdl/slashdash_prop_raw_string region -match-capture '/-\s*[\u000021-\u00FFFF]+=r"(#*)' '"(#*)' fill comment 54 add-highlighter shared/kdl/slashdash_prop_raw_string2 region -match-capture '/-\s*[\u000021-\u00FFFF]+="(#*)' '"(#*)' fill comment 55 add-highlighter shared/kdl/slashdash_prop_other region /-\s*[\u000021-\u00FFFF]+= \s fill comment 56 add-highlighter shared/kdl/slashdash_arg region /-\s*[\u000021-\u00FFFF]+ \s fill comment 57 58 add-highlighter shared/kdl/raw_string region -match-capture 'r(#+)"' '"(#+)' fill string 59 add-highlighter shared/kdl/raw_string2 region -match-capture '(#+)"' '"(#+)' fill string 60 61 add-highlighter shared/kdl/string region '"' (?<!\\)(\\\\)*" fill string 62 add-highlighter shared/kdl/triple_string region '"""' '(?<!\\)(?:\\\\)*"""' fill string 63 64 add-highlighter shared/kdl/comment region -recurse /\* /\* \*/ fill comment 65 add-highlighter shared/kdl/line_comment region // $ fill comment 66 67 68 add-highlighter shared/kdl/code/node regex \b([\u000021-\u00FFFF]*)\b 0:variable # Everything not covered below is a node 69 add-highlighter shared/kdl/code/property regex \b([\u000021-\u00FFFF]+)(=) 0:operator 1:attribute 70 add-highlighter shared/kdl/code/builtin_value regex [^\w](#true|#false|#null|#inf|#-inf|#nan|true|false|null)\b 0:value 71 add-highlighter shared/kdl/code/binary regex \b(0b[01_]+)\b 0:value 72 add-highlighter shared/kdl/code/octal regex \b(0o[0-7_]+)\b 0:value 73 add-highlighter shared/kdl/code/hex regex \b(0x[a-fA-F0-9_]+)\b 0:value 74 add-highlighter shared/kdl/code/decimal regex \b([0-9-+][0-9_]*)\b 0:value 75 add-highlighter shared/kdl/code/float regex \b([0-9-+][0-9_]*\.[0-9_]+)\b 0:value 76 add-highlighter shared/kdl/code/float_exp regex \b([0-9-+][0-9_]*(\.[0-9_]+)?[eE][-+]?[0-9_]+)\b 0:value 77 78 79 # Commands 80 # ‾‾‾‾‾‾‾‾ 81 82 define-command -hidden kdl-trim-indent %{ 83 # remove trailing white spaces 84 try %{ execute-keys -draft -itersel x s \h+$ <ret> d } 85 } 86 87 define-command -hidden kdl-indent-on-char %< 88 evaluate-commands -draft -itersel %< 89 # align closer token to its opener when alone on a line 90 try %< execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m <a-S> 1<a-&> > 91 > 92 > 93 94 define-command -hidden kdl-indent-on-new-line %{ 95 evaluate-commands -itersel -draft %{ 96 execute-keys <semicolon> 97 try %{ 98 evaluate-commands -draft -save-regs '/"' %{ 99 # copy the commenting prefix 100 execute-keys -save-regs '' k x1s^(\h*//+\h*)<ret> y 101 try %{ 102 # if the previous comment isn't empty, create a new one 103 execute-keys x<a-K>^\h*//+\h*$<ret> jxs^\h*<ret>P 104 } catch %{ 105 # if there is no text in the previous comment, remove it completely 106 execute-keys d 107 } 108 } 109 110 # trim trailing whitespace on the previous line 111 try %{ execute-keys -draft k x s\h+$<ret> d } 112 } 113 114 try %{ 115 # if the previous line isn't within a comment scope, break 116 execute-keys -draft kx <a-k>^(\h*/\*|\h+\*(?!/))<ret> 117 118 # find comment opening, validate it was not closed, and check its using star prefixes 119 execute-keys -draft <a-?>/\*<ret><a-H> <a-K>\*/<ret> <a-k>\A\h*/\*(\N*\n\h*\*)*\N*\n\h*.\z<ret> 120 121 try %{ 122 # if the previous line is opening the comment, insert star preceeded by space 123 execute-keys -draft kx<a-k>^\h*/\*<ret> 124 execute-keys -draft i*<space><esc> 125 } catch %{ 126 try %{ 127 # if the next line is a comment line insert a star 128 execute-keys -draft jx<a-k>^\h+\*<ret> 129 execute-keys -draft i*<space><esc> 130 } catch %{ 131 try %{ 132 # if the previous line is an empty comment line, close the comment scope 133 execute-keys -draft kx<a-k>^\h+\*\h+$<ret> x1s\*(\h*)<ret>c/<esc> 134 } catch %{ 135 # if the previous line is a non-empty comment line, add a star 136 execute-keys -draft i*<space><esc> 137 } 138 } 139 } 140 141 # trim trailing whitespace on the previous line 142 try %{ execute-keys -draft k x s\h+$<ret> d } 143 # align the new star with the previous one 144 execute-keys Kx1s^[^*]*(\*)<ret>& 145 } 146 } 147 } 148 149 @