From 102dc8787e6aef8400101f84517ab085bcf4f560 Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Sat, 3 Apr 2010 22:48:57 -0400 Subject: [PATCH] (using a hack) improve support for buffers with tabs --HG-- branch : pmacs2 --- buffer/__init__.py | 55 ++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/buffer/__init__.py b/buffer/__init__.py index f00cc58..eb03ea6 100644 --- a/buffer/__init__.py +++ b/buffer/__init__.py @@ -74,6 +74,7 @@ class Buffer(object): self.highlights = {} self.settings = {} self.indentlvl = 4 + self.usetabs = False self.writetabs = False self.metadata = {} @@ -226,16 +227,12 @@ class Buffer(object): return len(self.lines) def make_string(self): if self.writetabs: - lines = [] - for line in self.lines: - i = 0 - while i < len(line) and line[i] == ' ': - i += 1 - j, k = i // self.indentlvl, i % self.indentlvl - lines.append((u'\t' * j) + (u' ' * k) + line[i:]) - return self.nl.join(lines) + # reverse the "horrible hack" mentioned below when writing + # out the string. + lines2 = [s.replace('\t \t', '\t') for s in self.lines] else: - return self.nl.join(self.lines) + lines2 = self.lines + return self.nl.join(self.lines) # methods to be overridden by subclasses def name(self): @@ -625,14 +622,6 @@ class FileBuffer(Buffer): def store_checksum(self, data): self.checksum = hasher(data) - def decode(self, data, codec): - try: - data2 = data.decode(codec).replace("\t", " ") - self.codec = codec - return data2 - except UnicodeDecodeError: - return None - def read(self): if self.path_exists(): f = self._open_file_r() @@ -653,7 +642,17 @@ class FileBuffer(Buffer): data = data.decode(self.codec) - if '\t' in data: self.writetabs = True + if '\t' in data: + self.writetabs = True + self.usetabs = True + + # OK, this is a horrible, horrible hack. because the "one unicode + # char occupies one screen position" assumption is built into the + # syntax highlighting and render strings at a pretty low level, we + # have to "pad out" the tab to make it take up enough physical + # space. + data = data.replace('\t', '\t \t') + self.nl = self._detect_nl_type(data) if '\x00' in data[:8192]: @@ -693,15 +692,10 @@ class FileBuffer(Buffer): try: - data = self.make_string() - if self.windows[0].mode.savetabs: - data = data.replace(" ", "\t") - data = self.write_filter(data.encode(self.codec)) - - f2 = self._open_file_w(self.path, preserve=False) + data = self.write_filter(self.make_string().encode(self.codec)) + f2 = self._open_file_w(self.path, preserve=False) f2.write(data) f2.close() - #except Exception, e: except NameError, e: if exists: shutil.copyfile(temp_path, self.path) raise e @@ -713,17 +707,6 @@ class FileBuffer(Buffer): self.path = path self.save() -#class AesBuffer(FileBuffer): -# btype = 'aesfile' -# def __init__(self, path, password, name=None): -# '''fb = FileBuffer(path)''' -# FileBuffer.__init__(self, path, name) -# self.password = password -# def read_filter(self, data): -# return aes.decrypt_data(data, self.password) -# def write_filter(self, data): -# return aes.encrypt_data(data, self.password) - class Binary32Buffer(FileBuffer): btype = 'bin32file' grouppad = 2