(using a hack) improve support for buffers with tabs

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2010-04-03 22:48:57 -04:00
parent 215a53dbee
commit 102dc8787e
1 changed files with 19 additions and 36 deletions

View File

@ -74,6 +74,7 @@ class Buffer(object):
self.highlights = {} self.highlights = {}
self.settings = {} self.settings = {}
self.indentlvl = 4 self.indentlvl = 4
self.usetabs = False
self.writetabs = False self.writetabs = False
self.metadata = {} self.metadata = {}
@ -226,16 +227,12 @@ class Buffer(object):
return len(self.lines) return len(self.lines)
def make_string(self): def make_string(self):
if self.writetabs: if self.writetabs:
lines = [] # reverse the "horrible hack" mentioned below when writing
for line in self.lines: # out the string.
i = 0 lines2 = [s.replace('\t \t', '\t') for s in self.lines]
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)
else: else:
return self.nl.join(self.lines) lines2 = self.lines
return self.nl.join(self.lines)
# methods to be overridden by subclasses # methods to be overridden by subclasses
def name(self): def name(self):
@ -625,14 +622,6 @@ class FileBuffer(Buffer):
def store_checksum(self, data): def store_checksum(self, data):
self.checksum = hasher(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): def read(self):
if self.path_exists(): if self.path_exists():
f = self._open_file_r() f = self._open_file_r()
@ -653,7 +642,17 @@ class FileBuffer(Buffer):
data = data.decode(self.codec) 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) self.nl = self._detect_nl_type(data)
if '\x00' in data[:8192]: if '\x00' in data[:8192]:
@ -693,15 +692,10 @@ class FileBuffer(Buffer):
try: try:
data = self.make_string() data = self.write_filter(self.make_string().encode(self.codec))
if self.windows[0].mode.savetabs: f2 = self._open_file_w(self.path, preserve=False)
data = data.replace(" ", "\t")
data = self.write_filter(data.encode(self.codec))
f2 = self._open_file_w(self.path, preserve=False)
f2.write(data) f2.write(data)
f2.close() f2.close()
#except Exception, e:
except NameError, e: except NameError, e:
if exists: shutil.copyfile(temp_path, self.path) if exists: shutil.copyfile(temp_path, self.path)
raise e raise e
@ -713,17 +707,6 @@ class FileBuffer(Buffer):
self.path = path self.path = path
self.save() 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): class Binary32Buffer(FileBuffer):
btype = 'bin32file' btype = 'bin32file'
grouppad = 2 grouppad = 2