-
Notifications
You must be signed in to change notification settings - Fork 32
/
headline_move.py
61 lines (48 loc) · 2.02 KB
/
headline_move.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""This module provides commands for easily moving between headilnes.
The feature is borrowed from [Org-mode](http://org-mode.org).
"""
# Author: Muchenxuan Tong <demon386@gmail.com>
import sublime
import sublime_plugin
try:
from . import headline
from .utilities import is_region_void
except ValueError:
import headline
from utilities import is_region_void
class HeadlineMoveCommand(sublime_plugin.TextCommand):
def run(self, edit, forward=True, same_level=True):
"""Move between headlines, forward or backward.
If same_level is true, only move to headline with the same level
or higher level.
"""
new_sel = []
if same_level:
level_type = headline.MATCH_PARENT
else:
level_type = headline.MATCH_ANY
for region in self.view.sel():
if same_level:
_, level = headline.headline_and_level_at_point(self.view,\
region.a,
search_above_and_down=True)
if level is None:
return
else:
level = headline.ANY_LEVEL
match_region, _ = headline.find_headline(self.view, \
region.a, \
level, \
forward, \
level_type, \
skip_headline_at_point=True,\
skip_folded=True)
if is_region_void(match_region):
return
new_sel.append(sublime.Region(match_region.a, match_region.a))
self.adjust_view(new_sel)
def adjust_view(self, new_sel):
self.view.sel().clear()
for region in new_sel:
self.view.sel().add(region)
self.view.show(region)