From 952f91921425b7ad8e0c09337fcb311acc64da57 Mon Sep 17 00:00:00 2001 From: Daniel Mulford Date: Mon, 27 May 2019 15:46:55 -0700 Subject: [PATCH] Basic language support for Awk --- rc/filetype/awk.kak | 86 ++++++++++++++++++++++++++++++++++++++++ rc/filetype/markdown.kak | 8 ++-- 2 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 rc/filetype/awk.kak diff --git a/rc/filetype/awk.kak b/rc/filetype/awk.kak new file mode 100644 index 00000000..12d852ba --- /dev/null +++ b/rc/filetype/awk.kak @@ -0,0 +1,86 @@ +# Detection +# --------- + +hook global BufCreate .*\.awk %{ + set-option buffer filetype awk +} + +# Initialization +# -------------- + +hook global WinSetOption filetype=awk %{ + require-module awk + + hook window InsertChar \n -group awk-indent awk-indent-on-new-line + hook window ModeChange insert:.* -group awk-trim-indent awk-trim-indent + + hook -once -always window WinSetOption filetype=.* %{ remove-hooks window awk-.+ } +} + +hook -group awk-highlight global WinSetOption filetype=awk %{ + add-highlighter window/awk ref awk + hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/awk } +} + +provide-module awk %@ + +# Highlighters +# ------------ + +add-highlighter shared/awk regions +add-highlighter shared/awk/code default-region group +add-highlighter shared/awk/comment region '#' '$' fill comment +add-highlighter shared/awk/string region '"' (?|\?|~ 0:operator + +evaluate-commands %sh{ + # Grammar + patterns="BEGIN END BEGINFILE ENDFILE" + variables="BINMODE CONVFMT FIELDWIDTHS FPAT FS IGNORECASE LINT OFMT OFS + ORS PREC ROUNDMODE RS SUBSEP TEXTDOMAIN ARGC ARGV ARGIND ENVIRON + ERRNO FILENAME FNR NF FUNCTAB NR PROCINFO RLENGTH RSTART RT SYMTAB" + keywords="break continue delete exit function getline next print printf + return switch nextfile func if else while for do" + functions="atan2 cos exp int intdiv log rand sin sqrt srand asort asort1 + gensub gsub index length match patsplit split sprintf strtonum sub + substr tolower toupper close fflush system mktime strftime systime + and compl lshift or rshift xor isarray typeof bindtextdomain dcgettext + dcngetext" + + join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; } + + # Add the language's grammar to the static completion list + printf %s\\n "hook global WinSetOption filetype=awk %{ + set-option window static_words $(join "${patterns} ${variables} ${keywords} ${functions}" ' ') + }" + + # Highlight keywords + printf %s\\n "add-highlighter shared/awk/code/ regex \b($(join "${patterns}" '|'))\b 0:type" + printf %s\\n "add-highlighter shared/awk/code/ regex \b($(join "${variables}" '|'))\b 0:meta" + printf %s\\n "add-highlighter shared/awk/code/ regex \b($(join "${keywords}" '|'))\b 0:keyword" + printf %s\\n "add-highlighter shared/awk/code/ regex \b($(join "${functions}" '|'))\b 0:function" +} + +# Commands +# -------- + +define-command -hidden awk-indent-on-new-line %[ + evaluate-commands -draft -itersel %[ + # preserve previous line indent + try %[ execute-keys -draft \; K ] + # cleanup trailing whitespaces from previous line + try %[ execute-keys -draft k s \h+$ d ] + # indent after line ending in opening curly brace + try %[ execute-keys -draft k \{\h*(#.*)?$ j ] + ] +] + +define-command -hidden awk-trim-indent %{ + try %{ execute-keys -draft \; s ^\h+$ d } +} + +@ diff --git a/rc/filetype/markdown.kak b/rc/filetype/markdown.kak index ab380b7f..a8314928 100644 --- a/rc/filetype/markdown.kak +++ b/rc/filetype/markdown.kak @@ -35,10 +35,10 @@ add-highlighter shared/markdown/inline/text default-region group evaluate-commands %sh{ languages=" - c cabal clojure coffee cpp css cucumber d diff dockerfile fish gas go - haml haskell html ini java javascript json julia kak kickstart latex - lisp lua makefile markdown moon objc perl pug python ragel ruby rust - sass scala scss sh swift toml tupfile typescript yaml sql + awk c cabal clojure coffee cpp css cucumber d diff dockerfile fish + gas go haml haskell html ini java javascript json julia kak kickstart + latex lisp lua makefile markdown moon objc perl pug python ragel + ruby rust sass scala scss sh swift toml tupfile typescript yaml sql " for lang in ${languages}; do printf 'add-highlighter shared/markdown/%s region -match-capture ^(\h*)```\h*%s\\b ^(\h*)``` regions\n' "${lang}" "${lang}"