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.

75 lines
2.8 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. #
  4. # Chroma for `source' builtin - verifies if file to be sourced compiles
  5. # correctly.
  6. #
  7. # $1 - 0 or 1, denoting if it's first call to the chroma, or following one
  8. #
  9. # $2 - the current token, also accessible by $__arg from the above scope -
  10. # basically a private copy of $__arg; the token can be eg.: "grep"
  11. #
  12. # $3 - a private copy of $_start_pos, i.e. the position of the token in the
  13. # command line buffer, used to add region_highlight entry (see man),
  14. # because Zsh colorizes by *ranges* in command line buffer
  15. #
  16. # $4 - a private copy of $_end_pos from the above scope
  17. #
  18. (( next_word = 2 | 8192 ))
  19. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  20. local __style __chars __home=${XDG_CACHE_HOME:-$HOME/.cache}/fsh
  21. integer __idx1 __idx2
  22. # First call, i.e. command starts, i.e. "grep" token etc.
  23. (( __first_call )) && {
  24. FAST_HIGHLIGHT[chroma-src-counter]=0
  25. __style=${FAST_THEME_NAME}builtin
  26. } || {
  27. # Following call, i.e. not the first one.
  28. # Check if chroma should end – test if token is of type
  29. # "starts new command", if so pass-through – chroma ends
  30. [[ "$__arg_type" = 3 ]] && return 2
  31. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  32. return 1
  33. fi
  34. if [[ "$__wrd" = -* ]]; then
  35. # Detected option, add style for it.
  36. [[ "$__wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  37. __style=${FAST_THEME_NAME}single-hyphen-option
  38. else
  39. # Count non-option tokens.
  40. (( FAST_HIGHLIGHT[chroma-src-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-src-counter] ))
  41. if (( FAST_HIGHLIGHT[chroma-src-counter] == 1 )); then
  42. command mkdir -p "$__home"
  43. command cp -f "${__wrd}" "$__home" 2>/dev/null && {
  44. zcompile "$__home"/"${__wrd:t}" 2>/dev/null 1>&2 && __style=${FAST_THEME_NAME}correct-subtle || __style=${FAST_THEME_NAME}incorrect-subtle
  45. }
  46. elif (( FAST_HIGHLIGHT[chroma-src-counter] == 2 )); then
  47. # Handle paths, etc. normally - just pass-through to the big
  48. # highlighter (the main FSH highlighter, used before chromas).
  49. return 1
  50. fi
  51. fi
  52. }
  53. # Add region_highlight entry (via `reply' array).
  54. #
  55. # This is a common place of adding such entry, but any above
  56. # code can do it itself (and it does) and skip setting __style
  57. # to disable this code.
  58. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  59. # We aren't passing-through (no return 1 occured), do obligatory things ourselves.
  60. (( this_word = next_word ))
  61. _start_pos=$_end_pos
  62. return 0
  63. # vim:ft=zsh:et:sw=4