3 ### Common per-shell configuration.
5 ###--------------------------------------------------------------------------
8 __mdw_programp () { type >/dev/null 2>&1 "$1"; }
10 __mdw_source_if_exists () {
13 if [ -r "$i" ]; then . "$i"; fi
17 ###--------------------------------------------------------------------------
24 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
29 *) eval "$hk=\${$hk:+\$$hk }\$fn" ;;
34 local hk=$1 fn=$2 t l r
37 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
42 l=${t%% $fn*} r=${t##*$fn }
44 eval "$hk=\$l\${l:+ }\$r"
49 __mdw_setrc () { return $1; }
52 local hk=$1 saverc=$? t i; shift
55 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
58 for i in $t; do __mdw_setrc $saverc; "$i" "$@"; done
61 ###--------------------------------------------------------------------------
64 __mdw_host=$(hostname)
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=""; }
70 __mdw_system=$(uname -s)
71 : ${USER-${LOGNAME-$(id -un)}}
82 *" 544 "*) __mdw_rootp=t __mdw_user="$__mdw_user%admin" ;;
92 __mdw_set_prompt_pieces () {
94 ## Fancy highlighting in some terminals.
95 local bold unbold nl more host dir
96 local gitcolour extracolour rccolour uncolour
97 bold="" unbold="" nl="" more=""
98 gitcolour="" extracolour="" rccolour="" uncolour=""
99 __mdw_set_prompt_hacks
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
107 left=$(echo « | iconv -f UTF-8 -t //translit)
108 right=$(echo » | iconv -f UTF-8 -t //translit)
112 mdw | mwooding | nemo) u="" left="[" right="]" ;;
113 *) u="$__mdw_user@" left="{" right="}" ;;
117 "$tty") left="<" right=">" ;;
118 *) __mdw_tty=$tty; export __mdw_tty ;;
123 ## If this session is insecure then highlight that.
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=")" ;;
132 ## If this is an schroot environment or some other interesting augmented
133 ## environment then point this out.
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_extra_left="$unbold$extracolour"
142 __mdw_prompt_extra_right="$uncolour$bold"
143 __mdw_prompt_right="$right$unbold"
144 __mdw_prompt_more=" $more$bold>$unbold "
147 __mdw_set_prompt () {
148 case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
150 case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac
156 if type __git_ps1 >/dev/null 2>&1; then
157 git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
161 case $__mdw_last_rc in
163 *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
165 case $__mdw_prompt_extra in
166 ?*) extra="$__mdw_prompt_extra_left [$__mdw_prompt_extra]$__mdw_prompt_extra_right" ;;
169 PS1="$__mdw_prompt_left$git$extra$rc$__mdw_prompt_right"
170 PS2="$PS1$__mdw_prompt_more"
176 __mdw_xterm_settitle () {
178 "
\e]0;%s@%s:%s – %s
\e\\" \
179 "$__mdw_user" "$__mdw_host$__mdw_hqual" "$PWD" \
182 __mdw_xterm_precmd () { __mdw_xterm_settitle "$__mdw_shell"; }
183 __mdw_xterm_preexec () { __mdw_xterm_settitle "$1"; }
185 __mdw_screen_settitle () {
190 __mdw_screen_precmd () { __mdw_screen_settitle "$__mdw_shell"; }
191 __mdw_screen_preexec () { __mdw_screen_settitle "$1"; }
194 case ${STY+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t},${TERM} in
196 __mdw_addhook __mdw_precmd_hook __mdw_xterm_precmd
197 __mdw_addhook __mdw_preexec_hook __mdw_xterm_preexec
200 __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd
201 __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec
204 case ${__mdw_precmd_hook+t} in
205 t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;;
209 ###--------------------------------------------------------------------------
210 ### Some handy aliases.
213 alias which="command -v"
216 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
220 alias re="rootly $EDITOR"
221 alias pstree="pstree -hl"
222 alias cdtmp='cd ${TMPDIR-/tmp}'
223 alias pushtmp='pushd ${TMPDIR-/tmp}'
225 alias svn="svnwrap svn"
228 if command -v ionice >/dev/null 2>&1; then
229 __mdw_nice="$__mdw_nice ionice -c3"
231 alias mdw-nice="$__mdw_nice --"
232 alias make="mdw-nice make"
233 alias cross-run="mdw-nice cross-run"
236 ## Shut up Lisp interpreters.
237 alias clisp="clisp -q -q"
238 if command -v ionice >/dev/null 2>&1; then
239 alias maybe-rlwrap="rlwrap"
241 alias maybe-rlwrap=""
243 alias cmucl="maybe-rlwrap cmucl -quiet"
244 alias ecl="maybe-rlwrap ecl"
245 alias sbcl="maybe-rlwrap sbcl --noinform"
246 alias ccl="maybe-rlwrap ccl"
247 alias ccl32="maybe-rlwrap ccl32"
248 alias ccl64="maybe-rlwrap ccl64"
249 alias abcl="maybe-rlwrap abcl --noinform"
251 ###--------------------------------------------------------------------------
255 if ! [ -t 1 ]; then return 1; fi
256 case $TERM in dumb) return 1 ;; esac
260 ## Arrange for `ls' output to be in colour.
261 unalias ls 2>/dev/null || :
263 if mdw_colour_p; then command ls $LS_OPTIONS ${LS_COLORS+--color=always} "$@"
264 else command ls "$@"; fi
267 ## Arrange for `grep' output to be in colour.
270 if mdw_colour_p; then
271 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
276 alias grep="greplike grep"
277 alias egrep="greplike egrep"
278 alias fgrep="greplike fgrep"
279 alias zgrep="greplike zgrep"
281 ## Arrange for `diff' output to be in colour.
284 if mdw_colour_p; then
286 ${DIFF_COLORS+--color=always} \
287 ${DIFF_COLORS+--palette="$DIFF_COLORS"} \
290 command $diff "$@" | cat
293 alias diff="difflike diff"
295 ###--------------------------------------------------------------------------
298 ## Turn off pagers inside Emacs shell buffers.
299 case "$INSIDE_EMACS" in
300 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
303 ###--------------------------------------------------------------------------
304 ### More complicated shell functions.
306 ## xt [@HOST] XTERM-ARGS
308 ## Open a terminal, maybe on a remote host.
312 local remote=${1#@} title
314 if [ $# -gt 0 ]; then
315 title="xterm [$remote] $1"
317 title="xterm [$remote]"
319 (xterm -title "$title" -e ssh $remote "$@" &)
329 ## Tweak core dumps on and off, or show the current status.
332 xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;;
333 xoff|xn|xno) ulimit -Sc 0 ;;
335 local l=$(ulimit -Sc)
337 0) echo "Core dumps disabled" ;;
338 unlimited) echo "Core dumps enabled" ;;
339 *) echo "Core dump limit is $l blocks" ;;
343 echo >&2 "usage: core [y|n]"
351 ## Set current security world to NAME. With no NAME, print the currently
354 local nfast=${NFAST_HOME-/opt/nfast}
358 echo "${NFAST_KMDATA#$nfast/kmdata-}"
363 elif [ -d "$nfast/kmdata-$1" ]; then
364 kmdata=$nfast/kmdata-$1
366 echo >&2 "world: can't find world $1"
371 0) export NFAST_KMDATA=$kmdata ;;
378 ## path-add [VAR] DIR
380 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
381 ## it's not there already.
383 local pathvar export dir val
385 1) pathvar=PATH dir=$1 export="export PATH" ;;
386 2) pathvar=$1 dir=$2 export=: ;;
387 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
398 ## path-remove [VAR] DIR
400 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
401 ## error if DIR isn't in VAR.
403 local pathvar export dir val
405 1) pathvar=PATH dir=$1 export="export PATH" ;;
406 2) pathvar=$1 dir=$2 export=: ;;
407 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
412 :"$dir":*) val=${val#$dir:} ;;
413 *:"$dir":) val=${val%:$dir} ;;
414 *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;;
420 ## pathhack [-f] +HACK|-HACK...
422 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
423 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
424 ## that's already on the PATH doesn't do anything unless `-f' is set, in
425 ## which case it gets moved to the beginning. With no arguments, print the
426 ## currently installed hacks.
428 local p e force arg hack dir
430 if [ $# -eq 0 ]; then
433 case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac
434 case "$p" in *:*) p=${p#*:} ;; *) break ;; esac
439 while [ $# -gt 0 ]; do
442 -f | --force) force=t; shift; continue ;;
448 dir=$HOME/bin/hacks/$hack
449 if ! [ -d "$dir" ]; then
450 echo "$0: path hack $hack not found"
453 case "$arg,$force,:$PATH:" in
454 -*,*,*:"$dir":*) path_remove p "$dir" ;;
455 +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;;
456 +*,nil,*:"$dir":*) ;;
457 +*,*) path_add p "$dir" ;;
461 if [ $# -eq 0 ]; then PATH=$p; export PATH
462 else PATH=$p "$@"; fi
465 ###--------------------------------------------------------------------------
466 ### Finishing touches.
468 ## Make sure `$HOME/bin' is on the path.
471 ## Set the temporary directory again. (A setuid or setgid program may have
472 ## unhelpfully forgotten this for us.)
475 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
478 ## For `root' use -- some simple molly-guards.
481 alias rm="rm -i" cp="cp -i" mv="mv -i"
486 ## Install wakeup machinery for long-running programs.
487 case ${DISPLAY+t},${WINDOWID+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t} in
489 if [ -t 0 ] && __mdw_programp notify-send; then . $HOME/bin/wakey.sh; fi
493 ## Run any local hooks.
494 __mdw_source_if_exists "$HOME/.shell-local"
496 ###----- That's all, folks --------------------------------------------------