No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

615 líneas
25 KiB

minor: Fix WARN_CREATE_GLOBAL warnings issued by zsh 5.1.1-dev-0. The following warnings are issued by zsh as of zsh-workers/37018 (commit de9effbce601 to zsh itself): _zsh_highlight_main_highlighter_highlight_dollar_string:17: array parameter match created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_dollar_string:17: array parameter mbegin created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_dollar_string:17: array parameter mend created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_dollar_string:17: numeric parameter parameter MBEGIN created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_dollar_string:17: numeric parameter parameter MEND created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_dollar_string:17: scalar parameter MATCH created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_string:11: array parameter match created globally in function _zsh_highlight_main_highlighter_highlight_string _zsh_highlight_main_highlighter_highlight_string:11: array parameter mbegin created globally in function _zsh_highlight_main_highlighter_highlight_string _zsh_highlight_main_highlighter_highlight_string:11: array parameter mend created globally in function _zsh_highlight_main_highlighter_highlight_string _zsh_highlight_main_highlighter_highlight_string:11: numeric parameter parameter MBEGIN created globally in function _zsh_highlight_main_highlighter_highlight_string _zsh_highlight_main_highlighter_highlight_string:11: numeric parameter parameter MEND created globally in function _zsh_highlight_main_highlighter_highlight_string _zsh_highlight_main_highlighter_highlight_string:11: scalar parameter MATCH created globally in function _zsh_highlight_main_highlighter_highlight_string
hace 9 años
minor: Fix WARN_CREATE_GLOBAL warnings issued by zsh 5.1.1-dev-0. The following warnings are issued by zsh as of zsh-workers/37018 (commit de9effbce601 to zsh itself): _zsh_highlight_main_highlighter_highlight_dollar_string:17: array parameter match created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_dollar_string:17: array parameter mbegin created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_dollar_string:17: array parameter mend created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_dollar_string:17: numeric parameter parameter MBEGIN created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_dollar_string:17: numeric parameter parameter MEND created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_dollar_string:17: scalar parameter MATCH created globally in function _zsh_highlight_main_highlighter_highlight_dollar_string _zsh_highlight_main_highlighter_highlight_string:11: array parameter match created globally in function _zsh_highlight_main_highlighter_highlight_string _zsh_highlight_main_highlighter_highlight_string:11: array parameter mbegin created globally in function _zsh_highlight_main_highlighter_highlight_string _zsh_highlight_main_highlighter_highlight_string:11: array parameter mend created globally in function _zsh_highlight_main_highlighter_highlight_string _zsh_highlight_main_highlighter_highlight_string:11: numeric parameter parameter MBEGIN created globally in function _zsh_highlight_main_highlighter_highlight_string _zsh_highlight_main_highlighter_highlight_string:11: numeric parameter parameter MEND created globally in function _zsh_highlight_main_highlighter_highlight_string _zsh_highlight_main_highlighter_highlight_string:11: scalar parameter MATCH created globally in function _zsh_highlight_main_highlighter_highlight_string
hace 9 años
  1. # -------------------------------------------------------------------------------------------------
  2. # Copyright (c) 2010-2015 zsh-syntax-highlighting contributors
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without modification, are permitted
  6. # provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright notice, this list of conditions
  9. # and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright notice, this list of
  11. # conditions and the following disclaimer in the documentation and/or other materials provided
  12. # with the distribution.
  13. # * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
  14. # may be used to endorse or promote products derived from this software without specific prior
  15. # written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  18. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19. # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  20. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  23. # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  24. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. # -------------------------------------------------------------------------------------------------
  26. # -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
  27. # vim: ft=zsh sw=2 ts=2 et
  28. # -------------------------------------------------------------------------------------------------
  29. # Define default styles.
  30. : ${ZSH_HIGHLIGHT_STYLES[default]:=none}
  31. : ${ZSH_HIGHLIGHT_STYLES[unknown-token]:=fg=red,bold}
  32. : ${ZSH_HIGHLIGHT_STYLES[reserved-word]:=fg=yellow}
  33. : ${ZSH_HIGHLIGHT_STYLES[alias]:=fg=green}
  34. : ${ZSH_HIGHLIGHT_STYLES[suffix-alias]:=fg=green,underline}
  35. : ${ZSH_HIGHLIGHT_STYLES[builtin]:=fg=green}
  36. : ${ZSH_HIGHLIGHT_STYLES[function]:=fg=green}
  37. : ${ZSH_HIGHLIGHT_STYLES[command]:=fg=green}
  38. : ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline}
  39. : ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none}
  40. : ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green}
  41. : ${ZSH_HIGHLIGHT_STYLES[path]:=underline}
  42. : ${ZSH_HIGHLIGHT_STYLES[path_prefix]:=underline}
  43. : ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue}
  44. : ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue}
  45. : ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none}
  46. : ${ZSH_HIGHLIGHT_STYLES[double-hyphen-option]:=none}
  47. : ${ZSH_HIGHLIGHT_STYLES[back-quoted-argument]:=none}
  48. : ${ZSH_HIGHLIGHT_STYLES[single-quoted-argument]:=fg=yellow}
  49. : ${ZSH_HIGHLIGHT_STYLES[double-quoted-argument]:=fg=yellow}
  50. : ${ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]:=fg=yellow}
  51. : ${ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]:=fg=cyan}
  52. : ${ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]:=fg=cyan}
  53. : ${ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]:=fg=cyan}
  54. : ${ZSH_HIGHLIGHT_STYLES[assign]:=none}
  55. : ${ZSH_HIGHLIGHT_STYLES[redirection]:=none}
  56. : ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold}
  57. # Whether the highlighter should be called or not.
  58. _zsh_highlight_main_highlighter_predicate()
  59. {
  60. # accept-* may trigger removal of path_prefix highlighting
  61. [[ $WIDGET == accept-* ]] ||
  62. _zsh_highlight_buffer_modified
  63. }
  64. # Helper to deal with tokens crossing line boundaries.
  65. _zsh_highlight_main_add_region_highlight() {
  66. integer start=$1 end=$2
  67. local style=$3
  68. # The calculation was relative to $PREBUFFER$BUFFER, but region_highlight is
  69. # relative to $BUFFER.
  70. (( start -= $#PREBUFFER ))
  71. (( end -= $#PREBUFFER ))
  72. (( end < 0 )) && return # having end<0 would be a bug
  73. (( start < 0 )) && start=0 # having start<0 is normal with e.g. multiline strings
  74. _zsh_highlight_add_highlight $start $end $style
  75. }
  76. # Wrapper around 'type -w'.
  77. #
  78. # Takes a single argument and outputs the output of 'type -w $1'.
  79. #
  80. # NOTE: This runs 'setopt', but that should be safe since it'll only ever be
  81. # called inside a $(...) subshell, so the effects will be local.
  82. _zsh_highlight_main__type() {
  83. if (( $#options_to_set )); then
  84. setopt localoptions $options_to_set;
  85. fi
  86. LC_ALL=C builtin type -w -- $1 2>/dev/null
  87. }
  88. # Main syntax highlighting function.
  89. _zsh_highlight_main_highlighter()
  90. {
  91. ## Before we even 'emulate -L', we must test a few options that would reset.
  92. if [[ -o interactive_comments ]]; then
  93. local interactive_comments= # set to empty
  94. fi
  95. if [[ -o path_dirs ]]; then
  96. integer path_dirs_was_set=1
  97. else
  98. integer path_dirs_was_set=0
  99. fi
  100. emulate -L zsh
  101. setopt localoptions extendedglob bareglobqual
  102. # At the PS3 prompt and in vared, highlight nothing.
  103. #
  104. # (We can't check this in _zsh_highlight_main_highlighter_predicate because
  105. # if the predicate returns false, the previous value of region_highlight
  106. # would be reused.)
  107. if [[ $CONTEXT == (select|vared) ]]; then
  108. return
  109. fi
  110. ## Variable declarations and initializations
  111. local start_pos=0 end_pos highlight_glob=true arg style
  112. local in_array_assignment=false # true between 'a=(' and the matching ')'
  113. typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR
  114. typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
  115. typeset -a ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW
  116. local -a options_to_set # used in callees
  117. local buf="$PREBUFFER$BUFFER"
  118. integer len="${#buf}"
  119. if (( path_dirs_was_set )); then
  120. options_to_set+=( PATH_DIRS )
  121. fi
  122. unset path_dirs_was_set
  123. ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR=(
  124. '|' '||' ';' '&' '&&'
  125. '|&'
  126. '&!' '&|'
  127. # ### 'case' syntax, but followed by a pattern, not by a command
  128. # ';;' ';&' ';|'
  129. )
  130. ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS=(
  131. 'builtin' 'command' 'exec' 'nocorrect' 'noglob'
  132. 'pkexec' # immune to #121 because it's usually not passed --option flags
  133. )
  134. # Tokens that, at (naively-determined) "command position", are followed by
  135. # a de jure command position. All of these are reserved words.
  136. ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW=(
  137. $'\x7b' # block
  138. $'\x28' # subshell
  139. '()' # anonymous function
  140. 'while'
  141. 'until'
  142. 'if'
  143. 'then'
  144. 'elif'
  145. 'else'
  146. 'do'
  147. 'time'
  148. 'coproc'
  149. '!' # reserved word; unrelated to $histchars[1]
  150. )
  151. # State machine
  152. #
  153. # The states are:
  154. # - :start: Command word
  155. # - :sudo_opt: A leading-dash option to sudo (such as "-u" or "-i")
  156. # - :sudo_arg: The argument to a sudo leading-dash option that takes one,
  157. # when given as a separate word; i.e., "foo" in "-u foo" (two
  158. # words) but not in "-ufoo" (one word).
  159. # - :regular: "Not a command word", and command delimiters are permitted.
  160. # Mainly used to detect premature termination of commands.
  161. #
  162. # When the kind of a word is not yet known, $this_word / $next_word may contain
  163. # multiple states. For example, after "sudo -i", the next word may be either
  164. # another --flag or a command name, hence the state would include both :start:
  165. # and :sudo_opt:.
  166. #
  167. # The tokens are always added with both leading and trailing colons to serve as
  168. # word delimiters (an improvised array); [[ $x == *:foo:* ]] and x=${x//:foo:/}
  169. # will DTRT regardless of how many elements or repetitions $x has..
  170. #
  171. # Handling of redirections: upon seeing a redirection token, we must stall
  172. # the current state --- that is, the value of $this_word --- for two iterations
  173. # (one for the redirection operator, one for the word following it representing
  174. # the redirection target). Therefore, we set $in_redirection to 2 upon seeing a
  175. # redirection operator, decrement it each iteration, and stall the current state
  176. # when it is non-zero. Thus, upon reaching the next word (the one that follows
  177. # the redirection operator and target), $this_word will still contain values
  178. # appropriate for the word immediately following the word that preceded the
  179. # redirection operator.
  180. #
  181. # The "the previous word was a redirection operator" state is not communicated
  182. # to the next iteration via $next_word/$this_word as usual, but via
  183. # $in_redirection. The value of $next_word from the iteration that processed
  184. # the operator is discarded.
  185. #
  186. local this_word=':start:' next_word
  187. integer in_redirection
  188. # Processing buffer
  189. local proc_buf="$buf"
  190. for arg in ${interactive_comments-${(z)buf}} \
  191. ${interactive_comments+${(zZ+c+)buf}}; do
  192. if (( in_redirection )); then
  193. (( --in_redirection ))
  194. fi
  195. if (( in_redirection == 0 )); then
  196. # Initialize $next_word to its default value.
  197. next_word=':regular:'
  198. else
  199. # Stall $next_word.
  200. fi
  201. # $already_added is set to 1 to disable adding an entry to region_highlight
  202. # for this iteration. Currently, that is done for "" and $'' strings,
  203. # which add the entry early so escape sequences within the string override
  204. # the string's color.
  205. integer already_added=0
  206. local style_override=""
  207. if [[ $this_word == *':start:'* ]]; then
  208. in_array_assignment=false
  209. if [[ $arg == 'noglob' ]]; then
  210. highlight_glob=false
  211. fi
  212. fi
  213. # advance $start_pos, skipping over whitespace in $buf.
  214. if [[ $arg == ';' ]] ; then
  215. # We're looking for either a semicolon or a newline, whichever comes
  216. # first. Both of these are rendered as a ";" (SEPER) by the ${(z)..}
  217. # flag.
  218. #
  219. # We can't use the (Z+n+) flag because that elides the end-of-command
  220. # token altogether, so 'echo foo\necho bar' (two commands) becomes
  221. # indistinguishable from 'echo foo echo bar' (one command with three
  222. # words for arguments).
  223. local needle=$'[;\n]'
  224. integer offset=$(( ${proc_buf[(i)$needle]} - 1 ))
  225. (( start_pos += offset ))
  226. (( end_pos = start_pos + $#arg ))
  227. else
  228. integer offset=$(((len-start_pos)-${#${proc_buf##([[:space:]]|\\[[:space:]])#}}))
  229. ((start_pos+=offset))
  230. ((end_pos=$start_pos+${#arg}))
  231. fi
  232. # Above `if` computes new start_pos and end_pos.
  233. # Here we compute new proc_buf. We advance it
  234. # (chop off characters from the beginning)
  235. # beyond what end_pos points to, by skipping
  236. # as many characters as end_pos was advanced.
  237. #
  238. # end_pos was advanced by $offset (via start_pos)
  239. # and by $#arg. Note the `start_pos=$end_pos`
  240. # below.
  241. #
  242. # As for the [,len]. We could use [,len-start_pos+offset]
  243. # here, but to make it easier on eyes, we use len and
  244. # rely on the fact that Zsh simply handles that. The
  245. # length of proc_buf is len-start_pos+offset because
  246. # we're chopping it to match current start_pos, so its
  247. # length matches the previous value of start_pos.
  248. #
  249. # Why [,-1] is slower than [,length] isn't clear.
  250. proc_buf="${proc_buf[offset + $#arg + 1,len]}"
  251. if [[ -n ${interactive_comments+'set'} && $arg[1] == $histchars[3] ]]; then
  252. if [[ $this_word == *(':regular:'|':start:')* ]]; then
  253. style=comment
  254. else
  255. style=unknown-token # prematurely terminated
  256. fi
  257. _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
  258. already_added=1
  259. continue
  260. fi
  261. # Parse the sudo command line
  262. if (( ! in_redirection )); then
  263. if [[ $this_word == *':sudo_opt:'* ]]; then
  264. case "$arg" in
  265. # Flag that requires an argument
  266. '-'[Cgprtu]) this_word=${this_word//:start:/};
  267. next_word=':sudo_arg:';;
  268. # This prevents misbehavior with sudo -u -otherargument
  269. '-'*) this_word=${this_word//:start:/};
  270. next_word+=':start:';
  271. next_word+=':sudo_opt:';;
  272. *) ;;
  273. esac
  274. elif [[ $this_word == *':sudo_arg:'* ]]; then
  275. next_word+=':sudo_opt:'
  276. next_word+=':start:'
  277. fi
  278. fi
  279. if [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word
  280. if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then
  281. style=precommand
  282. elif [[ "$arg" = "sudo" ]]; then
  283. style=precommand
  284. next_word=${next_word//:regular:/}
  285. next_word+=':sudo_opt:'
  286. next_word+=':start:'
  287. else
  288. _zsh_highlight_main_highlighter_expand_path $arg
  289. local expanded_arg="$REPLY"
  290. local res="$(_zsh_highlight_main__type ${expanded_arg})"
  291. () {
  292. # Special-case: command word is '$foo', like that, without braces or anything.
  293. #
  294. # That's not entirely correct --- if the parameter's value happens to be a reserved
  295. # word, the parameter expansion will be highlighted as a reserved word --- but that
  296. # incorrectness is outweighed by the usability improvement of permitting the use of
  297. # parameters that refer to commands, functions, and builtins.
  298. local -a match mbegin mend
  299. local MATCH; integer MBEGIN MEND
  300. if [[ $res == *': none' ]] && (( ${+parameters} )) &&
  301. [[ ${arg[1]} == \$ ]] && [[ ${arg:1} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+)$ ]]; then
  302. res="$(_zsh_highlight_main__type ${(P)MATCH})"
  303. fi
  304. }
  305. case $res in
  306. *': reserved') style=reserved-word;;
  307. *': suffix alias')
  308. style=suffix-alias
  309. ;;
  310. *': alias') () {
  311. integer insane_alias
  312. case $arg in
  313. # Issue #263: aliases with '=' on their LHS.
  314. #
  315. # There are three cases:
  316. #
  317. # - Unsupported, breaks 'alias -L' output, but invokable:
  318. ('='*) :;;
  319. # - Unsupported, not invokable:
  320. (*'='*) insane_alias=1;;
  321. # - The common case:
  322. (*) :;;
  323. esac
  324. if (( insane_alias )); then
  325. style=unknown-token
  326. else
  327. style=alias
  328. local aliased_command="${"$(alias -- $arg)"#*=}"
  329. [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$aliased_command"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg)
  330. fi
  331. }
  332. ;;
  333. *': builtin') style=builtin;;
  334. *': function') style=function;;
  335. *': command') style=command;;
  336. *': hashed') style=hashed-command;;
  337. *) if _zsh_highlight_main_highlighter_check_assign; then
  338. style=assign
  339. if [[ $arg[-1] == '(' ]]; then
  340. in_array_assignment=true
  341. else
  342. # assignment to a scalar parameter.
  343. # (For array assignments, the command doesn't start until the ")" token.)
  344. next_word+=':start:'
  345. fi
  346. elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then
  347. style=history-expansion
  348. elif [[ $arg[0,1] == $histchars[2,2] ]]; then
  349. style=history-expansion
  350. elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
  351. if [[ $this_word == *':regular:'* ]]; then
  352. # This highlights empty commands (semicolon follows nothing) as an error.
  353. # Zsh accepts them, though.
  354. style=commandseparator
  355. else
  356. style=unknown-token
  357. fi
  358. elif [[ $arg == (<0-9>|)(\<|\>)* ]]; then
  359. # A '<' or '>', possibly followed by a digit
  360. style=redirection
  361. (( in_redirection=2 ))
  362. elif [[ $arg[1,2] == '((' ]]; then
  363. # Arithmetic evaluation.
  364. #
  365. # Note: prior to zsh-5.1.1-52-g4bed2cf (workers/36669), the ${(z)...}
  366. # splitter would only output the '((' token if the matching '))' had
  367. # been typed. Therefore, under those versions of zsh, BUFFER="(( 42"
  368. # would be highlighted as an error until the matching "))" are typed.
  369. #
  370. # We highlight just the opening parentheses, as a reserved word; this
  371. # is how [[ ... ]] is highlighted, too.
  372. style=reserved-word
  373. _zsh_highlight_main_add_region_highlight $start_pos $((start_pos + 2)) $style
  374. already_added=1
  375. if [[ $arg[-2,-1] == '))' ]]; then
  376. _zsh_highlight_main_add_region_highlight $((end_pos - 2)) $end_pos $style
  377. already_added=1
  378. fi
  379. elif [[ $arg == '()' || $arg == $'\x28' ]]; then
  380. # anonymous function
  381. # subshell
  382. style=reserved-word
  383. else
  384. if _zsh_highlight_main_highlighter_check_path; then
  385. style=path
  386. else
  387. style=unknown-token
  388. fi
  389. fi
  390. ;;
  391. esac
  392. fi
  393. else # $arg is a non-command word
  394. case $arg in
  395. $'\x29') # subshell or end of array assignment
  396. if $in_array_assignment; then
  397. style=assign
  398. in_array_assignment=false
  399. else
  400. style=reserved-word
  401. fi;;
  402. $'\x7d') style=reserved-word;; # block
  403. '--'*) style=double-hyphen-option;;
  404. '-'*) style=single-hyphen-option;;
  405. "'"*) style=single-quoted-argument;;
  406. '"'*) style=double-quoted-argument
  407. _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
  408. _zsh_highlight_main_highlighter_highlight_string
  409. already_added=1
  410. ;;
  411. \$\'*) style=dollar-quoted-argument
  412. _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
  413. _zsh_highlight_main_highlighter_highlight_dollar_string
  414. already_added=1
  415. ;;
  416. '`'*) style=back-quoted-argument;;
  417. [*?]*|*[^\\][*?]*)
  418. $highlight_glob && style=globbing || style=default;;
  419. *) if false; then
  420. elif [[ $arg[0,1] = $histchars[0,1] ]] && (( $#arg[0,2] == 2 )); then
  421. style=history-expansion
  422. elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
  423. if [[ $this_word == *':regular:'* ]]; then
  424. style=commandseparator
  425. else
  426. style=unknown-token
  427. fi
  428. elif [[ $arg == (<0-9>|)(\<|\>)* ]]; then
  429. style=redirection
  430. (( in_redirection=2 ))
  431. else
  432. if _zsh_highlight_main_highlighter_check_path; then
  433. style=path
  434. else
  435. style=default
  436. fi
  437. fi
  438. ;;
  439. esac
  440. fi
  441. # if a style_override was set (eg in _zsh_highlight_main_highlighter_check_path), use it
  442. [[ -n $style_override ]] && style=$style_override
  443. (( already_added )) || _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
  444. if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
  445. next_word=':start:'
  446. highlight_glob=true
  447. elif
  448. [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} && $this_word == *':start:'* ]] ||
  449. [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && $this_word == *':start:'* ]]; then
  450. next_word=':start:'
  451. elif [[ $arg == "repeat" && $this_word == *':start:'* ]]; then
  452. # skip the repeat-count word
  453. in_redirection=2
  454. # The redirection mechanism assumes $this_word describes the word
  455. # following the redirection. Make it so.
  456. #
  457. # The repeat-count word will be handled like a redirection target.
  458. this_word=':start:'
  459. fi
  460. start_pos=$end_pos
  461. (( in_redirection == 0 )) && this_word=$next_word
  462. done
  463. }
  464. # Check if $arg is variable assignment
  465. _zsh_highlight_main_highlighter_check_assign()
  466. {
  467. setopt localoptions extended_glob
  468. [[ $arg == [[:alpha:]_][[:alnum:]_]#(|\[*\])(|[+])=* ]]
  469. }
  470. # Check if $arg is a path.
  471. _zsh_highlight_main_highlighter_check_path()
  472. {
  473. _zsh_highlight_main_highlighter_expand_path $arg;
  474. local expanded_path="$REPLY"
  475. [[ -z $expanded_path ]] && return 1
  476. [[ -e $expanded_path ]] && return 0
  477. # Search the path in CDPATH
  478. local cdpath_dir
  479. for cdpath_dir in $cdpath ; do
  480. [[ -e "$cdpath_dir/$expanded_path" ]] && return 0
  481. done
  482. # If dirname($arg) doesn't exist, neither does $arg.
  483. [[ ! -e ${expanded_path:h} ]] && return 1
  484. # If this word ends the buffer, check if it's the prefix of a valid path.
  485. if [[ ${BUFFER[1]} != "-" && ${#BUFFER} == $end_pos ]] &&
  486. [[ $WIDGET != accept-* ]]; then
  487. local -a tmp
  488. tmp=( ${expanded_path}*(N) )
  489. (( $#tmp > 0 )) && style_override=path_prefix && return 0
  490. fi
  491. # It's not a path.
  492. return 1
  493. }
  494. # Highlight special chars inside double-quoted strings
  495. _zsh_highlight_main_highlighter_highlight_string()
  496. {
  497. setopt localoptions noksharrays
  498. local -a match mbegin mend
  499. local MATCH; integer MBEGIN MEND
  500. local i j k style
  501. # Starting quote is at 1, so start parsing at offset 2 in the string.
  502. for (( i = 2 ; i < end_pos - start_pos ; i += 1 )) ; do
  503. (( j = i + start_pos - 1 ))
  504. (( k = j + 1 ))
  505. case "$arg[$i]" in
  506. '$' ) style=dollar-double-quoted-argument
  507. # Look for an alphanumeric parameter name.
  508. if [[ ${arg:$i} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+) ]] ; then
  509. (( k += $#MATCH )) # highlight the parameter name
  510. (( i += $#MATCH )) # skip past it
  511. elif [[ ${arg:$i} =~ ^[{]([A-Za-z_][A-Za-z0-9_]*|[0-9]+)[}] ]] ; then
  512. (( k += $#MATCH )) # highlight the parameter name and braces
  513. (( i += $#MATCH )) # skip past it
  514. else
  515. continue
  516. fi
  517. ;;
  518. "\\") style=back-double-quoted-argument
  519. if [[ \\\`\"\$ == *$arg[$i+1]* ]]; then
  520. (( k += 1 )) # Color following char too.
  521. (( i += 1 )) # Skip parsing the escaped char.
  522. else
  523. continue
  524. fi
  525. ;;
  526. *) continue ;;
  527. esac
  528. _zsh_highlight_main_add_region_highlight $j $k $style
  529. done
  530. }
  531. # Highlight special chars inside dollar-quoted strings
  532. _zsh_highlight_main_highlighter_highlight_dollar_string()
  533. {
  534. setopt localoptions noksharrays
  535. local -a match mbegin mend
  536. local MATCH; integer MBEGIN MEND
  537. local i j k style
  538. local AA
  539. integer c
  540. # Starting dollar-quote is at 1:2, so start parsing at offset 3 in the string.
  541. for (( i = 3 ; i < end_pos - start_pos ; i += 1 )) ; do
  542. (( j = i + start_pos - 1 ))
  543. (( k = j + 1 ))
  544. case "$arg[$i]" in
  545. "\\") style=back-dollar-quoted-argument
  546. for (( c = i + 1 ; c <= end_pos - start_pos ; c += 1 )); do
  547. [[ "$arg[$c]" != ([0-9xXuUa-fA-F]) ]] && break
  548. done
  549. AA=$arg[$i+1,$c-1]
  550. # Matching for HEX and OCT values like \0xA6, \xA6 or \012
  551. if [[ "$AA" =~ "^(x|X)[0-9a-fA-F]{1,2}"
  552. || "$AA" =~ "^[0-7]{1,3}"
  553. || "$AA" =~ "^u[0-9a-fA-F]{1,4}"
  554. || "$AA" =~ "^U[0-9a-fA-F]{1,8}"
  555. ]]; then
  556. (( k += $#MATCH ))
  557. (( i += $#MATCH ))
  558. else
  559. if (( $#arg > $i+1 )) && [[ $arg[$i+1] == [xXuU] ]]; then
  560. # \x not followed by hex digits is probably an error
  561. style=unknown-token
  562. fi
  563. (( k += 1 )) # Color following char too.
  564. (( i += 1 )) # Skip parsing the escaped char.
  565. fi
  566. ;;
  567. *) continue ;;
  568. esac
  569. _zsh_highlight_main_add_region_highlight $j $k $style
  570. done
  571. }
  572. # Called with a single positional argument.
  573. # Perform filename expansion (tilde expansion) on the argument and set $REPLY to the expanded value.
  574. #
  575. # Does not perform filename generation (globbing).
  576. _zsh_highlight_main_highlighter_expand_path()
  577. {
  578. (( $# == 1 )) || echo "zsh-syntax-highlighting: BUG: _zsh_highlight_main_highlighter_expand_path: called without argument" >&2
  579. # The $~1 syntax normally performs filename generation, but not when it's on the right-hand side of ${x:=y}.
  580. setopt localoptions nonomatch
  581. unset REPLY
  582. : ${REPLY:=${(Q)~1}}
  583. }