Subversion Repositories Kolibri OS

Rev

Rev 9671 | Rev 9708 | 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.     Lines.destroy(line);
  1689.     RW.destroy(cliptext)
  1690. END paste;
  1691.  
  1692.  
  1693. PROCEDURE searchScroll (text: tText; n: INTEGER);
  1694. BEGIN
  1695.     IF n - text.scroll.Y > textsize.Y - 1 THEN
  1696.         text.scroll.Y := MAX(n - 2 * textsize.Y DIV 3, 0)
  1697.     ELSIF n < text.scroll.Y THEN
  1698.         text.scroll.Y := MAX(n - textsize.Y DIV 3, 0)
  1699.     END
  1700. END searchScroll;
  1701.  
  1702.  
  1703. PROCEDURE goto* (text: tText; n: INTEGER): BOOLEAN;
  1704. VAR
  1705.     res: BOOLEAN;
  1706. BEGIN
  1707.     DEC(n);
  1708.     IF (0 <= n) & (n < text.count) THEN
  1709.         resetSelect(text);
  1710.         searchScroll(text, n);
  1711.         SetPos(text, 0, n);
  1712.         res := TRUE
  1713.     ELSE
  1714.         res := FALSE
  1715.     END
  1716.     RETURN res
  1717. END goto;
  1718.  
  1719.  
  1720. PROCEDURE toggleLabel* (text: tText);
  1721. BEGIN
  1722.     text.curLine.label := ~text.curLine.label
  1723. END toggleLabel;
  1724.  
  1725.  
  1726. PROCEDURE gotoLabel* (text: tText; frw: BOOLEAN);
  1727. VAR
  1728.     line: tLine;
  1729.     n: INTEGER;
  1730.  
  1731.     PROCEDURE search (VAR line: tLine; VAR n: INTEGER; frw: BOOLEAN);
  1732.     BEGIN
  1733.         IF frw THEN
  1734.             WHILE (line # NIL) & ~line.label DO
  1735.                 NextLine(line);
  1736.                 INC(n)
  1737.             END
  1738.         ELSE
  1739.             WHILE (line # NIL) & ~line.label DO
  1740.                 PrevLine(line);
  1741.                 DEC(n)
  1742.             END
  1743.         END
  1744.     END search;
  1745.  
  1746. BEGIN
  1747.     n := text.cursor.Y;
  1748.     line := text.curLine;
  1749.     IF frw THEN
  1750.         NextLine(line);
  1751.         INC(n)
  1752.     ELSE
  1753.         PrevLine(line);
  1754.         DEC(n)
  1755.     END;
  1756.     search(line, n, frw);
  1757.     IF line = NIL THEN
  1758.         IF frw THEN
  1759.             n := 0;
  1760.             line := text.first(tLine)
  1761.         ELSE
  1762.             n := text.count - 1;
  1763.             line := text.last(tLine)
  1764.         END;
  1765.         search(line, n, frw)
  1766.     END;
  1767.     IF line # NIL THEN
  1768.         IF goto(text, n + 1) THEN END
  1769.     END
  1770. END gotoLabel;
  1771.  
  1772.  
  1773. PROCEDURE changeCase (text: tText; upper: BOOLEAN);
  1774. VAR
  1775.     i: INTEGER;
  1776.     line: tLine;
  1777.     func: Lines.fConvert;
  1778. BEGIN
  1779.     line := text.curLine;
  1780.     i := text.cursor.X - 1;
  1781.  
  1782.     WHILE (i >= 0) & U.isLetter(Lines.getChar(line, i)) DO
  1783.         DEC(i)
  1784.     END;
  1785.  
  1786.     IF upper THEN
  1787.         func := U.cap
  1788.     ELSE
  1789.         func := U.low
  1790.     END;
  1791.  
  1792.     IF Lines.convert(line, i + 1, text.cursor.X - 1, func) THEN
  1793.         INC(text.smallChange);
  1794.         modify(text)
  1795.     END
  1796. END changeCase;
  1797.  
  1798.  
  1799. PROCEDURE chCase* (text: tText; upper: BOOLEAN);
  1800. VAR
  1801.     selBeg, selEnd: tPoint;
  1802.     first, line: Lines.tLine;
  1803.     cnt: INTEGER;
  1804.     func: Lines.fConvert;
  1805.     modified: BOOLEAN;
  1806. BEGIN
  1807.     modified := FALSE;
  1808.     IF selected(text) THEN
  1809.         getSelect(text, selBeg, selEnd);
  1810.         first := getLine2(text, selBeg.Y);
  1811.         line := first;
  1812.         cnt := selEnd.Y - selBeg.Y;
  1813.  
  1814.             IF upper THEN
  1815.                 func := U.cap
  1816.             ELSE
  1817.                 func := U.low
  1818.             END;
  1819.  
  1820.         IF cnt = 0 THEN
  1821.             IF Lines.convert(line, selBeg.X, selEnd.X - 1, func) THEN
  1822.                 modified := TRUE
  1823.             END
  1824.         ELSE
  1825.             IF Lines.convert(line, selBeg.X, line.length - 1, func) THEN
  1826.                 modified := TRUE
  1827.             END;
  1828.             WHILE cnt > 1 DO
  1829.                 NextLine(line);
  1830.                 IF Lines.convert(line, 0, line.length - 1, func) THEN
  1831.                     modified := TRUE
  1832.                 END;
  1833.                 DEC(cnt)
  1834.             END;
  1835.             NextLine(line);
  1836.             IF Lines.convert(line, 0, selEnd.X - 1, func) THEN
  1837.                 modified := TRUE
  1838.             END
  1839.         END
  1840.     END;
  1841.     IF modified THEN
  1842.         INC(text.smallChange);
  1843.         modify(text)
  1844.     END
  1845. END chCase;
  1846.  
  1847.  
  1848. PROCEDURE UpDown (text: tText; step: INTEGER);
  1849. VAR
  1850.     temp: INTEGER;
  1851. BEGIN
  1852.     IF text.CurX = -1 THEN
  1853.         text.CurX := text.cursor.X
  1854.     END;
  1855.     temp := text.CurX;
  1856.     SetPos(text, temp, text.cursor.Y + step);
  1857.     text.CurX := temp
  1858. END UpDown;
  1859.  
  1860.  
  1861. PROCEDURE delLine* (text: tText);
  1862. BEGIN
  1863.         text.smallChange := 2;
  1864.     resetSelect(text);
  1865.     IF text.curLine.length > 0 THEN
  1866.         Lines.delCharN(text.curLine, 0, text.curLine.length)
  1867.     END;
  1868.     SetPos(text, 0, text.cursor.Y);
  1869.     IF text.cursor.Y = text.count - 1 THEN
  1870.         BkSpace(text)
  1871.     ELSE
  1872.         delete(text)
  1873.     END
  1874. END delLine;
  1875.  
  1876.  
  1877. PROCEDURE dupLine* (text: tText);
  1878. VAR
  1879.     newLine, curLine: tLine;
  1880. BEGIN
  1881.     curLine := text.curLine;
  1882.     newLine := Lines.create(FALSE);
  1883.     modify(text);
  1884.     Lines.insert3(newLine, 0, curLine.length);
  1885.     List._insert(text, curLine, newLine);
  1886.     Lines.move(curLine, newLine);
  1887.     Lines.modify(newLine)
  1888. END dupLine;
  1889.  
  1890.  
  1891. PROCEDURE MoveLines* (text: tText; down: BOOLEAN);
  1892. VAR
  1893.         last, first, line: tLine;
  1894.         selBeg, selEnd, temp: tPoint;
  1895.         step: INTEGER;
  1896.         frw: BOOLEAN;
  1897. BEGIN
  1898.         getSelect(text, selBeg, selEnd);
  1899.         IF ((selBeg.Y > 0) & ~down OR (selEnd.Y < text.count - 1) & down) THEN
  1900.                 modify(text);
  1901.                 step := ORD(down)*2 - 1;
  1902.                 IF selBeg.Y # selEnd.Y THEN
  1903.                         frw := (text.cursor.X = selEnd.X) & (text.cursor.Y = selEnd.Y);
  1904.  
  1905.                         IF down # frw THEN
  1906.                                 temp := text.cursor^;
  1907.                                 SetPos(text, 0, text.select.Y);
  1908.                                 setSelect(text);
  1909.                                 text.select^ := temp
  1910.                         END;
  1911.  
  1912.                         ASSERT(selBeg.Y < selEnd.Y);
  1913.  
  1914.                         first := getLine(text, selBeg.Y);
  1915.                         last := getLine(text, selEnd.Y);
  1916.  
  1917.                         line := first;
  1918.                         Lines.modify(line);
  1919.                         REPEAT
  1920.                                 NextLine(line);
  1921.                                 Lines.modify(line)
  1922.                         UNTIL line = last;
  1923.  
  1924.                         IF down THEN
  1925.                                 text.curLine := last.prev(tLine)
  1926.                         ELSE
  1927.                                 text.curLine := first.next(tLine)
  1928.                         END;
  1929.                         selBeg.X := 0;
  1930.                         selEnd.X := last.length;
  1931.                         IF down THEN
  1932.                                 last := last.next(tLine);
  1933.                                 List._delete(text, last);
  1934.                                 List._insert(text, first.prev, last)
  1935.                         ELSE
  1936.                                 first := first.prev(tLine);
  1937.                                 List._delete(text, first);
  1938.                                 List._insert(text, last, first)
  1939.                         END;
  1940.                         IF down THEN
  1941.                                 temp := selBeg;
  1942.                                 selBeg := selEnd;
  1943.                                 selEnd := temp;
  1944.                         END;
  1945.                         SetPos(text, selBeg.X, selBeg.Y + step);
  1946.                         setSelect(text);
  1947.                         text.select.X := selEnd.X;
  1948.                         text.select.Y := selEnd.Y + step
  1949.                 ELSE
  1950.                         first := getLine(text, selBeg.Y);
  1951.                         Lines.modify(first);
  1952.                         IF down THEN
  1953.                                 last := first.next(tLine)
  1954.                         ELSE
  1955.                                 last := first.prev.prev(tLine)
  1956.                         END;
  1957.                         List._delete(text, first);
  1958.                         List._insert(text, last, first);
  1959.                         INC(text.cursor.Y, step);
  1960.                         INC(text.select.Y, step);
  1961.                         SetPos(text, text.cursor.X, text.cursor.Y)
  1962.                 END
  1963.         END
  1964. END MoveLines;
  1965.  
  1966.  
  1967. PROCEDURE isWordChar (c: WCHAR): BOOLEAN;
  1968.     RETURN U.isLetter(c) OR U.isDigit(c) OR (c = "_")
  1969. END isWordChar;
  1970.  
  1971.  
  1972. PROCEDURE getSelectedText* (text: tText; VAR s: ARRAY OF WCHAR);
  1973. VAR
  1974.     n: INTEGER;
  1975.     selBeg, selEnd: tPoint;
  1976. BEGIN
  1977.         s[0] := 0X;
  1978.     IF selected(text) & (text.cursor.Y = text.select.Y) THEN
  1979.         getSelect(text, selBeg, selEnd);
  1980.         n := getString(text.curLine, selBeg.X, selEnd.X - selBeg.X, s)
  1981.     END
  1982. END getSelectedText;
  1983.  
  1984.  
  1985. PROCEDURE wordSel* (text: tText);
  1986. VAR
  1987.     n, i, x1, x2: INTEGER;
  1988.     selBeg, selEnd: tPoint;
  1989.     str: tString;
  1990.     curLine: tLine;
  1991. BEGIN
  1992.     curLine := text.curLine;
  1993.     IF selected(text) & (text.cursor.Y = text.select.Y) THEN
  1994.         getSelect(text, selBeg, selEnd);
  1995.         x1 := selBeg.X;
  1996.         x2 := selEnd.X;
  1997.         n := getString(curLine, x1, x2 - x1, str);
  1998.     ELSE
  1999.         str := ""
  2000.     END;
  2001.     IF str # "" THEN
  2002.         i := 0;
  2003.         WHILE (i < n) & isWordChar(str[i]) DO
  2004.             INC(i)
  2005.         END;
  2006.         IF (i # n) OR
  2007.             ((x1 > 0) & isWordChar(Lines.getChar(curLine, x1 - 1))) OR
  2008.             ((x2 < curLine.length) & isWordChar(Lines.getChar(curLine, x2))) THEN
  2009.             str := ""
  2010.         END
  2011.     END;
  2012.     IF text.searchText # str THEN
  2013.         text.smallMove := FALSE
  2014.     END;
  2015.     IF search(text, str, Lang.isCS(text.lang), TRUE) THEN END
  2016. END wordSel;
  2017.  
  2018.  
  2019. PROCEDURE getWordPos (line: tLine; pos: INTEGER): INTEGER;
  2020. VAR
  2021.         c: WCHAR;
  2022. BEGIN
  2023.         c := Lines.getChar(line, pos);
  2024.         IF isWordChar(c) THEN
  2025.                 WHILE (pos < line.length) & isWordChar(Lines.getChar(line, pos)) DO
  2026.                         INC(pos)
  2027.                 END
  2028.         ELSIF Lines.isSpace(c) THEN
  2029.                 WHILE (pos < line.length) & Lines.isSpace(Lines.getChar(line, pos)) DO
  2030.                         INC(pos)
  2031.                 END
  2032.         ELSE
  2033.                 WHILE (pos < line.length) & ~Lines.isSpace(Lines.getChar(line, pos)) & ~isWordChar(Lines.getChar(line, pos)) DO
  2034.                         INC(pos)
  2035.                 END
  2036.         END
  2037.         RETURN pos
  2038. END getWordPos;
  2039.  
  2040.  
  2041. PROCEDURE key* (text: tText; code: INTEGER; shift, ctrl: BOOLEAN);
  2042. VAR
  2043.         n, wPos: INTEGER;
  2044.         scrX, scrY: INTEGER;
  2045.         resSel: BOOLEAN;
  2046. BEGIN
  2047.         resSel := FALSE;
  2048.     IF shift THEN
  2049.         setSelect(text)
  2050.     ELSE
  2051.         IF (33 <= code) & (code <= 40) THEN
  2052.                 IF ~(((code = 38) OR (code = 40)) & ctrl) THEN
  2053.                         resSel := selected(text);
  2054.                     resetSelect(text)
  2055.             END
  2056.         END
  2057.     END;
  2058.  
  2059.     CASE code OF
  2060.     |33:
  2061.         IF ctrl THEN
  2062.             UpDown(text, text.scroll.Y - text.cursor.Y)
  2063.         ELSE
  2064.             text.scroll.Y := MAX(text.scroll.Y - textsize.Y, 0);
  2065.             UpDown(text, -textsize.Y)
  2066.         END
  2067.     |34:
  2068.         IF ctrl THEN
  2069.             UpDown(text, MIN(text.scroll.Y + textsize.Y - 1, text.count - 1) - text.cursor.Y)
  2070.         ELSE
  2071.             text.scroll.Y := MIN(text.scroll.Y + textsize.Y, text.count - 1);
  2072.             UpDown(text, textsize.Y)
  2073.         END
  2074.     |35:
  2075.         IF ctrl THEN
  2076.             SetPos(text, text.last(tLine).length, text.count - 1)
  2077.         ELSE
  2078.             SetPos(text, text.curLine.length, text.cursor.Y)
  2079.         END
  2080.     |36:
  2081.         IF ctrl THEN
  2082.             SetPos(text, 0, 0)
  2083.         ELSE
  2084.                 n := leadingSpaces(text.curLine);
  2085.                 IF text.cursor.X <= n THEN
  2086.                     n := 0
  2087.                 END;
  2088.                 SetPos(text, n, text.cursor.Y)
  2089.         END
  2090.     |37:
  2091.         IF (text.cursor.X = 0) & (text.curLine.prev # NIL) THEN
  2092.             SetPos(text, text.curLine.prev(tLine).length, text.cursor.Y - 1)
  2093.         ELSE
  2094.                 scrX := text.scroll.X;
  2095.                 scrY := text.scroll.Y;
  2096.                 IF ctrl THEN
  2097.                         wPos := 0;
  2098.                         REPEAT
  2099.                                 n := wPos;
  2100.                                 wPos := getWordPos(text.curLine, wPos)
  2101.                         UNTIL wPos >= text.cursor.X;
  2102.                                 move(text, n - text.cursor.X)
  2103.                 ELSE
  2104.                 move(text, -1)
  2105.             END;
  2106.             text.smallMove := (scrX = text.scroll.X) & (scrY = text.scroll.Y) & ~resSel
  2107.         END
  2108.     |38:
  2109.         IF ctrl THEN
  2110.             MoveLines(text, FALSE)
  2111.         ELSE
  2112.             UpDown(text, -1)
  2113.         END
  2114.     |39:
  2115.         IF (text.cursor.X = text.curLine.length) & (text.curLine.next # NIL) THEN
  2116.             SetPos(text, 0, text.cursor.Y + 1)
  2117.         ELSE
  2118.                 scrX := text.scroll.X;
  2119.                 scrY := text.scroll.Y;
  2120.                 IF ctrl THEN
  2121.                                 move(text, getWordPos(text.curLine, text.cursor.X) - text.cursor.X)
  2122.                 ELSE
  2123.                 move(text, 1)
  2124.                 END;
  2125.                 text.smallMove := (scrX = text.scroll.X) & (scrY = text.scroll.Y) & ~resSel
  2126.         END
  2127.     |40:
  2128.         IF ctrl THEN
  2129.             MoveLines(text, TRUE)
  2130.         ELSE
  2131.             UpDown(text, 1)
  2132.         END
  2133.     |46:
  2134.         IF ctrl THEN
  2135.             delLine(text)
  2136.         ELSE
  2137.                 IF ~selected(text) & (text.cursor.X < text.curLine.length) THEN
  2138.                         INC(text.smallChange)
  2139.                 END;
  2140.             delete(text);
  2141.             ShowCursor
  2142.         END
  2143.     |ORD("C"), ORD("X"):
  2144.         IF ctrl THEN
  2145.             IF selected(text) THEN
  2146.                 copy(text);
  2147.                 IF code = ORD("X") THEN
  2148.                         delSelect(text)
  2149.                 END
  2150.             END
  2151.         END
  2152.     |ORD("V"):
  2153.         IF ctrl THEN
  2154.             IF CB.available() THEN
  2155.                 paste(text)
  2156.             END
  2157.         END
  2158.     |ORD("A"):
  2159.         IF ctrl THEN
  2160.             text.select2.X := 0;
  2161.             text.select2.Y := 0;
  2162.             text.select := text.select2;
  2163.             SetPos(text, text.last(tLine).length, text.count - 1)
  2164.         END
  2165.     |ORD("L"), ORD("U"):
  2166.         IF ctrl THEN
  2167.                 IF selected(text) THEN
  2168.                 chCase(text, code = ORD("U"))
  2169.             ELSE
  2170.                 changeCase(text, code = ORD("U"))
  2171.             END;
  2172.             ShowCursor
  2173.         END
  2174.     |ORD("D"):
  2175.         IF ctrl THEN
  2176.             dupLine(text)
  2177.         END
  2178.     ELSE
  2179.     END
  2180. END key;
  2181.  
  2182.  
  2183. PROCEDURE mouse* (text: tText; x, y: INTEGER);
  2184. VAR
  2185.     cursorX: INTEGER;
  2186. BEGIN
  2187.     DEC(x, padding.left);
  2188.     DEC(y, padding.top);
  2189.     cursorX := (x*2) DIV charWidth;
  2190.     SetPos(text, cursorX DIV 2 + cursorX MOD 2 + text.scroll.X, y DIV charHeight + text.scroll.Y)
  2191. END mouse;
  2192.  
  2193.  
  2194. PROCEDURE selectWord* (text: tText);
  2195. VAR
  2196.     cursorX, x1, x2: INTEGER;
  2197.     line: tLine;
  2198. BEGIN
  2199.     resetSelect(text);
  2200.     cursorX := text.cursor.X;
  2201.     line := text.curLine;
  2202.     x1 := cursorX - 1;
  2203.     IF (cursorX < line.length) & isWordChar(Lines.getChar(line, cursorX)) THEN
  2204.         x2 := cursorX;
  2205.         WHILE (x2 < line.length) & isWordChar(Lines.getChar(line, x2)) DO
  2206.             INC(x2)
  2207.         END
  2208.     ELSE
  2209.         WHILE (x1 >= 0) & ~isWordChar(Lines.getChar(line, x1)) DO
  2210.             DEC(x1)
  2211.         END;
  2212.         x2 := x1 + 1
  2213.     END;
  2214.     WHILE (x1 >= 0) & isWordChar(Lines.getChar(line, x1)) DO
  2215.         DEC(x1)
  2216.     END;
  2217.     INC(x1);
  2218.     IF x1 < x2 THEN
  2219.         SetPos(text, x1, text.cursor.Y);
  2220.         setSelect(text);
  2221.         SetPos(text, x2, text.cursor.Y)
  2222.     END
  2223. END selectWord;
  2224.  
  2225.  
  2226. PROCEDURE cursor* (text: tText);
  2227. VAR
  2228.     x, y1, y2, scrollX, scrollY: INTEGER;
  2229.     cursor: pPoint;
  2230. BEGIN
  2231.         IF drawCursor THEN
  2232.             cursor := text.cursor;
  2233.             scrollX := text.scroll.X;
  2234.             scrollY := text.scroll.Y;
  2235.             IF ~((scrollY > cursor.Y) OR (scrollY + textsize.Y <= cursor.Y) OR
  2236.                (scrollX > cursor.X) OR (scrollX + textsize.X <= cursor.X)) THEN
  2237.                 x := (cursor.X - scrollX)*charWidth + padding.left;
  2238.                 y1 := (cursor.Y - scrollY)*charHeight + padding.top + (inter DIV 2 + 1);
  2239.                 y2 := y1 + charHeight - (inter + 2);
  2240.                 G.notVLine(canvas, x, y1, y2);
  2241.                 G.notVLine(canvas, x - 1, y1, y2)
  2242.             END
  2243.     END
  2244. END cursor;
  2245.  
  2246.  
  2247. PROCEDURE drawSelect (text: tText; line: tLine; selBeg, selEnd, y: INTEGER);
  2248. VAR
  2249.     Len, pos, x, firstCharIdx: INTEGER;
  2250. BEGIN
  2251.     firstCharIdx := MAX(text.scroll.X, selBeg);
  2252.     Len := MAX(MIN(line.length - firstCharIdx, selEnd - firstCharIdx), 0);
  2253.     Len := MIN(Len, textsize.X - pos + 1);
  2254.     SetColor(colors.seltext, colors.selback);
  2255.     pos := MAX((selBeg - text.scroll.X), 0);
  2256.     x := pos*charWidth + padding.left;
  2257.     G.SetColor(canvas, colors.selback);
  2258.     G.FillRect(canvas, x - 2, y - inter DIV 2, x + 1 + Len*charWidth, y - inter DIV 2 + charHeight);
  2259.     G.TextOut(canvas, pos*charWidth + padding.left, y, Lines.getPChar(line, firstCharIdx), Len, colors.seltext)
  2260. END drawSelect;
  2261.  
  2262.  
  2263. PROCEDURE mark (line: tLine; y: INTEGER);
  2264. VAR
  2265.     color, i: INTEGER;
  2266. BEGIN
  2267.     IF line.modified THEN
  2268.         color := colors.modified
  2269.     ELSIF line.saved THEN
  2270.         color := colors.saved
  2271.     ELSE
  2272.         color := colors.back
  2273.     END;
  2274.     G.SetColor(canvas, color);
  2275.  
  2276.     FOR i := 3 TO mark_width + 2 DO
  2277.         G.VLine(canvas, padding.left - i, y, y + charHeight)
  2278.     END
  2279. END mark;
  2280.  
  2281.  
  2282. PROCEDURE setPadding (left, top: INTEGER);
  2283. BEGIN
  2284.     padding.left := left;
  2285.     padding.top := top;
  2286.     textsize.X := (size.X - padding.left) DIV charWidth;
  2287.     textsize.Y := (size.Y - padding.top) DIV charHeight;
  2288. END setPadding;
  2289.  
  2290.  
  2291. PROCEDURE draw* (text: tText);
  2292. VAR
  2293.     y, n, cnt, i, x: INTEGER;
  2294.     line, firstLine, lastLine: tLine;
  2295.     selBeg, selEnd: tPoint;
  2296.     s: ARRAY 12 OF WCHAR;
  2297.     backColor, numWidth, xNum, wNum: INTEGER;
  2298.     p: Search.tPos;
  2299.     guard: tGuard;
  2300. BEGIN
  2301.     IF text.comments THEN
  2302.         Comments(text)
  2303.     END;
  2304.     IF text.search & search(text, text.searchText, text.cs, text.whole) THEN END;
  2305.     IF text.guard THEN
  2306.         NEW(guard);
  2307.         List.append(ChangeLog.CL.Log, guard);
  2308.         guard.saved := ChangeLog.isFirstGuard(guard);
  2309.         text.edition := guard;
  2310.         text.guard := FALSE
  2311.     ELSE
  2312.         guard := text.edition
  2313.     END;
  2314.  
  2315.     guard.cursor := text.cursor^;
  2316.     guard.select2 := text.select2^;
  2317.     guard.scroll := text.scroll;
  2318.     guard.CurX := text.CurX;
  2319.     guard.selected := text.select = text.select2;
  2320.  
  2321.     G.SetColor(canvas, colors.back);
  2322.     IF ~text.smallMove THEN
  2323.         G.clear(canvas)
  2324.     END;
  2325.     wNum := charWidth;
  2326.     IF lineNumbers THEN
  2327.         numWidth := U.lg10(text.count) + 2;
  2328.         xNum := numWidth*wNum - wNum DIV 2;
  2329.         setPadding(numWidth*wNum + pad_left, padding.top);
  2330.     ELSE
  2331.         setPadding(pad_left + wNum*2, padding.top)
  2332.     END;
  2333.     getSelect(text, selBeg, selEnd);
  2334.     y := padding.top + inter DIV 2;
  2335.     n := text.scroll.Y;
  2336.     firstLine := getLine2(text, n);
  2337.     IF text.smallMove THEN
  2338.         line := text.curLine;
  2339.         cnt := textsize.Y - 1;
  2340.         y := y + charHeight*(text.cursor.Y - text.scroll.Y);
  2341.         G.SetColor(canvas, colors.back);
  2342.         G.FillRect(canvas, padding.left - 2, y - inter DIV 2, size.X - 1, y - inter DIV 2 + charHeight);
  2343.         n := text.cursor.Y
  2344.     ELSE
  2345.             line := firstLine;
  2346.         cnt := 0
  2347.     END;
  2348.  
  2349.     WHILE (line # NIL) & (cnt < textsize.Y) DO
  2350.         backColor := colors.back;
  2351.         IF (line = text.curLine) & ~selected(text) THEN
  2352.             G.SetColor(canvas, colors.curline);
  2353.             G.FillRect(canvas, padding.left - 2, y - inter DIV 2, size.X - 1, y - inter DIV 2 + charHeight);
  2354.             backColor := colors.curline
  2355.         END;
  2356.         SetColor(colors.text, backColor);
  2357.                 IF (selBeg.Y < n) & (n < selEnd.Y) THEN
  2358.                         drawSelect(text, line, 0, line.length, y)
  2359.                 ELSE
  2360.                         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);
  2361.                         IF text.lang # Lang.langText THEN
  2362.                                 parse(text, line, y, backColor, text.lang)
  2363.                         END
  2364.                 END;
  2365.                 IF (selBeg.Y = n) & (selEnd.Y = n) & (selBeg.X # selEnd.X) THEN
  2366.             drawSelect(text, line, selBeg.X, selEnd.X, y)
  2367.         ELSIF (selBeg.Y = n) & (selEnd.Y # n) THEN
  2368.             drawSelect(text, line, selBeg.X, line.length, y)
  2369.         ELSIF (selBeg.Y # n) & (selEnd.Y = n) THEN
  2370.             drawSelect(text, line, 0, selEnd.X, y)
  2371.         END;
  2372.         mark(line, y - inter DIV 2);
  2373.         NextLine(line);
  2374.         INC(y, charHeight);
  2375.         INC(n);
  2376.         INC(cnt)
  2377.     END;
  2378.  
  2379.     G.SetColor(canvas, colors.numback);
  2380.     G.FillRect(canvas, 0, 0, padding.left - pad_left (*+ 1*), size.Y - 1);
  2381.     line := firstLine;
  2382.     SetColor(colors.numtext, colors.numback);
  2383.     y := padding.top + inter DIV 2;
  2384.     n := MIN(text.scroll.Y + textsize.Y, text.count);
  2385.     FOR i := text.scroll.Y + 1 TO n DO
  2386.         IF lineNumbers THEN
  2387.             IF (i MOD 10 = 0) OR (i - 1 = text.cursor.Y) OR line.label THEN
  2388.                 U.int2str(i, s);
  2389.                 G.TextOut2(canvas, (numWidth - U.lg10(i) - 1)*wNum - wNum DIV 2, y, s, LENGTH(s))
  2390.             ELSE
  2391.                 G.SetColor(canvas, colors.numtext);
  2392.                 G.HLine(canvas, y - inter DIV 2 + charHeight DIV 2, xNum - wNum DIV (1 + ORD(i MOD 5 # 0)), xNum)
  2393.             END
  2394.         END;
  2395.         IF line.label THEN
  2396.             FOR x := wNum DIV 2 TO (padding.left - pad_left) - wNum DIV 2 DO
  2397.                 G.notVLine(canvas, x, y, y + charHeight - inter);
  2398.                 G.xorVLine(canvas, x, y, y + charHeight - inter)
  2399.             END
  2400.         END;
  2401.         NextLine(line);
  2402.         INC(y, charHeight)
  2403.     END;
  2404.  
  2405.     IF text.searchText # "" THEN
  2406.         IF text.smallMove THEN
  2407.                 firstLine := text.curLine;
  2408.                 lastLine := firstLine
  2409.         ELSE
  2410.             lastLine := getLine2(text, MIN(text.scroll.Y + textsize.Y, text.count) - 1)
  2411.         END;
  2412.         p := text.foundList.first(Search.tPos);
  2413.         WHILE p # NIL DO
  2414.                 y := padding.top + inter DIV 2;
  2415.                 IF text.smallMove THEN
  2416.                         y := y + charHeight*(text.cursor.Y - text.scroll.Y)
  2417.             END;
  2418.             IF (firstLine.pos <= p.pos) & (p.pos <= lastLine.pos + lastLine.length) THEN
  2419.                 line := firstLine;
  2420.                 WHILE (line.pos <= p.pos) & (line # lastLine) DO
  2421.                     NextLine(line);
  2422.                     INC(y, charHeight)
  2423.                 END;
  2424.                 IF (line # lastLine) & (line # firstLine) OR (line = lastLine) & (line.pos > p.pos) THEN
  2425.                     PrevLine(line);
  2426.                     DEC(y, charHeight)
  2427.                 END;
  2428.                 x := (p.pos - line.pos - text.scroll.X)*charWidth + padding.left;
  2429.                 n := LENGTH(text.searchText)*charWidth;
  2430.                 WHILE n > 0 DO
  2431.                     IF x >= padding.left THEN
  2432.                         G.notVLine(canvas, x, y, y + charHeight - inter)
  2433.                     END;
  2434.                     INC(x);
  2435.                     DEC(n)
  2436.                 END
  2437.             END;
  2438.             p := p.next(Search.tPos)
  2439.         END
  2440.     END;
  2441.  
  2442.     text.smallMove := FALSE;
  2443.  
  2444.     IF text.foundSel > 0 THEN
  2445.         x := (text.cursor.X - text.scroll.X)*charWidth + padding.left;
  2446.         y := (text.cursor.Y - text.scroll.Y)*charHeight + padding.top + inter DIV 2;
  2447.         n := text.foundSel*charWidth;
  2448.         WHILE n > 0 DO
  2449.             IF x >= padding.left THEN
  2450.                 G.xorVLine(canvas, x, y, y + charHeight - inter)
  2451.             END;
  2452.             INC(x);
  2453.             DEC(n)
  2454.         END
  2455.     END;
  2456.         cursor(text)
  2457. END draw;
  2458.  
  2459.  
  2460. PROCEDURE switch* (text: tText);
  2461. BEGIN
  2462.     ChangeLog.set(text.chLog);
  2463.     Lines.setMaxLength(text.maxLength);
  2464.     Lang.setCurLang(text.lang)
  2465. END switch;
  2466.  
  2467.  
  2468. PROCEDURE create (fileName: RW.tFileName): tText;
  2469. VAR
  2470.     text: tText;
  2471. BEGIN
  2472.     NEW(text);
  2473.     text.maxLength := 64;
  2474.     text.chLog := ChangeLog.create(text.maxLength);
  2475.     NEW(text.cursor);
  2476.     NEW(text.select2);
  2477.     text.cursor.X := 0;
  2478.     text.cursor.Y := 0;
  2479.     resetSelect(text);
  2480.     text.scroll.X := 0;
  2481.     text.scroll.Y := 0;
  2482.     setPadding(padding.left, padding.top);
  2483.     text.curLine := NIL;
  2484.     text.modified := FALSE;
  2485.     text.smallMove := FALSE;
  2486.     text.comments := TRUE;
  2487.     text.search := TRUE;
  2488.     text.cs := FALSE;
  2489.     text.whole := FALSE;
  2490.     text.guard := TRUE;
  2491.     text.edition := NIL;
  2492.     text.foundList := List.create(NIL);
  2493.     text.searchText := "";
  2494.     text.foundSel := 0;
  2495.     text.CurX := -1;
  2496.     text.smallChange := 0;
  2497.     text.lang := Lang.langText;
  2498.     text.LinesVector := NIL;
  2499.     Lang.setCurLang(Lang.langText);
  2500.     setName(text, fileName);
  2501.     ASSERT(text = List.create(text))
  2502.     RETURN text
  2503. END create;
  2504.  
  2505.  
  2506. PROCEDURE setColors* (text, back, seltext, selback, modified, saved, curline, numtext, numback,
  2507.                         comment, string, escape, num, delim, key1, key2, key3: INTEGER);
  2508. BEGIN
  2509.     colors.text := text;
  2510.     colors.back := back;
  2511.     colors.seltext := seltext;
  2512.     colors.selback := selback;
  2513.     colors.modified := modified;
  2514.     colors.saved := saved;
  2515.     colors.curline := curline;
  2516.     colors.numtext := numtext;
  2517.     colors.numback := numback;
  2518.     colors.comment := comment;
  2519.     colors.string  := string;
  2520.     colors.escape  := escape;
  2521.     colors.num := num;
  2522.     colors.delim := delim;
  2523.     colors.key1 := key1;
  2524.     colors.key2 := key2;
  2525.     colors.key3 := key3;
  2526. END setColors;
  2527.  
  2528.  
  2529. PROCEDURE setCanvas* (_canvas: G.tCanvas);
  2530. BEGIN
  2531.     canvas := _canvas;
  2532.     charWidth := _canvas.font.width;
  2533.     charHeight := _canvas.font.height + inter
  2534. END setCanvas;
  2535.  
  2536.  
  2537. PROCEDURE resize* (width, height: INTEGER);
  2538. BEGIN
  2539.     size.X := width;
  2540.     size.Y := height;
  2541.     setPadding(padding.left, padding.top)
  2542. END resize;
  2543.  
  2544.  
  2545. PROCEDURE destroy* (VAR text: tText);
  2546. BEGIN
  2547.     IF search(text, "", FALSE, FALSE) THEN END;
  2548.     ChangeLog.destroy(text.chLog);
  2549.     Lines.destroyVector(text.LinesVector);
  2550.     DISPOSE(text.foundList);
  2551.     DISPOSE(text.cursor);
  2552.     DISPOSE(text.select2);
  2553.     DISPOSE(text)
  2554. END destroy;
  2555.  
  2556.  
  2557. PROCEDURE open* (name: RW.tFileName; ptr, size: INTEGER; VAR errno: INTEGER): tText;
  2558. VAR
  2559.     text: tText;
  2560.     file: RW.tInput;
  2561.     n, enc, eol: INTEGER;
  2562.     _eol: BOOLEAN;
  2563.     line: tLine;
  2564. BEGIN
  2565.     errno := 0;
  2566.     text := create(name);
  2567.     IF text # NIL THEN
  2568.         IF ptr = 0 THEN
  2569.                 file := RW.loadFromFile(name, enc, eol)
  2570.         ELSE
  2571.                 file := RW.loadFromMem(ptr, size, enc, eol)
  2572.         END;
  2573.         IF file = NIL THEN
  2574.                 destroy(text)
  2575.         END
  2576.     ELSE
  2577.         file := NIL
  2578.     END;
  2579.     IF file # NIL THEN
  2580.         ChangeLog.changeInt(text.enc, enc);
  2581.         ChangeLog.changeInt(text.eol, eol);
  2582.         text.enc := enc;
  2583.         text.eol := eol;
  2584.         line := Lines.create(FALSE);
  2585.         List._append(text, line);
  2586.         REPEAT
  2587.             n := RW.getString(file, line, Lines.tabs, _eol);
  2588.             IF _eol THEN
  2589.                 line := Lines.create(FALSE);
  2590.                 List._append(text, line)
  2591.             END
  2592.         UNTIL ~_eol;
  2593.         RW.destroy(file);
  2594.         text.curLine := text.first(tLine);
  2595.         SetPos(text, 0, 0);
  2596.         resetSelect(text);
  2597.         Comments(text)
  2598.     ELSE
  2599.         errno := 1
  2600.     END
  2601.     RETURN text
  2602. END open;
  2603.  
  2604.  
  2605. PROCEDURE findNext* (text: tText; prev: BOOLEAN): BOOLEAN;
  2606. VAR
  2607.     cursorPos, x, y, X, Y, Len: INTEGER;
  2608.     p: Search.tPos;
  2609.     line: tLine;
  2610.     res: BOOLEAN;
  2611. BEGIN
  2612.     X := text.cursor.X;
  2613.     Y := text.cursor.Y;
  2614.     text.cursor.X := MIN(text.cursor.X, text.curLine.length);
  2615.     cursorPos := text.curLine.pos + text.cursor.X - ORD(prev) - ORD(~prev & (text.foundSel = 0));
  2616.     p := text.foundList.first(Search.tPos);
  2617.     WHILE (p # NIL) & (p.pos <= cursorPos) DO
  2618.         p := p.next(Search.tPos)
  2619.     END;
  2620.     IF prev THEN
  2621.         IF p = NIL THEN
  2622.             p := text.foundList.last(Search.tPos)
  2623.         ELSE
  2624.             p := p.prev(Search.tPos)
  2625.         END
  2626.     END;
  2627.     res := p # NIL;
  2628.     IF res THEN
  2629.         y := 0;
  2630.         line := text.first(tLine);
  2631.         WHILE (line.pos <= p.pos) & (line.next # NIL) DO
  2632.             NextLine(line);
  2633.             INC(y)
  2634.         END;
  2635.         IF (line.next # NIL) OR (line.pos > p.pos) THEN
  2636.             PrevLine(line);
  2637.             DEC(y)
  2638.         END;
  2639.         resetSelect(text);
  2640.         searchScroll(text, y);
  2641.         x := p.pos - line.pos;
  2642.         Len := LENGTH(text.searchText);
  2643.         IF x + Len > text.scroll.X + textsize.X THEN
  2644.             text.scroll.X := MAX(x + Len - textsize.X + 3, 0)
  2645.         ELSIF x < text.scroll.X THEN
  2646.             text.scroll.X := MAX(x - 3, 0)
  2647.         END;
  2648.         SetPos(text, x, y);
  2649.         text.foundSel := Len
  2650.     ELSE
  2651.         SetPos(text, X, Y)
  2652.     END
  2653.     RETURN res
  2654. END findNext;
  2655.  
  2656.  
  2657. PROCEDURE replace* (text: tText; s: ARRAY OF WCHAR; n: INTEGER);
  2658. VAR
  2659.     line: tLine;
  2660.     sLen, i: INTEGER;
  2661. BEGIN
  2662.     IF text.foundSel > 0 THEN
  2663.         line := text.curLine;
  2664.         sLen := LENGTH(s);
  2665.         i := text.cursor.X;
  2666.         IF sLen > n THEN
  2667.             Lines.insert3(line, i, sLen - n)
  2668.         ELSIF n > sLen THEN
  2669.             Lines.delCharN(line, i, n - sLen)
  2670.         ELSE (* n = sLen *)
  2671.                 Lines.copy(line)
  2672.         END;
  2673.         SetPos(text, i + sLen, text.cursor.Y);
  2674.             WHILE sLen > 0 DO
  2675.                 DEC(sLen);
  2676.                 Lines.setChar(line, i + sLen, s[sLen])
  2677.             END;
  2678.         resetSelect(text);
  2679.         Lines.modify(line);
  2680.         modify(text)
  2681.     END
  2682. END replace;
  2683.  
  2684.  
  2685. PROCEDURE replaceAll* (text: tText; s: ARRAY OF WCHAR; n: INTEGER): INTEGER;
  2686. VAR
  2687.     p: Search.tPos;
  2688.     line: tLine;
  2689.     y, k, d, pos, y0: INTEGER;
  2690. BEGIN
  2691.     resetSelect(text);
  2692.     SetPos(text, 0, 0);
  2693.     line := text.first(tLine);
  2694.     y := 0;
  2695.     y0 := -1;
  2696.     k := 0;
  2697.     d := LENGTH(s) - n;
  2698.     p := text.foundList.first(Search.tPos);
  2699.     WHILE p # NIL DO
  2700.         pos := p.pos;
  2701.         WHILE (line.pos <= pos) & (line.next # NIL) DO
  2702.             NextLine(line);
  2703.             INC(y)
  2704.         END;
  2705.         IF (line.next # NIL) OR (line.pos > pos) THEN
  2706.             PrevLine(line);
  2707.             DEC(y)
  2708.         END;
  2709.         IF y = y0 THEN
  2710.             INC(k, d)
  2711.         ELSE
  2712.             k := 0;
  2713.             y0 := y
  2714.         END;
  2715.         SetPos(text, pos - line.pos + k, y);
  2716.         text.foundSel := n;
  2717.         replace(text, s, n);
  2718.         p := p.next(Search.tPos)
  2719.     END
  2720.     RETURN text.foundList.count
  2721. END replaceAll;
  2722.  
  2723.  
  2724. PROCEDURE conv (VAR c: WCHAR; cp: E.CP; enc: INTEGER): BOOLEAN;
  2725. VAR
  2726.         code: INTEGER;
  2727.         res: BOOLEAN;
  2728. BEGIN
  2729.         res := FALSE;
  2730.         IF (c # 0X) & (c # Lines.NUL) & (c # Lines.TAB1) THEN
  2731.                 code := E.UNI[ORD(c), enc];
  2732.                 IF (0 <= code) & (code <= 255) THEN
  2733.                         code := cp[code]
  2734.                 ELSE
  2735.                         code := ORD(c)
  2736.                 END;
  2737.                 res := WCHR(code) # c;
  2738.                 c := WCHR(code)
  2739.         END
  2740.         RETURN res
  2741. END conv;
  2742.  
  2743.  
  2744. PROCEDURE conv1251to866 (VAR c: WCHAR): BOOLEAN;
  2745.         RETURN conv(c, E.cp866, E.W1251)
  2746. END conv1251to866;
  2747.  
  2748.  
  2749. PROCEDURE conv866to1251 (VAR c: WCHAR): BOOLEAN;
  2750.         RETURN conv(c, E.cp1251, E.CP866)
  2751. END conv866to1251;
  2752.  
  2753.  
  2754. PROCEDURE convert* (text: tText; cp: E.CP; enc: INTEGER);
  2755. VAR
  2756.         line: tLine;
  2757.         func: Lines.fConvert;
  2758.         modified: BOOLEAN;
  2759. BEGIN
  2760.         modified := FALSE;
  2761.         line := text.first(tLine);
  2762.         IF enc = E.CP866 THEN
  2763.                 func := conv866to1251
  2764.         ELSIF enc = E.W1251 THEN
  2765.                 func := conv1251to866
  2766.         ELSE
  2767.                 line := NIL
  2768.         END;
  2769.  
  2770.         WHILE line # NIL DO
  2771.                 IF Lines.convert(line, 0, line.length - 1, func) THEN
  2772.                         modified := TRUE
  2773.                 END;
  2774.                 NextLine(line)
  2775.         END;
  2776.  
  2777.         IF modified THEN
  2778.                 modify(text)
  2779.         END
  2780. END convert;
  2781.  
  2782.  
  2783. PROCEDURE New* (): tText;
  2784. VAR
  2785.     text: tText;
  2786. BEGIN
  2787.     text := create("");
  2788.     List._append(text, Lines.create(FALSE));
  2789.     text.curLine := text.first(tLine);
  2790.     ChangeLog.changeInt(text.enc, E.CP866);
  2791.     ChangeLog.changeInt(text.eol, E.EOL_CRLF);
  2792.     text.enc := E.CP866;
  2793.     text.eol := E.EOL_CRLF;
  2794.     SetPos(text, 0, 0);
  2795.     resetSelect(text)
  2796.     RETURN text
  2797. END New;
  2798.  
  2799.  
  2800. PROCEDURE init* (pShowCursor: tProcedure; _lineNumbers, _autoIndents, _autoBrackets, _trimSpace: BOOLEAN);
  2801. BEGIN
  2802.     ShowCursor := pShowCursor;
  2803.     pdelete := delete;
  2804.     drawCursor := TRUE;
  2805.     lineNumbers := _lineNumbers;
  2806.     autoIndents := _autoIndents;
  2807.     autoBrackets := _autoBrackets;
  2808.     trimSpace := _trimSpace;
  2809.     padding.left := pad_left;
  2810.     padding.top := pad_top;
  2811. END init;
  2812.  
  2813.  
  2814. END Text.