hgkw/keyword.py
changeset 255 c4f37735be9b
parent 254 474eccf2964f
child 258 dab61b56b6b8
equal deleted inserted replaced
254:474eccf2964f 255:c4f37735be9b
   164         self.ui = ui
   164         self.ui = ui
   165         self.repo = repo
   165         self.repo = repo
   166         self.t = expand or None
   166         self.t = expand or None
   167         self.path = path
   167         self.path = path
   168         self.node = node
   168         self.node = node
       
   169         self.debug = self.ui.debugflag
   169 
   170 
   170         kwmaps = self.ui.configitems('keywordmaps')
   171         kwmaps = self.ui.configitems('keywordmaps')
   171         if kwmaps: # override default templates
   172         if kwmaps: # override default templates
   172             kwmaps = [(k, templater.parsestring(v, quoted=False))
   173             kwmaps = [(k, templater.parsestring(v, quoted=False))
   173                       for (k, v) in kwmaps]
   174                       for (k, v) in kwmaps]
   205             for f in files:
   206             for f in files:
   206                 self.repo.dirstate.normal(f)
   207                 self.repo.dirstate.normal(f)
   207         except AttributeError:
   208         except AttributeError:
   208             self.repo.dirstate.update(files, 'n')
   209             self.repo.dirstate.update(files, 'n')
   209 
   210 
   210     def _ctxnode(self, node):
       
   211         '''Obtains missing node from file context.'''
       
   212         if not self.node:
       
   213             c = context.filectx(self.repo, self.path, fileid=node)
       
   214             self.node = c.node()
       
   215 
       
   216     def _kwsub(self, mobj):
   211     def _kwsub(self, mobj):
   217         '''Substitutes keyword using corresponding template.'''
   212         '''Substitutes keyword using corresponding template.'''
   218         kw = mobj.group(1)
   213         kw = mobj.group(1)
   219         self.t.use_template(self.templates[kw])
   214         self.t.use_template(self.templates[kw])
   220         self.ui.pushbuffer()
   215         self.ui.pushbuffer()
   221         self.t.show(changenode=self.node, root=self.repo.root, file=self.path)
   216         self.t.show(changenode=self.node, root=self.repo.root, file=self.path)
   222         keywordsub = templater.firstline(self.ui.popbuffer())
   217         keywordsub = templater.firstline(self.ui.popbuffer())
   223         return '$%s: %s $' % (kw, keywordsub)
   218         return '$%s: %s $' % (kw, keywordsub)
   224 
   219 
       
   220     def _substitute(self, node, data, subfunc):
       
   221         '''Obtains node if missing.
       
   222         Ensures consistent templates regardless of ui.debugflag.
       
   223         Calls given substitution function.'''
       
   224         if not self.node:
       
   225             c = context.filectx(self.repo, self.path, fileid=node)
       
   226             self.node = c.node()
       
   227         self.ui.debugflag = False
       
   228         result = subfunc(self._kwsub, data)
       
   229         self.ui.debugflag = self.debug
       
   230         return result
       
   231 
   225     def expand(self, node, data):
   232     def expand(self, node, data):
   226         '''Returns data with keywords expanded.'''
   233         '''Returns data with keywords expanded.'''
   227         if util.binary(data):
   234         if util.binary(data):
   228             return data
   235             return data
   229         self._ctxnode(node)
   236         return self._substitute(node, data, self.re_kw.sub)
   230         return self.re_kw.sub(self._kwsub, data)
       
   231 
   237 
   232     def process(self, node, data):
   238     def process(self, node, data):
   233         '''Returns a tuple: data, count.
   239         '''Returns a tuple: data, count.
   234         Count is number of keywords/keyword substitutions.
   240         Count is number of keywords/keyword substitutions.
   235         Keywords in data are expanded, if templater was initialized.'''
   241         Keywords in data are expanded, if templater was initialized.'''
   236         if util.binary(data):
   242         if util.binary(data):
   237             return data, None
   243             return data, None
   238         if self.t:
   244         if self.t:
   239             self._ctxnode(node)
   245             return self._substitute(node, data, self.re_kw.subn)
   240             return self.re_kw.subn(self._kwsub, data)
       
   241         return data, self.re_kw.search(data)
   246         return data, self.re_kw.search(data)
   242 
   247 
   243     def shrink(self, text):
   248     def shrink(self, text):
   244         '''Returns text with all keyword substitutions removed.'''
   249         '''Returns text with all keyword substitutions removed.'''
   245         if util.binary(text):
   250         if util.binary(text):