[Bash-completion-devel] [RFC] support 'COMP_WORDBREAKS' value on a per-completion basis

Raphaël raphael.droz at gmail.com
Fri May 27 14:03:16 UTC 2011


=== Rational:
Let's say you want to complete http URL (which contain ':').

You'll probably use this kind of expression:

_comp() {
	COMPREPLY=( $(compgen -W "http://foo http://bar" -- "$cur") )
}

After the completion function is evaluated, readline will consider
the value of $COMP_WORDBREAKS to split the word to complete...
If the current argument is 'http://'
- if $COMP_WORDBREAKS contains ':' , only '//' will be used by the
completion process.
- otherwise (and if ' ' (space) is part of $COMP_WORDBREAKS), the
whole 'http://' string will be used.

The problem is that this evaluation happens after the completion script
returns (and won't work before $COMP_WORDBREAKS has been modified to
match our needs):

The FAQ says:
E13) Why does filename completion misbehave if a colon appears in the filename?

eg:
_comp() {
	export COMP_WORDBREAKS="${COMP_WORDBREAKS//:/}"
	COMPREPLY=( $(compgen -W "http://foo http://bar" -- "$cur") )
}

it has mainly two drawbacks:

1) the completion has to alter the user environment
 $ comp http://<TAB>
 $ echo $COMP_WORDBREAKS
"'><=;|&(       ### ':' has disappeared, other completion functions
		### may suffer from this

2) the first time we try a completion, _comp() has not yet been executed
so our modified $COMP_WORDBREAKS isn't yet part of the context.
 $ comp http://<TAB>
completes (the first time) to
 $ comp http:http://
but after that, $COMP_WORDBREAKS is modified and the next calls will succeed.


=== the proposed patch is in 3 parts (also attached):

https://gitorious.org/drzraf/bash/commit/0994f18671dc9c080b01af9c6005a19c7edac17c
Adds a char *word_breaks to the COMPSPEC struct

https://gitorious.org/drzraf/bash/commit/123ba1c50078c0857c489809132cc39ab59d7636
Support of a -B <COMP_WORDBREAKS> flag to the complete builtin

https://gitorious.org/drzraf/bash/commit/be1ff9edf02d7a28b1a4d18d8e996ef4ba56c490
registers readline 'rl_completion_word_break_hook' and makes the bash
function returns the value of the complete -B <flag> if it was
specified during the call to the 'complete' builtin.

===
If the rationale of this patch is accepted, I would be happy to discuss
the possibility to have it implemented at the 'compopt' level (what I
was unable to do) in order to change dynamically word bounds *during* the
completion process.



Raph
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-added-char-word_breaks-to-the-COMPSPEC-struct.patch
Type: text/x-sh
Size: 1475 bytes
Desc: not available
URL: <http://lists.alioth.debian.org/pipermail/bash-completion-devel/attachments/20110527/f6cd8790/attachment.sh>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-added-support-of-the-B-COMP_WORDBREAKS-flag-to-the-c.patch
Type: text/x-sh
Size: 2205 bytes
Desc: not available
URL: <http://lists.alioth.debian.org/pipermail/bash-completion-devel/attachments/20110527/f6cd8790/attachment-0001.sh>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0003-Registered-readline-rl_completion_word_break_hook-in.patch
Type: text/x-sh
Size: 3015 bytes
Desc: not available
URL: <http://lists.alioth.debian.org/pipermail/bash-completion-devel/attachments/20110527/f6cd8790/attachment-0002.sh>


More information about the Bash-completion-devel mailing list