dot/shell-rc: Capture the hostname once and for all.
[profile] / dot / shell-rc
index 4ab0355..67ad457 100644 (file)
@@ -15,9 +15,54 @@ __mdw_source_if_exists () {
 }
 
 ###--------------------------------------------------------------------------
+### Hooks.
+
+__mdw_addhook () {
+  local hk=$1 fn=$2 t
+
+  eval t=\${$hk+t}
+  case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
+
+  eval t=\$$hk
+  case " $t " in
+    *" $fn "*) ;;
+    *) eval "$hk=\${$hk:+\$$hk }\$fn" ;;
+  esac
+}
+
+__mdw_delhook () {
+  local hk=$1 fn=$2 t l r
+
+  eval t=\${$hk+t}
+  case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
+
+  eval t=\" \$$hk \"
+  case $t in
+    *" $fn "*)
+      l=${t%% $fn*} r=${t##*$fn }
+      l=${l# } r=${r% }
+      eval "$hk=\$l\${l:+ }\$r"
+      ;;
+  esac
+}
+
+__mdw_setrc () { return $1; }
+
+__mdw_runhook () {
+  local hk=$1 saverc=$? t i; shift
+
+  eval t=\${$hk+t}
+  case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
+
+  eval t=\$$hk
+  for i in $t; do __mdw_setrc $saverc; "$i" "$@"; done
+}
+
+###--------------------------------------------------------------------------
 ### Prompt machinery.
 
-__mdw_set_prompt_hacks () { host=$(hostname); dir=""; }
+__mdw_host=$(hostname)
+__mdw_set_prompt_hacks () { host=$__mdw_host; dir=""; }
 
 __mdw_set_prompt_pieces () {
   local hqual
@@ -25,8 +70,8 @@ __mdw_set_prompt_pieces () {
 
   ## Fancy highlighting in some terminals.
   local bold unbold nl gitcolour rccolour uncolour
-  local host dir
-  bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour=""
+  local host dir more
+  bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" more=""
   __mdw_set_prompt_hacks
 
   ## Choose the right delimiters.  Highlight root prompts specially;
@@ -61,8 +106,10 @@ __mdw_set_prompt_pieces () {
     *) sec_l="" sec_r=""
   esac
 
-  ## If this is an schroot environment then point this out.
+  ## If this is an schroot environment or some other interesting augmented
+  ## environment then point this out.
   hqual="$hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}"
+  hqual="$hqual${CROSS_BUILDENV+/$CROSS_BUILDENV}"
 
   ## Put together the main pieces.
   __mdw_prompt_left="$nl$bold$left$sec_l$u$host$hqual$sec_r$dir"
@@ -71,10 +118,11 @@ __mdw_set_prompt_pieces () {
   __mdw_prompt_rc_left="$unbold$rccolour"
   __mdw_prompt_rc_right="$uncolour$bold"
   __mdw_prompt_right="$right$unbold"
+  __mdw_prompt_more=" $more$bold>$unbold "
 }
 
 __mdw_set_prompt () {
-  case "$TERM:$INSIDE_EMACS" in
+  case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
     dumb:)
       case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac
       PS2='> '
@@ -92,24 +140,26 @@ __mdw_set_prompt () {
        *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
       esac
       PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right"
-      PS2="$PS1 $bold>$unbold "
+      PS2="$PS1$__mdw_prompt_more"
       unset __mdw_last_rc
       ;;
   esac
 }
 
-__mdw_precmd () {
-  __mdw_set_prompt
-  case ${STY+t} in
-    t) printf "\ek%s\e\\" "$__mdw_shell" ;;
-  esac
-}
+__mdw_screen_precmd () { printf "\ek%s\e\\" "$__mdw_shell"; }
+__mdw_screen_preexec () { printf "\ek%s\e\\" "$1"; }
 
-__mdw_preexec () {
-  case ${STY+t} in
-    t) printf "\ek%s\e\\" "$1" ;;
+if [ -t 0 ]; then
+  case ${STY+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t} in
+    t,t,t)
+      __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd
+      __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec
+      ;;
   esac
-}
+  case ${__mdw_precmd_hook+t} in
+    t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;;
+  esac
+fi
 
 ###--------------------------------------------------------------------------
 ### Some handy aliases.
@@ -129,6 +179,8 @@ alias pushtmp='pushd ${TMPDIR-/tmp}'
 alias e="$EDITOR"
 alias svn="svnwrap svn"
 alias @="ssh"
+alias make="nice make"
+alias gdb="gdb -q"
 
 ###--------------------------------------------------------------------------
 ### Colour output.
@@ -159,6 +211,21 @@ alias egrep="greplike egrep"
 alias fgrep="greplike fgrep"
 alias zgrep="greplike zgrep"
 
+## Arrange for `diff' output to be in colour.
+export DIFF_COLORS="hd=1:ln=36:ad=32:de=31"
+difflike () {
+  local diff=$1; shift
+  if [ -t 1 ]; then
+    command $diff \
+           ${DIFF_COLORS+--color=always} \
+           ${DIFF_COLORS+--palette="$DIFF_COLORS"} \
+           "$@" | mdw-pager
+  else
+    command $diff "$@" | cat
+  fi
+}
+alias diff="difflike diff"
+
 ###--------------------------------------------------------------------------
 ### Other hacks.