kakoune

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

fennel.kak (4936B)


      1 # http://fennel-lang.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.]fnl %{
      8     set-option buffer filetype fennel
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 hook global WinSetOption filetype=fennel %{
     14     require-module fennel
     15     fennel-configure-window
     16 }
     17 
     18 hook -group fennel-highlight global WinSetOption filetype=fennel %{
     19     add-highlighter window/fennel ref fennel
     20     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/fennel }
     21 }
     22 
     23 provide-module fennel %{
     24 
     25 require-module lisp
     26 
     27 # Highlighters
     28 # ‾‾‾‾‾‾‾‾‾‾‾‾
     29 
     30 add-highlighter shared/fennel regions
     31 add-highlighter shared/fennel/code default-region group
     32 add-highlighter shared/fennel/comment region '(?<!\\)(?:\\\\)*\K;' '$' fill comment
     33 add-highlighter shared/fennel/shebang region '(?<!\\)(?:\\\\)*\K#!' '$' fill comment
     34 add-highlighter shared/fennel/string region '(?<!\\)(?:\\\\)*\K"' '(?<!\\)(?:\\\\)*"' fill string
     35 add-highlighter shared/fennel/colon-string region '\W\K:[-\w?\\^_!$%&*+./@|<=>]' '(?![-\w?\\^_!$%&*+./@|<=>])' fill keyword
     36 add-highlighter shared/fennel/code/ regex \\(?:space|tab|newline|return|backspace|formfeed|u[0-9a-fA-F]{4}|o[0-3]?[0-7]{1,2}|.)\b 0:string
     37 
     38 evaluate-commands %sh{
     39     # Grammar
     40     keywords="require-macros eval-compiler doc lua hashfn macro macros import-macros pick-args pick-values macroexpand macrodebug
     41               do values if when each for fn lambda λ partial while set global var local let tset set-forcibly! doto match or and
     42               not not= collect icollect accumulate rshift lshift bor band bnot bxor with-open"
     43     re_keywords='\\$ \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9 \\$\\.\\.\\.'
     44     builtins="_G _VERSION arg assert bit32 collectgarbage coroutine debug
     45               dofile error getfenv getmetatable io ipairs length load
     46               loadfile loadstring math next os package pairs pcall
     47               print rawequal rawget rawlen rawset require select setfenv
     48               setmetatable string table tonumber tostring type unpack xpcall"
     49 
     50     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
     51 
     52 # Add the language's grammar to the static completion list
     53     printf %s\\n "declare-option str-list fennel_static_words $(join "${keywords} ${builtins} false nil true" ' ')"
     54 
     55     # Highlight keywords
     56     printf %s "
     57         add-highlighter shared/fennel/code/keywords regex \b($(join "${keywords} ${re_keywords}" '|'))\b 0:keyword
     58         add-highlighter shared/fennel/code/builtins regex \b($(join "${builtins}" '|'))\b 0:builtin
     59     "
     60 }
     61 
     62 add-highlighter shared/fennel/code/operator regex (\.|\?\.|\+|\^|-|\*|%|/|>|<|>=|<=|=|not=|:|->|->>|-\?>|-\?>>)\s+|\.\.\.? 0:operator
     63 add-highlighter shared/fennel/code/value regex (\b(false|nil|true)\b|-?\b[0-9][_0-9]*(:?\.[_0-9]+)?(:?[eE]-?[_0-9]+)?|0x[0-9a-fA-F]\b) 0:value
     64 add-highlighter shared/fennel/code/function_declaration regex \((?:fn|lambda|λ)\s+([\S]+) 1:function
     65 add-highlighter shared/fennel/code/method_call regex (?:\w+|\$[0-9]{0,1}):(\S+)\b 1:function
     66 
     67 # Commands
     68 # ‾‾‾‾‾‾‾‾
     69 
     70 define-command -hidden fennel-configure-window %{
     71     set-option window static_words %opt{fennel_static_words}
     72 
     73     hook window ModeChange pop:insert:.* -group fennel-trim-indent fennel-trim-indent
     74     hook window InsertChar \n -group fennel-indent fennel-indent-on-new-line
     75 
     76     set-option buffer extra_word_chars '_' . / * ? + - < > ! : "'"
     77     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window fennel-.+ }
     78 }
     79 
     80 define-command -hidden fennel-trim-indent lisp-trim-indent
     81 
     82 declare-option \
     83     -docstring 'regex matching the head of forms which have options *and* indented bodies' \
     84     regex fennel_special_indent_forms \
     85     '(?:if|when|each|for|fn|lambda|λ|partial|while|local|var|doto|let)'
     86 
     87 define-command -hidden fennel-indent-on-new-line %{
     88     # registers: i = best align point so far; w = start of first word of form
     89     evaluate-commands -draft -save-regs '/"|^@iw' -itersel %{
     90         execute-keys -draft 'gk"iZ'
     91         try %{
     92             execute-keys -draft '[bl"i<a-Z><gt>"wZ'
     93 
     94             try %{
     95                 # If a special form, indent another (indentwidth - 1) spaces
     96                 execute-keys -draft '"wze<a-K>[\s()\[\]\{\}]<ret><a-k>\A' %opt{fennel_special_indent_forms} '\z<ret>'
     97                 execute-keys -draft '"wze<a-L>s.{' %sh{printf $(( kak_opt_indentwidth - 1 ))} '}\K.*<ret><a-;>;"i<a-Z><gt>'
     98             } catch %{
     99                 # If not special and parameter appears on line 1, indent to parameter
    100                 execute-keys -draft '"wz<a-K>[()[\]{}]<ret>e<a-K>[\s()\[\]\{\}]<ret><a-l>s\h\K[^\s].*<ret><a-;>;"i<a-Z><gt>'
    101             }
    102         }
    103         try %{ execute-keys -draft '[rl"i<a-Z><gt>' }
    104         try %{ execute-keys -draft '[Bl"i<a-Z><gt>' }
    105         execute-keys -draft ';"i<a-z>a&,'
    106     }
    107 }
    108 
    109 }