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.

384 lines
16 KiB

  1. # -------------------------------------------------------------------------------------------------
  2. # Copyright (c) 2010-2016 zsh-syntax-highlighting contributors
  3. # Copyright (c) 2017-2019 Sebastian Gniazdowski (modifications)
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without modification, are permitted
  7. # provided that the following conditions are met:
  8. #
  9. # * Redistributions of source code must retain the above copyright notice, this list of conditions
  10. # and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright notice, this list of
  12. # conditions and the following disclaimer in the documentation and/or other materials provided
  13. # with the distribution.
  14. # * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
  15. # may be used to endorse or promote products derived from this software without specific prior
  16. # written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  19. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  21. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  24. # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  25. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. # -------------------------------------------------------------------------------------------------
  27. # -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
  28. # vim: ft=zsh sw=2 ts=2 et
  29. # -------------------------------------------------------------------------------------------------
  30. # Standarized way of handling finding plugin dir,
  31. # regardless of functionargzero and posixargzero,
  32. # and with an option for a plugin manager to alter
  33. # the plugin directory (i.e. set ZERO parameter)
  34. # http://zdharma.org/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
  35. 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  36. 0="${${(M)0:#/*}:-$PWD/$0}"
  37. typeset -g FAST_HIGHLIGHT_VERSION=1.55
  38. typeset -g FAST_BASE_DIR="${0:h}"
  39. typeset -ga _FAST_MAIN_CACHE
  40. # Holds list of indices pointing at brackets that
  41. # are complex, i.e. e.g. part of "[[" in [[ ... ]]
  42. typeset -ga _FAST_COMPLEX_BRACKETS
  43. typeset -g FAST_WORK_DIR=${FAST_WORK_DIR:-${XDG_CACHE_HOME:-~/.cache}/fast-syntax-highlighting}
  44. : ${FAST_WORK_DIR:=$FAST_BASE_DIR}
  45. # Expand any tilde in the (supposed) path.
  46. FAST_WORK_DIR=${~FAST_WORK_DIR}
  47. # Last (currently, possibly) loaded plugin isn't "fast-syntax-highlighting"?
  48. # And FPATH isn't containing plugin dir?
  49. if [[ ${zsh_loaded_plugins[-1]} != */fast-syntax-highlighting && -z ${fpath[(r)${0:h}]} ]]
  50. then
  51. fpath+=( "${0:h}" )
  52. fi
  53. if [[ ! -w $FAST_WORK_DIR ]]; then
  54. FAST_WORK_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/fsh"
  55. command mkdir -p "$FAST_WORK_DIR"
  56. fi
  57. # Invokes each highlighter that needs updating.
  58. # This function is supposed to be called whenever the ZLE state changes.
  59. _zsh_highlight()
  60. {
  61. # Store the previous command return code to restore it whatever happens.
  62. local ret=$?
  63. # Remove all highlighting in isearch, so that only the underlining done by zsh itself remains.
  64. # For details see FAQ entry 'Why does syntax highlighting not work while searching history?'.
  65. if [[ $WIDGET == zle-isearch-update ]] && ! (( $+ISEARCHMATCH_ACTIVE )); then
  66. region_highlight=()
  67. return $ret
  68. fi
  69. emulate -LR zsh
  70. setopt extendedglob warncreateglobal typesetsilent noshortloops
  71. local REPLY # don't leak $REPLY into global scope
  72. local -a reply
  73. # Do not highlight if there are more than 300 chars in the buffer. It's most
  74. # likely a pasted command or a huge list of files in that case..
  75. [[ -n ${ZSH_HIGHLIGHT_MAXLENGTH:-} ]] && [[ $#BUFFER -gt $ZSH_HIGHLIGHT_MAXLENGTH ]] && return $ret
  76. # Do not highlight if there are pending inputs (copy/paste).
  77. [[ $PENDING -gt 0 ]] && return $ret
  78. # Reset region highlight to build it from scratch
  79. # may need to remove path_prefix highlighting when the line ends
  80. if [[ $WIDGET == zle-line-finish ]] || _zsh_highlight_buffer_modified; then
  81. -fast-highlight-init
  82. -fast-highlight-process "$PREBUFFER" "$BUFFER" 0
  83. (( FAST_HIGHLIGHT[use_brackets] )) && {
  84. _FAST_MAIN_CACHE=( $reply )
  85. -fast-highlight-string-process "$PREBUFFER" "$BUFFER"
  86. }
  87. region_highlight=( $reply )
  88. else
  89. local char="${BUFFER[CURSOR+1]}"
  90. if [[ "$char" = ["{([])}"] || "${FAST_HIGHLIGHT[prev_char]}" = ["{([])}"] ]]; then
  91. FAST_HIGHLIGHT[prev_char]="$char"
  92. (( FAST_HIGHLIGHT[use_brackets] )) && {
  93. reply=( $_FAST_MAIN_CACHE )
  94. -fast-highlight-string-process "$PREBUFFER" "$BUFFER"
  95. region_highlight=( $reply )
  96. }
  97. fi
  98. fi
  99. {
  100. local cache_place
  101. local -a region_highlight_copy
  102. # Re-apply zle_highlight settings
  103. # region
  104. if (( REGION_ACTIVE == 1 )); then
  105. _zsh_highlight_apply_zle_highlight region standout "$MARK" "$CURSOR"
  106. elif (( REGION_ACTIVE == 2 )); then
  107. () {
  108. local needle=$'\n'
  109. integer min max
  110. if (( MARK > CURSOR )) ; then
  111. min=$CURSOR max=$(( MARK + 1 ))
  112. else
  113. min=$MARK max=$CURSOR
  114. fi
  115. (( min = ${${BUFFER[1,$min]}[(I)$needle]} ))
  116. (( max += ${${BUFFER:($max-1)}[(i)$needle]} - 1 ))
  117. _zsh_highlight_apply_zle_highlight region standout "$min" "$max"
  118. }
  119. fi
  120. # yank / paste (zsh-5.1.1 and newer)
  121. (( $+YANK_ACTIVE )) && (( YANK_ACTIVE )) && _zsh_highlight_apply_zle_highlight paste standout "$YANK_START" "$YANK_END"
  122. # isearch
  123. (( $+ISEARCHMATCH_ACTIVE )) && (( ISEARCHMATCH_ACTIVE )) && _zsh_highlight_apply_zle_highlight isearch underline "$ISEARCHMATCH_START" "$ISEARCHMATCH_END"
  124. # suffix
  125. (( $+SUFFIX_ACTIVE )) && (( SUFFIX_ACTIVE )) && _zsh_highlight_apply_zle_highlight suffix bold "$SUFFIX_START" "$SUFFIX_END"
  126. return $ret
  127. } always {
  128. typeset -g _ZSH_HIGHLIGHT_PRIOR_BUFFER="$BUFFER"
  129. typeset -g _ZSH_HIGHLIGHT_PRIOR_RACTIVE="$REGION_ACTIVE"
  130. typeset -gi _ZSH_HIGHLIGHT_PRIOR_CURSOR=$CURSOR
  131. }
  132. }
  133. # Apply highlighting based on entries in the zle_highlight array.
  134. # This function takes four arguments:
  135. # 1. The exact entry (no patterns) in the zle_highlight array:
  136. # region, paste, isearch, or suffix
  137. # 2. The default highlighting that should be applied if the entry is unset
  138. # 3. and 4. Two integer values describing the beginning and end of the
  139. # range. The order does not matter.
  140. _zsh_highlight_apply_zle_highlight() {
  141. local entry="$1" default="$2"
  142. integer first="$3" second="$4"
  143. # read the relevant entry from zle_highlight
  144. local region="${zle_highlight[(r)${entry}:*]}"
  145. if [[ -z "$region" ]]; then
  146. # entry not specified at all, use default value
  147. region=$default
  148. else
  149. # strip prefix
  150. region="${region#${entry}:}"
  151. # no highlighting when set to the empty string or to 'none'
  152. if [[ -z "$region" ]] || [[ "$region" == none ]]; then
  153. return
  154. fi
  155. fi
  156. integer start end
  157. if (( first < second )); then
  158. start=$first end=$second
  159. else
  160. start=$second end=$first
  161. fi
  162. region_highlight+=("$start $end $region")
  163. }
  164. # -------------------------------------------------------------------------------------------------
  165. # API/utility functions for highlighters
  166. # -------------------------------------------------------------------------------------------------
  167. # Whether the command line buffer has been modified or not.
  168. #
  169. # Returns 0 if the buffer has changed since _zsh_highlight was last called.
  170. _zsh_highlight_buffer_modified()
  171. {
  172. [[ "${_ZSH_HIGHLIGHT_PRIOR_BUFFER:-}" != "$BUFFER" ]] || [[ "$REGION_ACTIVE" != "$_ZSH_HIGHLIGHT_PRIOR_RACTIVE" ]] || { _zsh_highlight_cursor_moved && [[ "$REGION_ACTIVE" = 1 || "$REGION_ACTIVE" = 2 ]] }
  173. }
  174. # Whether the cursor has moved or not.
  175. #
  176. # Returns 0 if the cursor has moved since _zsh_highlight was last called.
  177. _zsh_highlight_cursor_moved()
  178. {
  179. [[ -n $CURSOR ]] && [[ -n ${_ZSH_HIGHLIGHT_PRIOR_CURSOR-} ]] && (($_ZSH_HIGHLIGHT_PRIOR_CURSOR != $CURSOR))
  180. }
  181. # -------------------------------------------------------------------------------------------------
  182. # Setup functions
  183. # -------------------------------------------------------------------------------------------------
  184. # Helper for _zsh_highlight_bind_widgets
  185. # $1 is name of widget to call
  186. _zsh_highlight_call_widget()
  187. {
  188. integer ret
  189. builtin zle "$@"
  190. ret=$?
  191. _zsh_highlight
  192. return $ret
  193. }
  194. # Rebind all ZLE widgets to make them invoke _zsh_highlights.
  195. _zsh_highlight_bind_widgets()
  196. {
  197. setopt localoptions noksharrays
  198. local -F2 SECONDS
  199. local prefix=orig-s${SECONDS/./}-r$(( RANDOM % 1000 )) # unique each time, in case we're sourced more than once
  200. # Load ZSH module zsh/zleparameter, needed to override user defined widgets.
  201. zmodload zsh/zleparameter 2>/dev/null || {
  202. print -r -- >&2 'zsh-syntax-highlighting: failed loading zsh/zleparameter.'
  203. return 1
  204. }
  205. # Override ZLE widgets to make them invoke _zsh_highlight.
  206. local -U widgets_to_bind
  207. widgets_to_bind=(${${(k)widgets}:#(.*|run-help|which-command|beep|set-local-history|yank|zle-line-pre-redraw|zle-keymap-select)})
  208. # Always wrap special zle-line-finish widget. This is needed to decide if the
  209. # current line ends and special highlighting logic needs to be applied.
  210. # E.g. remove cursor imprint, don't highlight partial paths, ...
  211. widgets_to_bind+=(zle-line-finish)
  212. # Always wrap special zle-isearch-update widget to be notified of updates in isearch.
  213. # This is needed because we need to disable highlighting in that case.
  214. widgets_to_bind+=(zle-isearch-update)
  215. local cur_widget
  216. for cur_widget in $widgets_to_bind; do
  217. case $widgets[$cur_widget] in
  218. # Already rebound event: do nothing.
  219. user:_zsh_highlight_widget_*);;
  220. # The "eval"'s are required to make $cur_widget a closure: the value of the parameter at function
  221. # definition time is used.
  222. #
  223. # We can't use ${0/_zsh_highlight_widget_} because these widgets are always invoked with
  224. # NO_function_argzero, regardless of the option's setting here.
  225. # User defined widget: override and rebind old one with prefix "orig-".
  226. user:*) zle -N -- $prefix-$cur_widget ${widgets[$cur_widget]#*:}
  227. eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }"
  228. zle -N -- $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
  229. # Completion widget: override and rebind old one with prefix "orig-".
  230. completion:*) zle -C $prefix-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]}
  231. eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }"
  232. zle -N -- $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
  233. # Builtin widget: override and make it call the builtin ".widget".
  234. builtin) eval "_zsh_highlight_widget_${(q)prefix}-${(q)cur_widget}() { _zsh_highlight_call_widget .${(q)cur_widget} -- \"\$@\" }"
  235. zle -N -- $cur_widget _zsh_highlight_widget_$prefix-$cur_widget;;
  236. # Incomplete or nonexistent widget: Bind to z-sy-h directly.
  237. *)
  238. if [[ $cur_widget == zle-* ]] && [[ -z $widgets[$cur_widget] ]]; then
  239. _zsh_highlight_widget_${cur_widget}() { :; _zsh_highlight }
  240. zle -N -- $cur_widget _zsh_highlight_widget_$cur_widget
  241. else
  242. # Default: unhandled case.
  243. print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget ${(qq)cur_widget}"
  244. fi
  245. esac
  246. done
  247. }
  248. # -------------------------------------------------------------------------------------------------
  249. # Setup
  250. # -------------------------------------------------------------------------------------------------
  251. # Try binding widgets.
  252. _zsh_highlight_bind_widgets || {
  253. print -r -- >&2 'zsh-syntax-highlighting: failed binding ZLE widgets, exiting.'
  254. return 1
  255. }
  256. # Reset scratch variables when commandline is done.
  257. _zsh_highlight_preexec_hook()
  258. {
  259. typeset -g _ZSH_HIGHLIGHT_PRIOR_BUFFER=
  260. typeset -gi _ZSH_HIGHLIGHT_PRIOR_CURSOR=0
  261. typeset -ga _FAST_MAIN_CACHE
  262. _FAST_MAIN_CACHE=()
  263. }
  264. autoload -Uz add-zsh-hook
  265. add-zsh-hook preexec _zsh_highlight_preexec_hook 2>/dev/null || {
  266. print -r -- >&2 'zsh-syntax-highlighting: failed loading add-zsh-hook.'
  267. }
  268. /fshdbg() {
  269. print -r -- "$@" >>! /tmp/reply
  270. }
  271. ZSH_HIGHLIGHT_MAXLENGTH=10000
  272. # Load zsh/parameter module if available
  273. zmodload zsh/parameter 2>/dev/null
  274. zmodload zsh/system 2>/dev/null
  275. autoload -Uz -- is-at-least fast-theme .fast-read-ini-file .fast-run-git-command \
  276. .fast-make-targets .fast-run-command .fast-zts-read-all
  277. autoload -Uz -- →chroma/-git.ch →chroma/-hub.ch →chroma/-lab.ch →chroma/-example.ch \
  278. →chroma/-grep.ch →chroma/-perl.ch →chroma/-make.ch →chroma/-awk.ch \
  279. →chroma/-vim.ch →chroma/-source.ch →chroma/-sh.ch →chroma/-docker.ch \
  280. →chroma/-autoload.ch →chroma/-ssh.ch →chroma/-scp.ch →chroma/-which.ch \
  281. →chroma/-printf.ch →chroma/-ruby.ch →chroma/-whatis.ch →chroma/-alias.ch \
  282. →chroma/-subcommand.ch →chroma/-autorandr.ch →chroma/-nmcli.ch \
  283. →chroma/-fast-theme.ch →chroma/-node.ch →chroma/-fpath_peq.ch \
  284. →chroma/-precommand.ch →chroma/-subversion.ch →chroma/-ionice.ch \
  285. →chroma/-nice.ch →chroma/main-chroma.ch →chroma/-ogit.ch →chroma/-zinit.ch
  286. source "${0:h}/fast-highlight"
  287. source "${0:h}/fast-string-highlight"
  288. local __fsyh_theme
  289. zstyle -s :plugin:fast-syntax-highlighting theme __fsyh_theme
  290. [[ ( "${+termcap}" != 1 || "${termcap[Co]}" != <-> || "${termcap[Co]}" -lt "256" ) && "$__fsyh_theme" = (default|) ]] && {
  291. FAST_HIGHLIGHT_STYLES[defaultvariable]="none"
  292. FAST_HIGHLIGHT_STYLES[defaultglobbing-ext]="fg=blue,bold"
  293. FAST_HIGHLIGHT_STYLES[defaulthere-string-text]="bg=blue"
  294. FAST_HIGHLIGHT_STYLES[defaulthere-string-var]="fg=cyan,bg=blue"
  295. FAST_HIGHLIGHT_STYLES[defaultcorrect-subtle]="bg=blue"
  296. FAST_HIGHLIGHT_STYLES[defaultsubtle-bg]="bg=blue"
  297. [[ "${FAST_HIGHLIGHT_STYLES[variable]}" = "fg=113" ]] && FAST_HIGHLIGHT_STYLES[variable]="none"
  298. [[ "${FAST_HIGHLIGHT_STYLES[globbing-ext]}" = "fg=13" ]] && FAST_HIGHLIGHT_STYLES[globbing-ext]="fg=blue,bold"
  299. [[ "${FAST_HIGHLIGHT_STYLES[here-string-text]}" = "bg=18" ]] && FAST_HIGHLIGHT_STYLES[here-string-text]="bg=blue"
  300. [[ "${FAST_HIGHLIGHT_STYLES[here-string-var]}" = "fg=cyan,bg=18" ]] && FAST_HIGHLIGHT_STYLES[here-string-var]="fg=cyan,bg=blue"
  301. [[ "${FAST_HIGHLIGHT_STYLES[correct-subtle]}" = "fg=12" ]] && FAST_HIGHLIGHT_STYLES[correct-subtle]="bg=blue"
  302. [[ "${FAST_HIGHLIGHT_STYLES[subtle-bg]}" = "bg=18" ]] && FAST_HIGHLIGHT_STYLES[subtle-bg]="bg=blue"
  303. }
  304. unset __fsyh_theme
  305. alias fsh-alias=fast-theme
  306. -fast-highlight-fill-option-variables
  307. if [[ ! -e $FAST_WORK_DIR/secondary_theme.zsh ]] {
  308. if { type curl &>/dev/null } {
  309. curl -fsSL -o "$FAST_WORK_DIR/secondary_theme.zsh" \
  310. https://raw.githubusercontent.com/zdharma/fast-syntax-highlighting/master/share/free_theme.zsh \
  311. &>/dev/null
  312. } elif { type wget &>/dev/null } {
  313. wget -O "$FAST_WORK_DIR/secondary_theme.zsh" \
  314. https://raw.githubusercontent.com/zdharma/fast-syntax-highlighting/master/share/free_theme.zsh \
  315. &>/dev/null
  316. }
  317. touch "$FAST_WORK_DIR/secondary_theme.zsh"
  318. }
  319. if [[ $(uname -a) = (#i)*darwin* ]] {
  320. typeset -gA FAST_HIGHLIGHT
  321. FAST_HIGHLIGHT[chroma-man]=
  322. }
  323. [[ $COLORTERM == (24bit|truecolor) || ${terminfo[colors]} -eq 16777216 ]] || zmodload zsh/nearcolor &>/dev/null