X-Git-Url: https://git.distorted.org.uk/~mdw/profile/blobdiff_plain/59c3438f0265c4a38135244ab8447b45d537fff3..e623f04118295c92c75eacaed5c50ce72fd108b6:/dot/bashrc diff --git a/dot/bashrc b/dot/bashrc index f6b7a56..f655756 100644 --- a/dot/bashrc +++ b/dot/bashrc @@ -1,70 +1,80 @@ -### -*-sh-*- +### -*-bash-*- ### -### Bash session things +### Bash session things. + +__mdw_shell=bash ## Only do this if we haven't done it before. (Note that this guard isn't ## exported, so subshells will need to make their own arrangements.) -if [ -z "$__mdw_bashrc" ]; then -__mdw_bashrc=done +case ${__mdw_bashrc+t} in + t) ;; + *) __mdw_bashrc=t + +###-------------------------------------------------------------------------- +### Hook implementation. + +__mdw_precmd_hook= __mdw_preexec_hook= +__mdw_running=t + +__mdw_run_precmd_hook () { + __mdw_runhook __mdw_precmd_hook "$@" + __mdw_running=nil +} +__mdw_run_preexec_hook () { + case $__mdw_running in + t) ;; + nil) + __mdw_running=t; + set -- $(history 1); shift + __mdw_runhook __mdw_preexec_hook "$*" + ;; + esac +} + +PROMPT_COMMAND=__mdw_run_precmd_hook +trap __mdw_run_preexec_hook DEBUG + +###-------------------------------------------------------------------------- +### Common shell configuration. + +. "$HOME/.shell-rc" + +###-------------------------------------------------------------------------- +### Other preliminaries. ## If we've not run the main profile yet, we should do that first. It sets ## up things we rely on. Also, if there's a system script, we should run ## that too. -[ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile -[ -r /etc/bashrc ] && . /etc/bashrc +case ${__mdw_profile+t} in t) ;; *) . "$HOME/.profile" ;; esac +__mdw_source_if_exists /etc/bashrc -## Set the temporary directory again. (If we've switched users, we'll want a -## different temporary directory.) -[ "${TMPDIR+yes}" ] || eval `tmpdir -b` +## Completion. +__mdw_source_if_exists /etc/bash_completion "$HOME/.bash_completion" ###-------------------------------------------------------------------------- ### Prompt hacking. -## Only bother if the shell is interactive. -if [ -t 0 ]; then - - ## Fancy highlighting in some terminals. +__mdw_set_prompt_hacks () { case "$TERM" in - linux*|screen*|xterm*|vt100*|eterm*) + linux*|screen*|xterm*|putty*|vt100*|eterm*) case "$(tput bold)" in "") bold="\[$(tput md)\]" unbold="\[$(tput me)\]" ;; *) bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" ;; esac + gitcolour="\[$(tput setaf 6)\]" + rccolour="\[$(tput setaf 1)\]" + uncolour="\[$(tput op)\]" nl="\[ \]" ;; - *) - bold='' unbold='' nl='' ;; esac + host='\h' dir=' \w' +} - ## 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. - if (( EUID == 0 )); then - left=`echo « | iconv -f UTF-8 -t //translit` - right=`echo » | iconv -f UTF-8 -t //translit` - else - case $USER in - mdw|mwooding) u="" left="[" right="]" ;; - *) u="\\u@" left="{" right="}" ;; - esac - if [ "$__mdw_tty" = "`tty`" ]; then - left="<" right=">" - else - export __mdw_tty="`tty`" - fi - fi - - ## If this session is insecure then highlight that. - if [ -z "$SSH_CLIENT" ] && - [ "$__mdw_sechost" != "`hostname`" ] - then - sec_l='(' sec_r=')' - fi - - ## Build the prompt string. - PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$right$unbold" - PS2="$PS1 $bold>$unbold " - +## Only bother if the shell is interactive. +if [ -t 0 ]; then + PROMPT_DIRTRIM=5 + __mdw_source_if_exists /usr/lib/git-core/git-sh-prompt + __mdw_set_prompt_pieces fi ###-------------------------------------------------------------------------- @@ -81,9 +91,10 @@ shopt -s cmdhist shopt -u dotglob shopt -s expand_aliases shopt -s extglob -shopt -s globstar +if (( ${BASH_VERSINFO[0]} >= 4 )); then shopt -s globstar; fi shopt -s gnu_errfmt shopt -s histappend +set -o histexpand shopt -s histreedit shopt -u histverify shopt -s hostcomplete @@ -98,285 +109,12 @@ shopt -u shift_verbose shopt -s sourcepath HISTCONTROL=ignorespace:erasedups -## Some handy aliases. -alias cx='chmod a+x' -alias which="command -v" -alias rc="rc -l" -alias ssync="rsync -e ssh" -rootly () { - case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac - $__MDW_ROOTLY "$@" -} -alias r=rootly -alias re="rootly $EDITOR" -alias pstree="pstree -hl" -alias cdtmp='cd ${TMPDIR-/tmp}' -alias pushtmp='pushd ${TMPDIR-/tmp}' -alias e="$EDITOR" -alias svn="svnwrap svn" -alias @="ssh" - -## Completion. -[ -r /etc/bash_completion ] && . /etc/bash_completion -[ -r $HOME/.bash_completion ] && . $HOME/.bash_completion - -###-------------------------------------------------------------------------- -### Colour output. - -## Arrange for `ls' output to be in colour. -if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then - eval `dircolors -b ~/.dircolors` -else - unset LS_COLORS -fi - -ls () { - if [ -t 1 ]; then - command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@" - else - command ls "$@" - fi -} - -## Arrange for `grep' output to be in colour. -export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34" - -greplike () { - declare grep=$1; shift - if [ -t 1 ]; then - command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager - else - command $grep "$@" - fi -} -alias grep="greplike grep" -alias egrep="greplike egrep" -alias fgrep="greplike fgrep" -alias zgrep="greplike zgrep" - -## Turn off pagers inside Emacs shell buffers. -case "$INSIDE_EMACS" in - 22.*,comint) export PAGER=cat ;; -esac - -###-------------------------------------------------------------------------- -### More complicated shell functions. - -## xt [@HOST] XTERM-ARGS -## -## Open a terminal, maybe on a remote host. -xt () { - case "$1" in - @*) - local remote=${1#@} title - shift - if [ $# -gt 0 ]; then - title="xterm [$remote] $1" - else - title="xterm [$remote]" - fi - (xterm -title "$title" -e ssh $remote "$@" &) - ;; - *) - (xterm "$@" &) - ;; - esac -} - -## core [y|n] -## -## Tweak core dumps on and off, or show the current status. -core () { - case "x$1" in - xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;; - xoff|xn|xno) ulimit -Sc 0 ;; - x) - local l=`ulimit -Sc` - case $l in - 0) echo "Core dumps disabled" ;; - unlimited) echo "Core dumps enabled" ;; - *) echo "Core dump limit is $l blocks" ;; - esac - ;; - *) - echo >&2 "usage: core [y|n]" - return 1 - ;; - esac -} - -## world [NAME] -## -## Set current security world to NAME. With no NAME, print the currently -## selected world. -world () { - local nfast=${NFAST_HOME-/opt/nfast} - local kmdata - case "$#" in - 0) - echo "${NFAST_KMDATA#$nfast/kmdata-}" - ;; - *) - if [ -d "$1" ]; then - kmdata=$1 - elif [ -d "$nfast/kmdata-$1" ]; then - kmdata=$nfast/kmdata-$1 - else - echo >&2 "world: can't find world $1" - return 1 - fi - shift - case "$#" in - 0) export NFAST_KMDATA=$kmdata ;; - *) "$@" ;; - esac - ;; - esac -} - -## Fix `man' under Slowaris. -case "$MACHTYPE" in - *solaris*) - man () { - declare -i i=0 - declare arg - declare -a man - for arg; do - case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac - man[i+=1]="$arg" - done - command man "${man[@]}" - } - ;; -esac - -###-------------------------------------------------------------------------- -### Path hacks. - -## path-add [VAR] DIR -## -## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if -## it's not there already. -path-add () { - local pathvar export dir val - case $# in - 1) pathvar=PATH dir=$1 export="export PATH";; - 2) pathvar=$1 dir=$2 export=:;; - *) echo >&2 "Usage: $0 [VAR] DIR";; - esac - eval "val=\$$pathvar" - case ":$val:" in - *:"$dir":*) ;; - *) val=$dir:$val ;; - esac - eval "$pathvar=\$val" - $export -} - -## path-remove [VAR] DIR -## -## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an -## error if DIR isn't in VAR. -path-remove () { - local pathvar export dir val - case $# in - 1) pathvar=PATH dir=$1 export="export PATH";; - 2) pathvar=$1 dir=$2 export=:;; - *) echo >&2 "Usage: $0 [VAR] DIR";; - esac - eval "val=\$$pathvar" - case ":$val:" in - :"$dir":) val= ;; - :"$dir":*) val=${val#$dir:} ;; - *:"$dir":) val=${val%:$dir} ;; - *:"$dir":*) val=${val/:$dir:/:} ;; - esac - eval "$pathvar=\$val" - $export -} - -## pathhack [-f] +HACK|-HACK... -## -## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded -## by `+' adds the directory to the PATH; a `-' removes. Adding a hack -## that's already on the PATH doesn't do anything unless `-f' is set, in -## which case it gets moved to the beginning. With no arguments, print the -## currently installed hacks. -pathhack () { - if [ $# -eq 0 ]; then - local IFS=: - for e in $PATH; do - case "$e" in - "$HOME/bin/hacks/"*) - echo ${e#$HOME/bin/hacks/} - ;; - esac - done - return - fi - local force=nil - local path=$PATH - while [ $# -gt 0 ]; do - arg=$1 - case "$arg" in - -f | --force) - force=t - shift - continue - ;; - --) - shift - break - ;; - [-+]*) - ;; - *) - break - ;; - esac - hack=${arg#[+-]} - dir=$HOME/bin/hacks/$hack - [ -d "$dir" ] || { - echo "$0: path hack $hack not found" - return 1 - } - case "$arg,$force,:$PATH:" in - -*,*,*:"$dir":*) - path-remove path "$dir" - ;; - +*,t,*:"$dir":*) - path-remove path "$dir" - path-add path "$dir" - ;; - +*,nil,*:"$dir":*) - ;; - +*,*) - path-add path "$dir" - ;; - esac - shift - done - if [ $# -eq 0 ]; then - PATH=$path - export PATH - else - PATH=$path "$@" - fi -} - ###-------------------------------------------------------------------------- ### Finishing touches. -## For `root' use -- some simple molly-guards. -if (( UID == 0 )); then - alias rm='rm -i' cp='cp -i' mv='mv -i' - set -o noclobber -fi - ## Run any local hooks. -[ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local" - -## Close the `__mdw_bashrc' guard. -fi +__mdw_source_if_exists "$HOME/.bashrc-local" ###----- That's all, folks -------------------------------------------------- + +esac