initial latin-1 support

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2009-07-11 21:57:41 -04:00
parent f747b06318
commit b3c6d6d717
3 changed files with 30 additions and 13 deletions

View File

@ -220,7 +220,7 @@ class Buffer(object):
while i < len(line) and line[i] == ' ': while i < len(line) and line[i] == ' ':
i += 1 i += 1
j, k = i // self.indentlvl, i % self.indentlvl j, k = i // self.indentlvl, i % self.indentlvl
lines.append(('\t' * j) + (' ' * k) + line[i:]) lines.append((u'\t' * j) + (u' ' * k) + line[i:])
return self.nl.join(lines) return self.nl.join(lines)
else: else:
return self.nl.join(self.lines) return self.nl.join(self.lines)
@ -565,6 +565,7 @@ class FileBuffer(Buffer):
self.path = os.path.realpath(path) self.path = os.path.realpath(path)
self.checksum = None self.checksum = None
self.bytemark = '' self.bytemark = ''
self.codec = 'utf-8'
if name is None: if name is None:
self._name = os.path.basename(self.path) self._name = os.path.basename(self.path)
else: else:
@ -612,6 +613,15 @@ class FileBuffer(Buffer):
return os.path.exists(self.path) return os.path.exists(self.path)
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()
@ -631,12 +641,16 @@ class FileBuffer(Buffer):
self.nl = self._detect_nl_type(data) self.nl = self._detect_nl_type(data)
data = self.read_filter(data) data = self.read_filter(data)
try:
data = data.decode('utf-8') if '\x00' in data[:8192]:
data = data.replace("\t", " ")
return data
except UnicodeDecodeError:
raise BinaryDataException("binary files are not supported") raise BinaryDataException("binary files are not supported")
for codec in ('utf-8', 'latin-1'):
data2 = self.decode(data, codec)
if data2 is not None: return data2
raise BinaryDataException("binary files are not supported")
def open(self): def open(self):
data = self.read() data = self.read()
self.lines = data.split(self.nl) self.lines = data.split(self.nl)
@ -671,7 +685,8 @@ class FileBuffer(Buffer):
try: try:
data = self.make_string() data = self.make_string()
if self.windows[0].mode.savetabs: if self.windows[0].mode.savetabs:
data = data.replace(" ", "\t") data = data.replace(" ", "\t").encode(self.codec)
data = self.write_filter(data) data = self.write_filter(data)
f2 = self._open_file_w(self.path, preserve=False) f2 = self._open_file_w(self.path, preserve=False)

View File

@ -95,8 +95,8 @@ class ArchiveBuffer(DirBuffer):
['.tar.bz2', 'tar xfj %(archive)r -C %(dir)r'], ['.tar.bz2', 'tar xfj %(archive)r -C %(dir)r'],
#['.gz', 'gunzip %(archive)r'], #['.gz', 'gunzip %(archive)r'],
#['.bz2', 'bunzip2 %(archive)r'], #['.bz2', 'bunzip2 %(archive)r'],
#['.zip', 'unzip %(archive)r'], ['.zip', 'unzip -qq %(archive)r -d %(dir)r'],
#['.jar', 'unzip %(archive)r'], ['.jar', 'unzip -qq %(archive)r -d %(dir)r'],
] ]
def __init__(self, path, name=None): def __init__(self, path, name=None):
self.archive = os.path.realpath(path) self.archive = os.path.realpath(path)

View File

@ -71,12 +71,14 @@ class OpenArchive(Method):
b.open() b.open()
Window(b, a, height=0, width=0, mode_name='dir') Window(b, a, height=0, width=0, mode_name='dir')
a.add_buffer(b) a.add_buffer(b)
a.methods['switch-buffer'].execute(w, buffername=b.name()) a.methods['switch-buffer'].execute(w, buffername=b.name())
if b.btype != 'archive': if b.btype != 'archive': return
return
names = [x for x in b.get_names() if x != '.' and x != '..'] names = [x for x in b.get_names() if x != '.' and x != '..']
if len(names) == 1: if len(names) == 1:
path2 = os.path.join(b.path, names[0]) path2 = os.path.join(b.path, names[0])
if os.path.isdir(path2):
a.methods['open-file'].execute(w, filename=path2) a.methods['open-file'].execute(w, filename=path2)
class DirRefresh(Method): class DirRefresh(Method):