-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.cpp
233 lines (180 loc) · 4.24 KB
/
cursor.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
///////////////////////////////////////////////////////////////////////
// cursor.cpp -- Cursor class implementation
// Date: Fri Nov 1 21:22:27 2013
///////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include "cursor.hpp"
#include "buffer.hpp"
Cursor::csrmap_t Cursor::cursors_map; // csrid => Cursor *
std::unordered_map<regid_t,Cursor::csrmap_t> Cursor::buffers_map; // Bufid => cursors map
regid_t Cursor::next_id = 100;
Cursor::Cursor() {
csrid = next_id++;
bufid = 0;
lno = 0;
col = 0;
end_file = 0;
register_cursor();
}
Cursor::Cursor(const Cursor& csr) {
(*this) = csr;
this->csrid = next_id++;
register_cursor();
}
Cursor::Cursor(const char *bufname,lineno_t lno,colno_t col) {
this->csrid = next_id++;
this->bufid = Buffer::lookup_id(bufname);
assert(bufid);
this->lno = lno;
this->col = col;
Buffer *buf = Buffer::lookup(this->bufid);
size_t lines = buf->length();
if ( this->lno >= lines ) {
this->lno = lines + 1;
end_file = true;
} else end_file = false;
register_cursor();
}
Cursor::Cursor(regid_t bufid,lineno_t lno,colno_t col) {
csrid = next_id++;
this->bufid = bufid;
this->lno = lno;
this->col = col;
end_file = false;
register_cursor();
}
Cursor::~Cursor() {
unregister_cursor();
}
void
Cursor::register_cursor() {
cursors_map[csrid] = this;
csrmap_t& map = buffers_map[bufid];
map[csrid] = this;
}
void
Cursor::unregister_cursor() {
csrmap_t& map = buffers_map[bufid];
map.erase(csrid);
cursors_map.erase(csrid);
}
// Disassociate from all buffers
void
Cursor::disassociate() {
csrmap_t& omap = buffers_map[bufid];
omap.erase(csrid);
if ( omap.size() == 0 )
buffers_map.erase(bufid);
bufid = 0;
csrmap_t nmap = buffers_map[bufid];
nmap[csrid] = this;
}
// Reassociate cursor to new buffer
void
Cursor::reassociate(Buffer *buf) {
csrmap_t& omap = buffers_map[bufid];
omap.erase(csrid);
if ( omap.size() == 0 )
buffers_map.erase(bufid);
bufid = buf->get_id();
csrmap_t nmap = buffers_map[bufid];
nmap[csrid] = this;
}
Buffer *
Cursor::buffer() {
return Buffer::lookup(bufid);
}
bool
Cursor::nline(int offset) {
if ( offset < 0 )
pline(-offset);
Buffer *buf = buffer();
lineno_t total_lines = lineno_t(buf->length());
lineno_t loffset = lineno_t(offset);
if ( lno + loffset >= total_lines ) {
lno = total_lines;
return lno + loffset <= total_lines; // Return true if the last line was target
}
lno += loffset;
return true;
}
bool
Cursor::pline(int offset) {
if ( offset < 0 )
nline(-offset);
lineno_t loffset = lineno_t(offset);
if ( loffset >= lno ) {
lno = 0;
return loffset <= lno;
}
lno -= loffset;
return true;
}
//////////////////////////////////////////////////////////////////////
// Static methods for Cursors
//////////////////////////////////////////////////////////////////////
Cursor *
Cursor::lookup(csrid_t id) {
auto it = Cursor::cursors_map.find(id);
if ( it == Cursor::cursors_map.end() )
return 0; // Not found
return it->second;
}
// Buffer bufid was/is destroyed
void
Cursor::destroyed(regid_t bufid) {
csrmap_t& omap = buffers_map[bufid];
csrmap_t& zmap = buffers_map[0];
for ( auto it = omap.begin(); it != omap.end(); ++it ) {
Cursor *csr = it->second;
csr->bufid = 0;
zmap[csr->bufid] = csr;
}
buffers_map.erase(bufid);
}
// Buffer was reloaded with new content
void
Cursor::reloaded(regid_t bufid) {
csrmap_t& map = buffers_map[bufid];
for ( auto it = map.begin(); it != map.end(); ++it ) {
Cursor *csr = it->second;
if ( csr->end_file ) {
Buffer *buf = Buffer::lookup(bufid);
size_t lines = buf->length();
csr->lno = lines+1;
} else csr->lno = 0;
}
}
void
Cursor::set_line(lineno_t lno) {
Buffer *buf = buffer();
if ( !buf ) {
assert(!lno);
this->lno = lno;
return;
}
size_t total_lines = buf->length();
if ( size_t(lno) > total_lines )
lno = lineno_t(total_lines);
this->lno = lno;
}
void
Cursor::set_column(colno_t col) {
Buffer *buf = buffer();
if ( !buf ) {
assert(!col);
this->col = col;
return;
}
std::string text;
buf->get_line(text,lno);
if ( col > colno_t(text.size()) )
col = colno_t(text.size());
this->col = col;
}
// End cursor.cpp