#zdep modules,aliases
### vim:ft=zsh:foldmethod=marker
###
### Z-Shell Line Editor Setup
###
### Frank Terbeck <ft@bewatermyfriend.org>
### Last-Modified: Thu Jul 31 13:40:01 2008
###
### URI: <http://ft.bewatermyfriend.org/comp/zsh.html>
###


### key setup {{{

### first of all: emacs mode
###   yeah, i'm a vi user, but vi-modes of shells drive me nuts
bindkey -e

### }}}
### zkbd (read keys if needed) {{{

if zis_316 "atleast" ; then

    function Printf_file() {
        [[ -f "$2" ]] && printf "$1" "$2" && return 0
        return 1
    }

    function zrc_printf_termfile() {
        Printf_file '%s' ~/.zkbd/$TERM-$VENDOR-$OSTYPE && return 0
        Printf_file '%s' ~/.zkbd/$TERM-$DISPLAY        && return 0
        return 1
    }

    termfile=$(zrc_printf_termfile)
    if [[ -z "$termfile" ]] ; then
        zrcautoload zkbd && zkbd
        termfile=$(zrc_printf_termfile)
    fi

    if [[ -f "$termfile" ]] ; then
        (( ZSHRC_VERBOSE > 0 )) && printf '  zle: loading %s\n' "$termfile"
        source "$termfile"

        ### setup keys accordingly
        [[ -n ${key[Home]}      ]]  &&  bindkey "${key[Home]}"      beginning-of-line
        [[ -n ${key[End]}       ]]  &&  bindkey "${key[End]}"       end-of-line
        [[ -n ${key[Insert]}    ]]  &&  bindkey "${key[Insert]}"    overwrite-mode
        [[ -n ${key[Delete]}    ]]  &&  bindkey "${key[Delete]}"    delete-char
        [[ -n ${key[Up]}        ]]  &&  bindkey "${key[Up]}"        up-line-or-history
        [[ -n ${key[Down]}      ]]  &&  bindkey "${key[Down]}"      down-line-or-history
        [[ -n ${key[Left]}      ]]  &&  bindkey "${key[Left]}"      backward-char
        [[ -n ${key[Right]}     ]]  &&  bindkey "${key[Right]}"     forward-char
        [[ -n ${key[PageUp]}    ]]  &&  bindkey "${key[PageUp]}"    history-search-backward
        [[ -n ${key[PageDown]}  ]]  &&  bindkey "${key[PageDown]}"  history-search-forward
    else
        printf 'termfile (%s) not found. zkbd failed.\n' "$termfile"
    fi

    unset termfile
    unfunction Printf_file zrc_printf_termfile
fi

### }}}
### custom bindings {{{

bindkey '^xp' history-beginning-search-backward
bindkey '^xP' history-beginning-search-forward

### zsh <3.1 does not have a zle builtin command
if ! zis_31 "atleast" ; then
    function zle() { }
fi

### edit-command-line bound to 'ESC e'
if zrcautoload edit-command-line ; then
    zle -N        edit-command-line
    bindkey '\ee' edit-command-line
fi

### _complete_help is not bound by default?! fix it.
if zis_316 "atleast" ; then
    bindkey '^xH' _complete_help
    bindkey '^xD' _complete_debug
fi

### menu selection: pick item but stay in the menu
### bind this to 'ESC RETURN'
if zis_317 "atleast" ; then
    bindkey -M menuselect '\e^M' accept-and-menu-complete
fi

### complete words from history
if zis_317 "atleast" ; then
    zle -C complete-history complete-word _generic
    zstyle ':completion:complete-history:*' completer _history
    bindkey '^xh' complete-history
fi

### force filename completion, no matter what...
if zis_317 "atleast" ; then
    zle -C complete-files complete-word _generic
    zstyle ':completion:complete-files:*' completer _files
    bindkey '^xf' complete-files
fi

### opposite of ^K
bindkey '^xk' backward-kill-line

### backward clear from cursor to the next '/'
if zis_317 "atleast" ; then

    function backward-kill-to-slash() {
        local WORDCHARS="${WORDCHARS:s,/,} \\\'"

        [[ $BUFFER != */* ]] && return
        [[ $LBUFFER == [^/]##/ ]] && return

        zle backward-kill-word
    }

    zle -N        backward-kill-to-slash
    bindkey '^xw' backward-kill-to-slash
fi

### predict-{on,off} is one hell of a cool feature...
if zrcautoload predict-on ; then
    zle -N predict-on
    zle -N predict-off
    bindkey '^X^Z' predict-on
    bindkey '^Z' predict-off
fi

### exec_on_xclip (see http://bewatermyfriend.org/posts/2007/05-17.17-57-48-computer.html)
if zis_317 "atleast" ; then
    zle -N exec_on_xclip
    bindkey '^xb'   exec_on_xclip
    bindkey '^x^b'  exec_on_xclip
fi

if zis_317 "atleast" ; then
    if [[ -n ${functions[accept-line]} ]] ; then
        accept-line
        zstyle ':acceptline:empty' call_default false
    fi
fi

### jump behind the Nth word on the cmdline.
if zis_317 "atleast" ; then

    function jump_after_wordN() {
        setopt localoptions noksharrays extendedglob
        local words
        local -i i num=$1

        (( num == 0 )) && (( num = 10 ))
        words=(${(z)BUFFER})
        if (( ${#words} <= num )) ; then
            CURSOR=${#BUFFER}
        else
            if (( num == 1 )) ; then
                CURSOR=${#${words[${num}]}}
                return
            fi

            (( CURSOR = 1 ))
            for (( i = 1 ; i <= num ; ++i )) ; do
                (( CURSOR += ${#${words[$(( i ))]}} ))
                (( i == num )) && break

                while [[ ${BUFFER[$(( CURSOR + 1 ))]} == [[:space:]] ]] ; do
                    (( CURSOR++ ))
                done
            done
        fi
    }

    zle -N jump_after_wordN

    for i in {0..9} ; do
        eval "function jump_after_word${i} () { jump_after_wordN ${i} }"
        zle -N jump_after_word${i}
        bindkey '^x'${i} jump_after_word${i}
    done
fi

### 'ESC y' inserts the latest modified file the the command line
bindkey '^[y' _most_recent_file

### }}}