/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.27.16 by Martin von Gagern
Added distutils setup script and plugin meta data.
1
#!/usr/bin/env python
0.27.2 by Martin von Gagern
First programmatic generation of completions.
2
0.27.14 by Martin von Gagern
Added GPL 2 license document and copyright notice.
3
# Copyright (C) 2009, 2010  Martin von Gagern
4
#
5
# This file is part of bzr-bash-completion
6
#
7
# bzr-bash-completion free software: you can redistribute it and/or
8
# modify it under the terms of the GNU General Public License as
9
# published by the Free Software Foundation, either version 2 of the
10
# License, or (at your option) any later version.
11
#
12
# bzr-bash-completion is distributed in the hope that it will be
13
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
# General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
0.28.2 by Martin von Gagern
merge from trunk
20
from bzrlib import (
21
    commands,
22
    config,
23
    help_topics,
24
    option,
25
    plugin,
26
)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
27
0.27.7 by Martin von Gagern
Introduce --function-only option
28
head="""\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
29
# Programmable completion for the Bazaar-NG bzr command under bash. Source
30
# this file (or on some systems add it to ~/.bash_completion and start a new
31
# shell) and bash's completion mechanism will know all about bzr's options!
32
33
# Known to work with bash 2.05a with programmable completion and extended
34
# pattern matching enabled (use 'shopt -s extglob progcomp' to enable
35
# these if they are not already enabled).
36
37
# Based originally on the svn bash completition script.
38
# Customized by Sven Wilhelm/Icecrash.com
0.27.2 by Martin von Gagern
First programmatic generation of completions.
39
# Adjusted for automatic generation by Martin von Gagern
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
40
0.27.6 by Martin von Gagern
Set extglob only temporarily for function definition. (LP #439827)
41
if shopt -q extglob; then
42
	_tmp_unset_extglob=""
43
else
44
	_tmp_unset_extglob="shopt -u extglob"
45
fi
0.27.5 by Martin von Gagern
Add shopt -s extglob progcomp. (LP #439827)
46
shopt -s extglob progcomp
0.27.7 by Martin von Gagern
Introduce --function-only option
47
"""
48
fun="""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
49
%(function_name)s ()
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
50
{
51
	local cur cmds cmdOpts opt helpCmds optBase i
52
53
	COMPREPLY=()
54
	cur=${COMP_WORDS[COMP_CWORD]}
55
0.27.2 by Martin von Gagern
First programmatic generation of completions.
56
	cmds='%(cmds)s'
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
57
58
	if [[ $COMP_CWORD -eq 1 ]] ; then
59
		COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
60
		return 0
61
	fi
62
63
	# if not typing an option, or if the previous option required a
64
	# parameter, then fallback on ordinary filename expansion
65
	helpCmds='help|--help|h|\?'
66
	if [[ ${COMP_WORDS[1]} != @($helpCmds) ]] && \
67
	   [[ "$cur" != -* ]] ; then
68
		return 0
69
	fi
70
71
	cmdOpts=
72
	case ${COMP_WORDS[1]} in
0.27.2 by Martin von Gagern
First programmatic generation of completions.
73
%(cases)s\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
74
	*)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
75
		cmdOpts='--help -h'
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
76
		;;
77
	esac
78
79
	COMPREPLY=( $( compgen -W "$cmdOpts" -- $cur ) )
80
81
	return 0
82
}
0.27.7 by Martin von Gagern
Introduce --function-only option
83
"""
84
tail="""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
85
complete -F %(function_name)s -o default bzr
0.27.6 by Martin von Gagern
Set extglob only temporarily for function definition. (LP #439827)
86
$_tmp_unset_extglob
87
unset _tmp_unset_extglob
0.27.2 by Martin von Gagern
First programmatic generation of completions.
88
"""
89
0.28.1 by Martin von Gagern
Include negated switches as well, using a modified options parser.
90
def wrap_container(list, parser):
91
    def tweaked_add_option(*opts, **attrs):
92
        list.extend(opts)
93
    parser.add_option = tweaked_add_option
94
    return parser
95
96
def wrap_parser(list, parser):
97
    orig_add_option_group = parser.add_option_group
98
    def tweaked_add_option_group(*opts, **attrs):
99
        return wrap_container(list, orig_add_option_group(*opts, **attrs))
100
    parser.add_option_group = tweaked_add_option_group
101
    return wrap_container(list, parser)
102
0.27.7 by Martin von Gagern
Introduce --function-only option
103
def bash_completion_function(out, function_name="_bzr", function_only=False):
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
104
    cmds = []
0.27.2 by Martin von Gagern
First programmatic generation of completions.
105
    cases = ""
106
    reqarg = {}
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
107
108
    user_aliases = {} # dict from cmd name to set of user-defined alias names
109
    for alias, expansion in config.GlobalConfig().get_aliases().iteritems():
110
        for token in commands.shlex_split_unicode(expansion):
111
            if not token.startswith("-"):
112
                user_aliases.setdefault(token, set()).add(alias)
113
                break
114
0.27.10 by Martin von Gagern
Complete help topics.
115
    all_cmds = sorted(commands.all_command_names())
116
    for cmdname in all_cmds:
117
        cmd = commands.get_cmd_object(cmdname)
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
118
119
        # Find all aliases to the command; both cmd-defined and user-defined.
120
        # We assume a user won't override one command with a different one,
121
        # but will choose completely new names or add options to existing
122
        # ones while maintaining the actual command name unchanged.
123
        aliases = [cmdname]
124
        aliases.extend(cmd.aliases)
125
        aliases.extend(sorted([alias
126
                               for name in aliases
127
                               if name in user_aliases
128
                               for alias in user_aliases[name]
129
                               if alias not in aliases]))
130
        cases += "\t%s)\n" % "|".join(aliases)
131
        cmds.extend(aliases)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
132
        plugin = cmd.plugin_name()
133
        if plugin is not None:
134
            cases += "\t\t# plugin \"%s\"\n" % plugin
135
        opts = cmd.options()
0.27.9 by Martin von Gagern
Remove suppression of alternate switches for the same option.
136
        switches = []
0.27.2 by Martin von Gagern
First programmatic generation of completions.
137
        for optname in sorted(cmd.options()):
138
            opt = opts[optname]
0.28.1 by Martin von Gagern
Include negated switches as well, using a modified options parser.
139
            optswitches = []
140
            parser = option.get_optparser({optname: opt})
141
            parser = wrap_parser(optswitches, parser)
142
            optswitches[:] = []
143
            opt.add_option(parser, opt.short_name())
144
            switches.extend(optswitches)
0.27.10 by Martin von Gagern
Complete help topics.
145
        if 'help' == cmdname or 'help' in cmd.aliases:
146
            switches.extend(sorted(help_topics.topic_registry.keys()))
147
            switches.extend(all_cmds)
0.27.9 by Martin von Gagern
Remove suppression of alternate switches for the same option.
148
        cases += "\t\tcmdOpts='" + " ".join(switches) + "'\n"
149
        cases += "\t\t;;\n"
0.27.7 by Martin von Gagern
Introduce --function-only option
150
    if function_only:
151
        template = fun
152
    else:
153
        template = head + fun + tail
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
154
    out.write(template % {"cmds": " ".join(cmds),
0.27.2 by Martin von Gagern
First programmatic generation of completions.
155
                          "cases": cases,
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
156
                          "function_name": function_name,
157
                          })
0.27.2 by Martin von Gagern
First programmatic generation of completions.
158
159
if __name__ == '__main__':
160
161
    import sys
162
    import locale
163
164
    locale.setlocale(locale.LC_ALL, '')
165
    plugin.load_plugins()
166
    commands.install_bzr_command_hooks()
167
    bash_completion_function(sys.stdout)