rndm_luser /.zshrc

# .zshrc
# $Id: .zshrc,v 1.184 2008/07/05 23:09:04 user Exp $

[[ $- != *i* ]] && return
[[ $ZSH_VERSION != 4.<->* ]] && return

# Initialization {{{1
autoload -U compinit; compinit
autoload -U calendar
autoload -U edit-command-line

#zmodload zsh/mathfunc
zmodload -i zsh/complist

# Completion {{{1

# Completion system settings.
_force_rehash()
{
    (( CURRENT == 1 )) && rehash
    return 1 # Return 1 as no completion was actually performed.
}
zstyle ':completion:*' completer \
  _oldlist _expand _force_rehash _complete _match _approximate

# Use color in completions.
eval $(dircolors)
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}

# Remove trailing '/' from dirnames.
zstyle ':completion:*' squeeze-slashes true

# Use a cache to proxy the list of results.
zstyle ':completion:*' use-cache 1
zstyle ':completion:*' cache-path ~/.zcompcache

# Ignore completion functions for commands that don't exist.
zstyle ':completion:*:functions' ignored-patterns '_*'
zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'
zstyle ':completion:*:*:kill:*:processes' list-colors "=(#b) #([0-9]#)*=37=31"
zstyle ":completion:*:*:$command:*:$tag" list-colors "=(#b)\
	=$zshregex_with_brackets\
	=$default_color_escape_number\
	=$color_number_for_letters_in_first_bracket-pair\
	=$color_number_for_letters_in_second_bracket-pair" "..."

# Fuzzy matching of completions for when you mistype them.
zstyle ':completion:*:match:*' original only
zstyle ':completion:*:approximate:*' max-errors 1 numeric

# Enable Case-insensitive matching.
zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}'

# The number of errors allowed by _approximate increase with the length of
# what has been typed so far.
zstyle -e ':completion:*:approximate:*' \
	  max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3))numeric)'

# Enable hostname completion.
local _myhosts
_myhosts=( ${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[0-9]*}%%\ *}%%,*} )
zstyle ':completion:*' hosts $_myhosts

# Enable `kill` completion menu.
zstyle ':completion:*:processes' command 'ps xuf'
zstyle ':completion:*:processes' sort false
zstyle ':completion:*:*:*:*:processes' menu yes select
zstyle ':completion:*:*:*:*:processes' force-list always
zstyle ':completion:*:descriptions' format '%U%B%d%b%u'

# Prevent CVS files/directories from being completed.
zstyle ':completion:*:(all-|)files' ignored-patterns '(|*/)CVS'
zstyle ':completion:*:cd:*' ignored-patterns '(*/)#CVS'

# Application may be invoked with no arguments using first would be completion as
# argument -- prevents having to manually complete.
xdvi()
{ command xdvi ${*:-*.dvi(om[1])} }

# Remove duplicate path entries.
typeset -U path cdpath manpath fpath

# Option Settings {{{1
setopt \
AUTO_CD \
AUTO_PUSHD \
CDABLE_VARS \
CORRECT_ALL \
EXTENDED_GLOB \
GLOB_SUBST \
HIST_IGNORE_ALL_DUPS \
HIST_IGNORE_SPACE \
IGNORE_EOF \
INC_APPEND_HISTORY \
INTERACTIVE_COMMENTS \
NO_BAD_PATTERN \
NO_BEEP \
NO_BG_NICE \
NO_CLOBBER \
NO_HIST_BEEP \
NO_NOMATCH \
NO_NOTIFY \
NOHUP \
PUSHD_MINUS \
PUSHD_SILENT \
PUSHD_TO_HOME
#HIST_VERIFY

unsetopt \
PRINT_EXIT_VALUE

# Type dirname to chdir to that directory.
#compctl -/ cd

#cdpath=(~ $dirstack)

# Filename suffixes to ignore in completions.
#fignore=(.o)

# Aliases {{{1
#alias -g DN=/dev/null
#alias -g EG='|& egrep'
#alias -g EH='|& head'
#alias -g EL='|& less'
#alias -g ET='|& tail'
#alias -g H='| head'
#alias -g L="| less"
#alias -g MM='| most'
#alias -g M='| more'
#alias -g S='| sort'
#alias -g T='| tail'
#alias -g X='| xargs'

alias mv='nocorrect mv'
alias cp='nocorrect cp'
alias ln='nocorrect ln'

# Abbreviations {{{1
# NOTE: This only works with the vi keymap
typeset -Ag abbreviations

abbreviations=(
    "AC"     "cat"
    "AL"     "| less"
    "AM"     "| more"
    "AA"     "| awk"
    "AG"     "| grep"
    "AT"     "| tail"
    "AH"     "| head"
    "AS"     "| sort"
    "AW"     "| wc -l"
    "AX"     "| xargs"
    "A2N"    "2>/dev/null"
    "ATL"    "| tar t"
)

magic-abbrev-expand()
{
    local MATCH
    LBUFFER=${LBUFFER%%(#m)[_a-zA-Z0-9]#}
    LBUFFER+=${abbreviations[$MATCH]:-$MATCH}
    zle self-insert
}

no-magic-abbrev-expand()
{
  LBUFFER+=' '
}

zle -N magic-abbrev-expand
zle -N no-magic-abbrev-expand
bindkey " " magic-abbrev-expand
bindkey "^x " no-magic-abbrev-expand

# Key Bindings {{{1
bindkey -v
#bindkey -e
bindkey '^i' expand-or-complete-prefix
# history
bindkey '' history-search-forward
bindkey '' history-search-backward
# movement
bindkey '' beginning-of-line
bindkey '' end-of-line
bindkey '^F' forward-char
bindkey '^B' backward-char
bindkey '\e[1~' beginning-of-line
bindkey '\e[4~' end-of-line
bindkey '\e[3~' delete-char
#bindkey 'p' history-beginning-search-backward
#bindkey 'n' history-beginning-search-forward

zle -N edit-command-line
# Bind 'v' when in vicmd mode to invoke vim.
bindkey -M vicmd v edit-command-line

# Hooks {{{1
function set_title()
{
    if [[ $TERM == (linux|dumb) ]] return;
    
    # screen title
    if [[ $TERM == screen* ]]; then
        # tab title
        print -nR $'\ek'$1$'\e'\\\
        # hardstatus title
        print -nR $'\e]0;'$2$'\a'
    # xterm title
    else [[ $TERM == (*term*|rxvt*) ]]
        print -Pn "\e]2;$*\a"
    fi
}

function precmd()
{
    set_title zsh "$PWD"
}
  
function preexec()
{
    emulate -L zsh
    local -a cmd; cmd=(${(z)1})
    set_title "$cmd[1]:t" "$cmd[2,-1]"
}

function TRAPINT()
{
    print "\nCaught SIGINT, aborting."
    return $((128 + $1))
}

function TRAPUSR1()
{
    print "\nCaught SIGUSR1, reloading."
    source /etc/zsh/zprofile
}

function TRAPALRM()
{
    [[ ! -z $NO_EXIT ]] || [[ $(sched) != '' ]] && return

    echo "\n\nInactivity timeout on $TTY after $TMOUT seconds"

#    echo
#    vlock -c
#    echo
#    echo Terminal unlocked [Press Enter]

    function _exit()
    {
        setopt localtraps

        trap break INT

        echo -n "Ctrl-C to cancel; exiting in "
        for i in {9..0}; do
            echo -n $i
            sleep 1
            echo -ne '\a\b'
        done
        (( ! $i )) && exec exit || echo -e "\nCancelled [Press Enter]\n"; echo
    }

    _exit
}

#function periodic()
#{
    #[[ -x ~/bin/clean.sh ]] && ~/bin/clean.sh 3>&2 2>/dev/null
    #[[ -x ~/bin/hosts.sh ]] && ~/bin/hosts.sh 3>&2 2>/dev/null
#}

# Prompt {{{1
function set_prompt ()
{
    if [[ $TERM == "dumb" || term == "linux" ]]; then
        PROMPT=$(print "> ")
        unset RPS1
    else
        autoload zsh/terminfo colors
        # Uncomment the following line to enable color.
        #autoload -U promptinit; promptinit
        [[ $terminfo[colors] -lt 8 ]] && return

        PROMPT=$(print "%{${fg[green]}%}%B%(?..[%?] )%b%(#.%{${fg[red]}%}.%{${fg[green]}%})%n @ %m [%d]\n%y>%{${fg[default]}%} ")
        RPS1=$(print "%{${fg[green]}%}%D{%H:%M:%S}%{${fg[default]}%}")
    fi
    
    PS2=$(print "%_> ")         # Secondary prompt continuation line
    PS3=$(print "?# ")          # Selection prompt used within a select loop
    PS4=$(print "+%N:%i> ")     # Execution trace prompt for debugging with XTRACE option
}
#prompt walters
set_prompt

# Prepend value of $showcmd variable when in vicmd mode (i.e.,
# line-edit mode).
function zle-keymap-select()
{
  local showcmd=""

  if [[ $KEYMAP = vicmd ]]; then
    if [[ $PS1 != ${showcmd}* ]]; then
      PS1="$showcmd$PS1"
      zle reset-prompt
    fi
  else
    if [[ $PS1 = ${showcmd}* ]]; then
      PS1=${PS1##$showcmd}
      zle reset-prompt
    fi
  fi

# TODO: Disable cursor when in zle; Doesn't work :(
#if [[ $KEYMAP = vicmd ]]; then
#echo -e "\e[?25l"
#else
#echo -e "\e[?25h"
#fi
}
zle -N zle-keymap-select

# Colorizes the stderr
# FIXME: This causes interactive commands (e.g. cp,mv,rm -i) to not display
# their prompt (which is written to stderr) until a carriage return is
# received.

#exec 2>>(while read line; do
#	print '\e[91m'${(q)line}'\e[0m' > /dev/tty; done &)
# }}}

# Export, source, and execute shared resources compatible between zsh & bash
for x in $HOME/.shell_{exports,aliases,env}; do [[ -f $x ]] && source $x; done
unset x