dot/shell-rc: Enable the `wakey' machinery by default, when it will work.
[profile] / dot / shell-rc
index 5c3c336..b8bfb3d 100644 (file)
@@ -15,34 +15,102 @@ __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_hqual=
+__mdw_hqual=$__mdw_hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}
+__mdw_hqual=$__mdw_hqual${CROSS_BUILDENV+/$CROSS_BUILDENV}
+__mdw_set_prompt_hacks () { host=$__mdw_host; dir=""; }
+
+__mdw_system=$(uname -s)
+: ${USER-${LOGNAME-$(id -un)}}
+__mdw_user=$USER
+
+case $(id -u) in
+  0)
+    __mdw_rootp=t
+    ;;
+  *)
+    case $__mdw_system in
+      CYGWIN_*)
+       case " $(id -G) " in
+         *" 544 "*) __mdw_rootp=t __mdw_user="$__mdw_user%admin" ;;
+         *) __mdw_rootp=nil ;;
+       esac
+       ;;
+      *)
+       __mdw_rootp=nil
+       ;;
+    esac
+esac
 
 __mdw_set_prompt_pieces () {
-  local hqual
-  hqual=""
 
   ## Fancy highlighting in some terminals.
-  local bold unbold nl gitcolour rccolour uncolour
-  local host dir
-  bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour=""
+  local bold unbold nl more host dir
+  local gitcolour extracolour rccolour uncolour
+  bold="" unbold="" nl="" more=""
+  gitcolour="" extracolour="" rccolour="" uncolour=""
   __mdw_set_prompt_hacks
 
   ## Choose the right delimiters.  Highlight root prompts specially;
   ## highlight when I'm running as some other user.  Highlight when this
   ## isn't the outermost shell on the terminal.
-  local left right user u tty
-  user=${USER-${LOGNAME-$(id -un)}}
-  case $(id -u) in
-    0)
+  local left right u tty
+  case $__mdw_rootp in
+    t)
       left=$(echo « | iconv -f UTF-8 -t //translit)
       right=$(echo » | iconv -f UTF-8 -t //translit)
       ;;
-    *)
-      case $user in
+    nil)
+      case $USER in
        mdw | mwooding | nemo) u="" left="[" right="]" ;;
-       *) u="$user@" left="{" right="}" ;;
+       *) u="$__mdw_user@" left="{" right="}" ;;
       esac
       tty=$(tty)
       case "$__mdw_tty" in
@@ -61,47 +129,82 @@ __mdw_set_prompt_pieces () {
     *) sec_l="" sec_r=""
   esac
 
-  ## If this is an schroot environment then point this out.
-  hqual="$hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}"
+  ## If this is an schroot environment or some other interesting augmented
+  ## environment then point this out.
 
   ## Put together the main pieces.
-  __mdw_prompt_left="$nl$bold$left$sec_l$u$host$hqual$sec_r$dir"
+  __mdw_prompt_left="$nl$bold$left$sec_l$u$host$__mdw_hqual$sec_r$dir"
   __mdw_prompt_git_left="$unbold$gitcolour"
   __mdw_prompt_git_right="$uncolour$bold"
   __mdw_prompt_rc_left="$unbold$rccolour"
   __mdw_prompt_rc_right="$uncolour$bold"
+  __mdw_prompt_extra_left="$unbold$extracolour"
+  __mdw_prompt_extra_right="$uncolour$bold"
   __mdw_prompt_right="$right$unbold"
+  __mdw_prompt_more=" $more$bold>$unbold "
 }
 
 __mdw_set_prompt () {
-  __mdw_last_rc=$?
-  local git rc
-  if type __git_ps1 >/dev/null 2>&1; then
-    git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
-  else
-    git=""
-  fi
-  case $__mdw_last_rc in
-    0) rc="" ;;
-    *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
+  case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
+    dumb:)
+      case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac
+      PS2='> '
+      ;;
+    *)
+      __mdw_last_rc=$?
+      local git extra rc
+      if type __git_ps1 >/dev/null 2>&1; then
+       git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
+      else
+       git=""
+      fi
+      case $__mdw_last_rc in
+       0) rc="" ;;
+       *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
+      esac
+      case $__mdw_prompt_extra in
+       ?*) extra="$__mdw_prompt_extra_left [$__mdw_prompt_extra]$__mdw_prompt_extra_right" ;;
+       *) extra="" ;;
+      esac
+      PS1="$__mdw_prompt_left$git$extra$rc$__mdw_prompt_right"
+      PS2="$PS1$__mdw_prompt_more"
+      unset __mdw_last_rc
+      ;;
   esac
-  PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right"
-  PS2="$PS1 $bold>$unbold "
-  unset __mdw_last_rc
 }
 
-__mdw_precmd () {
-  __mdw_set_prompt
-  case ${STY+t} in
-    t) printf "\ek%s\e\\" "$__mdw_shell" ;;
-  esac
+__mdw_xterm_settitle () {
+  printf >/dev/tty \
+    "\e]2;%s@%s:%s – %s\e\\" \
+    "$__mdw_user" "$__mdw_host$__mdw_hqual" "$PWD" \
+    "$1"
 }
+__mdw_xterm_precmd () { __mdw_xterm_settitle "$__mdw_shell"; }
+__mdw_xterm_preexec () { __mdw_xterm_settitle "$1"; }
 
-__mdw_preexec () {
-  case ${STY+t} in
-    t) printf "\ek%s\e\\" "$1" ;;
-  esac
+__mdw_screen_settitle () {
+  printf >/dev/tty \
+    "\ek%s\e\\" \
+    "$1"
 }
+__mdw_screen_precmd () { __mdw_screen_settitle "$__mdw_shell"; }
+__mdw_screen_preexec () { __mdw_screen_settitle "$1"; }
+
+if [ -t 0 ]; then
+  case ${STY+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t},${TERM} in
+    ,t,t,xterm*)
+      __mdw_addhook __mdw_precmd_hook __mdw_xterm_precmd
+      __mdw_addhook __mdw_preexec_hook __mdw_xterm_preexec
+      ;;
+    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.
@@ -121,6 +224,19 @@ alias pushtmp='pushd ${TMPDIR-/tmp}'
 alias e="$EDITOR"
 alias svn="svnwrap svn"
 alias @="ssh"
+alias make="nice make"
+alias cross-run="nice cross-run"
+alias gdb="gdb -q"
+
+## Shut up Lisp interpreters.
+alias clisp="clisp -q -q"
+alias cmucl="rlwrap cmucl -quiet"
+alias ecl="rlwrap ecl"
+alias sbcl="rlwrap sbcl --noinform"
+alias ccl="rlwrap ccl"
+alias ccl32="rlwrap ccl32"
+alias ccl64="rlwrap ccl64"
+alias abcl="rlwrap abcl --noinform"
 
 ###--------------------------------------------------------------------------
 ### Colour output.
@@ -151,6 +267,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.
 
@@ -342,6 +473,13 @@ case $(id -u) in
     ;;
 esac
 
+## Install wakeup machinery for long-running programs.
+case ${DISPLAY+t},${WINDOWID+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t} in
+  t,t,t,t)
+    if [ -t 0 ] && __mdw_programp notify-send; then . $HOME/bin/wakey.sh; fi
+    ;;
+esac
+
 ## Run any local hooks.
 __mdw_source_if_exists "$HOME/.shell-local"