makefile.kak (2581B)
1 # Detection 2 # ‾‾‾‾‾‾‾‾‾ 3 4 hook global BufCreate .*(/?[mM]akefile|\.mk|\.make) %{ 5 set-option buffer filetype makefile 6 } 7 8 # Initialization 9 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 10 11 hook global WinSetOption filetype=makefile %{ 12 require-module makefile 13 14 set-option window static_words %opt{makefile_static_words} 15 16 hook window ModeChange pop:insert:.* -group makefile-trim-indent makefile-trim-indent 17 hook window InsertChar \n -group makefile-indent makefile-indent-on-new-line 18 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window makefile-.+ } 19 } 20 21 hook -group makefile-highlight global WinSetOption filetype=makefile %{ 22 add-highlighter window/makefile ref makefile 23 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/makefile } 24 } 25 26 provide-module makefile %{ 27 28 # Highlighters 29 # ‾‾‾‾‾‾‾‾‾‾‾‾ 30 31 add-highlighter shared/makefile regions 32 33 add-highlighter shared/makefile/content default-region group 34 add-highlighter shared/makefile/comment region (?<!\\)(?:\\\\)*(?:^|\h)\K# '$' fill comment 35 add-highlighter shared/makefile/evaluate-commands region -recurse \( (?<!\$)(?:\$\$)*\K\$\( \) fill value 36 37 add-highlighter shared/makefile/content/ regex ^\S.*?(::|:|!)\s 0:variable 38 add-highlighter shared/makefile/content/ regex [+?:]= 0:operator 39 40 evaluate-commands %sh{ 41 # Grammar 42 keywords="ifeq|ifneq|ifdef|ifndef|else|endif|define|endef" 43 44 # Add the language's grammar to the static completion list 45 printf %s\\n "declare-option str-list makefile_static_words ${keywords}" | tr '|' ' ' 46 47 # Highlight keywords 48 printf %s "add-highlighter shared/makefile/content/ regex \b(${keywords})\b 0:keyword" 49 } 50 51 # Commands 52 # ‾‾‾‾‾‾‾‾ 53 54 define-command -hidden makefile-trim-indent %{ 55 evaluate-commands -no-hooks -draft -itersel %{ 56 execute-keys x 57 # remove trailing white spaces 58 try %{ execute-keys -draft s \h + $ <ret> d } 59 } 60 } 61 62 define-command -hidden makefile-indent-on-new-line %{ 63 evaluate-commands -draft -itersel %{ 64 # preserve previous line indent 65 try %{ execute-keys -draft <semicolon>K<a-&> } 66 ## If the line above is a target indent with a tab 67 try %{ execute-keys -draft Z kx <a-k>^\S.*?(::|:|!)\s<ret> z i<tab> } 68 # cleanup trailing white space son previous line 69 try %{ execute-keys -draft kx s \h+$ <ret>d } 70 # indent after some keywords 71 try %{ execute-keys -draft Z kx <a-k> ^\h*(ifeq|ifneq|ifdef|ifndef|else|define)\b<ret> z <a-gt> } 72 } 73 } 74 75 }