-whatis.ch 5.4 KB

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