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.

77 lines
3.1 KiB

  1. # vim:ft=zsh:sw=4:sts=4
  2. #
  3. # $1 - PREBUFFER
  4. # $2 - BUFFER
  5. #
  6. function -fast-highlight-string-process {
  7. emulate -LR zsh
  8. setopt extendedglob warncreateglobal typesetsilent
  9. local -A pos_to_level level_to_pos pair_map final_pairs
  10. local input=$1$2 _mybuf=$1$2 __style __quoting
  11. integer __idx=0 __pair_idx __level=0 __start __end
  12. local -a match mbegin mend
  13. pair_map=( "(" ")" "{" "}" "[" "]" )
  14. while [[ $_mybuf = (#b)([^"{}()[]\\\"'"]#)((["({[]})\"'"])|[\\](*))(*) ]]; do
  15. if [[ -n ${match[4]} ]] {
  16. __idx+=${mbegin[2]}
  17. [[ $__quoting = \' ]] && _mybuf=${match[4]} || { _mybuf=${match[4]:1}; (( ++ __idx )); }
  18. } else {
  19. __idx+=${mbegin[2]}
  20. [[ -z $__quoting && -z ${_FAST_COMPLEX_BRACKETS[(r)$((__idx-${#PREBUFFER}-1))]} ]] && {
  21. if [[ ${match[2]} = ["({["] ]]; then
  22. pos_to_level[$__idx]=$(( ++__level ))
  23. level_to_pos[$__level]=$__idx
  24. elif [[ ${match[2]} = ["]})"] ]]; then
  25. if (( __level > 0 )); then
  26. __pair_idx=${level_to_pos[$__level]}
  27. pos_to_level[$__idx]=$(( __level -- ))
  28. [[ ${pair_map[${input[__pair_idx]}]} = ${input[__idx]} ]] && {
  29. final_pairs[$__idx]=$__pair_idx
  30. final_pairs[$__pair_idx]=$__idx
  31. }
  32. else
  33. pos_to_level[$__idx]=-1
  34. fi
  35. fi
  36. }
  37. if [[ ${match[2]} = \" && $__quoting != \' ]] {
  38. [[ $__quoting = '"' ]] && __quoting="" || __quoting='"';
  39. }
  40. if [[ ${match[2]} = \' && $__quoting != \" ]] {
  41. if [[ $__quoting = ("'"|"$'") ]] {
  42. __quoting=""
  43. } else {
  44. if [[ $match[1] = *\$ ]] {
  45. __quoting="\$'";
  46. } else {
  47. __quoting="'";
  48. }
  49. }
  50. }
  51. _mybuf=${match[5]}
  52. }
  53. done
  54. for __idx in ${(k)pos_to_level}; do
  55. (( ${+final_pairs[$__idx]} )) && __style=${FAST_THEME_NAME}bracket-level-$(( ( (pos_to_level[$__idx]-1) % 3 ) + 1 )) || __style=${FAST_THEME_NAME}unknown-token
  56. (( __start=__idx-${#PREBUFFER}-1, __end=__idx-${#PREBUFFER}, __start >= 0 )) && \
  57. reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  58. done
  59. # If cursor is on a bracket, then highlight corresponding bracket, if any.
  60. if [[ $WIDGET != zle-line-finish ]]; then
  61. __idx=$(( CURSOR + 1 ))
  62. if (( ${+pos_to_level[$__idx]} )) && (( ${+final_pairs[$__idx]} )); then
  63. (( __start=final_pairs[$__idx]-${#PREBUFFER}-1, __end=final_pairs[$__idx]-${#PREBUFFER}, __start >= 0 )) && \
  64. reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}paired-bracket]}") && \
  65. reply+=("$CURSOR $__idx ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}paired-bracket]}")
  66. fi
  67. fi
  68. return 0
  69. }