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