directory handling in bash completion

Alexis S. L. Carvalho alexis at cecm.usp.br
Sun Sep 4 00:22:05 CDT 2005


Thus spake TK Soh:
> Another problem I noticed with bash completion is that a directory name will be
> completed with a space appended, instead of a '/'. This prevents me from
> travelling further into the directory, unless I backspace and add the '/'
> manually.
> 
> Anyone?

Yeah, I had noticed this, but I'm not sure how to /really/ fix it.

For bash to handle this correctly, it would be necessary to pass 
"-o filenames" to the "complete" command, but this would break 
"hg status"-based completion, since it also has other side-effects 
(AFAICS in this case bash looks only at the basename of the completion
candidates).

Since we pass "-o default" to the "complete" command, if we give bash no
completion candidates, it will try its default filename completion.  So,
as a work-around, we can just not generate any completion candidates.

This has two disadvantages:

- it tries to complete not just with directories, but also with files

- for commands that take repository names, if you have anything defined
  in the [paths] section of your hgrc, it will try to complete with
  directories only if no path alias match the current argument.  I.e.
    $ hg clone <TAB><TAB>
  will show only path aliases.  You'd have to use something like
    $ hg clone ./<TAB><TAB>
  to complete with directory names.

I'd guess that the second problem would affect mostly only hg clone,
since the other commands (pull/push/outgoing/incoming) are usually run
from inside a repository - i.e. you usually run "hg pull ../upstream" or
"hg pull /path/to/repo" instead of "hg pull foo/bar/" and in the first
two cases the "../" and "/" in the beginning would probably eliminate
any path alias candidates.

The following patch implements this work-around.

Alexis



# HG changeset patch
# User Alexis S. L. Carvalho <alexis at cecm.usp.br>
# Node ID f3930ba63cbb74378d6fc86dd535ebc8143c7f61
# Parent  c165cbf56bb117ebdd226aea950e67b543a665e7
bash_completion: work-around for directory completion problem

Currently bash completion is not appending "/" to directory names,
making it harder to walk around the filesystem.

For bash to handle this correctly, it would be necessary to pass
"-o filenames" to the "complete" command, but this would break
"hg status"-based completion, since it also has other side-effects
(AFAICS in this case bash looks only at the basename of the completion
candidates).

Since we pass "-o default" to the "complete" command, if we give bash no
completion candidates, it will try its default filename completion.  So,
as a work-around, we can just not generate any completion candidates.

This has two disadvantages:

- it tries to complete not just with directories, but also with files

- for commands that take repository names, if you have anything defined
  in the [paths] section of your hgrc, it will try to complete with
  directories only if no path alias match the current argument.  I.e.
    $ hg clone <TAB><TAB>
  will show only path aliases.  You'd have to use something like
    $ hg clone ./<TAB><TAB>
  to complete with directory names.

I'd guess that the second problem would affect mostly only hg clone,
since the other commands (pull/push/outgoing/incoming) are usually run
from inside a repository - i.e. you usually run "hg pull ../upstream" or
"hg pull /path/to/repo" instead of "hg pull foo/bar/" and in the first
two cases the "../" and "/" in the beginning would probably eliminate
any path alias candidates.

diff -r c165cbf56bb1 -r f3930ba63cbb contrib/bash_completion
--- a/contrib/bash_completion	Thu Sep  1 19:13:56 2005
+++ b/contrib/bash_completion	Sun Sep  4 05:12:07 2005
@@ -84,11 +84,9 @@
     # global options
     case "$prev" in
 	-R|--repository)
-	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
 	    return
 	;;
 	--cwd)
-	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
 	    return
 	;;
     esac
@@ -115,7 +113,6 @@
 	;;
 	pull|push|outgoing|incoming)
 	    _hg_paths
-	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
 	;;
 	paths)
 	    _hg_paths
@@ -143,7 +140,6 @@
 	    if [ $count = 1 ]; then
 		_hg_paths
 	    fi
-	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
 	;;
 	debugindex|debugindexdot)
 	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" ))
@@ -155,12 +151,7 @@
 	    local count=$(_hg_count_non_option '-o|--output')
 	    if [ $count = 2 ]; then
 		_hg_tags
-	    else
-		COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
 	    fi
-	;;
-	*) 
-            COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
 	;;
     esac
 


More information about the Mercurial mailing list