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.

117 lines
5.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[ionice-option-argument]=0
  43. # Set style for region_highlight entry. It is used below in
  44. # '[[ -n "$__style" ]] ...' line, which adds highlight entry,
  45. # like "10 12 fg=green", through `reply' array.
  46. #
  47. # Could check if command `example' exists and set `unknown-token'
  48. # style instead of `command'
  49. __style=${FAST_THEME_NAME}precommand
  50. } || {
  51. # Following call, i.e. not the first one
  52. # Check if chroma should end – test if token is of type
  53. # "starts new command", if so pass-through – chroma ends
  54. [[ "$__arg_type" = 3 ]] && return 2
  55. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  56. return 1
  57. fi
  58. if (( FAST_HIGHLIGHT[ionice-option-argument] )); then
  59. (( FAST_HIGHLIGHT[ionice-option-argument] = 0 ))
  60. [[ $__wrd == [0-9]# ]] && __style=${FAST_THEME_NAME}mathnum || __style=${FAST_THEME_NAME}incorrect-subtle
  61. else
  62. case $__wrd in
  63. --(class(data|)|(u|p(g|))id))
  64. __style=${FAST_THEME_NAME}double-hyphen-option
  65. FAST_HIGHLIGHT[ionice-option-argument]=1
  66. ;;
  67. -[cnpPu])
  68. __style=${FAST_THEME_NAME}single-hyphen-option
  69. FAST_HIGHLIGHT[ionice-option-argument]=1
  70. ;;
  71. --*)
  72. __style=${FAST_THEME_NAME}double-hyphen-option
  73. ;;
  74. -*)
  75. __style=${FAST_THEME_NAME}single-hyphen-option
  76. ;;
  77. *)
  78. this_word=1
  79. next_word=2
  80. return 1
  81. ;;
  82. esac
  83. fi
  84. }
  85. # Add region_highlight entry (via `reply' array).
  86. # If 1 will be added to __start_pos, this will highlight "oken".
  87. # If 1 will be subtracted from __end_pos, this will highlight "toke".
  88. # $PREBUFFER is for specific situations when users does command \<ENTER>
  89. # i.e. when multi-line command using backslash is entered.
  90. #
  91. # This is a common place of adding such entry, but any above code can do
  92. # it itself (and it does in other chromas) and skip setting __style to
  93. # this way disable this code.
  94. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  95. # We aren't passing-through, do obligatory things ourselves.
  96. # _start_pos=$_end_pos advainces pointers in command line buffer.
  97. #
  98. # To pass through means to `return 1'. The highlighting of
  99. # this single token is then done by fast-syntax-highlighting's
  100. # main code and chroma doesn't have to do anything.
  101. (( this_word = next_word ))
  102. _start_pos=$_end_pos
  103. return 0
  104. # vim:ft=zsh:et:sw=4