kakoune

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

coffee.kak (4251B)


      1 # http://coffeescript.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](coffee) %{
      8     set-option buffer filetype coffee
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=coffee %{
     15     require-module coffee
     16 
     17     hook window ModeChange pop:insert:.* -group coffee-trim-indent coffee-trim-indent
     18     hook window InsertChar \n -group coffee-insert coffee-insert-on-new-line
     19     hook window InsertChar \n -group coffee-indent coffee-indent-on-new-line
     20 
     21     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window coffee-.+ }
     22 }
     23 
     24 hook -group coffee-highlight global WinSetOption filetype=coffee %{
     25     add-highlighter window/coffee ref coffee
     26     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/coffee }
     27 }
     28 
     29 
     30 provide-module coffee %[
     31 
     32 # Highlighters
     33 # ‾‾‾‾‾‾‾‾‾‾‾‾
     34 
     35 add-highlighter shared/coffee regions
     36 add-highlighter shared/coffee/code     default-region group
     37 add-highlighter shared/coffee/single_string     region "'" "'"                    fill string
     38 add-highlighter shared/coffee/single_string_alt region "'''" "'''"                fill string
     39 add-highlighter shared/coffee/double_string     region '"' (?<!\\)(\\\\)*"        regions
     40 add-highlighter shared/coffee/double_string_alt region '"""' '"""'                ref shared/coffee/double_string
     41 add-highlighter shared/coffee/regex             region '/' (?<!\\)(\\\\)*/[gimy]* regions
     42 add-highlighter shared/coffee/regex_alt         region '///' ///[gimy]*           ref shared/coffee/regex
     43 add-highlighter shared/coffee/comment1          region '#' '$'                    fill comment
     44 add-highlighter shared/coffee/comment2          region '###' '###'                fill comment
     45 
     46 # Regular expression flags are: g → global match, i → ignore case, m → multi-lines, y → sticky
     47 # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
     48 
     49 add-highlighter shared/coffee/double_string/base default-region fill string
     50 add-highlighter shared/coffee/double_string/interpolation region -recurse \{ \Q#{ \} fill meta
     51 add-highlighter shared/coffee/regex/base default-region fill meta
     52 add-highlighter shared/coffee/regex/interpolation region -recurse \{ \Q#{ \} fill meta
     53 
     54 # Keywords are collected at
     55 # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
     56 # http://coffeescript.org/documentation/docs/lexer.html#section-63
     57 add-highlighter shared/coffee/code/ regex [$@]\w* 0:variable
     58 add-highlighter shared/coffee/code/ regex \b(Array|Boolean|Date|Function|Number|Object|RegExp|String)\b 0:type
     59 add-highlighter shared/coffee/code/ regex \b(document|false|no|null|off|on|parent|self|this|true|undefined|window|yes)\b 0:value
     60 add-highlighter shared/coffee/code/ regex \b(and|is|isnt|not|or)\b 0:operator
     61 add-highlighter shared/coffee/code/ regex \b(break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|function|if|implements|import|in|instanceof|interface|let|native|new|package|private|protected|public|return|static|super|switch|throw|try|typeof|var|void|while|with|yield)\b 0:keyword
     62 
     63 # Commands
     64 # ‾‾‾‾‾‾‾‾
     65 
     66 define-command -hidden coffee-trim-indent %{
     67     evaluate-commands -draft -itersel %{
     68         execute-keys x
     69         # remove trailing white spaces
     70         try %{ execute-keys -draft s \h + $ <ret> d }
     71     }
     72 }
     73 
     74 define-command -hidden coffee-insert-on-new-line %{
     75     evaluate-commands -draft -itersel %{
     76         # copy '#' comment prefix and following white spaces
     77         try %{ execute-keys -draft k x s '^\h*\K#\h*' <ret> y gh j P }
     78     }
     79 }
     80 
     81 define-command -hidden coffee-indent-on-new-line %{
     82     evaluate-commands -draft -itersel %{
     83         # preserve previous line indent
     84         try %{ execute-keys -draft <semicolon> K <a-&> }
     85         # filter previous line
     86         try %{ execute-keys -draft k : coffee-trim-indent <ret> }
     87         # indent after start structure
     88         try %{ execute-keys -draft k x <a-k> ^ \h * (case|catch|class|else|finally|for|function|if|switch|try|while|with) \b | (=|->) $ <ret> j <a-gt> }
     89     }
     90 }
     91 
     92 ]