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
12 local -i comonly gatoo
14 if [[ $1 == '-c' ]] ; then
17 elif [[ $1 == '-g' ]] ; then
24 if (( ${#argv} != 1 )) ; then
25 printf 'usage: check_com [-cg] <command>\n' >&2
29 if (( comonly
> 0 )) ; then
30 [[ -n ${commands[$1]} ]] && return 0
34 if [[ -n ${commands[$1]} ]] \
35 ||
[[ -n ${functions[$1]} ]] \
36 ||
[[ -n ${aliases[$1]} ]] \
37 ||
[[ -n ${reswords[(r)$1]} ]] ; then
42 if (( gatoo
> 0 )) && [[ -n ${galiases[$1]} ]] ; then
49 # creates an alias and precedes the command with
50 # sudo if $EUID is not zero.
53 local only
=0 ; local multi
=0
54 while [[ $1 == -* ]] ; do
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'
67 (*) printf "unkown option: '%s'\n" "$1" ; return 1 ;;
72 if (( ${#argv} > 1 )) ; then
73 printf 'Too many arguments %s\n' "${#argv}"
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}"
88 # Check if we can read given files and source those we can.
91 if (( ${#argv} < 1 )) ; then
92 printf 'usage: xsource FILE(s)...\n' >&2
96 while (( ${#argv} > 0 )) ; do
97 [[ -r $1 ]] && source $1
103 # Check if we can read a given file and 'cat(1)' it.
106 if (( ${#argv} != 1 )) ; then
107 printf 'usage: xcat FILE\n' >&2
111 [[ -r $1 ]] && cat $1
115 # Remove these functions again, they are of use only in these
116 # setup files. This should be called at the end of .zshrc.
120 funcs
=(salias xcat xsource xunfunction zrcautoload
)
122 for func
in $funcs ; do
123 [[ -n ${functions[$func]} ]] \
131 # Handles command not fount error
132 function command_not_found_handler
() {
134 if [[ -x /usr
/share
/command-not-found
/command-not-found
]] ; then
135 /usr
/share
/command-not-found
/command-not-found
$1
140 # Gets battery percentage.
142 PERCENT
="${${"$(acpi 2>/dev/null)"}/(#b)[[:space:]]#Battery <->: [^0-9]##, (<->)%*/${match[1]}}"
145 # Sets color in BATTCOLOR, designed to be used with RPROMPT.
148 if [ ! -z $PERCENT ]; then
149 if (acpi |
head -n 1 |
grep "Charging") 2> /dev
/null
1>&2; then
152 if [ $PERCENT -gt 65 ]; then
153 BATTCOLOR
="%F{green}"
155 if [ $PERCENT -gt 32 ]; then
156 BATTCOLOR
="%F{yellow}"
162 BATTERY
="${BATTCOLOR} ${PERCENT} %%"