mirror of
https://github.com/zsh-users/zsh-syntax-highlighting.git
synced 2025-02-13 10:05:31 +08:00
Add highlighting for files based on $LS_COLORS environment variable.
This commit is contained in:
parent
85d1268b5a
commit
28a5c6e3f0
@ -66,7 +66,7 @@ _zsh_highlight_main_highlighter()
|
|||||||
{
|
{
|
||||||
emulate -L zsh
|
emulate -L zsh
|
||||||
setopt localoptions extendedglob bareglobqual
|
setopt localoptions extendedglob bareglobqual
|
||||||
local start_pos=0 end_pos highlight_glob=true new_expression=true arg style sudo=false sudo_arg=false
|
local start_pos=0 end_pos highlight_glob=true new_expression=true arg style lsstyle sudo=false sudo_arg=false
|
||||||
typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR
|
typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR
|
||||||
typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
|
typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
|
||||||
typeset -a ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS
|
typeset -a ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS
|
||||||
@ -84,7 +84,7 @@ _zsh_highlight_main_highlighter()
|
|||||||
)
|
)
|
||||||
|
|
||||||
for arg in ${(z)BUFFER}; do
|
for arg in ${(z)BUFFER}; do
|
||||||
local substr_color=0
|
local substr_color=0 isfile=false
|
||||||
local style_override=""
|
local style_override=""
|
||||||
[[ $start_pos -eq 0 && $arg = 'noglob' ]] && highlight_glob=false
|
[[ $start_pos -eq 0 && $arg = 'noglob' ]] && highlight_glob=false
|
||||||
((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]##[[:space:]]#}}))
|
((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]##[[:space:]]#}}))
|
||||||
@ -136,6 +136,7 @@ _zsh_highlight_main_highlighter()
|
|||||||
else
|
else
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
|
style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
|
||||||
fi
|
fi
|
||||||
|
_zsh_highlight_main_highlighter_check_file && isfile=true
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
@ -160,14 +161,21 @@ _zsh_highlight_main_highlighter()
|
|||||||
else
|
else
|
||||||
style=$ZSH_HIGHLIGHT_STYLES[default]
|
style=$ZSH_HIGHLIGHT_STYLES[default]
|
||||||
fi
|
fi
|
||||||
|
_zsh_highlight_main_highlighter_check_file && isfile=true
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
# if a style_override was set (eg in _zsh_highlight_main_highlighter_check_path), use it
|
# if a style_override was set (eg in _zsh_highlight_main_highlighter_check_path), use it
|
||||||
[[ -n $style_override ]] && style=$ZSH_HIGHLIGHT_STYLES[$style_override]
|
[[ -n $style_override ]] && style=$ZSH_HIGHLIGHT_STYLES[$style_override]
|
||||||
|
if [[ $isfile == true ]]; then
|
||||||
|
start_file_pos=$((start_pos+${#arg}-${#arg:t}))
|
||||||
|
end_file_pos=$end_pos
|
||||||
|
end_pos=$((end_pos-${#arg:t}))
|
||||||
|
region_highlight+=("$start_file_pos $end_file_pos $lsstyle")
|
||||||
|
fi
|
||||||
[[ $substr_color = 0 ]] && region_highlight+=("$start_pos $end_pos $style")
|
[[ $substr_color = 0 ]] && region_highlight+=("$start_pos $end_pos $style")
|
||||||
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && new_expression=true
|
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && new_expression=true
|
||||||
start_pos=$end_pos
|
[[ $isfile == true ]] && start_pos=$end_file_pos || start_pos=$end_pos
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,3 +258,82 @@ _zsh_highlight_main_highlighter_check_command()
|
|||||||
for p in $path; do prefixed_command+=( $p/${arg}*(N) ); done
|
for p in $path; do prefixed_command+=( $p/${arg}*(N) ); done
|
||||||
[[ ${BUFFER[1]} != "-" && ${#LBUFFER} == $end_pos && $#prefixed_command > 0 ]] && return 0 || return 1
|
[[ ${BUFFER[1]} != "-" && ${#LBUFFER} == $end_pos && $#prefixed_command > 0 ]] && return 0 || return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## Fill table with colors and file types from $LS_COLORS
|
||||||
|
_zsh_highlight_files_highlighter_fill_table_of_types()
|
||||||
|
{
|
||||||
|
local group type code ncolors=$(echotc Co)
|
||||||
|
local -a attrib
|
||||||
|
|
||||||
|
for group in ${(s.:.)LS_COLORS}; do
|
||||||
|
type=${group%=*}
|
||||||
|
code=${group#*=}
|
||||||
|
attrib=()
|
||||||
|
takeattrib ${(s.;.)code}
|
||||||
|
ZSH_HIGHLIGHT_FILES+=($type ${(j.,.)attrib})
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
## Take attributes from unfolded $LS_COLORS code
|
||||||
|
takeattrib()
|
||||||
|
{
|
||||||
|
while [ "$#" -gt 0 ]; do
|
||||||
|
[[ $1 == 38 && $2 == 5 ]] && {attrib+=("fg=$3"); shift 3; continue}
|
||||||
|
[[ $1 == 48 && $2 == 5 ]] && {attrib+=("bg=$3"); shift 3; continue}
|
||||||
|
case $1 in
|
||||||
|
00|0) attrib+=("none"); shift;;
|
||||||
|
01|1) attrib+=("bold" ); shift;;
|
||||||
|
02|2) attrib+=("faint"); shift;;
|
||||||
|
03|3) attrib+=("italic"); shift;;
|
||||||
|
04|4) attrib+=("underscore"); shift;;
|
||||||
|
05|5) attrib+=("blink"); shift;;
|
||||||
|
07|7) attrib+=("standout"); shift;;
|
||||||
|
08|8) attrib+=("concealed"); shift;;
|
||||||
|
3[0-7]) attrib+=("fg=$(($1-30))"); shift;;
|
||||||
|
4[0-7]) attrib+=("bg=$(($1-40))"); shift;;
|
||||||
|
9[0-7]) [[ $ncolors == 256 ]] && attrib+=("fg=$(($1-82))") || attrib+=("fg=$(($1-90))" "bold"); shift;;
|
||||||
|
10[0-7]) [[ $ncolors == 256 ]] && attrib+=("bg=$(($1-92))") || attrib+=("bg=$(($1-100))" "bold"); shift;;
|
||||||
|
*) shift;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
## Check if the argument is a file, if yes change the style accordingly
|
||||||
|
_zsh_highlight_main_highlighter_check_file()
|
||||||
|
{
|
||||||
|
setopt localoptions nonomatch
|
||||||
|
local expanded_arg matched_file
|
||||||
|
|
||||||
|
expanded_arg=${(Q)~arg}
|
||||||
|
[[ -z $expanded_arg ]] && return 1
|
||||||
|
[[ -d $expanded_arg ]] && return 1
|
||||||
|
[[ ${BUFFER[1]} != "-" && ${#LBUFFER} == $end_pos ]] && matched_file=(${expanded_arg}*(Noa^/[1]))
|
||||||
|
[[ -e $expanded_arg || -e $matched_file ]] && lsstyle=none || return 1
|
||||||
|
|
||||||
|
# [[ rs ]]
|
||||||
|
# [[ -d $expanded_arg || -d $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[di] && return 0
|
||||||
|
[[ -h $expanded_arg || -h $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[ln] && return 0
|
||||||
|
# [[ mh ]]
|
||||||
|
[[ -p $expanded_arg || -p $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[pi] && return 0
|
||||||
|
[[ -S $expanded_arg || -S $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[so] && return 0
|
||||||
|
# [[ do ]]
|
||||||
|
[[ -b $expanded_arg || -b $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[bd] && return 0
|
||||||
|
[[ -c $expanded_arg || -c $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[cd] && return 0
|
||||||
|
# [[ or ]]
|
||||||
|
# [[ mi ]]
|
||||||
|
[[ -u $expanded_arg || -u $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[su] && return 0
|
||||||
|
[[ -g $expanded_arg || -g $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[sg] && return 0
|
||||||
|
# [[ ca ]]
|
||||||
|
# [[ tw ]]
|
||||||
|
# [[ ow ]]
|
||||||
|
[[ -k $expanded_arg || -k $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[st] && return 0
|
||||||
|
[[ -x $expanded_arg || -x $matched_file ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[ex] && return 0
|
||||||
|
|
||||||
|
[[ -e $expanded_arg ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[*.$expanded_arg:e] && return 0
|
||||||
|
[[ -n $matched_file:e ]] && lsstyle=$ZSH_HIGHLIGHT_FILES[*.$matched_file:e] && return 0
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
## Fill table only once, at the initialization process
|
||||||
|
_zsh_highlight_files_highlighter_fill_table_of_types
|
||||||
|
@ -35,6 +35,7 @@
|
|||||||
|
|
||||||
# Array declaring active highlighters names.
|
# Array declaring active highlighters names.
|
||||||
typeset -ga ZSH_HIGHLIGHT_HIGHLIGHTERS
|
typeset -ga ZSH_HIGHLIGHT_HIGHLIGHTERS
|
||||||
|
typeset -gA ZSH_HIGHLIGHT_FILES
|
||||||
|
|
||||||
# Update ZLE buffer syntax highlighting.
|
# Update ZLE buffer syntax highlighting.
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user