diff --git a/highlight.py b/highlight.py new file mode 100644 index 0000000..99d30d2 --- /dev/null +++ b/highlight.py @@ -0,0 +1,326 @@ +import re, sys +from lex import Token + +color_list = [] +color_list.extend(['\033[3%dm' % x for x in range(0, 8)]) +color_list.extend(['\033[3%d;1m' % x for x in range(0, 8)]) +color_list.extend(['\033[0m']) + +color_names = [ + 'black', 'dred', 'dgreen', 'brown', 'dblue', 'dpurple', 'dcyan', 'lgrey', + 'dgrey', 'lred', 'lgreen', 'yellow', 'lblue', 'lpurple', 'lcyan', 'white', + 'unset', +] + +color_dict ={} +for i in range(0, len(color_list)): + color_dict[color_names[i]] = color_list[i] + +def token_match(self, token, name, data=None): + return token.fqname() == name and data is None or token.string == data +def token_match2(self, token, name, regex): + return token.fqname() == name and regex.match(token.string) +def token_vmatch(self, token, *pairs): + for (name, data) in pairs: + if token_match(token, name, data): + return True + return False +def token_vmatch2(self, token, *pairs): + for (name, regex) in pairs: + if token_match(token, name, regex): + return True + return False + +class Highlighter: + def __init__(self, lexer): + self.lexer = lexer + self.tokens = [] + + def dump(self, fmt='(%3s, %2s) | %s'): + print fmt % ('y', 'x', 'string') + for i in range(0, len(self.tokens)): + group = self.tokens[i] + print 'LINE %d' % i + for token in group: + print fmt % (token.y, token.x, token.string) + + def display(self, token_colors={}, debug=False): + for group in self.tokens: + for token in group: + color_name = None + name_parts = token.name.split('.') + for i in range(0, len(name_parts)): + if '.'.join(name_parts[i:]) in token_colors: + color_name = token_colors['.'.join(name_parts[i:])] + break + if color_name is not None: + sys.stdout.write(color_dict[color_name]) + pass + elif debug: + raise Exception, "no highlighting for %r" % token.name + else: + color_name = 'white' + sys.stdout.write(color_dict[color_name]) + sys.stdout.write(token.string) + sys.stdout.write('\n') + + def delete_token(self, y, i): + assert y < len(self.tokens), "%d < %d" % (y, len(self.tokens)) + assert i < len(self.tokens[y]), "%d < %d" % (i, len(self.tokens[i])) + deleted = [] + deleted.append(self.tokens[y].pop(i)) + while y < len(self.tokens): + while i < len(self.tokens[y]): + while deleted and self.tokens[y][i].parent is not deleted[-1]: + del deleted[-1] + if not deleted: + return + elif self.tokens[y][i].parent is deleted[-1]: + deleted.append(self.tokens[y].pop(i)) + else: + raise Exception, "huh?? %r %r" % (self.tokens[y][i].parent, + deleted) + i = 0 + y += 1 + + def highlight(self, lines): + self.tokens = [[] for l in lines] + #self.lexer.lex(lines, y=0, x=0) + #for token in self.lexer: + for token in self.lexer.lex(lines, y=0, x=0): + self.tokens[token.y].append(token) + + # relexing + # ====================== + def relex(self, lines, y1, x1, y2, x2, token=None): + if token: + gen = self.lexer.resume(lines, y1, 0, token) + else: + gen = self.lexer.lex(lines, y1, 0) + + # these keep track of the current y coordinate, the current token index + # on line[y], and the current "new token", respectively. + y = y1 + i = 0 + getnext = True + new_token = None + + while True: + # if we have overstepped our bounds, then exit! + if y >= len(lines): + break + + # if we need another new_token, then try to get it. + if getnext: + try: + #new_token = self.lexer.next() + new_token = gen.next() + getnext = False + except StopIteration: + # ok, so this means that ALL the rest of the tokens didn't + # show up, because we're done. so delete them and exit + for j in range(y, len(lines)): + del self.tokens[j][i:] + i = 0 + break + + # if our next token is one a future line, we need to just get rid of + # all our old tokens until we get there + while new_token.y > y: + del self.tokens[y][i:] + i = 0 + y += 1 + + # ok, so see if we have current tokens on this line; if so get it + if i < len(self.tokens[y]): + old_token = self.tokens[y][i] + assert old_token.y == y, "%d == %d" % (old_token.y, y) + else: + #raise Exception, "K %d %r" % (i, new_token) + old_token = None + + if old_token is None: + #raise Exception, "J %d %r" % (i, new_token) + # since we don't have a previous token at this location, just + # insert the new one + self.tokens[y].insert(i, new_token) + i += 1 + getnext = True + elif old_token == new_token: + # if they match, then leave the old one alone + i += 1 + getnext = True + if new_token.y > y2: + # in this case, we can be sure that the rest of the lines + # will lex the same way + break + elif old_token.x < new_token.end_x(): + # ok, so we haven't gotten to this new token yet. obviously + # this token never showed up in the new lexing, so delete it. + del self.tokens[y][i] + elif old_token.x >= new_token.end_x(): + # ok, this token is further out, so just insert the new token + # ahead of it, move our counter out and continue + self.tokens[y].insert(i, new_token) + i += 1 + getnext = True + else: + # this should never happen + raise Exception, "this isn't happening" + + # deletion + # ====================== + def update_del(self, lines, y1, x1, y2, x2): + assert y1 >= 0 + assert y1 <= y2 + + # first let's delete any token who falls in the range of the change (or, + # in the case of child tokens, whose parent is being deleted). + y = y1 + i = 0 + done = False + if self.tokens[y1]: + ctoken = self.tokens[y1][0] + else: + ctoken = None + while not done: + if y >= len(self.tokens): + break + if i < len(self.tokens[y]): + # figure out if this token is in our range. notice that + # delete_token() will take care of the need to recursively + # delete children for us + token = self.tokens[y][i] + if token.y > y2 or y == y2 and token.x >= x2: + done = True + elif token.y < y1 or token.y == y1 and token.x < x1: + i += 1 + else: + self.delete_token(y, i) + y += 1 + i = 0 + + # ok, so now we need to "adjust" the (x,y) coordinates of all the tokens + # after the change. first we will copy over the pre-deletion tokens. + newtokens = [[] for x in range(0, len(self.tokens) - y2 + y1)] + + for y in range(0, y1): + for token in self.tokens[y]: + newtokens[y].append(token) + + # then the tokens which occured on the same line as the end of the + # deletion. + for token in self.tokens[y1]: + newtokens[y1].append(token) + if y2 != y1: + for token in self.tokens[y2]: + token.x = token.x - x2 + x1 + token.y = y1 + newtokens[y1].append(token) + + # finally, we will copy over the tokens from subsequent lines + for y in range(y2 + 1, len(self.tokens)): + for token in self.tokens[y]: + token.y = token.y - y2 + y1 + newtokens[y - y2 + y1].append(token) + + # now save our new tokens + self.tokens = newtokens + return ctoken + + def relex_del(self, lines, y1, x1, y2, x2): + # first let's update our existing tokens to fix their offsets, etc. + ctoken = self.update_del(lines, y1, x1, y2, x2) + + # then let's do some relexing + self.relex(lines, y1, x1, y2, x2, ctoken) + + # addition + # ====================== + def update_add(self, lines, y1, x1, newlines): + assert y1 >= 0 + assert len(newlines) > 0 + + y2 = y1 + len(newlines) - 1 + if y2 == y1: + x2 = x1 + len(newlines[0]) + else: + x2 = len(newlines[-1]) + + xdelta = x2 - x1 + ydelta = y2 - y1 + if self.tokens[y1]: + ctoken = self.tokens[y1][0] + else: + ctoken = None + + # construct a new token data structure, with the right number of lines + newtokens = [] + for i in range(0, len(self.tokens) + ydelta): + newtokens.append([]) + + # copy the tokens that show up before the changed line + for y in range(0, y1): + newtokens[y] = self.tokens[y] + + # process the tokens that show up on the changed line + post_change_list = [] + for t in self.tokens[y1]: + tx1 = t.x + tx2 = t.x + len(t.string) + ty = t.y + ts = t.string + if tx2 <= x1: + # '*| ' before the insertion + newtokens[y1].append(t) + elif tx1 >= x1: + # ' |*' after the insertion + t.x += xdelta + t.y = y2 + post_change_list.append(t) + else: + # '*|*' around the insertion + t1 = t.copy() + t1.string = t.string[:x1 - tx1] + newtokens[y1].append(t1) + + t2 = t.copy() + t2.string = t.string[x1 - tx1:] + t2.x = x2 + t2.y = y2 + post_change_list.append(t2) + + # add in the new data + newtokens[y1].append(Token('new', '', y1, x1, newlines[0])) + for i in range(1, len(newlines)): + yi = y1 + i + newtokens[yi].append(Token('new', '', yi, 0, newlines[i])) + + # add the post-change tokens back + for t in post_change_list: + newtokens[y2].append(t) + + # for each subsequent line, fix it's tokens' y coordinates + for y in range(y1 + 1, len(self.tokens)): + for t in self.tokens[y]: + t.y += ydelta + newtokens[t.y].append(t) + + # ok, now that we have built a correct new structure, store a reference + # to it instead. + self.tokens = newtokens + return ctoken + + def relex_add(self, lines, y1, x1, newlines): + # first let's update our existing tokens to fix their offsets, etc. + ctoken = self.update_add(lines, y1, x1, newlines) + + # create some extra info that we need + y2 = y1 + len(newlines) - 1 + if y2 == y1: + x2 = x1 + len(newlines[0]) + else: + x2 = len(newlines[-1]) + + # now let's start the relexing process + self.relex(lines, y1, x1, y2, x2, ctoken) diff --git a/tab.py b/tab.py new file mode 100644 index 0000000..a3215e5 --- /dev/null +++ b/tab.py @@ -0,0 +1,209 @@ +import regex, util +from point import Point + +class Marker: + def __init__(self, name, level): + self.name = name + self.level = level + def __repr__(self): + return '' % (self.name, self.level) + +class Tabber: + wsre = regex.whitespace + wst = ('null', 'eol',) + sre = regex.space + st = ('null',) + def __init__(self, m): + self.mode = m + self.lines = {} + + def get_highlighter(self): + return self.mode.window.buffer.highlights[self.mode.name()] + def get_tokens(self, y): + return self.mode.window.buffer.highlights[self.mode.name()].tokens[y] + def get_token(self, y, i): + return self.mode.window.buffer.highlights[self.mode.name()].tokens[y][i] + + def token_is_whitespace(self, y, i): + token = self.get_token(y, i) + return token.fqname() in self.wst and self.wsre.match(token.string) + def token_is_space(self, y, i): + token = self.get_token(y, i) + return token.fqname() in self.st and self.sre.match(token.string) + + def get_next_left_token(self, y, i): + tokens = self.get_tokens(y) + assert i >= 0 and i < len(tokens) + for j in range(1, i): + if not self.token_is_whitespace(y, i - j): + return tokens[i - j] + return None + def get_next_right_token(self, y, i): + tokens = self.get_tokens(y) + assert i >= 0 and i < len(tokens) + for j in range(i + 1, len(tokens)): + if not self.token_is_whitespace(y, j): + return tokens[j] + return None + def is_leftmost_token(self, y, i): + return self.get_next_left_token(y, i) is None + def is_rightmost_token(self, y, i): + return self.get_next_right_token(y, i) is None + def is_only_token(self, y, i): + return self.is_leftmost_token(y, i) and self.is_rightmost_token(y, i) + + def get_leftmost_token(self, y): + tokens = self.get_tokens(y) + for i in range(0, len(tokens)): + if not self.token_is_whitespace(y, i): + return tokens[i] + return None + def get_rightmost_token(self, y): + tokens = self.get_tokens(y) + i = len(tokens) - 1 + for j in range(0, len(tokens)): + if not self.token_is_whitespace(y, i - j): + return tokens[i - j] + return None + + def get_nonws_tokens(self, y): + tokens = self.get_tokens(y) + for i in range(0, len(tokens)): + if not self.token_is_whitespace(y, i): + yield tokens[i] + raise StopIteration + def get_nons_tokens(self, y): + tokens = self.get_tokens(y) + for i in range(0, len(tokens)): + if not self.token_is_space(y, i): + yield tokens[i] + raise StopIteration + + def region_added(self, p, newlines): + self.lines = {} + def region_removed(self, p1, p2): + self.lines = {} + + def is_base(self, y): + return True + def get_level(self, y): + if y in self.lines: + return self.lines[y] + else: + self._calc_level(y) + return self.lines.get(y) + def _calc_level(self, y): + pass + +class StackTabber(Tabber): + def __init__(self, m): + self.mode = m + self.lines = {} + self.record = {} + self.markers = [] + + def get_curr_level(self): + if self.markers: + return self.markers[-1].level + else: + return 0 + + def region_added(self, p, newlines): + self.lines = {} + self.record = {} + self.markers = [] + def region_removed(self, p1, p2): + self.lines = {} + self.record = {} + self.markers = [] + + def is_base(self, y): + return y == 0 + def _calc_level(self, y): + # first we need to step back to find the last place where we have tab + # stops figured out, or a suitable place to start + target = y + while not self.is_base(y) and y > 0: + y -= 1 + + # ok now, let's do this shit + self.markers = [] + currlvl = 0 + while y <= target: + currlvl = self.get_curr_level() + tokens = self.get_tokens(y) + for i in range(0, len(tokens)): + currlvl = self._handle_token(currlvl, y, i) + self.lines[y] = currlvl + self.record[y] = tuple(self.markers) + y += 1 + + def _handle_token(self, currlvl, y, i): + token = self.get_token(y, i) + s = token.string + fqname = token.fqname() + + if fqname in self.mode.closetokens and s in self.mode.closetags: + currlvl = self._handle_close_token(currlvl, y, i) + elif fqname in self.mode.opentokens and s in self.mode.opentags: + currlvl = self._handle_open_token(currlvl, y, i) + else: + currlvl = self._handle_other_token(currlvl, y, i) + return currlvl + + def _handle_open_token(self, currlvl, y, i): + token = self.get_token(y, i) + rtoken = self.get_next_right_token(y, i) + if rtoken is None: + #level = self.get_curr_level() + 4 + level = self.get_curr_level() + self.mode.tabwidth + else: + level = rtoken.x + self._append(token.string, level) + return currlvl + def _handle_close_token(self, currlvl, y, i): + token = self.get_token(y, i) + s1 = token.string + if not self.markers: + raise Exception, "unmatched closing token %r" % s1 + s2 = self.markers[-1].name + if self.mode.closetags[s1] == s2: + self._pop() + if self.is_leftmost_token(y, i): + currlvl = self.get_curr_level() + else: + raise Exception, "mismatched closing tag %r vs %r" % (s2, s1) + return currlvl + def _handle_other_token(self, currlvl, y, i): + return currlvl + + def _has_markers(self): + return len(self.markers) > 0 + def _empty(self): + return len(self.markers) == 0 + def _append(self, name, level): + self.markers.append(Marker(name, level)) + def _peek(self): + return self.markers[-1] + def _peek_name(self): + return self.markers[-1].name + def _peek_level(self): + return self.markers[-1].level + def _pop(self): + self.markers.pop(-1) + def _pop_until(self, *names): + while self.markers: + if self.markers[-1].name in names: + self.markers.pop(-1) + return + else: + self.markers.pop(-1) + + def _opt_append(self, name, level): + if self.markers and self.markers[-1].name == name: + pass + else: + self._append(name, level) + def _opt_pop(self, *names): + if self.markers and self.markers[-1].name in names: + self.markers.pop(-1) diff --git a/window.py b/window.py new file mode 100644 index 0000000..7870982 --- /dev/null +++ b/window.py @@ -0,0 +1,635 @@ +import os.path, string +import highlight, regex +from point import Point + +WORD_LETTERS = list(string.letters + string.digits) + +# 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 for points. + +class Window(object): + margins = ((80, 'blue'),) + margins_visible = False + def __init__(self, b, a, height=24, width=80, mode_name=None): + self.buffer = b + self.application = a + + self.first = Point(0, 0) + self.last = None + self.cursor = Point(0, 0) + self.mark = None + self.active_point = None + + self.height = height + self.width = width + + self.input_line = "" + + if mode_name is not None: + pass + elif hasattr(self.buffer, 'modename') and self.buffer.modename is not None: + mode_name = self.buffer.modename + elif self.buffer.btype == 'mini': + mode_name = 'mini' + elif self.buffer.btype == 'console': + mode_name = "fundamental" + elif self.buffer.btype == 'dir': + mode_name = 'dir' + elif hasattr(self.buffer, 'path'): + path = self.buffer.path + basename = os.path.basename(path) + ext = self._get_path_ext(path) + + 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(self.buffer.lines) > 0 and \ + self.buffer.lines[0].startswith('#!'): + line = self.buffer.lines[0] + 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) + self.buffer.add_window(self) + + # 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 + modename = m.name() + if modename not in self.buffer.highlights and m.lexer is not None: + self.buffer.highlights[modename] = highlight.Highlighter(m.lexer) + self.buffer.highlights[modename].highlight(self.buffer.lines) + + #self.redraw() + def get_highlighter(self): + if self.mode.lexer is None: + return None + else: + return self.buffer.highlights[self.mode.name()] + + # 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, character %(x)d'): + 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): + if p.y == len(self.buffer.lines)-1 and p.x == len(self.buffer.lines[-1]): + 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) + + # last visible point + def _calc_last(self): + (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.height = height + 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): + (x, y) = self.logical_cursor().xy() + 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) + break + counter += 1 + self.first = Point(x - (x % self.width), y) + self.redraw() + def assure_visible_cursor(self): + if not self.cursor_is_visible(): + #raise Exception, "%s < %s" % (self.last, self.logical_cursor()) + self.center_view() + + # moving in buffer + def forward(self): + cursor = self.logical_cursor() + if cursor.x < len(self.buffer.lines[cursor.y]): + self.cursor = Point(cursor.x + 1, cursor.y) + elif cursor.y < len(self.buffer.lines) -1: + self.cursor = Point(0, cursor.y + 1) + self.assure_visible_cursor() + def backward(self): + cursor = self.logical_cursor() + if cursor.x > 0: + self.cursor = Point(cursor.x - 1, cursor.y) + elif cursor.y > 0: + self.cursor = Point(len(self.buffer.lines[cursor.y - 1]), cursor.y - 1) + self.assure_visible_cursor() + def end_of_line(self): + cursor = self.logical_cursor() + self.cursor = Point(len(self.buffer.lines[cursor.y]), cursor.y) + self.assure_visible_cursor() + def start_of_line(self): + cursor = self.logical_cursor() + self.cursor = Point(0, cursor.y) + self.assure_visible_cursor() + def previous_line(self): + if self.cursor.y > 0: + self.cursor = Point(self.cursor.x, self.cursor.y - 1) + self.assure_visible_cursor() + def next_line(self): + if self.cursor.y < len(self.buffer.lines) - 1: + self.cursor = Point(self.cursor.x, self.cursor.y + 1) + 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 + while (y, x) >= start and self.xy_char(x, y) not in WORD_LETTERS: + if x == 0: + y -= 1 + x = len(self.buffer.lines[y]) + else: + x -= 1 + found_word = False + while (y, x) >= start and self.xy_char(x, y) in WORD_LETTERS: + 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() + while (y, x) < end and self.xy_char(x, y) not in WORD_LETTERS: + if x == len(self.buffer.lines[y]): + x = 0 + y += 1 + else: + x += 1 + while (y, x) < end and self.xy_char(x, y) in WORD_LETTERS: + 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) + def get_word_bounds_at_point(self, p, wl=WORD_LETTERS): + 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: + 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)) + def get_word_at_point(self, p, wl=WORD_LETTERS): + bounds = self.get_word_bounds_at_point(p, wl) + if bounds is None: + return None + else: + return self.buffer.get_substring(bounds[0], bounds[1]) + def get_word_bounds(self, wl=WORD_LETTERS): + return self.get_word_bounds_at_point(self.logical_cursor(), wl) + def get_word(self, wl=WORD_LETTERS): + return self.get_word_at_point(self.logical_cursor(), wl) + + # 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() + def goto_end(self): + self.cursor = self.buffer.get_buffer_end() + (x, y) = self.logical_cursor().xy() + if x == 0: + y -= 1 + x = len(self.buffer.lines[y]) + else: + x -= 1 + counter = 0 + while counter < self.height - 3: + 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 + + if not self.cursor_is_visible(): + self.first = Point(x - (x % self.width), y) + self.redraw() + + # 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) + + # deletion + def left_delete(self): + (x, y) = self.logical_cursor().xy() + if x > 0: + self.buffer.delete_char(Point(x - 1, y)) + elif y > 0: + self.buffer.delete_char(Point(len(self.buffer.lines[y - 1]), y - 1)) + def right_delete(self): + cursor = self.logical_cursor() + if cursor < self.last: + self.buffer.delete_char(cursor) + else: + pass + + # killing + def kill_line(self): + return self.copy_line(kill=True) + def kill_region(self): + return self.copy_region(kill=True) + def kill_left_word(self): + p1 = self.find_left_word() + p2 = self.logical_cursor() + if p1 == p2: + return + return self.kill(p1, p2) + def kill_right_word(self): + p1 = self.logical_cursor() + p2 = self.find_right_word() + if p1 == p2: + return + return self.kill(p1, p2) + def copy_line(self, kill=False): + 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:])): + limit = Point(len(lines[y]), y) + elif y < len(lines) - 1: + limit = Point(0, y + 1) + else: + return + if kill: + return self.kill(cursor, limit) + else: + return self.copy(cursor, limit) + def copy_region(self, kill=False): + cursor = self.logical_cursor() + if cursor < self.mark: + p1 = cursor + p2 = self.mark + elif self.mark < cursor: + p1 = self.mark + p2 = cursor + else: + self.input_line = "Empty kill region" + return + if kill: + return self.kill(p1, p2) + else: + return self.copy(p1, p2) + def kill(self, p1, p2): + killed = self.buffer.get_substring(p1, p2) + self.buffer.delete(p1, p2) + self.application.push_kill(killed) + return killed + def copy(self, p1, p2): + copied = self.buffer.get_substring(p1, p2) + self.application.push_kill(copied) + return copied + + # 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): + self.insert_string(p, c) + 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)