ninja.kak (3901B)
1 # ref: https://ninja-build.org/manual.html#ref_ninja_file 2 3 # Detection 4 # ‾‾‾‾‾‾‾‾‾ 5 6 hook global BufCreate .+\.ninja %{ 7 set-option buffer filetype ninja 8 } 9 10 11 # Initialization 12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 13 14 hook global WinSetOption filetype=ninja %{ 15 require-module ninja 16 17 set-option window static_words %opt{ninja_static_words} 18 19 hook window ModeChange pop:insert:.* -group ninja-trim-indent ninja-trim-indent 20 hook window InsertChar \n -group ninja-insert ninja-insert-on-new-line 21 hook window InsertChar \n -group ninja-indent ninja-indent-on-new-line 22 # cleanup trailing whitespaces on current line insert end 23 hook window ModeChange pop:insert:.* -group ninja-trim-indent %{ try %{ execute-keys -draft <semicolon> x s ^\h+$ <ret> d } } 24 25 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window ninja-.+ } 26 } 27 28 hook -group ninja-highlight global WinSetOption filetype=ninja %{ 29 add-highlighter window/ninja ref ninja 30 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/ninja } 31 } 32 33 34 provide-module ninja %{ 35 36 # Highlighters 37 # ‾‾‾‾‾‾‾‾‾‾‾‾ 38 39 add-highlighter shared/ninja regions 40 41 # `#` 42 add-highlighter shared/ninja/comment region '#' '\n' fill comment 43 44 # `subninja`, `include`, `rule`, `pool` and `default` declarations 45 add-highlighter shared/ninja/sirpd region '^(subninja|include|rule|pool|default)\b' '(?<!\$)\n' group 46 add-highlighter shared/ninja/sirpd/default regex '^(subninja|include)\b' 0:module 47 add-highlighter shared/ninja/sirpd/rulepool regex '^(rule|pool|default)\b' 0:keyword 48 add-highlighter shared/ninja/sirpd/linebreak regex '\$$' 0:operator 49 50 # `build` 51 add-highlighter shared/ninja/build region '^build\b' '(?<!\$)\n' group 52 add-highlighter shared/ninja/build/build regex '^build\b' 0:keyword 53 add-highlighter shared/ninja/build/rule regex ':\h+((\w|-)+)' 0:function 54 add-highlighter shared/ninja/build/colonpipe regex ':|\||\|\|' 0:operator 55 add-highlighter shared/ninja/build/linebreak regex '\$$' 0:operator 56 add-highlighter shared/ninja/build/variables regex '\$(\w|-)+|\$\{(\w|-)+\}' 0:value 57 58 # variables declarations 59 add-highlighter shared/ninja/variable region '^\h*(\w|-)+\h*=' '(?<!\$)\n' group 60 add-highlighter shared/ninja/variable/declaredname regex '^\h*((\w|-)+)\h*(=)' 1:variable 0:operator 61 add-highlighter shared/ninja/variable/linebreak regex '\$$' 0:operator 62 add-highlighter shared/ninja/variable/variables regex '\$(\w|-)+|\$\{(\w|-)+\}' 0:value 63 64 # keywords/builtin variable names 65 evaluate-commands %sh{ 66 keywords="rule build command default subninja include" 67 reserved_names="builddir ninja_required_version pool depfile deps depfile msvc_deps_prefix description dyndep generator restat rspfile rspfile_content" 68 69 printf %s " 70 declare-option str-list ninja_static_words ${keywords} ${reserved_names} 71 " 72 73 reserved_names_regex="$(echo ${reserved_names} | tr ' ' '|')" 74 printf %s " 75 add-highlighter shared/ninja/variable/reserved_names regex '^\h*(${reserved_names_regex})\b' 0:meta 76 " 77 } 78 79 80 # Indent 81 # ‾‾‾‾‾‾ 82 83 define-command -hidden ninja-trim-indent %{ 84 # remove trailing white spaces 85 try %{ execute-keys -draft -itersel x s \h+$ <ret> d } 86 } 87 88 define-command -hidden ninja-insert-on-new-line %{ 89 evaluate-commands -draft -itersel %{ 90 # copy -- comments prefix and following white spaces 91 try %{ execute-keys -draft k x s ^\h*\K--\h* <ret> y gh j P } 92 } 93 } 94 95 define-command -hidden ninja-indent-on-new-line %{ 96 evaluate-commands -draft -itersel %{ 97 # preserve previous line indent 98 try %{ execute-keys -draft \; K <a-&> } 99 # filter previous line 100 try %{ execute-keys -draft k : ninja-trim-indent <ret> } 101 # indent after lines begining with rule and pool 102 try %{ execute-keys -draft \; k x <a-k> ^(rule|pool|build)\b <ret> j <a-gt> } 103 } 104 } 105 106 }