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.

206 lines
8.6 KiB

  1. #!/usr/bin/env zsh
  2. # -------------------------------------------------------------------------------------------------
  3. # Copyright (c) 2010-2011 zsh-syntax-highlighting contributors
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without modification, are permitted
  7. # provided that the following conditions are met:
  8. #
  9. # * Redistributions of source code must retain the above copyright notice, this list of conditions
  10. # and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright notice, this list of
  12. # conditions and the following disclaimer in the documentation and/or other materials provided
  13. # with the distribution.
  14. # * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
  15. # may be used to endorse or promote products derived from this software without specific prior
  16. # written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  19. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  21. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  24. # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  25. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. # -------------------------------------------------------------------------------------------------
  27. # -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
  28. # vim: ft=zsh sw=2 ts=2 et
  29. # -------------------------------------------------------------------------------------------------
  30. # -------------------------------------------------------------------------------------------------
  31. # Core highlighting update system
  32. # -------------------------------------------------------------------------------------------------
  33. # Array declaring active highlighters names.
  34. typeset -ga ZSH_HIGHLIGHT_HIGHLIGHTERS
  35. # Update ZLE buffer syntax highlighting.
  36. #
  37. # Invokes each highlighter that needs updating.
  38. # This function is supposed to be called whenever the ZLE state changes.
  39. _zsh_highlight()
  40. {
  41. setopt localoptions nowarncreateglobal
  42. # Store the previous command return code to restore it whatever happens.
  43. local ret=$?
  44. # Do not highlight if there are more than 300 chars in the buffer. It's most
  45. # likely a pasted command or a huge list of files in that case..
  46. [[ -n $ZSH_HIGHLIGHT_MAXLENGTH ]] && [[ $#BUFFER -gt $ZSH_HIGHLIGHT_MAXLENGTH ]] && return $ret
  47. # Do not highlight if there are pending inputs (copy/paste).
  48. [[ $PENDING -gt 0 ]] && return $ret
  49. {
  50. local -a selected_highlighters
  51. local cache_place
  52. # Select which highlighters in ZSH_HIGHLIGHT_HIGHLIGHTERS need to be invoked.
  53. local highlighter; for highlighter in $ZSH_HIGHLIGHT_HIGHLIGHTERS; do
  54. # If highlighter needs to be invoked
  55. if "_zsh_highlight_${highlighter}_highlighter_predicate"; then
  56. # Mark the highlighter as selected for update.
  57. selected_highlighters+=($highlighter)
  58. # Remove what was stored in its cache from region_highlight.
  59. cache_place="_zsh_highlight_${highlighter}_highlighter_cache"
  60. [[ ${#${(P)cache_place}} -gt 0 ]] && region_highlight=(${region_highlight:#(${(P~j.|.)cache_place})})
  61. fi
  62. done
  63. # Invoke each selected highlighter and store the result in its cache.
  64. local -a region_highlight_copy
  65. for highlighter in $selected_highlighters; do
  66. cache_place="_zsh_highlight_${highlighter}_highlighter_cache"
  67. region_highlight_copy=($region_highlight)
  68. {
  69. "_zsh_highlight_${highlighter}_highlighter"
  70. } always {
  71. : ${(PA)cache_place::=${region_highlight:#(${(~j.|.)region_highlight_copy})}}
  72. }
  73. done
  74. } always {
  75. _ZSH_HIGHLIGHT_PRIOR_BUFFER=$BUFFER
  76. _ZSH_HIGHLIGHT_PRIOR_CURSOR=$CURSOR
  77. return $ret
  78. }
  79. }
  80. # -------------------------------------------------------------------------------------------------
  81. # API/utility functions for highlighters
  82. # -------------------------------------------------------------------------------------------------
  83. # Array used by highlighters to declare user overridable styles.
  84. typeset -gA ZSH_HIGHLIGHT_STYLES
  85. # Whether the command line buffer has been modified or not.
  86. #
  87. # Returns 0 if the buffer has changed since _zsh_highlight was last called.
  88. _zsh_highlight_buffer_modified()
  89. {
  90. [[ ${_ZSH_HIGHLIGHT_PRIOR_BUFFER:-} != $BUFFER ]]
  91. }
  92. # Whether the cursor has moved or not.
  93. #
  94. # Returns 0 if the cursor has moved since _zsh_highlight was last called.
  95. _zsh_highlight_cursor_moved()
  96. {
  97. [[ -n $CURSOR ]] && [[ -n $_ZSH_HIGHLIGHT_PRIOR_CURSOR ]] && (($_ZSH_HIGHLIGHT_PRIOR_CURSOR != $CURSOR))
  98. }
  99. # -------------------------------------------------------------------------------------------------
  100. # Setup functions
  101. # -------------------------------------------------------------------------------------------------
  102. # Rebind all ZLE widgets to make them invoke _zsh_highlights.
  103. _zsh_highlight_bind_widgets()
  104. {
  105. # Load ZSH module zsh/zleparameter, needed to override user defined widgets.
  106. zmodload zsh/zleparameter 2>/dev/null || {
  107. echo 'zsh-syntax-highlighting: failed loading zsh/zleparameter.' >&2
  108. return 1
  109. }
  110. # Override ZLE widgets to make them invoke _zsh_highlight.
  111. local cur_widget
  112. for cur_widget in ${${(f)"$(builtin zle -la)"}:#(.*|_*|orig-*|run-help|which-command|beep)}; do
  113. case $widgets[$cur_widget] in
  114. # Already rebound event: do nothing.
  115. user:$cur_widget|user:_zsh_highlight_widget_*);;
  116. # User defined widget: override and rebind old one with prefix "orig-".
  117. user:*) eval "zle -N orig-$cur_widget ${widgets[$cur_widget]#*:}; \
  118. _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget && _zsh_highlight }; \
  119. zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  120. # Completion widget: override and rebind old one with prefix "orig-".
  121. completion:*) eval "zle -C orig-$cur_widget ${${widgets[$cur_widget]#*:}/:/ }; \
  122. _zsh_highlight_widget_$cur_widget() { builtin zle orig-$cur_widget && _zsh_highlight }; \
  123. zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  124. # Builtin widget: override and make it call the builtin ".widget".
  125. builtin) eval "_zsh_highlight_widget_$cur_widget() { builtin zle .$cur_widget && _zsh_highlight }; \
  126. zle -N $cur_widget _zsh_highlight_widget_$cur_widget";;
  127. # Default: unhandled case.
  128. *) echo "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" >&2 ;;
  129. esac
  130. done
  131. }
  132. # Load highlighters from directory.
  133. #
  134. # Arguments:
  135. # 1) Path to the highlighters directory.
  136. _zsh_highlight_load_highlighters()
  137. {
  138. # Check the directory exists.
  139. [[ -d "$1" ]] || {
  140. echo "zsh-syntax-highlighting: highlighters directory '$1' not found." >&2
  141. return 1
  142. }
  143. # Load highlighters from highlighters directory and check they define required functions.
  144. local highlighter highlighter_dir
  145. for highlighter_dir ($1/*/); do
  146. highlighter="${highlighter_dir:t}"
  147. [[ -f "$highlighter_dir/${highlighter}-highlighter.zsh" ]] && {
  148. . "$highlighter_dir/${highlighter}-highlighter.zsh"
  149. type "_zsh_highlight_${highlighter}_highlighter" &> /dev/null &&
  150. type "_zsh_highlight_${highlighter}_highlighter_predicate" &> /dev/null || {
  151. echo "zsh-syntax-highlighting: '${highlighter}' highlighter should define both required functions '_zsh_highlight_${highlighter}_highlighter' and '_zsh_highlight_${highlighter}_highlighter_predicate' in '${highlighter_dir}/${highlighter}-highlighter.zsh'." >&2
  152. }
  153. }
  154. done
  155. }
  156. # -------------------------------------------------------------------------------------------------
  157. # Setup
  158. # -------------------------------------------------------------------------------------------------
  159. # Try binding widgets.
  160. _zsh_highlight_bind_widgets || {
  161. echo 'zsh-syntax-highlighting: failed binding ZLE widgets, exiting.' >&2
  162. return 1
  163. }
  164. # Resolve highlighters directory location.
  165. _zsh_highlight_load_highlighters "${ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR:-${0:h}/highlighters}" || {
  166. echo 'zsh-syntax-highlighting: failed loading highlighters, exiting.' >&2
  167. return 1
  168. }
  169. # Initialize the array of active highlighters if needed.
  170. [[ $#ZSH_HIGHLIGHT_HIGHLIGHTERS -eq 0 ]] && ZSH_HIGHLIGHT_HIGHLIGHTERS=(main) || true