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_encode(u'normal! gv')) else: echo(u'No heading found') return if not heading.parent: if mode == u'visual': vim.command(u_encode(u'normal! gv')) 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
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_encode(u'normal! gv')) else: echo(u'No heading found') return if not heading.parent or not heading.parent.next_sibling: if mode == u'visual': vim.command(u_encode(u'normal! gv')) 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
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
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
def clock_total(cls): d = ORGMODE.get_document() current_heading = d.current_heading() current_heading.init_logbook() total = get_total_time(current_heading) if total is not None: hours, minutes = divmod(total.total_seconds(), 3600) echo(u'Total time spent in this heading: %d:%d' % (hours, minutes // 60))
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_encode(u'normal! gv')) 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_encode(u'getpos("\'<")'))[1:3] ] line_end, col_end = [ int(i) for i in vim.eval(u_encode(u'getpos("\'>")'))[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(u_decode(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
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