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.

86 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. # Highlights the special sequences like "%s" in string passed to `printf'.
  5. #
  6. # $1 - 0 or 1, denoting if it's first call to the chroma, or following one
  7. #
  8. # $2 - the current token, also accessible by $__arg from the above scope -
  9. # basically a private copy of $__arg; the token can be eg.: "grep"
  10. #
  11. # $3 - a private copy of $_start_pos, i.e. the position of the token in the
  12. # command line buffer, used to add region_highlight entry (see man),
  13. # because Zsh colorizes by *ranges* in command line buffer
  14. #
  15. # $4 - a private copy of $_end_pos from the above scope
  16. #
  17. (( next_word = 2 | 8192 ))
  18. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  19. local __style __val
  20. integer __idx1 __idx2
  21. # First call, i.e. command starts, i.e. "grep" token etc.
  22. (( __first_call )) && {
  23. FAST_HIGHLIGHT[chroma-printf-counter]=0
  24. FAST_HIGHLIGHT[chroma-printf-counter-all]=1
  25. FAST_HIGHLIGHT[chroma-printf-message]=""
  26. FAST_HIGHLIGHT[chroma-printf-skip-two]=0
  27. return 1
  28. # Following call (not first one).
  29. } || {
  30. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  31. return 1
  32. fi
  33. (( FAST_HIGHLIGHT[chroma-printf-counter-all] += 1, __idx2 = FAST_HIGHLIGHT[chroma-printf-counter-all] ))
  34. # Check if chroma should end – test if token is of type
  35. # "starts new command", if so pass-through – chroma ends
  36. [[ "$__arg_type" = 3 ]] && return 2
  37. if [[ "$__wrd" = -* ]]; then
  38. if [[ "$__wrd" = "-v" ]]; then
  39. FAST_HIGHLIGHT[chroma-printf-skip-two]=1
  40. fi
  41. return 1
  42. else
  43. # Count non-option tokens.
  44. if (( FAST_HIGHLIGHT[chroma-printf-skip-two] )); then
  45. FAST_HIGHLIGHT[chroma-printf-skip-two]=0
  46. return 1
  47. else
  48. (( FAST_HIGHLIGHT[chroma-printf-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-printf-counter] ))
  49. if [[ "$__idx1" -eq 1 ]]; then
  50. [[ "$__wrd" = \"* ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) \
  51. && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}double-quoted-argument]}")
  52. [[ "$__wrd" = \'* ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) \
  53. && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}single-quoted-argument]}")
  54. FSH_LIST=() # use fsh_sy_h_append function to write to FSH_LIST
  55. : "${__wrd//(#m)\%[\#\+\ 0-]#[0-9]#([.][0-9]#)(#c0,1)[diouxXfFeEgGaAcsb]/$(( fsh_sy_h_append($MBEGIN, $MEND) ))}";
  56. for __val in "${FSH_LIST[@]}" ; do
  57. __idx1=$(( __start_pos + ${__val%%;;*} ))
  58. __idx2=__idx1+${__val##*;;}-${__val%%;;*}+1
  59. (( __start=__idx1-${#PREBUFFER}, __end=__idx2-${#PREBUFFER}-1, __start >= 0 )) && \
  60. reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}mathnum]}")
  61. done
  62. else
  63. return 1
  64. fi
  65. fi
  66. fi
  67. }
  68. # Add region_highlight entry (via `reply' array).
  69. #
  70. # This is a common place of adding such entry, but any above code
  71. # can do it itself and skip setting __style 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