zsh-workers
 help / color / mirror / Atom feed
* KEYBOARD_HACK breaks with escaped quotes
@ 2021-03-07 10:23 Linus Kardell
  2021-03-29 12:30 ` Daniel Shahaf
  0 siblings, 1 reply; 2+ messages in thread
From: Linus Kardell @ 2021-03-07 10:23 UTC (permalink / raw)
  To: zsh-workers


[-- Attachment #1.1.1: Type: text/plain, Size: 759 bytes --]

So, zsh has the KEYBOARD_HACK option as an anti-annoyance feature, which 
removes a trailing quote character in case you accidentally hit it along 
with enter. However, this naively looks at whether there is an odd 
number of quotes at the command line, which causes it to invert when you 
have (and odd number of) escaped quotes, removing the trailing quote 
when it shouldn't and vice versa. For example, if you write 
'test'\''test' or 'test'"'"'test'' with this enabled, zsh will 
inapropriately remove the trailing quotes, whereas if you write echo 
'test'\''test'' it will not remove the trailing quote. Instead, zsh 
needs to more smartly check whether the quoting is unbalanced and if 
removing the trailing quote would make it balanced.


[-- Attachment #1.1.2: OpenPGP_0xE0EDF4F5F115F537.asc --]
[-- Type: application/pgp-keys, Size: 9115 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

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

* Re: KEYBOARD_HACK breaks with escaped quotes
  2021-03-07 10:23 KEYBOARD_HACK breaks with escaped quotes Linus Kardell
@ 2021-03-29 12:30 ` Daniel Shahaf
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Shahaf @ 2021-03-29 12:30 UTC (permalink / raw)
  To: Linus Kardell; +Cc: zsh-workers

Linus Kardell wrote on Sun, Mar 07, 2021 at 11:23:40 +0100:
> So, zsh has the KEYBOARD_HACK option as an anti-annoyance feature, which
> removes a trailing quote character in case you accidentally hit it along
> with enter. However, this naively looks at whether there is an odd number of
> quotes at the command line, which causes it to invert when you have (and odd
> number of) escaped quotes, removing the trailing quote when it shouldn't and
> vice versa. For example, if you write 'test'\''test' or 'test'"'"'test''
> with this enabled, zsh will inapropriately remove the trailing quotes,
> whereas if you write echo 'test'\''test'' it will not remove the trailing
> quote. Instead, zsh needs to more smartly check whether the quoting is
> unbalanced and if removing the trailing quote would make it balanced.

In the general case, determining whether the end of the line is outside
quotes requires knowing the previous lines (because quoted strings can
contain literal newlines).

SUN_KEYBOARD_HACK is implemented in inputline(); it is not part of the
lexer but a black box the lexer uses.  Therefore, I suspect that
function can't easily answer the above question.

Even answering that question for a single line would essentially require
lexing the line — whether using lex.c, using get_comp_string()/addx(),
or using bufferwords().  That's not impossible, but it'll be a lot
easier to unset SUN_KEYBOARD_HACK and reimplement it as a zle widget,
perhaps using ${(z)} and the concept of addx():

accept-line() {
  BUFFER+=' x'
  {
    local buf=$PREBUFFER$BUFFER
    if [[ -o INTERACTIVE_COMMENTS ]]; then
      local -a split=( ${(zZ+C+)buf} )
    else
      local -a split=( ${(z)buf} )
    fi
  } always {
    BUFFER=${BUFFER% x}
  }
  if [[ ${split[-1]} == x ]]; then
    …
  fi
  zle .$WIDGET -- "$@"
}

You might want to use LBUFFER rather than BUFFER.

Incidentally, we might want to invent a syntax that applies Z+C+ iff
INTERACTIVE_COMMENTS is set — say, ${(zZ+CC+)} could mean that, to avoid
using up a fourth option letter in the Z+…+ context ;-)

Sorry for the late answer.

Daniel


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

end of thread, other threads:[~2021-03-29 12:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-07 10:23 KEYBOARD_HACK breaks with escaped quotes Linus Kardell
2021-03-29 12:30 ` Daniel Shahaf

zsh-workers

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-workers

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-workers zsh-workers/ http://inbox.vuxu.org/zsh-workers \
		zsh-workers@zsh.org
	public-inbox-index zsh-workers

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git