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.

108 lines
4.8 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 __val __style2
  18. integer __idx1 __idx2
  19. # First call, i.e. command starts, i.e. "grep" token etc.
  20. (( __first_call )) && {
  21. FAST_HIGHLIGHT[chroma-awk-counter]=0
  22. FAST_HIGHLIGHT[chroma-awk-f-seen]=0
  23. return 1
  24. } || {
  25. # Following call, i.e. not the first one.
  26. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  27. return 1
  28. fi
  29. # Check if chroma should end – test if token is of type
  30. # "starts new command", if so pass-through – chroma ends
  31. [[ "$__arg_type" = 3 ]] && return 2
  32. if [[ "$__wrd" = -* ]]; then
  33. # Detected option, add style for it.
  34. [[ "$__wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  35. __style=${FAST_THEME_NAME}single-hyphen-option
  36. [[ "$__wrd" = "-f" ]] && FAST_HIGHLIGHT[chroma-awk-f-seen]=1
  37. else
  38. # Count non-option tokens.
  39. (( FAST_HIGHLIGHT[chroma-awk-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-awk-counter] ))
  40. # First non-option token is the pattern (regex), we will
  41. # highlight it.
  42. if (( FAST_HIGHLIGHT[chroma-awk-counter] == 1 && FAST_HIGHLIGHT[chroma-awk-f-seen] == 0 )); then
  43. if print -r -- "${(Q)__wrd}" | gawk --source 'BEGIN { exit } END { exit 0 }' -f - >/dev/null 2>&1; then
  44. __style2="${FAST_THEME_NAME}subtle-bg"
  45. else
  46. __style2="${FAST_THEME_NAME}incorrect-subtle"
  47. fi
  48. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
  49. reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style2]}")
  50. # Highlight keywords
  51. FSH_LIST=()
  52. : "${__wrd//(#m)(BEGIN|END|FIELDWIDTHS|RS|ARGC|ARGV|ENVIRON|NF|NR|IGNORECASE|FILENAME|if|then|else|while|toupper|tolower|function|print|sub)/$(( fsh_sy_h_append($MBEGIN, $MEND) ))}";
  53. for __val in "${FSH_LIST[@]}" ; do
  54. [[ ${__wrd[${__val%%;;*}]} = [a-zA-Z0-9_] || ${__wrd[${__val##*;;}+1]} = [a-zA-Z0-9_] ]] && continue
  55. __idx1=$(( __start_pos + ${__val%%;;*} ))
  56. __idx2=__idx1+${__val##*;;}-${__val%%;;*}+1
  57. (( __start=__idx1-${#PREBUFFER}, __end=__idx2-${#PREBUFFER}-1, __start >= 0 )) && \
  58. reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}reserved-word]},${FAST_HIGHLIGHT_STYLES[$__style2]}")
  59. done
  60. # Highlight regex characters
  61. __chars="*+\\)(\{\}[]^"
  62. __idx1=__start_pos
  63. __idx2=__start_pos
  64. while [[ "$__wrd" = (#b)[^$__chars]#([\\][\\])#((+|\*|\[|\]|\)|\(|\^|\}|\{)|[\\](+|\*|\[|\]|\)|\(|\^|\{|\}))(*) ]]; do
  65. if [[ -n "${match[3]}" ]]; then
  66. __idx1+=${mbegin[3]}-1
  67. __idx2=__idx1+${mend[3]}-${mbegin[3]}+1
  68. (( __start=__idx1-${#PREBUFFER}, __end=__idx2-${#PREBUFFER}, __start >= 0 )) && \
  69. reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}mathnum]},${FAST_HIGHLIGHT_STYLES[$__style2]}")
  70. __idx1=__idx2
  71. else
  72. __idx1+=${mbegin[5]}-1
  73. fi
  74. __wrd="${match[5]}"
  75. done
  76. elif (( FAST_HIGHLIGHT[chroma-awk-counter] >= 2 || FAST_HIGHLIGHT[chroma-awk-f-seen] == 1 )); then
  77. FAST_HIGHLIGHT[chroma-awk-f-seen]=0
  78. # Handle paths, etc. normally - just pass-through to the big
  79. # highlighter (the main FSH highlighter, used before chromas).
  80. return 1
  81. fi
  82. fi
  83. }
  84. # Add region_highlight entry (via `reply' array).
  85. #
  86. # This is a common place of adding such entry, but any above
  87. # code can do it itself (and it does) and skip setting __style
  88. # to disable this code.
  89. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  90. # We aren't passing-through (no return 1 occured), do obligatory things ourselves.
  91. (( this_word = next_word ))
  92. _start_pos=$_end_pos
  93. return 0
  94. # vim:ft=zsh:et:sw=4