add the functions needed for some keybindings
[zsh.git] / .zsh / functions / salias
1 # -*- mode: sh;-*-
2 # creates an alias and precedes the command with
3 # sudo if $EUID is not zero.
4 # Taken from grml zshrc
5 # Authors: grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
6 # Bug-Reports: see http://grml.org/bugs/
7 # License: This file is licensed under the GPL v2.
8
9 emulate -L zsh
10 local only=0 ; local multi=0
11 while [[ $1 == -* ]] ; do
12 case $1 in
13 (-o) only=1 ;;
14 (-a) multi=1 ;;
15 (--) shift ; break ;;
16 (-h)
17 printf 'usage: salias [-h|-o|-a] <alias-expression>\n'
18 printf ' -h shows this help text.\n'
19 printf ' -a replace '\'' ; '\'' sequences with '\'' ; sudo '\''.\n'
20 printf ' be careful using this option.\n'
21 printf ' -o only sets an alias if a preceding sudo would be needed.\n'
22 return 0
23 ;;
24 (*) printf "unkown option: '%s'\n" "$1" ; return 1 ;;
25 esac
26 shift
27 done
28
29 if (( ${#argv} > 1 )) ; then
30 printf 'Too many arguments %s\n' "${#argv}"
31 return 1
32 fi
33
34 key="${1%%\=*}" ; val="${1#*\=}"
35 if (( EUID == 0 )) && (( only == 0 )); then
36 alias -- "${key}=${val}"
37 elif (( EUID > 0 )) ; then
38 (( multi > 0 )) && val="${val// ; / ; sudo }"
39 alias -- "${key}=sudo ${val}"
40 fi
41
42 return 0
43