Beispiel #1
0
    def _move_heading(cls,
                      direction=Direction.FORWARD,
                      including_children=True):
        u""" Move heading up or down

		:returns: heading or None
		"""
        d = ORGMODE.get_document()
        current_heading = d.current_heading()
        if not current_heading or \
         (direction == Direction.FORWARD and not current_heading.next_sibling) or \
         (direction == Direction.BACKWARD and not current_heading.previous_sibling):
            return None

        cursor_offset = vim.current.window.cursor[0] - (
            current_heading._orig_start + 1)
        l = current_heading.get_parent_list()
        if l is None:
            raise HeadingDomError(
                u'Current heading is not properly linked in DOM')

        if not including_children:
            if current_heading.previous_sibling:
                npl = current_heading.previous_sibling.children
                for child in current_heading.children:
                    npl.append(child, taint=False)
            elif current_heading.parent:
                # if the current heading doesn't have a previous sibling it
                # must be the first heading
                np = current_heading.parent
                for child in current_heading.children:
                    cls._append_heading(child, np)
            else:
                # if the current heading doesn't have a parent, its children
                # must be added as top level headings to the document
                npl = l
                for child in current_heading.children[::-1]:
                    npl.insert(0, child, taint=False)
            current_heading.children.remove_slice(
                0, len(current_heading.children), taint=False)

        idx = current_heading.get_index_in_parent_list()
        if idx is None:
            raise HeadingDomError(
                u'Current heading is not properly linked in DOM')

        offset = 1 if direction == Direction.FORWARD else -1
        del l[idx]
        l.insert(idx + offset, current_heading)

        d.write()

        vim.current.window.cursor = (current_heading.start_vim + cursor_offset,
                                     vim.current.window.cursor[1])

        return True
Beispiel #2
0
	def _move_heading(cls, direction=DIRECTION_FORWARD, including_children=True):
		u""" Move heading up or down

		:returns: heading or None
		"""
		d = ORGMODE.get_document()
		heading = d.current_heading()
		if not heading or \
				direction == DIRECTION_FORWARD and not heading.next_sibling or \
				direction == DIRECTION_BACKWARD and not heading.previous_sibling:
			return None

		cursor_offset_within_the_heading_vim = vim.current.window.cursor[0] - (heading._orig_start + 1)

		if not including_children:
			heading.previous_sibling.children.extend(heading.children)
			del heading.children

		heading_insert_position = 1 if direction == DIRECTION_FORWARD else -1
		l = heading.get_parent_list()
		idx = heading.get_index_in_parent_list()
		if l is not None and idx is not None:
			l.insert(idx + heading_insert_position, heading)
		else:
			raise HeadingDomError('Current heading is not properly linked in DOM')

		d.write()

		vim.current.window.cursor = (heading.start_vim + cursor_offset_within_the_heading_vim, vim.current.window.cursor[1])

		return True
Beispiel #3
0
    def new_heading(cls,
                    below=None,
                    insert_mode=False,
                    end_of_last_child=False):
        u"""
		:below:				True, insert heading below current heading, False,
							insert heading above current heading, None, special
							behavior for insert mode, use the current text as
							heading
		:insert_mode:		True, if action is performed in insert mode
		:end_of_last_child:	True, insert heading at the end of last child,
							otherwise the newly created heading will "take
							over" the current heading's children
		"""
        d = ORGMODE.get_document()
        current_heading = d.current_heading()
        cursor = vim.current.window.cursor[:]
        if not current_heading:
            # the user is in meta data region
            pos = cursor[0] - 1
            heading = Heading(title=d.meta_information[pos],
                              body=d.meta_information[pos + 1:])
            d.headings.insert(0, heading)
            del d.meta_information[pos:]
            d.write()
            vim.command(
                u_encode(u'exe "normal %dgg"|startinsert!' %
                         (heading.start_vim, )))
            return heading

        # check for plain list(checkbox)
        current_heading.init_checkboxes()
        c = current_heading.current_checkbox()
        if c is not None:
            ORGMODE.plugins[u"EditCheckbox"].new_checkbox(below, not c.status)
            return

        heading = Heading(level=current_heading.level)

        # it's weird but this is the behavior of original orgmode
        if below is None:
            below = cursor[1] != 0 or end_of_last_child

        # insert newly created heading
        l = current_heading.get_parent_list()
        idx = current_heading.get_index_in_parent_list()
        if l is not None and idx is not None:
            l.insert(idx + (1 if below else 0), heading)
        else:
            raise HeadingDomError(
                u'Current heading is not properly linked in DOM')

        if below and not end_of_last_child:
            # append heading at the end of current heading and also take
            # over the children of current heading
            for child in current_heading.children:
                heading.children.append(child, taint=False)
            current_heading.children.remove_slice(
                0, len(current_heading.children), taint=False)

        # if cursor is currently on a heading, insert parts of it into the
        # newly created heading
        if insert_mode and cursor[1] != 0 and cursor[
                0] == current_heading.start_vim:
            offset = cursor[1] - current_heading.level - 1 - (
                len(current_heading.todo) + 1 if current_heading.todo else 0)
            if offset < 0:
                offset = 0
            if int(settings.get(u'org_improve_split_heading', u'1')) and \
             offset > 0 and len(current_heading.title) == offset + 1 \
             and current_heading.title[offset - 1] not in (u' ', u'\t'):
                offset += 1
            heading.title = current_heading.title[offset:]
            current_heading.title = current_heading.title[:offset]
            heading.body = current_heading.body[:]
            current_heading.body = []

        d.write()
        # do not start insert upon adding new headings, unless already in insert mode. Issue #211
        if int(settings.get(u'org_prefer_insert_mode', u'1')) or insert_mode:
            vim.command(
                u_encode(u'exe "normal %dgg"|startinsert!' %
                         (heading.start_vim, )))
        else:
            vim.command(u_encode(u'exe "normal %dgg$"' %
                                 (heading.start_vim, )))

        # return newly created heading
        return heading
Beispiel #4
0
	def new_heading(cls, below=None, insert_mode=False, end_of_last_child=False):
		"""
		:below:				True, insert heading below current heading, False,
							insert heading above current heading, None, special
							behavior for insert mode, use the current text as
							heading
		:insert_mode:		True, if action is performed in insert mode
		:end_of_last_child:	True, insert heading at the end of last child,
							otherwise the newly created heading will "take
							over" the current heading's children
		"""
		d = ORGMODE.get_document()
		h = d.current_heading()
		cursor = vim.current.window.cursor[:]
		if not h:
			# the user is in meta data region
			pos = cursor[0] - 1
			heading = Heading(body=d.meta_information[pos:])
			d.headings.insert(0, heading)
			del d.meta_information[pos:]
			d.write()
			vim.current.window.cursor = (pos + 1, heading.level + 1)
			return

		heading = Heading(level=h.level)

		# it's weird but this is the behavior of original orgmode
		if below is None:
			below = cursor[1] != 0 or end_of_last_child

		heading_insert_position = 0
		if below:
			heading_insert_position = 1
			if not end_of_last_child:
				# append heading at the end of current heading but also take
				# over the children of current heading
				heading.children = h.children[:]
				del h.children

		# insert newly created heading
		l = h.get_parent_list()
		idx = h.get_index_in_parent_list()
		if l is not None and idx is not None:
			l.insert(idx + heading_insert_position, heading)
		else:
			raise HeadingDomError('Current heading is not properly linked in DOM')

		d.write()

		# if cursor is currently on a heading, insert parts of it into the
		# newly created heading
		# TODO implement me
		#if insert_mode and not end_of_last_child and cursor[0] == h.start_vim:
		#	if cursor[1] > h.level:
		#		tmp1 = vim.current.buffer[cursor[0] - 1][:cursor[1]]
		#		tmp2 = vim.current.buffer[cursor[0] - 1][cursor[1]:]
		#		vim.current.buffer[cursor[0] - 1] = tmp1
		#	else:
		#		tmp2 = u''
		#	if below:
		#		vim.current.buffer[cursor[0]:cursor[0]] = [(u'%s %s' % (u'*' * level, tmp2.lstrip())).encode(u'utf-8')]
		#		vim.current.window.cursor = (cursor[0] + 1, level + 1)
		#	else:
		#		# this can only happen at column 0
		#		vim.current.buffer[cursor[0] - 1:cursor[0] - 1] = [(u'%s ' % (u'*' * level, )).encode(u'utf-8')]
		#		vim.current.window.cursor = (cursor[0], level + 1)

		if insert_mode:
			vim.command((u'exe "normal %dgg"|startinsert!' % (heading.start_vim, )).encode(u'utf-8'))
		else:
			vim.current.window.cursor = (cursor[0], cursor[1] + heading.level + 1)

		# return newly created heading
		return heading
Beispiel #5
0
    def new_heading(cls,
                    below=None,
                    insert_mode=False,
                    end_of_last_child=False):
        u"""
		:below:				True, insert heading below current heading, False,
							insert heading above current heading, None, special
							behavior for insert mode, use the current text as
							heading
		:insert_mode:		True, if action is performed in insert mode
		:end_of_last_child:	True, insert heading at the end of last child,
							otherwise the newly created heading will "take
							over" the current heading's children
		"""
        d = ORGMODE.get_document()
        current_heading = d.current_heading()
        cursor = vim.current.window.cursor[:]
        if not current_heading:
            # the user is in meta data region
            pos = cursor[0] - 1
            heading = Heading(title=d.meta_information[pos],
                              body=d.meta_information[pos + 1:])
            d.headings.insert(0, heading)
            del d.meta_information[pos:]
            d.write()
            vim.command((u'exe "normal %dgg"|startinsert!' %
                         (heading.start_vim, )).encode(u'utf-8'))
            return heading

        heading = Heading(level=current_heading.level)

        # it's weird but this is the behavior of original orgmode
        if below is None:
            below = cursor[1] != 0 or end_of_last_child

        heading_insert_position = 0
        if below:
            heading_insert_position = 1
            if not end_of_last_child:
                # append heading at the end of current heading but also take
                # over the children of current heading
                heading.children = [h.copy() for h in current_heading.children]
                del current_heading.children

        # if cursor is currently on a heading, insert parts of it into the
        # newly created heading
        if insert_mode and cursor[1] != 0 and cursor[
                0] == current_heading.start_vim:
            offset = cursor[1] - current_heading.level - 1 - (len(current_heading.todo) \
              + 1 if current_heading.todo else 0)
            if offset < 0:
                offset = 0
            if int(settings.get(u'org_improve_split_heading', u'1')) and \
              offset > 0 and len(current_heading.title) == offset + 1 \
              and current_heading.title[offset - 1] not in (u' ', u'\t'):
                offset += 1
            heading.title = current_heading.title[offset:]
            current_heading.title = current_heading.title[:offset]
            heading.body = current_heading.body[:]
            current_heading.body = []

        # insert newly created heading
        l = current_heading.get_parent_list()
        idx = current_heading.get_index_in_parent_list()
        if l is not None and idx is not None:
            l.insert(idx + heading_insert_position, heading)
        else:
            raise HeadingDomError(
                u'Current heading is not properly linked in DOM')

        d.write()
        vim.command((u'exe "normal %dgg"|startinsert!' %
                     (heading.start_vim, )).encode(u'utf-8'))

        # return newly created heading
        return heading