set primary selection
[knntp.git] / editor.d
blob13c35418fba19c38943e197850b35646805110c7
1 /* DigitalMars NNTP reader
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 module editor is aliced;
20 import iv.cmdcon;
21 import iv.strex;
22 import iv.utfutil;
23 import iv.vfs;
26 // ////////////////////////////////////////////////////////////////////////// //
27 // very simple wrapping text editor, without much features
28 class Editor {
29 private:
30 int linewrap = 72; // "normal" line length
31 int maxlinelen = 79; // maximum line length
33 string[] lines;
35 int cx, cy;
36 int markx, marky = -1;
38 public:
39 enum SpecCh : dchar {
40 Left = '\x01',
41 Right = '\x02',
42 Up = '\x03',
43 Down = '\x04',
44 Home = '\x05',
45 End = '\x06',
46 PageUp = '\x07',
47 Backspace = '\x08',
48 Tab = '\x09',
49 Enter = '\x0a',
50 PageDown = '\x0b',
51 KillLine = '\x0c',
52 PutMark = '\x0e',
53 ResetMark = '\x0f',
54 Delete = cast(dchar)127,
57 private:
58 @property bool hasMark () const pure nothrow @safe @nogc { return (marky >= 0); }
60 int xy2pos (int x, int y) const pure nothrow @safe @nogc {
61 if (y < 0) return 0;
62 if (y >= lines.length) y = cast(int)lines.length;
63 uint pos = 0;
64 foreach (immutable yy; 0..y-1) pos += linelen(yy)+1; // 1 for virtual EOL
65 if (x > 0 && y < lines.length) {
66 Utf8DecoderFast dc;
67 string s = lines[y];
68 while (s.length) {
69 if (dc.decode(cast(ubyte)s[0])) {
70 ++pos;
71 if (--x == 0) break;
73 s = s[1..$];
76 return pos;
79 public:
80 this () {
83 void addLine (const(char)[] s) {
84 lines ~= s.idup;
87 @property int lineCount () const pure nothrow @safe @nogc { return cast(int)lines.length; }
89 @property int curx () const pure nothrow @safe @nogc { return cx; }
90 @property int cury () const pure nothrow @safe @nogc { return cy; }
92 bool isMarked (int x, int y) const pure nothrow @safe @nogc {
93 if (!hasMark) return false;
94 if (cy < marky) {
95 if (y < cy || y > marky) return false;
96 if (y > cy && y < marky) return true;
97 if (y == cy) return (x >= cx);
98 if (y == marky) return (x < markx);
99 assert(0, "wtf?!");
100 } else if (cy > marky) {
101 if (y < marky || y > cy) return false;
102 if (y > marky && y < cy) return true;
103 if (y == marky) return (x >= markx);
104 if (y == cy) return (x < cx);
105 assert(0, "wtf?!");
106 } else {
107 if (y != marky) return false;
108 if (cx == markx) return false;
109 return (cx < markx ? (x >= cx && x < markx) : (x >= markx && x < cx));
111 assert(0, "wtf?!");
114 bool lineHasMark (int y) const pure nothrow @safe @nogc {
115 if (!hasMark) return false;
116 if (cy == marky) return (y == cy);
117 return (cy < marky ? (y >= cy && y <= marky) : (y >= marky && y <= cy));
120 string getSelectionText () const pure nothrow @safe {
121 if (!hasMark || (cx == markx && cy == marky)) return null;
122 int sx, sy, ex, ey;
123 if (cy < marky) { sx = cx; sy = cy; ex = markx; ey = marky; }
124 else if (cy > marky) { sx = markx; sy = marky; ex = cx; ey = cy; }
125 else if (cx < markx) { sx = cx; sy = cy; ex = markx; ey = marky; }
126 else { sx = markx; sy = marky; ex = cx; ey = cy; }
127 string res;
128 while (sx != ex || sy != ey) {
129 dchar ch = chatAt(sx, sy);
130 if (ch == 0) {
131 res ~= "\n";
132 sx = 0;
133 ++sy;
134 } else {
135 res ~= ch;
136 ++sx;
139 return res;
142 int quoteLevel (int lidx) const pure nothrow @safe @nogc {
143 if (lidx < 0 || lidx >= lines.length) return 0;
144 string s = lines[lidx];
145 int res = 0;
146 while (s.length) {
147 if (s[0] <= ' ') { s = s[1..$]; continue; }
148 if (s[0] != '>') break;
149 ++res;
150 s = s[1..$];
152 return res;
155 string opIndex (int idx) const pure nothrow @safe @nogc { return (idx >= 0 && idx < lines.length ? lines[idx] : null); }
157 int linelen (int idx) const pure nothrow @safe @nogc {
158 if (idx < 0 || idx >= lines.length) return 0;
159 string s = lines[idx];
160 if (s.length == 0) return 0;
161 Utf8DecoderFast dc;
162 int res = 0;
163 while (s.length) {
164 if (dc.decode(cast(ubyte)s[0])) ++res;
165 s = s[1..$];
167 return res;
170 // x position to line offset
171 int lineofs (int x, int idx) const pure nothrow @safe @nogc {
172 if (x <= 0 || idx < 0 || idx >= lines.length) return 0;
173 string s = lines[idx];
174 if (x >= s.length) return cast(int)s.length;
175 Utf8DecoderFast dc;
176 int res = 0;
177 while (s.length) {
178 ++res;
179 if (dc.decode(cast(ubyte)s[0])) {
180 if (--x == 0) break;
182 s = s[1..$];
184 return res;
187 dchar chatAt (int x, int y) const pure nothrow @safe @nogc {
188 if (x < 0 || y < 0 || y >= lines.length) return 0;
189 string s = lines[y];
190 if (s.length == 0 || x >= s.length) return 0;
191 Utf8DecoderFast dc;
192 while (s.length) {
193 if (dc.decode(cast(ubyte)s[0])) {
194 if (x-- == 0) return dc.codepoint;
196 s = s[1..$];
198 return 0;
201 // reformat whole text
202 void reformat () {
203 cx = 0;
204 cy = 0;
206 while (cy < lines.length) {
207 if (linelen(cy) > maxlinelen) {
208 int pos = linewrap-1;
209 while (pos >= 0 && chatAt(pos, cy) > ' ') --pos;
210 if (pos > 0) {
211 cx = ++pos;
212 doEnter();
213 } else {
214 ++cy;
217 // join and rewrap
218 if (quoteLevel(cy) != quoteLevel(cy+1)) { ++cy; continue; }
219 if (lines[cy].length == 0 || lines[cy][$-1] > ' ') { ++cy; continue; }
220 doLineJoin();
223 cx = 0;
224 cy = cast(int)lines.length;
227 void putUtf (const(char)[] s) {
228 Utf8DecoderFast dc;
229 while (s.length) {
230 if (dc.decode(cast(ubyte)s[0])) {
231 if (dc.isValidDC(dc.codepoint)) putChar(dc.codepoint); else putChar('?');
233 s = s[1..$];
237 void putChar (dchar ch) {
238 if (ch == SpecCh.PutMark) { markx = cx; marky = cy; return; }
239 if (ch == SpecCh.ResetMark) { markx = 0; marky = -1; return; }
240 if (ch == 13 || ch == 10) { marky = -1; doEnter(); return; }
241 if (ch == SpecCh.Backspace) { marky = -1; doBackspace(); return; }
242 if (ch == SpecCh.Delete) { marky = -1; doDelete(); return; }
243 if (ch < ' ') {
244 switch (ch) {
245 case SpecCh.Left: doLeft(); break;
246 case SpecCh.Right: doRight(); break;
247 case SpecCh.Up: doUp(); break;
248 case SpecCh.Down: doDown(); break;
249 case SpecCh.Home: doHome(); break;
250 case SpecCh.End: doEnd(); break;
251 case SpecCh.KillLine:
252 marky = -1;
253 if (cy < lines.length) {
254 import std.algorithm : remove;
255 lines = lines.remove(cy);
256 cx = 0;
258 break;
259 default:
261 return;
263 marky = -1;
264 string s;
265 if (cy >= lines.length) {
266 cx = 0;
267 cy = cast(int)lines.length;
268 s ~= ch;
269 lines ~= s;
270 } else {
271 int len = linelen(cy);
272 if (cx >= len) {
273 cx = len;
274 lines[cy] ~= ch;
275 } else {
276 s = lines[cy];
277 int ofs = lineofs(cx, cy);
278 if (ofs >= s.length) {
279 s ~= ch;
280 } else {
281 string t = s[0..ofs];
282 t ~= ch;
283 s = t~s[ofs..$];
284 lines[cy] = s;
288 doRight();
289 // wrapping
290 if (linelen(cy) > linewrap) {
291 int ocx = cx, ocy = cy;
292 while (cy < lines.length) {
293 if (linelen(cy) > linewrap) {
294 int pos = linewrap-1;
295 while (pos >= 0 && chatAt(pos, cy) > ' ') --pos;
296 if (pos > 0) {
297 cx = ++pos;
298 doEnter();
299 } else {
300 ++cy;
303 // join and rewrap
304 if (quoteLevel(cy) != quoteLevel(cy+1)) break;
305 if (lines[cy].length == 0 || lines[cy][$-1] > ' ') break;
306 doLineJoin();
308 // fix cursor coordinates
309 cx = ocx;
310 cy = ocy;
311 while (cy < lines.length) {
312 if (cy == ocy) {
313 if (cx < linelen(cy)) break;
314 } else {
315 if (cx <= linelen(cy)) break;
317 cx -= linelen(cy);
318 ++cy;
319 skipQuotes();
324 private void skipQuotes () {
325 if (cy < 0 || cy >= lines.length) return;
326 string s = lines[cy];
327 if (s.length == 0 || s[0] != '>') return;
328 int pos = 0;
329 while (pos < s.length) {
330 if (s[pos] == ' ' || s[pos] == '>') {
331 ++cx;
332 ++pos;
333 } else {
334 break;
337 if (pos < s.length && s[pos] == ' ') ++cx;
340 void doLineJoin () {
341 import std.algorithm : remove;
342 if (cy < 0) cy = 0;
343 if (cy >= lines.length || lines.length-cy < 2) return;
344 cx = linelen(cy);
345 string s = lines[cy+1];
346 usize stpos = 0;
347 if (s.length > 0 && s[0] == '>') {
348 while (stpos < s.length) if (s[stpos] == ' ' || s[stpos] == '>') ++stpos; else break;
349 while (stpos < s.length && s[stpos] <= ' ') ++stpos;
350 s = s[stpos..$];
351 string cs = lines[cy];
352 if (cs.length == 0 || cs[$-1] != ' ') lines[cy] ~= " ";
354 lines[cy] ~= s;
355 lines = lines.remove(cy+1);
358 void doEnter () {
359 if (cy >= lines.length) {
360 lines ~= null;
361 cx = 0;
362 cy = cast(int)lines.length;
363 } else {
364 string s = lines[cy];
365 int ql = quoteLevel(cy);
366 int ofs = lineofs(cx, cy);
367 lines.length += 1;
368 foreach_reverse (immutable c; cy+1..lines.length) lines[c] = lines[c-1];
369 lines[cy] = s[0..ofs];
370 lines[cy+1] = s[ofs..$];
371 cx = 0;
372 ++cy;
373 while (ql-- > 0) { lines[cy] = ">"~lines[cy]; ++cx; }
377 void doBackspace () {
378 if (cy >= lines.length) {
379 if (cx > 0) --cx;
380 return;
381 } else if (cx > 0) {
382 string s = lines[cy];
383 int ofs1 = lineofs(cx, cy);
384 int ofs0 = lineofs(--cx, cy);
385 if (ofs1 > 0) {
386 lines[cy] = s[0..ofs0];
387 if (ofs1 < s.length) lines[cy] ~= s[ofs1..$];
388 return;
391 // join
392 if (cy > 0) {
393 --cy;
394 cx = linelen(cy);
395 doLineJoin();
399 void doDelete () {
400 void doJoin () {
401 if (cy >= lines.length || lines.length-cy < 2) return;
402 cx = linelen(cy);
403 doLineJoin();
406 if (cy < lines.length) {
407 string s = lines[cy];
408 int ofs0 = lineofs(cx, cy);
409 if (ofs0 >= s.length) { doJoin(); return; }
410 int ofs1 = lineofs(cx+1, cy);
411 if (ofs0 == ofs1) { doJoin(); return; }
412 lines[cy] = s[0..ofs0];
413 if (ofs1 < s.length) lines[cy] ~= s[ofs1..$];
417 void doLeft () {
418 int len = linelen(cy);
419 if (cx > len) cx = len;
420 if (cx == 0) {
421 if (cy > 0) {
422 --cy;
423 cx = linelen(cy);
425 } else {
426 --cx;
430 void doRight () {
431 auto len = linelen(cy);
432 if (cx > len) cx = len;
433 if (cx >= len) {
434 if (cy < lines.length) { ++cy; cx = 0; }
435 } else {
436 ++cx;
440 void doUp () {
441 if (cy > 0) --cy;
444 void doDown () {
445 if (cy < lines.length) ++cy;
448 void doHome () {
449 cx = 0;
452 void doEnd () {
453 cx = linelen(cy);