forked from SublimeText-Markdown/MarkdownEditing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
quote_indenting.py
73 lines (53 loc) · 2.36 KB
/
quote_indenting.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
62
63
64
65
66
67
68
69
70
71
72
73
import re
import sublime
import sublime_plugin
class IndentQuote(sublime_plugin.TextCommand):
def description(self):
return 'Indent a quote'
def run(self, edit):
view = self.view
selections = view.sel()
new_selections = []
for selection in selections:
lines_in_selection = self.view.lines(selection)
all_lines = []
expanded_selection_start = lines_in_selection[0].begin()
for line in lines_in_selection:
complete_line = view.line(line)
expanded_selection_end = complete_line.end()
text = view.substr(complete_line)
all_lines.append("> " + text)
expanded_selection = sublime.Region(expanded_selection_start, expanded_selection_end)
replacement_text = "\n".join(all_lines)
view.replace(edit, expanded_selection, replacement_text)
new_selections.append(sublime.Region(expanded_selection_start, expanded_selection_start + len(replacement_text)))
selections.clear()
for selection in new_selections:
selections.add(selection)
def is_enabled(self):
return self.view.sel()
class DeindentQuote(sublime_plugin.TextCommand):
def description(self):
return 'Deindent a quote'
def run(self, edit):
view = self.view
selections = view.sel()
new_selections = []
for selection in selections:
lines_in_selection = self.view.lines(selection)
all_lines = []
expanded_selection_start = lines_in_selection[0].begin()
for line in lines_in_selection:
complete_line = view.line(line)
expanded_selection_end = complete_line.end()
text = view.substr(complete_line)
all_lines.append(re.sub(r'^(> )', '', text))
expanded_selection = sublime.Region(expanded_selection_start, expanded_selection_end)
replacement_text = "\n".join(all_lines)
view.replace(edit, expanded_selection, replacement_text)
new_selections.append(sublime.Region(expanded_selection_start, expanded_selection_start + len(replacement_text)))
selections.clear()
for selection in new_selections:
selections.add(selection)
def is_enabled(self):
return self.view.sel()