From bbe7bee4c51b9526fe41f3ce15f6736aa075ae4a Mon Sep 17 00:00:00 2001 From: moculus Date: Sat, 20 Sep 2008 14:54:54 +0000 Subject: [PATCH] better tab support --HG-- branch : pmacs2 --- buffer.py | 15 ++++++++++++++- method/buffers.py | 10 ++++++++++ mode/__init__.py | 13 +++++++++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/buffer.py b/buffer.py index 76a6b68..355a2bc 100644 --- a/buffer.py +++ b/buffer.py @@ -55,6 +55,7 @@ class Buffer(object): self.modified = False self.highlights = {} self.indentlvl = 4 + self.writetabs = False def _detect_nl_type(self, data): mac_c = len(self.mac_re.findall(data)) @@ -192,7 +193,17 @@ class Buffer(object): def num_lines(self): return len(self.lines) def make_string(self): - return self.nl.join(self.lines) + 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(('\t' * j) + (' ' * k) + line[i:]) + return self.nl.join(lines) + else: + return self.nl.join(self.lines) # methods to be overridden by subclasses def name(self): @@ -575,6 +586,8 @@ class FileBuffer(Buffer): if self.path_exists(): f = self._open_file_r() data = f.read() + if '\t' in data: + self.writetabs = True f.close() self.store_checksum(data) else: diff --git a/method/buffers.py b/method/buffers.py index 293ba60..eed89ea 100644 --- a/method/buffers.py +++ b/method/buffers.py @@ -121,3 +121,13 @@ class SaveBuffer(Method): w.set_error("Wrote %s" % (w.buffer.path)) else: w.set_error("(No changes need to be saved)") + +class ToggleTabs(Method): + '''Toggle whether to write tabs out or not (defaults to false)''' + def _execute(self, w, **vargs): + b = w.buffer + b.writetabs = not b.writetabs + if b.writetabs: + w.set_error("Buffer will translate %d spaces to a tab" % b.indentlvl) + else: + w.set_error("Buffer will not write tabs") diff --git a/mode/__init__.py b/mode/__init__.py index 4d761f0..d2a0c3c 100644 --- a/mode/__init__.py +++ b/mode/__init__.py @@ -91,7 +91,7 @@ class Fundamental(Handler): config = {} actions = [] completers = {} - format = "%(flag)s %(bname)-18s (%(mname)s) %(cursor)s/%(mark)s %(perc)s" + format = "%(flag)s %(bname)-18s (%(mname)s) %(indent)s %(cursor)s/%(mark)s %(perc)s" # margin/line numbering show_line_numbers = False @@ -276,6 +276,13 @@ class Fundamental(Handler): return "Bot" else: return "%2d%%" % (w.first.y*100 / len(w.buffer.lines)) + def _get_indent(self): + b = self.window.buffer + if b.writetabs: + t = '[t]' + else: + t = '[ ]' + return '%d%s' % (b.indentlvl, t) def _get_mark(self): w = self.window if w.mark: @@ -285,14 +292,16 @@ class Fundamental(Handler): def get_status_names(self): w = self.window c = w.logical_cursor() - return { + d = { 'bname': w.buffer.name(), 'mname': self.name(), 'flag': self._get_flag(), 'perc': self._get_perc(), + 'indent': self._get_indent(), 'cursor': '(%d,%d)' % (c.y + 1, c.x + 1), 'mark': self._get_mark(), } + return d def get_status_bar(self): names = self.get_status_names() return self.format % names