autoload -Uz promptinit compinit
compinit
promptinit
prompt suse

##################
# shell features #
##################

setopt equals
setopt extendedglob
setopt interactivecomments
setopt autopushd

HISTSIZE=100000
SAVEHIST=100000
HISTFILE=~/.zsh-history
setopt INC_APPEND_HISTORY

###############
# keybindings #
###############

autoload -U edit-command-line
zle -N edit-command-line

## Superseded by bracketed paste mode:
# autoload -Uz url-quote-magic
# zle -N self-insert url-quote-magic

bindboth() {
	bindkey -M viins "$@"
	bindkey -M vicmd "$@"
}

# redefine history widgets to drop into cmd mode
widget-vicmd() {
	local cur=$CURSOR
	zle .${WIDGET%-vicmd}
	zle vi-cmd-mode
	CURSOR=$cur
}
for W in up-line-or-history down-line-or-history \
		history-beginning-search-backward history-beginning-search-forward
do
	zle -N ${W}-vicmd widget-vicmd
done

# insert
bindboth "^[[2~" overwrite-mode
# delete
bindboth "^[[3~" delete-char
bindboth "^[3~" delete-char

# home
bindboth "^[[7~" beginning-of-line
bindboth "^[[1~" beginning-of-line
bindboth "^[[H"  beginning-of-line
bindboth "^[OH"  beginning-of-line
# end
bindboth "^[[8~" end-of-line
bindboth "^[[4~" end-of-line
bindboth "^[[F"  end-of-line
bindboth "^[OF"  end-of-line

# page up
bindboth "^[[5~" history-beginning-search-backward
# page down
bindboth "^[[6~" history-beginning-search-forward

bindkey -M vicmd n history-beginning-search-backward
bindkey -M vicmd N history-beginning-search-forward
bindboth "^[n" history-beginning-search-backward-vicmd
bindboth "^[N" history-beginning-search-forward-vicmd
bindboth "^N" down-line-or-history-vicmd
bindboth "^P" up-line-or-history-vicmd

# ctrl-z
#bindkey '^z' edit-command-line
bindkey -M vicmd "^V" edit-command-line
bindkey -M vicmd 'v' edit-command-line

# ctrl-e
#bindkey -M viins '^E' menu-expand-or-complete
bindboth -M viins '^A' beginning-of-line
bindboth -M viins '^E' end-of-line

# tab
bindkey -M viins '^I' complete-word

# no escape timeout viins
#bindkey -M viins '^C' vi-cmd-mode
bindkey -M viins '^F' vi-cmd-mode
bindkey -M vicmd '^F' vi-cmd-mode
#bindkey -M vicmd ^F send-break

# no escape timeout vicmd
#bindkey -rpM vicmd '^C'

# consider / a word break, for ctrl-w
WORDCHARS=${WORDCHARS//\/}

autoload forward-word-match; zle -N forward-word-match
autoload backward-word-match; zle -N backward-word-match
autoload transpose-words-match; zle -N transpose-words-match
autoload select-word-style

select-word-style shell
zstyle ':zle:*kill*' word-style normal

transpose-words-match-left() {
	zle backward-word-match
	zle transpose-words-match
	zle backward-word-match
}
zle -N transpose-words-match-left

transpose-words-match-right() {
	zle backward-word-match
	zle forward-word-match
	zle transpose-words-match
}
zle -N transpose-words-match-right

# VIM-style i_CTRL-V_digit input for character codes
bindkey -M viins '^V0' viins-ctrl-v-digit
bindkey -M viins '^V1' viins-ctrl-v-digit
bindkey -M viins '^V2' viins-ctrl-v-digit
bindkey -M viins '^Vo' viins-ctrl-v-octal
bindkey -M viins '^VO' viins-ctrl-v-octal
bindkey -M viins '^Vx' viins-ctrl-v-hex
bindkey -M viins '^VX' viins-ctrl-v-hex
bindkey -M viins '^Vu' viins-ctrl-v-hex
bindkey -M viins '^VU' viins-ctrl-v-hex

zle -N viins-ctrl-v-digit _viins_escape_decimal
zle -N viins-ctrl-v-octal _viins_escape_octal
zle -N viins-ctrl-v-hex _viins_escape_hex

function _viins_escape_decimal() {
  IFS= read -srk 2
  REPLY='10#'$KEYS[2]${REPLY//[^0-9]/}
  LBUFFER+=${(#)REPLY}
}

function _viins_escape_octal() {
  IFS= read -srk 3
  REPLY='8#'${REPLY//[^0-7]/}
  LBUFFER+=${(#)REPLY}
}

function _viins_escape_hex() {
  case $KEYS[2] in
    (u) IFS= read -srk 4;;
    (U) IFS= read -srk 8;;
    (*) IFS= read -srk 2;;
  esac
  REPLY='16#'${REPLY//[^0-9a-fA-F]/}
  LBUFFER+=${(#)REPLY}
}

# arrow movements
bindboth "^[OC"   vi-forward-word
bindboth "^[OD"   vi-backward-word
bindboth "^[^[OC" forward-word-match
bindboth "^[^[OD" backward-word-match
bindboth "^[^[[C" transpose-words-match-right
bindboth "^[^[[D" transpose-words-match-left

# arrows:  up     down   right  left
#          ^[[A   ^[[B   ^[[C   ^[[D
# alt      ^[^[[A ^[^[[B ^[^[[C ^[^[[D
# ctrl     ^[OA   ^[OB   ^[OC   ^[OD
# ctrl+alt ^[^[OA ^[^[OB ^[^[OC ^[^[OD

bindboth '^[h' vi-backward-char
bindboth "^[H" backward-word-match
bindboth '^[l' vi-forward-char
bindboth "^[L" forward-word-match

bindboth '^[j' beginning-of-line
bindboth '^[k' end-of-line

bindboth '^[J' transpose-words-match-left
bindboth '^[K' transpose-words-match-right

widget-tmux-previous-prompt() {
	tmux copy-mode \; send-keys $(print -P '?%n@%m ') C-m n j
}
zle -N widget-tmux-previous-prompt
bindboth '^K' widget-tmux-previous-prompt

widget-tmux-pageup-prompt() {
	tmux copy-mode \; send-keys $LINES k $(print -P '?%n@%m ') C-m
}
zle -N widget-tmux-pageup-prompt
bindboth '^U' widget-tmux-pageup-prompt
if ((${+functions[zaw-register-src]})); then
	bindkey -M filterselect '^U' widget-tmux-pageup-prompt

	# zaw

	bindboth '^X^X' zaw-tmux-word
	bindboth '^Xt' zaw-tmux-word
	bindboth '^Xl' zaw-tmux-line

	bindboth '^XT' zaw-tmux-word-all
	bindboth '^XL' zaw-tmux-line-all

	bindboth '^[q' zaw-zmuch-saved-search
	bindboth '^[Q' zaw-zmuch-saved-search-all
	bindboth '^[m' zaw-zmuch-message-unread
	bindboth '^[M' zaw-zmuch-message
	bindboth '^[t' zaw-zmuch-thread-unread
	bindboth '^[T' zaw-zmuch-thread

	mark-and-move-next() {
		zle set-mark-command
		zle down-line-or-history
	}
	zle -N mark-and-move-next
	bindkey -M filterselect '^X' mark-and-move-next
fi

# vim: ft=zsh noet