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.

250 lines
13 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. →chroma/-subversion.ch/parse-revision() {
  31. setopt local_options extendedglob warn_create_global typeset_silent
  32. local __wrd="$1" __start_pos="$2" __end_pos="$3" __style __start __end
  33. case $__wrd in
  34. (r|)[0-9]##) __style=${FAST_THEME_NAME}mathnum ;;
  35. (HEAD|BASE|COMITTED|PREV)) __style=${FAST_THEME_NAME}correct-subtle ;;
  36. '{'[^}]##'}') __style=${FAST_THEME_NAME}subtle-bg ;;
  37. *) __style=${FAST_THEME_NAME}incorrect-subtle ;;
  38. esac
  39. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  40. }
  41. →chroma/-subversion.ch/parse-target() {
  42. setopt local_options extendedglob warn_create_global typeset_silent
  43. local __wrd="$1" __start_pos="$2" __end_pos="$3" __style __start __end
  44. if [[ $__wrd == *@[^/]# ]]
  45. then
  46. local place=${__wrd%@[^/]#}
  47. local rev=$__wrd[$(($#place+2)),$#__wrd]
  48. if [[ -e $place ]]; then
  49. local __style
  50. [[ -d $place ]] && __style="${FAST_THEME_NAME}path-to-dir" || __style="${FAST_THEME_NAME}path"
  51. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}-$#rev-1, __start >= 0 )) \
  52. && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  53. fi
  54. (( __start=__start_pos-${#PREBUFFER}+$#place, __end=__end_pos-${#PREBUFFER}-$#rev, __start >= 0 )) \
  55. && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}for-loop-separator]}")
  56. →chroma/-subversion.ch/parse-revision $rev $((__start_pos+$#place+1)) $__end_pos
  57. else
  58. return 1
  59. fi
  60. }
  61. setopt local_options extendedglob warn_create_global
  62. # Keep chroma-takever state meaning: until ;, handle highlighting via chroma.
  63. # So the below 8192 assignment takes care that next token will be routed to chroma.
  64. (( next_word = 2 | 8192 ))
  65. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  66. local __style
  67. integer __idx1 __idx2
  68. (( __first_call )) && {
  69. # Called for the first time - new command.
  70. # FAST_HIGHLIGHT is used because it survives between calls, and
  71. # allows to use a single global hash only, instead of multiple
  72. # global string variables.
  73. FAST_HIGHLIGHT[subversion-command]=$__wrd
  74. FAST_HIGHLIGHT[subversion-option-argument]=
  75. FAST_HIGHLIGHT[subversion-subcommand]=
  76. FAST_HIGHLIGHT[subversion-subcommand-arguments]=0
  77. # Set style for region_highlight entry. It is used below in
  78. # '[[ -n "$__style" ]] ...' line, which adds highlight entry,
  79. # like "10 12 fg=green", through `reply' array.
  80. #
  81. # Could check if command `example' exists and set `unknown-token'
  82. # style instead of `command'
  83. __style=${FAST_THEME_NAME}command
  84. } || {
  85. # Following call, i.e. not the first one
  86. # Check if chroma should end – test if token is of type
  87. # "starts new command", if so pass-through – chroma ends
  88. [[ "$__arg_type" = 3 ]] && return 2
  89. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  90. return 1
  91. fi
  92. if [[ "$__wrd" = -* ]]; then
  93. # Detected option, add style for it.
  94. [[ "$__wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  95. __style=${FAST_THEME_NAME}single-hyphen-option
  96. case $FAST_HIGHLIGHT[subversion-command]/$FAST_HIGHLIGHT[subversion-subcommand] in
  97. svn/)
  98. case $__wrd in
  99. --username|-u) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  100. --password|-p) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  101. --config-(dir|option)) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  102. esac
  103. ;;
  104. svn/?*)
  105. case $__wrd in
  106. --accept) FAST_HIGHLIGHT[subversion-option-argument]=accept;;
  107. --change|-c) FAST_HIGHLIGHT[subversion-option-argument]=revision;;
  108. --changelist|--cl) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  109. --(set-|)depth) FAST_HIGHLIGHT[subversion-option-argument]=depth;;
  110. --diff(3|)-cmd) FAST_HIGHLIGHT[subversion-option-argument]=cmd;;
  111. --editor-cmd) FAST_HIGHLIGHT[subversion-option-argument]=cmd;;
  112. --encoding) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  113. --file) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  114. --limit|-l) FAST_HIGHLIGHT[subversion-option-argument]=number;;
  115. --message|-m) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  116. --native-eol) FAST_HIGHLIGHT[subversion-option-argument]=eol;;
  117. --new|--old) FAST_HIGHLIGHT[subversion-option-argument]=target;;
  118. --revision|-r) FAST_HIGHLIGHT[subversion-option-argument]=revision-pair;;
  119. --show-revs) FAST_HIGHLIGHT[subversion-option-argument]=show-revs;;
  120. --strip) FAST_HIGHLIGHT[subversion-option-argument]=number;;
  121. --with-revprop) FAST_HIGHLIGHT[subversion-option-argument]=revprop;;
  122. esac
  123. ;;
  124. svnadmin/*)
  125. case $__wrd in
  126. --config-dir) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  127. --fs-type) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  128. --memory-cache-size|-M) FAST_HIGHLIGHT[subversion-option-argument]=number;;
  129. --parent-dir) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  130. --revision|-r) FAST_HIGHLIGHT[subversion-option-argument]=revision-pair;;
  131. esac
  132. ;;
  133. svndumpfilter/*)
  134. case $__wrd in
  135. --targets) FAST_HIGHLIGHT[subversion-option-argument]=any;;
  136. esac
  137. ;;
  138. esac
  139. elif [[ -n $FAST_HIGHLIGHT[subversion-option-argument] ]]; then
  140. case $FAST_HIGHLIGHT[subversion-option-argument] in
  141. any)
  142. FAST_HIGHLIGHT[subversion-option-argument]=
  143. return 1
  144. ;;
  145. accept)
  146. [[ $__wrd = (p(|ostpone)|e(|dit)|l(|aunch)|base|working|recommended|[mt][cf]|(mine|theirs)-(conflict|full)) ]] \
  147. && __style=${FAST_THEME_NAME}correct-subtle \
  148. || __style=${FAST_THEME_NAME}incorrect-subtle
  149. ;;
  150. depth)
  151. [[ $__wrd = (empty|files|immediates|infinity) ]] \
  152. && __style=${FAST_THEME_NAME}correct-subtle \
  153. || __style=${FAST_THEME_NAME}incorrect-subtle
  154. ;;
  155. number)
  156. [[ $__wrd = [0-9]## ]] \
  157. && __style=${FAST_THEME_NAME}mathnum \
  158. || __style=${FAST_THEME_NAME}incorrect-subtle
  159. ;;
  160. eol)
  161. [[ $__wrd = (CR(|LF)|LF) ]] \
  162. && __style=${FAST_THEME_NAME}correct-subtle \
  163. || __style=${FAST_THEME_NAME}incorrect-subtle
  164. ;;
  165. show-revs)
  166. [[ $__wrd = (merged|eligible) ]] \
  167. && __style=${FAST_THEME_NAME}correct-subtle \
  168. || __style=${FAST_THEME_NAME}incorrect-subtle
  169. ;;
  170. revision)
  171. →chroma/-subversion.ch/parse-revision $__wrd $__start_pos $__end_pos
  172. ;;
  173. revision-pair)
  174. local -a match mbegin mend
  175. if [[ $__wrd = (#b)(\{[^}]##\}|[^:]##)(:)(*) ]]; then
  176. →chroma/-subversion.ch/parse-revision $match[1] $__start_pos $(( __end_pos - ( mend[3]-mend[2] ) - 1 ))
  177. →chroma/-subversion.ch/parse-revision $match[3] $(( __start_pos + ( mbegin[3]-mbegin[1] ) )) $__end_pos
  178. (( __start=__start_pos-${#PREBUFFER}+(mbegin[2]-mbegin[1]), __end=__end_pos-${#PREBUFFER}-(mend[3]-mend[2]), __start >= 0 )) \
  179. && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}for-loop-separator]}")
  180. else
  181. →chroma/-subversion.ch/parse-revision $__wrd $__start_pos $__end_pos
  182. fi
  183. ;;
  184. target)
  185. →chroma/-subversion.ch/parse-target $__wrd $__start_pos $__end_pos || return $?
  186. ;;
  187. cmd)
  188. this_word=1
  189. return 1
  190. ;;
  191. esac
  192. FAST_HIGHLIGHT[subversion-option-argument]=
  193. elif [[ -z $FAST_HIGHLIGHT[subversion-subcommand] ]]
  194. then
  195. FAST_HIGHLIGHT[subversion-subcommand]=$__wrd
  196. local subcmds
  197. case $FAST_HIGHLIGHT[subversion-command] in
  198. svn) subcmds='(add|auth|blame|praise|annotate|ann|cat|changelist|cl|checkout|co|cleanup|commit|ci|copy|cp|delete|del|remove|rm|diff|di|export|help|\?|h|import|info|list|ls|lock|log|merge|mergeinfo|mkdir|move|mv|rename|ren|patch|propdel|pdel|pd|propedit|pedit|pe|propget|pget|pg|proplist|plist|pl|propset|pset|ps|relocate|resolve|resolved|revert|status|stat|st|switch|sw|unlock|update|up|upgrade|x-shelf-diff|x-shelf-drop|x-shelf-list|x-shelves|x-shelf-list-by-paths|x-shelf-log|x-shelf-save|x-shelve|x-unshelve)' ;;
  199. svnadmin) subcmds="(crashtest|create|delrevprop|deltify|dump|dump-revprops|freeze|help|\?|h|hotcopy|info|list-dblogs|list-unused-dblogs|load|load-revprops|lock|lslocks|lstxns|pack|recover|rmlocks|rmtxns|setlog|setrevprop|setuuid|unlock|upgrade|verify)";;
  200. svndumpfilter) subcmds='(include|exclude|help|\?)';;
  201. esac
  202. [[ $FAST_HIGHLIGHT[subversion-subcommand] = $~subcmds ]] \
  203. && __style=${FAST_THEME_NAME}subcommand \
  204. || __style=${FAST_THEME_NAME}incorrect-subtle
  205. FAST_HIGHLIGHT[subversion-subcommand-arguments]=0
  206. else
  207. (( FAST_HIGHLIGHT[subversion-subcommand-arguments]+=1 ))
  208. if [[ ( $FAST_HIGHLIGHT[subversion-subcommand] == (checkout|co|export|log|merge|switch|sw) && $FAST_HIGHLIGHT[subversion-subcommand-arguments] -eq 1 ) \
  209. || $FAST_HIGHLIGHT[subversion-subcommand] == (blame|praise|annotate|ann|cat|copy|cp|diff|info|list|ls|mergeinfo) ]]; then
  210. →chroma/-subversion.ch/parse-target $__wrd $__start_pos $__end_pos || return $?
  211. else
  212. return 1
  213. fi
  214. fi
  215. }
  216. # Add region_highlight entry (via `reply' array).
  217. # If 1 will be added to __start_pos, this will highlight "oken".
  218. # If 1 will be subtracted from __end_pos, this will highlight "toke".
  219. # $PREBUFFER is for specific situations when users does command \<ENTER>
  220. # i.e. when multi-line command using backslash is entered.
  221. #
  222. # This is a common place of adding such entry, but any above code can do
  223. # it itself (and it does in other chromas) and skip setting __style to
  224. # this way disable this code.
  225. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  226. # We aren't passing-through, do obligatory things ourselves.
  227. # _start_pos=$_end_pos advainces pointers in command line buffer.
  228. #
  229. # To pass through means to `return 1'. The highlighting of
  230. # this single token is then done by fast-syntax-highlighting's
  231. # main code and chroma doesn't have to do anything.
  232. (( this_word = next_word ))
  233. _start_pos=$_end_pos
  234. return 0
  235. # vim:ft=zsh:et:sw=4