zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@sunsite.dk
Subject: Re: How to make tab add quotes on expansion?
Date: Sat, 25 Oct 2003 18:26:27 +0000	[thread overview]
Message-ID: <1031025182627.ZM10361@candle.brasslantern.com> (raw)
In-Reply-To: <200310250100.12757.hisham@apple2.com>

On Oct 25,  1:00am, Hisham Muhammad wrote:
}
} I'd like to have tab-completion to add double quotes to words when the
} completed word has spaces in it (instead of having it add a backslash
} on each space).
} 
} I know there's probably something simple I am missing

No, actually, there isn't anything simple.

The builtins that update the command line don't modify the part to the
left of the cursor unless they have explicitly been told to (by, e.g.
"compadd -U").  A side-effect is that this preserves the quoting that's
already in use on the line.  So if you type

hisham@aria ~] mpg123 "01<TAB>

you'll get

hisham@aria ~] mpg123 "01 - Tom Sawyer.mp3"

but if you start with no quotes at all, you'll get backslashes.

Rather than try to convince the completion system to rewrite every word,
I suggest creating a normal (not completion, i.e., "zle -N" rather than
"zle -C") widget which first inserts the double quote and then calls a
completion widget.  Of course your widget will have to test whether any
quote is already there, etc.  This would best be accomplished by getting
the latest 4.1.x-dev from SourceForge, and using match-words-by-style to
parse the command line for you.  (I *think* m-w-b-s could be backported
to 4.0.7, but I'm not sure.)

As an alternative, you could use something like this completer:

  function _force_quote {
    [[ -z $compstate[quoting] ]] &&
    compstate[to_end]='' &&
    compadd -U -S "$SUFFIX" -I "$ISUFFIX"\" -i \""$IPREFIX" "$PREFIX"
  }

Plus this widget:

  function quote-and-complete-word {
    setopt localoptions unset noksharrays noshwordsplit
    local lbuf=$LBUFFER rbuf=$RBUFFER last=$LASTWIDGET
    if [[ $last != $WIDGET ]]
    then
      local oldcontext="$curcontext"
      local curcontext="$WIDGET:${${curcontext:-:::}#*:}"
      zle complete-word
      curcontext="$oldcontext"
    fi
    zle complete-word
    local ret=$?
    if [[ $_lastcomp[nmatches] -eq 0 && $last != $WIDGET ]]
    then
      LBUFFER=$lbuf RBUFFER=$rbuf
    fi
    return ret
  }
  zle -N quote-and-complete-word

Plus this zstyle:

  zstyle ':completion:quote-and-complete-word:*' completer _force_quote

And finally

  bindkey '^I' quote-and-complete-word

However, this seems to confuse zsh's "undo" mechanism pretty badly, so
use at your own risk.


      reply	other threads:[~2003-10-25 18:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-25  3:00 Hisham Muhammad
2003-10-25 18:26 ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1031025182627.ZM10361@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).