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.

105 lines
4.4 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. #
  4. # Chroma function for command `make'.
  5. #
  6. # $1 - 0 or 1, denoting if it's first call to the chroma, or following one
  7. # $2 - the current token, also accessible by $__arg from the above scope -
  8. # basically a private copy of $__arg
  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. # $4 - a private copy of $_end_pos from the above scope
  13. #
  14. (( next_word = 2 | 8192 ))
  15. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  16. local __style
  17. integer __idx1 __idx2
  18. local -a __lines_list reply2
  19. (( __first_call )) && {
  20. # Called for the first time - new command.
  21. # FAST_HIGHLIGHT is used because it survives between calls, and
  22. # allows to use a single global hash only, instead of multiple
  23. # global variables.
  24. FAST_HIGHLIGHT[chroma-make-counter]=0
  25. FAST_HIGHLIGHT[chroma-make-skip-two]=0
  26. FAST_HIGHLIGHT[chroma-make-custom-dir]="./"
  27. FAST_HIGHLIGHT[chroma-make-custom-file]="Makefile"
  28. FAST_HIGHLIGHT[chroma-make-got-custom-dir-opt]=0
  29. FAST_HIGHLIGHT[chroma-make-got-custom-file-opt]=0
  30. return 1
  31. } || {
  32. # Following call, i.e. not the first one.
  33. # Check if chroma should end – test if token is of type
  34. # "starts new command", if so pass-through – chroma ends
  35. [[ "$__arg_type" = 3 ]] && return 2
  36. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  37. return 1
  38. fi
  39. if [[ "$__wrd" = -* || "$__wrd" = *=* ]]; then
  40. [[ "$__wrd" = *=* ]] && {
  41. __style=${FAST_THEME_NAME}variable
  42. } || {
  43. __style=${FAST_THEME_NAME}${${${__wrd:#--*}:+single-hyphen-option}:-double-hyphen-option}
  44. }
  45. if [[ "$__wrd" = (-I|-o|-W) ]]; then
  46. FAST_HIGHLIGHT[chroma-make-skip-two]=1
  47. elif [[ "$__wrd" = "-C" ]]; then
  48. FAST_HIGHLIGHT[chroma-make-got-custom-dir-opt]=1
  49. elif [[ "$__wrd" = "-f" ]]; then
  50. FAST_HIGHLIGHT[chroma-make-got-custom-file-opt]=1
  51. fi
  52. else
  53. if (( FAST_HIGHLIGHT[chroma-make-skip-two] )); then
  54. FAST_HIGHLIGHT[chroma-make-skip-two]=0
  55. elif (( FAST_HIGHLIGHT[chroma-make-got-custom-dir-opt] )); then
  56. FAST_HIGHLIGHT[chroma-make-got-custom-dir-opt]=0
  57. FAST_HIGHLIGHT[chroma-make-custom-dir]="$__wrd"
  58. elif (( FAST_HIGHLIGHT[chroma-make-got-custom-file-opt] )); then
  59. FAST_HIGHLIGHT[chroma-make-got-custom-file-opt]=0
  60. FAST_HIGHLIGHT[chroma-make-custom-file]="$__wrd"
  61. else
  62. # Count non-option tokens.
  63. (( FAST_HIGHLIGHT[chroma-make-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-make-counter] ))
  64. if (( FAST_HIGHLIGHT[chroma-make-counter] == 1 )); then
  65. __wrd="${__wrd//\`/x}"
  66. __wrd="${(Q)__wrd}"
  67. if [[ -f "${FAST_HIGHLIGHT[chroma-make-custom-dir]%/}/${FAST_HIGHLIGHT[chroma-make-custom-file]}" ]] && \
  68. .fast-make-targets < "${FAST_HIGHLIGHT[chroma-make-custom-dir]%/}/${FAST_HIGHLIGHT[chroma-make-custom-file]}"
  69. then
  70. if [[ "${reply2[(r)$__wrd]}" ]]; then
  71. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}correct-subtle]}")
  72. else
  73. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}")
  74. fi
  75. fi
  76. else
  77. # Pass-through to the big-loop outside
  78. return 1
  79. fi
  80. fi
  81. fi
  82. }
  83. # Add region_highlight entry (via `reply' array)
  84. #
  85. # This is a common place of adding such entry, but any above
  86. # code can do it itself (and it does) and skip setting __style
  87. # to disable this code.
  88. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  89. # We aren't passing-through, do obligatory things ourselves
  90. (( this_word = next_word ))
  91. _start_pos=$_end_pos
  92. return 0
  93. # vim:ft=zsh:et:sw=4