Subversion Repositories Kolibri OS

Rev

Rev 9659 | Rev 9671 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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