tcl.kak (3325B)
1 hook global BufCreate .*[.](tcl) %{ 2 set-option buffer filetype tcl 3 } 4 5 hook global WinSetOption filetype=tcl %{ 6 require-module tcl 7 8 hook window ModeChange pop:insert:.* -group tcl-trim-indent tcl-trim-indent 9 hook window InsertChar \n -group tcl-insert tcl-insert-on-new-line 10 hook window InsertChar \n -group tcl-indent tcl-indent-on-new-line 11 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window tcl-.+ } 12 } 13 14 hook -group tcl-highlight global WinSetOption filetype=tcl %{ 15 add-highlighter window/tcl ref tcl 16 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/tcl } 17 } 18 19 # Using non-ascii characters here so that we can use the '[' command 20 provide-module tcl %§ 21 22 add-highlighter shared/tcl regions 23 add-highlighter shared/tcl/code default-region group 24 add-highlighter shared/tcl/comment region (?<!\\)(?:\\\\)*(?:^|\h)\K# '$' fill comment 25 add-highlighter shared/tcl/double_string region %{(?<!\\)(?:\\\\)*\K"} %{(?<!\\)(?:\\\\)*"} group 26 add-highlighter shared/tcl/double_string/fill fill string 27 28 evaluate-commands %sh{ 29 # Tcl does not have keywords, as everything in Tcl is a command. 30 # Highlight all builtin commads does not make a lot of sense as there are plenty of them. 31 # What is more, sometimes user defines varaibles have the same names as commands. 32 # On the other hand, highlight no commads makes the code harder to read. 33 # The approach for builtin commads highlighting is very simply. 34 # Highlight only two types of commands: 35 # 1. "Control statement" like commands, for exmaple, "if", "break", "return", etc. 36 # 2. Commands defining new scope, for exmaple, "proc", "namespace". 37 keywords="break catch continue default else elseif error exit for foreach if return switch while proc namespace" 38 39 join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } 40 41 # Highlight keywords 42 printf %s\\n "add-highlighter shared/tcl/code/ regex (?<!-)\b($(join "${keywords}" '|'))\b(?!-) 0:keyword" 43 } 44 45 add-highlighter shared/tcl/code/function regex ^\h*proc\h+((\w|-)+) 1:function 46 add-highlighter shared/tcl/code/brackets regex [\[\]]{1,2} 0:operator 47 add-highlighter shared/tcl/code/parameters regex \s-\w+\b 0:attribute 48 add-highlighter shared/tcl/code/variable regex \$(\w|:)+ 0:variable 49 add-highlighter shared/tcl/code/numbers regex '\b\d+\.?' 0:value 50 51 add-highlighter shared/tcl/double_string/variable regex \$(\w|:)+ 0:variable 52 add-highlighter shared/tcl/double_string/brackets regex [\[\]]{1,2} 0:operator 53 54 # Commands 55 # ‾‾‾‾‾‾‾‾ 56 57 define-command -hidden tcl-trim-indent %{ 58 # Remove trailing white spaces 59 try %{ execute-keys -draft -itersel x s \h+$ <ret> d } 60 } 61 62 define-command -hidden tcl-insert-on-new-line %[ 63 # Copy '#' comment prefix and following white spaces 64 try %{ execute-keys -draft k x s ^\h*\K#\h* <ret> y gh j P } 65 ] 66 67 define-command -hidden tcl-indent-on-new-line %¶ 68 evaluate-commands -draft -itersel %@ 69 # Preserve previous line indent 70 try %{ execute-keys -draft <semicolon> K <a-&> } 71 72 # Filter previous line 73 try %{ execute-keys -draft k : tcl-trim-indent <ret> } 74 75 # Indent after { 76 try %= execute-keys -draft , k x <a-k> (\s|^)\{$ <ret> j <a-gt> = 77 @ 78 ¶ 79 §