# -*-sh-*-
# notice: select-a-shell-word

### I rebind M-h etc to kill the whole argument
# sometimes I still need this (detail):

# see /etc/zsh/zshrc  for `arrows'

# http://www.zshwiki.org/cgi-bin/wiki.pl?ZshBindkeys

###  movement

my_extended_wordchars="\"*?_-.'[]~=+&;!#$%^(){}<>:@,\\";
my_extended_wordchars_space="${my_extended_wordchars} "
my_extended_wordchars_slash="${my_extended_wordchars}/"

# is forward-word the same as backward-word-match ?


backward-to-/ () {
    if ((CURSOR == 0)); then
	return
    elif (( --CURSOR == 0 ));  then
	return
    fi

    # at the end I'm on the / -- that means cursor is after
    while [[ $BUFFER[$CURSOR] != '/' ]]
	  do
	      if (( --CURSOR == 0 ));  then
		  break
	      fi
    done
}


forward-to-/ () {
    if ((CURSOR == $#BUFFER)); then
	return
    elif (( ++CURSOR == $#BUFFER ));  then
	return
    fi

    # at the end I'm on the / -- that means cursor is after
    while [[ $BUFFER[$CURSOR] != '/' ]]
	  do
	      if (( ++CURSOR == $#BUFFER ));  then
		  break
	      fi
    done
}


zle -N backward-to-/
zle -N forward-to-/


bindkey "\e/" forward-to-/
#bindkey "^X/" history-search-backward
bindkey "^\^" history-search-backward

# and
bindkey "\e\\" backward-to-/
bindkey "\e+" backward-to-/

# [ "${BUFFER[$CURSOR-1,$CURSOR]}" = '\ ' ]
# bindkey '\eb' miciah-backward-word



kill-backward-to-position(){
  to=shift;
 # to=shift;
  LBUFFER="${LBUFFER[0,$to]}"
}


# [22 nov 05]
kill-backward-with-spaces() {
    zle set-mark-command
    zle vi-backward-blank-word
    zle kill-region
}
zle -N kill-backward-with-spaces


##  to /
zle -N kill-to-/

kill-to-/(){
    local saved_mark=${MARK}
    zle set-mark-command
    zle forward-to-/
    zle kill-region
    MARK=${saved_mark}
}



zle -N kill-backward-to-/
kill-backward-to-/(){
    local saved_mark=${MARK}
    zle set-mark-command
    zle backward-to-/
    zle kill-region
    MARK=${saved_mark}
}


bindkey "^[*" kill-to-/
bindkey "^[<" kill-backward-to-/
bindkey "\e^k" kill-to-/

#bindkey "^Z undo



#LBUFFER RBUFFER
#MARK
#$CURSOR

# bindkey "^X^h" backward-kill-word

###   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
bindkey "^j" get-line
# exit-search


exit-search(){
    zle forward-char
    zle backward-char
    bindkey "^m" accept-line
}

zle -N exit-search

### misc

bindkey "^[o" copy-prev-shell-word


forward-next-word(){
     zle vi-forward-blank-word-end
     zle forward-char
}
zle -N forward-next-word

# My requirements:
# C-  1 char
# C-M  -- whole shell argument.

# some way to skip to /  (on filenames)
# b f  with group 1

# some way to skip words....


if false;
then
    bindkey '\ef' forward-next-word
    bindkey '\ee' forward-word
else
    # Meta:
    bindkey "\ef" emacs-forward-word
    bindkey "\eb" emacs-backward-word


    # bash-forward-word?
    bindkey '\ef' vi-forward-word
    # just after the current word:
    bindkey '\ee' forward-next-word
fi


### Use User-contributed ZLE Functions:

autoload -U select-word-style
select-word-style "shell"
# styles:
# `word-style'
# normal specified shell whitespace   -subword !!!
# `skip-chars'
zstyle ':zle:transpose-words' skip-chars 1
# forward-word  `skip-whitespace-first'

function autoload_widget()
{
    autoload $1
    zle -N $1
}

autoload_widget backward-word-match
autoload_widget forward-word-match
autoload_widget delete-whole-word-match



# Meta-Control:

bindkey "\e^F" forward-word-match
bindkey "\e^B" backward-word-match
bindkey "\e^k" delete-whole-word-match
# kill-backward-with-spaces

# delete-whole-word-match
# kill-word-match  -- I can use `delete-whole-word-match'
# backward-kill-word-match

autoload_widget transpose-words-match
bindkey "\e^t" transpose-words-match

# capitalize-word-match ... I can
# up-case-word-match
# down-case-word-match

# select-word-match ??
# match-word-context
# match-words-by-style

# bindkey "^Xh" kill-with-spaces

# bingo!
zstyle ':zle:transpose-words:whitespace' word-style shell
zstyle ':zle:transpose-words:filename' word-style normal
zstyle ':zle:transpose-words:filename' word-chars ''

bindkey '\ev' vi-oper-swap-case
# backward-kill-word
bindkey "^X^h" vi-backward-kill-word
