def detect_internal_link(self, text: str, start: Gtk.TextIter) -> None: """Detect internal links (to other gtg tasks) and apply tags.""" # Find all matches matches = re.finditer(INTERNAL_REGEX, text) # Go through each with its own iterator and tag 'em for match in matches: url_start = start.copy() url_end = start.copy() url_start.forward_chars(match.start()) url_end.forward_chars(match.end()) tid = match.group(0).replace('gtg://', '') task = self.req.get_task(tid) if task: link_tag = InternalLinkTag(task) self.tags_applied.append(link_tag) self.table.add(link_tag) self.buffer.apply_tag(link_tag, url_start, url_end)
def detect_tag(self, text: str, start: Gtk.TextIter) -> None: """Detect GTGs tags and applies text tags to them.""" # Find all matches matches = re.finditer(TAG_REGEX, text) # Go through each with its own iterator and tag 'em for match in matches: tag_start = start.copy() tag_end = start.copy() tag_start.forward_chars(match.start()) tag_end.forward_chars(match.end()) # I find this confusing too :) tag_name = match.group(0) tag_tag = TaskTagTag(tag_name, self.req) self.tags_applied.append(tag_tag) self.table.add(tag_tag) self.buffer.apply_tag(tag_tag, tag_start, tag_end) self.task_tags.add(tag_name) self.add_tasktag_cb(tag_name)
def follow_if_link(self, text_view: 'LinkedTextView', itr: Gtk.TextIter) -> bool: """Retrieve the target of a link that was clicked. This is done by emitting the `link-activated` signal defined in this class. Whether or not it was a link, the click won't be processed further. """ # Get the tags where the colours are set, marking the link text. begin = itr.copy() begin.forward_to_tag_toggle() end = itr.copy() end.backward_to_tag_toggle() # Get the target of the link from its text. link_text = text_view.get_buffer().get_text(begin, end) target = text_view.get_buffer().markup_dict.get(link_text) # Confirm that is is in the links dictionary. if target is not None: self.emit('link-activated', target) return False # Do not process the signal further.
def _get_start_pos_mark(textiter: Gtk.TextIter) -> Optional[Tuple[int, int]]: cursor: Gtk.TextIter = textiter.copy() limit = 50 i = 0 while cursor.backward_char(): if i > limit: break if cursor.get_char() == '>': # We are not in a PositionMark, for sure! return None if cursor.get_char() == 'P': # Start of position mark? end_cursor = cursor.copy() end_cursor.forward_chars(9) if cursor.get_text(end_cursor) == 'Position<': return cursor.get_line(), cursor.get_line_offset() i += 1 return None
def detect_subheading(self, text: str, start: Gtk.TextIter) -> None: """Detect subheadings (akin to H2).""" if text.startswith('# ') and len(text) > 2: end = start.copy() end.forward_chars(2) invisible_tag = InvisibleTag() self.table.add(invisible_tag) self.buffer.apply_tag(invisible_tag, start, end) start.forward_chars(2) end.forward_to_line_end() subheading_tag = SubheadingTag() self.table.add(subheading_tag) self.buffer.apply_tag(subheading_tag, start, end) self.tags_applied.append(subheading_tag) self.tags_applied.append(invisible_tag)
def add_checkbox(self, tid: int, start: Gtk.TextIter) -> None: """Add a checkbox for a subtask.""" task = self.req.get_task(tid) checkbox = Gtk.CheckButton.new() if task and task.status != task.STA_ACTIVE: checkbox.set_active(True) checkbox.connect('toggled', lambda _: self.on_checkbox_toggle(tid)) checkbox.set_can_focus(False) # Block the modified signal handler while we add the anchor # for the checkbox widget with GObject.signal_handler_block(self.buffer, self.id_modified): anchor = self.buffer.create_child_anchor(start) self.add_child_at_anchor(checkbox, anchor) end = start.copy() end.forward_char() self.buffer.apply_tag(self.checkbox_tag, start, end) self.buffer.set_modified(False) checkbox.show()
def detect_subtasks(self, text: str, start: Gtk.TextIter) -> bool: """Detect a subtask line. Returns True if a subtask was found.""" # This function has three paths: # * "Initial" Path: We find the line starts with '- ' and has text. # * "Modified" Path: We find the line starts with a subtask tag # * "None" Path: The line doesn't have any subtasks # The structure of a subtask in terms of text tags is: # <subtask> # <checkbox>[ ]</checkbox> # <internal-link>Subtask name</internal-link> # </subtask> # Add a new subtask if text.startswith('- ') and len(text[2:]) > 0: # Remove the - delete_end = start.copy() delete_end.forward_chars(2) self.buffer.delete(start, delete_end) # Add new subtask tid = self.new_subtask_cb(text[2:]) task = self.req.get_task(tid) status = task.get_status() if task else 'Active' # Add the checkbox self.add_checkbox(tid, start) after_checkbox = start.copy() after_checkbox.forward_char() # Add the internal link link_tag = InternalLinkTag(tid, status) self.table.add(link_tag) end = start.copy() end.forward_to_line_end() self.buffer.apply_tag(link_tag, after_checkbox, end) self.tags_applied.append(link_tag) # Add the subtask tag start.backward_char() subtask_tag = SubTaskTag(tid) self.table.add(subtask_tag) self.buffer.apply_tag(subtask_tag, start, end) self.subtasks['tags'].append(tid) return True # A subtask already exists elif start.starts_tag(): # Detect if it's a subtask tag sub_tag = None for tag in start.get_tags(): if type(tag) == SubTaskTag: sub_tag = tag # Otherwise return early if not sub_tag: return False # Don't auto-remove it tid = sub_tag.tid task = self.req.get_task(tid) parents = task.get_parents() # Remove if its not a child of this task if not parents or parents[0] != self.tid: log.debug('Task %s is not a subtask of %s', tid, self.tid) log.debug('Removing subtask %s from content', tid) end = start.copy() end.forward_to_line_end() # Move the start iter to take care of the newline at # the previous line start.backward_chars(2) self.buffer.delete(start, end) return False # Check that we still have a checkbox after_checkbox = start.copy() after_checkbox.forward_char() has_checkbox = any(type(t) == CheckboxTag for t in after_checkbox.get_tags()) if not has_checkbox: check = self.buffer.get_slice(start, after_checkbox, True) # Check that there's still a checkbox. If the user has # deleted all text including the space after the checkbox, # the check for the tag will return False. In this case # we want to check if the checkbox is still around, if it # is the user might still type a new name for the task. # Otherwise, if the checkbox was deleted, we return and let # the text tags be deleted. if check != u'\uFFFC': return False try: self.subtasks['to_delete'].remove(tid) except ValueError: pass self.rename_subtask_cb(tid, text) # Get the task and instantiate an internal link tag status = task.get_status() if task else 'Active' link_tag = InternalLinkTag(tid, status) self.table.add(link_tag) # Apply the new internal link tag (which was removed # by process()) end = start.copy() end.forward_to_line_end() self.buffer.apply_tag(link_tag, after_checkbox, end) self.tags_applied.append(link_tag) # Re-apply the subtask tag too self.table.remove(sub_tag) subtask_tag = SubTaskTag(tid) self.table.add(subtask_tag) self.buffer.apply_tag(subtask_tag, start, end) return True # No subtask, no fun else: return False
def detect_subtasks(self, text: str, start: Gtk.TextIter) -> bool: """Detect a subtask line. Returns True if a subtask was found.""" # This function has three paths: # * "Initial" Path: We find the line starts with '- ' and has text. # * "Modified" Path: We find the line starts with a subtask tag # * "None" Path: The line doesn't have any subtasks # The structure of a subtask in terms of text tags is: # <subtask> # <checkbox>[ ]</checkbox> # <internal-link>Subtask name</internal-link> # </subtask> # Add a new subtask if text.startswith('- ') and len(text[2:]) > 0: # Remove the - delete_end = start.copy() delete_end.forward_chars(2) self.buffer.delete(start, delete_end) # Add new subtask tid = self.new_subtask_cb(text[2:]) task = self.req.get_task(tid) status = task.get_status() if task else 'Active' # Add the checkbox self.add_checkbox(tid, start) after_checkbox = start.copy() after_checkbox.forward_char() # Add the internal link link_tag = InternalLinkTag(tid, status) self.table.add(link_tag) end = start.copy() end.forward_to_line_end() self.buffer.apply_tag(link_tag, after_checkbox, end) self.tags_applied.append(link_tag) # Add the subtask tag start.backward_char() subtask_tag = SubTaskTag(tid) self.table.add(subtask_tag) self.buffer.apply_tag(subtask_tag, start, end) self.subtasks['tags'].append(tid) return True # A subtask already exists elif start.starts_tag(): # Detect if it's a subtask tag sub_tag = None for tag in start.get_tags(): if type(tag) == SubTaskTag: sub_tag = tag # Otherwise return early if not sub_tag: return False # Check that we still have a checkbox after_checkbox = start.copy() after_checkbox.forward_char() has_checkbox = any(type(t) == CheckboxTag for t in after_checkbox.get_tags()) if not has_checkbox: return False # Don't auto-remove it tid = sub_tag.tid try: self.subtasks['to_delete'].remove(tid) except ValueError: pass self.rename_subtask_cb(tid, text) # Get the task and instantiate an internal link tag task = self.req.get_task(tid) status = task.get_status() if task else 'Active' link_tag = InternalLinkTag(tid, status) self.table.add(link_tag) # Apply the new internal link tag (which was removed # by process()) end = start.copy() end.forward_to_line_end() self.buffer.apply_tag(link_tag, after_checkbox, end) self.tags_applied.append(link_tag) # Re-apply the subtask tag too self.table.remove(sub_tag) subtask_tag = SubTaskTag(tid) self.table.add(subtask_tag) self.buffer.apply_tag(subtask_tag, start, end) return True # No subtask, no fun else: return False