~kiwec/ke

ke/src/History.cc -rw-r--r-- 968 bytes
1cc8481d — Wolf Clément Fix compilation 3 years ago
                                                                                
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
#include "History.h"
#include "Buffer.h"

void History::log_event(Event* ev) {
	// Don't log events caused by undo/redo
	if(m_working) return;

	if(m_done.empty()) {
		// Empty history - don't try merging
		m_done.push(std::move(ev));
	} else {
		// Try merging the event
		auto last_ev = m_done.top();
		if(!last_ev->merge(ev)) {
			m_done.push(std::move(ev));
		}
	}

	// Empty undone stack (can't redo after insertion)
	while(!m_undone.empty()) {
		delete m_undone.top();
		m_undone.pop();
	}
}

bool History::undo(Buffer& buf) {
	if(m_done.empty()) return false;

	m_working = true;
	auto event = std::move(m_done.top());
	event->undo(buf);
	m_undone.push(std::move(event));
	m_done.pop();
	m_working = false;

	return true;
}

bool History::redo(Buffer& buf) {
	if(m_undone.empty()) return false;

	m_working = true;
	auto event = std::move(m_undone.top());
	event->redo(buf);
	m_done.push(std::move(event));
	m_undone.pop();
	m_working = false;

	return true;
}