filetype: shell

# Detection based on filename is rather complicated as there are many
# different file extensions and special filenames in use.
# This expressions aims to capture them all while not matching
# filenames that coincidentally contain the same substring.
#
# File extensions:
# * .sh
# * .bash
# * .ash
# * .ebuild (Gentoo ebuild format)
#
# Special filenames:
# * .bashrc, .bash_aliases, .bash_functions .bash_profile
# * profile, .profile (/etc/profile or ~/.profile)
# * Pkgfile
# * pkgmk.conf
# * rc.conf
# * PKGBUILD (Arch Linux build scripts)
# * APKBUILD
#
# Fix command (fc) files:
# * bash-fc. (followed by a random string)
detect:
    filename: "(\\.(sh|bash|ash|ebuild)$|(\\.bash(rc|_aliases|_functions|_profile)|\\.?profile|Pkgfile|pkgmk\\.conf|rc\\.conf|PKGBUILD|APKBUILD)$|bash-fc\\.)"
    header: "^#!.*/(env +)?(ba)?(a)?(mk)?sh( |$)"

rules:
    # Numbers
    - constant.number: "\\b[0-9]+\\b"
      # Conditionals and control flow
    - statement: "\\b(break|case|continue|do|done|elif|else|esac|exec|exit|fi|for|function|if|in|return|select|then|trap|until|wait|while)\\b"
    - special: "[`$<>!=&~^\\{\\}\\(\\)\\;\\]\\[]+"
      # Shell commands
    - type: "\\b(cd|command|echo|eval|export|getopts|let|local|read|set|shift|time|umask|unset)\\b"
      # Common linux commands
    - type: "\\b((g|ig)?awk|bash|dash|find|getopt|\\w{0,4}grep|kill|killall|\\w{0,4}less|make|pkill|sed|sh|tar)\\b"
      # Coreutils commands
    - type: "\\b(base64|basename|cat|chcon|chgrp|chmod|chown|chroot|cksum|comm|cp|csplit|cut|date|dd|df|dir|dircolors|dirname|du|env|expand|expr|factor|false|fmt|fold|head|hostid|id|install|join|link|ln|logname|ls|md5sum|mkdir|mkfifo|mknod|mktemp|mv|nice|nl|nohup|nproc|numfmt|od|paste|pathchk|pinky|pr|printenv|printf|ptx|pwd|readlink|realpath|rm|rmdir|runcon|seq|(sha1|sha224|sha256|sha384|sha512)sum|shred|shuf|sleep|sort|split|stat|stdbuf|stty|sum|sync|tac|tail|tee|test|time|timeout|touch|tr|true|truncate|tsort|tty|uname|unexpand|uniq|unlink|users|vdir|wc|who|whoami|yes)\\b"
      # Conditional flags
    - statement: "(\\s|^)(--?[A-Za-z0-9][\\w-]*)"

    - identifier: "\\$\\{[\\w:!%&=+#~@*^$?, .\\-\\/\\[\\]]+\\}"
    - identifier: "\\$([0-9!#@*$?-]|[A-Za-z_]\\w*)"

    - constant.string:
        start: "\""
        end: "\""
        skip: "\\\\."
        rules: []

    - constant.string:
        start: "'"
        end: "'"
        skip: "\\\\."
        rules: []

    - constant.string:
        start: "<<[^\\s]+[-~.]*[A-Za-z0-9]+$"
        end: "^[^\\s]+[A-Za-z0-9]+$"
        skip: "\\\\."
        rules: []

    - comment:
        start: "(^|\\s)#"
        end: "$"
        rules:
            - todo: "(TODO|XXX|FIXME):?"
