Subversion Repositories Kolibri OS

Rev

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

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