選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

159 行
5.5 KiB

13年前
13年前
13年前
13年前
13年前
13年前
  1. #!/usr/bin/env zsh
  2. # Copyleft 2010 zsh-syntax-highlighting contributors
  3. # http://github.com/nicoulaj/zsh-syntax-highlighting
  4. # All wrongs reserved.
  5. # vim: ft=zsh sw=2 ts=2 et
  6. # Token types styles.
  7. # See http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#SEC135
  8. typeset -A ZSH_SYNTAX_HIGHLIGHTING_STYLES
  9. ZSH_SYNTAX_HIGHLIGHTING_STYLES=(
  10. default 'none'
  11. unknown-token 'fg=red,bold'
  12. reserved-word 'fg=yellow,bold'
  13. alias 'fg=green,bold'
  14. builtin 'fg=cyan,bold'
  15. function 'fg=blue,bold'
  16. command 'fg=green,bold'
  17. path 'underline'
  18. globbing 'fg=blue,bold'
  19. single-hyphen-option 'fg=yellow'
  20. double-hyphen-option 'fg=yellow'
  21. single-quoted-argument 'fg=yellow'
  22. double-quoted-argument 'fg=yellow'
  23. dollar-double-quoted-argument 'fg=cyan'
  24. back-quoted-argument 'fg=cyan,bold'
  25. back-double-quoted-argument 'fg=magenta'
  26. )
  27. # Tokens that are always followed by a command.
  28. ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS=(
  29. '|'
  30. '||'
  31. ';'
  32. '&'
  33. '&&'
  34. 'sudo'
  35. 'start'
  36. 'time'
  37. 'strace'
  38. 'noglob'
  39. 'command'
  40. 'builtin'
  41. )
  42. # ZLE events that trigger an update of the highlighting.
  43. ZSH_HIGHLIGHT_ZLE_UPDATE_EVENTS=(
  44. self-insert
  45. magic-space
  46. delete-char
  47. delete-char-or-list
  48. backward-delete-char
  49. kill-word
  50. backward-kill-word
  51. up-line-or-history
  52. down-line-or-history
  53. beginning-of-history
  54. end-of-history
  55. accept-line
  56. accept-line-and-down-history
  57. undo
  58. redo
  59. yank
  60. complete-word
  61. expand-or-complete
  62. expand-or-complete-prefix
  63. )
  64. # Check if the argument is a path.
  65. _zsh_check-path() {
  66. [[ -z $arg ]] && return 1
  67. [[ -e $arg ]] && return 0
  68. [[ ! -e ${arg:h} ]] && return 1
  69. [[ ${#BUFFER} == $end_pos && -n $(print $arg*(N)) ]] && return 0
  70. return 1
  71. }
  72. # Highlight special chars inside double-quoted strings
  73. _zsh_highlight-string() {
  74. setopt localoptions noksharrays
  75. local i j k style
  76. # Starting quote is at 1, so start parsing at offset 2 in the string.
  77. for (( i = 2 ; i < end_pos - start_pos ; i += 1 )) ; do
  78. (( j = i + start_pos - 1 ))
  79. (( k = j + 1 ))
  80. case "$arg[$i]" in
  81. '$') style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[dollar-double-quoted-argument];;
  82. "\\") style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[back-double-quoted-argument]
  83. (( k += 1 )) # Color following char too.
  84. (( i += 1 )) # Skip parsing the escaped char.
  85. ;;
  86. *) continue;;
  87. esac
  88. region_highlight+=("$j $k $style")
  89. done
  90. }
  91. # Recolorize the current ZLE buffer.
  92. _zsh_highlight-zle-buffer() {
  93. setopt localoptions extendedglob bareglobqual
  94. local colorize=true
  95. local start_pos=0
  96. local end_pos arg style
  97. region_highlight=()
  98. for arg in ${(z)BUFFER}; do
  99. local substr_color=0
  100. ((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]##[[:space:]]#}}))
  101. ((end_pos=$start_pos+${#arg}))
  102. if $colorize; then
  103. colorize=false
  104. res=$(LC_ALL=C builtin type -w $arg 2>/dev/null)
  105. case $res in
  106. *': reserved') style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[reserved-word];;
  107. *': alias') style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[alias]
  108. local aliased_command=${$(alias $arg)#*=}
  109. [[ ${${ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS[(r)$aliased_command]:-}:+yes} = 'yes' ]] && ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS+=($arg)
  110. ;;
  111. *': builtin') style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[builtin];;
  112. *': function') style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[function];;
  113. *': command') style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[command];;
  114. *) _zsh_check-path && style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[path] || style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[unknown-token];;
  115. esac
  116. else
  117. case $arg in
  118. '--'*) style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[double-hyphen-option];;
  119. '-'*) style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[single-hyphen-option];;
  120. "'"*"'") style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[single-quoted-argument];;
  121. '"'*'"') style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[double-quoted-argument]
  122. region_highlight+=("$start_pos $end_pos $style")
  123. _zsh_highlight-string
  124. substr_color=1
  125. ;;
  126. '`'*'`') style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[back-quoted-argument];;
  127. *"*"*) style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[globbing];;
  128. *) _zsh_check-path && style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[path] || style=$ZSH_SYNTAX_HIGHLIGHTING_STYLES[default];;
  129. esac
  130. fi
  131. [[ $substr_color = 0 ]] && region_highlight+=("$start_pos $end_pos $style")
  132. [[ ${${ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS[(r)${arg//|/\|}]:-}:+yes} = 'yes' ]] && colorize=true
  133. start_pos=$end_pos
  134. done
  135. }
  136. # Special treatment for completion/expansion events:
  137. # For each *complete* function, we create a widget which mimics the original
  138. # and use this orig-* version inside the new colorized zle function (the dot
  139. # idiom used for all others doesn't work right for these functions for some
  140. # reason). You can see the default setup using "zle -l -L".
  141. # Bind ZLE events to highlighting function.
  142. for f in $ZSH_HIGHLIGHT_ZLE_UPDATE_EVENTS; do
  143. case $f in
  144. *complete*)
  145. eval "zle -C orig-$f .$f _main_complete ; $f() { builtin zle orig-$f && _zsh_highlight-zle-buffer } ; zle -N $f"
  146. ;;
  147. *)
  148. eval "$f() { builtin zle .$f && _zsh_highlight-zle-buffer } ; zle -N $f"
  149. ;;
  150. esac
  151. done