dot/bash*, dot/rcrc: Force correct shell support in Emacs.
[profile] / dot / bashrc
1 ### -*-bash-*-
2 ###
3 ### Bash session things
4
5 ## Only do this if we haven't done it before. (Note that this guard isn't
6 ## exported, so subshells will need to make their own arrangements.)
7 if [ -z "$__mdw_bashrc" ]; then
8 __mdw_bashrc=done
9
10 ## If we've not run the main profile yet, we should do that first. It sets
11 ## up things we rely on. Also, if there's a system script, we should run
12 ## that too.
13 [ -z "$__mdw_profile" -a -r $HOME/.bash_profile ] && . $HOME/.bash_profile
14 [ -r /etc/bashrc ] && . /etc/bashrc
15
16 ## Set the temporary directory again. (If we've switched users, we'll want a
17 ## different temporary directory.)
18 [ "${TMPDIR+yes}" ] || eval `tmpdir -b`
19
20 ###--------------------------------------------------------------------------
21 ### Prompt hacking.
22
23 ## Only bother if the shell is interactive.
24 if [ -t 0 ]; then
25
26 ## Fancy highlighting in some terminals.
27 case "$TERM" in
28 linux*|screen*|xterm*|vt100*|eterm*)
29 case "$(tput bold)" in
30 "") bold="\[$(tput md)\]" unbold="\[$(tput me)\]" ;;
31 *) bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" ;;
32 esac
33 nl="\[ \]"
34 ;;
35 *)
36 bold='' unbold='' nl='' ;;
37 esac
38
39 ## Choose the right delimiters. Highlight root prompts specially;
40 ## highlight when I'm running as some other user. Highlight when this
41 ## isn't the outermost shell on the terminal.
42 if (( EUID == 0 )); then
43 left=`echo « | iconv -f UTF-8 -t //translit`
44 right=`echo » | iconv -f UTF-8 -t //translit`
45 else
46 case $USER in
47 mdw|mwooding) u="" left="[" right="]" ;;
48 *) u="\\u@" left="{" right="}" ;;
49 esac
50 if [ "$__mdw_tty" = "`tty`" ]; then
51 left="<" right=">"
52 else
53 export __mdw_tty="`tty`"
54 fi
55 fi
56
57 ## If this session is insecure then highlight that.
58 if [ -z "$SSH_CLIENT" ] &&
59 [ "$__mdw_sechost" != "`hostname`" ]
60 then
61 sec_l='(' sec_r=')'
62 fi
63
64 ## Build the prompt string.
65 PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$right$unbold"
66 PS2="$PS1 $bold>$unbold "
67
68 fi
69
70 ###--------------------------------------------------------------------------
71 ### Other shell tweaking.
72
73 ## Random shell tweaks.
74 notify=1
75 set -b
76 shopt -u cdable_vars
77 shopt -s cdspell
78 shopt -s checkhash
79 shopt -s checkwinsize
80 shopt -s cmdhist
81 shopt -u dotglob
82 shopt -s expand_aliases
83 shopt -s extglob
84 shopt -s globstar
85 shopt -s gnu_errfmt
86 shopt -s histappend
87 shopt -s histreedit
88 shopt -u histverify
89 shopt -s hostcomplete
90 shopt -s huponexit
91 shopt -s interactive_comments
92 shopt -s lithist
93 shopt -u mailwarn
94 shopt -u nocaseglob
95 shopt -u nullglob
96 shopt -s promptvars
97 shopt -u shift_verbose
98 shopt -s sourcepath
99 HISTCONTROL=ignorespace:erasedups
100
101 ## Some handy aliases.
102 alias cx='chmod a+x'
103 alias which="command -v"
104 alias rc="rc -l"
105 alias ssync="rsync -e ssh"
106 rootly () {
107 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
108 $__MDW_ROOTLY "$@"
109 }
110 alias r=rootly
111 alias re="rootly $EDITOR"
112 alias pstree="pstree -hl"
113 alias cdtmp='cd ${TMPDIR-/tmp}'
114 alias pushtmp='pushd ${TMPDIR-/tmp}'
115 alias e="$EDITOR"
116 alias svn="svnwrap svn"
117 alias @="ssh"
118
119 ## Completion.
120 [ -r /etc/bash_completion ] && . /etc/bash_completion
121 [ -r $HOME/.bash_completion ] && . $HOME/.bash_completion
122
123 ###--------------------------------------------------------------------------
124 ### Colour output.
125
126 ## Arrange for `ls' output to be in colour.
127 if [ -x /usr/bin/dircolors -o -x /usr/local/bin/dircolors ]; then
128 eval `dircolors -b ~/.dircolors`
129 else
130 unset LS_COLORS
131 fi
132
133 ls () {
134 if [ -t 1 ]; then
135 command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
136 else
137 command ls "$@"
138 fi
139 }
140
141 ## Arrange for `grep' output to be in colour.
142 export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
143
144 greplike () {
145 declare grep=$1; shift
146 if [ -t 1 ]; then
147 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
148 else
149 command $grep "$@"
150 fi
151 }
152 alias grep="greplike grep"
153 alias egrep="greplike egrep"
154 alias fgrep="greplike fgrep"
155 alias zgrep="greplike zgrep"
156
157 ## Turn off pagers inside Emacs shell buffers.
158 case "$INSIDE_EMACS" in
159 22.*,comint) export PAGER=cat ;;
160 esac
161
162 ###--------------------------------------------------------------------------
163 ### More complicated shell functions.
164
165 ## xt [@HOST] XTERM-ARGS
166 ##
167 ## Open a terminal, maybe on a remote host.
168 xt () {
169 case "$1" in
170 @*)
171 local remote=${1#@} title
172 shift
173 if [ $# -gt 0 ]; then
174 title="xterm [$remote] $1"
175 else
176 title="xterm [$remote]"
177 fi
178 (xterm -title "$title" -e ssh $remote "$@" &)
179 ;;
180 *)
181 (xterm "$@" &)
182 ;;
183 esac
184 }
185
186 ## core [y|n]
187 ##
188 ## Tweak core dumps on and off, or show the current status.
189 core () {
190 case "x$1" in
191 xon|xy|xyes) ulimit -Sc `ulimit -Hc` ;;
192 xoff|xn|xno) ulimit -Sc 0 ;;
193 x)
194 local l=`ulimit -Sc`
195 case $l in
196 0) echo "Core dumps disabled" ;;
197 unlimited) echo "Core dumps enabled" ;;
198 *) echo "Core dump limit is $l blocks" ;;
199 esac
200 ;;
201 *)
202 echo >&2 "usage: core [y|n]"
203 return 1
204 ;;
205 esac
206 }
207
208 ## world [NAME]
209 ##
210 ## Set current security world to NAME. With no NAME, print the currently
211 ## selected world.
212 world () {
213 local nfast=${NFAST_HOME-/opt/nfast}
214 local kmdata
215 case "$#" in
216 0)
217 echo "${NFAST_KMDATA#$nfast/kmdata-}"
218 ;;
219 *)
220 if [ -d "$1" ]; then
221 kmdata=$1
222 elif [ -d "$nfast/kmdata-$1" ]; then
223 kmdata=$nfast/kmdata-$1
224 else
225 echo >&2 "world: can't find world $1"
226 return 1
227 fi
228 shift
229 case "$#" in
230 0) export NFAST_KMDATA=$kmdata ;;
231 *) "$@" ;;
232 esac
233 ;;
234 esac
235 }
236
237 ## Fix `man' under Slowaris.
238 case "$MACHTYPE" in
239 *solaris*)
240 man () {
241 declare -i i=0
242 declare arg
243 declare -a man
244 for arg; do
245 case "$arg" in [0-9]*) man[i+=1]="-s" ;; esac
246 man[i+=1]="$arg"
247 done
248 command man "${man[@]}"
249 }
250 ;;
251 esac
252
253 ###--------------------------------------------------------------------------
254 ### Path hacks.
255
256 ## path-add [VAR] DIR
257 ##
258 ## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
259 ## it's not there already.
260 path-add () {
261 local pathvar export dir val
262 case $# in
263 1) pathvar=PATH dir=$1 export="export PATH";;
264 2) pathvar=$1 dir=$2 export=:;;
265 *) echo >&2 "Usage: $0 [VAR] DIR";;
266 esac
267 eval "val=\$$pathvar"
268 case ":$val:" in
269 *:"$dir":*) ;;
270 *) val=$dir:$val ;;
271 esac
272 eval "$pathvar=\$val"
273 $export
274 }
275
276 ## path-remove [VAR] DIR
277 ##
278 ## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
279 ## error if DIR isn't in VAR.
280 path-remove () {
281 local pathvar export dir val
282 case $# in
283 1) pathvar=PATH dir=$1 export="export PATH";;
284 2) pathvar=$1 dir=$2 export=:;;
285 *) echo >&2 "Usage: $0 [VAR] DIR";;
286 esac
287 eval "val=\$$pathvar"
288 case ":$val:" in
289 :"$dir":) val= ;;
290 :"$dir":*) val=${val#$dir:} ;;
291 *:"$dir":) val=${val%:$dir} ;;
292 *:"$dir":*) val=${val/:$dir:/:} ;;
293 esac
294 eval "$pathvar=\$val"
295 $export
296 }
297
298 ## pathhack [-f] +HACK|-HACK...
299 ##
300 ## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
301 ## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
302 ## that's already on the PATH doesn't do anything unless `-f' is set, in
303 ## which case it gets moved to the beginning. With no arguments, print the
304 ## currently installed hacks.
305 pathhack () {
306 if [ $# -eq 0 ]; then
307 local IFS=:
308 for e in $PATH; do
309 case "$e" in
310 "$HOME/bin/hacks/"*)
311 echo ${e#$HOME/bin/hacks/}
312 ;;
313 esac
314 done
315 return
316 fi
317 local force=nil
318 local path=$PATH
319 while [ $# -gt 0 ]; do
320 arg=$1
321 case "$arg" in
322 -f | --force)
323 force=t
324 shift
325 continue
326 ;;
327 --)
328 shift
329 break
330 ;;
331 [-+]*)
332 ;;
333 *)
334 break
335 ;;
336 esac
337 hack=${arg#[+-]}
338 dir=$HOME/bin/hacks/$hack
339 [ -d "$dir" ] || {
340 echo "$0: path hack $hack not found"
341 return 1
342 }
343 case "$arg,$force,:$PATH:" in
344 -*,*,*:"$dir":*)
345 path-remove path "$dir"
346 ;;
347 +*,t,*:"$dir":*)
348 path-remove path "$dir"
349 path-add path "$dir"
350 ;;
351 +*,nil,*:"$dir":*)
352 ;;
353 +*,*)
354 path-add path "$dir"
355 ;;
356 esac
357 shift
358 done
359 if [ $# -eq 0 ]; then
360 PATH=$path
361 export PATH
362 else
363 PATH=$path "$@"
364 fi
365 }
366
367 ###--------------------------------------------------------------------------
368 ### Finishing touches.
369
370 ## For `root' use -- some simple molly-guards.
371 if (( UID == 0 )); then
372 alias rm='rm -i' cp='cp -i' mv='mv -i'
373 set -o noclobber
374 fi
375
376 ## Run any local hooks.
377 [ -f "$HOME/.bashrc-local" ] && . "$HOME/.bashrc-local"
378
379 ## Close the `__mdw_bashrc' guard.
380 fi
381
382 ###----- That's all, folks --------------------------------------------------