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