]> gitweb.pimeys.fr Git - config-20-100.git/blob - .zsh/rc/base/15_functions
Ajout du zshrc, même s'il manque encore de commentaires etc.
[config-20-100.git] / .zsh / rc / base / 15_functions
1 #!/bin/zsh
2
3 # utility functions {{{
4 # this function checks if a command exists and returns either true
5 # or false. This avoids using 'which' and 'whence', which will
6 # avoid problems with aliases for which on certain weird systems. :-)
7 # Usage: check_com [-c|-g] word
8 # -c only checks for external commands
9 # -g does the usual tests and also checks for global aliases
10 check_com() {
11 emulate -L zsh
12 local -i comonly gatoo
13
14 if [[ $1 == '-c' ]] ; then
15 (( comonly = 1 ))
16 shift
17 elif [[ $1 == '-g' ]] ; then
18 (( gatoo = 1 ))
19 else
20 (( comonly = 0 ))
21 (( gatoo = 0 ))
22 fi
23
24 if (( ${#argv} != 1 )) ; then
25 printf 'usage: check_com [-cg] <command>\n' >&2
26 return 1
27 fi
28
29 if (( comonly > 0 )) ; then
30 [[ -n ${commands[$1]} ]] && return 0
31 return 1
32 fi
33
34 if [[ -n ${commands[$1]} ]] \
35 || [[ -n ${functions[$1]} ]] \
36 || [[ -n ${aliases[$1]} ]] \
37 || [[ -n ${reswords[(r)$1]} ]] ; then
38
39 return 0
40 fi
41
42 if (( gatoo > 0 )) && [[ -n ${galiases[$1]} ]] ; then
43 return 0
44 fi
45
46 return 1
47 }
48
49 # creates an alias and precedes the command with
50 # sudo if $EUID is not zero.
51 salias() {
52 emulate -L zsh
53 local only=0 ; local multi=0
54 while [[ $1 == -* ]] ; do
55 case $1 in
56 (-o) only=1 ;;
57 (-a) multi=1 ;;
58 (--) shift ; break ;;
59 (-h)
60 printf 'usage: salias [-h|-o|-a] <alias-expression>\n'
61 printf ' -h shows this help text.\n'
62 printf ' -a replace '\'' ; '\'' sequences with '\'' ; sudo '\''.\n'
63 printf ' be careful using this option.\n'
64 printf ' -o only sets an alias if a preceding sudo would be needed.\n'
65 return 0
66 ;;
67 (*) printf "unkown option: '%s'\n" "$1" ; return 1 ;;
68 esac
69 shift
70 done
71
72 if (( ${#argv} > 1 )) ; then
73 printf 'Too many arguments %s\n' "${#argv}"
74 return 1
75 fi
76
77 key="${1%%\=*}" ; val="${1#*\=}"
78 if (( EUID == 0 )) && (( only == 0 )); then
79 alias -- "${key}=${val}"
80 elif (( EUID > 0 )) ; then
81 (( multi > 0 )) && val="${val// ; / ; sudo }"
82 alias -- "${key}=sudo ${val}"
83 fi
84
85 return 0
86 }
87
88 # Check if we can read given files and source those we can.
89 xsource() {
90 emulate -L zsh
91 if (( ${#argv} < 1 )) ; then
92 printf 'usage: xsource FILE(s)...\n' >&2
93 return 1
94 fi
95
96 while (( ${#argv} > 0 )) ; do
97 [[ -r $1 ]] && source $1
98 shift
99 done
100 return 0
101 }
102
103 # Check if we can read a given file and 'cat(1)' it.
104 xcat() {
105 emulate -L zsh
106 if (( ${#argv} != 1 )) ; then
107 printf 'usage: xcat FILE\n' >&2
108 return 1
109 fi
110
111 [[ -r $1 ]] && cat $1
112 return 0
113 }
114
115 # Remove these functions again, they are of use only in these
116 # setup files. This should be called at the end of .zshrc.
117 xunfunction() {
118 emulate -L zsh
119 local -a funcs
120 funcs=(salias xcat xsource xunfunction zrcautoload)
121
122 for func in $funcs ; do
123 [[ -n ${functions[$func]} ]] \
124 && unfunction $func
125 done
126 return 0
127 }
128
129 #}}}
130
131 # Handles command not fount error
132 function command_not_found_handler() {
133 emulate -L zsh
134 if [[ -x /usr/share/command-not-found/command-not-found ]] ; then
135 /usr/share/command-not-found/command-not-found $1
136 fi
137 return 1
138 }
139
140 # Gets battery percentage.
141 battery() {
142 PERCENT="${${"$(acpi 2>/dev/null)"}/(#b)[[:space:]]#Battery <->: [^0-9]##, (<->)%*/${match[1]}}"
143 }
144
145 # Sets color in BATTCOLOR, designed to be used with RPROMPT.
146 batcolor() {
147 battery
148 if [ ! -z $PERCENT ]; then
149 if (acpi | head -n 1 | grep "Charging") 2> /dev/null 1>&2; then
150 BATTCOLOR="%F{cyan}"
151 else
152 if [ $PERCENT -gt 65 ]; then
153 BATTCOLOR="%F{green}"
154 else
155 if [ $PERCENT -gt 32 ]; then
156 BATTCOLOR="%F{yellow}"
157 else
158 BATTCOLOR="%F{red}"
159 fi
160 fi
161 fi
162 BATTERY="${BATTCOLOR} ${PERCENT} %%"
163 else
164 BATTERY=""
165 fi
166 }
167