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
5.0 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018-2019 Sebastian Gniazdowski
  3. (( next_word = 2 | 8192 ))
  4. local THEFD check __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  5. local __style
  6. (( ! ${+FAST_HIGHLIGHT[whatis_chroma_callback_was_ran]} )) && \
  7. FAST_HIGHLIGHT[whatis_chroma_callback_was_ran]=0
  8. (( ! ${+FAST_HIGHLIGHT[whatis_chroma_zle_-F_have_-w_opt]} )) && {
  9. is-at-least 5.0.6 && local __res=1 || local __res=0
  10. FAST_HIGHLIGHT[whatis_chroma_zle_-F_have_-w_opt]="$__res"
  11. }
  12. -fast-whatis-chroma-callback() {
  13. emulate -L zsh
  14. setopt extendedglob warncreateglobal typesetsilent
  15. local THEFD="$1" input check=2 nl=$'\n' __wrd __style
  16. .fast-zts-read-all "$THEFD" input
  17. zle -F "$THEFD"
  18. exec {THEFD}<&-
  19. __wrd="${${input#[^$nl]#$nl}%%$nl*}"
  20. if [[ "$input" = test* ]]; then
  21. if [[ "${input%$nl}" = *[^0-9]'0' ]]; then
  22. if [[ "${input#test$nl}" = *nothing\ appropriate* ]]; then
  23. FAST_HIGHLIGHT[whatis_chroma_type]=2
  24. else
  25. FAST_HIGHLIGHT[whatis_chroma_type]=0
  26. fi
  27. else
  28. FAST_HIGHLIGHT[whatis_chroma_type]=1
  29. fi
  30. elif [[ "$input" = type2* ]]; then
  31. [[ "$input" != *nothing\ appropriate* ]] && check=1 || check=0
  32. elif [[ "$input" = type1* ]]; then
  33. [[ "${input%$nl}" = *0 ]] && check=1 || check=0
  34. fi
  35. if (( check != 2 )); then
  36. FAST_HIGHLIGHT[whatis-cache-$__wrd]=$check
  37. if (( check )) then
  38. __style=${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}correct-subtle]}
  39. elif [[ ${~__wrd} = */* && -e ${~__wrd} ]] then
  40. __style=${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}path]}
  41. else
  42. __style=${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}
  43. fi
  44. local -a start_end
  45. start_end=( ${(s:/:)${${(M)${${input#type?${nl}[^$nl]#$nl}}#*$nl}%$nl}} )
  46. (( start_end[1] >= 0 )) && region_highlight+=("$start_end[1] $start_end[2] $__style")
  47. zle -R
  48. fi
  49. FAST_HIGHLIGHT[whatis_chroma_callback_was_ran]=1
  50. return 0
  51. }
  52. zle -N -- -fast-whatis-chroma-callback
  53. if (( __first_call )) && [[ -z "${FAST_HIGHLIGHT[whatis_chroma_type]}" ]] ;then
  54. if ! command -v whatis > /dev/null; then
  55. FAST_HIGHLIGHT[whatis_chroma_type]=0
  56. return 1
  57. fi
  58. exec {THEFD}< <(
  59. print "test"
  60. LANG=C whatis "osx whatis fallback check"
  61. print "$?"
  62. )
  63. command true # a workaround of Zsh bug
  64. zle -F ${${FAST_HIGHLIGHT[whatis_chroma_zle_-F_have_-w_opt]:#0}:+-w} "$THEFD" -fast-whatis-chroma-callback
  65. fi
  66. [[ "$__arg_type" = 3 ]] && return 2
  67. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  68. return 1
  69. fi
  70. if (( __first_call )) || [[ "$__wrd" = -* ]]; then
  71. return 1
  72. elif (( ! FAST_HIGHLIGHT[whatis_chroma_type] )); then
  73. # Return 1 (i.e. treat the argument as a path) only if the callback have
  74. # had a chance to establish the whatis_chroma_type field
  75. (( FAST_HIGHLIGHT[whatis_chroma_callback_was_ran] )) && return 1
  76. else
  77. if [[ -z "${FAST_HIGHLIGHT[whatis-cache-$__wrd]}" ]]; then
  78. if (( FAST_HIGHLIGHT[whatis_chroma_type] == 2 )); then
  79. exec {THEFD}< <(
  80. print "type2"
  81. print "$__wrd"
  82. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER} ))
  83. print "$__start/$__end"
  84. LANG=C whatis "$__wrd" 2>/dev/null
  85. )
  86. command true # see above
  87. zle -F ${${FAST_HIGHLIGHT[whatis_chroma_zle_-F_have_-w_opt]:#0}:+-w} "$THEFD" -fast-whatis-chroma-callback
  88. else
  89. exec {THEFD}< <(
  90. print "type1"
  91. print "$__wrd"
  92. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER} ))
  93. print "$__start/$__end"
  94. LANG=C whatis "$__wrd" &> /dev/null
  95. print "$?"
  96. )
  97. command true
  98. zle -F ${${FAST_HIGHLIGHT[whatis_chroma_zle_-F_have_-w_opt]:#0}:+-w} "$THEFD" -fast-whatis-chroma-callback
  99. fi
  100. __style=${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}
  101. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
  102. reply+=("$__start $__end $__style")
  103. else
  104. check=${FAST_HIGHLIGHT[whatis-cache-$__wrd]}
  105. if (( check )) then
  106. __style=${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}correct-subtle]}
  107. elif [[ ${~__wrd} = */* && -e ${~__wrd} ]] then
  108. __style=${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}path]}
  109. elif (( FAST_HIGHLIGHT[whatis_chroma_type] )); then
  110. __style=${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}
  111. fi
  112. [[ -n "$__style" ]] && \
  113. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
  114. reply+=("$__start $__end $__style")
  115. fi
  116. fi
  117. (( this_word = next_word ))
  118. _start_pos=$_end_pos
  119. return 0
  120. # vim:ft=zsh:et:sw=4:sts=4