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

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



autoload -U narrow-to-region
function _narrow_to_region_marked()
{ #I had this function since before
  local right
  local left
  local OLDMARK=MARK
  local wasregion=1
  if ((REGION_ACTIVE == 0)); then
    MARK=CURSOR
    wasregion=0
  fi
  REGION_ACTIVE=0
  if ((MARK < CURSOR)); then
    left="$LBUFFER[0,$((MARK-CURSOR-1))]"
    right="$RBUFFER"
  else
    left="$LBUFFER"
    right="$BUFFER[$((MARK+1)),-1]"
  fi
  narrow-to-region -p "$left>>|" -P "|<<$right"
  _line_redraw #update highlight stuff doesn't happen here otherwise
  MARK=OLDMARK
  if ((wasregion)); then
    REGION_ACTIVE=1
  fi
}

function _edit_quoted_word()
{
  local -a reply
  local REPLY REPLY2 len
  _split_shell_arguments_intuitive
  local STARTPOS ENDPOS
  local OLDCURSOR=$CURSOR OLDMARK=$MARK wasregion=$REGION_ACTIVE
  STARTPOS=${#${(j::)reply[1,REPLY-1]}}
  reply[REPLY]=${(Q)reply[REPLY]}
  ENDPOS=${#${(j::)reply[1,REPLY]}}
  CURSOR=$STARTPOS
  MARK=$ENDPOS
  REGION_ACTIVE=1
  BUFFER=${(j::)reply}
  len=$#BUFFER
  _narrow_to_region_marked
  CURSOR=$STARTPOS
  MARK=$((ENDPOS+$#BUFFER-len))
  zle quote-region
  #the following line is also my quote-unquote-word widget
  modify-current-argument '${(q-)${(Q)ARG}}'
  CURSOR=$OLDCURSOR
  MARK=$OLDMARK
  REGION_ACTIVE=$wasregion
}
zle -N edit-quoted-word _edit_quoted_word
bindkey '^_q' edit-quoted-word

autoload -U modify-current-argument
autoload -U split-shell-arguments

function _split_shell_arguments_intuitive()
{ #Had this since before too
  split-shell-arguments
  #have to duplicate some of modify-current-argument to get the word
  #_under_ the cursor, not after.
  setopt localoptions noksharrays multibyte
  if (( REPLY > 1 )); then
    if (( REPLY & 1 )); then
      (( REPLY-- ))
    fi
  fi
}

I'm not exactly sure how often I would use this, but I had the thought
and set off on an adventure. This lets you be on a thing like
% echo I\''d (like) to "have\" some better example?'
turns into
% echo >>|I'd (like) to "have\" some better example?|<<
which is then somewhat easier to edit, then you maybe type this instead
% echo >>|I'd (like) to "have\" '${(q-)${(Q)ARG}}' some better example?|<<
after pressing enter, it turns back into
% echo I\''d (like) to "have\" ''${(q-)${(Q)ARG}}'' some better example?'

I feel like it's unnecessarily hard to get the resulting text from a
narrow-to-region session.
It would be nice if quote-region let you specify the type of quoting.
Urk?

-- 
Mikael Magnusson



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