dot/shell-rc: Enable the `wakey' machinery by default, when it will work.
[profile] / dot / shell-rc
CommitLineData
74a53e28
MW
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###--------------------------------------------------------------------------
4877b8d8
MW
18### Hooks.
19
20__mdw_addhook () {
21 local hk=$1 fn=$2 t
22
23 eval t=\${$hk+t}
24 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
25
26 eval t=\$$hk
27 case " $t " in
28 *" $fn "*) ;;
29 *) eval "$hk=\${$hk:+\$$hk }\$fn" ;;
30 esac
31}
32
33__mdw_delhook () {
34 local hk=$1 fn=$2 t l r
35
36 eval t=\${$hk+t}
37 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
38
39 eval t=\" \$$hk \"
40 case $t in
41 *" $fn "*)
42 l=${t%% $fn*} r=${t##*$fn }
43 l=${l# } r=${r% }
44 eval "$hk=\$l\${l:+ }\$r"
45 ;;
46 esac
47}
48
49__mdw_setrc () { return $1; }
50
51__mdw_runhook () {
52 local hk=$1 saverc=$? t i; shift
53
54 eval t=\${$hk+t}
55 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
56
57 eval t=\$$hk
58 for i in $t; do __mdw_setrc $saverc; "$i" "$@"; done
59}
60
61###--------------------------------------------------------------------------
74a53e28
MW
62### Prompt machinery.
63
2bbac58c 64__mdw_host=$(hostname)
d4bbc0c0
MW
65__mdw_hqual=
66__mdw_hqual=$__mdw_hqual${SCHROOT_CHROOT_NAME+/$SCHROOT_CHROOT_NAME}
67__mdw_hqual=$__mdw_hqual${CROSS_BUILDENV+/$CROSS_BUILDENV}
2bbac58c 68__mdw_set_prompt_hacks () { host=$__mdw_host; dir=""; }
74a53e28 69
08785e51 70__mdw_system=$(uname -s)
c35d2efc 71: ${USER-${LOGNAME-$(id -un)}}
85e5c878 72__mdw_user=$USER
c35d2efc 73
fde3353f 74case $(id -u) in
08785e51
MW
75 0)
76 __mdw_rootp=t
77 ;;
78 *)
79 case $__mdw_system in
80 CYGWIN_*)
81 case " $(id -G) " in
82 *" 544 "*) __mdw_rootp=t __mdw_user="$__mdw_user%admin" ;;
83 *) __mdw_rootp=nil ;;
84 esac
85 ;;
86 *)
87 __mdw_rootp=nil
88 ;;
89 esac
fde3353f
MW
90esac
91
74a53e28 92__mdw_set_prompt_pieces () {
74a53e28
MW
93
94 ## Fancy highlighting in some terminals.
441fb2bd 95 local bold unbold nl more host dir
0c7867d9 96 local gitcolour extracolour rccolour uncolour
441fb2bd 97 bold="" unbold="" nl="" more=""
0c7867d9 98 gitcolour="" extracolour="" rccolour="" uncolour=""
74a53e28
MW
99 __mdw_set_prompt_hacks
100
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.
81ba988c 104 local left right u tty
fde3353f
MW
105 case $__mdw_rootp in
106 t)
74a53e28
MW
107 left=$(echo « | iconv -f UTF-8 -t //translit)
108 right=$(echo » | iconv -f UTF-8 -t //translit)
109 ;;
fde3353f 110 nil)
81ba988c 111 case $USER in
74a53e28 112 mdw | mwooding | nemo) u="" left="[" right="]" ;;
85e5c878 113 *) u="$__mdw_user@" left="{" right="}" ;;
74a53e28
MW
114 esac
115 tty=$(tty)
116 case "$__mdw_tty" in
117 "$tty") left="<" right=">" ;;
118 *) __mdw_tty=$tty; export __mdw_tty ;;
119 esac
120 ;;
121 esac
122
123 ## If this session is insecure then highlight that.
124 local sec_l sec_r h
125 h=$(hostname)
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=")" ;;
129 *) sec_l="" sec_r=""
130 esac
131
1a1af55e
MW
132 ## If this is an schroot environment or some other interesting augmented
133 ## environment then point this out.
74a53e28
MW
134
135 ## Put together the main pieces.
d4bbc0c0 136 __mdw_prompt_left="$nl$bold$left$sec_l$u$host$__mdw_hqual$sec_r$dir"
74a53e28
MW
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"
0c7867d9
MW
141 __mdw_prompt_extra_left="$unbold$extracolour"
142 __mdw_prompt_extra_right="$uncolour$bold"
74a53e28 143 __mdw_prompt_right="$right$unbold"
55360ba3 144 __mdw_prompt_more=" $more$bold>$unbold "
74a53e28
MW
145}
146
147__mdw_set_prompt () {
062b450d 148 case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
d281a650 149 dumb:)
55bd0261
MW
150 case $(id -u) in 0) PS1='# ' ;; *) PS1='$ ' ;; esac
151 PS2='> '
152 ;;
153 *)
154 __mdw_last_rc=$?
0c7867d9 155 local git extra rc
55bd0261
MW
156 if type __git_ps1 >/dev/null 2>&1; then
157 git="$__mdw_prompt_git_left$(__git_ps1)$__mdw_prompt_git_right"
158 else
159 git=""
160 fi
161 case $__mdw_last_rc in
162 0) rc="" ;;
163 *) rc="$__mdw_prompt_rc_left rc=$__mdw_last_rc$__mdw_prompt_rc_right" ;;
164 esac
0c7867d9
MW
165 case $__mdw_prompt_extra in
166 ?*) extra="$__mdw_prompt_extra_left [$__mdw_prompt_extra]$__mdw_prompt_extra_right" ;;
167 *) extra="" ;;
168 esac
169 PS1="$__mdw_prompt_left$git$extra$rc$__mdw_prompt_right"
55360ba3 170 PS2="$PS1$__mdw_prompt_more"
55bd0261
MW
171 unset __mdw_last_rc
172 ;;
74a53e28 173 esac
74a53e28
MW
174}
175
1e8b505f
MW
176__mdw_xterm_settitle () {
177 printf >/dev/tty \
178 "\e]2;%s@%s:%s – %s\e\\" \
85e5c878 179 "$__mdw_user" "$__mdw_host$__mdw_hqual" "$PWD" \
1e8b505f
MW
180 "$1"
181}
182__mdw_xterm_precmd () { __mdw_xterm_settitle "$__mdw_shell"; }
183__mdw_xterm_preexec () { __mdw_xterm_settitle "$1"; }
05f8a57d 184
1e8b505f
MW
185__mdw_screen_settitle () {
186 printf >/dev/tty \
187 "\ek%s\e\\" \
188 "$1"
189}
190__mdw_screen_precmd () { __mdw_screen_settitle "$__mdw_shell"; }
191__mdw_screen_preexec () { __mdw_screen_settitle "$1"; }
74a53e28 192
4877b8d8 193if [ -t 0 ]; then
05f8a57d
MW
194 case ${STY+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t},${TERM} in
195 ,t,t,xterm*)
196 __mdw_addhook __mdw_precmd_hook __mdw_xterm_precmd
197 __mdw_addhook __mdw_preexec_hook __mdw_xterm_preexec
198 ;;
199 t,t,t,*)
4877b8d8
MW
200 __mdw_addhook __mdw_precmd_hook __mdw_screen_precmd
201 __mdw_addhook __mdw_preexec_hook __mdw_screen_preexec
202 ;;
74a53e28 203 esac
4877b8d8
MW
204 case ${__mdw_precmd_hook+t} in
205 t) __mdw_addhook __mdw_precmd_hook __mdw_set_prompt ;;
206 esac
207fi
74a53e28
MW
208
209###--------------------------------------------------------------------------
210### Some handy aliases.
211
212alias cx='chmod +x'
213alias which="command -v"
214alias rc="rc -l"
215rootly () {
216 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
217 $__MDW_ROOTLY "$@"
218}
219alias r=rootly
220alias re="rootly $EDITOR"
221alias pstree="pstree -hl"
222alias cdtmp='cd ${TMPDIR-/tmp}'
223alias pushtmp='pushd ${TMPDIR-/tmp}'
224alias e="$EDITOR"
225alias svn="svnwrap svn"
226alias @="ssh"
daeece4b 227alias make="nice make"
ee747cc6 228alias cross-run="nice cross-run"
8712436f 229alias gdb="gdb -q"
74a53e28 230
4e7092a3
MW
231## Shut up Lisp interpreters.
232alias clisp="clisp -q -q"
233alias cmucl="rlwrap cmucl -quiet"
234alias ecl="rlwrap ecl"
235alias sbcl="rlwrap sbcl --noinform"
6a8c8070
MW
236alias ccl="rlwrap ccl"
237alias ccl32="rlwrap ccl32"
238alias ccl64="rlwrap ccl64"
239alias abcl="rlwrap abcl --noinform"
4e7092a3 240
74a53e28
MW
241###--------------------------------------------------------------------------
242### Colour output.
243
244## Arrange for `ls' output to be in colour.
245if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
246else unset LS_COLORS; fi
247
248unalias ls 2>/dev/null || :
249ls () {
250 if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
251 else command ls "$@"; fi
252}
253
254## Arrange for `grep' output to be in colour.
255export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
256
257greplike () {
258 local grep=$1; shift
259 if [ -t 1 ]; then
260 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
261 else
262 command $grep "$@"
263 fi
264}
265alias grep="greplike grep"
266alias egrep="greplike egrep"
267alias fgrep="greplike fgrep"
268alias zgrep="greplike zgrep"
269
049cd015
MW
270## Arrange for `diff' output to be in colour.
271export DIFF_COLORS="hd=1:ln=36:ad=32:de=31"
272difflike () {
273 local diff=$1; shift
274 if [ -t 1 ]; then
275 command $diff \
276 ${DIFF_COLORS+--color=always} \
277 ${DIFF_COLORS+--palette="$DIFF_COLORS"} \
278 "$@" | mdw-pager
279 else
280 command $diff "$@" | cat
281 fi
282}
283alias diff="difflike diff"
284
74a53e28
MW
285###--------------------------------------------------------------------------
286### Other hacks.
287
288## Turn off pagers inside Emacs shell buffers.
289case "$INSIDE_EMACS" in
290 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
291esac
292
293###--------------------------------------------------------------------------
294### More complicated shell functions.
295
296## xt [@HOST] XTERM-ARGS
297##
298## Open a terminal, maybe on a remote host.
299xt () {
300 case "$1" in
301 @*)
302 local remote=${1#@} title
303 shift
304 if [ $# -gt 0 ]; then
305 title="xterm [$remote] $1"
306 else
307 title="xterm [$remote]"
308 fi
309 (xterm -title "$title" -e ssh $remote "$@" &)
310 ;;
311 *)
312 (xterm "$@" &)
313 ;;
314 esac
315}
316
317## core [y|n]
318##
319## Tweak core dumps on and off, or show the current status.
320core () {
321 case "x$1" in
322 xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;;
323 xoff|xn|xno) ulimit -Sc 0 ;;
324 x)
325 local l=$(ulimit -Sc)
326 case $l in
327 0) echo "Core dumps disabled" ;;
328 unlimited) echo "Core dumps enabled" ;;
329 *) echo "Core dump limit is $l blocks" ;;
330 esac
331 ;;
332 *)
333 echo >&2 "usage: core [y|n]"
334 return 1
335 ;;
336 esac
337}
338
339## world [NAME]
340##
341## Set current security world to NAME. With no NAME, print the currently
342## selected world.
343world () {
344 local nfast=${NFAST_HOME-/opt/nfast}
345 local kmdata
346 case "$#" in
347 0)
348 echo "${NFAST_KMDATA#$nfast/kmdata-}"
349 ;;
350 *)
351 if [ -d "$1" ]; then
352 kmdata=$1
353 elif [ -d "$nfast/kmdata-$1" ]; then
354 kmdata=$nfast/kmdata-$1
355 else
356 echo >&2 "world: can't find world $1"
357 return 1
358 fi
359 shift
360 case "$#" in
361 0) export NFAST_KMDATA=$kmdata ;;
362 *) "$@" ;;
363 esac
364 ;;
365 esac
366}
367
368## path-add [VAR] DIR
369##
370## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
371## it's not there already.
372path_add () {
373 local pathvar export dir val
374 case $# in
375 1) pathvar=PATH dir=$1 export="export PATH" ;;
376 2) pathvar=$1 dir=$2 export=: ;;
377 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
378 esac
379 eval val=\$$pathvar
380 case ":$val:" in
381 *:"$dir":*) ;;
382 *) val=$dir:$val ;;
383 esac
384 eval $pathvar=\$val
385 eval $export
386}
387
388## path-remove [VAR] DIR
389##
390## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
391## error if DIR isn't in VAR.
392path_remove () {
393 local pathvar export dir val
394 case $# in
395 1) pathvar=PATH dir=$1 export="export PATH" ;;
396 2) pathvar=$1 dir=$2 export=: ;;
397 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
398 esac
399 eval val=\$$pathvar
400 case ":$val:" in
401 :"$dir":) val= ;;
402 :"$dir":*) val=${val#$dir:} ;;
403 *:"$dir":) val=${val%:$dir} ;;
404 *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;;
405 esac
406 eval $pathvar=\$val
407 eval $export
408}
409
410## pathhack [-f] +HACK|-HACK...
411##
412## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
413## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
414## that's already on the PATH doesn't do anything unless `-f' is set, in
415## which case it gets moved to the beginning. With no arguments, print the
416## currently installed hacks.
417pathhack () {
418 local p e force arg hack dir
419 p=$PATH
420 if [ $# -eq 0 ]; then
421 while :; do
422 e=${p%%:*}
423 case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac
424 case "$p" in *:*) p=${p#*:} ;; *) break ;; esac
425 done
426 return
427 fi
428 force=nil
429 while [ $# -gt 0 ]; do
430 arg=$1
431 case "$arg" in
432 -f | --force) force=t; shift; continue ;;
433 --) shift; break ;;
434 [-+]*) ;;
435 *) break; ;;
436 esac
437 hack=${arg#[+-]}
438 dir=$HOME/bin/hacks/$hack
439 if ! [ -d "$dir" ]; then
440 echo "$0: path hack $hack not found"
441 return 1
442 fi
443 case "$arg,$force,:$PATH:" in
444 -*,*,*:"$dir":*) path_remove p "$dir" ;;
445 +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;;
446 +*,nil,*:"$dir":*) ;;
447 +*,*) path_add p "$dir" ;;
448 esac
449 shift
450 done
451 if [ $# -eq 0 ]; then PATH=$p; export PATH
452 else PATH=$p "$@"; fi
453}
454
455###--------------------------------------------------------------------------
456### Finishing touches.
457
4cb8b8da
MW
458## Make sure `$HOME/bin' is on the path.
459path_add "$HOME/bin"
460
6054bdb6
MW
461## Set the temporary directory again. (A setuid or setgid program may have
462## unhelpfully forgotten this for us.)
0a030a39
MW
463case ${TMPDIR+t} in
464 t) ;;
465 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
466esac
6054bdb6 467
74a53e28
MW
468## For `root' use -- some simple molly-guards.
469case $(id -u) in
470 0)
471 alias rm="rm -i" cp="cp -i" mv="mv -i"
472 set -o noclobber
473 ;;
474esac
475
0a469e47
MW
476## Install wakeup machinery for long-running programs.
477case ${DISPLAY+t},${WINDOWID+t},${__mdw_precmd_hook+t},${__mdw_preexec_hook+t} in
478 t,t,t,t)
479 if [ -t 0 ] && __mdw_programp notify-send; then . $HOME/bin/wakey.sh; fi
480 ;;
481esac
482
74a53e28
MW
483## Run any local hooks.
484__mdw_source_if_exists "$HOME/.shell-local"
485
486###----- That's all, folks --------------------------------------------------