chiark / gitweb /
dot/zshrc: Recognize the keypad keys.
[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###--------------------------------------------------------------------------
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
55360ba3
MW
28 local host dir more
29 bold="" unbold="" nl="" gitcolour="" rccolour="" uncolour="" more=""
74a53e28
MW
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
2ab43296 36 user=${USER-${LOGNAME-$(id -un)}}
74a53e28
MW
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"
55360ba3 74 __mdw_prompt_more=" $more$bold>$unbold "
74a53e28
MW
75}
76
77__mdw_set_prompt () {
062b450d 78 case "${TERM-dumb}:${INSIDE_EMACS+$INSIDE_EMACS}" in
d281a650 79 dumb:)
55bd0261
MW
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"
55360ba3 96 PS2="$PS1$__mdw_prompt_more"
55bd0261
MW
97 unset __mdw_last_rc
98 ;;
74a53e28 99 esac
74a53e28
MW
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
118alias cx='chmod +x'
119alias which="command -v"
120alias rc="rc -l"
121rootly () {
122 case $# in 0) set -- "${SHELL-/bin/sh}" ;; esac
123 $__MDW_ROOTLY "$@"
124}
125alias r=rootly
126alias re="rootly $EDITOR"
127alias pstree="pstree -hl"
128alias cdtmp='cd ${TMPDIR-/tmp}'
129alias pushtmp='pushd ${TMPDIR-/tmp}'
130alias e="$EDITOR"
131alias svn="svnwrap svn"
132alias @="ssh"
133
134###--------------------------------------------------------------------------
135### Colour output.
136
137## Arrange for `ls' output to be in colour.
138if __mdw_programp dircolors; then eval $(dircolors -b "$HOME/.dircolors")
139else unset LS_COLORS; fi
140
141unalias ls 2>/dev/null || :
142ls () {
143 if [ -t 1 ]; then command ls $LS_OPTIONS ${LS_COLORS+--color=auto} "$@"
144 else command ls "$@"; fi
145}
146
147## Arrange for `grep' output to be in colour.
148export GREP_COLORS="mt=01;31:ms=01;31:mc=031;31:fn=36:ln=36:bn=36:se=34"
149
150greplike () {
151 local grep=$1; shift
152 if [ -t 1 ]; then
153 command $grep ${GREP_COLORS+--color=always} "$@" | mdw-pager
154 else
155 command $grep "$@"
156 fi
157}
158alias grep="greplike grep"
159alias egrep="greplike egrep"
160alias fgrep="greplike fgrep"
161alias zgrep="greplike zgrep"
162
163###--------------------------------------------------------------------------
164### Other hacks.
165
166## Turn off pagers inside Emacs shell buffers.
167case "$INSIDE_EMACS" in
168 2[2-9].*,comint | [3-9][0-9].*,comint) export PAGER=cat ;;
169esac
170
171###--------------------------------------------------------------------------
172### More complicated shell functions.
173
174## xt [@HOST] XTERM-ARGS
175##
176## Open a terminal, maybe on a remote host.
177xt () {
178 case "$1" in
179 @*)
180 local remote=${1#@} title
181 shift
182 if [ $# -gt 0 ]; then
183 title="xterm [$remote] $1"
184 else
185 title="xterm [$remote]"
186 fi
187 (xterm -title "$title" -e ssh $remote "$@" &)
188 ;;
189 *)
190 (xterm "$@" &)
191 ;;
192 esac
193}
194
195## core [y|n]
196##
197## Tweak core dumps on and off, or show the current status.
198core () {
199 case "x$1" in
200 xon|xy|xyes) ulimit -Sc $(ulimit -Hc) ;;
201 xoff|xn|xno) ulimit -Sc 0 ;;
202 x)
203 local l=$(ulimit -Sc)
204 case $l in
205 0) echo "Core dumps disabled" ;;
206 unlimited) echo "Core dumps enabled" ;;
207 *) echo "Core dump limit is $l blocks" ;;
208 esac
209 ;;
210 *)
211 echo >&2 "usage: core [y|n]"
212 return 1
213 ;;
214 esac
215}
216
217## world [NAME]
218##
219## Set current security world to NAME. With no NAME, print the currently
220## selected world.
221world () {
222 local nfast=${NFAST_HOME-/opt/nfast}
223 local kmdata
224 case "$#" in
225 0)
226 echo "${NFAST_KMDATA#$nfast/kmdata-}"
227 ;;
228 *)
229 if [ -d "$1" ]; then
230 kmdata=$1
231 elif [ -d "$nfast/kmdata-$1" ]; then
232 kmdata=$nfast/kmdata-$1
233 else
234 echo >&2 "world: can't find world $1"
235 return 1
236 fi
237 shift
238 case "$#" in
239 0) export NFAST_KMDATA=$kmdata ;;
240 *) "$@" ;;
241 esac
242 ;;
243 esac
244}
245
246## path-add [VAR] DIR
247##
248## Add DIR to the beginning of PATH-like variable VAR (defaults to PATH) if
249## it's not there already.
250path_add () {
251 local pathvar export dir val
252 case $# in
253 1) pathvar=PATH dir=$1 export="export PATH" ;;
254 2) pathvar=$1 dir=$2 export=: ;;
255 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
256 esac
257 eval val=\$$pathvar
258 case ":$val:" in
259 *:"$dir":*) ;;
260 *) val=$dir:$val ;;
261 esac
262 eval $pathvar=\$val
263 eval $export
264}
265
266## path-remove [VAR] DIR
267##
268## Remove DIR from PATH-like variable VAR (defaults to PATH); it's not an
269## error if DIR isn't in VAR.
270path_remove () {
271 local pathvar export dir val
272 case $# in
273 1) pathvar=PATH dir=$1 export="export PATH" ;;
274 2) pathvar=$1 dir=$2 export=: ;;
275 *) echo >&2 "Usage: $0 [VAR] DIR"; return 1 ;;
276 esac
277 eval val=\$$pathvar
278 case ":$val:" in
279 :"$dir":) val= ;;
280 :"$dir":*) val=${val#$dir:} ;;
281 *:"$dir":) val=${val%:$dir} ;;
282 *:"$dir":*) val=${val%%:$dir:*}:${val#*:$dir:} ;;
283 esac
284 eval $pathvar=\$val
285 eval $export
286}
287
288## pathhack [-f] +HACK|-HACK...
289##
290## Each HACK refers to a subdirectory of `~/bin/hacks'. A hack name preceded
291## by `+' adds the directory to the PATH; a `-' removes. Adding a hack
292## that's already on the PATH doesn't do anything unless `-f' is set, in
293## which case it gets moved to the beginning. With no arguments, print the
294## currently installed hacks.
295pathhack () {
296 local p e force arg hack dir
297 p=$PATH
298 if [ $# -eq 0 ]; then
299 while :; do
300 e=${p%%:*}
301 case "$e" in "$HOME/bin/hacks/"*) echo ${e#$HOME/bin/hacks/} ;; esac
302 case "$p" in *:*) p=${p#*:} ;; *) break ;; esac
303 done
304 return
305 fi
306 force=nil
307 while [ $# -gt 0 ]; do
308 arg=$1
309 case "$arg" in
310 -f | --force) force=t; shift; continue ;;
311 --) shift; break ;;
312 [-+]*) ;;
313 *) break; ;;
314 esac
315 hack=${arg#[+-]}
316 dir=$HOME/bin/hacks/$hack
317 if ! [ -d "$dir" ]; then
318 echo "$0: path hack $hack not found"
319 return 1
320 fi
321 case "$arg,$force,:$PATH:" in
322 -*,*,*:"$dir":*) path_remove p "$dir" ;;
323 +*,t,*:"$dir":*) path_remove p "$dir"; path_add p "$dir" ;;
324 +*,nil,*:"$dir":*) ;;
325 +*,*) path_add p "$dir" ;;
326 esac
327 shift
328 done
329 if [ $# -eq 0 ]; then PATH=$p; export PATH
330 else PATH=$p "$@"; fi
331}
332
333###--------------------------------------------------------------------------
334### Finishing touches.
335
4cb8b8da
MW
336## Make sure `$HOME/bin' is on the path.
337path_add "$HOME/bin"
338
6054bdb6
MW
339## Set the temporary directory again. (A setuid or setgid program may have
340## unhelpfully forgotten this for us.)
0a030a39
MW
341case ${TMPDIR+t} in
342 t) ;;
343 *) if __mdw_programp tmpdir; then eval $(tmpdir -b); fi ;;
344esac
6054bdb6 345
74a53e28
MW
346## For `root' use -- some simple molly-guards.
347case $(id -u) in
348 0)
349 alias rm="rm -i" cp="cp -i" mv="mv -i"
350 set -o noclobber
351 ;;
352esac
353
354## Run any local hooks.
355__mdw_source_if_exists "$HOME/.shell-local"
356
357###----- That's all, folks --------------------------------------------------