/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.27.2 by Martin von Gagern
First programmatic generation of completions.
1
#!/usr/bin/python
2
3
from bzrlib import plugin
4
from bzrlib import commands
5
0.27.7 by Martin von Gagern
Introduce --function-only option
6
head="""\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
7
# Programmable completion for the Bazaar-NG bzr command under bash. Source
8
# this file (or on some systems add it to ~/.bash_completion and start a new
9
# shell) and bash's completion mechanism will know all about bzr's options!
10
11
# Known to work with bash 2.05a with programmable completion and extended
12
# pattern matching enabled (use 'shopt -s extglob progcomp' to enable
13
# these if they are not already enabled).
14
15
# Based originally on the svn bash completition script.
16
# Customized by Sven Wilhelm/Icecrash.com
0.27.2 by Martin von Gagern
First programmatic generation of completions.
17
# Adjusted for automatic generation by Martin von Gagern
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
18
0.27.6 by Martin von Gagern
Set extglob only temporarily for function definition. (LP #439827)
19
if shopt -q extglob; then
20
	_tmp_unset_extglob=""
21
else
22
	_tmp_unset_extglob="shopt -u extglob"
23
fi
0.27.5 by Martin von Gagern
Add shopt -s extglob progcomp. (LP #439827)
24
shopt -s extglob progcomp
0.27.7 by Martin von Gagern
Introduce --function-only option
25
"""
26
fun="""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
27
%(function_name)s ()
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
28
{
29
	local cur cmds cmdOpts opt helpCmds optBase i
30
31
	COMPREPLY=()
32
	cur=${COMP_WORDS[COMP_CWORD]}
33
0.27.2 by Martin von Gagern
First programmatic generation of completions.
34
	cmds='%(cmds)s'
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
35
36
	if [[ $COMP_CWORD -eq 1 ]] ; then
37
		COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
38
		return 0
39
	fi
40
41
	# if not typing an option, or if the previous option required a
42
	# parameter, then fallback on ordinary filename expansion
43
	helpCmds='help|--help|h|\?'
44
	if [[ ${COMP_WORDS[1]} != @($helpCmds) ]] && \
45
	   [[ "$cur" != -* ]] ; then
46
		return 0
47
	fi
48
49
	cmdOpts=
50
	case ${COMP_WORDS[1]} in
0.27.2 by Martin von Gagern
First programmatic generation of completions.
51
%(cases)s\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
52
	*)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
53
		cmdOpts='--help -h'
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
54
		;;
55
	esac
56
0.27.2 by Martin von Gagern
First programmatic generation of completions.
57
	cmdOpts=" $cmdOpts "
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
58
59
	# take out options already given
60
	for (( i=2; i<=$COMP_CWORD-1; ++i )) ; do
61
		opt=${COMP_WORDS[$i]}
62
63
		case $opt in
64
		--*)    optBase=${opt/=*/} ;;
65
		-*)     optBase=${opt:0:2} ;;
66
		esac
67
68
		cmdOpts=" $cmdOpts "
69
		cmdOpts=${cmdOpts/ ${optBase} / }
70
0.27.2 by Martin von Gagern
First programmatic generation of completions.
71
		# take out some alternatives
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
72
		case $optBase in
0.27.2 by Martin von Gagern
First programmatic generation of completions.
73
%(optalt)s\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
74
		esac
75
76
		# skip next option if this one requires a parameter
77
		if [[ $opt == @($optsParam) ]] ; then
78
			((++i))
79
		fi
80
	done
81
82
	COMPREPLY=( $( compgen -W "$cmdOpts" -- $cur ) )
83
84
	return 0
85
}
0.27.7 by Martin von Gagern
Introduce --function-only option
86
"""
87
tail="""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
88
complete -F %(function_name)s -o default bzr
0.27.6 by Martin von Gagern
Set extglob only temporarily for function definition. (LP #439827)
89
$_tmp_unset_extglob
90
unset _tmp_unset_extglob
0.27.2 by Martin von Gagern
First programmatic generation of completions.
91
"""
92
0.27.7 by Martin von Gagern
Introduce --function-only option
93
def bash_completion_function(out, function_name="_bzr", function_only=False):
0.27.2 by Martin von Gagern
First programmatic generation of completions.
94
    aliases = []
95
    cases = ""
96
    optaliases = {}
97
    reqarg = {}
98
    for name in sorted(commands.all_command_names()):
99
        cmd = commands.get_cmd_object(name)
100
        cases += "\t" + name
101
        aliases.append(name)
102
        for alias in cmd.aliases:
103
            cases += "|" + alias
104
            aliases.append(alias)
105
        cases += ")\n"
106
        plugin = cmd.plugin_name()
107
        if plugin is not None:
108
            cases += "\t\t# plugin \"%s\"\n" % plugin
109
        opts = cmd.options()
110
        optnames = []
111
        for optname in sorted(cmd.options()):
112
            opt = opts[optname]
113
            optset = set()
114
            for (name, short_name, optname, help) in opt.iter_switches():
115
                if short_name is not None:
116
                    optset.add("-" + short_name)
117
                if name is not None:
118
                    optset.add("--" + name)
119
            for optname in optset:
120
                if optname not in optaliases:
121
                    optaliases[optname] = optset
122
                else:
123
                    optaliases[optname] &= optset
124
            optnames.extend(sorted(optset))
125
        cases += "\t\tcmdOpts='" + " ".join(optnames) + "'\n\t\t;;\n"
126
    optalt = ""
127
    for opt1 in sorted(optaliases):
128
        optset = optaliases[opt1]
129
        if len(optset) == 1:
130
            continue
131
        optalt += "\t\t" + opt1 + ")\n"
132
        for opt2 in sorted(optset):
133
            if opt1 != opt2:
134
                optalt += "\t\t\tcmdOpts=${cmdOpts/ " + opt2 + " / }\n"
135
        optalt += "\t\t\t;;\n"
0.27.7 by Martin von Gagern
Introduce --function-only option
136
    if function_only:
137
        template = fun
138
    else:
139
        template = head + fun + tail
0.27.2 by Martin von Gagern
First programmatic generation of completions.
140
    out.write(template % {"cmds": " ".join(aliases),
141
                          "cases": cases,
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
142
                          "optalt": optalt,
143
                          "function_name": function_name,
144
                          })
0.27.2 by Martin von Gagern
First programmatic generation of completions.
145
146
if __name__ == '__main__':
147
148
    import sys
149
    import locale
150
151
    locale.setlocale(locale.LC_ALL, '')
152
    plugin.load_plugins()
153
    commands.install_bzr_command_hooks()
154
    bash_completion_function(sys.stdout)