kakoune

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

awk.kak (3494B)


      1 # Detection
      2 # ---------
      3 
      4 hook global BufCreate .*\.awk %{
      5     set-option buffer filetype awk
      6 }
      7 
      8 # Initialization
      9 # --------------
     10 
     11 hook global WinSetOption filetype=awk %{
     12     require-module awk
     13     
     14     hook window InsertChar \n -group awk-indent awk-indent-on-new-line
     15     hook window ModeChange pop:insert:.* -group awk-trim-indent awk-trim-indent
     16 
     17     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window awk-.+ }
     18 }
     19 
     20 hook -group awk-highlight global WinSetOption filetype=awk %{
     21     add-highlighter window/awk ref awk
     22     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/awk }
     23 }
     24 
     25 provide-module awk %@
     26 
     27 # Highlighters
     28 # ------------
     29 
     30 add-highlighter shared/awk regions
     31 add-highlighter shared/awk/code default-region group
     32 add-highlighter shared/awk/comment region '#' '$' fill comment
     33 add-highlighter shared/awk/string region '"' (?<!\\)(\\\\)*" fill string
     34 add-highlighter shared/awk/regex region (\A|[^\s\w)\]+-]|\bcase)\s*\K/ (?<!\\)(\\\\)*/ fill attribute
     35 
     36 add-highlighter shared/awk/code/ regex (\.\d+|\b\d+\.?\d*)([eE][+-]?\d+)?\b 0:value # Decimal/octal/scientific
     37 add-highlighter shared/awk/code/ regex \b0[xX][\da-fA-F]+\b 0:value # Hexadecimal
     38 add-highlighter shared/awk/code/ regex \$|\+|-|\*|/|%|\^|=|&&|\||!|<|>|\?|~ 0:operator
     39 
     40 evaluate-commands %sh{
     41     # Grammar
     42     patterns="BEGIN END BEGINFILE ENDFILE"
     43     variables="BINMODE CONVFMT FIELDWIDTHS FPAT FS IGNORECASE LINT OFMT OFS
     44         ORS PREC ROUNDMODE RS SUBSEP TEXTDOMAIN ARGC ARGV ARGIND ENVIRON
     45         ERRNO FILENAME FNR NF FUNCTAB NR PROCINFO RLENGTH RSTART RT SYMTAB"
     46     keywords="break continue delete exit function getline next print printf
     47         return switch nextfile func if else while for do"
     48     functions="atan2 cos exp int intdiv log rand sin sqrt srand asort asort1
     49         gensub gsub index length match patsplit split sprintf strtonum sub
     50         substr tolower toupper close fflush system mktime strftime systime
     51         and compl lshift or rshift xor isarray typeof bindtextdomain dcgettext
     52         dcngetext"
     53 
     54     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
     55 
     56     # Add the language's grammar to the static completion list
     57     printf %s\\n "hook global WinSetOption filetype=awk %{
     58         set-option window static_words $(join "${patterns} ${variables} ${keywords} ${functions}" ' ')
     59     }"
     60 
     61     # Highlight keywords
     62     printf %s\\n "add-highlighter shared/awk/code/ regex \b($(join "${patterns}" '|'))\b 0:type"
     63     printf %s\\n "add-highlighter shared/awk/code/ regex \b($(join "${variables}" '|'))\b 0:meta"
     64     printf %s\\n "add-highlighter shared/awk/code/ regex \b($(join "${keywords}" '|'))\b 0:keyword"
     65     printf %s\\n "add-highlighter shared/awk/code/ regex \b($(join "${functions}" '|'))\b 0:function"
     66 }
     67 
     68 # Commands
     69 # --------
     70 
     71 define-command -hidden awk-indent-on-new-line %[
     72     evaluate-commands -draft -itersel %[
     73         # preserve previous line indent
     74         try %[ execute-keys -draft <semicolon> K <a-&> ]
     75         # cleanup trailing whitespaces from previous line
     76         try %[ execute-keys -draft k x s \h+$ <ret> d ]
     77         # indent after line ending in opening curly brace
     78         try %[ execute-keys -draft kx <a-k>\{\h*(#.*)?$<ret> j<a-gt> ]
     79         # deindent closing brace when after cursor
     80         try %[ execute-keys -draft x <a-k> ^\h*\} <ret> gh / \} <ret> m <a-S> 1<a-&> ]
     81     ]
     82 ]
     83 
     84 define-command -hidden awk-trim-indent %{
     85     try %{ execute-keys -draft <semicolon> x s ^\h+$ <ret> d }
     86 }
     87 
     88 @