better tab support

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-09-20 14:54:54 +00:00
parent b83e0049e0
commit bbe7bee4c5
3 changed files with 35 additions and 3 deletions

View File

@ -55,6 +55,7 @@ class Buffer(object):
self.modified = False self.modified = False
self.highlights = {} self.highlights = {}
self.indentlvl = 4 self.indentlvl = 4
self.writetabs = False
def _detect_nl_type(self, data): def _detect_nl_type(self, data):
mac_c = len(self.mac_re.findall(data)) mac_c = len(self.mac_re.findall(data))
@ -192,7 +193,17 @@ class Buffer(object):
def num_lines(self): def num_lines(self):
return len(self.lines) return len(self.lines)
def make_string(self): 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 # methods to be overridden by subclasses
def name(self): def name(self):
@ -575,6 +586,8 @@ class FileBuffer(Buffer):
if self.path_exists(): if self.path_exists():
f = self._open_file_r() f = self._open_file_r()
data = f.read() data = f.read()
if '\t' in data:
self.writetabs = True
f.close() f.close()
self.store_checksum(data) self.store_checksum(data)
else: else:

View File

@ -121,3 +121,13 @@ class SaveBuffer(Method):
w.set_error("Wrote %s" % (w.buffer.path)) w.set_error("Wrote %s" % (w.buffer.path))
else: else:
w.set_error("(No changes need to be saved)") 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")

View File

@ -91,7 +91,7 @@ class Fundamental(Handler):
config = {} config = {}
actions = [] actions = []
completers = {} 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 # margin/line numbering
show_line_numbers = False show_line_numbers = False
@ -276,6 +276,13 @@ class Fundamental(Handler):
return "Bot" return "Bot"
else: else:
return "%2d%%" % (w.first.y*100 / len(w.buffer.lines)) 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): def _get_mark(self):
w = self.window w = self.window
if w.mark: if w.mark:
@ -285,14 +292,16 @@ class Fundamental(Handler):
def get_status_names(self): def get_status_names(self):
w = self.window w = self.window
c = w.logical_cursor() c = w.logical_cursor()
return { d = {
'bname': w.buffer.name(), 'bname': w.buffer.name(),
'mname': self.name(), 'mname': self.name(),
'flag': self._get_flag(), 'flag': self._get_flag(),
'perc': self._get_perc(), 'perc': self._get_perc(),
'indent': self._get_indent(),
'cursor': '(%d,%d)' % (c.y + 1, c.x + 1), 'cursor': '(%d,%d)' % (c.y + 1, c.x + 1),
'mark': self._get_mark(), 'mark': self._get_mark(),
} }
return d
def get_status_bar(self): def get_status_bar(self):
names = self.get_status_names() names = self.get_status_names()
return self.format % names return self.format % names