zsh-workers
 help / color / mirror / code / Atom feed
* Re: Behaviour of {beginning,end}-of-buffer-or-history
@ 2011-06-01 19:57 Bernhard Tittelbach
  2011-06-02  2:22 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Bernhard Tittelbach @ 2011-06-01 19:57 UTC (permalink / raw)
  To: zsh-workers

On 2011-06-01 21:43, Bernhard Tittelbach wrote:
> 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 ?
>
to clarify:

I find it odd that {beginning,end}-of-buffer-or-history jumps to the 
beginning/end of the history and not to the beginning/end of the line
if called on a single-line buffer

Jumping to the beginning/end of a line in a multi-line buffer is just an extra 
my function does, and not something I think {beginning,end}-of-buffer-or-history
should do (it's name does not imply any such functionality :)

cheers,
Bernhard


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Behaviour of {beginning,end}-of-buffer-or-history
  2011-06-01 19:57 Behaviour of {beginning,end}-of-buffer-or-history Bernhard Tittelbach
@ 2011-06-02  2:22 ` Bart Schaefer
  2011-06-02  3:04   ` Bernhard Tittelbach
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2011-06-02  2:22 UTC (permalink / raw)
  To: zsh-workers

On Jun 1,  9:57pm, Bernhard Tittelbach wrote:
}
} I find it odd that {beginning,end}-of-buffer-or-history jumps to the 
} beginning/end of the history and not to the beginning/end of the line
} if called on a single-line buffer

The confusion is with the connotation of "beginning".

In the case of this particular widget, it means "anywhere on the first
line", which for a single-line buffer is ... anywhere.  This function
happens to be in the class of full-line-motions rather than character-
position-motions, despite that not being obvious from the name.

You'll note if you pay close attention that when it does beginning-of-
history, it often jumps to the end of the first history entry; if the
first history entry is a multi-line buffer it'll cycle between the
beginning and the end of that buffer.
 
} Jumping to the beginning/end of a line in a multi-line buffer
} is just an extra my function does, and not somewhere I think
} {beginning,end}-of-buffer-or-history
} should do (it's name does not imply any such functionality :)

If you care to travel back in time 20 years or so you can take that
up with Paul Falstad. :-)

Meanwhile ... I think there's an easier way than whatever that is
you're doing with buflines and cur_array ...

beginning-or-end-of-somewhere() {
 local hno=$HISTNO
 zle .${WIDGET:s/somewhere/line-hist/} "$@"
 if (( HISTNO != hno )); then
   zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
 fi
}
zle -N beginning-of-somewhere beginning-or-end-of-somewhere
zle -N end-of-somewhere beginning-or-end-of-somewhere


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Behaviour of {beginning,end}-of-buffer-or-history
  2011-06-02  2:22 ` Bart Schaefer
@ 2011-06-02  3:04   ` Bernhard Tittelbach
  2011-06-02 14:25     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Bernhard Tittelbach @ 2011-06-02  3:04 UTC (permalink / raw)
  To: zsh-workers

On 2011-06-02 04:22, Bart Schaefer wrote:
> On Jun 1,  9:57pm, Bernhard Tittelbach wrote:
[...]

Thanks ! :)

>
> } Jumping to the beginning/end of a line in a multi-line buffer
> } is just an extra my function does, and not somewhere I think
> } {beginning,end}-of-buffer-or-history
> } should do (it's name does not imply any such functionality :)
>
> If you care to travel back in time 20 years or so you can take that
> up with Paul Falstad. :-)

If I learn how to do that, I'm sure that will be the first thing I'll 
use it for ;-)

>
> Meanwhile ... I think there's an easier way than whatever that is
> you're doing with buflines and cur_array ...
>
> beginning-or-end-of-somewhere() {
>   local hno=$HISTNO
>   zle .${WIDGET:s/somewhere/line-hist/} "$@"
>   if (( HISTNO != hno )); then
>     zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
>   fi
> }
> zle -N beginning-of-somewhere beginning-or-end-of-somewhere
> zle -N end-of-somewhere beginning-or-end-of-somewhere
>

Mhh, just when I got used to jumping to the beginning/end of a 
multi-line buffer with a double keypress of HOME/END ...

But it certainly is shorter, faster and more readable ;-)
and on single-line buffers completely equivalent to what my code did 
which is obviously going to be the predominant use-case.

Thanks again,
Bernhard


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Behaviour of {beginning,end}-of-buffer-or-history
  2011-06-02  3:04   ` Bernhard Tittelbach
@ 2011-06-02 14:25     ` Bart Schaefer
  2011-06-02 16:02       ` Bernhard Tittelbach
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2011-06-02 14:25 UTC (permalink / raw)
  To: zsh-workers

On Jun 2,  5:04am, Bernhard Tittelbach wrote:
}
} > beginning-or-end-of-somewhere() {
} >   local hno=$HISTNO
} >   zle .${WIDGET:s/somewhere/line-hist/} "$@"
} >   if (( HISTNO != hno )); then
} >     zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
} >   fi
} > }
} > zle -N beginning-of-somewhere beginning-or-end-of-somewhere
} > zle -N end-of-somewhere beginning-or-end-of-somewhere
} >
} 
} Mhh, just when I got used to jumping to the beginning/end of a 
} multi-line buffer with a double keypress of HOME/END ...

A quick test of [[ $LBUFFER[-1] = $'\n' ]] should fix that.

beginning-or-end-of-somewhere() {
  local hno=$HISTNO
  if [[ "${LBUFFER[-1]}" = $'\n' ]]
  then
    zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
  else
    zle .${WIDGET:s/somewhere/line-hist/} "$@"
    if (( HISTNO != hno )); then
      zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
    fi
  fi
}
zle -N beginning-of-somewhere beginning-or-end-of-somewhere
zle -N end-of-somewhere beginning-or-end-of-somewhere


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Behaviour of {beginning,end}-of-buffer-or-history
  2011-06-02 14:25     ` Bart Schaefer
@ 2011-06-02 16:02       ` Bernhard Tittelbach
  0 siblings, 0 replies; 6+ messages in thread
From: Bernhard Tittelbach @ 2011-06-02 16:02 UTC (permalink / raw)
  To: zsh-workers

On 02/06/11 16:25, Bart Schaefer wrote:
> On Jun 2,  5:04am, Bernhard Tittelbach wrote:
> }
[...]
> A quick test of [[ $LBUFFER[-1] = $'\n' ]] should fix that.
[...]

indeed, works perfectly now:

beginning-or-end-of-somewhere() {
  local hno=$HISTNO
  if [[ ( "${LBUFFER[-1]}" == $'\n' && "${WIDGET}" == beginning-of* )\
     || ( "${RBUFFER[1]}" == $'\n' && "${WIDGET}" == end-of* ) ]]; then
    zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
  else
    zle .${WIDGET:s/somewhere/line-hist/} "$@"
    if (( HISTNO != hno )); then
      zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
    fi
  fi
}
zle -N beginning-of-somewhere beginning-or-end-of-somewhere
zle -N end-of-somewhere beginning-or-end-of-somewhere


Cheers & Thanks,
Bernhard


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Behaviour of {beginning,end}-of-buffer-or-history
@ 2011-06-01 19:43 Bernhard Tittelbach
  0 siblings, 0 replies; 6+ messages in thread
From: Bernhard Tittelbach @ 2011-06-01 19:43 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-06-02 16:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-01 19:57 Behaviour of {beginning,end}-of-buffer-or-history Bernhard Tittelbach
2011-06-02  2:22 ` Bart Schaefer
2011-06-02  3:04   ` Bernhard Tittelbach
2011-06-02 14:25     ` Bart Schaefer
2011-06-02 16:02       ` Bernhard Tittelbach
  -- strict thread matches above, loose matches on Subject: below --
2011-06-01 19:43 Bernhard Tittelbach

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).