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.

96 lines
4.0 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. #
  4. # Outputs (under prompt) result of query done with `which', `type -w',
  5. # `whence -v', `whereis', `whatis'.
  6. #
  7. # $1 - 0 or 1, denoting if it's first call to the chroma, or following one
  8. #
  9. # $2 - the current token, also accessible by $__arg from the above scope -
  10. # basically a private copy of $__arg; the token can be eg.: "grep"
  11. #
  12. # $3 - a private copy of $_start_pos, i.e. the position of the token in the
  13. # command line buffer, used to add region_highlight entry (see man),
  14. # because Zsh colorizes by *ranges* in command line buffer
  15. #
  16. # $4 - a private copy of $_end_pos from the above scope
  17. #
  18. (( next_word = 2 | 8192 ))
  19. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  20. local __style __output __chars
  21. integer __idx1 __idx2
  22. local -a __results
  23. # First call, i.e. command starts, i.e. "grep" token etc.
  24. (( __first_call )) && {
  25. FAST_HIGHLIGHT[chroma-which-counter]=0
  26. FAST_HIGHLIGHT[chroma-which-counter-all]=1
  27. FAST_HIGHLIGHT[chroma-which-message]=""
  28. FAST_HIGHLIGHT[chroma-which-skip-two]=0
  29. __style=${FAST_THEME_NAME}command
  30. __output=""
  31. # Following call (not first one).
  32. } || {
  33. (( FAST_HIGHLIGHT[chroma-which-counter-all] += 1, __idx2 = FAST_HIGHLIGHT[chroma-which-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 (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  38. return 1
  39. fi
  40. if [[ "$__wrd" = -* ]]; then
  41. # Detected option, add style for it.
  42. [[ "$__wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  43. __style=${FAST_THEME_NAME}single-hyphen-option
  44. if [[ "$__wrd" = "-x" ]]; then
  45. FAST_HIGHLIGHT[chroma-which-skip-two]=1
  46. fi
  47. else
  48. # Count non-option tokens.
  49. if (( FAST_HIGHLIGHT[chroma-which-skip-two] )); then
  50. FAST_HIGHLIGHT[chroma-which-skip-two]=0
  51. else
  52. (( FAST_HIGHLIGHT[chroma-which-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-which-counter] ))
  53. if [[ "$__idx1" -eq 1 ]]; then
  54. __chars="{"
  55. __output="$(command which "$__wrd" 2>/dev/null)"
  56. FAST_HIGHLIGHT[chroma-which-message]+=$'\n'"command which: $__output"
  57. __output="$(builtin which "$__wrd" 2>/dev/null)"
  58. FAST_HIGHLIGHT[chroma-which-message]+=$'\n'"builtin which: ${${${${__output[1,100]}//$'\n'/;}//$'\t'/ }//$__chars;/$__chars}${__output[101,101]:+...}"
  59. __output="$(builtin type -w "$__wrd" 2>/dev/null)"
  60. FAST_HIGHLIGHT[chroma-which-message]+=$'\n'"type -w: $__output"
  61. __output="$(builtin whence -v "$__wrd" 2>/dev/null)"
  62. FAST_HIGHLIGHT[chroma-which-message]+=$'\n'"whence -v: $__output"
  63. __output="$(command whereis "$__wrd" 2>/dev/null)"
  64. FAST_HIGHLIGHT[chroma-which-message]+=$'\n'"whereis: $__output"
  65. __output="$(command whatis "$__wrd" 2>/dev/null)"
  66. __output="${${__output%%$'\n'*}//[[:blank:]]##/ }"
  67. FAST_HIGHLIGHT[chroma-which-message]+=$'\n'"whatis: $__output"
  68. fi
  69. fi
  70. fi
  71. if (( ${#${(z)BUFFER}} <= FAST_HIGHLIGHT[chroma-which-counter-all] )); then
  72. [[ -n "${FAST_HIGHLIGHT[chroma-which-message]}" ]] && zle -M "${FAST_HIGHLIGHT[chroma-which-message]#$'\n'}"
  73. fi
  74. }
  75. # Add region_highlight entry (via `reply' array).
  76. #
  77. # This is a common place of adding such entry, but any above code
  78. # can do it itself and skip setting __style to disable this code.
  79. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  80. # We aren't passing-through (no return 1 occured), do obligatory things ourselves.
  81. (( this_word = next_word ))
  82. _start_pos=$_end_pos
  83. return 0
  84. # vim:ft=zsh:et:sw=4