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.

460 lines
29 KiB

  1. # Copyright (c) 2018 Sebastian Gniazdowski
  2. #
  3. # Main chroma function. It allows to create the command-dedicated chromas
  4. # (like -git.ch) through a definition provided by `chroma_def' array (read
  5. # from the upper scope).
  6. #
  7. # $1 - 0 or 1, denoting if it's first call to the chroma, or following one
  8. # $2 - the current token, also accessible by $__arg from the above scope -
  9. # basically a private copy of $__arg
  10. # $3 - a private copy of $_start_pos, i.e. the position of the token in the
  11. # command line buffer, used to add region_highlight entry (see man),
  12. # because Zsh colorizes by *ranges* in command line buffer
  13. # $4 - a private copy of $_end_pos from the above scope
  14. #
  15. (( next_word = 2 | 8192 ))
  16. →chroma/main-chroma-print() {
  17. (( FAST_HIGHLIGHT[DEBUG] )) && print "$@" >> /tmp/fsh-dbg
  18. }
  19. local __chroma_name="${1#\%}" __first_call="$2" __wrd="$3" __start_pos="$4" __end_pos="$5"
  20. # Not a well formed chroma name
  21. [[ -z "$__chroma_name" ]] && return 1
  22. # Load the fsh_{name-of-the-chroma}_chroma_def array
  23. (( !FAST_HIGHLIGHT[-${__chroma_name}.ch-chroma-def] )) && →chroma/-${__chroma_name}.ch
  24. →chroma/main-chroma-print -r -- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  25. →chroma/main-chroma-print -r -- @@@@@@@ local __chroma_name="${1#\%}" __first_call="$2" __wrd="$3" __start_pos="$4" __end_pos="$5" @@@@@@@
  26. local __style __entry __value __action __handler __tmp __svalue __hspaces=$'\t ' __nl=$'\n' __ch_def_name
  27. integer __idx1 __idx2 __start __end __ivalue __have_value=0
  28. local -a __lines_list __avalue
  29. local -A map
  30. map=( "#" "_H" "^" "_D" "*" "_S" )
  31. (( __start=_start_pos-__PBUFLEN, __end=_end_pos-__PBUFLEN ))
  32. # Handler that highlights the options
  33. →chroma/main-chroma-std-aopt-action() {
  34. integer _start="$2" _end="$3"
  35. local _scmd="$1" _wrd="$4"
  36. [[ "$_wrd" = (#b)(--[a-zA-Z0-9_-]##)=(*) ]] && {
  37. reply+=("$_start $(( _end - mend[2] + mbegin[2] - 1 )) ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}double-hyphen-option]}")
  38. } || {
  39. [[ "$_wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  40. __style=${FAST_THEME_NAME}single-hyphen-option
  41. }
  42. }
  43. # Handler that highlights the options' arguments
  44. →chroma/main-chroma-std-aopt-ARG-action() {
  45. integer _start="$2" _end="$3"
  46. local _scmd="$1" _wrd="$4"
  47. [[ "$_wrd" = (#b)(--[a-zA-Z0-9_-]##)=(*) ]] && {
  48. reply+=("$(( _start + 1 + mend[1] )) $_end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}optarg-${${${(M)match[2]:#<->}:+number}:-string}]}")
  49. } || __style=${FAST_THEME_NAME}optarg-${${${(M)_wrd:#(-|)<->}:+number}:-string}
  50. }
  51. # This handler also highlights explicit arguments, i.e. --opt=the-explicit-arg
  52. →chroma/main-chroma-std-aopt-SEMI-action() {
  53. integer _start="$2" _end="$3"
  54. local _scmd="$1" _wrd="$4"
  55. [[ "$_wrd" = (#b)(--[a-zA-Z0-9_-]##)=(*) ]] && {
  56. reply+=("$_start $(( _end - mend[2] + mbegin[2] - 1 )) ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}double-hyphen-option]}")
  57. reply+=("$(( _start + 1 + mend[1] )) $_end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}optarg-${${${(M)match[2]:#<->}:+number}:-string}]}")
  58. } || {
  59. [[ "$_wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  60. __style=${FAST_THEME_NAME}single-hyphen-option
  61. }
  62. }
  63. # A handler which verifies the token as an GIT url
  64. →chroma/main-chroma-std-verify-url() {
  65. setopt localoptions extendedglob
  66. local _wrd="$4"
  67. integer url_correct=0
  68. # Correct matches
  69. # Correct matches
  70. if [[ "$_wrd" = (#b)(git|http|https|ftp|ftps|file)://([a-zA-Z0-9._~-]##)(:[0-9]##)(#c0,1)(/([a-zA-Z0-9./_~:-]##))(#c0,1) ]]; then
  71. url_correct=1
  72. elif [[ "$_wrd" = (#b)rsync://([a-zA-Z0-9._~-]##)(/([a-zA-Z0-9./_~:-]##))(#c0,1) ]]; then
  73. url_correct=1
  74. elif [[ "$_wrd" = (#b)ssh://([a-zA-Z0-9._~-]##@)(#c0,1)([a-zA-Z0-9._~-]##)(:[0-9]##)(#c0,1)(/([a-zA-Z0-9./_~:-]##))(#c0,1) ]]; then
  75. url_correct=1
  76. elif [[ "$_wrd" = (#b)([a-zA-Z0-9._~-]##@)(#c0,1)([a-zA-Z0-9._~-]##):([a-zA-Z0-9./_~:-](#c0,1)[a-zA-Z0-9._~:-][a-zA-Z0-9./_~:-]#)(#c0,1) ]]; then
  77. url_correct=1
  78. elif [[ "$_wrd" = (#b)[[:alnum:]/_~:.-]## ]]; then
  79. url_correct=1
  80. fi
  81. (( url_correct )) && \
  82. { __style=${FAST_THEME_NAME}correct-subtle; return 0; } || \
  83. { __style=${FAST_THEME_NAME}incorrect-subtle; return 1; }
  84. }
  85. # A handler which verifies the token as a shell wildcard
  86. →chroma/main-chroma-std-verify-pattern() {
  87. setopt localoptions extendedglob
  88. local _wrd="$4"
  89. __style=${FAST_THEME_NAME}globbing-ext
  90. }
  91. # Creates a hash table for given option set (an *_opt field in the chroma def.)
  92. →chroma/main-create-OPTION-hash.ch() {
  93. local __subcmd="$1" __option_set_id="$2" __the_hash_name="$3" __ __e __el __the_hash_name __var_name
  94. local -a __split __sp __s
  95. →chroma/main-chroma-print -rl "======================" " **## STARTING ##** →chroma/main-##CREATE##-option-HASH.ch // subcmd:$__subcmd // option_set_id:$__option_set_id // h-nam:$__the_hash_name"
  96. →chroma/main-chroma-print "[D] Got option-set: ${(j:,:)__option_set_id}"
  97. typeset -gA "$__the_hash_name"
  98. →chroma/main-chroma-print "[E] __the_hash_name ${__the_hash_name}:[$__option_set_id]"
  99. # Split on ||
  100. __ch_def_name="fsh__${__chroma_name}__chroma__def[${__option_set_id}]"
  101. __split=( "${(P@s:||:)__ch_def_name}" )
  102. [[ ${#__split} -eq 1 && -z "${__split[1]}" ]] && __split=()
  103. # Remove only leading and trailing whitespace
  104. __split=( "${__split[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  105. →chroma/main-chroma-print -rl "[F] Got ||-__split: _________" ${${(@)${${__split[@]##[[:space:]]##}[@]//[${__hspaces}]##/ }[@]//[${__nl}]##/$__nl}[@]//(#s)/:::} "_________"
  106. for __el in $__split; do
  107. __sp=( "${(@s:<<>>:)__el}" )
  108. [[ ${#__sp} -eq 1 && -z "${__sp[1]}" ]] && __sp=()
  109. __sp=( "${__sp[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  110. →chroma/main-chroma-print -l -- "Processing an ||-part - got <<>>-split: _________" "${${__sp[@]}[@]/(#s)/-\\t}" "_________"
  111. __e="${__sp[1]}"
  112. local __e1=${${__e#\(}%\)(:add|:del|)}
  113. local __e2=${(M)__e##\(*\)(:add|:del)}
  114. # Split on | with the ( and ) and :add/:del stripped and then append
  115. # the :add or :del depending on what's on the input line
  116. __s=()
  117. for __ in ${(@s:|:)__e1}; do
  118. __s+=( $__${__e2:+${(M)__e%(:add|:del)}} )
  119. done
  120. [[ ${#__s} -eq 1 && -z "${__s[1]}" ]] && __s=()
  121. __s=( "${__s[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  122. shift __sp
  123. for __ in $__s; do
  124. __=${__%\^}
  125. [[ "$__" = -*:(add|del) ]] && __var_name="${__the_hash_name}[${__}-directive]" || __var_name="${__the_hash_name}[${__}-opt-action]"
  126. →chroma/main-chroma-print "${(r:70:: :):-${__var_name}} := >>${__sp[1]}${${${#__sp}:#(0|1)}:+ +}<<"
  127. : ${(P)__var_name::=${__sp[1]}${${${#__sp}:#(0|1)}:+ +}}
  128. if (( ${#__sp} >= 2 )); then
  129. __var_name="${__the_hash_name}[${__}-opt-arg-action]"
  130. →chroma/main-chroma-print "${(r:70:: :):-${__var_name}} := >>${__sp[2]}<<}"
  131. : ${(P)__var_name::=$__sp[2]}
  132. fi
  133. done
  134. done
  135. }
  136. # Processes given token
  137. →chroma/main-process-token.ch() {
  138. local __subcmd="$1" __wrd="$2" __val __var_name __main_hash_name __the_hash_name __i __size
  139. local -a __splitted __split __added
  140. →chroma/main-chroma-print "\n******************* Starting →chroma/main-process-token <<$__wrd>>// subcmd:${(qq)__subcmd}"
  141. __main_hash_name="fsh__chroma__main__${${FAST_HIGHLIGHT[chroma-current]//[^a-zA-Z0-9_]/_}//(#b)([\#\^\*])/${map[${match[1]}]}}"
  142. __var_name="${__main_hash_name}[subcmd:$__subcmd]"
  143. __splitted=( "${(@s://:P)__var_name}" )
  144. [[ ${#__splitted} -eq 1 && -z "${__splitted[1]}" ]] && __splitted=()
  145. __splitted=( "${__splitted[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  146. →chroma/main-chroma-print -rl -- "[B] MAIN-PROCESS-TOKEN: got [OPTION/ARG-**S-E-T-S**] //-splitted from subcmd:$__subcmd: ${${(j:, :)__splitted}:-EMPTY-SET!}" "----- __splitted\\Deleted: -----" ${${(j:, :)${__splitted[@]:#(${(~j:|:)${(@)=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]}})}}:-EMPTY-SET (deleted)!} "----- Added\\Deleted: -----" ${${(j:, :)${${(@)=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-added-nodes]}:#(${(~j:|:)${(@)=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]}})}}:-EMPTY-SET (added)!} -----\ Deleted:\ ----- ${(j:, :)${(@)=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]}} >> /tmp/reply
  147. (( ! ${#__splitted} )) && {
  148. __var_name="${__main_hash_name}[subcmd:*]"
  149. __splitted=( "${(@s://:P)__var_name}" )
  150. [[ ${#__splitted} -eq 1 && -z "${__splitted[1]}" ]] && __splitted=()
  151. __splitted=( "${__splitted[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  152. (( ! ${#__splitted} )) && return 1
  153. }
  154. →chroma/main-chroma-print -rl -- "---NO-HASH-CREATE-FROM-NOW-ON---"
  155. if [[ -z "${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-with-arg-active]}" ]]; then
  156. →chroma/main-chroma-print -rl -- "-z OPT-WITH-ARG-ACTIVE == true"
  157. if [[ "$__wrd" = -* ]]; then
  158. →chroma/main-chroma-print "1st-PATH (-z opt-with-arg-active, non-opt-arg branch, i.e. OPTION BRANCH) [#${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]}]"
  159. for __val in ${__splitted[@]:#(${(~j:|:)${(@)=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]}})} ${${(@)=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-added-nodes]}:#(${(~j:|:)${(@)=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]}})}; do
  160. [[ "${__val}" != "${__val%%_([0-9]##|\#)##*}"_${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]}_opt(\*|\^|) && "${__val}" != "${__val%%_([0-9]##|\#)*}"_"#"_opt(\*|\^|) ]] && { →chroma/main-chroma-print "DIDN'T MATCH $__val / arg counter:${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]}" ; continue; } || →chroma/main-chroma-print "Got candidate: $__val"
  161. # Create the hash cache-parameter if needed
  162. __the_hash_name="fsh__chroma__${FAST_HIGHLIGHT[chroma-current]//[^a-zA-Z0-9_]/_}__${__subcmd//[^a-zA-Z0-9_]/_}__${${__val//(#b)([\#\^\*])/${map[${match[1]}]}}//[^a-zA-Z0-9_]/_}"
  163. [[ "$__val" = *_opt(\*|\^|) && "${(P)+__the_hash_name}" -eq 0 ]] && →chroma/main-create-OPTION-hash.ch "$__subcmd" "$__val" "$__the_hash_name" || →chroma/main-chroma-print "Not creating, the hash already exists..."
  164. # Try dedicated-entry for the option
  165. __var_name="${__the_hash_name}[${${${${(M)__wrd#?*=}:+${__wrd%=*}=}:-$__wrd}}-opt-action]"
  166. __split=( "${(@s://:P)__var_name}" )
  167. [[ ${#__split} -eq 1 && -z "${__split[1]}" ]] && __split=()
  168. # If no result, then try with catch-all entry
  169. (( ! ${#__split} )) && {
  170. →chroma/main-chroma-print "% no ${(q-)${${${(M)__wrd#?*=}:+${__wrd%=*}=}:-$__wrd}}-opt-action, retrying with *-opt-action" "|__var_name|:$__var_name"
  171. __var_name="${__the_hash_name}[*-opt-action]"
  172. __split=( "${(@s://:P)__var_name}" )
  173. [[ ${#__split} -eq 1 && -z "${__split[1]}" ]] && __split=()
  174. }
  175. __svalue="$__var_name"
  176. # Remove whitespace
  177. __split=( "${__split[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  178. →chroma/main-chroma-print -l -- "\`$__val' // ${#__split} // $__wrd: (ch.run #${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-call-nr]}), deref. of \`$__var_name'"
  179. if (( ${#__split} )); then
  180. →chroma/main-chroma-print -l -- "Got split of {\$#__split:$#__split} ${__wrd}-opt-action or *-opt-action" "${${(q-)__split[@]}[@]/(#s)/->\\t}"
  181. if [[ "${__split[2]}" = *[[:blank:]]+ ]]; then
  182. →chroma/main-chroma-print "YES handling the value (the OPT.ARGUMENT)! [${__split[2]}]"
  183. if [[ "$__wrd" = *=* ]]; then
  184. →chroma/main-chroma-print "The-immediate Arg-Acquiring, of option"
  185. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-with-arg-active]="${__svalue%-opt-action\]}-opt-arg-action]"
  186. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-arg]="${__wrd#*=}"
  187. __have_value=2
  188. else
  189. →chroma/main-chroma-print "Enable Arg-Awaiting, of option"
  190. →chroma/main-chroma-print "FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-with-arg-active]=\"${__svalue%-opt-action\]}-opt-arg-action]\""
  191. __have_value=0
  192. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-with-arg-active]="${__svalue%-opt-action\]}-opt-arg-action]"
  193. fi
  194. fi
  195. __action="${__split[1]}"
  196. __handler="${__split[2]%[[:blank:]]+}"
  197. # Check for directives (like :add)
  198. if [[ "$__val" = *_opt\^ ]]; then
  199. __var_name="${__the_hash_name}[${${${${(M)__wrd#?*=}:+${__wrd%=*}=}:-$__wrd}}:add-directive]"
  200. (( ${(P)+__var_name} )) && __split=( "${(@s://:P)__var_name}" ) || __split=()
  201. [[ ${#__split} -eq 1 && -z "${__split[1]}" ]] && __split[1]=()
  202. __ivalue=${#__split}
  203. __var_name="${__var_name%:add-*}:del-directive]"
  204. (( ${(P)+__var_name} )) && __split+=( "${(@s://:P)__var_name}" )
  205. [[ ${#__split} -eq $(( __ivalue + 1 )) && -z "${__split[__ivalue+1]}" ]] && __split[__ivalue+1]=()
  206. __split=( "${__split[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  207. __tmp=${#__split}
  208. # First: del-directive
  209. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]+="${(j: :)__split[__ivalue+1,__tmp]} "
  210. →chroma/main-chroma-print -rl ":add / :del directives: __ivalue:$__ivalue, THE __SPLIT[#$__tmp]: " "${__split[@]}" "//" "The FAST_HIGHLIGHT[chroma-*deleted-nodes]: " ${=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]} >> /tmp/reply
  211. # Second: add-directive
  212. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-added-nodes]+="${(j: :)__split[1,__ivalue]} "
  213. fi
  214. [[ "$__handler" = ::[^[:space:]]* ]] && __handler="${__handler#::}" || __handler=""
  215. [[ -n "$__handler" && "$__handler" != "NO-OP" ]] && { →chroma/main-chroma-print -rl -- "Running handler(1): $__handler" ; "$__handler" "${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]:-NULL}" "$__start" "$__end" "$__wrd"; }
  216. [[ "$__have_value" -ne 2 && -n "$__action" && "$__action" != "NO-OP" ]] && { →chroma/main-chroma-print -rl "Running action (1): $__action" ; eval "() { $__action; }"; }
  217. [[ "$__val" != *\* ]] && break
  218. else
  219. →chroma/main-chroma-print -rl -- "NO-MATCH ROUTE TAKEN"
  220. fi
  221. done
  222. else
  223. →chroma/main-chroma-print "1st-PATH-B (-z opt-with-arg-active, non-opt-arg branch, ARGUMENT BRANCH [#${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]}]) //// added-nodes: ${=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-added-nodes]}"
  224. for __val in ${__splitted[@]:#(${(~j:|:)${(@)=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]}})} ${${(@)=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-added-nodes]}:#(${(~j:|:)${(@)=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]}})}; do
  225. [[ "${__val}" != "${__val%%_([0-9]##|\#)*}"_"${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]}"_arg(\*|\^|) && "${__val}" != "${__val%%_([0-9]##|\#)*}"_"#"_arg(\*|\^|) ]] && { →chroma/main-chroma-print "Continuing for $__val / arg counter ${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]}" ; continue }
  226. # Create the hash cache-parameter if needed
  227. __the_hash_name="fsh__chroma__${FAST_HIGHLIGHT[chroma-current]//[^a-zA-Z0-9_]/_}__${__subcmd//[^a-zA-Z0-9_]/_}__${${__val//\#/H}//[^a-zA-Z0-9_]/_}"
  228. __action="" __handler=""
  229. →chroma/main-chroma-print "A hit, chosen __val:$__val!"
  230. __ch_def_name="fsh__${__chroma_name}__chroma__def[$__val]"
  231. __split=( "${(P@s:<<>>:)__ch_def_name}" )
  232. __split=( "${__split[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  233. __sp=( "${(@s://:)__split[1]}" )
  234. __sp=( "${__sp[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  235. __action="${__sp[1]#*::::: ##}"
  236. # Verify if it's the expected argument
  237. [[ "${__sp[1]}" = *:::::* && "$__wrd" != ${~${__sp[1]%% ##:::::*}} ]] && \
  238. { →chroma/main-chroma-print -r "mismatch ${__sp[1]%% ##:::::*} != $__wrd, continuing" ; continue; }
  239. →chroma/main-chroma-print -l -- "Got action record for $__val, i.e. the split:" "${__sp[@]//(#s)/-\t}" "_________"
  240. [[ "${__sp[2]}" = ::[^[:space:]]* ]] && __handler="${__sp[2]#::}" || { [[ -n "$__handler" && "$__handler" != "NO-OP" ]] && →chroma/main-chroma-print "=== Error === In chroma definition: a handler entry ${(q)__sp[2]} without leading \`::'" ; }
  241. [[ -n "$__handler" && "$__handler" != "NO-OP" ]] && { →chroma/main-chroma-print -rl -- "Running handler(3): $__handler" ; "$__handler" "${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]:-NULL}" "$__start" "$__end" "$__wrd"; }
  242. [[ -n "$__action" && "$__action" != "NO-OP" ]] && { →chroma/main-chroma-print -rl -- "Running action(3): $__action" ; eval "() { $__action; } \"${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]:-NULL}\" \"$__start\" \"$__end\" \"$__wrd\""; }
  243. # Check for argument directives (like :add)
  244. if (( ${#__split} >= 2 )); then
  245. for __ in "${(@)__split[2,-1]}"; do
  246. __splitted=( "${(@s://:)__}" )
  247. if [[ "${__splitted[1]}" = add:* ]]; then
  248. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-added-nodes]+="${__splitted[1]#add:} ${(j: :)__splitted[2,-1]} "
  249. elif [[ "${__splitted[1]}" = del:* ]]; then
  250. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]+="${__splitted[1]#del:} ${(j: :)__splitted[2,-1]} "
  251. fi
  252. done
  253. →chroma/main-chroma-print -l "ARGUMENT :add / :del directives: THE __SPLIT[#${#__split}]: " "${__split[@]//(#s)/-\\t}" "//" "The FAST_HIGHLIGHT[chroma-*deleted-nodes]: " ${(@)${=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]}//(#s)/-\\t} "The FAST_HIGHLIGHT[chroma-*added-nodes]: " ${(@)${=FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-added-nodes]}//(#s)/-\\t}
  254. fi
  255. [[ "$__val" != *\* ]] && break
  256. done
  257. fi
  258. else
  259. →chroma/main-chroma-print -- "2nd-PATH (-n opt-with-arg-active) NON-EMPTY arg-active:\nThe actual opt-val <<< \$__wrd:$__wrd >>> store (after the \`Arg-Awaiting' in the chroma-run: #$(( FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-call-nr]-1 )) [current: #$(( FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-call-nr] ))])"
  260. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-arg]="$__wrd"
  261. __have_value=1
  262. fi
  263. # Execute the action if not during simulated opt-argument (--opt=...)
  264. →chroma/main-chroma-print "** BEFORE: \`if (( __have_value ))'"
  265. if (( __have_value )); then
  266. →chroma/main-chroma-print "In the \`if (( __have_value ))' [have_value: $__have_value]"
  267. # Split
  268. __var_name="${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-with-arg-active]}"
  269. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-with-arg-active]=""
  270. __split=( "${(@s://:P)__var_name}" )
  271. [[ ${#__split} -eq 1 && -z "${__split[1]}" ]] && { →chroma/main-chroma-print -rl "NULL at __var_name:$__var_name" ; __split=(); }
  272. __split=( "${__split[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  273. # Remember 1st level action
  274. (( __have_value == 2 )) && __value="$__action" || __value=""
  275. if (( ${#__split} )); then
  276. →chroma/main-chroma-print -l -- "Got //-split (3, for opt-ARG-action, from [$__var_name]):" "${${(q-)__split[@]}[@]/(#s)/+\\t}"
  277. __action="${__split[1]}"
  278. __handler="${__split[2]}"
  279. [[ "$__handler" = ::[^[:space:]]* ]] && __handler="${__handler#::}"
  280. [[ -n "$__handler" && "$__handler" != "NO-OP" ]] && { →chroma/main-chroma-print -rl -- "Running handler(2): $__handler" ; "$__handler" "${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]:-NULL}" "$__start" "$__end" "$__wrd"; }
  281. [[ -n "$__action" && "$__action" != "NO-OP" ]] && { →chroma/main-chroma-print -rl -- "Running action(2): $__action" ; eval "$__action"; }
  282. →chroma/main-chroma-print -rl -- "The __action value: [$__value]"
  283. [[ "$__have_value" -eq 2 && -n "$__value" && "$__value" != "NO-OP" ]] && { →chroma/main-chroma-print -rl "Running action (of 1, at 2): $__value" ; eval "$__value"; }
  284. fi
  285. fi
  286. →chroma/main-chroma-print -- "_________ Exiting →chroma/main-process-token.ch $__subcmd / $__wrd _________"
  287. }
  288. # Iterates over the chroma def. fields and creates initial
  289. # fields in the fsh__${__chroma_name}__chroma__def hash
  290. →chroma/-pre_process_chroma_def.ch() {
  291. local __key __value __ke _val __the_hash_name="$1" __var_name
  292. local -a __split
  293. →chroma/main-chroma-print -rl -- "Starting PRE_PROCESS for __the_hash_name:$__the_hash_name"
  294. __ch_def_name="fsh__${__chroma_name}__chroma__def[subcommands]"
  295. local __subcmds="${(P)__ch_def_name}"
  296. if [[ "$__subcmds" = "::"* ]]; then
  297. ${__subcmds#::}
  298. __var_name="${__the_hash_name}[subcommands]"
  299. : ${(P)__var_name::=(${(j:|:)reply})}
  300. else
  301. __var_name="${__the_hash_name}[subcommands]"
  302. : ${(P)__var_name::=$__subcmds}
  303. fi
  304. →chroma/main-chroma-print "Got SUBCOMMANDS: ${(P)__var_name}"
  305. __ch_def_name="fsh__${__chroma_name}__chroma__def[subcmd-hook]"
  306. local __subcmd_hook="${(P)__ch_def_name}"
  307. if [[ -n "$__subcmd_hook" ]]; then
  308. __var_name="${__the_hash_name}[subcmd-hook]"
  309. : ${(P)__var_name::=$__subcmd_hook}
  310. fi
  311. __ch_def_name="fsh__${__chroma_name}__chroma__def[(I)subcmd:*]"
  312. for __key in "${(P@)__ch_def_name}"; do
  313. __split=( "${(@s:|:)${${__key##subcmd:\((#c0,1)}%\)}}" )
  314. [[ ${#__split} -eq 1 && -z "${__split[1]}" ]] && __split=()
  315. __split=( "${__split[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}" )
  316. for __ke in "${__split[@]}"; do
  317. __var_name="${__the_hash_name}[subcmd:$__ke]"
  318. __ch_def_name="fsh__${__chroma_name}__chroma__def[$__key]"
  319. : ${(P)__var_name::=${(P)__ch_def_name}}
  320. →chroma/main-chroma-print -rl -- "Storred ${__var_name}=chroma_def[$__key], i.e. = ${(P)__ch_def_name}"
  321. done
  322. done
  323. }
  324. if (( __first_call )); then
  325. →chroma/-${__chroma_name}-first-call
  326. FAST_HIGHLIGHT[chroma-current]="$__wrd"
  327. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]=0
  328. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-got-subcommand]=0
  329. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]=""
  330. FAST_HIGHLIGHT[chrome-${FAST_HIGHLIGHT[chroma-current]}-occurred-double-hyphen]=0
  331. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-with-arg-active]=""
  332. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-arg]=""
  333. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-call-nr]=1
  334. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-added-nodes]=""
  335. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-deleted-nodes]=""
  336. __the_hash_name="fsh__chroma__main__${${FAST_HIGHLIGHT[chroma-current]//[^a-zA-Z0-9_]/_}//(#b)([\#\^])/${map[${match[1]}]}}"
  337. (( 0 == ${(P)+__the_hash_name} )) && {
  338. typeset -gA "$__the_hash_name"
  339. →chroma/-pre_process_chroma_def.ch "$__the_hash_name"
  340. } || →chroma/main-chroma-print "...No... [\${+$__the_hash_name} ${(P)+__the_hash_name}]"
  341. return 1
  342. else
  343. (( ++ FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-call-nr] ))
  344. # Following call, i.e. not the first one
  345. # Check if chroma should end – test if token is of type
  346. # "starts new command", if so pass-through – chroma ends
  347. [[ "$__arg_type" = 3 ]] && return 2
  348. →chroma/main-chroma-print "== @@ Starting @@ #${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-call-nr]} Main-Chroma-call == // << __WORD:$__wrd >> ## Subcommand: ${${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]}:-NULL} //@@// -n option-with-arg-active:${(q-)FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-with-arg-active]}"
  349. if [[ "$__wrd" = -* || -n "${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-option-with-arg-active]}"
  350. ]]; then
  351. →chroma/main-chroma-print "## The \`if -*' i.e. \`IF OPTION' MAIN branch"
  352. →chroma/main-process-token.ch "${${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]}:-NULL}" "$__wrd"
  353. else
  354. # If at e.g. '>' or destination/source spec (of the redirection)
  355. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  356. return 1
  357. elif (( FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-got-subcommand] == 0 )) {
  358. __the_hash_name="fsh__chroma__main__${${FAST_HIGHLIGHT[chroma-current]//[^a-zA-Z0-9_]/_}//(#b)([\#\^])/${map[${match[1]}]}}"
  359. __var_name="${__the_hash_name}[subcommands]"
  360. if [[ "$__wrd" = ${(P)~__var_name} ]]; then
  361. →chroma/main-chroma-print "GOT-SUBCOMMAND := $__wrd, subcmd verification / OK"
  362. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-got-subcommand]=1
  363. FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]="$__wrd"
  364. __var_name="${__the_hash_name}[subcmd-hook]"
  365. (( ${(P)+__var_name} )) && { →chroma/main-chroma-print -r -- "Running subcmd-hook: ${(P)__var_name}" ; "${(P)__var_name}" "$__wrd"; }
  366. __style="${FAST_THEME_NAME}subcommand"
  367. else
  368. →chroma/main-chroma-print "subcmd verif / NOT OK; Incrementing the COUNTER-ARG ${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]} -> $(( FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg] + 1 ))" >> /tmp/fsh-dbg
  369. (( FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg] += 1 ))
  370. →chroma/main-chroma-print "UNRECOGNIZED ARGUMENT ${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]}"
  371. →chroma/main-process-token.ch "${${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]}:-NULL}" "$__wrd"
  372. fi
  373. } else {
  374. __wrd="${__wrd//\`/x}"
  375. __arg="${__arg//\`/x}"
  376. __wrd="${(Q)__wrd}"
  377. local __tmp_def_name="fsh__${__chroma_name}__chroma__def[subcommands-blacklist]"
  378. if [[ ${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]} = \
  379. (${(~j:|:)${(@s:,:)${(PA)__tmp_def_name}}})
  380. ]] {
  381. return 1
  382. }
  383. →chroma/main-chroma-print "Incrementing the COUNTER-ARG ${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]} -> $(( FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg] + 1 ))"
  384. (( FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg] += 1 ))
  385. →chroma/main-chroma-print "ARGUMENT ${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-counter-arg]}"
  386. →chroma/main-chroma-print "ELSE *-got-subcommand == 1 is TRUE"
  387. →chroma/main-process-token.ch "${FAST_HIGHLIGHT[chroma-${FAST_HIGHLIGHT[chroma-current]}-subcommand]}" "$__wrd"
  388. }
  389. fi
  390. fi
  391. # Add region_highlight entry (via `reply' array)
  392. if [[ -n "$__style" ]]; then
  393. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) \
  394. && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  395. fi
  396. # We aren't passing-through, do obligatory things ourselves
  397. (( this_word = next_word ))
  398. _start_pos=$_end_pos
  399. return 0
  400. # vim:ft=zsh:et:sw=4