author | Chris Winter <elwintro@gmail.com> |
Thu, 13 Nov 2008 10:11:32 +0100 | |
changeset 7360 | 42f1b8cb9a60 |
parent 7213 | b4c035057d34 |
child 7643 | 9a1ea6587557 |
permissions | -rw-r--r-- |
4801 | 1 |
# Copyright (C) 2007 Brendan Cully <brendan@kublai.com> |
2 |
# This file is published under the GNU GPL. |
|
3 |
||
4 |
'''allow user-defined command aliases |
|
5 |
||
6 |
To use, create entries in your hgrc of the form |
|
7 |
||
8 |
[alias] |
|
9 |
mycmd = cmd --args |
|
10 |
''' |
|
11 |
||
12 |
from mercurial.cmdutil import findcmd, UnknownCommand, AmbiguousCommand |
|
13 |
from mercurial import commands |
|
6954
f7d545a866e8
i18n: mark strings for translation in alias extension
Martin Geisler <mg@daimi.au.dk>
parents:
5178
diff
changeset
|
14 |
from mercurial.i18n import _ |
4801 | 15 |
|
16 |
cmdtable = {} |
|
17 |
||
18 |
class RecursiveCommand(Exception): pass |
|
19 |
||
20 |
class lazycommand(object): |
|
21 |
'''defer command lookup until needed, so that extensions loaded |
|
22 |
after alias can be aliased''' |
|
23 |
def __init__(self, ui, name, target): |
|
24 |
self._ui = ui |
|
25 |
self._name = name |
|
26 |
self._target = target |
|
27 |
self._cmd = None |
|
28 |
||
29 |
def __len__(self): |
|
30 |
self._resolve() |
|
31 |
return len(self._cmd) |
|
32 |
||
33 |
def __getitem__(self, key): |
|
34 |
self._resolve() |
|
35 |
return self._cmd[key] |
|
36 |
||
37 |
def __iter__(self): |
|
38 |
self._resolve() |
|
39 |
return self._cmd.__iter__() |
|
40 |
||
41 |
def _resolve(self): |
|
42 |
if self._cmd is not None: |
|
43 |
return |
|
44 |
||
45 |
try: |
|
7213
b4c035057d34
findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents:
6954
diff
changeset
|
46 |
self._cmd = findcmd(self._target, commands.table, False)[1] |
4801 | 47 |
if self._cmd == self: |
48 |
raise RecursiveCommand() |
|
49 |
if self._target in commands.norepo.split(' '): |
|
50 |
commands.norepo += ' %s' % self._name |
|
51 |
return |
|
52 |
except UnknownCommand: |
|
6954
f7d545a866e8
i18n: mark strings for translation in alias extension
Martin Geisler <mg@daimi.au.dk>
parents:
5178
diff
changeset
|
53 |
msg = _('*** [alias] %s: command %s is unknown') % \ |
4801 | 54 |
(self._name, self._target) |
55 |
except AmbiguousCommand: |
|
6954
f7d545a866e8
i18n: mark strings for translation in alias extension
Martin Geisler <mg@daimi.au.dk>
parents:
5178
diff
changeset
|
56 |
msg = _('*** [alias] %s: command %s is ambiguous') % \ |
4801 | 57 |
(self._name, self._target) |
58 |
except RecursiveCommand: |
|
6954
f7d545a866e8
i18n: mark strings for translation in alias extension
Martin Geisler <mg@daimi.au.dk>
parents:
5178
diff
changeset
|
59 |
msg = _('*** [alias] %s: circular dependency on %s') % \ |
4801 | 60 |
(self._name, self._target) |
61 |
def nocmd(*args, **opts): |
|
62 |
self._ui.warn(msg + '\n') |
|
63 |
return 1 |
|
64 |
nocmd.__doc__ = msg |
|
65 |
self._cmd = (nocmd, [], '') |
|
66 |
commands.norepo += ' %s' % self._name |
|
67 |
||
68 |
def uisetup(ui): |
|
69 |
for cmd, target in ui.configitems('alias'): |
|
70 |
if not target: |
|
6954
f7d545a866e8
i18n: mark strings for translation in alias extension
Martin Geisler <mg@daimi.au.dk>
parents:
5178
diff
changeset
|
71 |
ui.warn(_('*** [alias] %s: no definition\n') % cmd) |
4801 | 72 |
continue |
73 |
args = target.split(' ') |
|
74 |
tcmd = args.pop(0) |
|
75 |
if args: |
|
4964
a28661788f2f
test-alias: Removed fallback to parentui, no longer needed since 10afa3fab6b4
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4808
diff
changeset
|
76 |
ui.setconfig('defaults', cmd, ' '.join(args)) |
4801 | 77 |
cmdtable[cmd] = lazycommand(ui, cmd, tcmd) |