/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
6
template="""\
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.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
25
%(function_name)s ()
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
26
{
27
	local cur cmds cmdOpts opt helpCmds optBase i
28
29
	COMPREPLY=()
30
	cur=${COMP_WORDS[COMP_CWORD]}
31
0.27.2 by Martin von Gagern
First programmatic generation of completions.
32
	cmds='%(cmds)s'
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
33
34
	if [[ $COMP_CWORD -eq 1 ]] ; then
35
		COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
36
		return 0
37
	fi
38
39
	# if not typing an option, or if the previous option required a
40
	# parameter, then fallback on ordinary filename expansion
41
	helpCmds='help|--help|h|\?'
42
	if [[ ${COMP_WORDS[1]} != @($helpCmds) ]] && \
43
	   [[ "$cur" != -* ]] ; then
44
		return 0
45
	fi
46
47
	cmdOpts=
48
	case ${COMP_WORDS[1]} in
0.27.2 by Martin von Gagern
First programmatic generation of completions.
49
%(cases)s\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
50
	*)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
51
		cmdOpts='--help -h'
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
52
		;;
53
	esac
54
0.27.2 by Martin von Gagern
First programmatic generation of completions.
55
	cmdOpts=" $cmdOpts "
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
56
57
	# take out options already given
58
	for (( i=2; i<=$COMP_CWORD-1; ++i )) ; do
59
		opt=${COMP_WORDS[$i]}
60
61
		case $opt in
62
		--*)    optBase=${opt/=*/} ;;
63
		-*)     optBase=${opt:0:2} ;;
64
		esac
65
66
		cmdOpts=" $cmdOpts "
67
		cmdOpts=${cmdOpts/ ${optBase} / }
68
0.27.2 by Martin von Gagern
First programmatic generation of completions.
69
		# take out some alternatives
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
70
		case $optBase in
0.27.2 by Martin von Gagern
First programmatic generation of completions.
71
%(optalt)s\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
72
		esac
73
74
		# skip next option if this one requires a parameter
75
		if [[ $opt == @($optsParam) ]] ; then
76
			((++i))
77
		fi
78
	done
79
80
	COMPREPLY=( $( compgen -W "$cmdOpts" -- $cur ) )
81
82
	return 0
83
}
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
84
complete -F %(function_name)s -o default bzr
0.27.6 by Martin von Gagern
Set extglob only temporarily for function definition. (LP #439827)
85
$_tmp_unset_extglob
86
unset _tmp_unset_extglob
0.27.2 by Martin von Gagern
First programmatic generation of completions.
87
"""
88
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
89
def bash_completion_function(out, function_name="_bzr"):
0.27.2 by Martin von Gagern
First programmatic generation of completions.
90
    aliases = []
91
    cases = ""
92
    optaliases = {}
93
    reqarg = {}
94
    for name in sorted(commands.all_command_names()):
95
        cmd = commands.get_cmd_object(name)
96
        cases += "\t" + name
97
        aliases.append(name)
98
        for alias in cmd.aliases:
99
            cases += "|" + alias
100
            aliases.append(alias)
101
        cases += ")\n"
102
        plugin = cmd.plugin_name()
103
        if plugin is not None:
104
            cases += "\t\t# plugin \"%s\"\n" % plugin
105
        opts = cmd.options()
106
        optnames = []
107
        for optname in sorted(cmd.options()):
108
            opt = opts[optname]
109
            optset = set()
110
            for (name, short_name, optname, help) in opt.iter_switches():
111
                if short_name is not None:
112
                    optset.add("-" + short_name)
113
                if name is not None:
114
                    optset.add("--" + name)
115
            for optname in optset:
116
                if optname not in optaliases:
117
                    optaliases[optname] = optset
118
                else:
119
                    optaliases[optname] &= optset
120
            optnames.extend(sorted(optset))
121
        cases += "\t\tcmdOpts='" + " ".join(optnames) + "'\n\t\t;;\n"
122
    optalt = ""
123
    for opt1 in sorted(optaliases):
124
        optset = optaliases[opt1]
125
        if len(optset) == 1:
126
            continue
127
        optalt += "\t\t" + opt1 + ")\n"
128
        for opt2 in sorted(optset):
129
            if opt1 != opt2:
130
                optalt += "\t\t\tcmdOpts=${cmdOpts/ " + opt2 + " / }\n"
131
        optalt += "\t\t\t;;\n"
132
    out.write(template % {"cmds": " ".join(aliases),
133
                          "cases": cases,
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
134
                          "optalt": optalt,
135
                          "function_name": function_name,
136
                          })
0.27.2 by Martin von Gagern
First programmatic generation of completions.
137
138
if __name__ == '__main__':
139
140
    import sys
141
    import locale
142
143
    locale.setlocale(locale.LC_ALL, '')
144
    plugin.load_plugins()
145
    commands.install_bzr_command_hooks()
146
    bash_completion_function(sys.stdout)