def change_indent(self, start, end, change): """Change indentation level""" # determine region if start is None or end is None: start, end = get_paragraphs_selected(self._buf) self._buf.begin_user_action() # loop through paragraphs for pos in paragraph_iter(self._buf, start, end): par_end = pos.copy() par_end.forward_line() indent, par_indent = self.get_indent(pos) if indent + change > 0: tag = self._buf.tag_table.lookup( RichTextIndentTag.tag_name(indent + change, par_indent)) self._buf.clear_tag_class(tag, pos, par_end) self._buf.apply_tag(tag, pos, par_end) elif indent > 0: # remove indent and possible bullets self._buf.clear_tag_class( self._buf.tag_table.lookup( RichTextIndentTag.tag_name(indent, par_indent)), pos, par_end) self._remove_bullet(pos) else: # do nothing pass self._buf.end_user_action()
def _set_bullet_list_paragraph(self, par_start, par_end, par_type): """Toggle the state of a bullet list for a paragraph""" # start indent if it is not present indent, _ = self.get_indent(par_start) if indent == 0: indent = 1 # apply indent to whole paragraph indent_tag = self._buf.tag_table.lookup( RichTextIndentTag.tag_name(indent, par_type)) self._buf.clear_tag_class(indent_tag, par_start, par_end) self._buf.apply_tag(indent_tag, par_start, par_end) self._queue_update_indentation(par_start, par_end)
def update_indentation(self): """Ensure the indentation tags between start and end are up to date""" # fixup indentation tags # The general rule is that the indentation at the start of # each paragraph should determines the indentation of the rest # of the paragraph # if no indentation requests are queued then do nothing if not self._indent_update: return self._indent_update = False self._buf.begin_user_action() self._buf.begin_noninteractive() # get range of updating pos = self._buf.get_iter_at_mark(self._indent_update_start) end = self._buf.get_iter_at_mark(self._indent_update_end) pos = move_to_start_of_line(pos) end.forward_line() # iterate through the paragraphs that need updating for pos in paragraph_iter(self._buf, pos, end): par_end = pos.copy() par_end.forward_line() indent_tag = self.get_indent_tag(pos) # remove bullets mid paragraph it = pos.copy() it.forward_char() while True: match = it.forward_search(BULLET_STR, 0, par_end) if not match: it.backward_char() pos, par_end = get_paragraph(it) break self._buf.move_mark(self._indent_update_start, match[0]) self._buf.delete(match[0], match[1]) it = self._buf.get_iter_at_mark(self._indent_update_start) par_end = it.copy() par_end.forward_line() # check indentation if indent_tag is None: # remove all indent tags # TODO: RichTextBaseBuffer function self._buf.clear_tag_class( self._buf.tag_table.lookup(RichTextIndentTag.tag_name(1)), pos, par_end) # remove bullets par_type = "none" else: self._buf.clear_tag_class(indent_tag, pos, par_end) self._buf.apply_tag(indent_tag, pos, par_end) # check for bullets par_type = indent_tag.get_par_indent() # check paragraph type if par_type == "bullet": # ensure proper bullet is in place pos = self._insert_bullet(pos, indent_tag) elif par_type == "none": # remove bullets pos = self._remove_bullet(pos) else: raise Exception("unknown par_type '%s'" % par_type) #self._updating = False self._buf.end_noninteractive() self._buf.end_user_action()
def update_indentation(self): """Ensure the indentation tags between start and end are up to date""" # fixup indentation tags # The general rule is that the indentation at the start of # each paragraph should determines the indentation of the rest # of the paragraph # if no indentation requests are queued then do nothing if not self._indent_update: return self._indent_update = False self._buf.begin_user_action() self._buf.begin_noninteractive() # get range of updating pos = self._buf.get_iter_at_mark(self._indent_update_start) end = self._buf.get_iter_at_mark(self._indent_update_end) pos = move_to_start_of_line(pos) end.forward_line() # iterate through the paragraphs that need updating for pos in paragraph_iter(self._buf, pos, end): par_end = pos.copy() par_end.forward_line() indent_tag = self.get_indent_tag(pos) # remove bullets mid paragraph it = pos.copy() it.forward_char() while True: match = it.forward_search(BULLET_STR, 0, par_end) if not match: it.backward_char() pos, par_end = get_paragraph(it) break self._buf.move_mark(self._indent_update_start, match[0]) self._buf.delete(match[0], match[1]) it = self._buf.get_iter_at_mark(self._indent_update_start) par_end = it.copy() par_end.forward_line() # check indentation if indent_tag is None: # remove all indent tags # TODO: RichTextBaseBuffer function self._buf.clear_tag_class( self._buf.tag_table.lookup( RichTextIndentTag.tag_name(1)), pos, par_end) # remove bullets par_type = "none" else: self._buf.clear_tag_class(indent_tag, pos, par_end) self._buf.apply_tag(indent_tag, pos, par_end) # check for bullets par_type = indent_tag.get_par_indent() # check paragraph type if par_type == "bullet": # ensure proper bullet is in place pos = self._insert_bullet(pos, indent_tag) elif par_type == "none": # remove bullets pos = self._remove_bullet(pos) else: raise Exception("unknown par_type '%s'" % par_type) #self._updating = False self._buf.end_noninteractive() self._buf.end_user_action()
def nest_indent_tags(contents, tag_table): """Convert indent tags so that they nest like HTML tags""" indent = 0 indent_closing = False # loop through contents stream for item in contents: # if we are in the middle of a indent closing event, then the next # item determines what we should do if indent_closing: if item[0] == "anchor" or item[0] == "text": # if we see "content" (anchors or text) (instead of # immediately opening a new indent) then we must close all # indents (i.e. indent=0) while indent > 0: yield ("end", None, tag_table.lookup( RichTextIndentTag.tag_name(indent))) indent -= 1 indent_closing = False elif item[0] == "begin": # if we see a begining tag then check to see if its an # indentation tag tag = item[2] if isinstance(tag, RichTextIndentTag): # (A) if it is a new indentation that is of lower indent # close all indents until we match next_indent = tag.get_indent() while indent > next_indent: yield ("end", None, tag_table.lookup( RichTextIndentTag.tag_name(indent))) indent -= 1 indent_closing = False else: # do nothing pass # yield items if item[0] == "begin" and \ isinstance(item[2], RichTextIndentTag): # if item is a begining indent, open indents until we match tag = item[2] next_indent = tag.get_indent() # should be true since (A) should have executed assert next_indent >= indent while indent < next_indent: # open new indents until we match level indent += 1 assert indent > 0 yield ("begin", None, tag_table.lookup( RichTextIndentTag.tag_name(indent))) elif item[0] == "end" and \ isinstance(item[2], RichTextIndentTag): next_indent = item[2].get_indent() indent_closing = True else: yield item # close all remaining indents while indent > 0: yield ("end", None, tag_table.lookup( RichTextIndentTag.tag_name(indent))) indent -= 1
def nest_indent_tags(contents, tag_table): """Convert indent tags so that they nest like HTML tags""" indent = 0 indent_closing = False # loop through contents stream for item in contents: # if we are in the middle of a indent closing event, then the next # item determines what we should do if indent_closing: if item[0] == "anchor" or item[0] == "text": # if we see "content" (anchors or text) (instead of # immediately opening a new indent) then we must close all # indents (i.e. indent=0) while indent > 0: yield ("end", None, tag_table.lookup( RichTextIndentTag.tag_name(indent))) indent -= 1 indent_closing = False elif item[0] == "begin": # if we see a begining tag then check to see if its an # indentation tag tag = item[2] if isinstance(tag, RichTextIndentTag): # (A) if it is a new indentation that is of lower indent # close all indents until we match next_indent = tag.get_indent() while indent > next_indent: yield ("end", None, tag_table.lookup( RichTextIndentTag.tag_name(indent))) indent -= 1 indent_closing = False else: # do nothing pass # yield items if item[0] == "begin" and \ isinstance(item[2], RichTextIndentTag): # if item is a begining indent, open indents until we match tag = item[2] next_indent = tag.get_indent() # should be true since (A) should have executed assert next_indent >= indent while indent < next_indent: # open new indents until we match level indent += 1 assert indent > 0 yield ("begin", None, tag_table.lookup(RichTextIndentTag.tag_name(indent))) elif item[0] == "end" and \ isinstance(item[2], RichTextIndentTag): next_indent = item[2].get_indent() indent_closing = True else: yield item # close all remaining indents while indent > 0: yield ("end", None, tag_table.lookup(RichTextIndentTag.tag_name(indent))) indent -= 1