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