# -*-sh-*-#

## Copied from https://wiki.archlinux.org/index.php/Zsh#Key_bindings
# create a zkbd compatible hash;
# to add other keys to this hash, see: man 5 terminfo

# mmc: use it locally!
# For now it's in the cli environment: echo $#key
typeset -A key

key[Home]=${terminfo[khome]}

key[End]=${terminfo[kend]}
key[Insert]=${terminfo[kich1]}
key[Delete]=${terminfo[kdch1]}
key[Up]=${terminfo[kcuu1]}
key[Down]=${terminfo[kcud1]}
key[Left]=${terminfo[kcub1]}
key[Right]=${terminfo[kcuf1]}
key[PageUp]=${terminfo[kpp]}
key[PageDown]=${terminfo[knp]}

# setup key 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]}"   beginning-of-buffer-or-history
[[ -n "${key[PageDown]}" ]]  && bindkey  "${key[PageDown]}" end-of-buffer-or-history

# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
    function zle-line-init () {
        printf '%s' "${terminfo[smkx]}"
    }
    function zle-line-finish () {
        printf '%s' "${terminfo[rmkx]}"
    }
    zle -N zle-line-init
    zle -N zle-line-finish
fi

########### End of the copy.
# Arrows for Beta:

if [[ $(id --user --name) = "beta" ]]
then
    # left, right
    bindkey '\e[D' backward-char
    bindkey '\e[C' forward-char


    # Home, End
    bindkey "\e[7~" beginning-of-line
    bindkey "\e[8~" end-of-line

    # Ctrl + left, right arrow keys!
    bindkey '\eOd' backward-word
    bindkey '\eOc' vi-forward-blank-word-end

    # Meta  left right
    bindkey '\e[\`2' backward-word
    #  word    next-wod
    #     12  34
    #     ^
    # at the end=last letter, words divided by space (or tab)
    # forward-next-word  at the first space!
    bindkey '\e[\`3' vi-forward-blank-word-end


    bindkey '\e[94~' kill-word

    bindkey '\e[93~' backward-kill-word
fi

### Line editing:
# rxvt delete:
bindkey "^[[3~" delete-char-or-list
bindkey -r "^[[3"
# Meta:
#kill-to-/
# Control
bindkey "^[[3\^" kill-word
# kill-word
bindkey "\eh" backward-kill-word
# delete-word does not save into kill-ring!
bindkey "^[d" kill-word
# complete shell argument:
# `new_widget' for the function
zle -N kill-parameter kill-word-match
zstyle :zle:kill-parameter word-style shell
bindkey "^[k" kill-parameter


# Meta:
bindkey "\e^H" kill-backward-with-spaces
# Control
bindkey "^?" backward-kill-word
# bindkey "\e[95~" backward-kill-word

bindkey "^w" kill-region
bindkey "\M-^k" kill-whole-line
#bindkey "^," backward-delete-char
# bindkey "\e^H backward-kill-word

# bindkey "\ec" capitalize-word
#bindkey "\C-i" kill-line




# Some nice key bindings
# #bindkey '^X^Z' universal-argument ' ' magic-space
# #bindkey '^X^A' vi-find-prev-char-skip
# #bindkey '^Xa' _expand_alias
# #bindkey '^Z' accept-and-hold
# #bindkey -s '\M-/' \\\\
# #bindkey -s '\M-=' \|

# # bindkey -v               # vi key bindings

# bindkey -e                 # emacs key bindings
# bindkey '^I' complete-word # complete on tab, leave expansion to _expand


###  mmc:
## Keyboard
#bindkey '^-' undo

# why do I need another one? C-space is enough!
# bindkey "\em" set-mark-command
# bindkey "\e@" set-mark-command


bindkey "^j" accept-and-infer-next-history
bindkey "\e/" history-beginning-search-backward
bindkey "\e|" history-beginning-search-backward


bindkey "\ei" complete-word
bindkey "^i" expand-or-complete-prefix
#bindkey "\C- " expand-or-complete
#bindkey "^ " expand-or-complete      ... 't was stupid

# never used:
# bindkey "\M- " expand-or-complete
# bindkey "\e " expand-or-complete

bindkey "\ee" expand-or-complete
bindkey "^X^I" expand-or-complete-prefix

bindkey '^Xx' all-matches
bindkey '\e*' match-word


 bindkey ' ' magic-space    # also do history expansion on space


# this had problems!!
# bindkey "\e^" history-beginning-search-backward

## \evi
# unused: bindkey "^xH" describe-key-briefly
bindkey "^Xk" describe-key-briefly

# bindkey "\e^H" run-help
# bindkey "^Xj" run-help
bindkey "^Xt" run-help
bindkey '^xr' caphuso_help

# bindkey "\eh" run-help

# bindkey "^-" undo
bindkey '^_' undo
#bindkey '^-' undo

#bindkey "\C-k" expand-or-complete


bindkey '\er' redisplay
#&& bindkey -me

## stty pass8 &&

if false;
then
    bindkey -s  "\M-OQ" "rsh -l postgres linux1"
    bindkey "\M-^I" reverse-menu-complete
fi
#bindkey -s "\M- " '/'
#bindkey -s "\M- " '/'
bindkey "^u" universal-argument



# bindkey -M menuselect '^M' send-break          # fails, disabled
# bindkey "\M-v" menu-select


autoload history-search-end
zle -N history-beginning-search-backward-end \
    history-search-end
zle -N history-beginning-search-forward-end \
    history-search-end
bindkey '\e^P' history-beginning-search-backward-end
bindkey '\e^N' history-beginning-search-forward-end


# unsetopt MULTIBYTE
bindkey '^g' vi-find-next-char
###   failed attempt to have a 'dead' key which stops the isearch. sort-of "C-m" in isearch in emacs.
my-history-incremental-search-backward()
{
    bindkey "^m" exit-search
    zle history-incremental-search-backward
    bindkey "^m" accept-line
}

zle -N my-history-incremental-search-backward
# bindkey "^r" my-history-incremental-search-backward

