Subversion Repositories Kolibri OS

Rev

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

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