Subversion Repositories Kolibri OS

Rev

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

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