Subversion Repositories Kolibri OS

Rev

Rev 9674 | Rev 9731 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8728 leency 1
(*
9560 akron1 2
    Copyright 2021, 2022 Anton Krotov
8728 leency 3
 
4
    This file is part of CEdit.
5
 
6
    CEdit is free software: you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation, either version 3 of the License, or
9
    (at your option) any later version.
10
 
11
    CEdit is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with CEdit. If not, see .
18
*)
19
 
20
MODULE Text;
21
 
22
IMPORT
23
    List, Lines,
24
    G := Graph,
25
    U := Utils,
26
    RW, Search,
27
    E := Encodings,
28
    CB := Clipboard,
9073 leency 29
    ChangeLog, File,
8728 leency 30
    Lang := Languages;
31
 
32
 
33
CONST
34
 
9174 akron1 35
    SPACE = Lines.SPACE;
36
    TAB = Lines.TAB;
37
    TAB1 = Lines.TAB1;
8728 leency 38
    lenEOL = CB.lenEOL;
39
 
40
    mark_width = 2;
41
    pad_left = mark_width + 3;
9060 leency 42
    pad_top = 0;
8728 leency 43
    inter = 2;
44
 
45
 
46
TYPE
47
 
9210 akron1 48
    tPoint* = RECORD
49
        X*, Y*: INTEGER
8728 leency 50
    END;
51
 
52
    pPoint = POINTER TO tPoint;
53
 
54
    tString* = ARRAY 1000 OF WCHAR;
55
 
56
    tLine = Lines.tLine;
57
 
58
    tGuard = POINTER TO RECORD (ChangeLog.tGuard)
59
        selected: BOOLEAN;
60
        cursor, select2, scroll: tPoint;
61
        CurX: INTEGER
62
    END;
63
 
64
    tText* = POINTER TO RECORD (List.tList)
65
        cursor, select, select2: pPoint;
66
        scroll: tPoint;
67
        CurX: INTEGER;
9668 akron1 68
        smallChange: INTEGER;
9671 akron1 69
        modified*, smallMove,
9674 akron1 70
        comments, guard, fasm*,
9671 akron1 71
        search, cs, whole: BOOLEAN;
8728 leency 72
        edition*: tGuard;
73
        curLine: tLine;
74
        lang*: INTEGER;
9336 akron1 75
        enc, eol: INTEGER;
8728 leency 76
        foundList: List.tList;
77
        foundSel: INTEGER;
9050 leency 78
        searchText: tString;
9668 akron1 79
        LinesVector: Lines.tVector;
9050 leency 80
        chLog*: ChangeLog.tLog;
9210 akron1 81
        maxLength*: INTEGER;
82
        fileName*: RW.tFileName
8728 leency 83
    END;
84
 
85
    tProcedure = PROCEDURE;
86
 
87
 
88
VAR
89
 
90
    pdelete: PROCEDURE (text: tText);
91
    ShowCursor: PROCEDURE;
92
 
9671 akron1 93
    colors: RECORD
8762 leency 94
                text, back, seltext, selback, modified, saved, curline, numtext, numback: INTEGER;
9413 akron1 95
                comment, string, escape, num, delim, key1, key2, key3: INTEGER
8728 leency 96
             END;
97
    canvas: G.tCanvas;
9174 akron1 98
    drawCursor: BOOLEAN;
8728 leency 99
    padding: RECORD left, top: INTEGER END;
100
    size, textsize: tPoint;
101
    charWidth, charHeight: INTEGER;
9671 akron1 102
    autoIndents*, lineNumbers*, autoBrackets*, trimSpace*: BOOLEAN;
9674 akron1 103
    idxTable: Search.IdxTable;
8728 leency 104
 
105
 
9210 akron1 106
PROCEDURE setLang* (text: tText; lang: INTEGER);
107
BEGIN
9674 akron1 108
	IF text.lang # lang THEN
109
		text.fasm := lang = Lang.langFasm;
110
		text.lang := lang
111
	END;
9210 akron1 112
    text.comments := TRUE;
113
    Lang.setCurLang(text.lang)
114
END setLang;
115
 
116
 
8728 leency 117
PROCEDURE setName* (text: tText; name: RW.tFileName);
118
VAR
119
    ext: RW.tFileName;
120
BEGIN
121
    text.fileName := name;
9210 akron1 122
    U.getFileName(name, ext, ".");
8728 leency 123
    U.upcase(ext);
9210 akron1 124
    setLang(text, Lang.getLang(ext))
8728 leency 125
END setName;
126
 
127
 
128
PROCEDURE getPos* (text: tText; VAR x, y: INTEGER);
129
BEGIN
130
    x := text.cursor.X + 1;
131
    y := text.cursor.Y + 1
132
END getPos;
133
 
134
 
135
PROCEDURE getScroll* (text: tText; VAR x, y: INTEGER);
136
BEGIN
137
    x := text.scroll.X;
138
    y := text.scroll.Y
139
END getScroll;
140
 
141
 
142
PROCEDURE getTextSize* (VAR x, y: INTEGER);
143
BEGIN
144
    x := textsize.X;
145
    y := textsize.Y
146
END getTextSize;
147
 
148
 
149
PROCEDURE getTextRect* (VAR left, top, rigth, bottom: INTEGER);
150
BEGIN
151
    left := padding.left - 1;
152
    top := padding.top - 1;
153
    rigth := size.X - 1;
154
    bottom := top + size.Y - 1;
155
END getTextRect;
156
 
157
 
9671 akron1 158
PROCEDURE toggleNumbers*;
8728 leency 159
BEGIN
9671 akron1 160
    lineNumbers := ~lineNumbers
8728 leency 161
END toggleNumbers;
162
 
163
 
9671 akron1 164
PROCEDURE toggleIndents*;
165
BEGIN
166
    autoIndents := ~autoIndents
167
END toggleIndents;
168
 
169
 
170
PROCEDURE toggleBrackets*;
171
BEGIN
172
    autoBrackets := ~autoBrackets
173
END toggleBrackets;
174
 
175
 
176
PROCEDURE toggleTrimSpace*;
177
BEGIN
178
    trimSpace := ~trimSpace
179
END toggleTrimSpace;
180
 
181
 
9174 akron1 182
PROCEDURE showCursor*;
183
BEGIN
184
	drawCursor := TRUE
185
END showCursor;
186
 
187
 
188
PROCEDURE hideCursor*;
189
BEGIN
190
	drawCursor := FALSE
191
END hideCursor;
192
 
193
 
8728 leency 194
PROCEDURE getString (src: tLine; pos, cnt: INTEGER; VAR dst: ARRAY OF WCHAR): INTEGER;
195
VAR
196
    i: INTEGER;
197
BEGIN
198
    i := 0;
199
    WHILE (pos < src.length) & (cnt > 0) DO
200
        IF i < LEN(dst) - 1 THEN
9659 akron1 201
            dst[i] := Lines.getChar(src, pos);
8728 leency 202
            INC(i)
203
        END;
204
        INC(pos);
205
        DEC(cnt)
206
    END;
207
    dst[i] := 0X
208
    RETURN i
209
END getString;
210
 
211
 
212
PROCEDURE NextLine (VAR line: tLine);
213
BEGIN
214
    line := line.next(tLine)
215
END NextLine;
216
 
217
 
218
PROCEDURE PrevLine (VAR line: tLine);
219
BEGIN
220
    line := line.prev(tLine)
221
END PrevLine;
222
 
223
 
224
PROCEDURE SetColor (textColor, backColor: INTEGER);
225
BEGIN
226
    G.SetTextColor(canvas, textColor);
227
    G.SetBkColor(canvas, backColor)
228
END SetColor;
229
 
230
 
231
PROCEDURE ProcessComments (line: tLine; VAR depth, pos: INTEGER; minDepth, n: INTEGER; lang: INTEGER);
232
VAR
233
    cond: INTEGER;
234
BEGIN
235
    cond := 0;
236
    WHILE (pos <= n) & (depth > minDepth) DO
237
        Lang.comments(line, depth, cond, pos, n, lang);
238
        INC(pos)
239
    END;
240
    DEC(pos)
241
END ProcessComments;
242
 
243
 
9668 akron1 244
PROCEDURE getLine2 (text: tText; n: INTEGER): tLine;
245
VAR
246
	item: tLine;
247
BEGIN
248
	IF (0 <= n) & (n < text.count) THEN
249
		item := Lines.getVectorItem(text.LinesVector, n)
250
	ELSE
251
		item := NIL
252
	END
253
	RETURN item
254
END getLine2;
255
 
256
 
8728 leency 257
PROCEDURE Comments (text: tText);
258
VAR
9668 akron1 259
	line: tLine;
260
	i, k, cout: INTEGER;
8728 leency 261
BEGIN
9668 akron1 262
	IF text.smallChange = 1 THEN
263
		line := getLine2(text, text.cursor.Y);
264
		IF line.prev # NIL THEN
265
			line.cin := line.prev(tLine).cout
266
		ELSE
267
			line.cin := 0
268
		END;
269
		cout := line.cout;
270
		line.cout := line.cin;
271
		i := 0;
272
		ProcessComments(line, line.cout, i, -1, line.length - 1, text.lang);
273
		IF line.cout # cout THEN
274
			text.smallChange := 0;
275
			Comments(text)
276
		END
277
	ELSE
278
		Lines.destroyVector(text.LinesVector);
279
		text.LinesVector := Lines.createVector(text.count);
280
		k := 0;
281
		line := text.first(tLine);
282
		Lines.setVectorItem(text.LinesVector, k, line);
283
		INC(k);
284
		line.cin := 0;
285
		line.cout := 0;
286
		i := 0;
287
		ProcessComments(line, line.cout, i, -1, line.length - 1, text.lang);
288
		NextLine(line);
289
		WHILE line # NIL DO
290
			Lines.setVectorItem(text.LinesVector, k, line);
291
			INC(k);
292
			line.cin := line.prev(tLine).cout;
293
			line.cout := line.cin;
294
			i := 0;
295
			ProcessComments(line, line.cout, i, -1, line.length - 1, text.lang);
296
			NextLine(line)
297
		END
298
	END;
299
	text.smallChange := 0;
300
	text.comments := FALSE
8728 leency 301
END Comments;
302
 
303
 
9674 akron1 304
PROCEDURE leadingSpaces (line: tLine): INTEGER;
305
VAR
306
    i: INTEGER;
307
BEGIN
308
    i := 0;
309
    WHILE Lines.isSpace(Lines.getChar(line, i)) DO
310
        INC(i)
311
    END
312
    RETURN i
313
END leadingSpaces;
314
 
315
 
8728 leency 316
PROCEDURE parse (text: tText; line: tLine; y: INTEGER; backColor: INTEGER; lang: INTEGER);
317
VAR
318
    c: WCHAR;
319
    i, n, k: INTEGER;
320
    cond, depth: INTEGER;
321
    color: INTEGER;
322
    hex: BOOLEAN;
323
    isDgt: PROCEDURE (c: WCHAR): BOOLEAN;
324
 
325
 
326
    PROCEDURE PrintLex (text: tText; line: tLine; lexStart, lexEnd: INTEGER; y: INTEGER; color, backColor: INTEGER);
327
    VAR
328
        lexLen: INTEGER;
329
    BEGIN
330
        SetColor(color, backColor);
331
        lexLen := MAX(MIN(line.length - lexStart, lexEnd - lexStart + 1), 0);
9193 akron1 332
        G.TextOut(canvas, padding.left + (lexStart - text.scroll.X) * charWidth, y, Lines.getPChar(line, lexStart), lexLen, color)
8728 leency 333
    END PrintLex;
334
 
335
 
9193 akron1 336
    PROCEDURE PrintComment (text: tText; line: tLine; VAR depth, i: INTEGER; w, y: INTEGER; backColor: INTEGER);
8728 leency 337
    VAR
338
        lexStart: INTEGER;
339
        color: INTEGER;
340
    BEGIN
341
        IF (text.lang = Lang.langLua) & ~ODD(depth) THEN
342
            color := colors.string
343
        ELSIF (text.lang = Lang.langIni) & (depth = 1) THEN
344
            color := colors.key2
345
        ELSIF (text.lang = Lang.langPascal) & (depth = 3) THEN
346
            color := colors.key3
347
        ELSE
348
            color := colors.comment
349
        END;
9193 akron1 350
        lexStart := MAX(i - w, 0);
8728 leency 351
        ProcessComments(line, depth, i, 0, line.length - 1, text.lang);
352
        PrintLex(text, line, lexStart, i, y, color, backColor)
353
    END PrintComment;
354
 
355
 
356
    PROCEDURE cap (c: WCHAR): WCHAR;
357
    BEGIN
358
        IF U.cap(c) THEN END
359
        RETURN c
360
    END cap;
361
 
362
 
363
    PROCEDURE UL (c: WCHAR): BOOLEAN;
364
        RETURN (cap(c) = "U") OR (cap(c) = "L")
365
    END UL;
366
 
367
 
368
    PROCEDURE FL (c: WCHAR): BOOLEAN;
369
        RETURN (cap(c) = "F") OR (cap(c) = "L")
370
    END FL;
371
 
372
 
373
    PROCEDURE ident (text: tText; VAR i: INTEGER; first, y: INTEGER; line: tLine; backColor: INTEGER; cs: BOOLEAN);
374
    VAR
375
        c: WCHAR;
376
        lexLen: INTEGER;
377
        s: ARRAY 32 OF WCHAR;
378
        color: INTEGER;
379
    BEGIN
9659 akron1 380
        c := Lines.getChar(line, i);
8728 leency 381
        WHILE U.isLetter(c) OR (c = "_") OR U.isDigit(c) DO
382
            INC(i);
9659 akron1 383
            c := Lines.getChar(line, i);
8728 leency 384
        END;
385
        DEC(i);
386
        lexLen := getString(line, first, i - first + 1, s);
387
        IF ~cs THEN
388
            U.upcase16(s)
389
        END;
390
        IF Lang.isKey(s, text.lang, 1) THEN
391
            color := colors.key1
392
        ELSIF Lang.isKey(s, text.lang, 2) THEN
393
            color := colors.key2
394
        ELSIF Lang.isKey(s, text.lang, 3) THEN
395
            color := colors.key3
396
        ELSE
397
            color := colors.text
398
        END;
399
        IF color # colors.text THEN
400
            PrintLex(text, line, first, i, y, color, backColor)
401
        END
402
    END ident;
403
 
404
 
405
    PROCEDURE String (text: tText; line: tLine; VAR i: INTEGER; y: INTEGER; backColor: INTEGER);
406
    VAR
9413 akron1 407
        k, j, Start, End: INTEGER;
408
        c: WCHAR;
8728 leency 409
    BEGIN
410
        k := i;
9413 akron1 411
        Lang.SkipString(line, i, line.length - 1, text.lang);
412
        PrintLex(text, line, k, i, y, colors.string, backColor);
413
        IF text.lang IN Lang.escLang THEN
414
	        Start := k + 1;
415
	        End := i - 1;
416
	        k := Start;
417
	        WHILE k <= End DO
9659 akron1 418
	        	c := Lines.getChar(line, k);
9413 akron1 419
	        	IF c = "\" THEN
420
	        		j := k;
421
	        		Lang.SkipEsc(line, k, line.length - 1, text.lang);
422
	        		PrintLex(text, line, j, k, y, colors.escape, backColor)
423
	        	END;
424
	        	INC(k)
425
	        END
426
        END
8728 leency 427
    END String;
428
 
429
 
430
BEGIN
431
    depth := line.cin;
432
    n := line.length - 1;
433
    i := 0;
434
    IF (depth > 0) & (n >= 0) THEN
9193 akron1 435
        PrintComment(text, line, depth, i, 2, y, backColor)
8728 leency 436
    END;
437
    cond := 0;
438
    WHILE i <= n DO
9659 akron1 439
        c := Lines.getChar(line, i);
8728 leency 440
 
441
        IF lang = Lang.langFasm THEN
442
 
443
            IF c = ";" THEN
444
                PrintLex(text, line, i, n, y, colors.comment, backColor);
445
                i := n
446
            ELSIF (c = "'") OR (c = '"') THEN
447
                String(text, line, i, y, backColor)
448
            ELSIF (U.isLetter(c) OR (c = "_")) THEN
9050 leency 449
                ident(text, i, i, y, line, backColor, Lang.isCS(lang))
8728 leency 450
            ELSIF U.isDigit(c) THEN
451
                hex := FALSE;
452
                k := i;
453
                INC(i);
9659 akron1 454
                c := Lines.getChar(line, i);
455
                IF (cap(c) = "X") & (Lines.getChar(line, i - 1) = "0") THEN
8728 leency 456
                    INC(i);
457
                    hex := TRUE
458
                END;
459
 
9659 akron1 460
                WHILE U.isHex(cap(Lines.getChar(line, i))) DO
8728 leency 461
                    INC(i)
462
                END;
463
 
9659 akron1 464
                IF (cap(Lines.getChar(line, i)) = "H") & ~hex THEN
8728 leency 465
                    INC(i)
466
                END;
467
 
468
                DEC(i);
469
                PrintLex(text, line, k, i, y, colors.num, backColor)
470
            END
471
 
9174 akron1 472
        ELSIF (lang = Lang.langC) OR (lang = Lang.langJSON) THEN
8728 leency 473
 
9210 akron1 474
	        IF depth = 0 THEN
475
	            IF c = "/" THEN
476
	                IF cond = 0 THEN
477
	                    cond := 1
478
	                ELSE
479
	                    PrintLex(text, line, i - 1, n, y, colors.comment, backColor);
480
	                    cond := 0;
481
	                    i := n
482
	                END
483
	            ELSIF (c = "*") & (cond = 1) THEN
484
	                depth := 1;
485
	                INC(i);
486
	                PrintComment(text, line, depth, i, 2, y, backColor);
487
	                cond := 0
488
	            ELSIF U.isLetter(c) OR (c = "_") OR (c = "'") OR (c = '"') THEN
489
	            	k := i;
490
	            	IF (c = "'") OR (c = '"') THEN
491
	            		String(text, line, i, y, backColor);
492
	            	ELSE
9659 akron1 493
	            		ident(text, i, i - ORD((lang = Lang.langC) & (i > 0) & (Lines.getChar(line, i - 1) = "#")), y, line, backColor, Lang.isCS(lang))
9210 akron1 494
	            	END;
495
	                IF lang = Lang.langJSON THEN
9659 akron1 496
		                WHILE Lines.isSpace(Lines.getChar(line, i + 1)) DO
9210 akron1 497
		                	INC(i)
498
		                END;
9659 akron1 499
		                IF Lines.getChar(line, i + 1) = ":" THEN
9210 akron1 500
		                	PrintLex(text, line, k, i, y, colors.key1, backColor)
501
		                END
9174 akron1 502
	                END;
9210 akron1 503
	                cond := 0
504
	            ELSIF U.isDigit(c) THEN
505
	                k := i;
506
	                INC(i);
9659 akron1 507
	                c := Lines.getChar(line, i);
9210 akron1 508
	                IF c = "." THEN
509
	                    DEC(i);
9659 akron1 510
	                    c := Lines.getChar(line, i)
9210 akron1 511
	                END;
9659 akron1 512
	                IF (cap(c) = "X") & (Lines.getChar(line, i - 1) = "0") THEN
9210 akron1 513
	                    REPEAT
514
	                        INC(i);
9659 akron1 515
	                        c := Lines.getChar(line, i)
9210 akron1 516
	                    UNTIL ~U.isHex(cap(c));
517
	                    IF UL(c) THEN
518
	                        INC(i)
519
	                    END
520
	                ELSIF UL(c) THEN
521
	                    INC(i)
522
	                ELSIF U.isDigit(c) THEN
523
	                    REPEAT
524
	                        INC(i)
9659 akron1 525
	                    UNTIL ~U.isDigit(Lines.getChar(line, i));
526
	                    c := Lines.getChar(line, i);
9210 akron1 527
	                    IF UL(c) THEN
528
	                        INC(i)
529
	                    ELSIF c = "." THEN
530
	                        INC(i);
9659 akron1 531
	                        WHILE U.isDigit(Lines.getChar(line, i)) DO
9210 akron1 532
	                            INC(i)
533
	                        END;
9659 akron1 534
	                        c := Lines.getChar(line, i);
9210 akron1 535
	                        IF cap(c) = "E" THEN
536
	                            INC(i);
9659 akron1 537
	                            c := Lines.getChar(line, i);
9210 akron1 538
	                            IF (c = "+") OR (c = "-") THEN
539
	                                INC(i)
540
	                            END;
9659 akron1 541
	                            IF U.isDigit(Lines.getChar(line, i)) THEN
542
	                                WHILE U.isDigit(Lines.getChar(line, i)) DO
9210 akron1 543
	                                    INC(i)
544
	                                END;
9659 akron1 545
	                                c := Lines.getChar(line, i);
9210 akron1 546
	                                IF FL(c) THEN
547
	                                    INC(i)
548
	                                END
549
	                            END
550
	                        ELSIF FL(c) THEN
551
	                            INC(i)
552
	                        END
553
	                    END
554
	                END;
555
	                DEC(i);
556
	                PrintLex(text, line, k, i, y, colors.num, backColor);
557
	                cond := 0
558
	            ELSE
559
	                cond := 0
560
	            END
561
	        ELSIF depth = 1 THEN
562
	            IF c = "*" THEN
563
	                cond := 1
564
	            ELSIF (c = "/") & (cond = 1) THEN
565
	                cond := 0;
566
	                depth := 0
567
	            ELSE
568
	                cond := 0
569
	            END
570
	        END
8728 leency 571
 
572
        ELSIF lang = Lang.langOberon THEN
573
 
9210 akron1 574
	        IF (depth = 0) & (c = "/") THEN
575
	            IF cond = 3 THEN
576
	                PrintLex(text, line, i - 1, n, y, colors.comment, backColor);
577
	                cond := 0;
578
	                i := n
579
	            ELSE
580
	                cond := 3
581
	            END
582
	        ELSIF (depth = 0) & ((c = "'") OR (c = '"')) THEN
583
	            String(text, line, i, y, backColor);
584
	            cond := 0
585
	        ELSIF (depth = 0) & U.isDigit(c) THEN
586
	            color := colors.num;
587
	            k := i;
588
	            INC(i);
9659 akron1 589
	            WHILE U.isHex(Lines.getChar(line, i)) DO
9210 akron1 590
	                INC(i)
591
	            END;
592
	            IF i <= n THEN
9659 akron1 593
	                IF Lines.getChar(line, i) = "." THEN
9210 akron1 594
	                    INC(i);
9659 akron1 595
	                    IF Lines.getChar(line, i) = "." THEN
9210 akron1 596
	                        DEC(i)
597
	                    END;
9659 akron1 598
	                    WHILE U.isDigit(Lines.getChar(line, i)) DO
9210 akron1 599
	                        INC(i)
600
	                    END;
9659 akron1 601
	                    IF Lines.getChar(line, i) = "E" THEN
9210 akron1 602
	                        INC(i);
9659 akron1 603
	                        IF (Lines.getChar(line, i) = "+") OR (Lines.getChar(line, i) = "-") THEN
9210 akron1 604
	                            INC(i)
605
	                        END;
9659 akron1 606
	                        WHILE U.isDigit(Lines.getChar(line, i)) DO
9210 akron1 607
	                            INC(i)
608
	                        END
609
	                    END
9659 akron1 610
	                ELSIF Lines.getChar(line, i) = "H" THEN
9210 akron1 611
	                    INC(i)
9659 akron1 612
	                ELSIF Lines.getChar(line, i) = "X" THEN
9210 akron1 613
	                    color := colors.string;
614
	                    INC(i)
615
	                END
616
	            END;
617
	            DEC(i);
618
	            PrintLex(text, line, k, i, y, color, backColor);
619
	            cond := 0
620
	        ELSIF (depth = 0) & (U.isLetter(c) OR (c = "_")) THEN
621
	            ident(text, i, i, y, line, backColor, Lang.isCS(lang));
622
	            cond := 0
623
	        ELSIF c = "(" THEN
624
	            cond := 1
625
	        ELSIF c = "*" THEN
626
	            IF cond = 1 THEN
627
	                INC(depth);
628
	                INC(i);
629
	                PrintComment(text, line, depth, i, 2, y, backColor);
630
	                cond := 0
631
	            ELSE
632
	                cond := 2
633
	            END
634
	        ELSIF c = ")" THEN
635
	            IF cond = 2 THEN
636
	                IF depth > 0 THEN
637
	                    DEC(depth)
638
	                END
639
	            END;
640
	            cond := 0
641
	        ELSE
642
	            cond := 0
643
	        END
8728 leency 644
 
645
        ELSIF lang = Lang.langLua THEN
646
 
9210 akron1 647
	        IF depth = 0 THEN
648
	            IF c = "-" THEN
649
	                IF cond = 1 THEN
650
	                    IF Lang.LuaLong(line, i + 1) >= 0 THEN
651
	                        depth := Lang.LuaLong(line, i + 1)*2 + 1;
652
	                        INC(i);
653
	                        PrintComment(text, line, depth, i, 2, y, backColor)
654
	                    ELSE
655
	                        PrintLex(text, line, i - 1, n, y, colors.comment, backColor);
656
	                        i := n
657
	                    END;
658
	                    cond := 0
659
	                ELSE
660
	                    cond := 1
661
	                END
662
	            ELSIF c = "[" THEN
663
	                cond := 0;
664
	                k := Lang.LuaLong(line, i);
665
	                IF k >= 0 THEN
666
	                    depth := (k + 1)*2;
667
	                    INC(i, 2);
668
	                    PrintComment(text, line, depth, i, 2, y, backColor);
669
	                    cond := 0
670
	                END
671
	            ELSIF (c = "'") OR (c = '"') THEN
672
	                String(text, line, i, y, backColor);
673
	                cond := 0
674
	            ELSIF U.isDigit(c) THEN
675
	                k := i;
9659 akron1 676
	                IF (c = "0") & (cap(Lines.getChar(line, i + 1)) = "X") THEN
9210 akron1 677
	                    isDgt := U.isHex;
678
	                    hex := TRUE;
679
	                    INC(i, 2)
680
	                ELSE
681
	                    isDgt := U.isDigit;
682
	                    hex := FALSE
683
	                END;
9659 akron1 684
	                WHILE isDgt(cap(Lines.getChar(line, i))) DO
9210 akron1 685
	                    INC(i)
686
	                END;
9659 akron1 687
	                IF Lines.getChar(line, i) = "." THEN
9210 akron1 688
	                    INC(i);
9659 akron1 689
	                    IF Lines.getChar(line, i) = "." THEN
9210 akron1 690
	                        DEC(i)
691
	                    END;
9659 akron1 692
	                    WHILE isDgt(cap(Lines.getChar(line, i))) DO
9210 akron1 693
	                        INC(i)
694
	                    END
695
	                END;
9659 akron1 696
	                IF (cap(Lines.getChar(line, i)) = "E") OR hex & (cap(Lines.getChar(line, i)) = "P") THEN
9210 akron1 697
	                    INC(i);
9659 akron1 698
	                    IF (Lines.getChar(line, i) = "-") OR (Lines.getChar(line, i) = "+") THEN
9210 akron1 699
	                        INC(i)
700
	                    END;
9659 akron1 701
	                    WHILE isDgt(cap(Lines.getChar(line, i))) DO
9210 akron1 702
	                        INC(i)
703
	                    END
704
	                END;
705
	                DEC(i);
706
	                PrintLex(text, line, k, i, y, colors.num, backColor);
707
	                cond := 0
708
	            ELSIF U.isLetter(c) OR (c = "_") THEN
709
	                ident(text, i, i, y, line, backColor, Lang.isCS(lang));
710
	                cond := 0
711
	            ELSE
712
	                cond := 0
713
	            END
8728 leency 714
 
9210 akron1 715
	        ELSIF depth > 0 THEN
716
	            IF (cond = 0) & (c = "]") THEN
717
	                cond := 1
718
	            ELSIF (cond >= 1) & (c = "=") THEN
719
	                INC(cond)
720
	            ELSIF (cond >= 1) & (c = "]") & (cond * 2 - depth MOD 2 = depth) THEN
721
	                depth := 0;
722
	                cond := 0
723
	            ELSE
724
	                cond := 0
725
	            END
726
	        END
8728 leency 727
 
728
        ELSIF lang = Lang.langPascal THEN
729
 
9210 akron1 730
	        IF depth = 0 THEN
731
	            IF c = "(" THEN
732
	                cond := 1
733
	            ELSIF (c = "*") & (cond = 1) THEN
734
	                depth := 2;
735
	                INC(i);
736
	                PrintComment(text, line, depth, i, 2, y, backColor);
737
	                cond := 0
738
	            ELSIF c = "/" THEN
739
	                IF cond = 2 THEN
740
	                    PrintLex(text, line, i - 1, n, y, colors.comment, backColor);
741
	                    cond := 0;
742
	                    i := n
743
	                ELSE
744
	                    cond := 2
745
	                END
746
	            ELSIF c = "'" THEN
747
	                String(text, line, i, y, backColor);
748
	                cond := 0
749
	            ELSIF c = "{" THEN
9659 akron1 750
	                IF Lines.getChar(line, i + 1) = "$" THEN
9210 akron1 751
	                    depth := 3
752
	                ELSE
753
	                    depth := 1
754
	                END;
755
	                INC(i);
756
	                PrintComment(text, line, depth, i, 1, y, backColor);
757
	                cond := 0
758
	            ELSIF c = "#" THEN
759
	                k := i;
760
	                INC(i);
9659 akron1 761
	                WHILE U.isDigit(Lines.getChar(line, i)) DO
9210 akron1 762
	                    INC(i)
763
	                END;
764
	                DEC(i);
765
	                PrintLex(text, line, k, i, y, colors.string, backColor);
766
	                cond := 0
767
	            ELSIF c = "$" THEN
9659 akron1 768
	                IF (i > 0 ) & (Lines.getChar(line, i - 1) = "#") THEN
9210 akron1 769
	                    color := colors.string
770
	                ELSE
771
	                    color := colors.num
772
	                END;
773
	                k := i;
774
	                INC(i);
9659 akron1 775
	                WHILE U.isHex(cap(Lines.getChar(line, i))) DO
9210 akron1 776
	                    INC(i)
777
	                END;
778
	                DEC(i);
779
	                PrintLex(text, line, k, i, y, color, backColor);
780
	                cond := 0
781
	            ELSIF U.isDigit(c) THEN
782
	                k := i;
9659 akron1 783
	                WHILE U.isDigit(Lines.getChar(line, i)) DO
9210 akron1 784
	                    INC(i)
785
	                END;
9659 akron1 786
	                IF Lines.getChar(line, i) = "." THEN
9210 akron1 787
	                    INC(i);
9659 akron1 788
	                    IF Lines.getChar(line, i) = "." THEN
9210 akron1 789
	                        DEC(i)
790
	                    END;
9659 akron1 791
	                    WHILE U.isDigit(Lines.getChar(line, i)) DO
9210 akron1 792
	                        INC(i)
793
	                    END;
9659 akron1 794
	                    IF cap(Lines.getChar(line, i)) = "E" THEN
9210 akron1 795
	                        INC(i);
9659 akron1 796
	                        IF (Lines.getChar(line, i) = "-") OR (Lines.getChar(line, i) = "+") THEN
9210 akron1 797
	                            INC(i)
798
	                        END;
9659 akron1 799
	                        WHILE U.isDigit(Lines.getChar(line, i)) DO
9210 akron1 800
	                            INC(i)
801
	                        END
802
	                    END
803
	                END;
804
	                DEC(i);
805
	                PrintLex(text, line, k, i, y, colors.num, backColor);
806
	                cond := 0
807
	            ELSIF (U.isLetter(c) OR (c = "_")) THEN
808
	                ident(text, i, i, y, line, backColor, Lang.isCS(lang));
809
	                cond := 0
810
	            ELSE
811
	                cond := 0
812
	            END
813
	        ELSIF depth IN {1, 3} THEN
814
	            IF c = "}" THEN
815
	                depth := 0
816
	            END
817
	        ELSIF depth = 2 THEN
818
	            IF c = "*" THEN
819
	                cond := 1
820
	            ELSIF (c = ")") & (cond = 1) THEN
821
	                depth := 0;
822
	                cond := 0
823
	            ELSE
824
	                cond := 0
825
	            END
826
	        END
8728 leency 827
 
828
        ELSIF lang = Lang.langIni THEN
829
 
9210 akron1 830
	        IF depth = 0 THEN
9674 akron1 831
	            IF ((c = ";") OR (c = "#")) & (leadingSpaces(line) = i) THEN
9210 akron1 832
	                PrintLex(text, line, i, n, y, colors.comment, backColor);
833
	                i := n
834
	            ELSIF c = '"' THEN
835
	                String(text, line, i, y, backColor)
836
	            ELSIF c = "[" THEN
837
	                depth := 1;
838
	                INC(i);
839
	                PrintComment(text, line, depth, i, 1, y, backColor)
840
	            ELSIF U.isDigit(c) THEN
841
	                k := i;
9659 akron1 842
	                WHILE U.isDigit(Lines.getChar(line, i)) DO
9210 akron1 843
	                    INC(i)
844
	                END;
845
	                DEC(i);
846
	                PrintLex(text, line, k, i, y, colors.num, backColor)
847
	            ELSIF (U.isLetter(c) OR (c = "_")) THEN
848
	                ident(text, i, i, y, line, backColor, Lang.isCS(lang))
849
	            END
850
	        ELSIF depth = 1 THEN
851
	            IF c = "]" THEN
852
	                depth := 0
853
	            END
854
	        END
8728 leency 855
 
856
        END;
857
        INC(i)
858
    END
859
END parse;
860
 
861
 
9599 akron1 862
PROCEDURE plain (text: tText): CB.tBuffer;
8728 leency 863
VAR
864
    buf: CB.tBuffer;
865
    size: INTEGER;
866
    line: tLine;
867
BEGIN
868
    size := 0;
869
    line := text.first(tLine);
870
    WHILE line # NIL DO
871
        line.pos := size;
872
        INC(size, line.length);
873
        NextLine(line);
874
        IF line # NIL THEN
875
            INC(size, CB.lenEOL)
876
        END
877
    END;
9599 akron1 878
    buf := CB.create(size + 2);
8728 leency 879
    line := text.first(tLine);
880
    WHILE line # NIL DO
881
        CB.append(buf, line, 0, line.length - 1);
882
        NextLine(line);
883
        IF line # NIL THEN
884
            CB.eol(buf)
885
        END
886
    END;
9599 akron1 887
    CB.appends(buf, 0X, 0, 0);
888
    CB.appends(buf, 0X, 0, 0)
8728 leency 889
    RETURN buf
890
END plain;
891
 
892
 
893
PROCEDURE search* (text: tText; s: ARRAY OF WCHAR; cs, whole: BOOLEAN): BOOLEAN;
894
VAR
9599 akron1 895
	pos: List.tItem;
896
	res: BOOLEAN;
897
	plainText, idxData: Search.tBuffer;
8728 leency 898
BEGIN
9599 akron1 899
	res := TRUE;
900
	plainText := NIL;
901
	idxData := NIL;
902
	WHILE text.foundList.count # 0 DO
903
		pos := List.pop(text.foundList);
904
		DISPOSE(pos)
905
	END;
906
	text.whole := whole;
907
	text.cs := cs;
908
	text.searchText := s;
909
	IF ~cs THEN
910
		U.upcase16(text.searchText)
911
	END;
912
	IF text.searchText # "" THEN
913
		plainText := plain(text);
9674 akron1 914
		idxData := Search.index(plainText, idxTable, cs);
915
		Search.find(plainText, idxTable, text.searchText, whole, text.foundList);
9599 akron1 916
		res := text.foundList.count > 0
917
	END;
918
	CB.destroy(plainText);
919
	CB.destroy(idxData);
920
	text.search := FALSE;
921
	text.foundSel := 0
922
	RETURN res
8728 leency 923
END search;
924
 
925
 
926
PROCEDURE modify (text: tText);
927
BEGIN
928
    text.modified := TRUE;
929
    text.comments := TRUE;
930
    text.search := TRUE;
931
    text.guard := TRUE
932
END modify;
933
 
934
 
9336 akron1 935
PROCEDURE setEnc* (text: tText; enc: INTEGER);
936
BEGIN
937
	IF text.enc # enc THEN
938
		ChangeLog.changeInt(text.enc, enc);
939
		text.enc := enc;
940
		modify(text)
941
	END
942
END setEnc;
943
 
944
 
945
PROCEDURE setEol* (text: tText; eol: INTEGER);
946
BEGIN
947
	IF text.eol # eol THEN
948
		ChangeLog.changeInt(text.eol, eol);
949
		text.eol := eol;
950
		modify(text)
951
	END
952
END setEol;
953
 
954
 
955
PROCEDURE getEnc* (text: tText): INTEGER;
956
	RETURN text.enc
957
END getEnc;
958
 
959
 
960
PROCEDURE getEol* (text: tText): INTEGER;
961
	RETURN text.eol
962
END getEol;
963
 
964
 
8728 leency 965
PROCEDURE DelLine (text: tText; line: tLine);
966
BEGIN
967
    List._delete(text, line);
968
    Lines.destroy(line);
969
    modify(text)
970
END DelLine;
971
 
972
 
973
PROCEDURE setSelect (text: tText);
974
BEGIN
975
    IF text.select = text.cursor THEN
976
        text.select2^ := text.cursor^;
977
        text.select := text.select2
978
    END
979
END setSelect;
980
 
981
 
982
PROCEDURE resetSelect* (text: tText);
983
BEGIN
984
    text.select := text.cursor
985
END resetSelect;
986
 
987
 
988
PROCEDURE getLine (text: tText; n: INTEGER): tLine;
989
VAR
9668 akron1 990
	item: List.tItem;
8728 leency 991
BEGIN
9668 akron1 992
	item := List.getItem(text, n)
993
	RETURN item(tLine)
8728 leency 994
END getLine;
995
 
996
 
997
PROCEDURE SetPos* (text: tText; x, y: INTEGER);
998
VAR
9174 akron1 999
    deltaY, n, L, R: INTEGER;
8728 leency 1000
    cursor: pPoint;
9174 akron1 1001
    c: WCHAR;
8728 leency 1002
   (* trimLength: INTEGER; *)
1003
BEGIN
1004
    cursor := text.cursor;
1005
    y := MIN(MAX(y, 0), text.count - 1);
1006
    deltaY := y - cursor.Y;
1007
    IF deltaY # 0 THEN
1008
        cursor.Y := y;
1009
(*        trimLength := Lines.trimLength(text.curLine);
1010
        IF text.curLine.length # trimLength THEN
1011
            Lines.setChar(text.curLine, trimLength, 0X);
1012
            text.curLine.length := trimLength
1013
        END;*)
1014
        IF deltaY = 1 THEN
1015
            NextLine(text.curLine)
1016
        ELSIF deltaY = -1 THEN
1017
            PrevLine(text.curLine)
1018
        ELSE
1019
            text.curLine := getLine(text, y)
1020
        END
1021
    END;
1022
    cursor.X := MIN(MAX(x, 0), text.curLine.length);
9659 akron1 1023
    c := Lines.getChar(text.curLine, cursor.X);
9174 akron1 1024
    IF c = TAB1 THEN
1025
        n := cursor.X;
9659 akron1 1026
        WHILE Lines.getChar(text.curLine, n) = TAB1 DO
9174 akron1 1027
            INC(n)
1028
        END;
1029
        R := n - cursor.X;
1030
        n := cursor.X;
9659 akron1 1031
        WHILE Lines.getChar(text.curLine, n) # TAB DO
9174 akron1 1032
            DEC(n)
1033
        END;
1034
        L := cursor.X - n;
1035
        IF L < R THEN
1036
            DEC(cursor.X, L)
1037
        ELSE
1038
            INC(cursor.X, R)
1039
        END
1040
    END;
8728 leency 1041
    IF text.scroll.Y > cursor.Y THEN
1042
        text.scroll.Y := cursor.Y
1043
    ELSIF text.scroll.Y + textsize.Y <= cursor.Y THEN
1044
        text.scroll.Y := cursor.Y - textsize.Y + 1
1045
    END;
1046
    IF text.scroll.X > cursor.X THEN
1047
        text.scroll.X := cursor.X
1048
    ELSIF text.scroll.X + textsize.X <= cursor.X THEN
1049
        text.scroll.X := cursor.X - textsize.X + 1
1050
    END;
1051
    IF (text.select.Y = cursor.Y) & (text.select.X > text.curLine.length) THEN
1052
        text.select.X := text.curLine.length
1053
    END;
1054
    setSelect(text);
1055
    text.foundSel := 0;
1056
    ShowCursor;
1057
    text.CurX := -1
1058
END SetPos;
1059
 
1060
 
1061
PROCEDURE getSelect (text: tText; VAR selBeg, selEnd: tPoint);
1062
BEGIN
1063
    selBeg := text.cursor^;
1064
    selEnd := text.select^;
1065
    IF (selBeg.Y > selEnd.Y) OR (selBeg.Y = selEnd.Y) & (selBeg.X > selEnd.X) THEN
1066
        selBeg := text.select^;
1067
        selEnd := text.cursor^
1068
    END
1069
END getSelect;
1070
 
1071
 
1072
PROCEDURE selected* (text: tText): BOOLEAN;
1073
    RETURN (text.cursor.X # text.select.X) OR (text.cursor.Y # text.select.Y)
1074
END selected;
1075
 
1076
 
1077
PROCEDURE delSelect (text: tText);
1078
VAR
1079
    selBeg, selEnd: tPoint;
1080
    line, last, cur: tLine;
1081
BEGIN
1082
    getSelect(text, selBeg, selEnd);
1083
    IF (selBeg.Y = selEnd.Y) & (selBeg.X < selEnd.X) THEN
1084
        line := text.curLine;
1085
        Lines.delCharN(line, selBeg.X, selEnd.X - selBeg.X);
1086
        Lines.modify(line);
1087
        text.cursor^ := selBeg;
1088
        resetSelect(text);
1089
        SetPos(text, text.cursor.X, text.cursor.Y);
1090
        modify(text)
1091
    ELSIF selBeg.Y < selEnd.Y THEN
1092
        SetPos(text, selBeg.X, selBeg.Y);
1093
        line := text.curLine;
1094
        Lines.delCharN(line, selBeg.X, line.length - selBeg.X);
1095
        last := getLine(text, selEnd.Y);
1096
        Lines.delCharN(last, 0, selEnd.X);
1097
        cur := line.next(tLine);
1098
        WHILE cur # last DO
1099
            DelLine(text, cur);
1100
            cur := line.next(tLine)
1101
        END;
1102
        resetSelect(text);
1103
        SetPos(text, text.cursor.X, text.cursor.Y);
1104
        pdelete(text);
1105
        modify(text)
1106
    END;
1107
    resetSelect(text)
1108
END delSelect;
1109
 
1110
 
1111
PROCEDURE delete (text: tText);
1112
VAR
9174 akron1 1113
    i, n: INTEGER;
8728 leency 1114
    nextLine, curLine: tLine;
1115
BEGIN
1116
    IF selected(text) THEN
1117
        delSelect(text)
1118
    ELSE
1119
        i := text.cursor.X;
1120
        curLine := text.curLine;
1121
        IF i < curLine.length THEN
9174 akron1 1122
            n := i;
1123
            INC(i);
9659 akron1 1124
            IF Lines.getChar(curLine, i - 1) = TAB THEN
1125
                WHILE Lines.getChar(curLine, i) = TAB1 DO
9174 akron1 1126
                    INC(i)
1127
                END
1128
            END;
1129
            Lines.delCharN(curLine, n, i - n);
8728 leency 1130
            Lines.modify(curLine);
1131
            modify(text)
1132
        ELSE
1133
            nextLine := curLine.next(tLine);
1134
            IF nextLine # NIL THEN
9174 akron1 1135
                Lines.insert2(curLine, i, nextLine);
1136
                DelLine(text, nextLine);
8728 leency 1137
                Lines.modify(curLine);
9174 akron1 1138
                modify(text)
8728 leency 1139
            END
1140
        END
1141
    END;
1142
    setSelect(text)
1143
END delete;
1144
 
1145
 
9174 akron1 1146
PROCEDURE move (text: tText; d: INTEGER);
1147
VAR
1148
    pos: INTEGER;
1149
BEGIN
1150
    pos := text.cursor.X + d;
9659 akron1 1151
    WHILE Lines.getChar(text.curLine, pos) = TAB1 DO
9174 akron1 1152
        INC(pos, d)
1153
    END;
1154
    SetPos(text, pos, text.cursor.Y)
1155
END move;
1156
 
1157
 
8728 leency 1158
PROCEDURE BkSpace (text: tText);
1159
VAR
9174 akron1 1160
    i, k, n: INTEGER;
1161
    curLine, line, line2: tLine;
8728 leency 1162
BEGIN
1163
    IF selected(text) THEN
1164
        delSelect(text)
1165
    ELSE
1166
        resetSelect(text);
1167
        curLine := text.curLine;
9174 akron1 1168
        IF text.cursor.X > 0 THEN
9668 akron1 1169
        	INC(text.smallChange);
9174 akron1 1170
            i := text.cursor.X;
9671 akron1 1171
            IF autoIndents THEN
1172
	            n := leadingSpaces(curLine)
1173
            ELSE
1174
    	        n := 0
1175
            END;
9175 akron1 1176
            modify(text);
8728 leency 1177
            IF n < i THEN
9174 akron1 1178
                move(text, -1);
1179
                delete(text)
8728 leency 1180
            ELSE
1181
                n := i;
1182
                line := curLine.prev(tLine);
9174 akron1 1183
                line2 := line;
8728 leency 1184
                k := n;
1185
                WHILE (line # NIL) & (k >= n) DO
1186
                    IF Lines.trimLength(line) # 0 THEN
9174 akron1 1187
                        k := leadingSpaces(line);
1188
                        line2 := line;
8728 leency 1189
                    END;
1190
                    PrevLine(line)
1191
                END;
1192
                IF k >= n THEN
1193
                    k := 0
1194
                END;
9174 akron1 1195
                n := k;
1196
                Lines.delCharN(curLine, 0, i);
1197
                Lines.insert3(curLine, 0, k);
1198
                WHILE k > 0 DO
9659 akron1 1199
                    Lines.setChar(curLine, k - 1, Lines.getChar(line2, k - 1));
9174 akron1 1200
                    DEC(k)
1201
                END;
8728 leency 1202
                Lines.modify(curLine);
9174 akron1 1203
                SetPos(text, n, text.cursor.Y)
1204
            END
8728 leency 1205
        ELSE
1206
            PrevLine(curLine);
1207
            IF curLine # NIL THEN
1208
                SetPos(text, curLine.length, text.cursor.Y - 1);
1209
                delete(text)
1210
            END
1211
        END
1212
    END;
1213
    setSelect(text)
1214
END BkSpace;
1215
 
1216
 
1217
PROCEDURE enter (text: tText);
1218
VAR
1219
    n: INTEGER;
9174 akron1 1220
    curLine, newLine, line, line2: tLine;
8728 leency 1221
BEGIN
1222
    delSelect(text);
1223
    newLine := Lines.create(FALSE);
1224
    modify(text);
1225
    curLine := text.curLine;
1226
    IF text.cursor.X < curLine.length THEN
9174 akron1 1227
        Lines.wrap(curLine, newLine, text.cursor.X);
1228
        Lines.modify(curLine)
8728 leency 1229
    END;
1230
    List._insert(text, curLine, newLine);
1231
    SetPos(text, 0, text.cursor.Y + 1);
1232
    line := text.curLine.prev(tLine);
1233
    n := -1;
9671 akron1 1234
    WHILE (line # NIL) & (n = -1) & autoIndents DO
8728 leency 1235
        IF (*line.length*)Lines.trimLength(line) # 0 THEN
9174 akron1 1236
            n := leadingSpaces(line);
1237
            line2 := line
8728 leency 1238
        END;
1239
        PrevLine(line)
1240
    END;
1241
    IF n = -1 THEN
1242
        n := 0
1243
    END;
1244
    Lines.insert3(text.curLine, 0, n);
1245
    SetPos(text, n, text.cursor.Y);
1246
    resetSelect(text);
1247
    WHILE n > 0 DO
9659 akron1 1248
        Lines.setChar(text.curLine, n - 1, Lines.getChar(line2, n - 1));
8728 leency 1249
        DEC(n)
9174 akron1 1250
    END;
1251
    Lines.modify(newLine)
8728 leency 1252
END enter;
1253
 
1254
 
9174 akron1 1255
PROCEDURE incIndent (line: tLine);
1256
VAR
1257
	c: WCHAR;
1258
	i: INTEGER;
1259
BEGIN
1260
	Lines.modify(line);
1261
	Lines.insert3(line, 0, Lines.tab);
1262
	IF Lines.tabs THEN
1263
		c := TAB1
1264
	ELSE
1265
		c := SPACE
1266
	END;
1267
	i := Lines.tab - 1;
1268
	WHILE i >= 0 DO
1269
		Lines.setChar(line, i, c);
1270
		DEC(i)
1271
	END;
1272
	IF Lines.tabs THEN
1273
		Lines.setChar(line, 0, TAB)
1274
	END
1275
END incIndent;
1276
 
1277
 
1278
PROCEDURE decIndent (line: tLine): BOOLEAN;
1279
VAR
1280
	n: INTEGER;
1281
BEGIN
1282
	n := leadingSpaces(line);
1283
	IF n > 0 THEN
1284
		Lines.delCharN(line, 0, MIN(Lines.tab, n));
1285
		Lines.modify(line)
1286
	END
1287
	RETURN n > 0
1288
END decIndent;
1289
 
1290
 
1291
PROCEDURE Indent* (text: tText; incr: BOOLEAN);
1292
VAR
1293
    i: INTEGER;
1294
    line: tLine;
1295
    selBeg, selEnd: tPoint;
1296
    modified: BOOLEAN;
1297
BEGIN
1298
	getSelect(text, selBeg, selEnd);
1299
	i := selEnd.Y - selBeg.Y + 1;
9668 akron1 1300
	line := getLine2(text, selBeg.Y);
9174 akron1 1301
	modified := incr;
1302
	WHILE i > 0 DO
1303
		IF incr THEN
1304
			incIndent(line)
1305
		ELSE
1306
    		modified := decIndent(line) OR modified
1307
		END;
1308
		NextLine(line);
1309
		DEC(i)
1310
	END;
9668 akron1 1311
	line := getLine2(text, selEnd.Y);
9174 akron1 1312
	text.select^ := selBeg;
1313
	text.select.X := 0;
1314
	SetPos(text, line.length, selEnd.Y);
1315
	IF modified THEN
9668 akron1 1316
		INC(text.smallChange);
9174 akron1 1317
   		modify(text)
1318
	END
1319
END Indent;
1320
 
1321
 
8728 leency 1322
PROCEDURE input* (text: tText; code: INTEGER);
1323
VAR
1324
    curLine: tLine;
1325
 
9174 akron1 1326
 
8728 leency 1327
    PROCEDURE tab (text: tText);
1328
    VAR
1329
        i, x: INTEGER;
1330
        curLine: tLine;
9174 akron1 1331
        c: WCHAR;
8728 leency 1332
    BEGIN
9174 akron1 1333
		delSelect(text);
1334
		curLine := text.curLine;
1335
		x := text.cursor.X;
1336
		i := Lines.tab - x MOD Lines.tab;
1337
		Lines.insert3(curLine, x, i);
1338
		SetPos(text, x + i, text.cursor.Y);
1339
		IF Lines.tabs THEN
1340
			c := TAB1
1341
		ELSE
1342
			c := SPACE
1343
		END;
1344
		WHILE i > 0 DO
1345
			Lines.setChar(curLine, x + i - 1, c);
1346
			DEC(i)
1347
		END;
1348
		IF Lines.tabs THEN
1349
			Lines.setChar(curLine, x + i, TAB)
1350
		END;
1351
		Lines.modify(curLine);
1352
		modify(text)
8728 leency 1353
    END tab;
1354
 
9174 akron1 1355
 
8728 leency 1356
BEGIN
1357
    IF (code >= ORD(SPACE)) & (code # 127) THEN
9668 akron1 1358
    	IF ~selected(text) THEN
1359
    		INC(text.smallChange)
1360
    	END;
8728 leency 1361
        delSelect(text);
1362
        curLine := text.curLine;
1363
        Lines.insert(curLine, text.cursor.X, WCHR(code));
9671 akron1 1364
        IF autoBrackets THEN
1365
        	IF code = ORD("(") THEN
1366
        		code := ORD(")")
1367
        	ELSIF code = ORD("[") THEN
1368
        		code := ORD("]")
1369
        	ELSIF code = ORD("{") THEN
1370
        		code := ORD("}")
1371
        	ELSE
1372
        		code := -1
1373
        	END;
1374
        	IF code # -1 THEN
1375
        		Lines.insert(curLine, text.cursor.X + 1, WCHR(code))
1376
        	END
1377
        END;
8728 leency 1378
        Lines.modify(curLine);
1379
        modify(text);
1380
        SetPos(text, text.cursor.X + 1, text.cursor.Y)
1381
    ELSIF code = 8 THEN
9174 akron1 1382
		BkSpace(text)
1383
    ELSIF code = -8 THEN
1384
    	IF selected(text) THEN
1385
    		Indent(text, FALSE)
1386
        END
8728 leency 1387
    ELSIF code = 9 THEN
9174 akron1 1388
    	IF selected(text) THEN
1389
    		Indent(text, TRUE)
1390
    	ELSE
9668 akron1 1391
    		INC(text.smallChange);
9174 akron1 1392
	        tab(text)
1393
        END
8728 leency 1394
    ELSIF code = 13 THEN
1395
        enter(text)
1396
    END
1397
END input;
1398
 
1399
 
1400
PROCEDURE scroll* (text: tText; h, v: INTEGER);
1401
BEGIN
1402
    INC(text.scroll.X, h);
1403
    INC(text.scroll.Y, v);
9050 leency 1404
    text.scroll.X := MIN(MAX(text.scroll.X, 0), text.maxLength);
8728 leency 1405
    text.scroll.Y := MIN(MAX(text.scroll.Y, 0), text.count - 1)
1406
END scroll;
1407
 
1408
 
9180 akron1 1409
PROCEDURE save* (text: tText; name: RW.tFileName): BOOLEAN;
9073 leency 1410
CONST
1411
    tempFile = "/tmp0/1/cedit~.tmp";
8728 leency 1412
VAR
1413
    line: tLine;
1414
    file: RW.tOutput;
1415
    res: BOOLEAN;
9671 akron1 1416
    Len: INTEGER;
8728 leency 1417
BEGIN
9073 leency 1418
    ChangeLog.setGuard(text.edition);
9180 akron1 1419
    file := RW.create(tempFile, text.enc, text.eol);
8728 leency 1420
    IF file # NIL THEN
9073 leency 1421
        ChangeLog.delSaved;
8728 leency 1422
        line := text.first(tLine);
9522 akron1 1423
        WHILE line # NIL DO
9671 akron1 1424
        	IF trimSpace THEN
1425
        		Len := Lines.trimLength(line)
1426
        	ELSE
1427
        		Len := line.length
1428
        	END;
1429
            RW.putString(file, line, Len);
8728 leency 1430
            NextLine(line);
1431
            IF line # NIL THEN
9522 akron1 1432
                RW.newLine(file)
8728 leency 1433
            END
1434
        END;
9522 akron1 1435
        res := RW.close(file)
8728 leency 1436
    ELSE
1437
        res := FALSE
1438
    END;
9073 leency 1439
    IF res THEN
1440
        res := File.Copy(tempFile, name);
1441
        IF res THEN
1442
            text.modified := FALSE;
1443
            ChangeLog.save(text.edition);
9452 akron1 1444
 
1445
	        line := text.first(tLine);
1446
	        WHILE line # NIL DO
1447
	            IF line.modified THEN
1448
	                Lines.save(line)
1449
	            END;
1450
	            NextLine(line)
9522 akron1 1451
	        END
9073 leency 1452
        END
1453
    END;
9522 akron1 1454
    IF File.Delete(tempFile) THEN END;
9073 leency 1455
    IF ~res THEN
1456
        ChangeLog.delCurSaved
1457
    END
8728 leency 1458
    RETURN res
1459
END save;
1460
 
1461
 
1462
PROCEDURE redoGuard (text: tText; guard: tGuard);
1463
BEGIN
1464
    text.edition := guard;
1465
    text.cursor^ := guard.cursor;
1466
    text.select2^ := guard.select2;
1467
    text.scroll := guard.scroll;
1468
    text.CurX := guard.CurX;
1469
    IF guard.selected THEN
1470
        text.select := text.select2
1471
    ELSE
1472
        text.select := text.cursor
1473
    END;
1474
    text.curLine := getLine(text, text.cursor.Y);
1475
    text.comments := TRUE;
1476
    text.search := TRUE
1477
END redoGuard;
1478
 
1479
 
1480
PROCEDURE undo* (text: tText);
1481
VAR
1482
    item: List.tItem;
1483
    guard: tGuard;
1484
BEGIN
1485
    guard := text.edition;
1486
    item := guard.prev;
1487
    WHILE (item # NIL) & ~(item IS tGuard) DO
1488
        item := item.prev
1489
    END;
1490
 
1491
    IF item # NIL THEN
1492
        guard := item(tGuard);
9073 leency 1493
        text.edition := guard
8728 leency 1494
    END;
1495
 
9050 leency 1496
    item := ChangeLog.CL.Log.first;
8728 leency 1497
    WHILE item # guard DO
1498
        ChangeLog.redo(item);
1499
        item := item.next
1500
    END;
1501
    redoGuard(text, guard);
9073 leency 1502
    ChangeLog.setGuard(guard);
9174 akron1 1503
    text.modified := ~guard.saved;
1504
    ShowCursor
8728 leency 1505
END undo;
1506
 
1507
 
1508
PROCEDURE redo* (text: tText);
1509
VAR
1510
    item: List.tItem;
1511
    guard: tGuard;
1512
BEGIN
1513
    guard := text.edition;
1514
    item := guard.next;
1515
    WHILE (item # NIL) & ~(item IS tGuard) DO
1516
        ChangeLog.redo(item);
1517
        item := item.next
1518
    END;
1519
    IF item # NIL THEN
1520
        guard := item(tGuard);
1521
        redoGuard(text, guard)
1522
    END;
9073 leency 1523
    ChangeLog.setGuard(guard);
9174 akron1 1524
    text.modified := ~guard.saved;
1525
    ShowCursor
8728 leency 1526
END redo;
1527
 
1528
 
9462 akron1 1529
PROCEDURE getSelCnt* (text: tText; VAR chars, lines: INTEGER);
1530
VAR
1531
	selBeg, selEnd: tPoint;
1532
	first, last, line: tLine;
1533
 
1534
	PROCEDURE charCnt (line: tLine; first, last: INTEGER): INTEGER;
1535
	VAR
1536
		i, res: INTEGER;
1537
	BEGIN
1538
		res := 0;
1539
		FOR i := first TO last DO
9659 akron1 1540
			IF Lines.getChar(line, i) # TAB1 THEN
9462 akron1 1541
				INC(res)
1542
			END
1543
		END
1544
		RETURN res
1545
	END charCnt;
1546
 
1547
BEGIN
1548
	IF selected(text) THEN
1549
		getSelect(text, selBeg, selEnd);
9668 akron1 1550
		first := getLine2(text, selBeg.Y);
1551
		last  := getLine2(text, selEnd.Y);
9462 akron1 1552
		lines := selEnd.Y - selBeg.Y + 1;
1553
 
1554
		IF lines > 1 THEN
1555
			chars := charCnt(first, selBeg.X, first.length - 1) + charCnt(last, 0, selEnd.X - 1) + lenEOL;
9659 akron1 1556
			line := first.next(tLine);
1557
			WHILE line # last DO
1558
				INC(chars, charCnt(line, 0, line.length - 1) + lenEOL);
1559
				NextLine(line)
1560
			END
9462 akron1 1561
		ELSE
9659 akron1 1562
			chars := charCnt(first, selBeg.X, selEnd.X - 1)
9462 akron1 1563
		END
1564
	ELSE
1565
		chars := 0;
1566
		lines := 0
1567
	END
1568
END getSelCnt;
1569
 
1570
 
8728 leency 1571
PROCEDURE copy (text: tText);
1572
VAR
1573
    selBeg, selEnd: tPoint;
1574
    first, line: tLine;
1575
    cnt, n: INTEGER;
1576
    buffer: CB.tBuffer;
1577
 
1578
 
1579
    PROCEDURE append (buffer: CB.tBuffer; line: tLine; first, last: INTEGER);
1580
    BEGIN
1581
        IF first <= last THEN
1582
            CB.append(buffer, line, first, last)
1583
        ELSE
1584
            IF U.OS = "KOS" THEN
1585
                CB.appends(buffer, SPACE, 0, 0)
1586
            END
1587
        END
1588
    END append;
1589
 
1590
 
1591
BEGIN
1592
    getSelect(text, selBeg, selEnd);
1593
 
9668 akron1 1594
    first := getLine2(text, selBeg.Y);
8728 leency 1595
    line := first;
1596
 
1597
    n := selEnd.Y - selBeg.Y;
1598
    cnt := 0;
1599
    WHILE n >= 0 DO
9457 akron1 1600
        INC(cnt, line.length + (lenEOL + ORD(U.OS = "KOS")));
8728 leency 1601
        NextLine(line);
1602
        DEC(n)
1603
    END;
1604
 
9599 akron1 1605
    buffer := CB.create(cnt + 2); (* +2 wchars EOT *)
8728 leency 1606
 
1607
    n := selEnd.Y - selBeg.Y;
1608
    line := first;
1609
    IF n = 0 THEN
9457 akron1 1610
        append(buffer, line, selBeg.X, selEnd.X - 1)
8728 leency 1611
    ELSE
1612
        append(buffer, line, selBeg.X, line.length - 1);
1613
        REPEAT
1614
            DEC(n);
1615
            CB.eol(buffer);
1616
            NextLine(line);
1617
            IF n > 0 THEN
1618
                append(buffer, line, 0, line.length - 1)
1619
            END
1620
        UNTIL n = 0;
1621
        append(buffer, line, 0, selEnd.X - 1)
1622
    END;
1623
    CB.eot(buffer);
1624
    CB.put(buffer);
1625
    CB.destroy(buffer)
1626
END copy;
1627
 
1628
 
1629
PROCEDURE paste (text: tText);
1630
VAR
1631
    line, newLine, curLine: tLine;
9174 akron1 1632
    w: INTEGER;
8728 leency 1633
    cliptext: RW.tInput;
1634
    eol: BOOLEAN;
1635
    cursor: pPoint;
9174 akron1 1636
 
1637
 
1638
    PROCEDURE lineWidth (line: tLine; pos: INTEGER): INTEGER;
1639
    VAR
1640
    	i, res: INTEGER;
1641
    	c: WCHAR;
1642
    BEGIN
1643
    	res := pos;
1644
    	i := 0;
1645
    	REPEAT
9659 akron1 1646
	    	c := Lines.getChar(line, i);
9174 akron1 1647
	    	IF c = TAB THEN
1648
	    		INC(res, Lines.tab - res MOD Lines.tab)
1649
	    	ELSIF c # TAB1 THEN
1650
	    		INC(res)
1651
	    	END;
1652
	    	INC(i)
1653
    	UNTIL c = 0X
1654
    	RETURN res - pos - 1
1655
    END lineWidth;
1656
 
1657
 
8728 leency 1658
BEGIN
1659
    line := Lines.create(TRUE);
1660
    cliptext := RW.clipboard();
1661
    delSelect(text);
1662
    cursor := text.cursor;
9174 akron1 1663
    WHILE (cliptext # NIL) & (RW.getString(cliptext, line, Lines.tabs, eol) >= 0) DO
1664
        IF line.length > 0 THEN
1665
        	w := lineWidth(line, cursor.X);
8728 leency 1666
            Lines.insert2(text.curLine, cursor.X, line);
1667
            Lines.modify(text.curLine);
1668
            modify(text);
9174 akron1 1669
            SetPos(text, cursor.X + w, cursor.Y);
8728 leency 1670
            resetSelect(text)
1671
        END;
1672
        IF eol THEN
1673
            newLine := Lines.create(FALSE);
1674
            modify(text);
1675
            curLine := text.curLine;
1676
            IF cursor.X < curLine.length THEN
9174 akron1 1677
                Lines.wrap(curLine, newLine, cursor.X);
1678
                Lines.modify(curLine)
8728 leency 1679
            END;
1680
            List._insert(text, curLine, newLine);
9174 akron1 1681
            Lines.modify(newLine);
8728 leency 1682
            SetPos(text, 0, cursor.Y + 1);
1683
            resetSelect(text)
1684
        END;
1685
        Lines.destroy(line);
1686
        line := Lines.create(TRUE)
1687
    END;
1688
    Lines.destroy(line);
1689
    RW.destroy(cliptext)
1690
END paste;
1691
 
1692
 
1693
PROCEDURE searchScroll (text: tText; n: INTEGER);
1694
BEGIN
1695
    IF n - text.scroll.Y > textsize.Y - 1 THEN
1696
        text.scroll.Y := MAX(n - 2 * textsize.Y DIV 3, 0)
1697
    ELSIF n < text.scroll.Y THEN
1698
        text.scroll.Y := MAX(n - textsize.Y DIV 3, 0)
1699
    END
1700
END searchScroll;
1701
 
1702
 
1703
PROCEDURE goto* (text: tText; n: INTEGER): BOOLEAN;
1704
VAR
1705
    res: BOOLEAN;
1706
BEGIN
1707
    DEC(n);
1708
    IF (0 <= n) & (n < text.count) THEN
1709
        resetSelect(text);
1710
        searchScroll(text, n);
1711
        SetPos(text, 0, n);
1712
        res := TRUE
1713
    ELSE
1714
        res := FALSE
1715
    END
1716
    RETURN res
1717
END goto;
1718
 
1719
 
9060 leency 1720
PROCEDURE toggleLabel* (text: tText);
1721
BEGIN
1722
    text.curLine.label := ~text.curLine.label
1723
END toggleLabel;
1724
 
1725
 
1726
PROCEDURE gotoLabel* (text: tText; frw: BOOLEAN);
1727
VAR
1728
    line: tLine;
1729
    n: INTEGER;
9073 leency 1730
 
1731
    PROCEDURE search (VAR line: tLine; VAR n: INTEGER; frw: BOOLEAN);
1732
    BEGIN
1733
        IF frw THEN
1734
            WHILE (line # NIL) & ~line.label DO
1735
                NextLine(line);
1736
                INC(n)
1737
            END
1738
        ELSE
1739
            WHILE (line # NIL) & ~line.label DO
1740
                PrevLine(line);
1741
                DEC(n)
1742
            END
1743
        END
1744
    END search;
1745
 
9060 leency 1746
BEGIN
1747
    n := text.cursor.Y;
1748
    line := text.curLine;
1749
    IF frw THEN
9073 leency 1750
        NextLine(line);
1751
        INC(n)
9060 leency 1752
    ELSE
9073 leency 1753
        PrevLine(line);
1754
        DEC(n)
9060 leency 1755
    END;
9073 leency 1756
    search(line, n, frw);
1757
    IF line = NIL THEN
1758
        IF frw THEN
1759
            n := 0;
1760
            line := text.first(tLine)
1761
        ELSE
1762
            n := text.count - 1;
1763
            line := text.last(tLine)
1764
        END;
1765
        search(line, n, frw)
1766
    END;
9060 leency 1767
    IF line # NIL THEN
1768
        IF goto(text, n + 1) THEN END
1769
    END
1770
END gotoLabel;
1771
 
1772
 
8728 leency 1773
PROCEDURE changeCase (text: tText; upper: BOOLEAN);
1774
VAR
1775
    i: INTEGER;
1776
    line: tLine;
9674 akron1 1777
    func: Lines.fConvert;
8728 leency 1778
BEGIN
1779
    line := text.curLine;
1780
    i := text.cursor.X - 1;
1781
 
9659 akron1 1782
    WHILE (i >= 0) & U.isLetter(Lines.getChar(line, i)) DO
8728 leency 1783
        DEC(i)
1784
    END;
1785
 
9674 akron1 1786
    IF upper THEN
1787
    	func := U.cap
1788
    ELSE
1789
    	func := U.low
1790
    END;
1791
 
1792
    IF Lines.convert(line, i + 1, text.cursor.X - 1, func) THEN
9668 akron1 1793
    	INC(text.smallChange);
8728 leency 1794
        modify(text)
1795
    END
1796
END changeCase;
1797
 
1798
 
1799
PROCEDURE chCase* (text: tText; upper: BOOLEAN);
1800
VAR
1801
    selBeg, selEnd: tPoint;
1802
    first, line: Lines.tLine;
1803
    cnt: INTEGER;
9674 akron1 1804
    func: Lines.fConvert;
8728 leency 1805
    modified: BOOLEAN;
1806
BEGIN
1807
    modified := FALSE;
1808
    IF selected(text) THEN
1809
        getSelect(text, selBeg, selEnd);
9668 akron1 1810
        first := getLine2(text, selBeg.Y);
8728 leency 1811
        line := first;
1812
        cnt := selEnd.Y - selBeg.Y;
9674 akron1 1813
 
1814
	    IF upper THEN
1815
	    	func := U.cap
1816
	    ELSE
1817
	    	func := U.low
1818
	    END;
1819
 
8728 leency 1820
        IF cnt = 0 THEN
9674 akron1 1821
            IF Lines.convert(line, selBeg.X, selEnd.X - 1, func) THEN
8728 leency 1822
                modified := TRUE
1823
            END
1824
        ELSE
9674 akron1 1825
            IF Lines.convert(line, selBeg.X, line.length - 1, func) THEN
8728 leency 1826
                modified := TRUE
1827
            END;
1828
            WHILE cnt > 1 DO
1829
                NextLine(line);
9674 akron1 1830
                IF Lines.convert(line, 0, line.length - 1, func) THEN
8728 leency 1831
                    modified := TRUE
1832
                END;
1833
                DEC(cnt)
1834
            END;
1835
            NextLine(line);
9674 akron1 1836
            IF Lines.convert(line, 0, selEnd.X - 1, func) THEN
8728 leency 1837
                modified := TRUE
1838
            END
1839
        END
1840
    END;
1841
    IF modified THEN
9668 akron1 1842
    	INC(text.smallChange);
8728 leency 1843
        modify(text)
1844
    END
1845
END chCase;
1846
 
1847
 
1848
PROCEDURE UpDown (text: tText; step: INTEGER);
1849
VAR
1850
    temp: INTEGER;
1851
BEGIN
1852
    IF text.CurX = -1 THEN
1853
        text.CurX := text.cursor.X
1854
    END;
1855
    temp := text.CurX;
1856
    SetPos(text, temp, text.cursor.Y + step);
1857
    text.CurX := temp
1858
END UpDown;
1859
 
1860
 
1861
PROCEDURE delLine* (text: tText);
1862
BEGIN
9668 akron1 1863
	text.smallChange := 2;
8728 leency 1864
    resetSelect(text);
1865
    IF text.curLine.length > 0 THEN
1866
        Lines.delCharN(text.curLine, 0, text.curLine.length)
1867
    END;
1868
    SetPos(text, 0, text.cursor.Y);
1869
    IF text.cursor.Y = text.count - 1 THEN
1870
        BkSpace(text)
1871
    ELSE
1872
        delete(text)
1873
    END
1874
END delLine;
1875
 
1876
 
9174 akron1 1877
PROCEDURE dupLine* (text: tText);
9010 leency 1878
VAR
1879
    newLine, curLine: tLine;
1880
BEGIN
1881
    curLine := text.curLine;
1882
    newLine := Lines.create(FALSE);
1883
    modify(text);
1884
    Lines.insert3(newLine, 0, curLine.length);
1885
    List._insert(text, curLine, newLine);
9174 akron1 1886
    Lines.move(curLine, newLine);
1887
    Lines.modify(newLine)
9010 leency 1888
END dupLine;
1889
 
1890
 
9413 akron1 1891
PROCEDURE MoveLines* (text: tText; down: BOOLEAN);
1892
VAR
9560 akron1 1893
	last, first, line: tLine;
9413 akron1 1894
	selBeg, selEnd, temp: tPoint;
9560 akron1 1895
	step: INTEGER;
9413 akron1 1896
	frw: BOOLEAN;
1897
BEGIN
1898
	getSelect(text, selBeg, selEnd);
9560 akron1 1899
	IF ((selBeg.Y > 0) & ~down OR (selEnd.Y < text.count - 1) & down) THEN
1900
		modify(text);
1901
		step := ORD(down)*2 - 1;
1902
		IF selBeg.Y # selEnd.Y THEN
1903
			frw := (text.cursor.X = selEnd.X) & (text.cursor.Y = selEnd.Y);
1904
 
9413 akron1 1905
			IF down # frw THEN
1906
				temp := text.cursor^;
1907
				SetPos(text, 0, text.select.Y);
1908
				setSelect(text);
1909
				text.select^ := temp
1910
			END;
9560 akron1 1911
 
1912
			ASSERT(selBeg.Y < selEnd.Y);
1913
 
1914
			first := getLine(text, selBeg.Y);
9413 akron1 1915
			last := getLine(text, selEnd.Y);
9560 akron1 1916
 
1917
			line := first;
1918
			Lines.modify(line);
1919
			REPEAT
1920
				NextLine(line);
1921
				Lines.modify(line)
1922
			UNTIL line = last;
1923
 
1924
			IF down THEN
1925
				text.curLine := last.prev(tLine)
1926
			ELSE
1927
				text.curLine := first.next(tLine)
1928
			END;
9413 akron1 1929
			selBeg.X := 0;
1930
			selEnd.X := last.length;
9560 akron1 1931
			IF down THEN
1932
				last := last.next(tLine);
1933
				List._delete(text, last);
1934
				List._insert(text, first.prev, last)
1935
			ELSE
1936
				first := first.prev(tLine);
1937
				List._delete(text, first);
1938
				List._insert(text, last, first)
1939
			END;
1940
			IF down THEN
1941
				temp := selBeg;
1942
				selBeg := selEnd;
1943
				selEnd := temp;
1944
			END;
1945
			SetPos(text, selBeg.X, selBeg.Y + step);
1946
			setSelect(text);
1947
			text.select.X := selEnd.X;
1948
			text.select.Y := selEnd.Y + step
9413 akron1 1949
		ELSE
9560 akron1 1950
			first := getLine(text, selBeg.Y);
1951
			Lines.modify(first);
1952
			IF down THEN
1953
				last := first.next(tLine)
1954
			ELSE
1955
				last := first.prev.prev(tLine)
1956
			END;
1957
			List._delete(text, first);
1958
			List._insert(text, last, first);
1959
			INC(text.cursor.Y, step);
1960
			INC(text.select.Y, step);
1961
			SetPos(text, text.cursor.X, text.cursor.Y)
1962
		END
9413 akron1 1963
	END
1964
END MoveLines;
1965
 
1966
 
9010 leency 1967
PROCEDURE isWordChar (c: WCHAR): BOOLEAN;
1968
    RETURN U.isLetter(c) OR U.isDigit(c) OR (c = "_")
1969
END isWordChar;
1970
 
1971
 
9197 akron1 1972
PROCEDURE getSelectedText* (text: tText; VAR s: ARRAY OF WCHAR);
1973
VAR
1974
    n: INTEGER;
1975
    selBeg, selEnd: tPoint;
1976
BEGIN
1977
	s[0] := 0X;
1978
    IF selected(text) & (text.cursor.Y = text.select.Y) THEN
1979
        getSelect(text, selBeg, selEnd);
1980
        n := getString(text.curLine, selBeg.X, selEnd.X - selBeg.X, s)
1981
    END
1982
END getSelectedText;
1983
 
1984
 
9010 leency 1985
PROCEDURE wordSel* (text: tText);
1986
VAR
1987
    n, i, x1, x2: INTEGER;
1988
    selBeg, selEnd: tPoint;
1989
    str: tString;
1990
    curLine: tLine;
1991
BEGIN
1992
    curLine := text.curLine;
1993
    IF selected(text) & (text.cursor.Y = text.select.Y) THEN
1994
        getSelect(text, selBeg, selEnd);
1995
        x1 := selBeg.X;
1996
        x2 := selEnd.X;
1997
        n := getString(curLine, x1, x2 - x1, str);
1998
    ELSE
1999
        str := ""
2000
    END;
2001
    IF str # "" THEN
2002
        i := 0;
2003
        WHILE (i < n) & isWordChar(str[i]) DO
2004
            INC(i)
2005
        END;
2006
        IF (i # n) OR
9659 akron1 2007
            ((x1 > 0) & isWordChar(Lines.getChar(curLine, x1 - 1))) OR
2008
            ((x2 < curLine.length) & isWordChar(Lines.getChar(curLine, x2))) THEN
9010 leency 2009
            str := ""
2010
        END
2011
    END;
9668 akron1 2012
    IF text.searchText # str THEN
2013
    	text.smallMove := FALSE
2014
    END;
9050 leency 2015
    IF search(text, str, Lang.isCS(text.lang), TRUE) THEN END
9010 leency 2016
END wordSel;
2017
 
2018
 
9336 akron1 2019
PROCEDURE getWordPos (line: tLine; pos: INTEGER): INTEGER;
2020
VAR
2021
	c: WCHAR;
2022
BEGIN
9659 akron1 2023
	c := Lines.getChar(line, pos);
9336 akron1 2024
	IF isWordChar(c) THEN
9659 akron1 2025
		WHILE (pos < line.length) & isWordChar(Lines.getChar(line, pos)) DO
9336 akron1 2026
			INC(pos)
2027
		END
2028
	ELSIF Lines.isSpace(c) THEN
9659 akron1 2029
		WHILE (pos < line.length) & Lines.isSpace(Lines.getChar(line, pos)) DO
9336 akron1 2030
			INC(pos)
2031
		END
2032
	ELSE
9659 akron1 2033
		WHILE (pos < line.length) & ~Lines.isSpace(Lines.getChar(line, pos)) & ~isWordChar(Lines.getChar(line, pos)) DO
9336 akron1 2034
			INC(pos)
2035
		END
2036
	END
2037
	RETURN pos
2038
END getWordPos;
2039
 
2040
 
9073 leency 2041
PROCEDURE key* (text: tText; code: INTEGER; shift, ctrl: BOOLEAN);
9174 akron1 2042
VAR
9336 akron1 2043
	n, wPos: INTEGER;
9668 akron1 2044
	scrX, scrY: INTEGER;
2045
	resSel: BOOLEAN;
8728 leency 2046
BEGIN
9668 akron1 2047
	resSel := FALSE;
9073 leency 2048
    IF shift THEN
8728 leency 2049
        setSelect(text)
2050
    ELSE
2051
        IF (33 <= code) & (code <= 40) THEN
9413 akron1 2052
        	IF ~(((code = 38) OR (code = 40)) & ctrl) THEN
9668 akron1 2053
        		resSel := selected(text);
9413 akron1 2054
	            resetSelect(text)
2055
            END
8728 leency 2056
        END
2057
    END;
2058
 
2059
    CASE code OF
2060
    |33:
9073 leency 2061
        IF ctrl THEN
8728 leency 2062
            UpDown(text, text.scroll.Y - text.cursor.Y)
2063
        ELSE
2064
            text.scroll.Y := MAX(text.scroll.Y - textsize.Y, 0);
2065
            UpDown(text, -textsize.Y)
2066
        END
2067
    |34:
9073 leency 2068
        IF ctrl THEN
8728 leency 2069
            UpDown(text, MIN(text.scroll.Y + textsize.Y - 1, text.count - 1) - text.cursor.Y)
2070
        ELSE
2071
            text.scroll.Y := MIN(text.scroll.Y + textsize.Y, text.count - 1);
2072
            UpDown(text, textsize.Y)
2073
        END
2074
    |35:
9073 leency 2075
        IF ctrl THEN
8728 leency 2076
            SetPos(text, text.last(tLine).length, text.count - 1)
2077
        ELSE
2078
            SetPos(text, text.curLine.length, text.cursor.Y)
2079
        END
2080
    |36:
9073 leency 2081
        IF ctrl THEN
8728 leency 2082
            SetPos(text, 0, 0)
2083
        ELSE
9174 akron1 2084
        	n := leadingSpaces(text.curLine);
9659 akron1 2085
        	IF text.cursor.X <= n THEN
2086
	            n := 0
2087
        	END;
2088
        	SetPos(text, n, text.cursor.Y)
8728 leency 2089
        END
2090
    |37:
2091
        IF (text.cursor.X = 0) & (text.curLine.prev # NIL) THEN
2092
            SetPos(text, text.curLine.prev(tLine).length, text.cursor.Y - 1)
2093
        ELSE
9668 akron1 2094
        	scrX := text.scroll.X;
2095
        	scrY := text.scroll.Y;
9336 akron1 2096
        	IF ctrl THEN
2097
        		wPos := 0;
2098
        		REPEAT
2099
        			n := wPos;
2100
        			wPos := getWordPos(text.curLine, wPos)
2101
        		UNTIL wPos >= text.cursor.X;
2102
				move(text, n - text.cursor.X)
2103
        	ELSE
2104
            	move(text, -1)
9668 akron1 2105
            END;
2106
            text.smallMove := (scrX = text.scroll.X) & (scrY = text.scroll.Y) & ~resSel
8728 leency 2107
        END
2108
    |38:
9073 leency 2109
        IF ctrl THEN
9413 akron1 2110
            MoveLines(text, FALSE)
9010 leency 2111
        ELSE
2112
            UpDown(text, -1)
2113
        END
8728 leency 2114
    |39:
2115
        IF (text.cursor.X = text.curLine.length) & (text.curLine.next # NIL) THEN
2116
            SetPos(text, 0, text.cursor.Y + 1)
2117
        ELSE
9668 akron1 2118
        	scrX := text.scroll.X;
2119
        	scrY := text.scroll.Y;
9336 akron1 2120
        	IF ctrl THEN
2121
				move(text, getWordPos(text.curLine, text.cursor.X) - text.cursor.X)
2122
        	ELSE
2123
            	move(text, 1)
9668 akron1 2124
        	END;
2125
        	text.smallMove := (scrX = text.scroll.X) & (scrY = text.scroll.Y) & ~resSel
8728 leency 2126
        END
2127
    |40:
9073 leency 2128
        IF ctrl THEN
9413 akron1 2129
            MoveLines(text, TRUE)
9010 leency 2130
        ELSE
2131
            UpDown(text, 1)
2132
        END
2133
    |46:
9073 leency 2134
        IF ctrl THEN
9010 leency 2135
            delLine(text)
2136
        ELSE
9668 akron1 2137
        	IF ~selected(text) & (text.cursor.X < text.curLine.length) THEN
2138
        		INC(text.smallChange)
2139
        	END;
9174 akron1 2140
            delete(text);
2141
            ShowCursor
9010 leency 2142
        END
9659 akron1 2143
    |ORD("C"), ORD("X"):
9073 leency 2144
        IF ctrl THEN
8728 leency 2145
            IF selected(text) THEN
2146
                copy(text);
9659 akron1 2147
                IF code = ORD("X") THEN
2148
                	delSelect(text)
2149
                END
8728 leency 2150
            END
2151
        END
2152
    |ORD("V"):
9073 leency 2153
        IF ctrl THEN
8728 leency 2154
            IF CB.available() THEN
2155
                paste(text)
2156
            END
2157
        END
2158
    |ORD("A"):
9073 leency 2159
        IF ctrl THEN
8728 leency 2160
            text.select2.X := 0;
2161
            text.select2.Y := 0;
2162
            text.select := text.select2;
2163
            SetPos(text, text.last(tLine).length, text.count - 1)
2164
        END
2165
    |ORD("L"), ORD("U"):
9073 leency 2166
        IF ctrl THEN
9174 akron1 2167
        	IF selected(text) THEN
2168
            	chCase(text, code = ORD("U"))
2169
            ELSE
2170
            	changeCase(text, code = ORD("U"))
2171
            END;
2172
            ShowCursor
8728 leency 2173
        END
9010 leency 2174
    |ORD("D"):
9073 leency 2175
        IF ctrl THEN
9010 leency 2176
            dupLine(text)
2177
        END
8728 leency 2178
    ELSE
2179
    END
2180
END key;
2181
 
2182
 
2183
PROCEDURE mouse* (text: tText; x, y: INTEGER);
2184
VAR
2185
    cursorX: INTEGER;
2186
BEGIN
2187
    DEC(x, padding.left);
2188
    DEC(y, padding.top);
2189
    cursorX := (x*2) DIV charWidth;
2190
    SetPos(text, cursorX DIV 2 + cursorX MOD 2 + text.scroll.X, y DIV charHeight + text.scroll.Y)
2191
END mouse;
2192
 
2193
 
2194
PROCEDURE selectWord* (text: tText);
2195
VAR
2196
    cursorX, x1, x2: INTEGER;
2197
    line: tLine;
2198
BEGIN
2199
    resetSelect(text);
2200
    cursorX := text.cursor.X;
2201
    line := text.curLine;
2202
    x1 := cursorX - 1;
9659 akron1 2203
    IF (cursorX < line.length) & isWordChar(Lines.getChar(line, cursorX)) THEN
8728 leency 2204
        x2 := cursorX;
9659 akron1 2205
        WHILE (x2 < line.length) & isWordChar(Lines.getChar(line, x2)) DO
8728 leency 2206
            INC(x2)
2207
        END
2208
    ELSE
9659 akron1 2209
        WHILE (x1 >= 0) & ~isWordChar(Lines.getChar(line, x1)) DO
8728 leency 2210
            DEC(x1)
2211
        END;
2212
        x2 := x1 + 1
2213
    END;
9659 akron1 2214
    WHILE (x1 >= 0) & isWordChar(Lines.getChar(line, x1)) DO
8728 leency 2215
        DEC(x1)
2216
    END;
2217
    INC(x1);
2218
    IF x1 < x2 THEN
2219
        SetPos(text, x1, text.cursor.Y);
2220
        setSelect(text);
2221
        SetPos(text, x2, text.cursor.Y)
2222
    END
2223
END selectWord;
2224
 
2225
 
9668 akron1 2226
PROCEDURE cursor* (text: tText);
8728 leency 2227
VAR
9295 akron1 2228
    x, y1, y2, scrollX, scrollY: INTEGER;
8728 leency 2229
    cursor: pPoint;
2230
BEGIN
9668 akron1 2231
	IF drawCursor THEN
2232
	    cursor := text.cursor;
2233
	    scrollX := text.scroll.X;
2234
	    scrollY := text.scroll.Y;
2235
	    IF ~((scrollY > cursor.Y) OR (scrollY + textsize.Y <= cursor.Y) OR
2236
	       (scrollX > cursor.X) OR (scrollX + textsize.X <= cursor.X)) THEN
2237
	        x := (cursor.X - scrollX)*charWidth + padding.left;
2238
	        y1 := (cursor.Y - scrollY)*charHeight + padding.top + (inter DIV 2 + 1);
2239
	        y2 := y1 + charHeight - (inter + 2);
2240
	        G.notVLine(canvas, x, y1, y2);
2241
	        G.notVLine(canvas, x - 1, y1, y2)
2242
	    END
8728 leency 2243
    END
2244
END cursor;
2245
 
2246
 
2247
PROCEDURE drawSelect (text: tText; line: tLine; selBeg, selEnd, y: INTEGER);
2248
VAR
2249
    Len, pos, x, firstCharIdx: INTEGER;
2250
BEGIN
2251
    firstCharIdx := MAX(text.scroll.X, selBeg);
2252
    Len := MAX(MIN(line.length - firstCharIdx, selEnd - firstCharIdx), 0);
2253
    Len := MIN(Len, textsize.X - pos + 1);
2254
    SetColor(colors.seltext, colors.selback);
2255
    pos := MAX((selBeg - text.scroll.X), 0);
2256
    x := pos*charWidth + padding.left;
2257
    G.SetColor(canvas, colors.selback);
2258
    G.FillRect(canvas, x - 2, y - inter DIV 2, x + 1 + Len*charWidth, y - inter DIV 2 + charHeight);
9193 akron1 2259
    G.TextOut(canvas, pos*charWidth + padding.left, y, Lines.getPChar(line, firstCharIdx), Len, colors.seltext)
8728 leency 2260
END drawSelect;
2261
 
2262
 
2263
PROCEDURE mark (line: tLine; y: INTEGER);
2264
VAR
2265
    color, i: INTEGER;
2266
BEGIN
2267
    IF line.modified THEN
2268
        color := colors.modified
2269
    ELSIF line.saved THEN
2270
        color := colors.saved
2271
    ELSE
2272
        color := colors.back
2273
    END;
2274
    G.SetColor(canvas, color);
2275
 
2276
    FOR i := 3 TO mark_width + 2 DO
2277
        G.VLine(canvas, padding.left - i, y, y + charHeight)
2278
    END
2279
END mark;
2280
 
2281
 
2282
PROCEDURE setPadding (left, top: INTEGER);
2283
BEGIN
2284
    padding.left := left;
2285
    padding.top := top;
2286
    textsize.X := (size.X - padding.left) DIV charWidth;
2287
    textsize.Y := (size.Y - padding.top) DIV charHeight;
2288
END setPadding;
2289
 
2290
 
2291
PROCEDURE draw* (text: tText);
2292
VAR
9668 akron1 2293
    y, n, cnt, i, x: INTEGER;
8728 leency 2294
    line, firstLine, lastLine: tLine;
2295
    selBeg, selEnd: tPoint;
2296
    s: ARRAY 12 OF WCHAR;
2297
    backColor, numWidth, xNum, wNum: INTEGER;
2298
    p: Search.tPos;
2299
    guard: tGuard;
2300
BEGIN
9668 akron1 2301
    IF text.comments THEN
8728 leency 2302
        Comments(text)
2303
    END;
9668 akron1 2304
    IF text.search & search(text, text.searchText, text.cs, text.whole) THEN END;
8728 leency 2305
    IF text.guard THEN
2306
        NEW(guard);
9050 leency 2307
        List.append(ChangeLog.CL.Log, guard);
9073 leency 2308
        guard.saved := ChangeLog.isFirstGuard(guard);
8728 leency 2309
        text.edition := guard;
9073 leency 2310
        text.guard := FALSE
8728 leency 2311
    ELSE
2312
        guard := text.edition
2313
    END;
2314
 
2315
    guard.cursor := text.cursor^;
2316
    guard.select2 := text.select2^;
2317
    guard.scroll := text.scroll;
2318
    guard.CurX := text.CurX;
2319
    guard.selected := text.select = text.select2;
2320
 
2321
    G.SetColor(canvas, colors.back);
9668 akron1 2322
    IF ~text.smallMove THEN
2323
    	G.clear(canvas)
2324
    END;
9060 leency 2325
    wNum := charWidth;
9671 akron1 2326
    IF lineNumbers THEN
8728 leency 2327
        numWidth := U.lg10(text.count) + 2;
2328
        xNum := numWidth*wNum - wNum DIV 2;
2329
        setPadding(numWidth*wNum + pad_left, padding.top);
2330
    ELSE
9060 leency 2331
        setPadding(pad_left + wNum*2, padding.top)
8728 leency 2332
    END;
2333
    getSelect(text, selBeg, selEnd);
2334
    y := padding.top + inter DIV 2;
2335
    n := text.scroll.Y;
9668 akron1 2336
    firstLine := getLine2(text, n);
2337
    IF text.smallMove THEN
2338
    	line := text.curLine;
2339
    	cnt := textsize.Y - 1;
2340
    	y := y + charHeight*(text.cursor.Y - text.scroll.Y);
2341
    	G.SetColor(canvas, colors.back);
2342
    	G.FillRect(canvas, padding.left - 2, y - inter DIV 2, size.X - 1, y - inter DIV 2 + charHeight);
2343
    	n := text.cursor.Y
2344
    ELSE
2345
	    line := firstLine;
2346
    	cnt := 0
2347
    END;
2348
 
9210 akron1 2349
    WHILE (line # NIL) & (cnt < textsize.Y) DO
8728 leency 2350
        backColor := colors.back;
2351
        IF (line = text.curLine) & ~selected(text) THEN
2352
            G.SetColor(canvas, colors.curline);
2353
            G.FillRect(canvas, padding.left - 2, y - inter DIV 2, size.X - 1, y - inter DIV 2 + charHeight);
2354
            backColor := colors.curline
2355
        END;
2356
        SetColor(colors.text, backColor);
9668 akron1 2357
		IF (selBeg.Y < n) & (n < selEnd.Y) THEN
2358
			drawSelect(text, line, 0, line.length, y)
2359
		ELSE
2360
			G.TextOut(canvas, padding.left, y, Lines.getPChar(line, text.scroll.X), MIN(MAX(line.length - text.scroll.X, 0), textsize.X + 1), colors.delim);
2361
			IF text.lang # Lang.langText THEN
2362
				parse(text, line, y, backColor, text.lang)
2363
			END
2364
		END;
2365
		IF (selBeg.Y = n) & (selEnd.Y = n) & (selBeg.X # selEnd.X) THEN
8728 leency 2366
            drawSelect(text, line, selBeg.X, selEnd.X, y)
2367
        ELSIF (selBeg.Y = n) & (selEnd.Y # n) THEN
2368
            drawSelect(text, line, selBeg.X, line.length, y)
2369
        ELSIF (selBeg.Y # n) & (selEnd.Y = n) THEN
2370
            drawSelect(text, line, 0, selEnd.X, y)
2371
        END;
9668 akron1 2372
        mark(line, y - inter DIV 2);
8728 leency 2373
        NextLine(line);
2374
        INC(y, charHeight);
2375
        INC(n);
2376
        INC(cnt)
2377
    END;
9668 akron1 2378
 
9060 leency 2379
    G.SetColor(canvas, colors.numback);
2380
    G.FillRect(canvas, 0, 0, padding.left - pad_left (*+ 1*), size.Y - 1);
2381
    line := firstLine;
2382
    SetColor(colors.numtext, colors.numback);
2383
    y := padding.top + inter DIV 2;
9210 akron1 2384
    n := MIN(text.scroll.Y + textsize.Y, text.count);
9060 leency 2385
    FOR i := text.scroll.Y + 1 TO n DO
9671 akron1 2386
        IF lineNumbers THEN
9060 leency 2387
            IF (i MOD 10 = 0) OR (i - 1 = text.cursor.Y) OR line.label THEN
8728 leency 2388
                U.int2str(i, s);
9060 leency 2389
                G.TextOut2(canvas, (numWidth - U.lg10(i) - 1)*wNum - wNum DIV 2, y, s, LENGTH(s))
8728 leency 2390
            ELSE
2391
                G.SetColor(canvas, colors.numtext);
9208 akron1 2392
                G.HLine(canvas, y - inter DIV 2 + charHeight DIV 2, xNum - wNum DIV (1 + ORD(i MOD 5 # 0)), xNum)
9060 leency 2393
            END
2394
        END;
2395
        IF line.label THEN
2396
            FOR x := wNum DIV 2 TO (padding.left - pad_left) - wNum DIV 2 DO
2397
                G.notVLine(canvas, x, y, y + charHeight - inter);
2398
                G.xorVLine(canvas, x, y, y + charHeight - inter)
2399
            END
2400
        END;
2401
        NextLine(line);
2402
        INC(y, charHeight)
8728 leency 2403
    END;
2404
 
2405
    IF text.searchText # "" THEN
9668 akron1 2406
    	IF text.smallMove THEN
2407
    		firstLine := text.curLine;
2408
    		lastLine := firstLine
2409
    	ELSE
9671 akron1 2410
    	    lastLine := getLine2(text, MIN(text.scroll.Y + textsize.Y, text.count) - 1)
8728 leency 2411
        END;
2412
        p := text.foundList.first(Search.tPos);
2413
        WHILE p # NIL DO
9668 akron1 2414
        	y := padding.top + inter DIV 2;
2415
        	IF text.smallMove THEN
2416
        		y := y + charHeight*(text.cursor.Y - text.scroll.Y)
2417
            END;
8728 leency 2418
            IF (firstLine.pos <= p.pos) & (p.pos <= lastLine.pos + lastLine.length) THEN
2419
                line := firstLine;
2420
                WHILE (line.pos <= p.pos) & (line # lastLine) DO
2421
                    NextLine(line);
2422
                    INC(y, charHeight)
2423
                END;
2424
                IF (line # lastLine) & (line # firstLine) OR (line = lastLine) & (line.pos > p.pos) THEN
2425
                    PrevLine(line);
2426
                    DEC(y, charHeight)
2427
                END;
2428
                x := (p.pos - line.pos - text.scroll.X)*charWidth + padding.left;
2429
                n := LENGTH(text.searchText)*charWidth;
2430
                WHILE n > 0 DO
2431
                    IF x >= padding.left THEN
2432
                        G.notVLine(canvas, x, y, y + charHeight - inter)
2433
                    END;
2434
                    INC(x);
2435
                    DEC(n)
9668 akron1 2436
                END
8728 leency 2437
            END;
2438
            p := p.next(Search.tPos)
2439
        END
2440
    END;
2441
 
9668 akron1 2442
    text.smallMove := FALSE;
2443
 
8728 leency 2444
    IF text.foundSel > 0 THEN
2445
        x := (text.cursor.X - text.scroll.X)*charWidth + padding.left;
2446
        y := (text.cursor.Y - text.scroll.Y)*charHeight + padding.top + inter DIV 2;
2447
        n := text.foundSel*charWidth;
2448
        WHILE n > 0 DO
2449
            IF x >= padding.left THEN
2450
                G.xorVLine(canvas, x, y, y + charHeight - inter)
2451
            END;
2452
            INC(x);
2453
            DEC(n)
2454
        END
2455
    END;
9668 akron1 2456
	cursor(text)
8728 leency 2457
END draw;
2458
 
2459
 
9050 leency 2460
PROCEDURE switch* (text: tText);
2461
BEGIN
2462
    ChangeLog.set(text.chLog);
9194 akron1 2463
    Lines.setMaxLength(text.maxLength);
2464
    Lang.setCurLang(text.lang)
9050 leency 2465
END switch;
2466
 
2467
 
8728 leency 2468
PROCEDURE create (fileName: RW.tFileName): tText;
2469
VAR
2470
    text: tText;
2471
BEGIN
2472
    NEW(text);
9050 leency 2473
    text.maxLength := 64;
2474
    text.chLog := ChangeLog.create(text.maxLength);
8728 leency 2475
    NEW(text.cursor);
2476
    NEW(text.select2);
2477
    text.cursor.X := 0;
2478
    text.cursor.Y := 0;
2479
    resetSelect(text);
2480
    text.scroll.X := 0;
2481
    text.scroll.Y := 0;
2482
    setPadding(padding.left, padding.top);
2483
    text.curLine := NIL;
2484
    text.modified := FALSE;
9668 akron1 2485
    text.smallMove := FALSE;
8728 leency 2486
    text.comments := TRUE;
2487
    text.search := TRUE;
2488
    text.cs := FALSE;
2489
    text.whole := FALSE;
2490
    text.guard := TRUE;
2491
    text.edition := NIL;
2492
    text.foundList := List.create(NIL);
2493
    text.searchText := "";
2494
    text.foundSel := 0;
2495
    text.CurX := -1;
9668 akron1 2496
    text.smallChange := 0;
9193 akron1 2497
    text.lang := Lang.langText;
9668 akron1 2498
    text.LinesVector := NIL;
9193 akron1 2499
    Lang.setCurLang(Lang.langText);
8728 leency 2500
    setName(text, fileName);
2501
    ASSERT(text = List.create(text))
2502
    RETURN text
2503
END create;
2504
 
2505
 
2506
PROCEDURE setColors* (text, back, seltext, selback, modified, saved, curline, numtext, numback,
9413 akron1 2507
                        comment, string, escape, num, delim, key1, key2, key3: INTEGER);
8728 leency 2508
BEGIN
2509
    colors.text := text;
2510
    colors.back := back;
2511
    colors.seltext := seltext;
2512
    colors.selback := selback;
2513
    colors.modified := modified;
2514
    colors.saved := saved;
2515
    colors.curline := curline;
2516
    colors.numtext := numtext;
2517
    colors.numback := numback;
2518
    colors.comment := comment;
2519
    colors.string  := string;
9413 akron1 2520
    colors.escape  := escape;
8728 leency 2521
    colors.num := num;
2522
    colors.delim := delim;
2523
    colors.key1 := key1;
2524
    colors.key2 := key2;
2525
    colors.key3 := key3;
2526
END setColors;
2527
 
2528
 
9659 akron1 2529
PROCEDURE setCanvas* (_canvas: G.tCanvas);
8728 leency 2530
BEGIN
9659 akron1 2531
    canvas := _canvas;
2532
    charWidth := _canvas.font.width;
2533
    charHeight := _canvas.font.height + inter
8728 leency 2534
END setCanvas;
2535
 
2536
 
2537
PROCEDURE resize* (width, height: INTEGER);
2538
BEGIN
2539
    size.X := width;
2540
    size.Y := height;
2541
    setPadding(padding.left, padding.top)
2542
END resize;
2543
 
2544
 
2545
PROCEDURE destroy* (VAR text: tText);
2546
BEGIN
2547
    IF search(text, "", FALSE, FALSE) THEN END;
9448 akron1 2548
    ChangeLog.destroy(text.chLog);
9668 akron1 2549
    Lines.destroyVector(text.LinesVector);
8728 leency 2550
    DISPOSE(text.foundList);
2551
    DISPOSE(text.cursor);
2552
    DISPOSE(text.select2);
2553
    DISPOSE(text)
2554
END destroy;
2555
 
2556
 
9674 akron1 2557
PROCEDURE open* (name: RW.tFileName; ptr, size: INTEGER; VAR errno: INTEGER): tText;
8728 leency 2558
VAR
2559
    text: tText;
2560
    file: RW.tInput;
9180 akron1 2561
    n, enc, eol: INTEGER;
2562
    _eol: BOOLEAN;
8728 leency 2563
    line: tLine;
2564
BEGIN
2565
    errno := 0;
9522 akron1 2566
    text := create(name);
2567
    IF text # NIL THEN
9674 akron1 2568
    	IF ptr = 0 THEN
2569
    		file := RW.loadFromFile(name, enc, eol)
2570
    	ELSE
2571
    		file := RW.loadFromMem(ptr, size, enc, eol)
2572
    	END;
9522 akron1 2573
    	IF file = NIL THEN
2574
    		destroy(text)
2575
    	END
2576
    ELSE
2577
    	file := NIL
2578
    END;
8728 leency 2579
    IF file # NIL THEN
9336 akron1 2580
        ChangeLog.changeInt(text.enc, enc);
2581
        ChangeLog.changeInt(text.eol, eol);
8728 leency 2582
        text.enc := enc;
9180 akron1 2583
        text.eol := eol;
2584
        line := Lines.create(FALSE);
2585
        List._append(text, line);
8728 leency 2586
        REPEAT
9180 akron1 2587
            n := RW.getString(file, line, Lines.tabs, _eol);
2588
            IF _eol THEN
2589
            	line := Lines.create(FALSE);
2590
            	List._append(text, line)
8728 leency 2591
            END
9180 akron1 2592
        UNTIL ~_eol;
8728 leency 2593
        RW.destroy(file);
9180 akron1 2594
        text.curLine := text.first(tLine);
2595
        SetPos(text, 0, 0);
9668 akron1 2596
        resetSelect(text);
2597
        Comments(text)
8728 leency 2598
    ELSE
2599
        errno := 1
2600
    END
2601
    RETURN text
2602
END open;
2603
 
2604
 
2605
PROCEDURE findNext* (text: tText; prev: BOOLEAN): BOOLEAN;
2606
VAR
2607
    cursorPos, x, y, X, Y, Len: INTEGER;
2608
    p: Search.tPos;
2609
    line: tLine;
2610
    res: BOOLEAN;
2611
BEGIN
2612
    X := text.cursor.X;
2613
    Y := text.cursor.Y;
2614
    text.cursor.X := MIN(text.cursor.X, text.curLine.length);
2615
    cursorPos := text.curLine.pos + text.cursor.X - ORD(prev) - ORD(~prev & (text.foundSel = 0));
2616
    p := text.foundList.first(Search.tPos);
2617
    WHILE (p # NIL) & (p.pos <= cursorPos) DO
2618
        p := p.next(Search.tPos)
2619
    END;
2620
    IF prev THEN
2621
        IF p = NIL THEN
2622
            p := text.foundList.last(Search.tPos)
2623
        ELSE
2624
            p := p.prev(Search.tPos)
2625
        END
2626
    END;
2627
    res := p # NIL;
2628
    IF res THEN
2629
        y := 0;
2630
        line := text.first(tLine);
2631
        WHILE (line.pos <= p.pos) & (line.next # NIL) DO
2632
            NextLine(line);
2633
            INC(y)
2634
        END;
2635
        IF (line.next # NIL) OR (line.pos > p.pos) THEN
2636
            PrevLine(line);
2637
            DEC(y)
2638
        END;
2639
        resetSelect(text);
2640
        searchScroll(text, y);
2641
        x := p.pos - line.pos;
2642
        Len := LENGTH(text.searchText);
2643
        IF x + Len > text.scroll.X + textsize.X THEN
2644
            text.scroll.X := MAX(x + Len - textsize.X + 3, 0)
2645
        ELSIF x < text.scroll.X THEN
2646
            text.scroll.X := MAX(x - 3, 0)
2647
        END;
2648
        SetPos(text, x, y);
2649
        text.foundSel := Len
2650
    ELSE
2651
        SetPos(text, X, Y)
2652
    END
2653
    RETURN res
2654
END findNext;
2655
 
2656
 
2657
PROCEDURE replace* (text: tText; s: ARRAY OF WCHAR; n: INTEGER);
2658
VAR
2659
    line: tLine;
2660
    sLen, i: INTEGER;
2661
BEGIN
2662
    IF text.foundSel > 0 THEN
2663
        line := text.curLine;
2664
        sLen := LENGTH(s);
2665
        i := text.cursor.X;
2666
        IF sLen > n THEN
2667
            Lines.insert3(line, i, sLen - n)
9560 akron1 2668
        ELSIF n > sLen THEN
2669
            Lines.delCharN(line, i, n - sLen)
2670
        ELSE (* n = sLen *)
2671
        	Lines.copy(line)
8728 leency 2672
        END;
2673
        SetPos(text, i + sLen, text.cursor.Y);
9560 akron1 2674
	    WHILE sLen > 0 DO
2675
	        DEC(sLen);
2676
	        Lines.setChar(line, i + sLen, s[sLen])
2677
	    END;
8728 leency 2678
        resetSelect(text);
2679
        Lines.modify(line);
2680
        modify(text)
2681
    END
2682
END replace;
2683
 
2684
 
2685
PROCEDURE replaceAll* (text: tText; s: ARRAY OF WCHAR; n: INTEGER): INTEGER;
2686
VAR
2687
    p: Search.tPos;
2688
    line: tLine;
2689
    y, k, d, pos, y0: INTEGER;
2690
BEGIN
2691
    resetSelect(text);
2692
    SetPos(text, 0, 0);
2693
    line := text.first(tLine);
2694
    y := 0;
2695
    y0 := -1;
2696
    k := 0;
2697
    d := LENGTH(s) - n;
2698
    p := text.foundList.first(Search.tPos);
2699
    WHILE p # NIL DO
2700
        pos := p.pos;
2701
        WHILE (line.pos <= pos) & (line.next # NIL) DO
2702
            NextLine(line);
2703
            INC(y)
2704
        END;
2705
        IF (line.next # NIL) OR (line.pos > pos) THEN
2706
            PrevLine(line);
2707
            DEC(y)
2708
        END;
2709
        IF y = y0 THEN
2710
            INC(k, d)
2711
        ELSE
2712
            k := 0;
2713
            y0 := y
2714
        END;
2715
        SetPos(text, pos - line.pos + k, y);
2716
        text.foundSel := n;
2717
        replace(text, s, n);
2718
        p := p.next(Search.tPos)
2719
    END
2720
    RETURN text.foundList.count
2721
END replaceAll;
2722
 
2723
 
9674 akron1 2724
PROCEDURE conv (VAR c: WCHAR; cp: E.CP; enc: INTEGER): BOOLEAN;
2725
VAR
2726
	code: INTEGER;
2727
	res: BOOLEAN;
2728
BEGIN
2729
	res := FALSE;
2730
	IF (c # 0X) & (c # Lines.NUL) & (c # Lines.TAB1) THEN
2731
		code := E.UNI[ORD(c), enc];
2732
		IF (0 <= code) & (code <= 255) THEN
2733
			code := cp[code]
2734
		ELSE
2735
			code := ORD(c)
2736
		END;
9708 akron1 2737
		IF code # ORD(c) THEN
2738
			c := WCHR(code);
2739
			res := TRUE
2740
		END
9674 akron1 2741
	END
2742
	RETURN res
2743
END conv;
2744
 
2745
 
2746
PROCEDURE conv1251to866 (VAR c: WCHAR): BOOLEAN;
9708 akron1 2747
	RETURN conv(c, E.cp866, E.CP1251)
9674 akron1 2748
END conv1251to866;
2749
 
2750
 
2751
PROCEDURE conv866to1251 (VAR c: WCHAR): BOOLEAN;
2752
	RETURN conv(c, E.cp1251, E.CP866)
2753
END conv866to1251;
2754
 
2755
 
9708 akron1 2756
PROCEDURE convert* (text: tText; (*cp: E.CP;*) enc: INTEGER);
9674 akron1 2757
VAR
2758
	line: tLine;
2759
	func: Lines.fConvert;
2760
	modified: BOOLEAN;
2761
BEGIN
2762
	modified := FALSE;
2763
	line := text.first(tLine);
2764
	IF enc = E.CP866 THEN
2765
		func := conv866to1251
9708 akron1 2766
	ELSIF enc = E.CP1251 THEN
9674 akron1 2767
		func := conv1251to866
2768
	ELSE
2769
		line := NIL
2770
	END;
2771
 
2772
	WHILE line # NIL DO
2773
		IF Lines.convert(line, 0, line.length - 1, func) THEN
2774
			modified := TRUE
2775
		END;
2776
		NextLine(line)
2777
	END;
2778
 
2779
	IF modified THEN
2780
		modify(text)
2781
	END
2782
END convert;
2783
 
2784
 
8728 leency 2785
PROCEDURE New* (): tText;
2786
VAR
2787
    text: tText;
2788
BEGIN
2789
    text := create("");
2790
    List._append(text, Lines.create(FALSE));
2791
    text.curLine := text.first(tLine);
9336 akron1 2792
    ChangeLog.changeInt(text.enc, E.CP866);
9659 akron1 2793
    ChangeLog.changeInt(text.eol, E.EOL_CRLF);
8728 leency 2794
    text.enc := E.CP866;
9659 akron1 2795
    text.eol := E.EOL_CRLF;
8728 leency 2796
    SetPos(text, 0, 0);
2797
    resetSelect(text)
2798
    RETURN text
2799
END New;
2800
 
2801
 
9671 akron1 2802
PROCEDURE init* (pShowCursor: tProcedure; _lineNumbers, _autoIndents, _autoBrackets, _trimSpace: BOOLEAN);
8728 leency 2803
BEGIN
9174 akron1 2804
    ShowCursor := pShowCursor;
8728 leency 2805
    pdelete := delete;
2806
    drawCursor := TRUE;
9671 akron1 2807
    lineNumbers := _lineNumbers;
2808
    autoIndents := _autoIndents;
2809
    autoBrackets := _autoBrackets;
2810
    trimSpace := _trimSpace;
8728 leency 2811
    padding.left := pad_left;
2812
    padding.top := pad_top;
2813
END init;
2814
 
2815
 
2816
END Text.