pmacs3/window.py

798 lines
27 KiB
Python
Raw Normal View History

2007-10-21 20:55:43 -04:00
import os.path, string
2008-04-06 22:31:13 -04:00
import color, highlight, regex
2007-10-21 20:55:43 -04:00
from point import Point
from render import RenderString
2007-10-21 20:55:43 -04:00
# note about the cursor: the cursor position will insert in front of the
# character it highlights. to this end, it needs to be able to highlight behind
# the last character on a line. thus, the x coordinate of the (logical) cursor
# can equal the length of lines[y], even though lines[y][x] throws an index
# error. both buffer and window need to be aware of this possibility.
#
# furthermore, when moving down from a long line to a shorter one, the cursor
# will jump in to the end of the shorter line. however, moving back up will
# jump back out to its previous position. in these cases the cursor stores the
# original x value, but the logical cursor function maps x to the "last
# character on the line" for the shorter line. any line movement or actions on
# the shorter line will "set" the cursor to its position on the shorter line.
2007-10-21 20:55:43 -04:00
class Window(object):
margins_visible = False
def __init__(self, b, a, height=24, width=80, mode_name=None):
2008-11-08 10:30:04 -05:00
self.buffer = b
self.application = a
self.height = height
self.width = width
self.first = Point(0, 0)
self.last = None
self.cursor = Point(0, 0)
self.mark = None
self.active_point = None
self.input_line = ""
2007-10-21 20:55:43 -04:00
if mode_name is not None:
pass
elif hasattr(b, 'modename') and b.modename is not None:
mode_name = b.modename
elif b.btype == 'mini':
2007-10-21 20:55:43 -04:00
mode_name = 'mini'
elif b.btype == 'console':
2007-10-21 20:55:43 -04:00
mode_name = "fundamental"
elif b.btype == 'dir':
2007-10-21 20:55:43 -04:00
mode_name = 'dir'
elif hasattr(b, 'path'):
path = b.path
2007-10-21 20:55:43 -04:00
basename = os.path.basename(path)
ext = self._get_path_ext(path)
2007-10-21 20:55:43 -04:00
if path in self.application.mode_paths:
mode_name = self.application.mode_paths[path]
elif basename in self.application.mode_basenames:
mode_name = self.application.mode_basenames[basename]
elif ext in self.application.mode_extensions:
mode_name = self.application.mode_extensions[ext]
elif len(b.lines) > 0 and \
b.lines[0].startswith('#!'):
line = b.lines[0]
2007-10-21 20:55:43 -04:00
for word in self.application.mode_detection:
if word in line:
mode_name = self.application.mode_detection[word]
if mode_name is None:
mode_name = "fundamental"
m = self.application.modes[mode_name](self)
self.set_mode(m)
b.add_window(self)
2007-10-21 20:55:43 -04:00
# private method used in window constructor
def _get_path_ext(self, path):
name = os.path.basename(path).lower()
tokens = name.split('.')
if len(tokens) > 2 and tokens[-1] in ('gz', 'in', 'zip'):
return '.%s.%s' % (tokens[-2], tokens[-1])
else:
return os.path.splitext(path)[1].lower()
# some useful pass-through to application
def set_error(self, s):
self.application.set_error(s)
def clear_error(self):
self.application.clear_error()
# mode stuff
def set_mode(self, m):
self.mode = m
if m.name not in self.buffer.highlights and m.lexer is not None:
self.buffer.highlights[m.name] = highlight.Highlighter(m.lexer)
self.buffer.highlights[m.name].highlight(self.buffer.lines)
2007-10-21 20:55:43 -04:00
#self.redraw()
def get_highlighter(self):
if self.mode.lexer is None:
return None
else:
return self.buffer.highlights[self.mode.name]
2007-10-21 20:55:43 -04:00
# this is used to temporarily draw the user's attention to another point
def set_active_point(self, p, msg='marking on line %(y)d, char %(x)d'):
2007-10-21 20:55:43 -04:00
self.active_point = p
if not self.point_is_visible(p):
self.application.set_error(msg % {'x': p.x, 'y': p.y})
# point left
def point_left(self, p):
if p.y == 0 and p.x == 0:
return None
elif p.x == 0:
return Point(len(self.buffer.lines[p.y - 1]), p.y - 1)
else:
return Point(p.x - 1, p.y)
# point right
def point_right(self, p):
blen = len(self.buffer.lines)
if p.y >= blen:
return None
elif p.y == blen-1 and p.x >= len(self.buffer.lines[-1]):
2007-10-21 20:55:43 -04:00
return None
elif p.x == len(self.buffer.lines[p.y]):
return Point(0, p.y + 1)
else:
return Point(p.x + 1, p.y)
# cursors
def logical_cursor(self):
if len(self.buffer.lines) > self.cursor.y:
l = len(self.buffer.lines[self.cursor.y])
else:
l = 0
x = min(self.cursor.x, l)
return Point(x, self.cursor.y)
2008-04-06 22:31:13 -04:00
2007-10-21 20:55:43 -04:00
# last visible point
def _calc_last(self):
# POSSIBLE BUG
2007-10-21 20:55:43 -04:00
(x, y) = self.first.xy()
count = 0
while count < self.height - 1 and y < len(self.buffer.lines) - 1:
line = self.buffer.lines[y]
if x >= len(line) or len(line[x:]) <= self.width:
x = 0
y += 1
count += 1
else:
count += 1
x += self.width
if y < len(self.buffer.lines):
x = min(x + self.width, len(self.buffer.lines[y]))
self.last = Point(x, y)
# redrawing
def redraw(self):
self._calc_last()
def set_size(self, width, height):
assert type(width) == type(0), width
assert type(height) == type(0), height
self.width = width - self.mode.lmargin - self.mode.rmargin
2009-01-28 16:28:33 -05:00
self.height = height - self.mode.header - self.mode.footer
2007-10-21 20:55:43 -04:00
self.redraw()
# region added
def region_added(self, p, newlines):
(x, y) = self.logical_cursor().xy()
l = len(newlines)
assert l > 0, repr(newlines)
visible = self.point_is_visible(p)
if l > 1:
if y > p.y:
self.cursor = Point(x, y + l - 1)
elif y == p.y and x >= p.x:
self.cursor = Point(len(newlines[-1]) + x - p.x, y + l - 1)
elif y == p.y and x >= p.x:
self.cursor = Point(x + len(newlines[0]), y)
if not visible and l > 1 and self.first.y > p.y:
self.first = Point(self.first.x, self.first.y + l - 1)
self.redraw()
self.mode.region_added(p, newlines)
self.assure_visible_cursor()
# region removed
def region_removed(self, p1, p2):
cursor = self.logical_cursor()
(x, y) = cursor.xy()
visible = self.point_is_visible(p2)
xdelta = p2.x - p1.x
ydelta = p2.y - p1.y
if cursor < p1:
pass
elif cursor < p2:
self.cursor = p1
elif cursor.y == p2.y:
#self.cursor = Point(self.cursor.x - p2.x + p1.x, p1.y)
self.cursor = Point(self.cursor.x - xdelta, p1.y)
else:
#self.cursor = Point(self.cursor.x, self.cursor.y - p2.y + p1.y)
self.cursor = Point(self.cursor.x, self.cursor.y - ydelta)
if not visible and ydelta and self.first.y > p2.y:
self.first = Point(self.first.x, self.first.y - ydelta)
self.redraw()
self.mode.region_removed(p1, p2)
self.assure_visible_cursor()
def point_is_visible(self, p):
return self.first <= p and p <= self.last
def cursor_is_visible(self):
return self.point_is_visible(self.logical_cursor())
def first_is_visible(self):
return self.point_is_visible(self.buffer.get_buffer_start())
def last_is_visible(self):
return self.point_is_visible(self.buffer.get_buffer_end())
def center_view(self, force=False):
x, y = self.logical_cursor().xy()
2007-10-21 20:55:43 -04:00
counter = 0
while counter < self.height / 2:
if x > self.width:
x -= self.width
elif y > 0:
y -= 1
x = len(self.buffer.lines[y])
else:
x, y = 0, 0
2007-10-21 20:55:43 -04:00
break
counter += 1
# trim off the slop
x = x - (x % self.width)
# in this case, the line is a multiple of width, and there is nothing
# (besides a newline) to display on the "next" line, so we should
# reduce the wrapping by one line.
if x >= self.width and x == len(self.buffer.lines[y]):
x -= self.width
# make sure we aren't "centering" on the end of the file (where half the
# screen is empty). that is, unless that's what the user wants
# (indicated by force=True).
2008-11-20 16:16:38 -05:00
#if not force:
# (x2, y2) = self.last_visible_coords()
# if y2 < y or x2 < x:
# x, y = x2, y2
self.first = Point(x, y)
2007-10-21 20:55:43 -04:00
self.redraw()
def lower_view(self):
(x, y) = self.logical_cursor().xy()
counter = 0
while counter < self.height - 1:
if x > self.width:
x -= self.width
elif y > 0:
y -= 1
x = len(self.buffer.lines[y])
else:
(x, y) = (0, 0)
break
counter += 1
self.first = Point(x - (x % self.width), y)
self.redraw()
def upper_view(self):
(x, y) = self.logical_cursor().xy()
counter = 0
while counter < 2:
if x > self.width:
x -= self.width
elif y > 0:
y -= 1
x = len(self.buffer.lines[y])
else:
(x, y) = (0, 0)
break
counter += 1
self.first = Point(x - (x % self.width), y)
self.redraw()
2007-10-21 20:55:43 -04:00
def assure_visible_cursor(self):
if not self.cursor_is_visible():
self.center_view()
#p = self.logical_cursor()
#if self.first > p:
# self.upper_view()
#elif p > self.last:
# self.lower_view()
2007-10-21 20:55:43 -04:00
# moving in buffer
def forward(self):
2008-11-03 09:30:06 -05:00
self.cursor = self.buffer.forward(self.logical_cursor())
2007-10-21 20:55:43 -04:00
self.assure_visible_cursor()
def backward(self):
2008-11-03 09:30:06 -05:00
self.cursor = self.buffer.backward(self.logical_cursor())
2007-10-21 20:55:43 -04:00
self.assure_visible_cursor()
def end_of_line(self):
2008-11-03 09:30:06 -05:00
self.cursor = self.buffer.end_of_line(self.logical_cursor())
2007-10-21 20:55:43 -04:00
self.assure_visible_cursor()
def start_of_line(self):
2008-11-03 09:30:06 -05:00
self.cursor = self.buffer.start_of_line(self.logical_cursor())
2007-10-21 20:55:43 -04:00
self.assure_visible_cursor()
def previous_line(self):
2008-11-03 09:30:06 -05:00
self.cursor = self.buffer.previous_line(self.cursor)
2007-10-21 20:55:43 -04:00
self.assure_visible_cursor()
def next_line(self):
2008-11-03 09:30:06 -05:00
self.cursor = self.buffer.next_line(self.cursor)
2007-10-21 20:55:43 -04:00
self.assure_visible_cursor()
# word handling
def find_left_word(self, p=None):
if p is None:
(x, y) = self.logical_cursor().xy()
else:
(x, y) = p.xy()
start = self.buffer.get_buffer_start()
if (x, y) == start:
return
elif x == 0:
y -= 1
x = len(self.buffer.lines[y])
else:
x -= 1
2008-11-08 10:30:04 -05:00
while (y, x) >= start and self.xy_char(x, y) not in self.mode.word_letters:
2007-10-21 20:55:43 -04:00
if x == 0:
y -= 1
x = len(self.buffer.lines[y])
else:
x -= 1
found_word = False
2008-11-08 10:30:04 -05:00
while (y, x) >= start and self.xy_char(x, y) in self.mode.word_letters:
2007-10-21 20:55:43 -04:00
found_word = True
if x == 0:
y -= 1
x = len(self.buffer.lines[y])
else:
x -= 1
if not found_word:
return None
elif x == len(self.buffer.lines[y]):
x = 0
y += 1
else:
x += 1
return Point(x, y)
def find_right_word(self, p=None):
if p is None:
(x, y) = self.logical_cursor().xy()
else:
(x, y) = p.xy()
end = self.buffer.get_buffer_end()
2008-11-08 10:30:04 -05:00
while (y, x) < end and self.xy_char(x, y) not in self.mode.word_letters:
2007-10-21 20:55:43 -04:00
if x == len(self.buffer.lines[y]):
x = 0
y += 1
else:
x += 1
2008-11-08 10:30:04 -05:00
while (y, x) < end and self.xy_char(x, y) in self.mode.word_letters:
2007-10-21 20:55:43 -04:00
if x == len(self.buffer.lines[y]):
x = 0
y += 1
else:
x += 1
return Point(x, y)
def left_word(self):
p = self.find_left_word()
if p is not None:
self.goto(p)
def right_word(self):
p = self.find_right_word()
if p is not None:
self.goto(p)
2008-11-08 10:30:04 -05:00
def get_word_bounds_at_point(self, p):
wl = self.mode.word_letters
2007-10-21 20:55:43 -04:00
if len(self.buffer.lines[p.y]) == 0:
return None
elif self.cursor_char() not in wl:
return None
x1 = x2 = p.x
while x1 > 0 and self.xy_char(x1 - 1, p.y) in wl:
2007-10-21 20:55:43 -04:00
x1 -= 1
while x2 < len(self.buffer.lines[p.y]) and self.xy_char(x2, p.y) in wl:
x2 += 1
return (Point(x1, p.y), Point(x2, p.y))
2008-11-08 10:30:04 -05:00
def get_word_at_point(self, p):
bounds = self.get_word_bounds_at_point(p)
2007-10-21 20:55:43 -04:00
if bounds is None:
return None
else:
return self.buffer.get_substring(bounds[0], bounds[1])
2008-11-08 10:30:04 -05:00
def get_word_bounds(self):
return self.get_word_bounds_at_point(self.logical_cursor())
def get_word(self):
return self.get_word_at_point(self.logical_cursor())
2007-10-21 20:55:43 -04:00
# page up/down
def _pshift_up(self, p, num):
(x, y) = p.xy()
orig_x = x
counter = 0
while counter < num and y > 0:
if x > self.width:
x -= self.width
else:
y -= 1
x = len(self.buffer.lines[y])
counter += 1
return Point(orig_x, y)
def _pshift_down(self, p, num):
(x, y) = p.xy()
orig_x = x
counter = 0
while counter < num and y < len(self.buffer.lines):
if x + self.width >= len(self.buffer.lines[y]):
y += 1
x = 0
else:
x += self.width
counter += 1
if y == len(self.buffer.lines):
y -= 1
x = len(self.buffer.lines[y])
return Point(orig_x, y)
def page_up(self):
first_point = self.buffer.get_buffer_start()
if self.point_is_visible(first_point):
self.goto_beginning()
return
self.cursor = self._pshift_up(self.cursor, self.height - 3)
if self.first > first_point:
self.first = self._pshift_up(self.first, self.height - 3)
self.redraw()
def page_down(self):
last_point = self.buffer.get_buffer_end()
if self.point_is_visible(last_point):
self.goto_end()
return
self.cursor = self._pshift_down(self.cursor, self.height - 3)
if self.last < last_point:
self.first = self._pshift_down(self.first, self.height - 3)
self.redraw()
# jumping in buffer
def goto(self, p):
self.cursor = p
self.assure_visible_cursor()
def goto_line(self, n):
assert n > 0 and n <= len(self.buffer.lines) , "illegal line: %d" % n
self.cursor = Point(0, n - 1)
self.assure_visible_cursor()
def forward_lines(self, n):
assert n > 0, "illegal number of lines: %d" % n
y = min(self.logical_cursor().y + n, len(self.buffer.lines) - 1)
self.goto(Point(0, y))
def forward_chars(self, n):
(x, y) = self.logical_cursor().xy()
for i in range(0, n):
if x == len(self.buffer.lines[y]):
y += 1
x = 0
if y >= len(self.buffer.lines):
break
else:
x += 1
self.goto(Point(x, y))
def goto_char(self, n):
self.goto_beginning()
self.forward_chars(n)
def goto_beginning(self):
self.cursor = Point(0, 0)
self.assure_visible_cursor()
2008-05-11 23:37:57 -04:00
def goto_end(self, force=False):
2007-10-21 20:55:43 -04:00
self.cursor = self.buffer.get_buffer_end()
(x, y) = self.last_visible_coords()
if force or not self.cursor_is_visible():
self.first = Point(x - (x % self.width), y)
self.redraw()
def last_visible_coords(self):
(x, y) = self.buffer.get_buffer_end().xy()
#if x == 0:
# y -= 1
# x = len(self.buffer.lines[y])
#else:
# x -= 1
2007-10-21 20:55:43 -04:00
counter = 0
# 3 means last line we see + 2 blank line of context
2007-10-21 20:55:43 -04:00
while counter < self.height - 3:
if x > self.width:
d = x % self.width
if d:
x -= d
else:
x -= self.width
2007-10-21 20:55:43 -04:00
elif y > 0:
y -= 1
x = len(self.buffer.lines[y])
else:
(x, y) = (0, 0)
break
counter += 1
return (x, y)
2007-10-21 20:55:43 -04:00
# mark manipulation
def set_mark_point(self, p):
self.mark = p
def set_mark(self):
self.set_mark_point(self.logical_cursor())
self.application.set_error("Mark set")
def goto_mark(self):
self.goto(self.mark)
def switch_mark(self):
if self.mark:
p = self.mark
self.set_mark_point(self.logical_cursor())
self.goto(p)
2008-11-08 10:30:04 -05:00
# finding (used by deletion, killing, and copying)
def get_line(self):
(x, y) = self.logical_cursor().xy()
lines = self.buffer.lines
if y < len(lines) - 1:
return (Point(0, y), Point(0, y + 1))
elif y > 0:
return (Point(len(lines[y - 1]), y - 1), Point(len(lines[y]), y))
else:
return (None, None)
def get_line_left(self):
cursor = self.logical_cursor()
(x, y) = cursor.xy()
lines = self.buffer.lines
if (x > 0) and not regex.whitespace.match(lines[y][:x]):
return (Point(0, y), cursor)
elif y > 0:
return (Point(len(lines[y - 1]), y - 1), cursor)
else:
return (None, None)
def get_line_right(self):
2007-10-21 20:55:43 -04:00
cursor = self.logical_cursor()
(x, y) = cursor.xy()
lines = self.buffer.lines
if (x < len(lines[y]) and not regex.whitespace.match(lines[y][x:])):
2008-11-08 10:30:04 -05:00
return (cursor, Point(len(lines[y]), y))
2007-10-21 20:55:43 -04:00
elif y < len(lines) - 1:
2008-11-08 10:30:04 -05:00
return (cursor, Point(0, y + 1))
2007-10-21 20:55:43 -04:00
else:
2008-11-08 10:30:04 -05:00
return (None, None)
def get_region(self):
2007-10-21 20:55:43 -04:00
cursor = self.logical_cursor()
2008-11-08 10:30:04 -05:00
p1, p2 = None, None
2007-10-21 20:55:43 -04:00
if cursor < self.mark:
p1 = cursor
p2 = self.mark
elif self.mark < cursor:
p1 = self.mark
p2 = cursor
2008-11-08 10:30:04 -05:00
return (p1, p2)
def get_left_word(self):
p1 = self.find_left_word()
p2 = self.logical_cursor()
if p1 == p2:
return (None, None)
2007-10-21 20:55:43 -04:00
else:
2008-11-08 10:30:04 -05:00
return (p1, p2)
def get_right_word(self):
p1 = self.logical_cursor()
p2 = self.find_right_word()
if p1 == p2:
return (None, None)
2007-10-21 20:55:43 -04:00
else:
2008-11-08 10:30:04 -05:00
return (p1, p2)
# deletion
def delete(self, p1, p2):
self.buffer.delete(p1, p2)
def delete_line(self):
(p1, p2) = self.get_line_right()
if p1 is not None: self.delete(p1, p2)
def delete_region(self):
(p1, p2) = self.get_region()
if p1 is not None: self.delete(p1, p2)
def delete_left_word(self):
(p1, p2) = self.get_left_word()
if p1 is not None: self.delete(p1, p2)
def delete_right_word(self):
(p1, p2) = self.get_right_word()
if p1 is not None: self.delete(p1, p2)
def left_delete(self):
self.buffer.left_delete(self.logical_cursor())
def right_delete(self):
self.buffer.right_delete(self.logical_cursor())
# killing
2007-10-21 20:55:43 -04:00
def kill(self, p1, p2):
killed = self.buffer.get_substring(p1, p2)
self.buffer.delete(p1, p2)
self.application.push_kill(killed)
return killed
2008-11-08 10:30:04 -05:00
def kill_line(self):
(p1, p2) = self.get_line_right()
if p1 is not None: self.kill(p1, p2)
def kill_region(self):
(p1, p2) = self.get_region()
if p1 is not None: self.kill(p1, p2)
def kill_left_word(self):
(p1, p2) = self.get_left_word()
if p1 is not None: self.kill(p1, p2)
def kill_right_word(self):
(p1, p2) = self.get_right_word()
if p1 is not None: self.kill(p1, p2)
# copying
2007-10-21 20:55:43 -04:00
def copy(self, p1, p2):
copied = self.buffer.get_substring(p1, p2)
self.application.push_kill(copied)
return copied
2008-11-08 10:30:04 -05:00
def copy_line(self):
(p1, p2) = self.get_line_right()
if p1 is not None: return self.copy(p1, p2)
def copy_region(self, kill=False):
(p1, p2) = self.get_region()
if p1 is not None: return self.copy(p1, p2)
2007-10-21 20:55:43 -04:00
# overwriting
def overwrite_char_at_cursor(self, c):
self.overwrite_char(self.logical_cursor(), c)
def overwrite_char(self, p, c):
line = self.buffer.lines[p.y]
if p.x >= len(line):
return
2007-10-21 20:55:43 -04:00
elif p.x == len(line) - 1:
self.buffer.overwrite_char(p, c)
if p.y < len(self.buffer.lines):
self.cursor = Point(0, p.y + 1)
else:
self.buffer.overwrite_char(p, c)
self.cursor = Point(p.x + 1, p.y)
# insertion
def insert_string_at_cursor(self, s):
self.insert_string(self.logical_cursor(), s)
def insert_string(self, p, s):
lines = s.split('\n')
self.insert_lines(p, lines)
def insert_lines_at_cursor(self, lines):
self.insert_lines(self.logical_cursor(), lines)
def insert_lines(self, p, lines):
self.buffer.insert_lines(p, lines)
self.redraw()
# yank/pop
def yank(self):
self.insert_string_at_cursor(self.application.get_kill())
def get_kill(self):
return self.application.get_kill()
def has_kill(self, i=-1):
return self.application.has_kill(i)
def pop_kill(self):
return self.application.pop_kill()
def push_kill(self, s):
return self.application.push_kill(s)
# querying
def cursor_char(self):
return self.point_char(self.logical_cursor())
def point_char(self, p):
return self.xy_char(p.x, p.y)
def xy_char(self, x, y):
if x == len(self.buffer.lines[y]):
return "\n"
else:
return self.buffer.lines[y][x]
# undo/redo
def undo(self):
p = self.buffer.undo()
if not self.point_is_visible(p):
self.goto(p)
def redo(self):
p = self.buffer.redo()
if not self.point_is_visible(p):
self.goto(p)
# highlighting tokens
def get_token(self):
return self.get_token_at_point(self.logical_cursor())
def get_token2(self):
c = self.logical_cursor()
p = Point(max(0, c.x - 1), c.y)
return self.get_token_at_point(p)
def get_token_at_point(self, p):
for token in self.get_highlighter().tokens[p.y]:
if token.end_x() <= p.x:
continue
elif token.x > p.x:
continue
else:
return token
return None
def get_next_token_by_lambda(self, p, f):
tokens = self.get_highlighter().tokens[p.y]
for token in tokens:
if token.x < p.x:
continue
if f(token):
return token
return None
def get_next_token_by_type(self, p, name):
return self.get_next_token_by_lambda(p, lambda t: t.name == name)
def get_next_token_except_type(self, p, name):
return self.get_next_token_by_lambda(p, lambda t: t.name != name)
def get_next_token_by_type_regex(self, p, name, regex):
l = lambda t: t.name == name and regex.match(t.string)
return self.get_next_token_by_lambda(p, l)
def get_next_token_except_type_regex(self, p, name, regex):
l = lambda t: t.name != name or regex.match(t.string)
return self.get_next_token_by_lambda(p, l)
def get_next_token_by_types(self, p, *names):
return self.get_next_token_by_lambda(p, lambda t: t.name in names)
def get_next_token_except_types(self, p, *names):
return self.get_next_token_by_lambda(p, lambda t: t.name not in names)
# application drawing
#
# render methods return a list of lists of RenderString objects
# (i.e. multiple physical lines containing multiple areas to be drawn)
def render_line(self, y, width):
if self.mode.name in self.buffer.highlights:
return self.render_line_lit(y, width)
else:
return self.render_line_raw(y, width)
def render_line_raw(self, y, width):
if y >= len(self.buffer.lines):
r = RenderString(s='~', attrs=color.build('red', 'default'))
return [(r,)]
x = 0
line = self.buffer.lines[y]
lines = []
if line:
while x < len(line):
r = RenderString(s=line[x:x + width])
lines.append((r,))
x += width
else:
r = RenderString(s='')
lines.append((r,))
return lines
def render_line_lit(self, y, width):
if y >= len(self.buffer.lines):
r = RenderString(s='~', attrs=color.build('red', 'default'))
return [(r,)]
highlighter = self.buffer.highlights[self.mode.name]
line = []
lines = []
j = x = 0
while j < len(highlighter.tokens[y]):
# get our token, and do some basic checking/bleaching
token = highlighter.tokens[y][j]
if token.string.endswith('\n'):
tstring = token.string[:-1]
else:
tstring = token.string
assert token.y == y, '%d == %d' % (token.y, y)
# figure out what portion of the string to use
s_offset = max(x - token.x, 0)
s = tstring[s_offset:]
# for debugging things like lexing/relexing/etc.
if token._debug:
attr = color.build('blue', 'green', 'bold')
elif token.color:
attr = color.build(*token.color)
else:
attr = color.build("default", "default")
# ok, so add a region with data, position, and color info
x_offset = max(token.x - x, 0)
r = RenderString(s=s[:width - x_offset], x=x_offset, attrs=attr)
line.append(r)
# see if the token is wrapping, or if we move on to the next one
if x_offset + len(s) > width:
lines.append(line)
line = []
x += width
else:
j += 1
lines.append(line)
return lines