You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.7 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. #
  4. # $1 - 0 or 1, denoting if it's first call to the chroma, or following one
  5. #
  6. # $2 - the current token, also accessible by $__arg from the above scope -
  7. # basically a private copy of $__arg; the token can be eg.: "grep"
  8. #
  9. # $3 - a private copy of $_start_pos, i.e. the position of the token in the
  10. # command line buffer, used to add region_highlight entry (see man),
  11. # because Zsh colorizes by *ranges* in command line buffer
  12. #
  13. # $4 - a private copy of $_end_pos from the above scope
  14. #
  15. (( next_word = 2 | 8192 ))
  16. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  17. local __style __chars
  18. integer __idx1 __idx2
  19. # First call, i.e. command starts, i.e. "grep" token etc.
  20. (( __first_call )) && {
  21. FAST_HIGHLIGHT[chroma-grep-counter]=0
  22. return 1
  23. } || {
  24. # Following call, i.e. not the first one.
  25. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  26. return 1
  27. fi
  28. # Check if chroma should end – test if token is of type
  29. # "starts new command", if so pass-through – chroma ends
  30. [[ "$__arg_type" = 3 ]] && return 2
  31. if [[ "$__wrd" = -* ]]; then
  32. # Detected option, add style for it.
  33. [[ "$__wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  34. __style=${FAST_THEME_NAME}single-hyphen-option
  35. else
  36. # Count non-option tokens.
  37. (( FAST_HIGHLIGHT[chroma-grep-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-grep-counter] ))
  38. # First non-option token is the pattern (regex), we will
  39. # highlight it.
  40. if (( FAST_HIGHLIGHT[chroma-grep-counter] == 1 )); then
  41. [[ "$__wrd" = \"* ]] && __style=${FAST_THEME_NAME}double-quoted-argument
  42. [[ "$__wrd" = \'* ]] && __style=${FAST_THEME_NAME}single-quoted-argument
  43. [[ "$__wrd" = \$\'* ]] && __style=${FAST_THEME_NAME}dollar-quoted-argument
  44. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  45. __style=""
  46. __chars="*+\\)([]^\$"
  47. __idx1=__start_pos
  48. __idx2=__start_pos
  49. while [[ "$__wrd" = (#b)[^$__chars]#([\\][\\])#((+|\*|\[|\]|\)|\(|\^|\$)|[\\](+|\*|\[|\]|\)|\(|\^|\$))(*) ]]; do
  50. if [[ -n "${match[3]}" ]]; then
  51. __idx1+=${mbegin[3]}-1
  52. __idx2=__idx1+${mend[3]}-${mbegin[3]}+1
  53. (( __start=__idx1-${#PREBUFFER}, __end=__idx2-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}for-loop-operator]}")
  54. __idx1=__idx2
  55. else
  56. __idx1+=${mbegin[5]}-1
  57. fi
  58. __wrd="${match[5]}"
  59. done
  60. elif (( FAST_HIGHLIGHT[chroma-grep-counter] == 2 )); then
  61. # Handle paths, etc. normally - just pass-through to the big
  62. # highlighter (the main FSH highlighter, used before chromas).
  63. return 1
  64. fi
  65. fi
  66. }
  67. # Add region_highlight entry (via `reply' array).
  68. #
  69. # This is a common place of adding such entry, but any above
  70. # code can do it itself (and it does) and skip setting __style
  71. # to disable this code.
  72. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  73. # We aren't passing-through (no return 1 occured), do obligatory things ourselves.
  74. (( this_word = next_word ))
  75. _start_pos=$_end_pos
  76. return 0
  77. # vim:ft=zsh:et:sw=4