/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.27.17 by Martin von Gagern
Mention version and homepage in generated shell code comment.
20
from meta import __version__
0.28.2 by Martin von Gagern
merge from trunk
21
from bzrlib import (
22
    commands,
23
    config,
24
    help_topics,
25
    option,
26
    plugin,
27
)
0.27.18 by Martin von Gagern
Add global options in all cases
28
import re
0.27.2 by Martin von Gagern
First programmatic generation of completions.
29
0.27.7 by Martin von Gagern
Introduce --function-only option
30
head="""\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
31
# Programmable completion for the Bazaar-NG bzr command under bash. Source
32
# this file (or on some systems add it to ~/.bash_completion and start a new
33
# shell) and bash's completion mechanism will know all about bzr's options!
34
35
# Known to work with bash 2.05a with programmable completion and extended
36
# pattern matching enabled (use 'shopt -s extglob progcomp' to enable
37
# these if they are not already enabled).
38
39
# Based originally on the svn bash completition script.
40
# Customized by Sven Wilhelm/Icecrash.com
0.27.2 by Martin von Gagern
First programmatic generation of completions.
41
# Adjusted for automatic generation by Martin von Gagern
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
42
0.27.17 by Martin von Gagern
Mention version and homepage in generated shell code comment.
43
# Generated using the bzr-bash-completion plugin version %(version)s.
44
# See https://launchpad.net/bzr-bash-completion for details.
45
0.27.6 by Martin von Gagern
Set extglob only temporarily for function definition. (LP #439827)
46
if shopt -q extglob; then
47
	_tmp_unset_extglob=""
48
else
49
	_tmp_unset_extglob="shopt -u extglob"
50
fi
0.27.5 by Martin von Gagern
Add shopt -s extglob progcomp. (LP #439827)
51
shopt -s extglob progcomp
0.27.7 by Martin von Gagern
Introduce --function-only option
52
"""
53
fun="""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
54
%(function_name)s ()
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
55
{
0.27.18 by Martin von Gagern
Add global options in all cases
56
	local cur cmds cmdOpts opt helpCmds optBase i globalOpts
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
57
58
	COMPREPLY=()
59
	cur=${COMP_WORDS[COMP_CWORD]}
60
0.27.2 by Martin von Gagern
First programmatic generation of completions.
61
	cmds='%(cmds)s'
0.27.18 by Martin von Gagern
Add global options in all cases
62
        globalOpts='%(global_options)s'
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
63
64
	if [[ $COMP_CWORD -eq 1 ]] ; then
0.27.18 by Martin von Gagern
Add global options in all cases
65
		COMPREPLY=( $( compgen -W "$cmds $globalOpts" -- $cur ) )
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
66
		return 0
67
	fi
68
69
	# if not typing an option, or if the previous option required a
70
	# parameter, then fallback on ordinary filename expansion
71
	helpCmds='help|--help|h|\?'
72
	if [[ ${COMP_WORDS[1]} != @($helpCmds) ]] && \
73
	   [[ "$cur" != -* ]] ; then
74
		return 0
75
	fi
76
77
	cmdOpts=
78
	case ${COMP_WORDS[1]} in
0.27.2 by Martin von Gagern
First programmatic generation of completions.
79
%(cases)s\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
80
	*)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
81
		cmdOpts='--help -h'
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
82
		;;
83
	esac
84
0.27.18 by Martin von Gagern
Add global options in all cases
85
	COMPREPLY=( $( compgen -W "$cmdOpts $globalOpts" -- $cur ) )
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
86
87
	return 0
88
}
0.27.7 by Martin von Gagern
Introduce --function-only option
89
"""
90
tail="""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
91
complete -F %(function_name)s -o default bzr
0.27.6 by Martin von Gagern
Set extglob only temporarily for function definition. (LP #439827)
92
$_tmp_unset_extglob
93
unset _tmp_unset_extglob
0.27.2 by Martin von Gagern
First programmatic generation of completions.
94
"""
95
0.28.1 by Martin von Gagern
Include negated switches as well, using a modified options parser.
96
def wrap_container(list, parser):
97
    def tweaked_add_option(*opts, **attrs):
98
        list.extend(opts)
99
    parser.add_option = tweaked_add_option
100
    return parser
101
102
def wrap_parser(list, parser):
103
    orig_add_option_group = parser.add_option_group
104
    def tweaked_add_option_group(*opts, **attrs):
105
        return wrap_container(list, orig_add_option_group(*opts, **attrs))
106
    parser.add_option_group = tweaked_add_option_group
107
    return wrap_container(list, parser)
108
0.27.7 by Martin von Gagern
Introduce --function-only option
109
def bash_completion_function(out, function_name="_bzr", function_only=False):
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
110
    cmds = []
0.27.2 by Martin von Gagern
First programmatic generation of completions.
111
    cases = ""
112
    reqarg = {}
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
113
0.27.18 by Martin von Gagern
Add global options in all cases
114
    re_switch = re.compile(r'\n(--[A-Za-z0-9-_]+)(?:, (-\S))?\s')
115
    help_text = help_topics.topic_registry.get_detail('global-options')
116
    global_options = set()
117
    for long, short in re_switch.findall(help_text):
118
        global_options.add(long)
119
        if short:
120
            global_options.add(short)
121
    global_options = " ".join(sorted(global_options))
122
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
123
    user_aliases = {} # dict from cmd name to set of user-defined alias names
124
    for alias, expansion in config.GlobalConfig().get_aliases().iteritems():
125
        for token in commands.shlex_split_unicode(expansion):
126
            if not token.startswith("-"):
127
                user_aliases.setdefault(token, set()).add(alias)
128
                break
129
0.27.10 by Martin von Gagern
Complete help topics.
130
    all_cmds = sorted(commands.all_command_names())
131
    for cmdname in all_cmds:
132
        cmd = commands.get_cmd_object(cmdname)
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
133
134
        # Find all aliases to the command; both cmd-defined and user-defined.
135
        # We assume a user won't override one command with a different one,
136
        # but will choose completely new names or add options to existing
137
        # ones while maintaining the actual command name unchanged.
138
        aliases = [cmdname]
139
        aliases.extend(cmd.aliases)
140
        aliases.extend(sorted([alias
141
                               for name in aliases
142
                               if name in user_aliases
143
                               for alias in user_aliases[name]
144
                               if alias not in aliases]))
145
        cases += "\t%s)\n" % "|".join(aliases)
146
        cmds.extend(aliases)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
147
        plugin = cmd.plugin_name()
148
        if plugin is not None:
149
            cases += "\t\t# plugin \"%s\"\n" % plugin
150
        opts = cmd.options()
0.27.9 by Martin von Gagern
Remove suppression of alternate switches for the same option.
151
        switches = []
0.27.2 by Martin von Gagern
First programmatic generation of completions.
152
        for optname in sorted(cmd.options()):
153
            opt = opts[optname]
0.28.1 by Martin von Gagern
Include negated switches as well, using a modified options parser.
154
            optswitches = []
155
            parser = option.get_optparser({optname: opt})
156
            parser = wrap_parser(optswitches, parser)
157
            optswitches[:] = []
158
            opt.add_option(parser, opt.short_name())
159
            switches.extend(optswitches)
0.27.10 by Martin von Gagern
Complete help topics.
160
        if 'help' == cmdname or 'help' in cmd.aliases:
161
            switches.extend(sorted(help_topics.topic_registry.keys()))
162
            switches.extend(all_cmds)
0.27.9 by Martin von Gagern
Remove suppression of alternate switches for the same option.
163
        cases += "\t\tcmdOpts='" + " ".join(switches) + "'\n"
164
        cases += "\t\t;;\n"
0.27.7 by Martin von Gagern
Introduce --function-only option
165
    if function_only:
166
        template = fun
167
    else:
168
        template = head + fun + tail
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
169
    out.write(template % {"cmds": " ".join(cmds),
0.27.2 by Martin von Gagern
First programmatic generation of completions.
170
                          "cases": cases,
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
171
                          "function_name": function_name,
0.27.18 by Martin von Gagern
Add global options in all cases
172
                          "version": __version__,
173
                          "global_options": global_options,
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
174
                          })
0.27.2 by Martin von Gagern
First programmatic generation of completions.
175
176
if __name__ == '__main__':
177
178
    import sys
179
    import locale
180
181
    locale.setlocale(locale.LC_ALL, '')
182
    plugin.load_plugins()
183
    commands.install_bzr_command_hooks()
184
    bash_completion_function(sys.stdout)