Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Behaviour of {beginning,end}-of-buffer-or-history



Hi,

recently I was dissatisfied with the behavior of my HOME/END keys.
What I wanted was this:

If cursor is a beginning/end of the buffer
  then jump to the beginning/end of the history
Else
  jump to the beginning/end of that line,
  no matter whether we are in a multi- or single-line buffer


Seemingly there are functions that do exactly that:

beginning-of-buffer-or-history
   Move to the beginning of the buffer, or if already there, move to the first
   event in the history list.

end-of-buffer-or-history
   Move to the end of the buffer, or if already there, move to the last
   event in the history list.

But in reality, what above functions really do is this:

If cursor is a beginning/end of a buffer
  then jump to the beginning/end of the history
ElseIf cursor is somewhere in a SINGLE-LINE buffer
  then jump to the beginning/end of the history (why ???)
Else
  jump to the beginning/end of the MULTI-LINE buffer


I wonder if this is really how these functions are intended to work ?
At the very least I would consider them misnamed, or does "buffer" really only ever refer to multi-line buffers ?


In any case I thus wrote a bit of shell code that does the correct thing from my point of view (improvements welcome):

## ---------------------------------------------------------------------
## beginning-of-line OR beginning-of-buffer OR beginning of history
## (i.e. do it right, like beginning-of-buffer-or-history doesn't)
function jump-position-in-line-or-buffer-or-history
{
  local -a buflines beginning_of_lines end_of_lines cur_array
  local linepossum=0
  buflines=(${(f)BUFFER})
  beginning_of_lines=(0)
  for ((c=1;c<=$#buflines;c++)) do
    linepossum=$(( linepossum + ${#buflines[c]} ))
    end_of_lines+=( $linepossum )
    beginning_of_lines+=( $((++linepossum)) )
  done
  cur_array=(${(P)${${WIDGET/line-or-buffer-or-history/lines}//-/_}})
  if [[ ${cur_array[(i)$CURSOR]} -le ${#cur_array} ]]; then
    zle .${WIDGET/line-or-buffer-or-history/buffer-or-history} "$@"
  else
    zle .${WIDGET/line-or-buffer-or-history/line} "$@"
  fi
}
zle -N beginning-of-line-or-buffer-or-history \
	jump-position-in-line-or-buffer-or-history && \
	bindkey '\eOH' beginning-of-line-or-buffer-or-history
zle -N end-of-line-or-buffer-or-history \
	jump-position-in-line-or-buffer-or-history && \
	bindkey '\eOF' end-of-line-or-buffer-or-history
## ---------------------------------------------------------------------

cheers,
Bernhard



Messages sorted by: Reverse Date, Date, Thread, Author