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.

104 lines
4.7 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. #
  4. # Tracks autoload command - highlights function names if they exist somewhere
  5. # in $fpath. Also warns that the autoloaded function is already defined.
  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 __chars
  21. integer __idx1 __idx2
  22. local -a __results __deserialized __noshsplit
  23. # First call, i.e. command starts, i.e. "grep" token etc.
  24. (( __first_call )) && {
  25. FAST_HIGHLIGHT[chroma-autoload-counter]=0
  26. FAST_HIGHLIGHT[chroma-autoload-counter-all]=1
  27. FAST_HIGHLIGHT[chroma-autoload-message]=""
  28. #FAST_HIGHLIGHT[chroma-autoload-message-shown]=""
  29. [[ -z ${FAST_HIGHLIGHT[chroma-autoload-message-shown-at]} ]] && FAST_HIGHLIGHT[chroma-autoload-message-shown-at]=0
  30. FAST_HIGHLIGHT[chroma-autoload-elements]=""
  31. __style=${FAST_THEME_NAME}command
  32. } || {
  33. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  34. return 1
  35. fi
  36. (( FAST_HIGHLIGHT[chroma-autoload-counter-all] += 1, __idx2 = FAST_HIGHLIGHT[chroma-autoload-counter-all] ))
  37. # Following call, i.e. not the first one.
  38. # Check if chroma should end – test if token is of type
  39. # "starts new command", if so pass-through – chroma ends
  40. [[ "$__arg_type" = 3 ]] && return 2
  41. if [[ "$__wrd" = [-+]* ]]; then
  42. # Detected option, add style for it.
  43. [[ "$__wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  44. __style=${FAST_THEME_NAME}single-hyphen-option
  45. else
  46. # Count non-option tokens.
  47. (( FAST_HIGHLIGHT[chroma-autoload-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-autoload-counter] ))
  48. if [[ $__wrd != (\$|\"\$)* && $__wrd != (/|\"/|\'/)* && $__wrd != \`* ]]; then
  49. __results=( ${^fpath}/$__wrd(N) )
  50. __deserialized=( "${(Q@)${(z@)FAST_HIGHLIGHT[chroma-fpath_peq-elements]}}" )
  51. __results+=( ${^__deserialized}/$__wrd(N) )
  52. [[ "${#__results}" -gt 0 ]] && {
  53. __style=${FAST_THEME_NAME}correct-subtle
  54. __deserialized=( "${(Q@)${(z@)FAST_HIGHLIGHT[chroma-autoload-elements]}}" )
  55. [[ -z "${__deserialized[1]}" && ${#__deserialized} -eq 1 ]] && __deserialized=()
  56. # Cannot use ${abc:+"$abc"} trick with ${~...}, so handle most
  57. # cases of the possible shwordsplit through an additional array
  58. __noshsplit=( ${~__wrd} )
  59. __deserialized+=( "${(j: :)__noshsplit}" )
  60. FAST_HIGHLIGHT[chroma-autoload-elements]="${(j: :)${(q@)__deserialized}}"
  61. # Make the function defined for big-loop's *main-type mechanism
  62. __fast_highlight_main__command_type_cache[${(j: :)__noshsplit}]="function"
  63. } || __style=${FAST_THEME_NAME}incorrect-subtle
  64. fi
  65. if (( ${+functions[${(Q)__wrd}]} )); then
  66. FAST_HIGHLIGHT[chroma-autoload-message]+="Warning: Function ${(Q)__wrd} already defined (e.g. loaded)"$'\n'
  67. fi
  68. fi
  69. # Display only when processing last autoload argument
  70. if (( ${#${(z)BUFFER}} == FAST_HIGHLIGHT[chroma-autoload-counter-all] )); then
  71. # Display only if already shown message differs or when it timeouts
  72. if [[ ${FAST_HIGHLIGHT[chroma-autoload-message]} != ${FAST_HIGHLIGHT[chroma-autoload-message-shown]} ||
  73. $(( EPOCHSECONDS - FAST_HIGHLIGHT[chroma-autoload-message-shown-at] )) -gt 7
  74. ]]; then
  75. FAST_HIGHLIGHT[chroma-autoload-message-shown]=${FAST_HIGHLIGHT[chroma-autoload-message]}
  76. FAST_HIGHLIGHT[chroma-autoload-message-shown-at]=$EPOCHSECONDS
  77. zle -M "${FAST_HIGHLIGHT[chroma-autoload-message]}"
  78. fi
  79. fi
  80. }
  81. # Add region_highlight entry (via `reply' array).
  82. #
  83. # This is a common place of adding such entry, but any above code
  84. # can do it itself and skip setting __style to disable this code.
  85. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  86. # We aren't passing-through (no return 1 occured), do obligatory things ourselves.
  87. (( this_word = next_word ))
  88. _start_pos=$_end_pos
  89. return 0
  90. # vim:ft=zsh:et:sw=4