#zdep zshrc.d/keyboard,zshrc.d/modules,zshrc.d/options,zshrc.d/aliases,zshrc.d/compsys
### vim:ft=zsh:foldmethod=marker
###
### Z-Shell Line Editor Setup
###
### Frank Terbeck <ft@bewatermyfriend.org>
### Last-Modified: Sun Dec 27 00:03:50 2009
###
### URI: <http://ft.bewatermyfriend.org/comp/zsh.html>
###

zis_31 "atleast" || function zle() { }

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

### load optional widgets {{{
### edit-command-line bound to 'ESC e' {{{

zrcautoload edit-command-line && \
    zle -N edit-command-line

### }}}
### load a number of smarter drop-in replacements for word actions {{{

zstyle ':zle:*' word-style shell
for i in \
        kill-word-match       \
        forward-word-match    \
        backward-word-match   \
        transpose-words-match \
        delete-whole-word-match; do
    zrcautoload $i
    zle -N $i
done; unset i
zle -N kill-whole-word-match delete-whole-word-match

### }}}
### string replacements in the current command buffer {{{

zstyle ':zle:replace-pattern' edit-previous false
zrcautoload replace-string
zrcautoload replace-string-again
zle -N replace-pattern replace-string
zle -N replace-string-again

### }}}
### complete words from history, and force filename completion {{{
if zis_317 "atleast" ; then
    zle -C complete-history complete-word _generic
    zstyle ':completion:complete-history:*' completer _history
    zle -C complete-files complete-word _generic
    zstyle ':completion:complete-files:*' completer _files
fi
### }}}
### 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
fi
### }}}
### predict-{on,off} {{{

zrcautoload predict-on && \
    zle -N predict-on  && \
    zle -N predict-off

### }}}
### exec_on_xclip (see http://bewatermyfriend.org/posts/2007/05-17.17-57-48-computer.html) {{{

zrcautoload exec_on_xclip && \
    zle -N exec_on_xclip

### }}}
### 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 ))
            (( i = 1 ))
            while [[ ${i} -le ${num} ]] ; do
                (( CURSOR += ${#${words[$(( i ))]}} ))
                (( i == num )) && break

                while [[ ${BUFFER[$(( CURSOR + 1 ))]} == [[:space:]] ]] ; do
                    (( CURSOR++ ))
                done
                (( ++i ))
            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}
    done
fi
### }}}
### history-search-end, similar to history-beginning-search-{back,for}ward {{{

if zrcautoload history-search-end ; then
    zle -N history-beginning-search-backward-end history-search-end
    zle -N history-beginning-search-forward-end history-search-end
fi

### }}}
### a zle widget to trigger completion {{{

zle -C ft-complete-tilde complete-word _generic
zstyle ':completion:ft-complete-tilde:*' completer _tilde _expand _complete _ignored _approximate

function ft-complete() {
    setopt extendedglob localoptions
    local action context widget word

    if [[ -z ${BUFFER} ]]; then
        context=empty
        zstyle -s ":zle:ft-complete:${context}" action action || action=empty
        zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
        case ${action} in
            dot-slash-complete)
                BUFFER='./'
                CURSOR=2
                zle ${widget} -w
                ;;
            empty)
                ;;
            *)
                zle ${widget} -w
                ;;
        esac

        return 0
    fi

    word=${LBUFFER##* }
    if [[ ${word} == \~* ]] ; then
        context=tilde
        zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
        zle ${widget} -w
        return 0
    fi
    context=default
    zstyle -s ":zle:ft-complete:${context}" widget widget || widget=complete-word
    zle ${widget} -w
    return 0
}
zle -N ft-complete

### }}}
### use a powerful accep-line replacement {{{

zis_317 "atleast" && zrcautoload accept-line && accept-line

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

zrcbindkey    Tab           ft-complete
zrcbindkey    Home          beginning-of-line
zrcbindkey    End           end-of-line
zrcbindkey    Insert        overwrite-mode
zrcbindkey    Delete        delete-char
zrcbindkey    Up            up-line-or-history
zrcbindkey    Down          down-line-or-history
zrcbindkey    Left          backward-char
zrcbindkey    Right         forward-char
zrcbindkey    Ctrl-x-p      history-search-backward
zrcbindkey    Ctrl-x-P      history-search-forward
zrcbindkey    Ctrl-x-k      backward-kill-line
zrcbindkey    Alt-y         _most_recent_file
zrcbindkey    Alt-Enter     self-insert-unmeta
zrcbindkey -W Alt-D         zap-to-char
zrcbindkey -W Alt-F         delete-to-char
zrcbindkey -W Alt-b         backward-word-match
zrcbindkey -W Alt-d         kill-word-match
zrcbindkey -W Alt-e         edit-command-line
zrcbindkey -W Alt-f         forward-word-match
zrcbindkey -W Alt-t         tranpose-words-match
zrcbindkey -W Ctrl-x-,      replace-pattern
zrcbindkey -W Ctrl-x-.      replace-string-again
zrcbindkey -W Ctrl-r        history-incremental-pattern-search-backward
zrcbindkey -W Ctrl-s        history-incremental-pattern-search-forward
zrcbindkey -W Ctrl-x-b      exec_on_xclip
zrcbindkey -W Ctrl-x-d      kill-whole-word-match
zrcbindkey -W Ctrl-x-Ctrl-b exec_on_xclip
zrcbindkey -W Ctrl-x-Ctrl-z predict-on
zrcbindkey -W Ctrl-z        predict-off
zrcbindkey -W Ctrl-x-w      backward-kill-to-slash
zrcbindkey -W Ctrl-x-f      complete-files
zrcbindkey -W Ctrl-x-h      complete-history
zrcbindkey -W Ctrl-x-H      _complete_help
zrcbindkey -W Ctrl-x-D      _complete_debug
zrcbindkey -W PageUp        history-beginning-search-backward-end
zrcbindkey -W PageDown      history-beginning-search-forward-end

zrcbindkey -s Ctrl-x-s      '*.[ch]'

zis_317 "atleast" && \
    zrcbindkey -W -M menuselect Alt-Enter accept-and-menu-complete

for i in {0..9} ; do zrcbindkey -W Ctrl-x-${i} jump_after_word${i} ; done

### }}}