dot/shell-rc, dot/bashrc, dot/zshrc: Introduce a formal notion of hooks.
[profile] / dot / shell-rc
1 ### -*-sh-*-
2 ###
3 ### Common per-shell configuration.
4
5 ###--------------------------------------------------------------------------
6 ### Utilities.
7
8 __mdw_programp () { type >/dev/null 2>&1 "$1"; }
9
10 __mdw_source_if_exists () {
11 local i
12 for i in "$@"; do
13 if [ -r "$i" ]; then . "$i"; fi
14 done
15 }
16
17 ###--------------------------------------------------------------------------
18 ### Hooks.
19
20 __mdw_addhook () {
21 local hk=$1 fn=$2 t
22
23 eval t=\${$hk+t}
24 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
25
26 eval t=\$$hk
27 case " $t " in
28 *" $fn "*) ;;
29 *) eval "$hk=\${$hk:+\$$hk }\$fn" ;;
30 esac
31 }
32
33 __mdw_delhook () {
34 local hk=$1 fn=$2 t l r
35
36 eval t=\${$hk+t}
37 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
38
39 eval t=\" \$$hk \"
40 case $t in
41 *" $fn "*)
42 l=${t%% $fn*} r=${t##*$fn }
43 l=${l# } r=${r% }
44 eval "$hk=\$l\${l:+ }\$r"
45 ;;
46 esac
47 }
48
49 __mdw_setrc () { return $1; }
50
51 __mdw_runhook () {
52 local hk=$1 saverc=$? t i; shift
53
54 eval t=\${$hk+t}
55 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
56
57 eval t=\$$hk
58 for i in $t; do __mdw_setrc $saverc; "$i" "$@"; done
59 }
60
61 ###--------------------------------------------------------------------------
62 ### Prompt machinery.
63
64 __mdw_set_prompt_hacks () { host=$(hostname); dir=""; }
65
66 __mdw_set_prompt_pieces () {
67 local hqual
68 hqual=""
69
70 ## Fancy highlighting in some terminals.
71 local bold unbold nl gitcolour rccolour uncolour
72 local host dir more
73 bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" more=""
74 __mdw_set_prompt_hacks
75
76 ## Choose the right delimiters. Highlight root prompts specially;
77 ## highlight when I'm running as some other user. Highlight when this
78 ## isn't the outermost shell on the terminal.
79 local left right user u tty
80 user=${USER-${LOGNAME-$(id -un)}}
81 case $(id -u) in
82 0)
83 left=$(echo « | iconv -f UTF-8 -t //translit)
84 right=$(echo » | iconv -f UTF-8 -t //translit)
85 ;;
86 *)
87 case $user in
88 mdw | mwooding | nemo) u="" left="[" right="]" ;;
89 *) u="$user@" left="{" right="}" ;;
90 esac
91 tty=$(tty)
92 case "$__mdw_tty" in
93 "$tty") left="<" right=">" ;;
94 *) __mdw_tty=$tty; export __mdw_tty ;;
95 esac
96 ;;
97 esac
98
99 ## If this session is insecure then highlight that.
100 local sec_l sec_r h
101 h=$(hostname)
102 case ${SSH_CLIENT-nil},${SCHROOT_CHROOT_NAME-nil},$__mdw_sechost in
103 nil,nil,"$h") sec_l="" sec_r="" ;;
104 nil,nil,*) sec_l="(" sec_r=")" ;;
105 *) sec_l="" sec_r=""
106 esac
107
108 ## If this is an schroot environment or some other interesting augmented
109 ## environment then point this out.
110 hqual="$hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}"
111 hqual="$hqual${MDW_BUILDENV+/$MDW_BUILDENV}"
112
113 ## Put together the main pieces.
114 __mdw_prompt_left="$nl$bold$left$sec_l$u$host$hqual$sec_r$dir"
115 __mdw_prompt_git_left="$unbold$gitcolour"
116 __mdw_prompt_git_right="$uncolour$bold"
117 __mdw_prompt_rc_left="$unbold$rccolour"
118 __mdw_prompt_rc_right="$uncolour$bold"
119 __mdw_prompt_right="$right$unbold"
120 __mdw_prompt_more=" $more$bold>$unbold "
121 }
122
123 __mdw_set_prompt () {
124 case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
125 dumb:)
126 case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac
127 PS2='> '
128 ;;
129 *)
130 __mdw_last_rc=$?
131 local git rc
132 if type __git_ps1 >/dev/null 2>&1; then
133 git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
134 else
135 git=""
136 fi
137 case $__mdw_last_rc in
138 0) rc="" ;;
139 *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
140 esac
141 PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right"
142 PS2="$PS1$__mdw_prompt_more"
143 unset __mdw_last_rc
144 ;;
145 esac
146 }
147
148 __mdw_screen_precmd () { printf "\ek%s\e\\" "$__mdw_shell"; }
149 __mdw_screen_preexec () { printf "\ek%s\e\\" "$1"; }
150
151 if [ -t 0 ]; then
152 case ${STY+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t} in
153 t,t,t)
154 __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd
155 __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec
156 ;;
157 esac
158 case ${__mdw_precmd_hook+t} in
159 t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;;
160 esac
161 fi
162
163 ###--------------------------------------------------------------------------
164 ### Some handy aliases.
165
166 alias cx='chmod +x'
167 alias which="command -v"
168 alias rc="rc -l"
169 rootly () {
170 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
171 $__MDW_ROOTLY "$@"
172 }
173 alias r=rootly
174 alias re="rootly $EDITOR"
175 alias pstree="pstree -hl"
176 alias cdtmp='cd ${TMPDIR-/tmp}'
177 alias pushtmp='pushd ${TMPDIR-/tmp}'
178 alias e="$EDITOR"
179 alias svn="svnwrap svn"
180 alias @="ssh"
181 alias make="nice make"
182 alias gdb="gdb -q"
183
184 ###--------------------------------------------------------------------------
185 ### Colour output.
186
187 ## Arrange for `ls' output to be in colour.
188 if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
189 else unset LS_COLORS; fi
190
191 unalias ls 2>/dev/null || :
192 ls () {
193 if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
194 else command ls "$@"; fi
195 }
196
197 ## Arrange for `grep' output to be in colour.
198 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
199
200 greplike () {
201 local grep=$1; shift
202 if [ -t 1 ]; then
203 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
204 else
205 command $grep "$@"
206 fi
207 }
208 alias grep="greplike grep"
209 alias egrep="greplike egrep"
210 alias fgrep="greplike fgrep"
211 alias zgrep="greplike zgrep"
212
213 ## Arrange for `diff' output to be in colour.
214 export DIFF_COLORS="hd=1:ln=36:ad=32:de=31"
215 difflike () {
216 local diff=$1; shift
217 if [ -t 1 ]; then
218 command $diff \
219 ${DIFF_COLORS+--color=always} \
220 ${DIFF_COLORS+--palette="$DIFF_COLORS"} \
221 "$@" | mdw-pager
222 else
223 command $diff "$@" | cat
224 fi
225 }
226 alias diff="difflike diff"
227
228 ###--------------------------------------------------------------------------
229 ### Other hacks.
230
231 ## Turn off pagers inside Emacs shell buffers.
232 case "$INSIDE_EMACS" in
233 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
234 esac
235
236 ###--------------------------------------------------------------------------
237 ### More complicated shell functions.
238
239 ## xt [@HOST] XTERM-ARGS
240 ##
241 ## Open a terminal, maybe on a remote host.
242 xt () {
243 case "$1" in
244 @*)
245 local remote=${1#@} title
246 shift
247 if [ $# -gt 0 ]; then
248 title="xterm [$remote] $1"
249 else
250 title="xterm [$remote]"
251 fi
252 (xterm -title "$title" -e ssh $remote "$@" &)
253 ;;
254 *)
255 (xterm "$@" &)
256 ;;
257 esac
258 }
259
260 ## core [y|n]
261 ##
262 ## Tweak core dumps on and off, or show the current status.
263 core () {
264 case "x$1" in
265 xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;;
266 xoff|xn|xno) ulimit -Sc 0 ;;
267 x)
268 local l=$(ulimit -Sc)
269 case $l in
270 0) echo "Core dumps disabled" ;;
271 unlimited) echo "Core dumps enabled" ;;
272 *) echo "Core dump limit is $l blocks" ;;
273 esac
274 ;;
275 *)
276 echo >&2 "usage: core [y|n]"
277 return 1
278 ;;
279 esac
280 }
281
282 ## world [NAME]
283 ##
284 ## Set current security world to NAME. With no NAME, print the currently
285 ## selected world.
286 world () {
287 local nfast=${NFAST_HOME-/opt/nfast}
288 local kmdata
289 case "$#" in
290 0)
291 echo "${NFAST_KMDATA#$nfast/kmdata-}"
292 ;;
293 *)
294 if [ -d "$1" ]; then
295 kmdata=$1
296 elif [ -d "$nfast/kmdata-$1" ]; then
297 kmdata=$nfast/kmdata-$1
298 else
299 echo >&2 "world: can't find world $1"
300 return 1
301 fi
302 shift
303 case "$#" in
304 0) export NFAST_KMDATA=$kmdata ;;
305 *) "$@" ;;
306 esac
307 ;;
308 esac
309 }
310
311 ## path-add [VAR] DIR
312 ##
313 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
314 ## it's not there already.
315 path_add () {
316 local pathvar export dir val
317 case $# in
318 1) pathvar=PATH dir=$1 export="export PATH" ;;
319 2) pathvar=$1 dir=$2 export=: ;;
320 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
321 esac
322 eval val=\$$pathvar
323 case ":$val:" in
324 *:"$dir":*) ;;
325 *) val=$dir:$val ;;
326 esac
327 eval $pathvar=\$val
328 eval $export
329 }
330
331 ## path-remove [VAR] DIR
332 ##
333 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
334 ## error if DIR isn't in VAR.
335 path_remove () {
336 local pathvar export dir val
337 case $# in
338 1) pathvar=PATH dir=$1 export="export PATH" ;;
339 2) pathvar=$1 dir=$2 export=: ;;
340 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
341 esac
342 eval val=\$$pathvar
343 case ":$val:" in
344 :"$dir":) val= ;;
345 :"$dir":*) val=${val#$dir:} ;;
346 *:"$dir":) val=${val%:$dir} ;;
347 *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;;
348 esac
349 eval $pathvar=\$val
350 eval $export
351 }
352
353 ## pathhack [-f] +HACK|-HACK...
354 ##
355 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
356 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
357 ## that's already on the PATH doesn't do anything unless `-f' is set, in
358 ## which case it gets moved to the beginning. With no arguments, print the
359 ## currently installed hacks.
360 pathhack () {
361 local p e force arg hack dir
362 p=$PATH
363 if [ $# -eq 0 ]; then
364 while :; do
365 e=${p%%:*}
366 case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac
367 case "$p" in *:*) p=${p#*:} ;; *) break ;; esac
368 done
369 return
370 fi
371 force=nil
372 while [ $# -gt 0 ]; do
373 arg=$1
374 case "$arg" in
375 -f | --force) force=t; shift; continue ;;
376 --) shift; break ;;
377 [-+]*) ;;
378 *) break; ;;
379 esac
380 hack=${arg#[+-]}
381 dir=$HOME/bin/hacks/$hack
382 if ! [ -d "$dir" ]; then
383 echo "$0: path hack $hack not found"
384 return 1
385 fi
386 case "$arg,$force,:$PATH:" in
387 -*,*,*:"$dir":*) path_remove p "$dir" ;;
388 +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;;
389 +*,nil,*:"$dir":*) ;;
390 +*,*) path_add p "$dir" ;;
391 esac
392 shift
393 done
394 if [ $# -eq 0 ]; then PATH=$p; export PATH
395 else PATH=$p "$@"; fi
396 }
397
398 ###--------------------------------------------------------------------------
399 ### Finishing touches.
400
401 ## Make sure `$HOME/bin' is on the path.
402 path_add "$HOME/bin"
403
404 ## Set the temporary directory again. (A setuid or setgid program may have
405 ## unhelpfully forgotten this for us.)
406 case ${TMPDIR+t} in
407 t) ;;
408 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
409 esac
410
411 ## For `root' use -- some simple molly-guards.
412 case $(id -u) in
413 0)
414 alias rm="rm -i" cp="cp -i" mv="mv -i"
415 set -o noclobber
416 ;;
417 esac
418
419 ## Run any local hooks.
420 __mdw_source_if_exists "$HOME/.shell-local"
421
422 ###----- That's all, folks --------------------------------------------------