kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

toml.kak (2642B)


      1 # https://github.com/toml-lang/toml/tree/v0.4.0
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*\.(toml) %{
      8     set-option buffer filetype toml
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=toml %{
     15     require-module toml
     16 
     17     hook window ModeChange pop:insert:.* -group toml-trim-indent toml-trim-indent
     18     hook window InsertChar \n -group toml-insert toml-insert-on-new-line
     19     hook window InsertChar \n -group toml-indent toml-indent-on-new-line
     20 
     21     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window toml-.+ }
     22 }
     23 
     24 hook -group toml-highlight global WinSetOption filetype=toml %{
     25     add-highlighter window/toml ref toml
     26     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/toml }
     27 }
     28 
     29 
     30 provide-module toml %{
     31 
     32 # Highlighters
     33 # ‾‾‾‾‾‾‾‾‾‾‾‾
     34 
     35 add-highlighter shared/toml regions
     36 add-highlighter shared/toml/code default-region group
     37 add-highlighter shared/toml/comment region '#'   $           fill comment
     38 add-highlighter shared/toml/string1 region  '"""' (?<!\\)(\\\\)*"""(?!") fill string
     39 add-highlighter shared/toml/string2 region  "'''" "'''(?!')"             fill string
     40 add-highlighter shared/toml/string3 region  '"'   (?<!\\)(\\\\)*"        fill string
     41 add-highlighter shared/toml/string4 region  "'"   "'"                    fill string
     42 
     43 add-highlighter shared/toml/code/ regex \
     44     "^\h*\[\[?([A-Za-z0-9._-]*)\]\]?" 1:title
     45 add-highlighter shared/toml/code/ regex \
     46     (?<!\w)[+-]?[0-9](_?\d)*(\.[0-9](_?\d)*)?([eE][+-]?[0-9](_?\d)*)?\b 0:value
     47 add-highlighter shared/toml/code/ regex \
     48     true|false 0:value
     49 add-highlighter shared/toml/code/ regex \
     50     '\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}:\d{2}(.\d+)?([Zz]|[+-]\d{2}:\d{2})' 0:value
     51 
     52 # Commands
     53 # ‾‾‾‾‾‾‾‾
     54 
     55 define-command -hidden toml-trim-indent %{
     56     # remove trailing white spaces
     57     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
     58 }
     59 
     60 define-command -hidden toml-insert-on-new-line %{
     61     evaluate-commands -draft -itersel %{
     62         # copy # comments prefix and following white spaces
     63         try %{ execute-keys -draft k x s ^\h*\K#\h* <ret> y gh j P }
     64     }
     65 }
     66 
     67 define-command -hidden toml-indent-on-new-line %{
     68     evaluate-commands -draft -itersel %{
     69         # preserve previous line indent
     70         try %{ execute-keys -draft <semicolon> K <a-&> }
     71         # filter previous line
     72         try %{ execute-keys -draft k : toml-trim-indent <ret> }
     73     }
     74 }
     75 
     76 }