[Bash-completion-devel] Bash bug regarding redirection and completion

Freddy Vulto fvulto at gmail.com
Sun Jan 25 16:26:22 UTC 2009


Santiago M. Mola <cooldwind <at> gmail.com> writes:
> Probably we can workaround that in bash-completion adding a helper
> function handling it at the beggining of every completion function.
> 
> If we adopt a completion wrapper like bash-completion-lib's, this may be
> cleaner.

Bash-completion-lib is only a wrapper for loading the actual completion (see
also BASH_COMPLETION_LIB - COMP_LOAD below).  In its current state it has no
real additional helper functions, because I'm trying to let the actual
completions of bash-completion-lib to be as similar to completion-debian as
possible.
In my opinion the most flexibility is gained if each completion script stays
responsible for calling completion wrapper functions.  This also eases a
possible migration to bash-completion-lib.

So, without waiting for bash-completion-lib, I would like to propose two helper
functions `comp_init()' and `comp_deinit()':

    comp_init() {
	# TODO: Handle redirection
	cur=$(_get_cword)
    }

    comp_deinit() {
	unset -v cur
    }

Now if we want `_cd()' to use the helper functions we can modify it like this:

--- cd.orig	2009-01-25 13:22:09.000000000 +0100
+++ cd	2009-01-25 13:23:46.000000000 +0100
@@ -7,7 +7,8 @@
 
 _cd()
 {
-    local IFS=$'\t\n' cur=`_get_cword` i j k
+    comp-init
+    local IFS=$'\t\n' i j k
 
     # try to allow variable completion
     if [[ "$cur" == ?(\\)\$* ]]; then
@@ -46,5 +47,6 @@
         fi
     fi
         
+    comp_deinit
     return 0
 }


BASH_COMPLETION_LIB - comp_load()
---------------------------------

Bash-completion-lib's central function `comp_load()' is relative slow when doing
TAB completion.  This is because `comp_load()' has to do quite some work (i.e.
look in many directories) to find the right completion.  Here you can see the
time difference between `comp_load cd' and the actual completion (_cd):

    $ COMP_WORDS=(cd)      # Initialize necessary variable for completion scripts
    $ time comp_load cd
    
    real    0m0.072s
    user    0m0.036s
    sys     0m0.036s
    $ time _cd 
    
    real    0m0.020s
    user    0m0.008s
    sys     0m0.012s

Because of this, and because a user is very likely to complete a second time,
`comp_load()' replaces itself with the actual completion, as can be seen in this
example:

    $ complete -p cd
    complete -o filenames -o nospace -F comp_load cd
    $ cd <TAB><TAB><^C>
    complete -o filenames -o nospace -F _cd cd

The just released `bash_completion_lib-1.3.0' looks at the environment variable
COMP_INSTALL to determine if `comp_load()' should replace itself with the actual
completion.  If COMP_INSTALL equals 0, `comp_load()' stays defined for
subsequent completions, so you can try the difference in speed.


Regards,

Freddy Vulto
http://fvue.nl/wiki/Bash_completion_library




More information about the Bash-completion-devel mailing list