Move into an external dir
[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 # Idea taken from oh-my-zsh, but code is different
60 function dirpersistrestore () {
61 if [ -f ${DIRSTACKFILE} ]; then
62 dirstack=( ${(f)"$(< ${DIRSTACKFILE} )"} )
63 if zstyle -t ':ganneff:config' dirstackhandling dirpersist; then
64 cd -q ${dirstack[-1]}
65 fi
66 fi
67 }
68
69 function dirpersiststore () {
70 print -l ${(Oau)dirstack} ${PWD} >| ${DIRSTACKFILE}
71 }
72
73 function agent() {
74 local agent agentfiles af
75 local _agent_ssh_env=${HOME}/.ssh/agentenv.${HOST}
76 typeset -a agentfiles
77
78 touch ${_agent_ssh_env}
79 chmod 0600 ${_agent_ssh_env}
80
81 zstyle -s ':ganneff:config' agent agent || agent=gpg-agent
82 case ${agent} in
83 gpg-agent)
84 if is-callable gpg-agent; then
85 eval $(gpg-agent --enable-ssh-support \
86 --daemon \
87 --write-env-file ${_agent_ssh_env})
88 else
89 print "gpg-agent binary not found"
90 return 1
91 fi
92 ;;
93 ssh-agent)
94 if is-callable ssh-agent; then
95 ssh-agent | sed s'/^echo/#echo/g' >| ${_agent_ssh_env}
96 source ${_agent_ssh_env}
97 else
98 print "ssh-agent binary not found"
99 return 1
100 fi
101 ;;
102 *)
103 print "Unknown agent ${agent}, not doing anything!"
104 ;;
105 esac
106
107 zstyle -a ':ganneff:config' agentfiles agentfiles || agentfiles = ()
108 for af in ${agentfiles}; do
109 ssh-add ${af}
110 done
111
112 if is435 && zstyle -T ':ganneff:config' killagent true; then
113 add-zsh-hook zshexit kill_agent
114 fi
115 }
116
117 function kill_agent() {
118 local _agent_ssh_env=${HOME}/.ssh/agentenv.${HOST}
119 kill -TERM ${SSH_AGENT_PID}
120 rm -f ${_agent_ssh_env}
121 }
122
123 function Status Start Stop Restart Reload {
124 typeset script
125 for script in ${*}; {
126 sudo /etc/init.d/${script} ${0:l}
127 }
128 }
129
130 # copied from grml
131 function zrcgotwidget() {
132 (( ${+widgets[$1]} ))
133 }
134
135 # copied from grml
136 function zrcgotkeymap() {
137 [[ -n ${(M)keymaps:#$1} ]]
138 }
139
140 # copied from grml
141 function zrcbindkey() {
142 if (( ARGC )) && zrcgotwidget ${argv[-1]}; then
143 bindkey "$@"
144 fi
145 }
146
147 # copied from grml
148 function bind2maps () {
149 local i sequence widget
150 local -a maps
151
152 while [[ "$1" != "--" ]]; do
153 maps+=( "$1" )
154 shift
155 done
156 shift
157
158 if [[ "$1" == "-s" ]]; then
159 shift
160 sequence="$1"
161 else
162 sequence="${key_info[$1]}"
163 fi
164 widget="$2"
165
166 [[ -z "$sequence" ]] && return 1
167
168 for i in "${maps[@]}"; do
169 zrcbindkey -M "$i" "$sequence" "$widget"
170 done
171 }
172
173 # move cursor between chars when typing '', "", (), [], and {}
174 magic-single-quotes() { if [[ $LBUFFER[-1] == \' ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
175 magic-double-quotes() { if [[ $LBUFFER[-1] == \" ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
176 magic-parentheses() { if [[ $LBUFFER[-1] == \( ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
177 magic-square-brackets() { if [[ $LBUFFER[-1] == \[ ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
178 magic-curly-brackets() { if [[ $LBUFFER[-1] == \{ ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
179 magic-angle-brackets() { if [[ $LBUFFER[-1] == \< ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };