1 | (function (Prism) {
|
---|
2 | // $ set | grep '^[A-Z][^[:space:]]*=' | cut -d= -f1 | tr '\n' '|'
|
---|
3 | // + LC_ALL, RANDOM, REPLY, SECONDS.
|
---|
4 | // + make sure PS1..4 are here as they are not always set,
|
---|
5 | // - some useless things.
|
---|
6 | var envVars = '\\b(?:BASH|BASHOPTS|BASH_ALIASES|BASH_ARGC|BASH_ARGV|BASH_CMDS|BASH_COMPLETION_COMPAT_DIR|BASH_LINENO|BASH_REMATCH|BASH_SOURCE|BASH_VERSINFO|BASH_VERSION|COLORTERM|COLUMNS|COMP_WORDBREAKS|DBUS_SESSION_BUS_ADDRESS|DEFAULTS_PATH|DESKTOP_SESSION|DIRSTACK|DISPLAY|EUID|GDMSESSION|GDM_LANG|GNOME_KEYRING_CONTROL|GNOME_KEYRING_PID|GPG_AGENT_INFO|GROUPS|HISTCONTROL|HISTFILE|HISTFILESIZE|HISTSIZE|HOME|HOSTNAME|HOSTTYPE|IFS|INSTANCE|JOB|LANG|LANGUAGE|LC_ADDRESS|LC_ALL|LC_IDENTIFICATION|LC_MEASUREMENT|LC_MONETARY|LC_NAME|LC_NUMERIC|LC_PAPER|LC_TELEPHONE|LC_TIME|LESSCLOSE|LESSOPEN|LINES|LOGNAME|LS_COLORS|MACHTYPE|MAILCHECK|MANDATORY_PATH|NO_AT_BRIDGE|OLDPWD|OPTERR|OPTIND|ORBIT_SOCKETDIR|OSTYPE|PAPERSIZE|PATH|PIPESTATUS|PPID|PS1|PS2|PS3|PS4|PWD|RANDOM|REPLY|SECONDS|SELINUX_INIT|SESSION|SESSIONTYPE|SESSION_MANAGER|SHELL|SHELLOPTS|SHLVL|SSH_AUTH_SOCK|TERM|UID|UPSTART_EVENTS|UPSTART_INSTANCE|UPSTART_JOB|UPSTART_SESSION|USER|WINDOWID|XAUTHORITY|XDG_CONFIG_DIRS|XDG_CURRENT_DESKTOP|XDG_DATA_DIRS|XDG_GREETER_DATA_DIR|XDG_MENU_PREFIX|XDG_RUNTIME_DIR|XDG_SEAT|XDG_SEAT_PATH|XDG_SESSION_DESKTOP|XDG_SESSION_ID|XDG_SESSION_PATH|XDG_SESSION_TYPE|XDG_VTNR|XMODIFIERS)\\b';
|
---|
7 |
|
---|
8 | var commandAfterHeredoc = {
|
---|
9 | pattern: /(^(["']?)\w+\2)[ \t]+\S.*/,
|
---|
10 | lookbehind: true,
|
---|
11 | alias: 'punctuation', // this looks reasonably well in all themes
|
---|
12 | inside: null // see below
|
---|
13 | };
|
---|
14 |
|
---|
15 | var insideString = {
|
---|
16 | 'bash': commandAfterHeredoc,
|
---|
17 | 'environment': {
|
---|
18 | pattern: RegExp('\\$' + envVars),
|
---|
19 | alias: 'constant'
|
---|
20 | },
|
---|
21 | 'variable': [
|
---|
22 | // [0]: Arithmetic Environment
|
---|
23 | {
|
---|
24 | pattern: /\$?\(\([\s\S]+?\)\)/,
|
---|
25 | greedy: true,
|
---|
26 | inside: {
|
---|
27 | // If there is a $ sign at the beginning highlight $(( and )) as variable
|
---|
28 | 'variable': [
|
---|
29 | {
|
---|
30 | pattern: /(^\$\(\([\s\S]+)\)\)/,
|
---|
31 | lookbehind: true
|
---|
32 | },
|
---|
33 | /^\$\(\(/
|
---|
34 | ],
|
---|
35 | 'number': /\b0x[\dA-Fa-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee]-?\d+)?/,
|
---|
36 | // Operators according to https://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic
|
---|
37 | 'operator': /--|\+\+|\*\*=?|<<=?|>>=?|&&|\|\||[=!+\-*/%<>^&|]=?|[?~:]/,
|
---|
38 | // If there is no $ sign at the beginning highlight (( and )) as punctuation
|
---|
39 | 'punctuation': /\(\(?|\)\)?|,|;/
|
---|
40 | }
|
---|
41 | },
|
---|
42 | // [1]: Command Substitution
|
---|
43 | {
|
---|
44 | pattern: /\$\((?:\([^)]+\)|[^()])+\)|`[^`]+`/,
|
---|
45 | greedy: true,
|
---|
46 | inside: {
|
---|
47 | 'variable': /^\$\(|^`|\)$|`$/
|
---|
48 | }
|
---|
49 | },
|
---|
50 | // [2]: Brace expansion
|
---|
51 | {
|
---|
52 | pattern: /\$\{[^}]+\}/,
|
---|
53 | greedy: true,
|
---|
54 | inside: {
|
---|
55 | 'operator': /:[-=?+]?|[!\/]|##?|%%?|\^\^?|,,?/,
|
---|
56 | 'punctuation': /[\[\]]/,
|
---|
57 | 'environment': {
|
---|
58 | pattern: RegExp('(\\{)' + envVars),
|
---|
59 | lookbehind: true,
|
---|
60 | alias: 'constant'
|
---|
61 | }
|
---|
62 | }
|
---|
63 | },
|
---|
64 | /\$(?:\w+|[#?*!@$])/
|
---|
65 | ],
|
---|
66 | // Escape sequences from echo and printf's manuals, and escaped quotes.
|
---|
67 | 'entity': /\\(?:[abceEfnrtv\\"]|O?[0-7]{1,3}|U[0-9a-fA-F]{8}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{1,2})/
|
---|
68 | };
|
---|
69 |
|
---|
70 | Prism.languages.bash = {
|
---|
71 | 'shebang': {
|
---|
72 | pattern: /^#!\s*\/.*/,
|
---|
73 | alias: 'important'
|
---|
74 | },
|
---|
75 | 'comment': {
|
---|
76 | pattern: /(^|[^"{\\$])#.*/,
|
---|
77 | lookbehind: true
|
---|
78 | },
|
---|
79 | 'function-name': [
|
---|
80 | // a) function foo {
|
---|
81 | // b) foo() {
|
---|
82 | // c) function foo() {
|
---|
83 | // but not “foo {”
|
---|
84 | {
|
---|
85 | // a) and c)
|
---|
86 | pattern: /(\bfunction\s+)[\w-]+(?=(?:\s*\(?:\s*\))?\s*\{)/,
|
---|
87 | lookbehind: true,
|
---|
88 | alias: 'function'
|
---|
89 | },
|
---|
90 | {
|
---|
91 | // b)
|
---|
92 | pattern: /\b[\w-]+(?=\s*\(\s*\)\s*\{)/,
|
---|
93 | alias: 'function'
|
---|
94 | }
|
---|
95 | ],
|
---|
96 | // Highlight variable names as variables in for and select beginnings.
|
---|
97 | 'for-or-select': {
|
---|
98 | pattern: /(\b(?:for|select)\s+)\w+(?=\s+in\s)/,
|
---|
99 | alias: 'variable',
|
---|
100 | lookbehind: true
|
---|
101 | },
|
---|
102 | // Highlight variable names as variables in the left-hand part
|
---|
103 | // of assignments (“=” and “+=”).
|
---|
104 | 'assign-left': {
|
---|
105 | pattern: /(^|[\s;|&]|[<>]\()\w+(?:\.\w+)*(?=\+?=)/,
|
---|
106 | inside: {
|
---|
107 | 'environment': {
|
---|
108 | pattern: RegExp('(^|[\\s;|&]|[<>]\\()' + envVars),
|
---|
109 | lookbehind: true,
|
---|
110 | alias: 'constant'
|
---|
111 | }
|
---|
112 | },
|
---|
113 | alias: 'variable',
|
---|
114 | lookbehind: true
|
---|
115 | },
|
---|
116 | // Highlight parameter names as variables
|
---|
117 | 'parameter': {
|
---|
118 | pattern: /(^|\s)-{1,2}(?:\w+:[+-]?)?\w+(?:\.\w+)*(?=[=\s]|$)/,
|
---|
119 | alias: 'variable',
|
---|
120 | lookbehind: true
|
---|
121 | },
|
---|
122 | 'string': [
|
---|
123 | // Support for Here-documents https://en.wikipedia.org/wiki/Here_document
|
---|
124 | {
|
---|
125 | pattern: /((?:^|[^<])<<-?\s*)(\w+)\s[\s\S]*?(?:\r?\n|\r)\2/,
|
---|
126 | lookbehind: true,
|
---|
127 | greedy: true,
|
---|
128 | inside: insideString
|
---|
129 | },
|
---|
130 | // Here-document with quotes around the tag
|
---|
131 | // → No expansion (so no “inside”).
|
---|
132 | {
|
---|
133 | pattern: /((?:^|[^<])<<-?\s*)(["'])(\w+)\2\s[\s\S]*?(?:\r?\n|\r)\3/,
|
---|
134 | lookbehind: true,
|
---|
135 | greedy: true,
|
---|
136 | inside: {
|
---|
137 | 'bash': commandAfterHeredoc
|
---|
138 | }
|
---|
139 | },
|
---|
140 | // “Normal” string
|
---|
141 | {
|
---|
142 | // https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
|
---|
143 | pattern: /(^|[^\\](?:\\\\)*)"(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|[^"\\`$])*"/,
|
---|
144 | lookbehind: true,
|
---|
145 | greedy: true,
|
---|
146 | inside: insideString
|
---|
147 | },
|
---|
148 | {
|
---|
149 | // https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html
|
---|
150 | pattern: /(^|[^$\\])'[^']*'/,
|
---|
151 | lookbehind: true,
|
---|
152 | greedy: true
|
---|
153 | },
|
---|
154 | {
|
---|
155 | // https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
|
---|
156 | pattern: /\$'(?:[^'\\]|\\[\s\S])*'/,
|
---|
157 | greedy: true,
|
---|
158 | inside: {
|
---|
159 | 'entity': insideString.entity
|
---|
160 | }
|
---|
161 | }
|
---|
162 | ],
|
---|
163 | 'environment': {
|
---|
164 | pattern: RegExp('\\$?' + envVars),
|
---|
165 | alias: 'constant'
|
---|
166 | },
|
---|
167 | 'variable': insideString.variable,
|
---|
168 | 'function': {
|
---|
169 | pattern: /(^|[\s;|&]|[<>]\()(?:add|apropos|apt|apt-cache|apt-get|aptitude|aspell|automysqlbackup|awk|basename|bash|bc|bconsole|bg|bzip2|cal|cargo|cat|cfdisk|chgrp|chkconfig|chmod|chown|chroot|cksum|clear|cmp|column|comm|composer|cp|cron|crontab|csplit|curl|cut|date|dc|dd|ddrescue|debootstrap|df|diff|diff3|dig|dir|dircolors|dirname|dirs|dmesg|docker|docker-compose|du|egrep|eject|env|ethtool|expand|expect|expr|fdformat|fdisk|fg|fgrep|file|find|fmt|fold|format|free|fsck|ftp|fuser|gawk|git|gparted|grep|groupadd|groupdel|groupmod|groups|grub-mkconfig|gzip|halt|head|hg|history|host|hostname|htop|iconv|id|ifconfig|ifdown|ifup|import|install|ip|java|jobs|join|kill|killall|less|link|ln|locate|logname|logrotate|look|lpc|lpr|lprint|lprintd|lprintq|lprm|ls|lsof|lynx|make|man|mc|mdadm|mkconfig|mkdir|mke2fs|mkfifo|mkfs|mkisofs|mknod|mkswap|mmv|more|most|mount|mtools|mtr|mutt|mv|nano|nc|netstat|nice|nl|node|nohup|notify-send|npm|nslookup|op|open|parted|passwd|paste|pathchk|ping|pkill|pnpm|podman|podman-compose|popd|pr|printcap|printenv|ps|pushd|pv|quota|quotacheck|quotactl|ram|rar|rcp|reboot|remsync|rename|renice|rev|rm|rmdir|rpm|rsync|scp|screen|sdiff|sed|sendmail|seq|service|sftp|sh|shellcheck|shuf|shutdown|sleep|slocate|sort|split|ssh|stat|strace|su|sudo|sum|suspend|swapon|sync|sysctl|tac|tail|tar|tee|time|timeout|top|touch|tr|traceroute|tsort|tty|umount|uname|unexpand|uniq|units|unrar|unshar|unzip|update-grub|uptime|useradd|userdel|usermod|users|uudecode|uuencode|v|vcpkg|vdir|vi|vim|virsh|vmstat|wait|watch|wc|wget|whereis|which|who|whoami|write|xargs|xdg-open|yarn|yes|zenity|zip|zsh|zypper)(?=$|[)\s;|&])/,
|
---|
170 | lookbehind: true
|
---|
171 | },
|
---|
172 | 'keyword': {
|
---|
173 | pattern: /(^|[\s;|&]|[<>]\()(?:case|do|done|elif|else|esac|fi|for|function|if|in|select|then|until|while)(?=$|[)\s;|&])/,
|
---|
174 | lookbehind: true
|
---|
175 | },
|
---|
176 | // https://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html
|
---|
177 | 'builtin': {
|
---|
178 | pattern: /(^|[\s;|&]|[<>]\()(?:\.|:|alias|bind|break|builtin|caller|cd|command|continue|declare|echo|enable|eval|exec|exit|export|getopts|hash|help|let|local|logout|mapfile|printf|pwd|read|readarray|readonly|return|set|shift|shopt|source|test|times|trap|type|typeset|ulimit|umask|unalias|unset)(?=$|[)\s;|&])/,
|
---|
179 | lookbehind: true,
|
---|
180 | // Alias added to make those easier to distinguish from strings.
|
---|
181 | alias: 'class-name'
|
---|
182 | },
|
---|
183 | 'boolean': {
|
---|
184 | pattern: /(^|[\s;|&]|[<>]\()(?:false|true)(?=$|[)\s;|&])/,
|
---|
185 | lookbehind: true
|
---|
186 | },
|
---|
187 | 'file-descriptor': {
|
---|
188 | pattern: /\B&\d\b/,
|
---|
189 | alias: 'important'
|
---|
190 | },
|
---|
191 | 'operator': {
|
---|
192 | // Lots of redirections here, but not just that.
|
---|
193 | pattern: /\d?<>|>\||\+=|=[=~]?|!=?|<<[<-]?|[&\d]?>>|\d[<>]&?|[<>][&=]?|&[>&]?|\|[&|]?/,
|
---|
194 | inside: {
|
---|
195 | 'file-descriptor': {
|
---|
196 | pattern: /^\d/,
|
---|
197 | alias: 'important'
|
---|
198 | }
|
---|
199 | }
|
---|
200 | },
|
---|
201 | 'punctuation': /\$?\(\(?|\)\)?|\.\.|[{}[\];\\]/,
|
---|
202 | 'number': {
|
---|
203 | pattern: /(^|\s)(?:[1-9]\d*|0)(?:[.,]\d+)?\b/,
|
---|
204 | lookbehind: true
|
---|
205 | }
|
---|
206 | };
|
---|
207 |
|
---|
208 | commandAfterHeredoc.inside = Prism.languages.bash;
|
---|
209 |
|
---|
210 | /* Patterns in command substitution. */
|
---|
211 | var toBeCopied = [
|
---|
212 | 'comment',
|
---|
213 | 'function-name',
|
---|
214 | 'for-or-select',
|
---|
215 | 'assign-left',
|
---|
216 | 'parameter',
|
---|
217 | 'string',
|
---|
218 | 'environment',
|
---|
219 | 'function',
|
---|
220 | 'keyword',
|
---|
221 | 'builtin',
|
---|
222 | 'boolean',
|
---|
223 | 'file-descriptor',
|
---|
224 | 'operator',
|
---|
225 | 'punctuation',
|
---|
226 | 'number'
|
---|
227 | ];
|
---|
228 | var inside = insideString.variable[1].inside;
|
---|
229 | for (var i = 0; i < toBeCopied.length; i++) {
|
---|
230 | inside[toBeCopied[i]] = Prism.languages.bash[toBeCopied[i]];
|
---|
231 | }
|
---|
232 |
|
---|
233 | Prism.languages.sh = Prism.languages.bash;
|
---|
234 | Prism.languages.shell = Prism.languages.bash;
|
---|
235 | }(Prism));
|
---|