From 1b8c01e3191de2171abf47e7b65a66079e8a4d1c Mon Sep 17 00:00:00 2001 From: moculus Date: Sat, 29 Mar 2008 14:07:22 +0000 Subject: [PATCH] svn-blame2 has color!!! --HG-- branch : pmacs2 --- application.py | 11 +++++++++++ buffer.py | 15 ++++++++------- method/svn.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/application.py b/application.py index 2ba1ff3..6208483 100755 --- a/application.py +++ b/application.py @@ -353,6 +353,17 @@ class Application(object): self.add_buffer(b) if switch_to: self.switch_buffer(b) + def color_data_buffer(self, name, data, switch_to=True, modename='colortext'): + if self.has_buffer_name(name): + b = self.bufferlist.buffer_names[name] + self.remove_buffer(b) + b = buffer.ColorDataBuffer(name, data) + if modename is not None: + b.modename = modename + window.Window(b, self, height=0, width=0) + self.add_buffer(b) + if switch_to: + self.switch_buffer(b) def get_buffer_by_path(self, path): return self.bufferlist.get_buffer_by_path(path) def has_buffer_name(self, name): diff --git a/buffer.py b/buffer.py index 3c8cea7..99e761d 100644 --- a/buffer.py +++ b/buffer.py @@ -646,11 +646,12 @@ class ColorHighlighter(highlight.Highlighter): def __init__(self): self.tokens = [] def append_token(self, y, x, s, color): - s = s.replace('\\[', '[') - s = s.replace('\\]', ']') - s = s.replace('\\\\', '\\') - t = lex.Token('color_data', None, y, x, s, color) + s2 = s.replace('\\[', '[') + s2 = s2.replace('\\]', ']') + s2 = s2.replace('\\\\', '\\') + t = lex.Token('color_data', None, y, x, s2, color) self.tokens[y].append(t) + return len(s) - len(s2) def highlight(self, lines): if self.tokens: return @@ -666,13 +667,13 @@ class ColorHighlighter(highlight.Highlighter): if m: (j, k) = (m.start(), m.end()) if j > i: - self.append_token(y, i - offset, line[i:j], c) + offset += self.append_token(y, i - offset, line[i:j], c) fields = m.group(1).split(':') c = [self.color_map.get(x, x) for x in fields] offset += k - j i = k else: - self.append_token(y, i - offset, line[i:], c) + offset += self.append_token(y, i - offset, line[i:], c) break class ColorDataBuffer(DataBuffer): @@ -707,7 +708,7 @@ ABOUT_DATA = ''' [y:d:*]***** [c:d:*]pmacs[d:d:*] is available to you under the [c:d:*]GNU General Public License v2 [y:d:*]***** [y:d:*]***** [d:d:*]to see all available commands use: [c:d:*]C-c M-h [b:d:*](show-bindings-buffer) - +\\[foo\\]x\\[bar\\]x\\\\x\\[foo\\]x\\[[y:d]bar\\]x\\[\\]x\\[[r:d:*]\\] [r:d:*]================================================================================ ''' class AboutBuffer(ColorDataBuffer): diff --git a/method/svn.py b/method/svn.py index 96df1c2..eaf60fa 100644 --- a/method/svn.py +++ b/method/svn.py @@ -1,7 +1,7 @@ import os, commands, re, sets, tempfile from subprocess import Popen, PIPE, STDOUT -import buffer, default, dirutil, regex, util, window +import buffer, default, dirutil, lex, regex, util, window from point import Point from method import DATATYPES, Method, Argument @@ -182,3 +182,51 @@ class SvnBlame(Method): w.application.data_buffer("*Blame*", data, switch_to=True, modename='blame') else: w.set_error("There was an error (%s)" % (status)) + +class SvnBlame2(Method): + '''show blame output for the current version in SVN''' + line_re = re.compile('^ *(\d+) *([a-zA-Z0-9_]+) *([-0-9]+) *([:0-9]+) *(-\d{4}) *\(([^\)]+)\) (.*)\n$') + def _execute(self, w, **vargs): + if not hasattr(w.buffer, 'path'): + w.set_error("Buffer has no corresponding file") + return + + cmd = ("/usr/bin/svn", 'blame', '-v', w.buffer.path) + pipe = Popen(cmd, stdin=PIPE, stdout=PIPE) + + linetokens = [] + for line in pipe.stdout: + m = self.line_re.match(line) + if not m: + raise Exception, line + (rev, user, date, t, tz, vdate, content) = m.groups() + linetokens.append([rev, user, date, content, []]) + status = pipe.wait() >> 8 + + lines = [x[3] for x in linetokens] + if w.mode.grammar: + lexer = lex.Lexer(w.mode, w.mode.grammar) + lextokens = [[] for l in lines] + for t in lexer.lex(lines): + linetokens[t.y][4].append(t) + + lines = [] + for linetoken in linetokens: + (rev, user, date, content, lextokens) = linetoken + prefix = '[b:d:*]%-4s [c:d:*]%-10s [b:d:*]%10s[d:d:*]' % (rev, user, date) + if lextokens: + suffixes = [] + for lt in lextokens: + s = lt.string.replace('\\', '\\\\') + s = s.replace('[', '\\[').replace(']', '\\]') + suffixes.append('[%s:%s:*]%s' % (lt.color[0], lt.color[1], s)) + suffix = ''.join(suffixes) + else: + suffix = content + lines.append('%s %s' % (prefix, suffix)) + data = ''.join(lines) + + if status == 0: + w.application.color_data_buffer("*Blame*", data, switch_to=True) + else: + w.set_error("There was an error (%s)" % (status))