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.

156 lines
7.2 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. # Copyright (C) 2019 by Philippe Troin (F-i-f on GitHub)
  4. #
  5. # Tracks ssh command and emits message when one tries to pass port to hostspec.
  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. emulate -LR zsh
  19. setopt extended_glob warn_create_global typeset_silent
  20. # This chroma guards that port number isn't passed in hostname (no :{port} occurs).
  21. (( next_word = 2 | 8192 ))
  22. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  23. local __style check_port=0 host_start_offset host_style user_style possible_host
  24. local -a match mbegin mend completions_user completions_host
  25. # First call, i.e. command starts, i.e. "grep" token etc.
  26. (( __first_call )) && {
  27. FAST_HIGHLIGHT[chroma-ssh-counter]=0
  28. FAST_HIGHLIGHT[chroma-ssh-counter-all]=1
  29. FAST_HIGHLIGHT[chroma-ssh-message]=""
  30. FAST_HIGHLIGHT[chroma-ssh-skip-two]=0
  31. return 1
  32. } || {
  33. # Following call, i.e. not the first one.
  34. # Check if chroma should end – test if token is of type
  35. # "starts new command", if so pass-through – chroma ends
  36. [[ "$__arg_type" = 3 ]] && return 2
  37. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  38. return 1
  39. fi
  40. (( FAST_HIGHLIGHT[chroma-ssh-counter-all] += 1 ))
  41. if [[ "$__wrd" = -* ]]; then
  42. # Detected option, add style for it.
  43. [[ "$__wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  44. __style=${FAST_THEME_NAME}single-hyphen-option
  45. if [[ "$__wrd" = (-b|-c|-D|-E|-e|-F|-I|-i|-J|-L|-l|-m|-O|-o|-p|Q|R|-S|-W|-w) ]]; then
  46. FAST_HIGHLIGHT[chroma-ssh-skip-two]=1
  47. fi
  48. else
  49. if (( FAST_HIGHLIGHT[chroma-ssh-skip-two] )); then
  50. FAST_HIGHLIGHT[chroma-ssh-skip-two]=0
  51. else
  52. # Count non-option tokens.
  53. (( FAST_HIGHLIGHT[chroma-ssh-counter] += 1 ))
  54. if [[ "${FAST_HIGHLIGHT[chroma-ssh-counter]}" -eq 1 ]]; then
  55. if [[ $__arg = (#b)(([^@]#)(@)|)(*) ]]
  56. then
  57. [[ -n $match[2] ]] \
  58. && {
  59. user_style=
  60. () {
  61. # Zstyle clobbers reply for sure
  62. zstyle -a ":completion:*:users" users completions_users
  63. }
  64. (( ! $#completions_users )) && completions_users=(${(k)userdirs})
  65. if (( $#completions_users )); then
  66. [[ $match[2] = ${~${:-(${(j:|:)completions_users})}} ]] \
  67. && user_style=${FAST_THEME_NAME}correct-subtle \
  68. || user_style=${FAST_THEME_NAME}incorrect-subtle
  69. fi
  70. [[ -n $user_style ]] \
  71. && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}-(mend[5]-mend[2]), __start >= 0 )) \
  72. && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$user_style]}")
  73. }
  74. [[ -n $match[3] ]] \
  75. && (( __start=__start_pos-${#PREBUFFER}+(mbegin[3]-mbegin[1]), __end=__end_pos-${#PREBUFFER}-(mend[5]-mend[3]), __start >= 0 )) \
  76. && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}subtle-separator]}")
  77. host_style=
  78. case $match[4] in
  79. (<->|<0-255>.<0-255>.<0-255>.<0-255>)
  80. host_style=${FAST_THEME_NAME}mathnum
  81. check_port=1
  82. ;;
  83. (([0-9a-fA-F][0-9a-fA-F:]#|)::([0-9a-fA-F:]#[0-9a-fA-F]|)|[0-9a-fA-F]##:[0-9a-fA-F:]#[0-9a-fA-F])
  84. host_style=${FAST_THEME_NAME}mathnum
  85. ;;
  86. (*)
  87. check_port=1
  88. ;;
  89. esac
  90. possible_host=$match[4]
  91. (( host_start_offset = mbegin[4] - mbegin[1], host_end_offset = 0 ))
  92. if (( check_port )) && [[ $possible_host = (#b)(*)(:[0-9]##) ]]; then
  93. (( __start=__start_pos-${#PREBUFFER}+(host_start_offset+mbegin[2]-mbegin[1]), __end=__end_pos-host_end_offset-${#PREBUFFER}, __start >= 0,
  94. host_end_offset+=mend[2]-mend[1] )) \
  95. && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}") \
  96. && possible_host=$match[1] \
  97. && FAST_HIGHLIGHT[chroma-ssh-message]+="Format of hostname incorrect, use -p to pass port number"
  98. fi
  99. if [[ -z $host_style ]]; then
  100. () {
  101. # Zstyle clobbers reply for sure
  102. local mbegin mend match reply
  103. zstyle -a ":completion:*:hosts" hosts completions_host
  104. }
  105. (( ! $#completions_host && $+_cache_hosts )) && completions_host=($_cache_hosts[*])
  106. if (( $#completions_host )); then
  107. [[ $possible_host = ${~${:-(${(j:|:)completions_host})}} ]] \
  108. && host_style=${FAST_THEME_NAME}correct-subtle \
  109. || host_style=${FAST_THEME_NAME}incorrect-subtle
  110. fi
  111. fi
  112. [[ -n $host_style ]] \
  113. && (( __start=__start_pos-${#PREBUFFER}+host_start_offset, __end=__end_pos-${#PREBUFFER}-host_end_offset, __start >= 0 )) \
  114. && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$host_style]}")
  115. else
  116. __style=${FAST_THEME_NAME}incorrect-subtle
  117. fi
  118. (( next_word = 1 ))
  119. fi
  120. fi
  121. fi
  122. if (( ${#${(z)BUFFER}} <= FAST_HIGHLIGHT[chroma-ssh-counter-all] )); then
  123. [[ -n "${FAST_HIGHLIGHT[chroma-ssh-message]}" ]] && zle -M "${FAST_HIGHLIGHT[chroma-ssh-message]}"
  124. fi
  125. }
  126. # Add region_highlight entry (via `reply' array).
  127. #
  128. # This is a common place of adding such entry, but any above code
  129. # can do it itself and skip setting __style to disable this code.
  130. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  131. # We aren't passing-through (no return 1 occured), do obligatory things ourselves.
  132. (( this_word = next_word ))
  133. _start_pos=$_end_pos
  134. return 0
  135. # vim:ft=zsh:et:sw=4