# Setup {{{
# Tmux clear screen
tmux-clear-screen() {
    for line in {1..$(( $LINES ))} ; do echo; done
    zle clear-screen
}

# Run with sudo
run-with-sudo() { LBUFFER="sudo $LBUFFER" }

foreach carga (
	history-substring-search-up
	history-substring-search-down
	up-line-or-beginning-search
	down-line-or-beginning-search
	surround
	select-bracketed
	select-quoted
	edit-command-line
) {
	autoload -U $carga
}
foreach widge (
	history-substring-search-up
	history-substring-search-down
	up-line-or-beginning-search
	down-line-or-beginning-search
	delete-surround surround
	add-surround surround
	change-surround surround
	select-bracketed
	select-quoted
	tmux-clear-screen
	run-with-sudo
	edit-command-line
) {
	zle -N $widge
}

bindkey -v

typeset -g -A key

key[Home]="$terminfo[khome]"
key[End]="$terminfo[kend]"
key[Insert]="$terminfo[kich1]"
key[Backspace]="$terminfo[kbs]"
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]"
# }}}
# Insert {{{
[[ -n "$key[Home]"      ]] && bindkey -M viins -- "$key[Home]"      beginning-of-line
[[ -n "$key[End]"       ]] && bindkey -M viins -- "$key[End]"       end-of-line
[[ -n "$key[Insert]"    ]] && bindkey -M viins -- "$key[Insert]"    overwrite-mode
[[ -n "$key[Backspace]" ]] && bindkey -M viins -- "$key[Backspace]" backward-delete-char
[[ -n "$key[Delete]"    ]] && bindkey -M viins -- "$key[Delete]"    delete-char
[[ -n "$key[Right]"     ]] && bindkey -M viins -- "$key[Right]"     forward-char
[[ -n "$key[PageUp]"    ]] && bindkey -M viins -- "$key[PageUp]"    history-beginning-search-backward
[[ -n "$key[PageDown]"  ]] && bindkey -M viins -- "$key[PageDown]"  history-beginning-search-forward
[[ -n "$key[Up]"        ]] && bindkey -M viins -- "$key[Up]"        history-substring-search-up
[[ -n "$key[Down]"      ]] && bindkey -M viins -- "$key[Down]"      history-substring-search-down

if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then
	autoload -Uz add-zle-hook-widget
	function zle_application_mode_start { echoti smkx }
	function zle_application_mode_stop { echoti rmkx }
	add-zle-hook-widget -Uz zle-line-init zle_application_mode_start
	add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop
fi

bindkey -M viins '^K'	up-history # ^K to previous command.
bindkey -M viins '^J' 	down-history # ^J to next command.
bindkey -M viins '^F' 	vi-forward-char # Go forward char or complete current completion.
bindkey -M viins '^B' 	backward-kill-word # Delete a WORD backward.
bindkey -M viins '^Y' 	redo
bindkey -M viins '^P' 	up-line-or-beginning-search
bindkey -M viins '^N' 	down-line-or-beginning-search
bindkey -M viins '^U' 	history-incremental-pattern-search-backward
bindkey -M viins '^[[A' history-substring-search-up
bindkey -M viins '^[[B' history-substring-search-down
bindkey -M viins '^E'   edit-command-line
bindkey '^L'		tmux-clear-screen
bindkey '^X'		run-with-sudo
# }}}
# Normal {{{
bindkey -M vicmd 'j'	down-line # Override down-line-or-history.
bindkey -M vicmd 'k'	up-line # Override up-line-or-history.
bindkey -M vicmd '^K'	up-history # ^K to previous command.
bindkey -M vicmd '^J'	down-history # ^J to next command.
bindkey -M vicmd 'H'	vi-beginning-of-line # Go beginning of line.
bindkey -M vicmd 'L'	vi-end-of-line # Go end of line.
bindkey -M vicmd 'cs'	change-surround # Change surround operator.
bindkey -M vicmd 'ds'	delete-surround # Delete surround operator.
bindkey -M vicmd 'ys'	add-surround # Add surround operator.
bindkey -M vicmd '?'    history-incremental-search-backward
bindkey -M vicmd '/'    history-incremental-search-forward
bindkey -M vicmd '^[[A' history-substring-search-up
bindkey -M vicmd '^[[B' history-substring-search-down
bindkey -M vicmd '^Y'	redo
# }}}
# Visual {{{
bindkey -M visual 'H' vi-beginning-of-line # Go beginning of line.
bindkey -M visual 'L' vi-end-of-line # Go end of line.
bindkey -M visual 'S' add-surround # Add surround.
foreach char ({a,i}{\',\",\`}) { bindkey -M visual $char select-quoted } && unset char # Text objects for delimiters.
foreach char ({a,i}${(s..)^:-'()[]{}<>bB'}) { bindkey -M visual $char select-bracketed } && unset char # Text objects for pairs of brackets.
# }}}
# Operator {{{
bindkey -M viopp 'H' vi-beginning-of-line # Go beginning of line.
bindkey -M viopp 'L' vi-end-of-line # Go end of line.
# bindkey -M menuselect '^[' undo
foreach char ({a,i}{\',\",\`}) { bindkey -M viopp $char select-quoted } && unset char # Text objects for delimiters.
foreach char ({a,i}${(s..)^:-'()[]{}<>bB'}) { bindkey -M viopp $char select-bracketed } && unset char # Text objects for pairs of brackets.

export KEYTIMEOUT=5
# }}}
# vim:foldmethod=marker:foldlevel=0
