forked from optilude/SublimeTextMisc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboardHistory.py
109 lines (85 loc) · 3.16 KB
/
clipboardHistory.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import threading
import sublime, sublime_plugin
class HistoryList(list):
"""List type for storing the history - fairly
inefficient, but useful.
"""
SIZE = 256
index = 0
def append(self, item, update_index=True):
self.insert(0, item)
if update_index:
self.index = 0
if len(self) > self.SIZE:
del self[self.SIZE:]
def current(self):
if len(self) == 0:
return None
return self[self.index]
def next(self):
if self.index > 0:
self.index -= 1
def previous(self):
if self.index < len(self) - 1:
self.index += 1
_LOCK = threading.RLock()
_HISTORY = HistoryList()
class ClipboardHistoryBase(sublime_plugin.TextCommand):
def update_clipboard(self, content):
sublime.set_clipboard(content)
def next(self):
with _LOCK:
_HISTORY.next()
self.update_clipboard(_HISTORY.current())
def previous(self):
with _LOCK:
_HISTORY.previous()
self.update_clipboard(_HISTORY.current())
def appendClipboard(self):
with _LOCK:
# append the contents of the clipboard to the history if it is unique
if not self.onCurrent():
_HISTORY.append(sublime.get_clipboard())
def onCurrent(self):
return sublime.get_clipboard() == _HISTORY.current()
class ClipboardHistoryPaste(ClipboardHistoryBase):
def run(self, edit):
# If the user pastes something that was copied in a different program, it will not be in sublime's buffer, so we attempt to append every time
self.appendClipboard()
self.view.run_command('paste')
class ClipboardHistoryPasteAndIndent(ClipboardHistoryBase):
def run(self, edit):
self.appendClipboard()
self.view.run_command('paste_and_indent')
class ClipboardHistoryCut(ClipboardHistoryBase):
def run(self, edit):
# First run sublime's command to extract the selected text.
# This will set the cut/copy'd data on the clipboard which we can easily steal without recreating the cut/copy logic.
self.view.run_command('cut')
self.appendClipboard()
class ClipboardHistoryCopy(ClipboardHistoryBase):
def run(self, edit):
self.view.run_command('copy')
self.appendClipboard()
class ClipboardHistoryNext(ClipboardHistoryBase):
def run(self, edit):
self.next()
class ClipboardHistoryPrevious(ClipboardHistoryBase):
def run(self, edit):
self.previous()
class ClipboardHistoryPreviousAndPaste(ClipboardHistoryBase):
def run(self, edit):
self.previous()
self.view.run_command('paste')
class ClipboardHistoryChooseAndPaste(ClipboardHistoryBase):
def run(self, edit):
def on_done(idx):
if idx >= 0:
with _LOCK:
_HISTORY.index = idx
self.update_clipboard(_HISTORY.current())
self.view.run_command('paste')
def format(line):
return line.replace('\n', '$ ')[:64]
lines = map(format, _HISTORY)
sublime.active_window().show_quick_panel(lines, on_done)