Example #1
0
    def parent_next_sibling(cls, mode):
        u"""
		Focus the parent's next sibling

		:returns: parent's next sibling heading or None
		"""
        heading = ORGMODE.get_document().current_heading()
        if not heading:
            if mode == u"visual":
                vim.command(u"normal! gv".encode(u"utf-8"))
            else:
                echo(u"No heading found")
            return

        if not heading.parent or not heading.parent.next_sibling:
            if mode == u"visual":
                vim.command(u"normal! gv".encode(u"utf-8"))
            else:
                echo(u"No parent heading found")
            return

        ns = heading.parent.next_sibling

        if mode == u"visual":
            cls._change_visual_selection(heading, ns, direction=Direction.FORWARD, parent=False)
        elif mode == u"operator":
            vim.current.window.cursor = (ns.start_vim, 0)
        else:
            vim.current.window.cursor = (ns.start_vim, ns.level + 1)
        return ns
Example #2
0
    def parent(cls, mode):
        u"""
		Focus parent heading

		:returns: parent heading or None
		"""
        heading = ORGMODE.get_document().current_heading()
        if not heading:
            if mode == u'visual':
                vim.command(u'normal gv'.encode(u'utf-8'))
            else:
                echo(u'No heading found')
            return

        if not heading.parent:
            if mode == u'visual':
                vim.command(u'normal gv'.encode(u'utf-8'))
            else:
                echo(u'No parent heading found')
            return

        p = heading.parent

        if mode == u'visual':
            cls._change_visual_selection(heading,
                                         p,
                                         direction=DIRECTION_BACKWARD,
                                         parent=True)
        else:
            vim.current.window.cursor = (p.start_vim, p.level + 1)
        return p
Example #3
0
    def parent(cls, mode):
        u"""
		Focus parent heading

		:returns: parent heading or None
		"""
        heading = ORGMODE.get_document().current_heading()
        if not heading:
            if mode == u"visual":
                vim.command(u"normal! gv".encode(u"utf-8"))
            else:
                echo(u"No heading found")
            return

        if not heading.parent:
            if mode == u"visual":
                vim.command(u"normal! gv".encode(u"utf-8"))
            else:
                echo(u"No parent heading found")
            return

        p = heading.parent

        if mode == u"visual":
            cls._change_visual_selection(heading, p, direction=Direction.BACKWARD, parent=True)
        else:
            vim.current.window.cursor = (p.start_vim, p.level + 1)
        return p
Example #4
0
    def parent_next_sibling(cls, mode):
        u"""
		Focus the parent's next sibling

		:returns: parent's next sibling heading or None
		"""
        heading = ORGMODE.get_document().current_heading()
        if not heading:
            if mode == u'visual':
                vim.command(u'normal gv'.encode(u'utf-8'))
            else:
                echo(u'No heading found')
            return

        if not heading.parent or not heading.parent.next_sibling:
            if mode == u'visual':
                vim.command(u'normal gv'.encode(u'utf-8'))
            else:
                echo(u'No parent heading found')
            return

        ns = heading.parent.next_sibling

        if mode == u'visual':
            cls._change_visual_selection(heading,
                                         ns,
                                         direction=DIRECTION_FORWARD,
                                         parent=False)
        elif mode == u'operator':
            vim.current.window.cursor = (ns.start_vim, 0)
        else:
            vim.current.window.cursor = (ns.start_vim, ns.level + 1)
        return ns
Example #5
0
	def parent(cls, mode):
		u"""
		Focus parent heading

		:returns: parent heading or None
		"""
		heading = ORGMODE.get_document().current_heading()
		if not heading:
			if mode == u'visual':
				vim.command(u'normal gv'.encode(u'utf-8'))
			else:
				echo(u'No heading found')
			return

		if not heading.parent:
			if mode == u'visual':
				vim.command(u'normal gv'.encode(u'utf-8'))
			else:
				echo(u'No parent heading found')
			return

		if mode == u'visual':
			cls._change_visual_selection(heading, heading.parent, direction=DIRECTION_BACKWARD, parent=True)
		else:
			vim.current.window.cursor = (heading.parent.start_vim, heading.parent.level + 1)
		return heading.parent
Example #6
0
	def parent_next_sibling(cls, mode):
		u"""
		Focus the parent's next sibling

		:returns: parent's next sibling heading or None
		"""
		heading = ORGMODE.get_document().current_heading()
		if not heading:
			if mode == u'visual':
				vim.command(u'normal gv'.encode(u'utf-8'))
			else:
				echo(u'No heading found')
			return

		if not heading.parent or not heading.parent.next_sibling:
			if mode == u'visual':
				vim.command(u'normal gv'.encode(u'utf-8'))
			else:
				echo(u'No parent heading found')
			return

		ns = heading.parent.next_sibling

		if mode == u'visual':
			cls._change_visual_selection(heading, ns, direction=DIRECTION_FORWARD, parent=False)
		elif mode == u'operator':
			vim.current.window.cursor = (ns.start_vim, 0)
		else:
			vim.current.window.cursor = (ns.start_vim, ns.level + 1)
		return ns
Example #7
0
    def _focus_heading(cls, mode, direction=Direction.FORWARD, skip_children=False):
        u"""
		Focus next or previous heading in the given direction

		:direction: True for next heading, False for previous heading
		:returns: next heading or None
		"""
        d = ORGMODE.get_document()
        current_heading = d.current_heading()
        heading = current_heading
        focus_heading = None
        # FIXME this is just a piece of really ugly and unmaintainable code. It
        # should be rewritten
        if not heading:
            if direction == Direction.FORWARD and d.headings and vim.current.window.cursor[0] < d.headings[0].start_vim:
                # the cursor is in the meta information are, therefore focus
                # first heading
                focus_heading = d.headings[0]
            if not (heading or focus_heading):
                if mode == u"visual":
                    # restore visual selection when no heading was found
                    vim.command(u"normal! gv".encode(u"utf-8"))
                else:
                    echo(u"No heading found")
                return
        elif direction == Direction.BACKWARD:
            if vim.current.window.cursor[0] != heading.start_vim:
                # the cursor is in the body of the current heading, therefore
                # the current heading will be focused
                if mode == u"visual":
                    line_start, col_start = [int(i) for i in vim.eval(u'getpos("\'<")'.encode(u"utf-8"))[1:3]]
                    line_end, col_end = [int(i) for i in vim.eval(u'getpos("\'>")'.encode(u"utf-8"))[1:3]]
                    if line_start >= heading.start_vim and line_end > heading.start_vim:
                        focus_heading = heading
                else:
                    focus_heading = heading

                    # so far no heading has been found that the next focus should be on
        if not focus_heading:
            if not skip_children and direction == Direction.FORWARD and heading.children:
                focus_heading = heading.children[0]
            elif direction == Direction.FORWARD and heading.next_sibling:
                focus_heading = heading.next_sibling
            elif direction == Direction.BACKWARD and heading.previous_sibling:
                focus_heading = heading.previous_sibling
                if not skip_children:
                    while focus_heading.children:
                        focus_heading = focus_heading.children[-1]
            else:
                if direction == Direction.FORWARD:
                    focus_heading = current_heading.next_heading
                else:
                    focus_heading = current_heading.previous_heading

        noheadingfound = False
        if not focus_heading:
            if mode in (u"visual", u"operator"):
                # the cursor seems to be on the last or first heading of this
                # document and performes another next/previous operation
                focus_heading = heading
                noheadingfound = True
            else:
                if direction == Direction.FORWARD:
                    echo(u"Already focussing last heading")
                else:
                    echo(u"Already focussing first heading")
                return

        if mode == u"visual":
            cls._change_visual_selection(
                current_heading, focus_heading, direction=direction, noheadingfound=noheadingfound
            )
        elif mode == u"operator":
            if direction == Direction.FORWARD and vim.current.window.cursor[0] >= focus_heading.start_vim:
                vim.current.window.cursor = (
                    focus_heading.end_vim,
                    len(vim.current.buffer[focus_heading.end].decode(u"utf-8")),
                )
            else:
                vim.current.window.cursor = (focus_heading.start_vim, 0)
        else:
            vim.current.window.cursor = (focus_heading.start_vim, focus_heading.level + 1)
        if noheadingfound:
            return
        return focus_heading
Example #8
0
    def _focus_heading(cls,
                       mode,
                       direction=DIRECTION_FORWARD,
                       skip_children=False):
        u"""
		Focus next or previous heading in the given direction

		:direction: True for next heading, False for previous heading
		:returns: next heading or None
		"""
        d = ORGMODE.get_document()
        current_heading = d.current_heading()
        heading = current_heading
        focus_heading = None
        # FIXME this is just a piece of really ugly and unmaintainable code. It
        # should be rewritten
        if not heading:
            if direction == DIRECTION_FORWARD and d.headings \
              and vim.current.window.cursor[0] < d.headings[0].start_vim:
                # the cursor is in the meta information are, therefore focus
                # first heading
                focus_heading = d.headings[0]
            if not (heading or focus_heading):
                if mode == u'visual':
                    # restore visual selection when no heading was found
                    vim.command(u'normal gv'.encode(u'utf-8'))
                else:
                    echo(u'No heading found')
                return
        elif direction == DIRECTION_BACKWARD:
            if vim.current.window.cursor[0] != heading.start_vim:
                # the cursor is in the body of the current heading, therefore
                # the current heading will be focused
                if mode == u'visual':
                    line_start, col_start = [
                        int(i) for i in vim.eval(u'getpos("\'<")'.encode(
                            u'utf-8'))[1:3]
                    ]
                    line_end, col_end = [
                        int(i) for i in vim.eval(u'getpos("\'>")'.encode(
                            u'utf-8'))[1:3]
                    ]
                    if line_start >= heading.start_vim and line_end > heading.start_vim:
                        focus_heading = heading
                else:
                    focus_heading = heading

        # so far no heading has been found that the next focus should be on
        if not focus_heading:
            if not skip_children and direction == DIRECTION_FORWARD and heading.children:
                focus_heading = heading.children[0]
            elif direction == DIRECTION_FORWARD and heading.next_sibling:
                focus_heading = heading.next_sibling
            elif direction == DIRECTION_BACKWARD and heading.previous_sibling:
                focus_heading = heading.previous_sibling
                if not skip_children:
                    while focus_heading.children:
                        focus_heading = focus_heading.children[-1]
            else:
                if direction == DIRECTION_FORWARD:
                    focus_heading = current_heading.next_heading
                else:
                    focus_heading = current_heading.previous_heading

        noheadingfound = False
        if not focus_heading:
            if mode in (u'visual', u'operator'):
                # the cursor seems to be on the last or first heading of this
                # document and performes another next/previous operation
                focus_heading = heading
                noheadingfound = True
            else:
                if direction == DIRECTION_FORWARD:
                    echo(u'Already focussing last heading')
                else:
                    echo(u'Already focussing first heading')
                return

        if mode == u'visual':
            cls._change_visual_selection(current_heading,
                                         focus_heading,
                                         direction=direction,
                                         noheadingfound=noheadingfound)
        elif mode == u'operator':
            if direction == DIRECTION_FORWARD and vim.current.window.cursor[
                    0] >= focus_heading.start_vim:
                vim.current.window.cursor = (
                    focus_heading.end_vim,
                    len(vim.current.buffer[focus_heading.end].decode(u'utf-8'))
                )
            else:
                vim.current.window.cursor = (focus_heading.start_vim, 0)
        else:
            vim.current.window.cursor = (focus_heading.start_vim,
                                         focus_heading.level + 1)
        if noheadingfound:
            return
        return focus_heading
Example #9
0
    def _focus_heading(cls,
                       mode,
                       direction=DIRECTION_FORWARD,
                       skip_children=False):
        u"""
		Focus next or previous heading in the given direction

		:direction: True for next heading, False for previous heading
		:returns: next heading or None
		"""
        d = ORGMODE.get_document()
        current_heading = d.current_heading()
        heading = current_heading
        focus_heading = None
        if not heading:
            if direction == DIRECTION_FORWARD and d.headings \
              and vim.current.window.cursor[0] < d.headings[0].start_vim:
                focus_heading = d.headings[0]
            if not (heading or focus_heading):
                if mode == u'visual':
                    # restore visual selection when no heading was found
                    vim.command(u'normal gv'.encode(u'utf-8'))
                else:
                    echo(u'No heading found')
                return
        elif direction == DIRECTION_BACKWARD:
            if vim.current.window.cursor[0] != heading.start_vim:
                if mode == u'visual':
                    # TODO maybe this has to be changed!
                    line_start, col_start = [
                        int(i) for i in vim.eval(u'getpos("\'<")'.encode(
                            u'utf-8'))[1:3]
                    ]
                    line_end, col_end = [
                        int(i) for i in vim.eval(u'getpos("\'>")'.encode(
                            u'utf-8'))[1:3]
                    ]
                    if line_start >= heading.start_vim and line_end > heading.start_vim:
                        focus_heading = heading
                else:
                    focus_heading = heading

        if not focus_heading:
            if not skip_children and direction == DIRECTION_FORWARD and heading.children:
                focus_heading = heading.children[0]
            elif direction == DIRECTION_FORWARD and heading.next_sibling:
                focus_heading = heading.next_sibling
            elif direction == DIRECTION_BACKWARD and heading.previous_sibling:
                focus_heading = heading.previous_sibling
                if not skip_children:
                    while focus_heading.children:
                        focus_heading = focus_heading.children[-1]
            else:
                while heading.level > 1:
                    if heading.parent:
                        if direction == DIRECTION_FORWARD and heading.parent.next_sibling:
                            focus_heading = heading.parent.next_sibling
                            break
                        elif direction == DIRECTION_BACKWARD:
                            focus_heading = heading.parent
                            break
                        else:
                            heading = heading.parent
                    else:
                        break

        noheadingfound = False
        if not focus_heading:
            if mode in (u'visual', u'operator'):
                # the cursor seems to be on the last or first heading of this
                # document and performes another next/previous-operation
                focus_heading = heading
                noheadingfound = True
            else:
                if direction == DIRECTION_FORWARD:
                    echo(u'Already focussing last heading')
                else:
                    echo(u'Already focussing first heading')
                return

        if mode == u'visual':
            cls._change_visual_selection(current_heading,
                                         focus_heading,
                                         mode,
                                         direction=direction,
                                         noheadingfound=noheadingfound)
        elif mode == u'operator':
            if direction == DIRECTION_FORWARD and vim.current.window.cursor[
                    0] >= focus_heading.start_vim:
                vim.current.window.cursor = (
                    focus_heading.end_vim,
                    len(vim.current.buffer[focus_heading.end]))
            else:
                vim.current.window.cursor = (focus_heading.start_vim, 0)
        else:
            vim.current.window.cursor = (focus_heading.start_vim,
                                         focus_heading.level + 1)
        if noheadingfound:
            return
        return focus_heading
Example #10
0
	def _focus_heading(cls, mode, direction=DIRECTION_FORWARD, skip_children=False):
		u"""
		Focus next or previous heading in the given direction

		:direction: True for next heading, False for previous heading
		:returns: next heading or None
		"""
		d = ORGMODE.get_document()
		current_heading = d.current_heading()
		heading = current_heading
		focus_heading = None
		if not heading:
			if direction == DIRECTION_FORWARD and d.headings \
					and vim.current.window.cursor[0] < d.headings[0].start_vim:
				focus_heading = d.headings[0]
			if not (heading or focus_heading):
				if mode == u'visual':
					# restore visual selection when no heading was found
					vim.command(u'normal gv'.encode(u'utf-8'))
				else:
					echo(u'No heading found')
				return
		elif direction == DIRECTION_BACKWARD:
			if vim.current.window.cursor[0] != heading.start_vim:
				if mode == u'visual':
					# TODO maybe this has to be changed!
					line_start, col_start = [ int(i) for i in vim.eval(u'getpos("\'<")'.encode(u'utf-8'))[1:3] ]
					line_end, col_end = [ int(i) for i in vim.eval(u'getpos("\'>")'.encode(u'utf-8'))[1:3] ]
					if line_start >= heading.start_vim and line_end > heading.start_vim:
						focus_heading = heading
				else:
					focus_heading = heading

		if not focus_heading:
			if not skip_children and direction == DIRECTION_FORWARD and heading.children:
				focus_heading = heading.children[0]
			elif direction == DIRECTION_FORWARD and heading.next_sibling:
				focus_heading = heading.next_sibling
			elif direction == DIRECTION_BACKWARD and heading.previous_sibling:
				focus_heading = heading.previous_sibling
				if not skip_children:
					while focus_heading.children:
						focus_heading = focus_heading.children[-1]
			else:
				while heading.level > 1:
					if heading.parent:
						if direction == DIRECTION_FORWARD and heading.parent.next_sibling:
							focus_heading = heading.parent.next_sibling
							break
						elif direction == DIRECTION_BACKWARD:
							focus_heading = heading.parent
							break
						else:
							heading = heading.parent
					else:
						break

		noheadingfound = False
		if not focus_heading:
			if mode in (u'visual', u'operator'):
				# the cursor seems to be on the last or first heading of this
				# document and performes another next/previous-operation
				focus_heading = heading
				noheadingfound = True
			else:
				if direction == DIRECTION_FORWARD:
					echo(u'Already focussing last heading')
				else:
					echo(u'Already focussing first heading')
				return

		if mode == u'visual':
			cls._change_visual_selection(current_heading, focus_heading, mode, direction=direction, noheadingfound=noheadingfound)
		elif mode == u'operator':
			if direction == DIRECTION_FORWARD and vim.current.window.cursor[0] >= focus_heading.start_vim:
				vim.current.window.cursor = (focus_heading.end_vim, len(vim.current.buffer[focus_heading.end]))
			else:
				vim.current.window.cursor = (focus_heading.start_vim, 0)
		else:
			vim.current.window.cursor = (focus_heading.start_vim, focus_heading.level + 1)
		if noheadingfound:
			return
		return focus_heading