* Code improvements to /etc/zsh/zshrc and /etc/skel/.zshrc,
[grml-etc-core.git] / etc / zsh / zshrc
index 68b4cea..936c704 100644 (file)
@@ -3,7 +3,7 @@
 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
 # Bug-Reports:   see http://grml.org/bugs/
 # License:       This file is licensed under the GPL v2.
-# Latest change: Son Okt 08 01:46:31 CEST 2006 [mika]
+# Latest change: Sam Okt 21 16:20:30 CEST 2006 [mika]
 ################################################################################
 # This file is sourced only for interactive shells. It
 # should contain commands to set up aliases, functions,
 # {{{ check for version/system
 # check for versions (compatibility reasons)
   is4(){
-    if [[ $ZSH_VERSION == 4.* ]]; then
-        return 0
-    else
-        return 1
-    fi
+    [[ $ZSH_VERSION == 4.* ]] && return 0
+    return 1
   }
 
 # current release
   is42(){
-    if [[ $ZSH_VERSION == 4.<2->* ]]; then
-        return 0
-    else
-        return 1
-    fi
+    [[ $ZSH_VERSION == 4.<2->* ]] && return 0
+    return 1
   }
 
 # grml specific stuff
   isgrml(){
-    if [ -f /etc/grml_version ] ; then
-        return 0
-    else
-        return 1
-    fi
+    [ -f /etc/grml_version ] && return 0
+    return 1
   }
 
   isgrmlcd(){
-    if [ -f /etc/grml_cd ] ; then
-        return 0
-    else
-        return 1
-    fi
+    [ -f /etc/grml_cd ] && return 0
+    return 1
   }
 
 # check for user, if not running as root set $SUDO to sudo
 # }}}
 
 # {{{ set some variables
-  [[ -z "$EDITOR" ]] && export EDITOR='vim'
-  [[ -z "$SHELL"  ]] && export SHELL='/bin/zsh'
-  [[ -z "$MAIL"   ]] && export MAIL="/var/mail/$USER"
+  export EDITOR=${EDITOR:-vim}
+  export SHELL=${SHELL:-/bin/zsh}
+  export MAIL=${MAIL:-/var/mail/$USER}
   [[ -f ~/.terminfo/m/mostlike ]] && MYLESS='LESS=C TERMINFO=~/.terminfo TERM=mostlike less' || MYLESS='less'
   [[ -x /usr/bin/dircolors ]] && eval `dircolors -b`
 
 # Search path for the cd comman
 #  cdpath=(.. ~)
 
+# Support our own site-functions
+  [ -d /etc/zsh/site-functions ] && export FPATH=/etc/zsh/site-functions:$FPATH
+
 # automatically remove duplicates from these arrays
   typeset -U path cdpath fpath manpath
 # }}}
@@ -129,25 +120,38 @@ fi
 
   # we don't want to quote/espace URLs on our own...
   # avoid 'url-quote-magic: function definition file not found' on some older boxes
-  if [ -f /usr/share/zsh/$ZSH_VERSION/functions/Zle/url-quote-magic ] ; then
-     autoload -U url-quote-magic
-     zle -N self-insert url-quote-magic
+  if [ -f "/usr/share/zsh/$ZSH_VERSION/functions/Zle/url-quote-magic" ] ; then
+     autoload -U url-quote-magic && zle -N self-insert url-quote-magic
+  else
+     print 'Notice: no url-quote-magic available :('
   fi
 
   alias run-help >&/dev/null && unalias run-help
   autoload run-help # use via 'esc-h'
 
-  is4 && autoload -U compinit && compinit # completition system
+# completion system
+  is42 && if autoload -U compinit && [ -f "/usr/share/zsh/$ZSH_VERSION/functions/Completion/compinit" ] ; then
+    compinit
+   else
+     print 'Notice: no compinit available :('
+     function zstyle { }
+     function compdef { }
+  fi
   is4 && autoload -U zed                  # use ZLE editor to edit a file or function
 
-  is4 && zmodload -i zsh/complist
-  is4 && zmodload -i zsh/deltochar
-  is4 && zmodload -i zsh/mathfunc
+  is4 && \
+  for mod in complist deltochar mathfunc ; do
+    zmodload -i zsh/${mod}
+  done
 # autoload zsh modules when they are referenced
-  is4 && zmodload -a zsh/stat stat
-  is4 && zmodload -a zsh/zpty zpty
-  is4 && zmodload -a zsh/zprof zprof
-  is4 && zmodload -ap zsh/mapfile mapfile
+  is4 && \
+  for opt mod in a  stat    \
+                 a  zpty    \
+                 a  zprof   \
+                 ap mapfile ; do
+    zmodload -${opt} zsh/${mod} ${mod}
+  done
+  unset opt mod
 
   is4 && autoload -U insert-files && \
   zle -N insert-files && \
@@ -161,6 +165,9 @@ fi
   zle -N edit-command-line && \
   bindkey '\ee' edit-command-line
 
+## menu selection: pick item but stay in the menu (press esc-return)
+  is4 && bindkey -M menuselect '\e^M' accept-and-menu-complete
+
 # press "ctrl-e d" to insert the actual date in the form yyyy-mm-dd
   _bkdate() { BUFFER="$BUFFER$(date '+%F')"; CURSOR=$#BUFFER; }
   bindkey '\C-ed' _bkdate
@@ -269,7 +276,11 @@ fi
 # }}}
 
 # {{{ set prompt
-  autoload promptinit ; promptinit # people should be able to use their favourite prompt
+  if [ -f "/usr/share/zsh/$ZSH_VERSION/functions/Prompts/promptinit" ] ; then
+     autoload promptinit && promptinit # people should be able to use their favourite prompt
+  else
+     print 'Notice: no prompiinit available :('
+  fi
   # precmd() => a function which is executed just before each prompt
   # precmd () { setopt promptsubst; [[ -o interactive ]] && jobs -l;
   # run 'NOPRECMD=1 zsh' to disable the precmd + preexec commands
@@ -515,6 +526,7 @@ grmlcomp() {
   zstyle ':completion:*:*:lintian:*'     file-patterns '*.deb'               # complete debian packages for command 'lintian'
   zstyle ':completion:*'                 matcher-list 'm:{a-z}={A-Z}'        # match uppercase from lowercase
   zstyle ':completion:*:matches'         group 'yes'                         # separate matches into groups
+  zstyle ':completion:*'                 group-name ''
   if [[ -z "$NOMENU" ]] ; then
     zstyle ':completion:*'               menu select=5                       # if there are more than 5 options allow selecting from a menu
   else
@@ -532,8 +544,9 @@ grmlcomp() {
   zstyle ':completion::(^approximate*):*:functions' ignored-patterns '_*'    # Ignore completion functions for commands you don't have:
 
 # complete manual by their section
-  zstyle ':completion:*:manuals' separate-sections true
-  zstyle ':completion:*:man:*' menu yes select
+  zstyle ':completion:*:manuals'    separate-sections true
+  zstyle ':completion:*:manuals.*'  insert-sections   true
+  zstyle ':completion:*:man:*'      menu yes select
 
 ## correction
 # run rehash on completion so new installed program are found automatically:
@@ -563,6 +576,17 @@ grmlcomp() {
 # zstyle ':completion:*' completer _complete _correct _approximate
 # zstyle ':completion:*' expand prefix suffix
 
+# automatic rehash? Credits go to Frank Terbeck
+# function my_accept () {
+#   local buf
+#   [[ -z ${BUFFER} ]] && zle accept-line && return
+#   buf=( ${(z)BUFFER}  )
+#   [[ -z ${commands[${buf[1]}]} ]] && rehash
+#   zle accept-line
+# }
+# zle -N my_accept
+# bindkey "^M" my_accept
+
 # command for process lists, the local web server details and host completion
   hosts=(`hostname` grml.org)
   zstyle '*' hosts $hosts
@@ -657,12 +681,30 @@ If you want to use xinit anyway please call \"/usr/bin/xinit\"."
 #  manzsh() {  /usr/bin/man zshall |  most +/"$1" ; }
 
 # use "dchange <package-name>" to view Debian's changelog of the package:
-  dchange() { most /usr/share/doc/${1}/changelog.Debian.gz ; }
+  dchange() {
+    if [ -r /usr/share/doc/${1}/changelog.Debian.gz ] ; then
+       most /usr/share/doc/${1}/changelog.Debian.gz
+    else
+       if [ -r /usr/share/doc/${1}/changelog.gz ] ; then
+         most /usr/share/doc/${1}/changelog.gz
+       else
+         echo "No changelog for package $1 found, sorry."
+         return 1
+       fi
+    fi
+  }
   _dchange() { _files -W /usr/share/doc -/ }
   compdef _dchange dchange
 
 # use "uchange <package-name>" to view upstream's changelog of the package:
-  uchange() { most /usr/share/doc/${1}/changelog.gz ; }
+  uchange() {
+    if [ -r /usr/share/doc/${1}/changelog.gz ] ; then
+       most /usr/share/doc/${1}/changelog.gz
+    else
+       echo "No changelog for package $1 found, sorry."
+       return 1
+    fi
+  }
   _uchange() { _files -W /usr/share/doc -/ }
   compdef _uchange uchange