f91375b6fedbc71e00770e67efa4abc31e1299eb
[zsh.git] / .zsh / 05_Functions.zsh
1 # -*- mode: sh;-*-
2
3 ## User-defined functions
4 #
5 preprint()
6 {
7 local my_color start stop pipe1 pipe2 hbar out
8
9 if [[ "$COLORS" == "true" ]]; then
10 my_color=${2:-${BOLD_RED}}
11 else
12 my_color=${2:-""}
13 fi
14 if [[ $LINEDRAW == "true" ]]; then
15 # Some stuff to help us draw nice lines
16 if isutf8 || isconsole; then
17 start=""
18 stop=""
19 PR_ULCORNER="┌"
20 PR_LLCORNER="└"
21 PR_LRCORNER="┘"
22 PR_URCORNER="┐"
23 draw="─"
24 else
25 start="$terminfo[smacs]"
26 stop="$terminfo[rmacs]"
27 pipe1="u"
28 pipe2="t"
29 draw="q"
30 fi
31 else
32 start=""
33 stop=""
34 pipe1="|"
35 pipe2="|"
36 draw="-"
37 fi
38 hbar="${start}${${(l:$(( 74 - ${#1} - 5 ))::X:)}//X/$draw}${stop}"
39 out="${my_color}${hbar}${start}"
40
41 if [[ "${1}" != "" ]]; then
42 out+="${pipe1}${stop}${my_color} $1 ${my_color}${start}${pipe2}"
43 else
44 out+="${draw}${draw}"
45 fi
46 out+="${draw}${stop}${NO_COLOR}\r"
47
48 print -Pn -- $out
49 }
50
51 _jj_chpwd()
52 {
53 if ( is-callable git && test -d .git ); then
54 # Shows tracked branches and modified files
55 git checkout HEAD 2>&1 | sed 's/^/ /'
56 fi
57 }
58
59 if is434 ; then
60 add-zsh-hook chpwd _jj_chpwd
61 else
62 function chpwd() {
63 _jj_chpwd
64 }
65 fi
66
67 # Idea taken from oh-my-zsh, but code is different
68 function dirpersistrestore () {
69 if [ -f ${DIRSTACKFILE} ]; then
70 dirstack=( ${(f)"$(< ${DIRSTACKFILE} )"} )
71 if zstyle -t ':ganneff:config' dirstackhandling dirpersist; then
72 cd -q ${dirstack[-1]}
73 fi
74 fi
75 }
76
77 function dirpersiststore () {
78 print -l ${(Oau)dirstack} ${PWD} >| ${DIRSTACKFILE}
79 }
80
81 if is434; then
82 add-zsh-hook zshexit dirpersiststore
83 else
84 echo "Sorry, zsh version too old"
85 fi
86
87 function agent() {
88 local agent agentfiles af
89 local _agent_ssh_env=${HOME}/.ssh/agentenv.${HOST}
90 typeset -a agentfiles
91
92 touch ${_agent_ssh_env}
93 chmod 0600 ${_agent_ssh_env}
94
95 zstyle -s ':ganneff:config' agent agent || agent=gpg-agent
96 case ${agent} in
97 gpg-agent)
98 if is-callable gpg-agent; then
99 eval $(gpg-agent --enable-ssh-support \
100 --daemon \
101 --write-env-file ${_agent_ssh_env})
102 else
103 print "gpg-agent binary not found"
104 return 1
105 fi
106 ;;
107 ssh-agent)
108 if is-callable ssh-agent; then
109 ssh-agent | sed s'/^echo/#echo/g' >| ${_agent_ssh_env}
110 source ${_agent_ssh_env}
111 else
112 print "ssh-agent binary not found"
113 return 1
114 fi
115 ;;
116 *)
117 print "Unknown agent ${agent}, not doing anything!"
118 ;;
119 esac
120
121 zstyle -a ':ganneff:config' agentfiles agentfiles || agentfiles = ()
122 for af in ${agentfiles}; do
123 ssh-add ${af}
124 done
125
126 if is434 && zstyle -T ':ganneff:config' killagent true; then
127 add-zsh-hook zshexit kill_agent
128 fi
129 }
130
131 function kill_agent() {
132 local _agent_ssh_env=${HOME}/.ssh/agentenv.${HOST}
133 kill -TERM ${SSH_AGENT_PID}
134 rm -f ${_agent_ssh_env}
135 }
136
137 function Status Start Stop Restart Reload {
138 typeset script
139 for script in ${*}; {
140 sudo /etc/init.d/${script} ${0:l}
141 }
142 }
143 # move cursor between chars when typing '', "", (), [], and {}
144 magic-single-quotes() { if [[ $LBUFFER[-1] == \' ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
145 magic-double-quotes() { if [[ $LBUFFER[-1] == \" ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
146 magic-parentheses() { if [[ $LBUFFER[-1] == \( ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
147 magic-square-brackets() { if [[ $LBUFFER[-1] == \[ ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
148 magic-curly-brackets() { if [[ $LBUFFER[-1] == \{ ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
149 magic-angle-brackets() { if [[ $LBUFFER[-1] == \< ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };