chiark / gitweb /
dot/ercrc.el: This is the modern way to configure autojoin.
[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 ## Set the temporary directory again.  (A setuid or setgid program may have
18 ## unhelpfully forgotten this for us.)
19 case ${TMPDIR+t} in t) ;; *) eval $(tmpdir -b); esac
20
21 ###--------------------------------------------------------------------------
22 ### Prompt machinery.
23
24 __mdw_set_prompt_hacks () { host=$(hostname); dir=""; }
25
26 __mdw_set_prompt_pieces () {
27   local hqual
28   hqual=""
29
30   ## Fancy highlighting in some terminals.
31   local bold unbold nl gitcolour rccolour uncolour
32   local host dir
33   bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour=""
34   __mdw_set_prompt_hacks
35
36   ## Choose the right delimiters.  Highlight root prompts specially;
37   ## highlight when I'm running as some other user.  Highlight when this
38   ## isn't the outermost shell on the terminal.
39   local left right user u tty
40   user=${USER-${LOGNAME-$(id -un)}}
41   case $(id -u) in
42     0)
43       left=$(echo « | iconv -f UTF-8 -t //translit)
44       right=$(echo » | iconv -f UTF-8 -t //translit)
45       ;;
46     *)
47       case $user in
48         mdw | mwooding | nemo) u="" left="[" right="]" ;;
49         *) u="$user@" left="{" right="}" ;;
50       esac
51       tty=$(tty)
52       case "$__mdw_tty" in
53         "$tty") left="<" right=">" ;;
54         *) __mdw_tty=$tty; export __mdw_tty ;;
55       esac
56       ;;
57   esac
58
59   ## If this session is insecure then highlight that.
60   local sec_l sec_r h
61   h=$(hostname)
62   case ${SSH_CLIENT-nil},${SCHROOT_CHROOT_NAME-nil},$__mdw_sechost in
63     nil,nil,"$h") sec_l="" sec_r="" ;;
64     nil,nil,*) sec_l="(" sec_r=")" ;;
65     *) sec_l="" sec_r=""
66   esac
67
68   ## If this is an schroot environment then point this out.
69   hqual="$hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}"
70
71   ## Put together the main pieces.
72   __mdw_prompt_left="$nl$bold$left$sec_l$u$host$hqual$sec_r$dir"
73   __mdw_prompt_git_left="$unbold$gitcolour"
74   __mdw_prompt_git_right="$uncolour$bold"
75   __mdw_prompt_rc_left="$unbold$rccolour"
76   __mdw_prompt_rc_right="$uncolour$bold"
77   __mdw_prompt_right="$right$unbold"
78 }
79
80 __mdw_set_prompt () {
81   __mdw_last_rc=$?
82   local git rc
83   if type __git_ps1 >/dev/null 2>&1; then
84     git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
85   else
86     git=""
87   fi
88   case $__mdw_last_rc in
89     0) rc="" ;;
90     *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
91   esac
92   PS1="$__mdw_prompt_left$git$rc$__mdw_prompt_right"
93   PS2="$PS1 $bold>$unbold "
94   unset __mdw_last_rc
95 }
96
97 __mdw_precmd () {
98   __mdw_set_prompt
99   case ${STY+t} in
100     t) printf "\ek%s\e\\" "$__mdw_shell" ;;
101   esac
102 }
103
104 __mdw_preexec () {
105   case ${STY+t} in
106     t) printf "\ek%s\e\\" "$1" ;;
107   esac
108 }
109
110 ###--------------------------------------------------------------------------
111 ### Some handy aliases.
112
113 alias cx='chmod +x'
114 alias which="command -v"
115 alias rc="rc -l"
116 rootly () {
117   case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
118   $__MDW_ROOTLY "$@"
119 }
120 alias r=rootly
121 alias re="rootly $EDITOR"
122 alias pstree="pstree -hl"
123 alias cdtmp='cd ${TMPDIR-/tmp}'
124 alias pushtmp='pushd ${TMPDIR-/tmp}'
125 alias e="$EDITOR"
126 alias svn="svnwrap svn"
127 alias @="ssh"
128
129 ###--------------------------------------------------------------------------
130 ### Colour output.
131
132 ## Arrange for `ls' output to be in colour.
133 if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
134 else unset LS_COLORS; fi
135
136 unalias ls 2>/dev/null || :
137 ls () {
138   if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
139   else command ls "$@"; fi
140 }
141
142 ## Arrange for `grep' output to be in colour.
143 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
144
145 greplike () {
146   local grep=$1; shift
147   if [ -t 1 ]; then
148     command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
149   else
150     command $grep "$@"
151   fi
152 }
153 alias grep="greplike grep"
154 alias egrep="greplike egrep"
155 alias fgrep="greplike fgrep"
156 alias zgrep="greplike zgrep"
157
158 ###--------------------------------------------------------------------------
159 ### Other hacks.
160
161 ## Turn off pagers inside Emacs shell buffers.
162 case "$INSIDE_EMACS" in
163   2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
164 esac
165
166 ###--------------------------------------------------------------------------
167 ### More complicated shell functions.
168
169 ## xt [@HOST] XTERM-ARGS
170 ##
171 ## Open a terminal, maybe on a remote host.
172 xt () {
173   case "$1" in
174     @*)
175       local remote=${1#@} title
176       shift
177       if [ $# -gt 0 ]; then
178         title="xterm [$remote] $1"
179       else
180         title="xterm [$remote]"
181       fi
182       (xterm -title "$title" -e ssh $remote "$@" &)
183       ;;
184     *)
185       (xterm "$@" &)
186       ;;
187   esac
188 }
189
190 ## core [y|n]
191 ##
192 ## Tweak core dumps on and off, or show the current status.
193 core () {
194   case "x$1" in
195     xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;;
196     xoff|xn|xno) ulimit -Sc 0 ;;
197     x)
198       local l=$(ulimit -Sc)
199       case $l in
200         0) echo "Core dumps disabled" ;;
201         unlimited) echo "Core dumps enabled" ;;
202         *) echo "Core dump limit is $l blocks" ;;
203       esac
204       ;;
205     *)
206       echo >&2 "usage: core [y|n]"
207       return 1
208       ;;
209   esac
210 }
211
212 ## world [NAME]
213 ##
214 ## Set current security world to NAME.  With no NAME, print the currently
215 ## selected world.
216 world () {
217   local nfast=${NFAST_HOME-/opt/nfast}
218   local kmdata
219   case "$#" in
220     0)
221       echo "${NFAST_KMDATA#$nfast/kmdata-}"
222       ;;
223     *)
224       if [ -d "$1" ]; then
225         kmdata=$1
226       elif [ -d "$nfast/kmdata-$1" ]; then
227         kmdata=$nfast/kmdata-$1
228       else
229         echo >&2 "world: can't find world $1"
230         return 1
231       fi
232       shift
233       case "$#" in
234         0) export NFAST_KMDATA=$kmdata ;;
235         *) "$@" ;;
236       esac
237       ;;
238   esac
239 }
240
241 ## path-add [VAR] DIR
242 ##
243 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
244 ## it's not there already.
245 path_add () {
246   local pathvar export dir val
247   case $# in
248     1) pathvar=PATH dir=$1 export="export PATH" ;;
249     2) pathvar=$1 dir=$2 export=: ;;
250     *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
251   esac
252   eval val=\$$pathvar
253   case ":$val:" in
254     *:"$dir":*) ;;
255     *) val=$dir:$val ;;
256   esac
257   eval $pathvar=\$val
258   eval $export
259 }
260
261 ## path-remove [VAR] DIR
262 ##
263 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
264 ## error if DIR isn't in VAR.
265 path_remove () {
266   local pathvar export dir val
267   case $# in
268     1) pathvar=PATH dir=$1 export="export PATH" ;;
269     2) pathvar=$1 dir=$2 export=: ;;
270     *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
271   esac
272   eval val=\$$pathvar
273   case ":$val:" in
274     :"$dir":) val= ;;
275     :"$dir":*) val=${val#$dir:} ;;
276     *:"$dir":) val=${val%:$dir} ;;
277     *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;;
278   esac
279   eval $pathvar=\$val
280   eval $export
281 }
282
283 ## pathhack [-f] +HACK|-HACK...
284 ##
285 ## Each HACK refers to a subdirectory of `~/bin/hacks'.  A hack name preceded
286 ## by `+' adds the directory to the PATH; a `-' removes.  Adding a hack
287 ## that's already on the PATH doesn't do anything unless `-f' is set, in
288 ## which case it gets moved to the beginning.  With no arguments, print the
289 ## currently installed hacks.
290 pathhack () {
291   local p e force arg hack dir
292   p=$PATH
293   if [ $# -eq 0 ]; then
294     while :; do
295       e=${p%%:*}
296       case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac
297       case "$p" in *:*) p=${p#*:} ;; *) break ;; esac
298     done
299     return
300   fi
301   force=nil
302   while [ $# -gt 0 ]; do
303     arg=$1
304     case "$arg" in
305       -f | --force) force=t; shift; continue ;;
306       --) shift; break ;;
307       [-+]*) ;;
308       *) break; ;;
309     esac
310     hack=${arg#[+-]}
311     dir=$HOME/bin/hacks/$hack
312     if ! [ -d "$dir" ]; then
313       echo "$0: path hack $hack not found"
314       return 1
315     fi
316     case "$arg,$force,:$PATH:" in
317       -*,*,*:"$dir":*) path_remove p "$dir" ;;
318       +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;;
319       +*,nil,*:"$dir":*) ;;
320       +*,*) path_add p "$dir" ;;
321     esac
322     shift
323   done
324   if [ $# -eq 0 ]; then PATH=$p; export PATH
325   else PATH=$p "$@"; fi
326 }
327
328 ###--------------------------------------------------------------------------
329 ### Finishing touches.
330
331 ## For `root' use -- some simple molly-guards.
332 case $(id -u) in
333   0)
334     alias rm="rm -i" cp="cp -i" mv="mv -i"
335     set -o noclobber
336     ;;
337 esac
338
339 ## Run any local hooks.
340 __mdw_source_if_exists "$HOME/.shell-local"
341
342 ###----- That's all, folks --------------------------------------------------