a5c5f17d3c8548055c7f19bfc22549486f0c5916
[zsh.git] / .zsh / 05_Functions.zsh
1 # -*- mode: sh;-*-
2
3 ## User-defined functions
4 #
5 cmd_exists ()
6 {
7 \which -p $1 >/dev/null 2>&1
8 }
9
10 preprint()
11 {
12 local my_color start stop pipe1 pipe2 hbar out
13
14 if [[ "$COLORS" == "true" ]]; then
15 my_color=${2:-${BOLD_RED}}
16 else
17 my_color=${2:-""}
18 fi
19 if [[ $LINEDRAW == "true" ]]; then
20 # Some stuff to help us draw nice lines
21 start="$terminfo[smacs]"
22 stop="$terminfo[rmacs]"
23 hbar="${start}${(l:$(( 74 - ${#1} - 5 ))::q:)}${stop}"
24 pipe1="u"
25 pipe2="t"
26 draw="q"
27 else
28 start=""
29 stop=""
30 hbar="${start}${(l:$((74 - ${#1} - 5))::-:)}${stop}"
31 pipe1="|"
32 pipe2="|"
33 draw="-"
34 fi
35 out="${my_color}${hbar}${start}"
36
37 if [[ "${1}" != "" ]]; then
38 out+="${pipe1}${stop}${my_color} $1 ${my_color}${start}${pipe2}"
39 else
40 out+="${draw}${draw}${draw}${draw}"
41 fi
42 out+="${draw}${stop}${NO_COLOR}\r"
43
44 print -Pn -- $out
45 }
46
47 normal_user ()
48 {
49 if [ -e /etc/login.defs ]; then
50 eval `grep -v '^[$#]' /etc/login.defs | grep "^UID_" | tr -d '[:blank:]' | sed 's/^[A-Z_]\+/&=/'`
51 [ \( $UID -ge $UID_MIN \) ]
52 else
53 [ "`whoami`" != "root" ]
54 fi
55 }
56
57 privileged_user ()
58 {
59 ! normal_user
60 }
61
62 _jj_chpwd()
63 {
64 if ( cmd_exists git && test -d .git ); then
65 # Shows tracked branches and modified files
66 git checkout HEAD 2>&1 | sed 's/^/ /'
67 fi
68 }
69
70 if is434 ; then
71 add-zsh-hook chpwd _jj_chpwd
72 else
73 function chpwd() {
74 _jj_chpwd
75 }
76 fi
77
78 # Taken from oh-my-zsh
79 if is434; then
80 function dirpersiststore () {
81 dirs -p | perl -e 'foreach (reverse <STDIN>) {chomp;s/([& ])/\\$1/g ;print "if [ -d $_ ]; then pushd -q $_; fi\n"}' >| $zdirstore
82 }
83 add-zsh-hook zshexit dirpersiststore
84
85 function dirpersistrestore () {
86 if [ -f $zdirstore ]; then
87 source $zdirstore
88 fi
89 }
90 fi