~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bashcomp.py

More flexible handling of fixed word lists.

Now a command (like help) which accepts a known fixed set of non-option
arguments can simply set the bash variable fixedWords and have them
expanded.  All other commands fall back to default expansion unless the
current word starts with a '-'.

This avoids our need for the extglob shopt.  It also allows us to add more
such fixed wordlist commands in the future.  There might be a slight
performance impact as the case distinction is now executed even for the
completion of non-option arguments, but that shouldn't be noticable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
# Generated using the bzr-bash-completion plugin version %(version)s.
44
44
# See https://launchpad.net/bzr-bash-completion for details.
45
45
 
46
 
if shopt -q extglob; then
47
 
        _tmp_unset_extglob=""
48
 
else
49
 
        _tmp_unset_extglob="shopt -u extglob"
50
 
fi
51
 
shopt -s extglob progcomp
 
46
shopt -s progcomp
52
47
"""
53
48
fun="""\
54
49
%(function_name)s ()
55
50
{
56
 
        local cur cmds cmdIdx cmd cmdOpts opt helpCmds i globalOpts
 
51
        local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
57
52
 
58
53
        COMPREPLY=()
59
54
        cur=${COMP_WORDS[COMP_CWORD]}
81
76
                return 0
82
77
        fi
83
78
 
84
 
        # if not typing an option, or if the previous option required a
85
 
        # parameter, then fallback on ordinary filename expansion
86
 
        helpCmds='help|--help|h|\?'
87
 
        if [[ $cmd != @($helpCmds) ]] && [[ $cur != -* ]] ; then
88
 
                return 0
89
 
        fi
90
 
 
91
79
        cmdOpts=
 
80
        fixedWords=
92
81
        case $cmd in
93
82
%(cases)s\
94
83
        *)
96
85
                ;;
97
86
        esac
98
87
 
99
 
        COMPREPLY=( $( compgen -W "$cmdOpts $globalOpts" -- $cur ) )
 
88
        # if not typing an option, and if we don't know all the
 
89
        # possible non-option arguments for the current command,
 
90
        # then fallback on ordinary filename expansion
 
91
        if [[ -z $fixedWords ]] && [[ $cur != -* ]] ; then
 
92
                return 0
 
93
        fi
 
94
 
 
95
        COMPREPLY=( $( compgen -W "$cmdOpts $globalOpts $fixedWords" -- $cur ) )
100
96
 
101
97
        return 0
102
98
}
103
99
"""
104
100
tail="""\
105
101
complete -F %(function_name)s -o default bzr
106
 
$_tmp_unset_extglob
107
 
unset _tmp_unset_extglob
108
102
"""
109
103
 
110
104
def wrap_container(list, parser):
163
157
            cases += "\t\t# plugin \"%s\"\n" % plugin
164
158
        opts = cmd.options()
165
159
        switches = []
 
160
        fixedWords = []
166
161
        for optname in sorted(cmd.options()):
167
162
            opt = opts[optname]
168
163
            optswitches = []
172
167
            opt.add_option(parser, opt.short_name())
173
168
            switches.extend(optswitches)
174
169
        if 'help' == cmdname or 'help' in cmd.aliases:
175
 
            switches.extend(sorted(help_topics.topic_registry.keys()))
176
 
            switches.extend(all_cmds)
 
170
            fixedWords.extend(sorted(help_topics.topic_registry.keys()))
 
171
            fixedWords.extend(all_cmds)
177
172
        cases += "\t\tcmdOpts='" + " ".join(switches) + "'\n"
 
173
        if fixedWords:
 
174
            cases += "\t\tfixedWords='" + " ".join(fixedWords) + "'\n"
178
175
        cases += "\t\t;;\n"
179
176
    if function_only:
180
177
        template = fun