-
Notifications
You must be signed in to change notification settings - Fork 5
/
killring.cpp
78 lines (68 loc) · 1.45 KB
/
killring.cpp
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
#include "killring.h"
#include <QApplication>
#include <QClipboard>
#include <QDebug>
KillRing::KillRing()
: currentView(0), iter(ring.begin()), ignore(false)
{
connect(QApplication::clipboard(), SIGNAL(dataChanged()),
SLOT(clipboardDataChanged()));
}
KillRing* KillRing::instance()
{
static KillRing* instance;
if (!instance) {
instance = new KillRing();
}
return instance;
}
void KillRing::ignoreNextClipboardChange()
{
ignore = true;
}
void KillRing::add(const QString& text)
{
if (text.isEmpty()) {
return;
}
if (ignore) {
ignore = false;
return;
}
// original emacs implementation does not remove duplicates
ring.removeAll(text);
ring.prepend(text);
// shrink ring to default emacs max size
while (ring.count() > 60) {
ring.pop_back();
}
iter = ring.begin();
}
QString KillRing::next()
{
if (ring.isEmpty()) {
return QString::null;
}
else if (++iter == ring.end()) {
iter = ring.begin();
}
KillRing::instance()->ignoreNextClipboardChange();
QApplication::clipboard()->setText(*iter);
return *iter;
}
void KillRing::setCurrentYankView(QWidget* view)
{
currentView = view;
}
QWidget* KillRing::currentYankView() const
{
return currentView;
}
void KillRing::clipboardDataChanged()
{
qDebug() << "clipboard changed " << QApplication::clipboard()->text()
<< endl;
// TODO handle mouse selection too, optionally
QString text(QApplication::clipboard()->text());
add(text);
}