zshrc: new function uopen
[grml-etc-core.git] / etc / skel / .zshrc
1 # Filename:      .zshrc
2 # Purpose:       config file for zsh
3 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
4 # Bug-Reports:   see http://grml.org/bugs/
5 # License:       This file is licensed under the GPL v2.
6 # Latest change: Wed Aug 06 14:17:12 CEST 2008 [mika]
7 ################################################################################
8
9 # source ~/.zshrc.global {{{
10
11 # see /etc/zsh/zshrc for some general settings
12 # If you don't have write permissions to /etc/zsh/zshrc on your own
13 # copy the file to your $HOME as /.zshrc.global and we source it:
14
15 # Note, that xsource() is defined in the global file, so here,
16 # we will have to do the sourcing manually for once:
17
18 [[ -z "$ZSHRC_GLOBAL_HAS_BEEN_READ" ]]  \
19 && [[ -r "${HOME}/.zshrc.global" ]]     \
20 && source "${HOME}/.zshrc.global"
21 # }}}
22
23 # check whether global file has been read {{{
24 if [[ -z "$ZSHRC_GLOBAL_HAS_BEEN_READ" ]] ; then
25     print 'Warning: global zsh config has not been read.' >&2
26     print '         prepare for possible errors!'         >&2
27     print '' >&2
28     print 'See our refcard for info on how to get the complete configuration:' >&2
29     print '    <http://grml.org/zsh/grml-zsh-refcard.pdf>' >&2
30 fi
31 # }}}
32
33 # autoloading stuff {{{
34 # associate types and extensions (be aware with perl scripts and anwanted behaviour!)
35 #  check_com zsh-mime-setup || { autoload zsh-mime-setup && zsh-mime-setup }
36 #  alias -s pl='perl -S'
37 # }}}
38
39 # completion system {{{
40 # just make sure it is loaded in this file too
41 # TODO: is this *really* needed? compsys should be run in the global zshrc already.
42 check_com compinit || { autoload -U compinit && compinit }
43 # }}}
44
45 # make sure isgrmlsmall is defined {{{
46 check_com isgrmlsmall || function isgrmlsmall () { return 1 }
47 # }}}
48
49 ## variables {{{
50
51 # do you want grmlsmall-specific adjustments?
52 GRMLSMALL_SPECIFIC=1
53
54 # set terminal property (used e.g. by msgid-chooser)
55 export COLORTERM="yes"
56
57 # set default browser
58 if [[ -z "$BROWSER" ]] ; then
59     if [[ -n "$DISPLAY" ]] ; then
60         #v# If X11 is running
61         check_com -c firefox && export BROWSER=firefox
62     else
63         #v# If no X11 is running
64         check_com -c w3m && export BROWSER=w3m
65     fi
66 fi
67
68 #m# v QTDIR \kbd{/usr/share/qt[34]}\quad [for non-root only]
69 [[ -d /usr/share/qt3 ]] && export QTDIR=/usr/share/qt3
70 [[ -d /usr/share/qt4 ]] && export QTDIR=/usr/share/qt4
71
72 # support running 'jikes *.java && jamvm HelloWorld' OOTB:
73 #v# [for non-root only]
74 [[ -f /usr/share/classpath/glibj.zip ]] && export JIKESPATH=/usr/share/classpath/glibj.zip
75 # }}}
76
77 ## set options {{{
78
79 # Allow comments even in interactive shells i. e.
80 # $ uname # This command prints system informations
81 # zsh: bad pattern: #
82 # $ setopt interactivecomments
83 # $ uname # This command prints system informations
84 # Linux
85 #  setopt interactivecomments
86
87 # ctrl-s will no longer freeze the terminal.
88 #  stty erase "^?"
89
90 # }}}
91
92 # {{{ global aliases
93 # These do not have to be at the beginning of the command line.
94 # Avoid typing cd ../../ for going two dirs down and so on
95 # Usage, e.g.: "$ cd ...' or just '$ ...' with 'setopt auto_cd'
96 # Notice: deactivated by 061112 by default, we use another approach
97 # known as "power completion / abbreviation expansion"
98 #  alias -g '...'='../..'
99 #  alias -g '....'='../../..'
100 #  alias -g BG='& exit'
101 #  alias -g C='|wc -l'
102 #  alias -g G='|grep'
103 #  alias -g H='|head'
104 #  alias -g Hl=' --help |& less -r'
105 #  alias -g K='|keep'
106 #  alias -g L='|less'
107 #  alias -g LL='|& less -r'
108 #  alias -g M='|most'
109 #  alias -g N='&>/dev/null'
110 #  alias -g R='| tr A-z N-za-m'
111 #  alias -g SL='| sort | less'
112 #  alias -g S='| sort'
113 #  alias -g T='|tail'
114 #  alias -g V='| vim -'
115 # }}}
116
117 ## aliases {{{
118
119 # Xterm resizing-fu.
120 # Based on http://svn.kitenet.net/trunk/home-full/.zshrc?rev=11710&view=log (by Joey Hess)
121 alias hide='echo -en "\033]50;nil2\007"'
122 alias tiny='echo -en "\033]50;-misc-fixed-medium-r-normal-*-*-80-*-*-c-*-iso8859-15\007"'
123 alias small='echo -en "\033]50;6x10\007"'
124 alias medium='echo -en "\033]50;-misc-fixed-medium-r-normal--13-120-75-75-c-80-iso8859-15\007"'
125 alias default='echo -e "\033]50;-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-15\007"'
126 alias large='echo -en "\033]50;-misc-fixed-medium-r-normal-*-*-150-*-*-c-*-iso8859-15\007"'
127 alias huge='echo -en "\033]50;-misc-fixed-medium-r-normal-*-*-210-*-*-c-*-iso8859-15\007"'
128 alias smartfont='echo -en "\033]50;-artwiz-smoothansi-*-*-*-*-*-*-*-*-*-*-*-*\007"'
129 alias semifont='echo -en "\033]50;-misc-fixed-medium-r-semicondensed-*-*-120-*-*-*-*-iso8859-15\007"'
130 #  if [[ "$TERM" == "xterm" ]] && [[ "$LINES" -ge 50 ]] && [[ "$COLUMNS" -ge 100 ]] && [[ -z "$SSH_CONNECTION" ]] ; then
131 #          large
132 #  fi
133
134 # general
135 #a2# Execute \kbd{du -sch}
136 alias da='du -sch'
137 #a2# Execute \kbd{jobs -l}
138 alias j='jobs -l'
139 #  alias u='translate -i'          # translate
140
141 # compile stuff
142 #a2# Execute \kbd{./configure}
143 alias CO="./configure"
144 #a2# Execute \kbd{./configure --help}
145 alias CH="./configure --help"
146
147 # http://conkeror.mozdev.org/
148 #a2# Run a keyboard driven firefox
149 alias conkeror='firefox -chrome chrome://conkeror/content'
150
151 # arch/tla stuff
152 if check_com -c tla ; then
153     #a2# Execute \kbd{tla what-changed --diffs | less}
154     alias tdi='tla what-changed --diffs | less'
155     #a2# Execute \kbd{tla-buildpackage}
156     alias tbp='tla-buildpackage'
157     #a2# Execute \kbd{tla archive-mirror}
158     alias tmi='tla archive-mirror'
159     #a2# Execute \kbd{tla commit}
160     alias tco='tla commit'
161     #a2# Execute \kbd{tla star-merge}
162     alias tme='tla star-merge'
163 fi
164
165 # listing stuff
166 #a2# Execute \kbd{ls -lSrah}
167 alias dir="ls -lSrah"
168 #a2# Only show dot-directories
169 alias lad='ls -d .*(/)'                # only show dot-directories
170 #a2# Only show dot-files
171 alias lsa='ls -a .*(.)'                # only show dot-files
172 #a2# Only files with setgid/setuid/sticky flag
173 alias lss='ls -l *(s,S,t)'             # only files with setgid/setuid/sticky flag
174 #a2# Only show 1st ten symlinks
175 alias lsl='ls -l *(@[1,10])'           # only symlinks
176 #a2# Display only executables
177 alias lsx='ls -l *(*[1,10])'           # only executables
178 #a2# Display world-{readable,writable,executable} files
179 alias lsw='ls -ld *(R,W,X.^ND/)'       # world-{readable,writable,executable} files
180 #a2# Display the ten biggest files
181 alias lsbig="ls -flh *(.OL[1,10])"     # display the biggest files
182 #a2# Only show directories
183 alias lsd='ls -d *(/)'                 # only show directories
184 #a2# Only show empty directories
185 alias lse='ls -d *(/^F)'               # only show empty directories
186 #a2# Display the ten newest files
187 alias lsnew="ls -rl *(D.om[1,10])"     # display the newest files
188 #a2# Display the ten oldest files
189 alias lsold="ls -rtlh *(D.om[1,10])"   # display the oldest files
190 #a2# Display the ten smallest files
191 alias lssmall="ls -Srl *(.oL[1,10])"   # display the smallest files
192
193 # chmod
194 #a2# Execute \kbd{chmod 600}
195 alias rw-='chmod 600'
196 #a2# Execute \kbd{chmod 700}
197 alias rwx='chmod 700'
198 #m# a2 r-{}- Execute \kbd{chmod 644}
199 alias r--='chmod 644'
200 #a2# Execute \kbd{chmod 755}
201 alias r-x='chmod 755'
202
203 # some useful aliases
204 #a2# Execute \kbd{mkdir -o}
205 alias md='mkdir -p'
206
207 check_com -c ipython && alias ips='ipython -p sh'
208
209 # console stuff
210 #a2# Execute \kbd{mplayer -vo fbdev}
211 alias cmplayer='mplayer -vo fbdev'
212 #a2# Execute \kbd{mplayer -vo fbdev -fs -zoom}
213 alias fbmplayer='mplayer -vo fbdev -fs -zoom'
214 #a2# Execute \kbd{links2 -driver fb}
215 alias fblinks='links2 -driver fb'
216
217 # ignore ~/.ssh/known_hosts entries
218 #  alias insecssh='ssh -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null" -o "PreferredAuthentications=keyboard-interactive"'
219 #a2# ssh with StrictHostKeyChecking=no \\&\quad and UserKnownHostsFile unset
220 alias insecssh='ssh -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null"'
221 alias insecscp='scp -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null"'
222
223 # Use 'g' instead of 'git':
224 check_com g || alias g='git'
225
226 # use colors when browsing man pages, but only if not using LESS_TERMCAP_* from /etc/zsh/zshenv:
227 if [[ -z "$LESS_TERMCAP_md" ]] ; then
228     [[ -d ~/.terminfo/ ]] && alias man='TERMINFO=~/.terminfo/ LESS=C TERM=mostlike PAGER=less man'
229 fi
230
231 # check whether Debian's package management (dpkg) is running
232 if check_com salias ; then
233     #a2# Check whether a dpkg instance is currently running
234     salias check_dpkg_running="dpkg_running"
235 fi
236
237 # work around non utf8 capable software in utf environment via $LANG and luit
238 if check_com isutfenv && check_com luit ; then
239     if check_com -c mrxvt ; then
240         isutfenv && [[ -n "$LANG" ]] && \
241             alias mrxvt="LANG=${LANG/(#b)(*)[.@]*/$match[1].iso885915} luit mrxvt"
242     fi
243
244     if check_com -c aterm ; then
245         isutfenv && [[ -n "$LANG" ]] && \
246             alias aterm="LANG=${LANG/(#b)(*)[.@]*/$match[1].iso885915} luit aterm"
247     fi
248
249     if check_com -c centericq ; then
250         isutfenv && [[ -n "$LANG" ]] && \
251             alias centericq="LANG=${LANG/(#b)(*)[.@]*/$match[1].iso885915} luit centericq"
252     fi
253 fi
254 # }}}
255
256 ## useful functions {{{
257
258 # searching
259 #f4# Search for newspostings from authors
260 agoogle() { ${=BROWSER} "http://groups.google.com/groups?as_uauthors=$*" ; }
261 #f4# Search Debian Bug Tracking System
262 debbug()  {
263     setopt localoptions extendedglob
264     if [[ $# -eq 1 ]]; then
265         case "$1" in
266             ([0-9]##)
267             ${=BROWSER} "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=$1"
268             ;;
269             (*@*)
270             ${=BROWSER} "http://bugs.debian.org/cgi-bin/pkgreport.cgi?submitter=$1"
271             ;;
272             (*)
273             ${=BROWSER} "http://bugs.debian.org/$*"
274             ;;
275         esac
276     else
277         print "$0 needs one argument"
278         return 1
279     fi
280 }
281 #f4# Search Debian Bug Tracking System by BugID in mbox format
282 debbugm() { bts show --mbox $1 } # provide bugnummer as "$1"
283 #f4# Search DMOZ
284 dmoz()    { ${=BROWSER} http://search.dmoz.org/cgi-bin/search\?search=${1// /_} }
285 #f4# Search German   Wiktionary
286 dwicti()  { ${=BROWSER} http://de.wiktionary.org/wiki/${(C)1// /_} }
287 #f4# Search English  Wiktionary
288 ewicti()  { ${=BROWSER} http://en.wiktionary.org/wiki/${(C)1// /_} }
289 #f4# Search Google Groups
290 ggogle()  { ${=BROWSER} "http://groups.google.com/groups?q=$*" }
291 #f4# Search Google
292 google()  { ${=BROWSER} "http://www.google.com/search?&num=100&q=$*" }
293 #f4# Search Google Groups for MsgID
294 mggogle() { ${=BROWSER} "http://groups.google.com/groups?selm=$*" }
295 #f4# Search Netcraft
296 netcraft(){ ${=BROWSER} "http://toolbar.netcraft.com/site_report?url=$1" }
297 #f4# Use German Wikipedia's full text search
298 swiki()   { ${=BROWSER} http://de.wikipedia.org/wiki/Spezial:Search/${(C)1} }
299 #f4# search \kbd{dict.leo.org}
300 oleo()    { ${=BROWSER} "http://dict.leo.org/?search=$*" }
301 #f4# Search German   Wikipedia
302 wikide()  { ${=BROWSER} http://de.wikipedia.org/wiki/"${(C)*}" }
303 #f4# Search English  Wikipedia
304 wikien()  { ${=BROWSER} http://en.wikipedia.org/wiki/"${(C)*}" }
305 #f4# Search official debs
306 wodeb()   { ${=BROWSER} "http://packages.debian.org/search?keywords=$1&searchon=contents&suite=${2:=unstable}&section=all" }
307
308 #m# f4 gex() Exact search via Google
309 check_com google && gex () { google "\"[ $1]\" $*" } # exact search at google
310
311 # misc
312 #f5# Backup \kbd{file {\rm to} file\_timestamp}
313 bk()      { cp -b ${1} ${1}_`date --iso-8601=m` }
314 #f5# Copied diff
315 cdiff()   { diff -crd "$*" | egrep -v "^Only in |^Binary files " }
316 #f5# cd to directoy and list files
317 cl()      { cd $1 && ls -a }        # cd && ls
318 #f5# Cvs add
319 cvsa()    { cvs add $* && cvs com -m 'initial checkin' $* }
320 #f5# Cvs diff
321 cvsd()    { cvs diff -N $* |& $PAGER }
322 #f5# Cvs log
323 cvsl()    { cvs log $* |& $PAGER }
324 #f5# Cvs update
325 cvsq()    { cvs -nq update }
326 #f5# Rcs2log
327 cvsr()    { rcs2log $* | $PAGER }
328 #f5# Cvs status
329 cvss()    { cvs status -v $* }
330 #f5# Disassemble source files using gcc and as
331 disassemble(){ gcc -pipe -S -o - -O -g $* | as -aldh -o /dev/null }
332 #f5# Firefox remote control - open given URL
333 fir()     { firefox -a firefox -remote "openURL($1)" }
334 #f5# Create Directoy and \kbd{cd} to it
335 mcd()     { mkdir -p "$@"; cd "$@" } # mkdir && cd
336 #f5# Unified diff to timestamped outputfile
337 mdiff()   { diff -udrP "$1" "$2" > diff.`date "+%Y-%m-%d"`."$1" }
338 #f5# Memory overview
339 memusage(){ ps aux | awk '{if (NR > 1) print $5; if (NR > 2) print "+"} END { print "p" }' | dc }
340 #f5# Show contents of tar file
341 shtar()   { gunzip -c $1 | tar -tf - -- | $PAGER }
342 #f5# Show contents of tgz file
343 shtgz()   { tar -ztf $1 | $PAGER }
344 #f5# Show contents of zip file
345 shzip()   { unzip -l $1 | $PAGER }
346 #f5# Greps signature from file
347 sig()     { agrep -d '^-- $' "$*" ~/.Signature }
348 #f5# Unified diff
349 udiff()   { diff -urd $* | egrep -v "^Only in |^Binary files " }
350 #f5# (Mis)use \kbd{vim} as \kbd{less}
351 viless()  { vim --cmd 'let no_plugin_maps = 1' -c "so \$VIMRUNTIME/macros/less.vim" "${@:--}" }
352
353 # download video from youtube
354 ytdl() {
355     if ! [[ -n "$2" ]] ; then
356         print "Usage: ydtl http://youtube.com/watch?v=.... outputfile.flv">&2
357         return 1
358     else
359         wget -O${2} "http://youtube.com/get_video?"${${${"$(wget -o/dev/null -O- "${1}" | grep -e watch_fullscreen)"}##*watch_fullscreen\?}%%\&fs=*}
360     fi
361 }
362
363 # Function Usage: uopen $URL/$file
364 #f5# Download a file and display it locally
365 uopen() {
366     if ! [[ -n "$1" ]] ; then
367         print "Usage: uopen \$URL/\$file">&2
368         return 1
369     else
370         FILE=$1
371         MIME=$(curl --head $FILE | grep Content-Type | cut -d ' ' -f 2 | cut -d\; -f 1)
372         MIME=${MIME%$'\r'}
373         curl $FILE | see ${MIME}:-
374     fi
375 }
376
377 # Function Usage: doc packagename
378 #f5# \kbd{cd} to /usr/share/doc/\textit{package}
379 doc() { cd /usr/share/doc/$1 && ls }
380 _doc() { _files -W /usr/share/doc -/ }
381 check_com compdef && compdef _doc doc
382
383 #f5# Make screenshot
384 sshot() {
385     [[ ! -d ~/shots  ]] && mkdir ~/shots
386     #cd ~/shots ; sleep 5 ; import -window root -depth 8 -quality 80 `date "+%Y-%m-%d--%H:%M:%S"`.png
387     cd ~/shots ; sleep 5; import -window root shot_`date --iso-8601=m`.jpg
388 }
389
390 # list images only
391 limg() {
392     local -a images
393     images=( *.{jpg,gif,png}(.N) )
394
395     if [[ $#images -eq 0 ]] ; then
396         print "No image files found"
397     else
398         ls "$@" "$images[@]"
399     fi
400 }
401
402 #f5# Create PDF file from source code
403 makereadable() {
404     output=$1
405     shift
406     a2ps --medium A4dj -E -o $output $*
407     ps2pdf $output
408 }
409
410 # zsh with perl-regex - use it e.g. via:
411 # regcheck '\s\d\.\d{3}\.\d{3} Euro' ' 1.000.000 Euro'
412 #f5# Checks whether a regex matches or not.\\&\quad Example: \kbd{regcheck '.\{3\} EUR' '500 EUR'}
413 regcheck() {
414     zmodload -i zsh/pcre
415     pcre_compile $1 && \
416     pcre_match $2 && echo "regex matches" || echo "regex does not match"
417 }
418
419 #f5# List files which have been modified within the last {\it n} days
420 new() { print -l *(m-$1) }
421
422 #f5# Grep in history
423 greph() { history 0 | grep $1 }
424 # use colors when GNU grep with color-support
425 #a2# Execute \kbd{grep -{}-color=auto}
426 (grep --help 2>/dev/null |grep -- --color) >/dev/null && alias grep='grep --color=auto'
427 #a2# Execute \kbd{grep -i -{}-color=auto}
428 alias GREP='grep -i --color=auto'
429
430 # one blank line between each line
431 if [[ -r ~/.terminfo/m/mostlike ]] ; then
432 #   alias man2='MANPAGER="sed -e G |less" TERMINFO=~/.terminfo TERM=mostlike /usr/bin/man'
433     #f5# Watch manpages in a stretched style
434     man2() { PAGER='dash -c "sed G | /usr/bin/less"' TERM=mostlike /usr/bin/man "$@" ; }
435 fi
436
437 # d():Copyright 2005 Nikolai Weibull <nikolai@bitwi.se>
438 # notice: option AUTO_PUSHD has to be set
439 #f5# Jump between directories
440 d() {
441     emulate -L zsh
442     autoload -U colors
443     local color=$fg_bold[blue]
444     integer i=0
445     dirs -p | while read dir; do
446         local num="${$(printf "%-4d " $i)/ /.}"
447         printf " %s  $color%s$reset_color\n" $num $dir
448         (( i++ ))
449     done
450     integer dir=-1
451     read -r 'dir?Jump to directory: ' || return
452     (( dir == -1 )) && return
453     if (( dir < 0 || dir >= i )); then
454         echo d: no such directory stack entry: $dir
455         return 1
456     fi
457     cd ~$dir
458 }
459
460 # usage example: 'lcheck strcpy'
461 #f5# Find out which libs define a symbol
462 lcheck() {
463     if [[ -n "$1" ]] ; then
464         nm -go /usr/lib/lib*.a 2>/dev/null | grep ":[[:xdigit:]]\{8\} . .*$1"
465     else
466         echo "Usage: lcheck <function>" >&2
467     fi
468 }
469
470 #f5# Clean up directory - remove well known tempfiles
471 purge() {
472     FILES=(*~(N) .*~(N) \#*\#(N) *.o(N) a.out(N) *.core(N) *.cmo(N) *.cmi(N) .*.swp(N))
473     NBFILES=${#FILES}
474     if [[ $NBFILES > 0 ]] ; then
475         print $FILES
476         local ans
477         echo -n "Remove these files? [y/n] "
478         read -q ans
479         if [[ $ans == "y" ]] ; then
480             rm ${FILES}
481             echo ">> $PWD purged, $NBFILES files removed"
482         else
483             echo "Ok. .. than not.."
484         fi
485     fi
486 }
487
488 # Translate DE<=>EN
489 # 'translate' looks up fot a word in a file with language-to-language
490 # translations (field separator should be " : "). A typical wordlist looks
491 # like at follows:
492 #  | english-word : german-transmission
493 # It's also only possible to translate english to german but not reciprocal.
494 # Use the following oneliner to turn back the sort order:
495 #  $ awk -F ':' '{ print $2" : "$1" "$3 }' \
496 #    /usr/local/lib/words/en-de.ISO-8859-1.vok > ~/.translate/de-en.ISO-8859-1.vok
497 #f5# Translates a word
498 trans() {
499     case "$1" in
500         -[dD]*)
501             translate -l de-en $2
502             ;;
503         -[eE]*)
504             translate -l en-de $2
505             ;;
506         *)
507             echo "Usage: $0 { -D | -E }"
508             echo "         -D == German to English"
509             echo "         -E == English to German"
510     esac
511 }
512
513 # Some quick Perl-hacks aka /useful/ oneliner
514 #  bew() { perl -le 'print unpack "B*","'$1'"' }
515 #  web() { perl -le 'print pack "B*","'$1'"' }
516 #  hew() { perl -le 'print unpack "H*","'$1'"' }
517 #  weh() { perl -le 'print pack "H*","'$1'"' }
518 #  pversion()    { perl -M$1 -le "print $1->VERSION" } # i. e."pversion LWP -> 5.79"
519 #  getlinks ()   { perl -ne 'while ( m/"((www|ftp|http):\/\/.*?)"/gc ) { print $1, "\n"; }' $* }
520 #  gethrefs ()   { perl -ne 'while ( m/href="([^"]*)"/gc ) { print $1, "\n"; }' $* }
521 #  getanames ()  { perl -ne 'while ( m/a name="([^"]*)"/gc ) { print $1, "\n"; }' $* }
522 #  getforms ()   { perl -ne 'while ( m:(\</?(input|form|select|option).*?\>):gic ) { print $1, "\n"; }' $* }
523 #  getstrings () { perl -ne 'while ( m/"(.*?)"/gc ) { print $1, "\n"; }' $*}
524 #  getanchors () { perl -ne 'while ( m/«([^«»\n]+)»/gc ) { print $1, "\n"; }' $* }
525 #  showINC ()    { perl -e 'for (@INC) { printf "%d %s\n", $i++, $_ }' }
526 #  vimpm ()      { vim `perldoc -l $1 | sed -e 's/pod$/pm/'` }
527 #  vimhelp ()    { vim -c "help $1" -c on -c "au! VimEnter *" }
528
529 #f5# List all occurrences of programm in current PATH
530 plap() {
531     if [[ $# = 0 ]] ; then
532         echo "Usage:    $0 program"
533         echo "Example:  $0 zsh"
534         echo "Lists all occurrences of program in the current PATH."
535     else
536         ls -l ${^path}/*$1*(*N)
537     fi
538 }
539
540 # Found in the mailinglistarchive from Zsh (IIRC ~1996)
541 #f5# Select items for specific command(s) from history
542 selhist() {
543     emulate -L zsh
544     local TAB=$'\t';
545     (( $# < 1 )) && {
546         echo "Usage: $0 command"
547         return 1
548     };
549     cmd=(${(f)"$(grep -w $1 $HISTFILE | sort | uniq | pr -tn)"})
550     print -l $cmd | less -F
551     echo -n "enter number of desired command [1 - $(( ${#cmd[@]} - 1 ))]: "
552     local answer
553     read answer
554     print -z "${cmd[$answer]#*$TAB}"
555 }
556
557 # Use vim to convert plaintext to HTML
558 #f5# Transform files to html with highlighting
559 2html() { vim -u NONE -n -c ':syntax on' -c ':so $VIMRUNTIME/syntax/2html.vim' -c ':wqa' $1 &>/dev/null }
560
561 # Usage: simple-extract <file>
562 #f5# Smart archive extractor
563 simple-extract () {
564     if [[ -f $1 ]] ; then
565         case $1 in
566             *.tar.bz2)  bzip2 -v -d $1      ;;
567             *.tar.gz)   tar -xvzf $1        ;;
568             *.rar)      unrar $1            ;;
569             *.deb)      ar -x $1            ;;
570             *.bz2)      bzip2 -d $1         ;;
571             *.lzh)      lha x $1            ;;
572             *.gz)       gunzip -d $1        ;;
573             *.tar)      tar -xvf $1         ;;
574             *.tgz)      gunzip -d $1        ;;
575             *.tbz2)     tar -jxvf $1        ;;
576             *.zip)      unzip $1            ;;
577             *.Z)        uncompress $1       ;;
578             *)          echo "'$1' Error. Please go away" ;;
579         esac
580     else
581         echo "'$1' is not a valid file"
582     fi
583 }
584
585 # Usage: smartcompress <file> (<type>)
586 #f5# Smart archive creator
587 smartcompress() {
588     if [[ -n $2 ]] ; then
589         case $2 in
590             tgz | tar.gz)   tar -zcvf$1.$2 $1 ;;
591             tbz2 | tar.bz2) tar -jcvf$1.$2 $1 ;;
592             tar.Z)          tar -Zcvf$1.$2 $1 ;;
593             tar)            tar -cvf$1.$2  $1 ;;
594             gz | gzip)      gzip           $1 ;;
595             bz2 | bzip2)    bzip2          $1 ;;
596             *)
597                 echo "Error: $2 is not a valid compression type"
598                 ;;
599         esac
600     else
601         smartcompress $1 tar.gz
602     fi
603 }
604
605 # Usage: show-archive <archive>
606 #f5# List an archive's content
607 show-archive() {
608     if [[ -f $1 ]] ; then
609         case $1 in
610             *.tar.gz)      gunzip -c $1 | tar -tf - -- ;;
611             *.tar)         tar -tf $1 ;;
612             *.tgz)         tar -ztf $1 ;;
613             *.zip)         unzip -l $1 ;;
614             *.bz2)         bzless $1 ;;
615             *.deb)         dpkg-deb --fsys-tarfile $1 | tar -tf - -- ;;
616             *)             echo "'$1' Error. Please go away" ;;
617         esac
618     else
619         echo "'$1' is not a valid archive"
620     fi
621 }
622
623 # It's shameless stolen from <http://www.vim.org/tips/tip.php?tip_id=167>
624 #f5# Use \kbd{vim} as your manpage reader
625 vman() { man $* | col -b | view -c 'set ft=man nomod nolist' - }
626
627 # function readme() { $PAGER -- (#ia3)readme* }
628 #f5# View all README-like files in current directory in pager
629 readme() {
630     local files
631     files=(./(#i)*(read*me|lue*m(in|)ut)*(ND))
632     if (($#files)) ; then
633         $PAGER $files
634     else
635         print 'No README files.'
636     fi
637 }
638
639 # function ansi-colors()
640 #f5# Display ANSI colors
641 ansi-colors() {
642     typeset esc="\033[" line1 line2
643     echo " _ _ _40 _ _ _41_ _ _ _42 _ _ 43_ _ _ 44_ _ _45 _ _ _ 46_ _ _ 47_ _ _ 49_ _"
644     for fore in 30 31 32 33 34 35 36 37; do
645         line1="$fore "
646         line2="   "
647         for back in 40 41 42 43 44 45 46 47 49; do
648             line1="${line1}${esc}${back};${fore}m Normal ${esc}0m"
649             line2="${line2}${esc}${back};${fore};1m Bold   ${esc}0m"
650         done
651         echo -e "$line1\n$line2"
652     done
653 }
654
655 # suidfind() { ls -latg $path | grep '^...s' }
656 #f5# Find all files in \$PATH with setuid bit set
657 suidfind() { ls -latg $path/*(sN) }
658
659 # See above but this is /better/ ... anywise ..
660 findsuid() {
661     print 'Output will be written to ~/suid_* ...'
662     $SUDO find / -type f \( -perm -4000 -o -perm -2000 \) -ls > ~/suid_suidfiles.`date "+%Y-%m-%d"`.out 2>&1
663     $SUDO find / -type d \( -perm -4000 -o -perm -2000 \) -ls > ~/suid_suiddirs.`date "+%Y-%m-%d"`.out 2>&1
664     $SUDO find / -type f \( -perm -2 -o -perm -20 \) -ls > ~/suid_writefiles.`date "+%Y-%m-%d"`.out 2>&1
665     $SUDO find / -type d \( -perm -2 -o -perm -20 \) -ls > ~/suid_writedirs.`date "+%Y-%m-%d"`.out 2>&1
666     print 'Finished'
667 }
668
669 #f5# Reload given functions
670 refunc() {
671     for func in $argv ; do
672         unfunction $func
673         autoload $func
674     done
675 }
676
677 # a small check to see which DIR is located on which server/partition.
678 # stolen and modified from Sven's zshrc.forall
679 #f5# Report diskusage of a directory
680 dirspace() {
681     if [[ -n "$1" ]] ; then
682         for dir in $* ; do
683             if [[ -d "$dir" ]] ; then
684                 ( cd $dir; echo "-<$dir>"; du -shx .; echo);
685             else
686                 echo "warning: $dir does not exist" >&2
687             fi
688         done
689     else
690         for dir in $path; do
691             if [[ -d "$dir" ]] ; then
692                 ( cd $dir; echo "-<$dir>"; du -shx .; echo);
693             else
694                 echo "warning: $dir does not exist" >&2
695             fi
696         done
697     fi
698 }
699
700 # % slow_print `cat /etc/passwd`
701 #f5# Slowly print out parameters
702 slow_print() {
703     for argument in "${@}" ; do
704         for ((i = 1; i <= ${#1} ;i++)) ; do
705             print -n "${argument[i]}"
706             sleep 0.08
707         done
708         print -n " "
709     done
710     print ""
711 }
712
713 #f5# Show some status info
714 status() {
715     print ""
716     print "Date..: "$(date "+%Y-%m-%d %H:%M:%S")""
717     print "Shell.: Zsh $ZSH_VERSION (PID = $$, $SHLVL nests)"
718     print "Term..: $TTY ($TERM), $BAUD bauds, $COLUMNS x $LINES cars"
719     print "Login.: $LOGNAME (UID = $EUID) on $HOST"
720     print "System: $(cat /etc/[A-Za-z]*[_-][rv]e[lr]*)"
721     print "Uptime:$(uptime)"
722     print ""
723 }
724
725 # Rip an audio CD
726 #f5# Rip an audio CD
727 audiorip() {
728     mkdir -p ~/ripps
729     cd ~/ripps
730     cdrdao read-cd --device $DEVICE --driver generic-mmc audiocd.toc
731     cdrdao read-cddb --device $DEVICE --driver generic-mmc audiocd.toc
732     echo " * Would you like to burn the cd now? (yes/no)"
733     read input
734     if [[ "$input" = "yes" ]] ; then
735         echo " ! Burning Audio CD"
736         audioburn
737         echo " * done."
738     else
739         echo " ! Invalid response."
740     fi
741 }
742
743 # and burn it
744 #f5# Burn an audio CD (in combination with audiorip)
745 audioburn() {
746     cd ~/ripps
747     cdrdao write --device $DEVICE --driver generic-mmc audiocd.toc
748     echo " * Should I remove the temporary files? (yes/no)"
749     read input
750     if [[ "$input" = "yes" ]] ; then
751         echo " ! Removing Temporary Files."
752         cd ~
753         rm -rf ~/ripps
754         echo " * done."
755     else
756         echo " ! Invalid response."
757     fi
758 }
759
760 #f5# Make an audio CD from all mp3 files
761 mkaudiocd() {
762     # TODO: do the renaming more zshish, possibly with zmv()
763     cd ~/ripps
764     for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done
765     for i in *.mp3; do mv "$i" `echo $i | tr ' ' '_'`; done
766     for i in *.mp3; do mpg123 -w `basename $i .mp3`.wav $i; done
767     normalize -m *.wav
768     for i in *.wav; do sox $i.wav -r 44100 $i.wav resample; done
769 }
770
771 #f5# Create an ISO image. You are prompted for\\&\quad volume name, filename and directory
772 mkiso() {
773     echo " * Volume name "
774     read volume
775     echo " * ISO Name (ie. tmp.iso)"
776     read iso
777     echo " * Directory or File"
778     read files
779     mkisofs -o ~/$iso -A $volume -allow-multidot -J -R -iso-level 3 -V $volume -R $files
780 }
781
782 #f5# Simple thumbnails generator
783 genthumbs() {
784     rm -rf thumb-* index.html
785     echo "
786 <html>
787   <head>
788     <title>Images</title>
789   </head>
790   <body>" > index.html
791     for f in *.(gif|jpeg|jpg|png) ; do
792         convert -size 100x200 "$f" -resize 100x200 thumb-"$f"
793         echo "    <a href=\"$f\"><img src=\"thumb-$f\"></a>" >> index.html
794     done
795     echo "
796   </body>
797 </html>" >> index.html
798 }
799
800 #f5# Set all ulimit parameters to \kbd{unlimited}
801 allulimit() {
802     ulimit -c unlimited
803     ulimit -d unlimited
804     ulimit -f unlimited
805     ulimit -l unlimited
806     ulimit -n unlimited
807     ulimit -s unlimited
808     ulimit -t unlimited
809 }
810
811 # ogg2mp3 with bitrate of 192
812 ogg2mp3_192() {
813     oggdec -o - ${1} | lame -b 192 - ${1:r}.mp3
814 }
815
816 #f5# RFC 2396 URL encoding in Z-Shell
817 urlencode() {
818     setopt localoptions extendedglob
819     input=( ${(s::)1} )
820     print ${(j::)input/(#b)([^A-Za-z0-9_.!~*\'\(\)-])/%$(([##16]#match))}
821 }
822
823 #f5# Install x-lite (VoIP software)
824 getxlite() {
825     setopt local_options
826     setopt errreturn
827     [[ -d ~/tmp ]] || mkdir ~/tmp
828     cd ~/tmp
829
830     echo "Downloading http://www.counterpath.com/download/X-Lite_Install.tar.gz and storing it in ~/tmp:"
831     if wget http://www.counterpath.com/download/X-Lite_Install.tar.gz ; then
832         unp X-Lite_Install.tar.gz && echo done || echo failed
833     else
834         echo "Error while downloading." ; return 1
835     fi
836
837     if [[ -x xten-xlite/xtensoftphone ]] ; then
838         echo "Execute xten-xlite/xtensoftphone to start xlite."
839     fi
840 }
841
842 #f5# Install skype
843 getskype() {
844     setopt local_options
845     setopt errreturn
846     echo "Downloading debian package of skype."
847     echo "Notice: If you want to use a more recent skype version run 'getskypebeta'."
848     wget http://www.skype.com/go/getskype-linux-deb
849     $SUDO dpkg -i skype*.deb && echo "skype installed."
850 }
851
852 #f5# Install beta-version of skype
853 getskypebeta() {
854     setopt local_options
855     setopt errreturn
856     echo "Downloading debian package of skype (beta version)."
857     wget http://www.skype.com/go/getskype-linux-beta-deb
858     $SUDO dpkg -i skype-beta*.deb && echo "skype installed."
859 }
860
861 #f5# Install gizmo (VoIP software)
862 getgizmo() {
863     setopt local_options
864     setopt errreturn
865     echo "gconf2-common and libgconf2-4 have to be available. Installing therefor."
866     $SUDO apt-get update
867     $SUDO apt-get install gconf2-common libgconf2-4
868     wget $(lynx --dump http://www.gizmoproject.com/download-linux.html | awk '/\.deb/ {print $2" "}' | tr -d '\n')
869     $SUDO dpkg -i libsipphoneapi*.deb bonjour_*.deb gizmo-*.deb && echo "gizmo installed."
870 }
871
872 #f5# Get and run AIR (Automated Image and Restore)
873 getair() {
874     setopt local_options
875     setopt errreturn
876     [[ -w . ]] || { echo 'Error: you do not have write permissions in this directory. Exiting.' ; return 1 }
877     local VER='1.2.8'
878     wget http://puzzle.dl.sourceforge.net/sourceforge/air-imager/air-$VER.tar.gz
879     tar zxf air-$VER.tar.gz
880     cd air-$VER
881     INTERACTIVE=no $SUDO ./install-air-1.2.8
882     [[ -x /usr/local/bin/air ]] && [[ -n "$DISPLAY" ]] && $SUDO air
883 }
884
885 #f5# Get specific git commitdiff
886 git-get-diff() {
887     if [[ -z $GITTREE ]] ; then
888         GITTREE='linux/kernel/git/torvalds/linux-2.6.git'
889     fi
890     if ! [[ -z $1 ]] ; then
891         ${=BROWSER} "http://kernel.org/git/?p=$GITTREE;a=commitdiff;h=$1"
892     else
893         echo "Usage: git-get-diff <commit>"
894     fi
895 }
896
897 #f5# Get specific git commit
898 git-get-commit() {
899     if [[ -z $GITTREE ]] ; then
900         GITTREE='linux/kernel/git/torvalds/linux-2.6.git'
901     fi
902     if ! [[ -z $1 ]] ; then
903         ${=BROWSER} "http://kernel.org/git/?p=$GITTREE;a=commit;h=$1"
904     else
905         echo "Usage: git-get-commit <commit>"
906     fi
907 }
908
909 #f5# Get specific git diff
910 git-get-plaindiff () {
911     if [[ -z $GITTREE ]] ; then
912        GITTREE='linux/kernel/git/torvalds/linux-2.6.git'
913     fi
914     if [[ -z $1 ]] ; then
915        echo 'Usage: git-get-plaindiff '
916     else
917        echo -n "Downloading $1.diff ... "
918        # avoid "generating ..." stuff from kernel.org server:
919        wget --quiet "http://kernel.org/git/?p=$GITTREE;a=commitdiff_plain;h=$1" -O /dev/null
920        wget --quiet "http://kernel.org/git/?p=$GITTREE;a=commitdiff_plain;h=$1" -O $1.diff \
921             && echo done || echo failed
922     fi
923 }
924
925
926 # http://strcat.de/blog/index.php?/archives/335-Software-sauber-deinstallieren...html
927 #f5# Log 'make install' output
928 mmake() {
929     [[ ! -d ~/.errorlogs ]] && mkdir ~/.errorlogs
930     make -n install > ~/.errorlogs/${PWD##*/}-makelog
931 }
932
933 #f5# Indent source code
934 smart-indent() {
935     indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs $*
936 }
937
938 # highlight important stuff in diff output, usage example: hg diff | hidiff
939 #m# a2 hidiff \kbd{histring} oneliner for diffs
940 check_com -c histring && \
941     alias hidiff="histring -fE '^Comparing files .*|^diff .*' | histring -c yellow -fE '^\-.*' | histring -c green -fE '^\+.*'"
942
943 # rename pictures based on information found in exif headers
944 #f5# Rename pictures based on information found in exif headers
945 exirename() {
946     if [[ $# -lt 1 ]] ; then
947         echo 'Usage: jpgrename $FILES' >& 2
948         return 1
949     else
950         echo -n 'Checking for jhead with version newer than 1.9: '
951         jhead_version=`jhead -h | grep 'used by most Digital Cameras.  v.*' | awk '{print $6}' | tr -d v`
952         if [[ $jhead_version > '1.9' ]]; then
953             echo 'success - now running jhead.'
954             jhead -n%Y-%m-%d_%Hh%M_%f $*
955         else
956             echo 'failed - exiting.'
957         fi
958     fi
959 }
960
961 # open file in vim and jump to line
962 # http://www.downgra.de/archives/2007/05/08/T19_21_11/
963 j2v() {
964     local -a params
965     params=(${*//(#m):[0-9]*:/\\n+${MATCH//:/}}) # replace ':23:' to '\n+23'
966     params=(${(s|\n|)${(j|\n|)params}}) # join array using '\n', then split on all '\n'
967     vim ${params}
968 }
969
970 # get_ic() - queries imap servers for capabilities; real simple. no imaps
971 ic_get() {
972     local port
973     if [[ ! -z $1 ]] ; then
974         port=${2:-143}
975         print "querying imap server on $1:${port}...\n";
976         print "a1 capability\na2 logout\n" | nc $1 ${port}
977     else
978         print "usage:\n  $0 <imap-server> [port]"
979     fi
980 }
981
982 # creates a Maildir/ with its {new,cur,tmp} subdirs
983 mkmaildir() {
984     local root subdir
985     root=${MAILDIR_ROOT:-${HOME}/Mail}
986     if [[ -z ${1} ]] ; then print "Usage:\n $0 <dirname>" ; return 1 ; fi
987     subdir=${1}
988     mkdir -p ${root}/${subdir}/{cur,new,tmp}
989 }
990
991 # xtrename() rename xterm from within GNU-screen
992 xtrename() {
993     if [[ -z ${DISPLAY} ]] ; then
994         printf 'xtrename only makes sense in X11.\n'
995         return 1
996     fi
997     if [[ -z ${1} ]] ; then
998         printf 'usage: xtrename() "title for xterm"\n'
999         printf '  renames the title of xterm from _within_ screen.\n'
1000         printf '  Also works without screen.\n'
1001         return 0
1002     fi
1003     print -n "\eP\e]0;${1}\C-G\e\\"
1004     return 0
1005 }
1006
1007 # hl() highlighted less
1008 # http://ft.bewatermyfriend.org/comp/data/zsh/zfunct.html
1009 if check_com -c highlight ; then
1010     function hl() {
1011         local theme lang
1012         theme=${HL_THEME:-""}
1013         case ${1} in
1014             (-l|--list)
1015                 ( printf 'available languages (syntax parameter):\n\n' ;
1016                     highlight --list-langs ; ) | less -SMr
1017                 ;;
1018             (-t|--themes)
1019                 ( printf 'available themes (style parameter):\n\n' ;
1020                     highlight --list-themes ; ) | less -SMr
1021                 ;;
1022             (-h|--help)
1023                 printf 'usage: hl <syntax[:theme]> <file>\n'
1024                 printf '    available options: --list (-l), --themes (-t), --help (-h)\n\n'
1025                 printf '  Example: hl c main.c\n'
1026                 ;;
1027             (*)
1028                 if [[ -z ${2} ]] || (( ${#argv} > 2 )) ; then
1029                     printf 'usage: hl <syntax[:theme]> <file>\n'
1030                     printf '    available options: --list (-l), --themes (-t), --help (-h)\n'
1031                     (( ${#argv} > 2 )) && printf '  Too many arguments.\n'
1032                     return 1
1033                 fi
1034                 lang=${1%:*}
1035                 [[ ${1} == *:* ]] && [[ -n ${1#*:} ]] && theme=${1#*:}
1036                 if [[ -n ${theme} ]] ; then
1037                     highlight --xterm256 --syntax ${lang} --style ${theme} ${2} | less -SMr
1038                 else
1039                     highlight --ansi --syntax ${lang} ${2} | less -SMr
1040                 fi
1041                 ;;
1042         esac
1043         return 0
1044     }
1045     # ... and a proper completion for hl()
1046     # needs 'highlight' as well, so it fits fine in here.
1047     function _hl_genarg()  {
1048         local expl
1049         if [[ -prefix 1 *: ]] ; then
1050             local themes
1051             themes=(${${${(f)"$(LC_ALL=C highlight --list-themes)"}/ #/}:#*(Installed|Use name)*})
1052             compset -P 1 '*:'
1053             _wanted -C list themes expl theme compadd ${themes}
1054         else
1055             local langs
1056             langs=(${${${(f)"$(LC_ALL=C highlight --list-langs)"}/ #/}:#*(Installed|Use name)*})
1057             _wanted -C list languages expl languages compadd -S ':' -q ${langs}
1058         fi
1059     }
1060     function _hl_complete() {
1061         _arguments -s '1: :_hl_genarg' '2:files:_path_files'
1062     }
1063     compdef _hl_complete hl
1064 fi
1065
1066 # create small urls via tinyurl.com using wget, grep and sed
1067 zurl() {
1068     [[ -z ${1} ]] && print "please give an url to shrink." && return 1
1069     local url=${1}
1070     local tiny="http://tinyurl.com/create.php?url="
1071     #print "${tiny}${url}" ; return
1072     wget  -O-             \
1073           -o/dev/null     \
1074           "${tiny}${url}" \
1075         | grep -Eio 'value="(http://tinyurl.com/.*)"' \
1076         | sed 's/value=//;s/"//g'
1077 }
1078
1079 #f2# Find history events by search pattern and list them by date.
1080 whatwhen()  {
1081 # {{{
1082     local usage help ident format_l format_s first_char remain first last
1083     usage='USAGE: whatwhen [options] <searchstring> <search range>'
1084     help='Use' \`'whatwhen -h'\'' for further explanations.'
1085     ident=${(l,${#${:-Usage: }},, ,)}
1086     format_l="${ident}%s\t\t\t%s\n"
1087     format_s="${format_l//(\\t)##/\\t}"
1088     # Make the first char of the word to search for case
1089     # insensitive; e.g. [aA]
1090     first_char=[${(L)1[1]}${(U)1[1]}]
1091     remain=${1[2,-1]}
1092     # Default search range is `-100'.
1093     first=${2:-\-100}
1094     # Optional, just used for `<first> <last>' given.
1095     last=$3
1096     case $1 in
1097         ("")
1098             printf '%s\n\n' 'ERROR: No search string specified. Aborting.'
1099             printf '%s\n%s\n\n' ${usage} ${help} && return 1
1100         ;;
1101         (-h)
1102             printf '%s\n\n' ${usage}
1103             print 'OPTIONS:'
1104             printf $format_l '-h' 'show help text'
1105             print '\f'
1106             print 'SEARCH RANGE:'
1107             printf $format_l "'0'" 'the whole history,'
1108             printf $format_l '-<n>' 'offset to the current history number; (default: -100)'
1109             printf $format_s '<[-]first> [<last>]' 'just searching within a give range'
1110             printf '\n%s\n' 'EXAMPLES:'
1111             printf ${format_l/(\\t)/} 'whatwhen grml' '# Range is set to -100 by default.'
1112             printf $format_l 'whatwhen zsh -250'
1113             printf $format_l 'whatwhen foo 1 99'
1114         ;;
1115         (\?)
1116             printf '%s\n%s\n\n' ${usage} ${help} && return 1
1117         ;;
1118         (*)
1119             # -l list results on stout rather than invoking $EDITOR.
1120             # -i Print dates as in YYYY-MM-DD.
1121             # -m Search for a - quoted - pattern within the history.
1122             fc -li -m "*${first_char}${remain}*" $first $last
1123         ;;
1124     esac
1125 # }}}
1126 }
1127
1128 # change fluxbox keys from 'Alt-#' to 'Alt-F#' and vice versa
1129 fluxkey-change() {
1130     [[ -n "$FLUXKEYS" ]] || local FLUXKEYS="$HOME/.fluxbox/keys"
1131     if ! [[ -r "$FLUXKEYS" ]] ; then
1132         echo "Sorry, \$FLUXKEYS file $FLUXKEYS could not be read - nothing to be done."
1133         return 1
1134     else
1135         if grep -q 'Mod1 F[0-9] :Workspace [0-9]' $FLUXKEYS ; then
1136             echo -n 'Switching to Alt-# mode in ~/.fluxbox/keys: '
1137             sed -i -e 's|^\(Mod[0-9]\+[: space :]\+\)F\([0-9]\+[: space :]\+:Workspace.*\)|\1\2|' $FLUXKEYS && echo done || echo failed
1138         elif grep -q 'Mod1 [0-9] :Workspace [0-9]' $FLUXKEYS ; then
1139             echo -n 'Switching to Alt-F# mode in ~/.fluxbox/keys: '
1140             sed -i -e 's|^\(Mod[0-9]\+[: space :]\+\)\([0-9]\+[: space :]\+:Workspace.*\)|\1F\2|' $FLUXKEYS && echo done || echo failed
1141         else
1142             echo 'Sorry, do not know what to do.'
1143             return 1
1144         fi
1145     fi
1146 }
1147
1148 # retrieve weather information on the console
1149 # Usage example: 'weather LOWG'
1150 weather() {
1151     [[ -n "$1" ]] || {
1152         print 'Usage: weather <station_id>' >&2
1153         print 'List of stations: http://en.wikipedia.org/wiki/List_of_airports_by_ICAO_code'>&2
1154         return 1
1155     }
1156
1157     local PLACE="${1:u}"
1158     local FILE="$HOME/.weather/$PLACE"
1159     local LOG="$HOME/.weather/log"
1160
1161     [[ -d $HOME/.weather ]] || {
1162         print -n "Creating $HOME/.weather: "
1163         mkdir $HOME/.weather
1164         print 'done'
1165     }
1166
1167     print "Retrieving information for ${PLACE}:"
1168     print
1169     wget -T 10 --no-verbose --output-file=$LOG --output-document=$FILE --timestamping http://weather.noaa.gov/pub/data/observations/metar/decoded/$PLACE.TXT
1170
1171     if [[ $? -eq 0 ]] ; then
1172         if [[ -n "$VERBOSE" ]] ; then
1173             cat $FILE
1174         else
1175             DATE=$(grep 'UTC' $FILE | sed 's#.* /##')
1176             TEMPERATURE=$(awk '/Temperature/ { print $4" degree Celcius / " $2" degree Fahrenheit" }' $FILE| tr -d '(')
1177             echo "date: $DATE"
1178             echo "temp:  $TEMPERATURE"
1179         fi
1180     else
1181         print "There was an error retrieving the weather information for $PLACE" >&2
1182         cat $LOG
1183         return 1
1184     fi
1185 }
1186 # }}}
1187
1188 # mercurial related stuff {{{
1189 if check_com -c hg ; then
1190     # gnu like diff for mercurial
1191     # http://www.selenic.com/mercurial/wiki/index.cgi/TipsAndTricks
1192     #f5# GNU like diff for mercurial
1193     hgdi() {
1194         for i in $(hg status -marn "$@") ; diff -ubwd <(hg cat "$i") "$i"
1195     }
1196
1197     # build debian package
1198     #a2# Alias for \kbd{hg-buildpackage}
1199     alias hbp='hg-buildpackage'
1200
1201     # execute commands on the versioned patch-queue from the current repos
1202     alias mq='hg -R $(readlink -f $(hg root)/.hg/patches)'
1203
1204     # diffstat for specific version of a mercurial repository
1205     #   hgstat      => display diffstat between last revision and tip
1206     #   hgstat 1234 => display diffstat between revision 1234 and tip
1207     #f5# Diffstat for specific version of a mercurial repos
1208     hgstat() {
1209         [[ -n "$1" ]] && hg diff -r $1 -r tip | diffstat || hg export tip | diffstat
1210     }
1211
1212     #f5# Get current mercurial tip via hg itself
1213     gethgclone() {
1214         setopt local_options
1215         setopt errreturn
1216         if [[ -f mercurial-tree/.hg ]] ; then
1217             cd mercurial-tree
1218             echo "Running hg pull for retreiving latest version..."
1219             hg pull
1220             echo "Finished update. Building mercurial"
1221             make local
1222             echo "Setting \$PATH to $PWD:\$PATH..."
1223             export PATH="$PWD:$PATH"
1224         else
1225             echo "Downloading mercurial via hg"
1226             hg clone http://selenic.com/repo/hg mercurial-tree
1227             cd mercurial-tree
1228             echo "Building mercurial"
1229             make local
1230             echo "Setting \$PATH to $PWD:\$PATH..."
1231             export PATH="$PWD:$PATH"
1232             echo "make sure you set it permanent via ~/.zshrc if you plan to use it permanently."
1233             # echo "Setting \$PYTHONPATH to PYTHONPATH=\${HOME}/lib/python,"
1234             # export PYTHONPATH=${HOME}/lib/python
1235         fi
1236     }
1237
1238 fi # end of check whether we have the 'hg'-executable
1239
1240 # get current mercurial snapshot
1241 #f5# Get current mercurial snapshot
1242 gethgsnap() {
1243     setopt local_options
1244     setopt errreturn
1245     if [[ -f mercurial-snapshot.tar.gz ]] ; then
1246          echo "mercurial-snapshot.tar.gz exists already, skipping download."
1247     else
1248         echo "Downloading mercurial snapshot"
1249         wget http://www.selenic.com/mercurial/mercurial-snapshot.tar.gz
1250     fi
1251     echo "Unpacking mercurial-snapshot.tar.gz"
1252     tar zxf mercurial-snapshot.tar.gz
1253     cd mercurial-snapshot/
1254     echo "Installing required build-dependencies"
1255     $SUDO apt-get update
1256     $SUDO apt-get install python2.4-dev
1257     echo "Building mercurial"
1258     make local
1259     echo "Setting \$PATH to $PWD:\$PATH..."
1260     export PATH="$PWD:$PATH"
1261     echo "make sure you set it permanent via ~/.zshrc if you plan to use it permanently."
1262 }
1263 # }}}
1264
1265 # some useful commands often hard to remember - let's grep for them {{{
1266
1267 # Work around ion/xterm resize bug.
1268 #if [[ "$SHLVL" -eq 1 ]]; then
1269 #       if check_com -c resize ; then
1270 #               eval `resize </dev/null`
1271 #       fi
1272 #fi
1273
1274 # enable jackd:
1275 #  /usr/bin/jackd -dalsa -dhw:0 -r48000 -p1024 -n2
1276 # now play audio file:
1277 #  alsaplayer -o jack foobar.mp3
1278
1279 # send files via netcat
1280 # on sending side:
1281 #  send() {j=$*; tar cpz ${j/%${!#}/}|nc -w 1 ${!#} 51330;}
1282 #  send dir* $HOST
1283 #  alias receive='nc -vlp 51330 | tar xzvp'
1284
1285 # debian stuff:
1286 # dh_make -e foo@localhost -f $1
1287 # dpkg-buildpackage -rfakeroot
1288 # lintian *.deb
1289 # dpkg-scanpackages ./ /dev/null | gzip > Packages.gz
1290 # dpkg-scansources . | gzip > Sources.gz
1291 # grep-dctrl --field Maintainer $* /var/lib/apt/lists/*
1292
1293 # other stuff:
1294 # convert -geometry 200x200 -interlace LINE -verbose
1295 # ldapsearch -x -b "OU=Bedienstete,O=tug" -h ldap.tugraz.at sn=$1
1296 # ps -ao user,pcpu,start,command
1297 # gpg --keyserver blackhole.pca.dfn.de --recv-keys
1298 # xterm -bg black -fg yellow -fn -misc-fixed-medium-r-normal--14-140-75-75-c-90-iso8859-15 -ah
1299 # nc -vz $1 1-1024   # portscan via netcat
1300 # wget --mirror --no-parent --convert-links
1301 # pal -d `date +%d`
1302 # autoload -U tetris; zle -N tetris; bindkey '...' ; echo "press ... for playing tennis"
1303 #
1304 # modify console cursor
1305 # see http://www.tldp.org/HOWTO/Framebuffer-HOWTO-5.html
1306 # print $'\e[?96;0;64c'
1307 # }}}
1308
1309 # finally source a local zshrc and grmlsmall-specific configuration {{{
1310
1311 # The following file is used to remove zsh-config-items that do not work
1312 # in grml-small by default.
1313 # If you do not want these adjustments (for whatever reason),
1314 # there are three ways to accomplish that:
1315 #  a) at the beginning of this file (variables section), set
1316 #     $GRMLSMALL_SPECIFIC to 0 or comment out the variable definition.
1317 #  b) remove/rename .zshrc.grmlsmall
1318 #  c) comment out the following line
1319 (( GRMLSMALL_SPECIFIC > 0 )) && isgrmlsmall && source ~/.zshrc.grmlsmall
1320
1321 # this allows us to stay in sync with /etc/skel/.zshrc
1322 # through 'ln -s /etc/skel/.zshrc ~/.zshrc' and put own
1323 # modifications in ~/.zshrc.local
1324 xsource "${HOME}/.zshrc.local"
1325
1326 # }}}
1327
1328 ### doc strings for external functions from files
1329 #m# f5 grml-wallpaper() Sets a wallpaper (try completion for possible values)
1330 ## END OF FILE #################################################################
1331 # vim:foldmethod=marker autoindent expandtab shiftwidth=4