pmacs3/tab_xml.py

49 lines
1.6 KiB
Python

import tab
class XMLTabber(tab.TokenStackTabber):
close_tags = {'}': '{',
')': '(',
']': '['}
def stack_append_const(self, c, n):
if self.tab_stack[-1][0] != c:
self.stack_append((c, self.tab_stack[-1][1] + n))
def stack_pop_const(self, *c_args):
if self.tab_stack[-1][0] in c_args:
self.stack_pop()
def base_indentation_level(self, y):
return False
def handle_token(self, prev_token, token, next_token, y=None):
buffer = self.mode.window.buffer
name = token.name
s = token.string
if name == 'opentag':
if next_token is None:
x = len(s) + 2
else:
x = next_token.start - token.start + 1
# x is an offset from the current indention level
self.stack_append_const('cont', x)
elif name == 'gtc':
self.stack_pop_const('cont')
if prev_token is None:
self.line_depth = self.tab_stack[-1][1]
elif name == 'gt':
self.stack_pop_const('cont')
if prev_token is None:
self.line_depth = self.tab_stack[-1][1]
if self.tab_stack[-1][0] == 'close':
self.stack_pop_const('close')
else:
self.stack_append(('tag', self.tab_stack[-1][1] + 4))
elif name == 'ltc':
self.stack_pop_const('cont')
self.stack_pop_const('tag')
l = self.tab_stack[-1][1]
self.stack_append(('close', l))
if prev_token is None:
self.line_depth = l