##// END OF EJS Templates
dispatch: support for $ escaping in shell-alias definition...
Roman Sokolov -
r13392:777cef34 default
parent child Browse files
Show More
@@ -221,15 +221,17 b' class cmdalias(object):'
221 def fn(ui, *args):
221 def fn(ui, *args):
222 env = {'HG_ARGS': ' '.join((self.name,) + args)}
222 env = {'HG_ARGS': ' '.join((self.name,) + args)}
223 def _checkvar(m):
223 def _checkvar(m):
224 if int(m.groups()[0]) <= len(args):
224 if m.groups()[0] == '$':
225 return m.group()
226 elif int(m.groups()[0]) <= len(args):
225 return m.group()
227 return m.group()
226 else:
228 else:
227 return ''
229 return ''
228 cmd = re.sub(r'\$(\d+)', _checkvar, self.definition[1:])
230 cmd = re.sub(r'\$(\d+|\$)', _checkvar, self.definition[1:])
229 replace = dict((str(i + 1), arg) for i, arg in enumerate(args))
231 replace = dict((str(i + 1), arg) for i, arg in enumerate(args))
230 replace['0'] = self.name
232 replace['0'] = self.name
231 replace['@'] = ' '.join(args)
233 replace['@'] = ' '.join(args)
232 cmd = util.interpolate(r'\$', replace, cmd)
234 cmd = util.interpolate(r'\$', replace, cmd, escape_prefix=True)
233 return util.system(cmd, environ=env)
235 return util.system(cmd, environ=env)
234 self.fn = fn
236 self.fn = fn
235 return
237 return
@@ -1502,7 +1502,7 b' except NameError:'
1502 return False
1502 return False
1503 return True
1503 return True
1504
1504
1505 def interpolate(prefix, mapping, s, fn=None):
1505 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False):
1506 """Return the result of interpolating items in the mapping into string s.
1506 """Return the result of interpolating items in the mapping into string s.
1507
1507
1508 prefix is a single character string, or a two character string with
1508 prefix is a single character string, or a two character string with
@@ -1511,9 +1511,20 b' def interpolate(prefix, mapping, s, fn=N'
1511
1511
1512 fn is an optional function that will be applied to the replacement text
1512 fn is an optional function that will be applied to the replacement text
1513 just before replacement.
1513 just before replacement.
1514
1515 escape_prefix is an optional flag that allows using doubled prefix for
1516 its escaping.
1514 """
1517 """
1515 fn = fn or (lambda s: s)
1518 fn = fn or (lambda s: s)
1516 r = re.compile(r'%s(%s)' % (prefix, '|'.join(mapping.keys())))
1519 patterns = '|'.join(mapping.keys())
1520 if escape_prefix:
1521 patterns += '|' + prefix
1522 if len(prefix) > 1:
1523 prefix_char = prefix[1:]
1524 else:
1525 prefix_char = prefix
1526 mapping[prefix_char] = prefix_char
1527 r = re.compile(r'%s(%s)' % (prefix, patterns))
1517 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
1528 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
1518
1529
1519 def getport(port):
1530 def getport(port):
General Comments 0
You need to be logged in to leave comments. Login now