/
custom-colors
71 lines (65 loc) · 1.62 KB
/
custom-colors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
color_cmd() {
res=$(builtin type $1 2>/dev/null)
[ -z $res ] && return
case $res in
*'reserved word'*) color='magenta' ;;
*'an alias'*) color='cyan' ;;
*'shell builtin'*) color='green' ;;
*'shell function'*) color='green' ;;
*"$cms is"*) color='yello' ;;
*) color='red'
esac
case $cmd in
'sudo') state=1 ;;
'start') state=1 ;;
'time') state=1 ;;
'strace') state=1 ;;
*) state=2
esac
}
color_arg() {
case $1 in
'--'*) color='magenta' ;;
'-'*) color='cyan' ;;
*) color='red'
esac
}
color_string() {
case $1 in
'"'*) color='yellow' ;;
"'"*) color='yellow' ;;
*) color=''
esac
}
recolor-cmd() {
args=(${(z)BUFFER})
offset=0
state=1
region_highlight=()
for cmd in $args; do
if [ $state -eq 1 ]; then
color_cmd $cmd
elif [ $state -eq 2 ]; then
color_arg $cmd
if [[ "$color" =~ 'red' ]]; then
color_string $cmd
fi
fi
if [ -n "$color" ]; then
region_highlight=($region_highlight "$offset $((${#cmd}+offset))
fg=${color}")
fi
offset=$((offset+${#cmd}+1))
case $cmd in
*'|') state=1 ;;
*'&') state=1 ;;
*';') state=1 ;;
esac
done
}
check-cmd-self-insert() { zle .self-insert && recolor-cmd }
check-cmd-backward-delete-char() { zle .backward-delete-char && recolor-cmd }
#check-cmd-expand-or-complete() { zle .expand-or-complete } # && recolor-cmd }
zle -N self-insert check-cmd-self-insert
#zle -N expand-or-complete check-cmd-expand-or-complete
zle -N backward-delete-char check-cmd-backward-delete-char