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.

138 lines
6.2 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # -------------------------------------------------------------------------------------------------
  3. # Copyright (c) 2018 Sebastian Gniazdowski
  4. # Copyright (C) 2019 by Philippe Troin (F-i-f on GitHub)
  5. # All rights reserved.
  6. #
  7. # The only licensing for this file follows.
  8. #
  9. # Redistribution and use in source and binary forms, with or without modification, are permitted
  10. # provided that the following conditions are met:
  11. #
  12. # * Redistributions of source code must retain the above copyright notice, this list of conditions
  13. # and the following disclaimer.
  14. # * Redistributions in binary form must reproduce the above copyright notice, this list of
  15. # conditions and the following disclaimer in the documentation and/or other materials provided
  16. # with the distribution.
  17. # * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
  18. # may be used to endorse or promote products derived from this software without specific prior
  19. # written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  22. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  23. # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  24. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  28. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. # -------------------------------------------------------------------------------------------------
  30. setopt local_options extendedglob warn_create_global typeset_silent
  31. # Keep chroma-takever state meaning: until ;, handle highlighting via chroma.
  32. # So the below 8192 assignment takes care that next token will be routed to chroma.
  33. (( next_word = 2 | 8192 ))
  34. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  35. local __style option_start=0 option_end=0 number_start=0 number_end=0
  36. local -a match mbegin mend
  37. (( __first_call )) && {
  38. # Called for the first time - new command.
  39. # FAST_HIGHLIGHT is used because it survives between calls, and
  40. # allows to use a single global hash only, instead of multiple
  41. # global string variables.
  42. FAST_HIGHLIGHT[nice-arg-count]=0
  43. FAST_HIGHLIGHT[nice-increment-argument]=0
  44. # Set style for region_highlight entry. It is used below in
  45. # '[[ -n "$__style" ]] ...' line, which adds highlight entry,
  46. # like "10 12 fg=green", through `reply' array.
  47. #
  48. # Could check if command `example' exists and set `unknown-token'
  49. # style instead of `command'
  50. __style=${FAST_THEME_NAME}precommand
  51. } || {
  52. # Following call, i.e. not the first one
  53. # Check if chroma should end – test if token is of type
  54. # "starts new command", if so pass-through – chroma ends
  55. [[ "$__arg_type" = 3 ]] && return 2
  56. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  57. return 1
  58. fi
  59. if (( FAST_HIGHLIGHT[nice-increment-argument] )); then
  60. (( FAST_HIGHLIGHT[nice-increment-argument] = 0 ))
  61. [[ $__wrd = (-|+|)[0-9]## ]] \
  62. && __style=${FAST_THEME_NAME}mathnum \
  63. || __style=${FAST_THEME_NAME}incorrect-subtle
  64. else
  65. case $__wrd in
  66. -(-|+|)[0-9]##)
  67. (( option_start = __start_pos-${#PREBUFFER} ,
  68. option_end = option_start+1 ,
  69. number_start = option_end ,
  70. number_end = __end_pos-${#PREBUFFER} ))
  71. option_style=${FAST_THEME_NAME}single-hyphen-option
  72. ;;
  73. (#b)(--adjustment)(=(-|+|)[0-9]#|))
  74. (( option_start = __start_pos-${#PREBUFFER} ,
  75. option_end = option_start+mend[1] ))
  76. option_style=${FAST_THEME_NAME}double-hyphen-option
  77. [[ -z $match[2] ]] \
  78. && (( FAST_HIGHLIGHT[nice-increment-argument] = 1 )) \
  79. || (( option_end += 1 ,
  80. number_start = option_start+mbegin[2]-mbegin[1]+1 ,
  81. number_end = __end_pos-${#PREBUFFER} ))
  82. ;;
  83. -n)
  84. __style=${FAST_THEME_NAME}double-hyphen-option
  85. FAST_HIGHLIGHT[nice-increment-argument]=1
  86. ;;
  87. --*)
  88. __style=${FAST_THEME_NAME}double-hyphen-option
  89. ;;
  90. -*)
  91. __style=${FAST_THEME_NAME}single-hyphen-option
  92. ;;
  93. *)
  94. this_word=1
  95. next_word=2
  96. return 1
  97. ;;
  98. esac
  99. (( option_start > 0 && option_end )) \
  100. && reply+=("$option_start $option_end ${FAST_HIGHLIGHT_STYLES[$option_style]}")
  101. (( number_start > 0 && number_end )) \
  102. && reply+=("$number_start $number_end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}mathnum]}")
  103. fi
  104. }
  105. # Add region_highlight entry (via `reply' array).
  106. # If 1 will be added to __start_pos, this will highlight "oken".
  107. # If 1 will be subtracted from __end_pos, this will highlight "toke".
  108. # $PREBUFFER is for specific situations when users does command \<ENTER>
  109. # i.e. when multi-line command using backslash is entered.
  110. #
  111. # This is a common place of adding such entry, but any above code can do
  112. # it itself (and it does in other chromas) and skip setting __style to
  113. # this way disable this code.
  114. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  115. # We aren't passing-through, do obligatory things ourselves.
  116. # _start_pos=$_end_pos advainces pointers in command line buffer.
  117. #
  118. # To pass through means to `return 1'. The highlighting of
  119. # this single token is then done by fast-syntax-highlighting's
  120. # main code and chroma doesn't have to do anything.
  121. (( this_word = next_word ))
  122. _start_pos=$_end_pos
  123. return 0
  124. # vim:ft=zsh:et:sw=4