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.

98 lines
2.9 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Almost all code borrowed from Zshell's _make function
  3. #
  4. # Copyright (c) 2018 Sebastian Gniazdowski
  5. local -a TARGETS
  6. .make-expandVars() {
  7. local open close var val front='' rest=$1
  8. while [[ $rest == (#b)[^$]#($)* ]]; do
  9. front=$front${rest[1,$mbegin[1]-1]}
  10. rest=${rest[$mbegin[1],-1]}
  11. case $rest[2] in
  12. ($) # '$$'. may not appear in target and variable's value
  13. front=$front\$\$
  14. rest=${rest[3,-1]}
  15. continue
  16. ;;
  17. (\() # Variable of the form $(foobar)
  18. open='('
  19. close=')'
  20. ;;
  21. ({) # ${foobar}
  22. open='{'
  23. close='}'
  24. ;;
  25. ([[:alpha:]]) # $foobar. This is exactly $(f)oobar.
  26. open=''
  27. close=''
  28. var=$rest[2]
  29. ;;
  30. (*) # bad parameter name
  31. print -- $front$rest
  32. return 1
  33. ;;
  34. esac
  35. if [[ -n $open ]]; then
  36. if [[ $rest == \$$open(#b)([[:alnum:]_]##)(#B)$close* ]]; then
  37. var=$match
  38. else # unmatched () or {}, or bad parameter name
  39. print -- $front$rest
  40. return 1
  41. fi
  42. fi
  43. val=''
  44. if [[ -n ${VAR_ARGS[(i)$var]} ]]; then
  45. val=${VAR_ARGS[$var]}
  46. else
  47. if [[ -n $opt_args[(I)(-e|--environment-overrides)] ]]; then
  48. if [[ $parameters[$var] == scalar-export* ]]; then
  49. val=${(P)var}
  50. elif [[ -n ${VARIABLES[(i)$var]} ]]; then
  51. val=${VARIABLES[$var]}
  52. fi
  53. else
  54. if [[ -n ${VARIABLES[(i)$var]} ]]; then
  55. val=${VARIABLES[$var]}
  56. elif [[ $parameters[$var] == scalar-export* ]]; then
  57. val=${(P)var}
  58. fi
  59. fi
  60. fi
  61. rest=${rest//\$$open$var$close/$val}
  62. done
  63. print -- ${front}${rest}
  64. }
  65. .make-parseMakefile () {
  66. local input var val target dep TAB=$'\t' tmp IFS=
  67. while read input
  68. do
  69. case "$input " in
  70. # TARGET: dependencies
  71. # TARGET1 TARGET2 TARGET3: dependencies
  72. ([[*?[:alnum:]$][^$TAB:=%]#:[^=]*)
  73. target=$(.make-expandVars ${input%%:*})
  74. TARGETS+=( ${(z)target} )
  75. ;;
  76. esac
  77. done
  78. }
  79. if [[ -z "${FAST_HIGHLIGHT[chroma-make-cache]}" || $(( EPOCHSECONDS - FAST_HIGHLIGHT[chroma-make-cache-born-at] )) -gt 7 ]]; then
  80. .make-parseMakefile
  81. FAST_HIGHLIGHT[chroma-make-cache-born-at]="$EPOCHSECONDS"
  82. FAST_HIGHLIGHT[chroma-make-cache]="${(j:;:)TARGETS}"
  83. fi
  84. reply2=( "${(s:;:)FAST_HIGHLIGHT[chroma-make-cache]}" )
  85. # vim:ft=zsh:et