Re: programmable completion: completing filenames with default Readline behavior

2013-10-02 Thread Jeremy Lin
On Tue, Oct 1, 2013 at 1:59 PM, Chet Ramey chet.ra...@case.edu wrote:
 On 10/1/13 4:55 PM, Jeremy Lin wrote:

 Thanks, I actually ended up with the same solution (for Bash 4.0 and above,
 where 'compopt' is available, anyway). I just set '+o default' at the top of 
 the
 completion function and then set '-o default' as needed. Unfortunately, for
 earlier versions of Bash, the workaround I mentioned previously seems to be
 about as good as it gets.

 Earlier versions of bash are pretty old; bash-4.0 came out almost five
 years ago.

I know what you mean, but for one reason or another, there are still a
fair number
of people stuck on RHEL/CentOS 5.x, and hence using 3.2. Hopefully there will
be far fewer such people within the next few years...

 In future versions of Bash, it would be nice if the default Readline 
 completion
 could be invoked explicitly, rather than requiring this 'compopt' workaround.

 What syntax would you suggest for doing that?  It may require inventing a
 new way for completion functions to interact with their calling environment.

If COMPREPLY could be something other than an array, that might be used
to indicate that some other behavior is desired. For example, perhaps
COMPREPLY='complete-filename' could produce the behavior you currently
get with '-o default' and COMPREPLY=(), but without needing to set '-o default',
of course. I'm not sure how cleanly this would fit into the current scheme of
things, but it's just an idea.

Jeremy



Re: programmable completion: completing filenames with default Readline behavior

2013-10-01 Thread Chet Ramey
On 9/27/13 3:57 AM, Jeremy Lin wrote:
 I'm writing a completion where, in some cases, I'd like to use
 COMPREPLY=() to indicate that no more arguments to a command are
 expected, but in other cases, I'd like to offer the default Readline
 behavior for filename completions.
 
 So, if I have a directory 'foo', I'd like the shell to first complete
 with 'foo/' and then offer to complete files under 'foo'. But if I do
 something like COMPREPLY=($(compgen -f -- $cur)), then I simply get
 a completion of 'foo' (with a space appended).
 
 I know I can pass -o default to get Readline's default filename
 completion when COMPREPLY is empty, but then that seems to preclude
 using COMPREPLY=() to deny completions.

The `compopt' builtin was intended to add this kind of dynamism to
programmable completion shell functions.  You can install a completion
without -o default and turn it on using compopt when the completion
function is executed.  It appears to be lightly used, so there are
probably some usability improvements to be made there.  For instance, I
think that options, once set using compopt, remain set and need to be
manually disabled.  I'm waiting for some more reports from people using
it before I decide how (or whether) to change it.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: programmable completion: completing filenames with default Readline behavior

2013-10-01 Thread Jeremy Lin
On Tue, Oct 1, 2013 at 1:35 PM, Chet Ramey chet.ra...@case.edu wrote:
 On 9/27/13 3:57 AM, Jeremy Lin wrote:
 I'm writing a completion where, in some cases, I'd like to use
 COMPREPLY=() to indicate that no more arguments to a command are
 expected, but in other cases, I'd like to offer the default Readline
 behavior for filename completions.

 So, if I have a directory 'foo', I'd like the shell to first complete
 with 'foo/' and then offer to complete files under 'foo'. But if I do
 something like COMPREPLY=($(compgen -f -- $cur)), then I simply get
 a completion of 'foo' (with a space appended).

 I know I can pass -o default to get Readline's default filename
 completion when COMPREPLY is empty, but then that seems to preclude
 using COMPREPLY=() to deny completions.

 The `compopt' builtin was intended to add this kind of dynamism to
 programmable completion shell functions.  You can install a completion
 without -o default and turn it on using compopt when the completion
 function is executed.  It appears to be lightly used, so there are
 probably some usability improvements to be made there.  For instance, I
 think that options, once set using compopt, remain set and need to be
 manually disabled.  I'm waiting for some more reports from people using
 it before I decide how (or whether) to change it.

 Chet

Thanks, I actually ended up with the same solution (for Bash 4.0 and above,
where 'compopt' is available, anyway). I just set '+o default' at the top of the
completion function and then set '-o default' as needed. Unfortunately, for
earlier versions of Bash, the workaround I mentioned previously seems to be
about as good as it gets.

In future versions of Bash, it would be nice if the default Readline completion
could be invoked explicitly, rather than requiring this 'compopt' workaround.

Jeremy



Re: programmable completion: completing filenames with default Readline behavior

2013-10-01 Thread Chet Ramey
On 10/1/13 4:55 PM, Jeremy Lin wrote:

 Thanks, I actually ended up with the same solution (for Bash 4.0 and above,
 where 'compopt' is available, anyway). I just set '+o default' at the top of 
 the
 completion function and then set '-o default' as needed. Unfortunately, for
 earlier versions of Bash, the workaround I mentioned previously seems to be
 about as good as it gets.

Earlier versions of bash are pretty old; bash-4.0 came out almost five
years ago.

 In future versions of Bash, it would be nice if the default Readline 
 completion
 could be invoked explicitly, rather than requiring this 'compopt' workaround.

What syntax would you suggest for doing that?  It may require inventing a
new way for completion functions to interact with their calling environment.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



programmable completion: completing filenames with default Readline behavior

2013-09-27 Thread Jeremy Lin
I'm writing a completion where, in some cases, I'd like to use
COMPREPLY=() to indicate that no more arguments to a command are
expected, but in other cases, I'd like to offer the default Readline
behavior for filename completions.

So, if I have a directory 'foo', I'd like the shell to first complete
with 'foo/' and then offer to complete files under 'foo'. But if I do
something like COMPREPLY=($(compgen -f -- $cur)), then I simply get
a completion of 'foo' (with a space appended).

I know I can pass -o default to get Readline's default filename
completion when COMPREPLY is empty, but then that seems to preclude
using COMPREPLY=() to deny completions.

I've tried a number of different workarounds, none of which I was
completely happy with (or I probably wouldn't be asking this
question). The closest I got was setting -o default, and then using
COMPREPLY=() to semi-deny completion, and COMPREPLY=() to get
filename completion.

Can anyone offer a better approach?

Thanks,
Jeremy