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