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