mercury.kak (6712B)
1 # Detection 2 # ‾‾‾‾‾‾‾‾‾ 3 4 hook global BufCreate .*[.](m) %{ 5 set-option buffer filetype mercury 6 } 7 8 # Initialization 9 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 10 11 hook global WinSetOption filetype=mercury %{ 12 require-module mercury 13 14 set-option window static_words %opt{mercury_static_words} 15 16 hook window InsertChar \n -group mercury-insert mercury-insert-on-new-line 17 hook window InsertChar \n -group mercury-indent mercury-indent-on-new-line 18 # cleanup trailing whitespaces on current line insert end 19 hook window ModeChange pop:insert:.* -group mercury-trim-indent %{ try %{ execute-keys -draft <semicolon> x s ^\h+$ <ret> d } } 20 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window mercury-.+ } 21 } 22 23 hook -group mercury-highlight global WinSetOption filetype=mercury %{ 24 add-highlighter window/mercury ref mercury 25 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/mercury } 26 } 27 28 29 provide-module mercury %§ 30 31 32 # Highlighters 33 # ‾‾‾‾‾‾‾‾‾‾‾‾ 34 35 add-highlighter shared/mercury regions 36 add-highlighter shared/mercury/code default-region group 37 add-highlighter shared/mercury/comment region '%' '$' fill comment 38 add-highlighter shared/mercury/line_comment region '%' '$' fill comment 39 40 add-highlighter shared/mercury/string region '"' (?<!\\)(\\\\)*" fill string 41 # Integer formats 42 add-highlighter shared/mercury/code/ regex '\b0b[01]+\b' 0:value 43 add-highlighter shared/mercury/code/ regex '\b0x[\da-f]+\b' 0:value 44 add-highlighter shared/mercury/code/ regex '\b0o?[0-7]+\b' 0:value 45 add-highlighter shared/mercury/code/ regex '\b([1-9]\d*|0)(i8|i16|i32|i64|u|u8|u16|u32|u64)?\b' 0:value 46 # Float formats 47 add-highlighter shared/mercury/code/ regex '\b(\d+_\d+)*\d+[eE][+-]?\d+(\d+_\d+)*\b' 0:value 48 add-highlighter shared/mercury/code/ regex '(\b\d+)\.\d+\b' 0:value 49 add-highlighter shared/mercury/code/ regex '`[A-Za-z][A-Za-z_0-9]+`' 0:operator 50 51 add-highlighter shared/mercury/code/ regex \b[a-z][A-Za-z_0-9]*(?=\.\w) 0:module 52 add-highlighter shared/mercury/code/ regex \b[a-z][A-Za-z_0-9]*(?=__\w) 0:module 53 # func() and pred() 54 add-highlighter shared/mercury/code/ regex \b(pred|func)(?=\() 0:builtin 55 add-highlighter shared/mercury/code/ regex \b[A-Z][A-Za-z_0-9]*\b 0:variable 56 57 # operator symbols 58 add-highlighter shared/mercury/code/ regex (\.|!|!\.|!:|@|\^|:|\*\*|\\|\*|/|//|<<|>>|\+|\+\+|-|--|/\\|\\/|\.\.|:=|=\^|<|=|=\.\.|=:=|=<|==|=\\=|>|>=|@<|@=<|@>|@>=|\\=|\\==|~=|\\\+|~|<=|<=>|=>|,|&|->|\;|::|==>|--->|-->|:-|\?-) 0:operator 59 60 evaluate-commands %sh{ 61 # Grammar 62 values="true fail" 63 64 # There is overlap between all of these. Not sure what to do about that. 65 operators="event div mod rem for is and or impure semipure not when all arbitrary atomic disable_warning disable_warnings promise_equivalent_solutions promise_equivalent_solution_setspromise_exclusive promise_exclusive_exhaustive promise_exhaustive promise_impure promise_pure promise_semipure require_complete_switch require_switch_arms_det require_switch_arms_semidet require_switch_arms_multi require_switch_arms_nondet require_switch_arms_cc_multi require_switch_arms_cc_nondet require_switch_arms_erroneous require_switch_arms_failure require_det require_semidet require_multi require_nondet require_cc_multi require_cc_nondet require_erroneous require_failure trace try some or_else then if else where catch catch_any initialize finalize rule solver type pred func inst mode typeclass instance pragma promise initialise finalise mutable module import_module use_module include_module" 66 modes="free bound in out di mdi uo muo" 67 determinisms="erroneous failure det semidet multi cc_multi nondet cc_nondet" 68 reserved_insts="any bound bound_unique clobbered clobbered_any free ground is mostly_clobbered mostly_unique mostly_unique_any not_reached unique unique_any" 69 reserved_modes="any_func any_pred func is pred" 70 reserved_types="int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 float character string pred func pure semipure impure" 71 72 declarations="type solver type pred func inst mode typeclass instance pragma promise initialise finalise mutable module interface implementation import_module use_module include_module end_module" 73 74 join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } 75 76 # Add the language's grammar to the static completion list 77 printf %s\\n "declare-option str-list mercury_static_words $(join "${values} ${declarations} ${operators} ${determinisms} ${modes} ${reserved_insts} ${reserved_modes}" ' ')" 78 79 # Highlight keywords 80 printf %s " 81 add-highlighter shared/mercury/code/ regex '\b($(join "${values}" '|'))\b' 0:value 82 add-highlighter shared/mercury/code/ regex '^:-(\s+($(join "${declarations}" '|')))+\b' 1:keyword 83 add-highlighter shared/mercury/code/ regex '\b($(join "${operators}" '|'))\b' 0:operator 84 add-highlighter shared/mercury/code/ regex '\b($(join "${determinisms}" '|'))\b' 0:keyword 85 add-highlighter shared/mercury/code/ regex '\b($(join "${modes}" '|'))\b' 0:keyword 86 87 # These overlap with previous. Not sure what the solution is. 88 # add-highlighter shared/mercury/code/ regex '\b($(join "${reserved_types}" '|'))\b' 0:type 89 # add-highlighter shared/mercury/code/ regex '\b($(join "${reserved_insts}" '|'))\b' 0:builtin 90 # add-highlighter shared/mercury/code/ regex '\b($(join "${reserved_modes}" '|'))\b' 0:builtin 91 92 # Implementation-defined literals - don't know how to get these to work 93 # add-highlighter shared/mercury/code/ regex '\$(file|line|module|pred)' 0:keyword 94 " 95 } 96 97 define-command -hidden mercury-insert-on-new-line %{ 98 evaluate-commands -draft -itersel %{ 99 # copy '%' comment prefix and following white spaces 100 try %{ execute-keys -draft k x s ^\h*\%\h* <ret> y gh j P } 101 } 102 } 103 104 define-command -hidden mercury-indent-on-char %< 105 evaluate-commands -draft -itersel %< 106 # align closer token to its opener when alone on a line 107 try %/ execute-keys -draft <a-h> <a-k> ^\h+[]}]$ <ret> m s \A|.\z <ret> 1<a-&> / 108 > 109 > 110 111 define-command -hidden mercury-indent-on-new-line %< 112 evaluate-commands -draft -itersel %< 113 # preserve previous line indent 114 try %{ execute-keys -draft <semicolon> K <a-&> } 115 # cleanup trailing whitespaces from previous line 116 try %{ execute-keys -draft k x s \h+$ <ret> d } 117 # indent after line ending with :- 118 try %{ execute-keys -draft , k x <a-k> :-$ <ret> j <a-gt> } 119 # deindent closing brace/bracket when after cursor 120 try %< execute-keys -draft x <a-k> ^\h*[}\])] <ret> gh / [}\])] <ret> m <a-S> 1<a-&> > 121 > 122 > 123 124 §