-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcopy_edit.py
More file actions
94 lines (78 loc) · 3.11 KB
/
Copy pathcopy_edit.py
File metadata and controls
94 lines (78 loc) · 3.11 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import sublime, sublime_plugin
selection_strings = []
line_endings = {'CR': '\r', 'Unix': '\n', 'Windows': '\r\n'}
#A note about copy_with_empty_selection
#----------------------------------------
#The way the built-in copy works is like this:
#all non-empty sels: copy them
#all empty sels: copy each whole line
#some non-empty sels, some empty: copy only the non-empty
#We're not going to do that, though. If they want to copy empty lines, we'll
#always copy empty lines. I don't understand the copying empty lines in the
#first place, but I would rather be internally consistent.
def print_status_message(verb, numregions=None):
numregions = numregions or len(selection_strings)
numchars = sum([len(s) for s in selection_strings])
message = "{0} {1} character{2}".format(verb, numchars, 's' if numchars != 1 else '')
if numregions > 1:
message += " over {0} selection regions".format(numregions)
sublime.status_message(message)
class CopyEditCommand(sublime_plugin.TextCommand):
def copy(self, edit):
#See copy_with_empty_selection note above.
copy_with_empty_sel = self.view.settings().get("copy_with_empty_selection")
new_sel_strings = []
for s in self.view.sel():
if len(s):
new_sel_strings.append(self.view.substr(s))
elif copy_with_empty_sel:
new_sel_strings.append(self.view.substr(self.view.full_line(s)))
if len(new_sel_strings) > 0:
selection_strings[:] = [] #.clear() doesn't exist in 2.7
selection_strings.extend(new_sel_strings)
line_ending = line_endings[self.view.line_endings()]
sublime.set_clipboard(line_ending.join(selection_strings))
return True
return False
def run(self, edit, verb="Copied"):
if self.copy(edit):
print_status_message(verb)
class CutEditCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command("copy_edit", {"verb":"Cut"})
for s in reversed(self.view.sel()):
self.view.erase(edit, s)
class PasteEditCommand(sublime_plugin.TextCommand):
def run(self, edit):
#check if clipboard is more up to date
pasteboard = sublime.get_clipboard()
from_clipboard = False
if pasteboard != '\n'.join(selection_strings):
selection_strings[:] = [] #.clear() doesn't exist in 2.7
selection_strings.append(pasteboard)
from_clipboard = True #what should be done in this case?
numstrings = len(selection_strings)
numsels = len(self.view.sel())
if numsels == 0:
return
if numstrings <= numsels and numsels % numstrings == 0:
strs_per_sel = 1
elif numsels < numstrings and numstrings % numsels == 0:
strs_per_sel = int(numstrings / numsels)
else:
strs_per_sel = numstrings
str_index = 0
new_sels = []
for sel in self.view.sel():
self.view.erase(edit, sel)
insertion_point = sel.begin()
for string in selection_strings[str_index:str_index+strs_per_sel]:
self.view.insert(edit, insertion_point, string)
insertion_point += len(string)
region = sublime.Region(insertion_point)
new_sels.append(region)
str_index = (str_index + strs_per_sel) % numstrings
print_status_message("Pasted", len(self.view.sel()))
self.view.sel().clear()
for s in new_sels:
self.view.sel().add(s)