zsh-users
 help / color / mirror / code / Atom feed
* How to make tab add quotes on expansion?
@ 2003-10-25  3:00 Hisham Muhammad
  2003-10-25 18:26 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Hisham Muhammad @ 2003-10-25  3:00 UTC (permalink / raw)
  To: zsh-users

Hello zsh users and developers,

I am a developer of the GoboLinux distribution (http://www.gobolinux.org) 
which is, AFAIK, the only Linux distro where zsh is the default shell. I 
prepared a custom default zshrc, suited for the distro's needs (featuring 
case insensitive matching, because of our verbose names, such as 
/Programs/XFree86/Settings and other niceties) and all users seem to love 
it... zsh gets lots of praise in the GoboLinux mailing list!

There is one thing, however, that I could not find out how to configure:
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). So that it would do this:

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

instead of 

hisham@aria ~] mpg123 01<TAB>
hisham@aria ~] mpg123 01\ -\ Tom\ Sawyer.mp3

I know there's probably something simple I am missing, but I read the man 
pages, the FAQs and mailing list archives and haven't found out yet.

Thanks in advance,

-- 
Hisham Muhammad
"Embrace the change."


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

* Re: How to make tab add quotes on expansion?
  2003-10-25  3:00 How to make tab add quotes on expansion? Hisham Muhammad
@ 2003-10-25 18:26 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2003-10-25 18:26 UTC (permalink / raw)
  To: zsh-users

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.


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

end of thread, other threads:[~2003-10-25 18:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-25  3:00 How to make tab add quotes on expansion? Hisham Muhammad
2003-10-25 18:26 ` Bart Schaefer

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).