Example #1
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
Example #2
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
Example #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'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