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

Re: This widget implementation feels a bit clunky (edit-quoted-word)



On Sun, Jun 21, 2015 at 7:17 PM, Peter Stephenson
<p.w.stephenson@xxxxxxxxxxxx> wrote:
> On Sun, 21 Jun 2015 09:09:29 +0200
> Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
>> On Sat, Jun 20, 2015 at 7:06 PM, Peter Stephenson
>> <p.w.stephenson@xxxxxxxxxxxx> wrote:
>> > On Sat, 20 Jun 2015 11:16:20 +0200
>> > Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
>> >> I feel like it's unnecessarily hard to get the resulting text from a
>> >> narrow-to-region session.
>> >
>> > Would you want something like this?
>>
>> That does look very promising, yeah. :)
>> I suppose I should volunteer for writing the doc patch for this.
>
> I've remembered to commit it on the master branch.

This feels more reasonable now (some of the improvements might be
applied to the previous version too, so not an entirely fair
comparison), (the business with the :+ is because if you delete the
whole string, ${(q-)lbuf} when lbuf is empty results in '', and so
does rbuf, thus concatenated to '''' which with rc_quotes enabled
leaves a single ' that should not be there).

function _edit_quoted_word()
{
  local -a reply
  local REPLY REPLY2 len left right lbuf rbuf
  _split_shell_arguments_intuitive
  left=${(j::)reply[1,REPLY-1]}
  right=${(j::)reply[REPLY+1,-1]}
  (( CURSOR-=$#left ))
  BUFFER=${(Q)reply[REPLY]}
  REGION_ACTIVE=0
  narrow-to-region -l lbuf -r rbuf -p "$left>>|" -P "|<<$right" 0 $#BUFFER
  LBUFFER=$left${lbuf:+${(q-)lbuf}}
  RBUFFER=${rbuf:+${(q-)rbuf}}$right
}
zle -N _edit_quoted_word
bindkey '^_q' _edit_quoted_word

autoload -U split-shell-arguments

function _split_shell_arguments_intuitive()
{
  split-shell-arguments
  # borrowed logic from modify-current-argument to get word under cursor
  (( REPLY > 1 && REPLY & 1 )) && (( REPLY-- ))
}

-- 
Mikael Magnusson



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