kakoune

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

sh.kak (8254B)


      1 hook global BufCreate .*\.((z|ba|c|k|mk)?(sh(rc|_profile|env)?|profile)) %{
      2     set-option buffer filetype sh
      3 }
      4 
      5 hook global WinSetOption filetype=sh %{
      6     require-module sh
      7     set-option window static_words %opt{sh_static_words}
      8 
      9     hook window ModeChange pop:insert:.* -group sh-trim-indent sh-trim-indent
     10     hook window InsertChar \n -group sh-insert sh-insert-on-new-line
     11     hook window InsertChar \n -group sh-indent sh-indent-on-new-line
     12     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window sh-.+ }
     13 }
     14 
     15 hook -group sh-highlight global WinSetOption filetype=sh %{
     16     add-highlighter window/sh ref sh
     17     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/sh }
     18 }
     19 
     20 # using non-ascii characters here so that we can use the '[' command
     21 provide-module sh %§
     22 
     23 add-highlighter shared/sh regions
     24 add-highlighter shared/sh/code default-region group
     25 add-highlighter shared/sh/arithmetic region -recurse \(.*?\( (\$|(?<=for)\h*)\(\( \)\) group
     26 add-highlighter shared/sh/double_string region  %{(?<!\\)(?:\\\\)*\K"} %{(?<!\\)(?:\\\\)*"} group
     27 add-highlighter shared/sh/single_string region %{(?<!\\)(?:\\\\)*\K'} %{'} fill string
     28 add-highlighter shared/sh/expansion region -recurse (?<!\\)(?:\\\\)*\K\$\{ (?<!\\)(?:\\\\)*\K\$\{ \}|\n fill value
     29 add-highlighter shared/sh/comment region (?<!\\)(?:\\\\)*(?:^|\h)\K# '$' fill comment
     30 add-highlighter shared/sh/heredoc region -match-capture '<<-?\h*''?(\w+)''?' '^\t*(\w+)$' fill string
     31 
     32 add-highlighter shared/sh/arithmetic/expansion ref sh/double_string/expansion
     33 add-highlighter shared/sh/double_string/fill fill string
     34 
     35 evaluate-commands %sh{
     36     # Grammar
     37     # Generated with `compgen -k` in bash
     38     keywords="if then else elif fi case esac for select while until do done in
     39              function time coproc"
     40 
     41     # Generated with `compgen -b` in bash
     42     builtins="alias bg bind break builtin caller cd command compgen complete
     43              compopt continue declare dirs disown echo enable eval exec
     44              exit export false fc fg getopts hash help history jobs kill
     45              let local logout mapfile popd printf pushd pwd read readarray
     46              readonly return set shift shopt source suspend test times trap
     47              true type typeset ulimit umask unalias unset wait"
     48 
     49     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
     50 
     51     # Add the language's grammar to the static completion list
     52     printf %s\\n "declare-option str-list sh_static_words $(join "${keywords}" ' ') $(join "${builtins}" ' ')"
     53 
     54     # Highlight keywords
     55     printf %s\\n "add-highlighter shared/sh/code/ regex (?<!-)\b($(join "${keywords}" '|'))\b(?!-) 0:keyword"
     56 
     57     # Highlight builtins
     58     printf %s "add-highlighter shared/sh/code/builtin regex (?<!-)\b($(join "${builtins}" '|'))\b(?!-) 0:builtin"
     59 }
     60 
     61 add-highlighter shared/sh/code/operators regex [\[\]\(\)&|]{1,2} 0:operator
     62 add-highlighter shared/sh/code/variable regex ((?<![-:])\b\w+)= 1:variable
     63 add-highlighter shared/sh/code/alias regex \balias(\h+[-+]\w)*\h+([\w-.]+)= 2:variable
     64 add-highlighter shared/sh/code/function regex ^\h*(\S+(?<!=))\h*\(\) 1:function
     65 
     66 add-highlighter shared/sh/code/unscoped_expansion regex (?<!\\)(?:\\\\)*\K\$(\w+|#|@|\?|\$|!|-|\*) 0:value
     67 add-highlighter shared/sh/double_string/expansion regex (?<!\\)(?:\\\\)*\K\$(\w+|#|@|\?|\$|!|-|\*|\{.+?\}) 0:value
     68 
     69 # Commands
     70 # ‾‾‾‾‾‾‾‾
     71 
     72 define-command -hidden sh-trim-indent %{
     73     # remove trailing white spaces
     74     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
     75 }
     76 
     77 # This is at best an approximation, since shell syntax is very complex.
     78 # Also note that this targets plain sh syntax, not bash - bash adds a whole
     79 # other level of complexity. If your bash code is fairly portable this will
     80 # probably work.
     81 #
     82 # Of necessity, this is also fairly opinionated about indentation styles.
     83 # Doing it "properly" would require far more context awareness than we can
     84 # bring to this kind of thing.
     85 define-command -hidden sh-insert-on-new-line %[
     86     evaluate-commands -draft -itersel %[
     87         # copy '#' comment prefix and following white spaces
     88         try %{ execute-keys -draft k x s ^\h*\K#\h* <ret> y gh j P }
     89     ]
     90 ]
     91 
     92 # Use custom object matching to copy indentation for the various logical
     93 # blocks.
     94 #
     95 # Note that we're using a weird non-ascii character instead of [ or { here
     96 # because the '[' and '{' characters need to be available for the commands.
     97 define-command -hidden sh-indent-on-new-line %¶
     98     evaluate-commands -draft -itersel %@
     99         # preserve previous line indent
    100         try %{ execute-keys -draft <semicolon> K <a-&> }
    101         # filter previous line
    102         try %{ execute-keys -draft k : sh-trim-indent <ret> }
    103 
    104         # Indent loop syntax, e.g.:
    105         # for foo in bar; do
    106         #       things
    107         # done
    108         #
    109         # or:
    110         #
    111         # while foo; do
    112         #       things
    113         # done
    114         #
    115         # or equivalently:
    116         #
    117         # while foo
    118         # do
    119         #       things
    120         # done
    121         #
    122         # indent after do
    123         try %{ execute-keys -draft , k x <a-k> \bdo$ <ret> j <a-gt> }
    124         # copy the indentation of the matching for/when - matching on the do
    125         # statement, so we don't need to duplicate this for the two loop
    126         # structures.
    127         try %{ execute-keys -draft , k x <a-k> \bdone$ <ret> gh [c\bdo\b,\bdone\b <ret> x <a-S> 1<a-&> , j K <a-&> }
    128 
    129         # Indent if/then/else syntax, e.g.:
    130         # if [ $foo = $bar ]; then
    131         #       things
    132         # else
    133         #       other_things
    134         # fi
    135         #
    136         # or equivalently:
    137         # if [ $foo = $bar ]
    138         # then
    139         #       things
    140         # else
    141         #       other_things
    142         # fi
    143         #
    144         # indent after then
    145         try %{ execute-keys -draft , k x <a-k> \bthen$ <ret> j <a-gt> }
    146         # copy the indentation of the matching if
    147         try %{ execute-keys -draft , k x <a-k> \bfi$ <ret> gh [c\bif\b,\bfi\b <ret> x <a-S> 1<a-&> , j K <a-&> }
    148         # copy the indentation of the matching if, and then re-indent afterwards
    149         try %{ execute-keys -draft , k x <a-k> \belse$ <ret> gh [c\bif\b,\bfi\b <ret> x <a-S> 1<a-&> , j K <a-&> j <a-gt> }
    150 
    151         # Indent case syntax, e.g.:
    152         # case "$foo" in
    153         #       bar) thing1;;
    154         #       baz)
    155         #               things
    156         #               ;;
    157         #       *)
    158         #               default_things
    159         #               ;;
    160         # esac
    161         #
    162         # or equivalently:
    163         # case "$foo"
    164         # in
    165         #       bar) thing1;;
    166         # esac
    167         #
    168         # indent after in
    169         try %{ execute-keys -draft , k x <a-k> \bin$ <ret> j <a-gt> }
    170         # copy the indentation of the matching case
    171         try %{ execute-keys -draft , k x <a-k> \besac$ <ret> gh [c\bcase\b,\besac\b <ret> x <a-S> 1<a-&> , j K <a-&> }
    172         # indent after )
    173         try %{ execute-keys -draft , k x <a-k> ^\s*\(?[^(]+[^)]\)$ <ret> j <a-gt> }
    174         # deindent after ;;
    175         try %{ execute-keys -draft , k x <a-k> ^\s*\;\;$ <ret> j <a-lt> }
    176 
    177         # Indent compound commands as logical blocks, e.g.:
    178         # {
    179         #       thing1
    180         #       thing2
    181         # }
    182         #
    183         # or in a function definition:
    184         # foo () {
    185         #       thing1
    186         #       thing2
    187         # }
    188         #
    189         # We don't handle () delimited compond commands - these are technically very
    190         # similar, but the use cases are quite different and much less common.
    191         #
    192         # Note that in this context the '{' and '}' characters are reserved
    193         # words, and hence must be surrounded by a token separator - typically
    194         # white space (including a newline), though technically it can also be
    195         # ';'. Only vertical white space makes sense in this context, though,
    196         # since the syntax denotes a logical block, not a simple compound command.
    197         try %= execute-keys -draft , k x <a-k> (\s|^)\{$ <ret> j <a-gt> =
    198         # deindent closing }
    199         try %= execute-keys -draft , k x <a-k> ^\s*\}$ <ret> <a-lt> j K <a-&> =
    200         # deindent closing } when after cursor
    201         try %= execute-keys -draft x <a-k> ^\h*\} <ret> gh / \} <ret> m <a-S> 1<a-&> =
    202 
    203     @
    204    205 
    206 §