Subversion Repositories Kolibri OS

Rev

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

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