def add_selection_to_layout(pl, colours, doc, selection, offset, size): yoffset, xoffset = offset chars, rows = size selection = selection.get_normalised() sel_start = doc.offset_to_cursor_pos(selection.start) sel_end = doc.offset_to_cursor_pos(selection.end) # print selection.start, selection.end, sel_start, sel_end scr_start = offset lastline_num = min(yoffset + rows - 1, doc.num_lines - 1) scr_end = (lastline_num, chars) # we're going to add attributes to this attrs = pl.get_attributes() # if the selection starts before the last position displayed and # it ends after the first position displayed, then the selection is # on screen if not (min(scr_end, sel_start) == sel_start and min(scr_start, sel_end) == scr_start): # short-circuit so that we don't set all the attrs for nothing return start = max(sel_start, scr_start) end = min(sel_end, scr_end) endline_num = end[0] # work out the character positions to actual screen positions # by converting characters to spaces sy, sx = start sline = doc.get_line(sy) sx = char_pos_to_tab_pos(sline, sx, doc.tab_size) ey, ex = end eline = doc.get_line(ey) ex = char_pos_to_tab_pos(eline, ex, doc.tab_size) # cap the positions so that it is only the bit that's on screen start_pos = (cap(sy - yoffset, 0, rows), cap(sx - xoffset, 0, chars)) end_pos = (cap(ey - yoffset, 0, rows), cap(ex - xoffset, 0, chars)) sy, sx = start_pos ey, ex = end_pos if endline_num != sel_end[0]: # if this isn't the last line of the selection, then the # selection has to go to the end of the screen ex = chars # convert the (y, x) coordinates to indexes into the layout text start_index = sy * (chars + 1) + sx # 1 for line ending end_index = ey * (chars + 1) + ex # add the selection attribute bg = colours["sel"]["pango"] attr = pango.AttrBackground(red=bg.red, green=bg.green, blue=bg.blue, start_index=start_index, end_index=end_index) attrs.insert(attr) pl.set_attributes(attrs)
def execute(self): """ Save positions so that we can return later and call self.do(). """ self.is_executed = True view = self.view # for undo purposes self.before_cursor_pos = view.cursor_pos self.before_last_x_pos = view.last_x_pos self.before_scroll_pos = view.scroll_pos self.do() # recalculate last_x_pos based on where the cursor is now doc = view.document y, x = view.cursor_pos line = doc.get_line(y) view.last_x_pos = char_pos_to_tab_pos(line, x, doc.tab_size) # for redo purposes self.after_cursor_pos = view.cursor_pos self.after_last_x_pos = view.last_x_pos self.after_scroll_pos = view.scroll_pos view.invalidate()
def move(self): view = self.view doc = view.document y = doc.num_lines - 1 line = doc.get_line(y) x = len(line) view.cursor_pos = (y, x) view.last_x_pos = char_pos_to_tab_pos(line, x, doc.tab_size)
def move(self): view = self.view doc = view.document y, x = view.cursor_pos if x == 0: if y > 0: line = doc.get_line(y-1) lx = len(line) view.cursor_pos = (y-1, lx) view.last_x_pos = char_pos_to_tab_pos(line, lx, doc.tab_size) else: line = doc.get_line(y) span = get_word_range(line, x-1) if span: view.cursor_pos = (y, span[0]) view.last_x_pos = char_pos_to_tab_pos(line, span[0], doc.tab_size)
def move(self): view = self.view doc = view.document y, x = view.cursor_pos line = doc.get_line(y) line_end = len(line.rstrip()) if x == line_end: x = len(line) else: x = line_end view.cursor_pos = (y, x) view.last_x_pos = char_pos_to_tab_pos(line, x, doc.tab_size)
def move(self): view = self.view doc = view.document y, x = view.cursor_pos line = doc.get_line(y) line_start = len(line) - len(line.lstrip()) # toggle between first non-whitespace character and actual start if x == line_start: x = 0 else: x = line_start view.cursor_pos = (y, x) view.last_x_pos = char_pos_to_tab_pos(line, x, doc.tab_size)
def move(self): view = self.view doc = view.document #x, y = next_pos(doc, view.cursor_pos) y, x = view.cursor_pos line = doc.get_line(y) if x < len(line): x += 1 elif y < doc.num_lines-1: y += 1 x = 0 line = doc.get_line(y) else: return view.cursor_pos = (y, x) view.last_x_pos = char_pos_to_tab_pos(line, x, doc.tab_size)
def move(self): view = self.view doc = view.document #y, x = prev_pos(doc, view.cursor_pos) y, x = view.cursor_pos if x > 0: x -= 1 line = doc.get_line(y) elif y > 0: y -= 1 line = doc.get_line(y) x = len(line) else: return view.cursor_pos = (y, x) view.last_x_pos = char_pos_to_tab_pos(line, x, doc.tab_size)
def move(self): view = self.view doc = view.document y, x = view.cursor_pos if x == len(doc.get_line(y)): last_line = doc.num_lines - 1 if y < last_line: view.cursor_pos = (y+1, 0) view.last_x_pos = 0 else: line = doc.get_line(y) span = get_word_range(line, x) if span: view.cursor_pos = (y, span[1]) view.last_x_pos = char_pos_to_tab_pos(line, span[1], doc.tab_size)
def execute(self): view = self.view doc = view.document y, x = view.cursor_pos if view.selection: # typically select word only occurs when you double-click and # click will probably clear the selection, but you never know... view.selection = None view.invalidate() line = doc.get_line(y) span = get_word_range(line, x) if span: start_index = span[0] end_index = span[1] start_offset = doc.cursor_pos_to_offset((y, start_index)) end_offset = doc.cursor_pos_to_offset((y, end_index)) view.selection = Selection(doc, start_offset, end_offset) view.invalidate() view.last_x_pos = char_pos_to_tab_pos(line, end_index, doc.tab_size) view.cursor_pos = (y, end_index)
def test_char_pos_to_tab_pos(): assert char_pos_to_tab_pos('\ta', 1, 8) == 8