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.

87 lines
3.2 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. #
  4. # Tracks scp command and emits message when one tries to pass port to hostspec.
  5. #
  6. # $1 - 0 or 1, denoting if it's first call to the chroma, or following one
  7. #
  8. # $2 - the current token, also accessible by $__arg from the above scope -
  9. # basically a private copy of $__arg; the token can be eg.: "grep"
  10. #
  11. # $3 - a private copy of $_start_pos, i.e. the position of the token in the
  12. # command line buffer, used to add region_highlight entry (see man),
  13. # because Zsh colorizes by *ranges* in command line buffer
  14. #
  15. # $4 - a private copy of $_end_pos from the above scope
  16. #
  17. (( next_word = 2 | 8192 ))
  18. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  19. local __style __chars
  20. integer __idx1 __idx2
  21. local -a __results
  22. # First call, i.e. command starts, i.e. "grep" token etc.
  23. (( __first_call )) && {
  24. FAST_HIGHLIGHT[chroma-scp-counter]=0
  25. FAST_HIGHLIGHT[chroma-scp-counter-all]=1
  26. FAST_HIGHLIGHT[chroma-scp-message]=""
  27. FAST_HIGHLIGHT[chroma-scp-skip-two]=0
  28. return 1
  29. } || {
  30. (( FAST_HIGHLIGHT[chroma-scp-counter-all] += 1, __idx2 = FAST_HIGHLIGHT[chroma-scp-counter-all] ))
  31. # Following call, i.e. not the first one.
  32. # Check if chroma should end – test if token is of type
  33. # "starts new command", if so pass-through – chroma ends
  34. [[ "$__arg_type" = 3 ]] && return 2
  35. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  36. return 1
  37. fi
  38. if [[ "$__wrd" = -* ]]; then
  39. # Detected option, add style for it.
  40. [[ "$__wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  41. __style=${FAST_THEME_NAME}single-hyphen-option
  42. if [[ "$__wrd" = (-c|-F|-i|-l|-o|-P|-S) ]]; then
  43. FAST_HIGHLIGHT[chroma-scp-skip-two]=1
  44. fi
  45. else
  46. # Count non-option tokens.
  47. if (( FAST_HIGHLIGHT[chroma-scp-skip-two] )); then
  48. FAST_HIGHLIGHT[chroma-scp-skip-two]=0
  49. else
  50. (( FAST_HIGHLIGHT[chroma-scp-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-scp-counter] ))
  51. if [[ "${FAST_HIGHLIGHT[chroma-scp-counter]}" -eq 1 ]]; then
  52. if [[ "$__arg" = [^:]##:[0-9]## ]]; then
  53. FAST_HIGHLIGHT[chroma-scp-message]+="Format of hostname incorrect, use -P to pass port number"
  54. else
  55. return 1
  56. fi
  57. else
  58. return 1
  59. fi
  60. fi
  61. fi
  62. if (( ${#${(z)BUFFER}} <= FAST_HIGHLIGHT[chroma-scp-counter-all] )); then
  63. [[ -n "${FAST_HIGHLIGHT[chroma-scp-message]}" ]] && zle -M "${FAST_HIGHLIGHT[chroma-scp-message]}"
  64. fi
  65. }
  66. # Add region_highlight entry (via `reply' array).
  67. #
  68. # This is a common place of adding such entry, but any above code
  69. # can do it itself and skip setting __style to disable this code.
  70. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  71. # We aren't passing-through (no return 1 occured), do obligatory things ourselves.
  72. (( this_word = next_word ))
  73. _start_pos=$_end_pos
  74. return 0
  75. # vim:ft=zsh:et:sw=4