Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

135 рядки
4.6 KiB

  1. # Update ZLE buffer syntax highlighting.
  2. #
  3. # Invokes each highlighter that needs updating.
  4. # This function is supposed to be called whenever the ZLE state changes.
  5. _zsh_highlight_internal()
  6. {
  7. emulate -L zsh
  8. setopt localoptions warncreateglobal nobashrematch
  9. local REPLY # don't leak $REPLY into global scope
  10. # Do not highlight if there are more than 300 chars in the buffer. It's most
  11. # likely a pasted command or a huge list of files in that case..
  12. [[ -n ${ZSH_HIGHLIGHT_MAXLENGTH:-} ]] && [[ $#BUFFER -gt $ZSH_HIGHLIGHT_MAXLENGTH ]] && return
  13. # Do not highlight if there are pending inputs (copy/paste).
  14. [[ $PENDING -gt 0 ]] && return
  15. {
  16. local cache_place
  17. local -a region_highlight_copy
  18. # Select which highlighters in ZSH_HIGHLIGHT_HIGHLIGHTERS need to be invoked.
  19. local highlighter; for highlighter in $ZSH_HIGHLIGHT_HIGHLIGHTERS; do
  20. # eval cache place for current highlighter and prepare it
  21. cache_place="_zsh_highlight__highlighter_${highlighter}_cache"
  22. typeset -ga ${cache_place}
  23. # If highlighter needs to be invoked
  24. if ! type "_zsh_highlight_highlighter_${highlighter}_predicate" >&/dev/null; then
  25. echo "zsh-syntax-highlighting: warning: disabling the ${(qq)highlighter} highlighter as it has not been loaded" >&2
  26. # TODO: use ${(b)} rather than ${(q)} if supported
  27. ZSH_HIGHLIGHT_HIGHLIGHTERS=( ${ZSH_HIGHLIGHT_HIGHLIGHTERS:#${highlighter}} )
  28. elif "_zsh_highlight_highlighter_${highlighter}_predicate"; then
  29. # save a copy, and cleanup region_highlight
  30. region_highlight_copy=("${region_highlight[@]}")
  31. region_highlight=()
  32. # Execute highlighter and save result
  33. {
  34. "_zsh_highlight_highlighter_${highlighter}_paint"
  35. } always {
  36. : ${(AP)cache_place::="${region_highlight[@]}"}
  37. }
  38. # Restore saved region_highlight
  39. region_highlight=("${region_highlight_copy[@]}")
  40. fi
  41. # Use value form cache if any cached
  42. region_highlight+=("${(@P)cache_place}")
  43. done
  44. # Re-apply zle_highlight settings
  45. # region
  46. () {
  47. (( REGION_ACTIVE )) || return
  48. integer min max
  49. if (( MARK > CURSOR )) ; then
  50. min=$CURSOR max=$MARK
  51. else
  52. min=$MARK max=$CURSOR
  53. fi
  54. if (( REGION_ACTIVE == 1 )); then
  55. [[ $KEYMAP = vicmd ]] && (( max++ ))
  56. elif (( REGION_ACTIVE == 2 )); then
  57. local needle=$'\n'
  58. # CURSOR and MARK are 0 indexed between letters like region_highlight
  59. # Do not include the newline in the highlight
  60. (( min = ${BUFFER[(Ib:min:)$needle]} ))
  61. (( max = ${BUFFER[(ib:max:)$needle]} - 1 ))
  62. fi
  63. _zsh_highlight_apply_zle_highlight region standout "$min" "$max"
  64. }
  65. # yank / paste (zsh-5.1.1 and newer)
  66. (( $+YANK_ACTIVE )) && (( YANK_ACTIVE )) && _zsh_highlight_apply_zle_highlight paste standout "$YANK_START" "$YANK_END"
  67. # isearch
  68. (( $+ISEARCHMATCH_ACTIVE )) && (( ISEARCHMATCH_ACTIVE )) && _zsh_highlight_apply_zle_highlight isearch underline "$ISEARCHMATCH_START" "$ISEARCHMATCH_END"
  69. # suffix
  70. (( $+SUFFIX_ACTIVE )) && (( SUFFIX_ACTIVE )) && _zsh_highlight_apply_zle_highlight suffix bold "$SUFFIX_START" "$SUFFIX_END"
  71. } always {
  72. typeset -g _ZSH_HIGHLIGHT_PRIOR_BUFFER="$BUFFER"
  73. typeset -gi _ZSH_HIGHLIGHT_PRIOR_CURSOR=$CURSOR
  74. }
  75. }
  76. # Apply highlighting based on entries in the zle_highlight array.
  77. # This function takes four arguments:
  78. # 1. The exact entry (no patterns) in the zle_highlight array:
  79. # region, paste, isearch, or suffix
  80. # 2. The default highlighting that should be applied if the entry is unset
  81. # 3. and 4. Two integer values describing the beginning and end of the
  82. # range. The order does not matter.
  83. _zsh_highlight_apply_zle_highlight() {
  84. local entry="$1" default="$2"
  85. integer first="$3" second="$4"
  86. # read the relevant entry from zle_highlight
  87. #
  88. # ### In zsh≥5.0.8 we'd use ${(b)entry}, but we support older zsh's, so we don't
  89. # ### add (b). The only effect is on the failure mode for callers that violate
  90. # ### the precondition.
  91. local region="${zle_highlight[(r)${entry}:*]-}"
  92. if [[ -z "$region" ]]; then
  93. # entry not specified at all, use default value
  94. region=$default
  95. else
  96. # strip prefix
  97. region="${region#${entry}:}"
  98. # no highlighting when set to the empty string or to 'none'
  99. if [[ -z "$region" ]] || [[ "$region" == none ]]; then
  100. return
  101. fi
  102. fi
  103. integer start end
  104. if (( first < second )); then
  105. start=$first end=$second
  106. else
  107. start=$second end=$first
  108. fi
  109. region_highlight+=("$start $end $region, memo=zsh-syntax-highlighting")
  110. }
  111. _zsh_highlight_internal "$@"