ecere/gui/EditBox: Clearing Undo buffer on setting contents
[sdk] / ecere / src / gui / controls / EditBox.ec
index 5755d61..05816d5 100644 (file)
@@ -11,6 +11,16 @@ import "FindDialog"
 import "GoToDialog"
 import "Array"
 
+char * strchrmax(const char * s, int c, int max)
+{
+   int i;
+   char ch;
+   for(i = 0; i < max && (ch = s[i]); i++)
+      if(ch == c)
+         return (char *)s + i;
+   return null;
+}
+
 public class SyntaxColorScheme
 {
 public:
@@ -288,21 +298,33 @@ public:
       else
          delete action;
    }
+
+   void Clear()
+   {
+      actions.Free();
+      actions.size = 8;
+      count = 0;
+      curAction = 0;
+      firstEvent = true;
+   }
 };
 
 static class AddCharAction : UndoAction
 {
    int y, x;
    unichar ch;
-   int addedSpaces, addedTabs;
+   int addedSpaces, addedTabs, xAdjustment;
    type = class(AddCharAction);
 
    void Undo(EditBox editBox)
    {
-      editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
+      editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1) - xAdjustment);
       editBox.BackSpace();
       if(addedTabs || addedSpaces)
-         editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
+      {
+         editBox.DelCh(editBox.line, y, x - xAdjustment - (addedSpaces + addedTabs), editBox.line, y, x - xAdjustment, false);
+         editBox.GoToPosition(editBox.line, y, x);
+      }
       editBox.UpdateDirty();
    }
 
@@ -324,7 +346,7 @@ static class AddTextAction : UndoAction
 {
    int y1, x1, y2, x2;
    char * string;
-   int addedSpaces, addedTabs;
+   int addedSpaces, addedTabs, xAdjustment;
    type = class(AddTextAction);
 
 #ifdef _DEBUG
@@ -439,7 +461,7 @@ static class ReplaceTextAction : UndoAction
    char * oldString;
    char * newString;
    bool placeAfter;
-   int addedSpaces, addedTabs;
+   int addedSpaces, addedTabs, xAdjustment;
 
    type = class(ReplaceTextAction);
 
@@ -528,7 +550,7 @@ public class EditLine : struct
    int length;
    EditBox editBox;
 public:
-   property char * text
+   property const char * text
    {
       set
       {
@@ -658,7 +680,7 @@ public struct BufferLocation
 
 public enum EditBoxFindResult { notFound, found, wrapped };
 
-static char * keyWords1[] =
+static const char * keyWords1[] =
 {
    // C
    "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
@@ -696,12 +718,14 @@ static char * keyWords1[] =
    null
 };
 
-static char * keyWords2[] =
+static const char * keyWords2[] =
 {
-   "defined", "warning", null
+   "defined", "warning",
+   "include", "pragma", "elif", "ifdef", "ifndef", "endif", "undef", "line",
+   null
 };
 
-static char ** keyWords[] = { keyWords1, keyWords2 };
+static const char ** keyWords[] = { keyWords1, keyWords2 };
 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
 //static int * keyLen[NUM_KEYWORD_GROUPS];
 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
@@ -794,13 +818,15 @@ public:
    property EditLine firstLine { get { return lines.first; } };      // Change these to a List<EditLine>... (this.lines[10].text)
    property EditLine lastLine  { get { return lines.last; } };
    property EditLine line { get { return this.line; } }; // TODO: Add Set   this.line = this.lines[10]
-   property char * contents
+   property const char * contents
    {
       property_category $"Data"
       set
       {
          if(this)
          {
+            undoBuffer.Clear();
+
             undoBuffer.dontRecord++;
             Deselect();
             DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
@@ -886,7 +912,7 @@ public:
       return null;
    }
 
-   void SetLineText(char * text)
+   void SetLineText(const char * text)
    {
       if(this)
       {
@@ -963,7 +989,7 @@ private:
 
    bool modified;
 
-   void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
+   void (* FontExtent)(Display display, Font font, const char * text, int len, int * width, int * height, int prevGlyph, int * rPrevGlyph, int * overHang);
 
    Color backColor;
    bool rightButtonDown;
@@ -1130,7 +1156,7 @@ private:
             void NotifyDestroyed(Window window, DialogResult result)
             {
                ReplaceDialog dialog = (ReplaceDialog)window;
-               char * replace = dialog.replaceString;
+               const char * replace = dialog.replaceString;
                if(replace)
                   strcpy(replaceString, replace);
                strcpy(searchString, dialog.searchString);
@@ -1196,9 +1222,9 @@ private:
          colorScheme.keywordColors = [ blue, blue ];
       }
 
-      FontExtent = Display::FontExtent;
+      FontExtent = Display::FontExtent2;
       font = fontObject;
-      lines.offset = (uint)&((EditLine)0).prev;
+      lines.offset = (uint)(uintptr)&((EditLine)0).prev;
 
       style = EditBoxBits { hScroll = true };
 
@@ -1252,7 +1278,7 @@ private:
       lines.Free(EditLine::Free);
    }
 
-   void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, bool drawSpaces, Box box)
+   void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int * previousGlyph, int * oh, int numSpaces, bool drawSpaces, Box box)
    {
       int count = wc - *renderStart;
       if(count)
@@ -1263,18 +1289,24 @@ private:
 
             if(!numSpaces)
             {
+               int coh;
                //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
                surface.TextFont(font);
-               surface.TextExtent(line.buffer + *renderStart, count, &w, null);
-               if(*x + w + XOFFSET > 0)
-                  surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
+               surface.TextExtent2(line.buffer + *renderStart, count, &w, null, *previousGlyph, null, &coh);
+
+               if(*x + w + coh + XOFFSET > 0)
+               {
+                  surface.WriteText2(XOFFSET + *x,y, line.buffer + *renderStart, count, *previousGlyph, previousGlyph);
+                  *oh = coh;
+                  //surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
+               }
                *x += w;
             }
             else
             {
                w = numSpaces; // * space.w;
-               if(*x + w + XOFFSET > 0 && (drawSpaces || surface.GetTextOpacity()))
-                  surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
+               if(*x + w + XOFFSET > 0 && (drawSpaces))
+                  surface.Area(XOFFSET + *x /*- 1*/ + *oh, y, XOFFSET + *x + w, y + space.h-1);
                   // WHATS UP WITH THIS...  surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
                *x += w;
             }
@@ -1283,8 +1315,8 @@ private:
       }
    }
 
-   int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
-                   Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
+   bool CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
+                    Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, int *overwrite)
    {
       bool flush = false;
 
@@ -1308,7 +1340,7 @@ private:
          if((style.stuckCaret && wc == line.count && !line.next) ||
             (!mouseMove && line == this.line && wc == editX))
          {
-            *overwrite = true;
+            *overwrite = 1;
             flush = true;
          }
       }
@@ -1463,6 +1495,25 @@ private:
       }
    }*/
 
+   static inline int countTabsExtraSpaces(char * buffer, int tabSize, int start, int end)
+   {
+      // TODO: Handle tabs position properly with UTF-8 (and everywhere else)
+      int p = 0, i, extra = 0;
+      for(i = 0; i < end; i++)
+      {
+         if(buffer[i] == '\t')
+         {
+            int t = tabSize - (p % tabSize);
+            p += t;
+            if(i >= start)
+               extra += t-1;
+         }
+         else
+            p++;
+      }
+      return extra;
+   }
+
    void OnRedraw(Surface surface)
    {
       EditLine line;
@@ -1480,9 +1531,9 @@ private:
       bool opacity;
 
       // Overwrite Caret Stuff
-      bool overWrite = false;
-      int overWriteX, overWriteY;
-      byte overWriteCh;
+      int overWrite = 0;
+      int overWriteX = 0, overWriteY = 0;
+      char overWriteCh;
 
       // ****** SYNTAX STATES ******
       bool inMultiLineComment = style.inMultiLineComment;
@@ -1497,6 +1548,13 @@ private:
       bool wasInMultiLine = style.wasInMultiLine;
       // ****** ************* ******
 
+      // For drawing selection background
+      EditLine selStartLine = this.y < this.selY ? this.line : this.selLine;
+      EditLine selEndLine   = this.y < this.selY ? this.selLine : this.line;
+      int selStartX = this.y < this.selY || (this.y == this.selY && this.x < this.selX) ? this.x : this.selX;
+      int selEndX   = this.y > this.selY || (this.y == this.selY && this.x > this.selX) ? this.x : this.selX;
+      ///////////////////////////////////
+
       if(!isEnabled)
          defaultTextColor = Color { 85, 85, 85 };
       textColor = defaultTextColor;
@@ -1547,7 +1605,7 @@ private:
    */
       surface.SetForeground(foreground);
       surface.SetBackground(background);
-      surface.TextOpacity(opacity);
+      surface.TextOpacity(false);
 
       surface.GetBox(box);
 
@@ -1559,7 +1617,7 @@ private:
          int start = 0;
          Color newTextColor = textColor = defaultTextColor;
          bool lineComplete = false;
-
+         int overHang = 0;
 
          // ****** SYNTAX HIGHLIGHTING ******
          bool lastWasStar = false;
@@ -1586,6 +1644,64 @@ private:
          }
    */
 
+         // Draw Selection Background all at once here
+         if(selected || line == this.line || line == this.selLine)
+         {
+            int sx = XOFFSET + x, sy = y;
+            int tw, th;
+            int oh = 0;
+            char * buffer = line.buffer;
+            if(line != this.line && line != this.selLine)
+            {
+               if(style.freeCaret)
+               {
+                  tw = clientSize.w - sx;
+                  th = space.h;
+               }
+               else
+               {
+                  surface.TextExtent2(buffer, line.count, &tw, &th, 0, null, &oh);
+                  tw += countTabsExtraSpaces(buffer, tabSize, 0, line.count) * space.w;
+               }
+            }
+            else if(line == selStartLine)
+            {
+               int prevGlyph = 0;
+               int start = Min(line.count, selStartX);
+               int end   = Min(line.count, selEndX);
+               surface.TextExtent2(buffer, start, &tw, &th, 0, &prevGlyph, null);
+               sx += tw;
+               sx += countTabsExtraSpaces(buffer, tabSize, 0, start) * space.w;
+               if(selStartX > start) sx += space.w * (selStartX - start);
+               if(style.freeCaret && line != selEndLine)
+               {
+                  tw = clientSize.w - sx;
+                  th = space.h;
+               }
+               else if(line != selEndLine)
+               {
+                  surface.TextExtent2(buffer + start, line.count - start, &tw, &th, prevGlyph, null, &oh);
+                  tw += countTabsExtraSpaces(buffer, tabSize, start, line.count) * space.w;
+               }
+               else
+               {
+                  surface.TextExtent2(buffer + start, end - start, &tw, &th, prevGlyph, null, &oh);
+                  tw += countTabsExtraSpaces(buffer, tabSize, start, end) * space.w;
+                  end = Max(end, selStartX);
+                  if(selEndX > end) { tw += space.w * (selEndX - end); th = space.h; }
+               }
+            }
+            else if(line == selEndLine)
+            {
+               int end = Min(line.count, selEndX);
+               surface.TextExtent2(buffer, end, &tw, &th, 0, null, &oh);
+               tw += countTabsExtraSpaces(buffer, tabSize, 0, end) * space.w;
+               if(selEndX - end) { tw += space.w * (selEndX - end); th = space.h; }
+            }
+            tw += oh;
+            surface.Area(sx, sy, sx + tw-1, sy + th-1);
+         }
+
          if(line == this.selLine && line == this.line)
          {
             end = Max(line.count, this.x);
@@ -1775,26 +1891,29 @@ private:
                            if(!wasEscaped)
                               escaped = true;
                         }
-                        else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
+                        else if(x < box.right && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
                         {
-                           char * dot = strchr(word, '.');
-                           bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
-                           char * exponent;
-                           bool isReal;
+                           char * dot = word[wordLen] == '.' ? word + wordLen : (word[0] == '.' && (word == line.buffer || word[-1] == '-' || isspace(word[-1])) ? word : null);
+                           bool isReal = dot != null;
                            char * s = null;
-                           if(isHex)
-                           {
-                              exponent = strchr(word, 'p');
-                              if(!exponent) exponent = strchr(word, 'P');
-                           }
+                           if(dot)
+                              isReal = true;
                            else
                            {
-                              exponent = strchr(word, 'e');
-                              if(!exponent) exponent = strchr(word, 'E');
+                              char * exponent;
+                              bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
+                              if(isHex)
+                              {
+                                 exponent = strchrmax(word, 'p', wordLen);
+                                 if(!exponent) exponent = strchrmax(word, 'P', wordLen);
+                              }
+                              else
+                              {
+                                 exponent = strchrmax(word, 'e', wordLen);
+                                 if(!exponent) exponent = strchrmax(word, 'E', wordLen);
+                              }
+                              isReal = exponent != null;
                            }
-                           if(exponent && exponent > word + wordLen) exponent = null;
-                           if(dot && dot > word + wordLen) dot = null;
-                           isReal = dot || exponent;
                            if(isReal)
                               strtod(word, &s);      // strtod() seems to break on hex floats (e.g. 0x23e3p12, 0x1.fp3)
                            else
@@ -1814,7 +1933,7 @@ private:
                                     case 'f': case 'F': gotF++; if(gotF > 1 || !isReal) valid = false; break;
                                     case 'l': case 'L':
                                        gotL++;
-                                       if(gotL > 2 || isReal || (gotL == 2 && (s[i-1] != ch)))
+                                       if(gotL > 2 || (isReal && (gotL == 2 || gotF)) || (gotL == 2 && (s[i-1] != ch)))
                                        valid = false;
                                        break;
                                     case 'u': case 'U': gotU++; if(gotU > 1 || isReal) valid = false; break;
@@ -1856,14 +1975,14 @@ private:
                               if(firstWord)
                               {
                                  inPrep = true;
-                                 newTextColor = colorScheme.preprocessorColor;
+                                 newTextColor = wordLen == 1 ? colorScheme.keywordColors[1] : colorScheme.preprocessorColor;
                               }
                            }
-                           if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
+                           if(x < box.right && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
                            {
                               for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
                               {
-                                 char ** keys = keyWords[g];
+                                 const char ** keys = keyWords[g];
                                  int * len = keyLen[g];
                                  for(ccc = 0; keys[ccc]; ccc++)
                                  {
@@ -1919,7 +2038,8 @@ private:
                      //if(!numSpaces)
                      {
                         int tw;
-                        FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
+                        int oh;
+                        FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null, 0, null, &oh);
                         w = tw;
                      }
                      /*else
@@ -1948,13 +2068,14 @@ private:
                bool flagTrailingSpace = false;
                int numSpaces = 0;
                int wc;
+               int previousGlyph = 0;
 
                // Render checking if we need to split because of selection or to find where to draw insert caret
                for(wc = start; wc < start + bufferLen; wc++)
                {
                   flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
                      selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
-                  if(overWrite == true)
+                  if(overWrite == 1)
                   {
                      overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
                      if(overWriteCh == '\t') overWriteCh = ' ';
@@ -1964,8 +2085,9 @@ private:
                   {
                      flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
                      if(flagTrailingSpace) surface.SetBackground(red);
-                     FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
-                     if(overWrite == true)
+                     FlushBuffer(surface, line, wc, &renderStart, &x, y, &previousGlyph, &overHang, numSpaces, flagTrailingSpace, box);
+                     if(flagTrailingSpace) surface.SetBackground(background);
+                     if(overWrite == 1)
                      {
                         overWriteX = x;
                         overWriteY = y;
@@ -1973,8 +2095,6 @@ private:
                      }
                      numSpaces = 0;
 
-                     surface.TextOpacity(opacity);
-                     surface.SetBackground(background);
                      surface.SetForeground(foreground);
 
                      flush = false;
@@ -1994,7 +2114,8 @@ private:
                }
                flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
                if(flagTrailingSpace) surface.SetBackground(red);
-               FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
+               FlushBuffer(surface, line, wc, &renderStart, &x, y, &previousGlyph, &overHang, numSpaces, flagTrailingSpace, box);
+               if(flagTrailingSpace) surface.SetBackground(background);
                start += bufferLen;
             }
          }
@@ -2002,33 +2123,17 @@ private:
          if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
                         selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
          {
-            if(overWrite == true)
+            if(overWrite == 1)
             {
                overWriteX = x;
                overWriteY = y;
                overWriteCh = ' ';
                overWrite = 2;
             }
-            surface.TextOpacity(opacity);
             surface.SetBackground(background);
             surface.SetForeground(foreground);
          }
 
-         if(style.freeCaret && selected)
-         {
-            surface.SetBackground(selectionBackground);
-            surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
-            // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
-         }
-
-
-         /*
-         if(style.freeCaret && selected)
-         {
-            surface.SetBackground(selectionBackground);
-            surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
-         }
-         */
          if(line.count && line.text[line.count - 1] == '\\')
          {
             continuedSingleLineComment = inSingleLineComment;
@@ -2085,8 +2190,6 @@ private:
    void ComputeLength(EditLine line)
    {
       int c;
-      int tabOccur = 0;
-      int tabWidth;
       int x = 0;
 
       for(c = 0; c < line.count; )
@@ -2121,7 +2224,10 @@ private:
                len = 1;
             }
             else
-               FontExtent(display, font, line.buffer + start, len, &w, null);
+            {
+               int oh;
+               FontExtent(display, font, line.buffer + start, len, &w, null, 0, null, &oh);
+            }
          }
          else
          {
@@ -2468,7 +2574,7 @@ private:
       return false;
    }
 
-   bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
+   bool AddToLine(const char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr, int * xAdjustmentPtr)
    {
       bool hadComment = false;
       // Add the line here
@@ -2494,7 +2600,7 @@ private:
          {
             int w;
             int numBytes = 1;
-            char * string;
+            const char * string;
             if(c < Min(this.x, line.count))
                string = line.buffer + c;
             else if(c < endX)
@@ -2510,8 +2616,9 @@ private:
             }
             else
             {
+               int oh;
                numBytes = UTF8_NUM_BYTES(*string);
-               FontExtent(display, this.font, string, numBytes, &w, null);
+               FontExtent(display, this.font, string, numBytes, &w, null, 0, null, &oh);
             }
             x += w;
 
@@ -2528,6 +2635,7 @@ private:
       {
          int addedSpaces = 0;
          int addedTabs = 0;
+         int xAdjustment = 0;
 
          // Add blank spaces if EES_FREECARET
          if(this.x > line.count)
@@ -2554,6 +2662,8 @@ private:
                      addedSpaces = wantedPosition - position;
                   else
                   {
+                     xAdjustment = wantedPosition - position;
+
                      // Put a first tab
                      addedTabs = 1;
                      position += this.tabSize - (position % this.tabSize);
@@ -2562,6 +2672,8 @@ private:
                      position += (addedTabs-1) * this.tabSize;
                      // Finish off with spaces
                      addedSpaces = wantedPosition - position;
+
+                     xAdjustment -= addedSpaces + addedTabs;
                   }
                }
                else
@@ -2590,7 +2702,6 @@ private:
             CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
             if(addedTabs)
             {
-               *addedTabsPtr = addedTabs;
                FillBytes(line.buffer+line.count,'\t',addedTabs);
 #ifdef _DEBUG
       if(addedTabs > 4000 || addedTabs < 0)
@@ -2598,8 +2709,7 @@ private:
 #endif
                line.count += addedTabs;
             }
-            else if(addedTabs)
-               *addedTabsPtr = 0;
+
             if(addedSpaces)
             {
                FillBytes(line.buffer+line.count,' ',addedSpaces);
@@ -2608,10 +2718,11 @@ private:
          printf("Warning");
 #endif
                line.count += addedSpaces;
-               if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
             }
-            else if(addedSpacesPtr)
-               *addedSpacesPtr = 0;
+
+            if(addedTabsPtr) *addedTabsPtr = addedTabs;
+            if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
+            if(xAdjustmentPtr) *xAdjustmentPtr = xAdjustment;
 #ifdef _DEBUG
       if(count > 4000 || count < 0)
          printf("Warning");
@@ -2692,9 +2803,8 @@ private:
    // Returns true if it needs scrolling
    bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
    {
-      int w;
       int c;
-      int x, y;
+      int y;
       EditLine line;
       bool needHScroll = false;
 
@@ -2881,7 +2991,10 @@ private:
                         len = 1;
                      }
                      else
-                        FontExtent(display, this.font, line.buffer + start, len, &w, null);
+                     {
+                        int oh;
+                        FontExtent(display, this.font, line.buffer + start, len, &w, null, 0, null, &oh);
+                     }
                   }
                   else
                   {
@@ -2957,7 +3070,6 @@ private:
       while(true)
       {
          int start = c;
-         int numBytes = 1;
          int len = 1;
          int w;
          if(c < Min(max, line.count))
@@ -2987,7 +3099,10 @@ private:
                len = 1;
             }
             else
-               FontExtent(display, font, line.buffer + start, len, &w, null);
+            {
+               int oh;
+               FontExtent(display, font, line.buffer + start, len, &w, null, 0, null, &oh);
+            }
          }
          else
          {
@@ -3012,7 +3127,10 @@ private:
                else
                   a--;
                if(a > start)
-                  FontExtent(display, font, line.buffer + start, a - start, &w, null);
+               {
+                  int oh;
+                  FontExtent(display, font, line.buffer + start, a - start, &w, null, 0, null, &oh);
+               }
                else
                   w = 0;
                if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
@@ -3102,7 +3220,7 @@ private:
    }
 
    /*
-   bool SaveFile(char * fileName)
+   bool SaveFile(const char * fileName)
    {
       File f = eFile_Open(fileName, FO_WRITE);
       if(f)
@@ -3532,8 +3650,9 @@ private:
    {
       if(FontExtent)
       {
-         FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
-         FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
+         int oh;
+         FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h, 0, null, &oh);
+         FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h, 0, null, &oh);
 
          space.w = Max(space.w, 1);
          large.w = Max(large.w, 1);
@@ -3558,7 +3677,7 @@ private:
 
    bool OnLoadGraphics()
    {
-      FontExtent = Display::FontExtent;
+      FontExtent = Display::FontExtent2;
       font = fontObject;
       ComputeFont();
       // UpdateCaretPosition(true);
@@ -3597,7 +3716,7 @@ private:
                int y;
                bool done = false;
                EditLine line = this.line;
-               int c;
+               int c = 0;
                for(y = this.y; y>= 0; y--)
                {
                   c = (y == this.y) ? (Min(this.x-1, line.count-1)) : line.count-1;
@@ -3678,7 +3797,6 @@ private:
                      if(key.ctrl)
                      {
                         int i;
-                        int length;
                         char * buffer = line1.buffer;
                         for(i = x1; i < line1.count; i++)
                         {
@@ -3758,12 +3876,13 @@ private:
                   stuffAfter = true;
 
                //If last character is a { indent one tab
-               if(this.line.buffer[this.x - 1] == '{')
+               c = Min(x, line.count);
+               if(c > 0 && line.buffer[c - 1] == '{')
                {
                   //Except if the next non space character is a }
                   bool indent = false;
                   int i;
-                  for(i = this.x; i < this.line.size; i++)
+                  for(i = c; i <= this.line.count; i++)   // indent will be set to true on nul terminating char
                      if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
                      {
                         if(this.line.buffer[i] != '}')
@@ -3845,8 +3964,8 @@ private:
                bool foundAlpha = false;
                bool found = false;
                int y = this.y;
-               EditLine line, lastLine;
-               int lastC, lastY;
+               EditLine line, lastLine = null;
+               int lastC = 0, lastY = 0;
 
                for(line = this.line; (line && !found); line = line.prev, y--)
                {
@@ -3884,7 +4003,7 @@ private:
                            break;
                         }
                      }
-                     while(--c)
+                     while(--c >= 0)
                      {
                         byte ch = line.buffer[c];
                         if(UTF8_IS_FIRST(ch)) break;
@@ -3917,7 +4036,7 @@ private:
                {
                   if(x <= line.count)
                   {
-                     byte * buffer = line.buffer;
+                     byte * buffer = (byte *)line.buffer;
                      while(--x)
                      {
                         byte ch = buffer[x];
@@ -3967,9 +4086,9 @@ private:
                {
                   bool foundAlpha = false;
                   bool found = false;
-                  EditLine line, lastLine;
+                  EditLine line = null, lastLine = null;
                   int y = this.y;
-                  int lastC, lastY, lastNumBytes;
+                  int lastC = 0, lastY = 0, lastNumBytes = 0;
 
                   for(line = this.line; (line && !found); line = line.next, y++)
                   {
@@ -4063,7 +4182,7 @@ private:
                {
                   if(x < line.count)
                   {
-                     byte * buffer = line.buffer;
+                     byte * buffer = (byte *)line.buffer;
                      while(++x)
                      {
                         byte ch = buffer[x];
@@ -4274,11 +4393,13 @@ private:
             {
                if(style.stuckCaret) break;
                {
+                  /*
                   int th = space.h;
                   int textPos = 0;
                   int sx = 0, sy = this.y * this.space.h;
                   int maxW = clientSize.w - sx;
                   char * text = line.buffer;
+                  */
 
                   if(!shift) SelDirty();
                   DirtyLine(this.y);
@@ -4809,12 +4930,12 @@ private:
                   {
                      //Only indent back if you are exactly at one tab.
                      {
-                        bool whitespace = true;
+                        //bool whitespace = true;
                         int i;
                         char * newline;
                         int putsize;
 
-                        int indentwidth;
+                        int indentwidth = 0;
                         EditLine line = this.line;
 
                         //Only remove one tab if there is nothing else on the line.
@@ -4968,14 +5089,14 @@ private:
       UpdateDirty();
    }
 
-   bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
+   bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr, int * xAdjustmentPtr)
    {
       EditLine line;
-      int length, endX;
+      int length, endX = 0;
       bool result;
       ReplaceTextAction replaceAction = null;
       AddCharAction addCharAction = null;
-      int addedSpaces = 0, addedTabs = 0;
+      int addedSpaces = 0, addedTabs = 0, xAdjustment = 0;
 
       if(ch == '\r') return true;
       if(style.stuckCaret /*|EES_READONLY)*/ )
@@ -5109,9 +5230,10 @@ private:
          char string[5];
          int count = UTF32toUTF8Len(&ch, 1, string, 5);
          DelSel(&addedSpaces);
-         result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
+         result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs, &xAdjustment);
          if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
          if(addedTabsPtr) *addedTabsPtr = addedTabs;
+         if(xAdjustmentPtr) *xAdjustmentPtr = xAdjustment;
       }
       this.selX = this.x;
       this.selY = this.y;
@@ -5143,7 +5265,7 @@ public:
 
    bool AddCh(unichar ch)
    {
-      return _AddCh(ch, null, null);
+      return _AddCh(ch, null, null, null);
    }
 
    void Modified()
@@ -5167,6 +5289,11 @@ public:
       undoBuffer.Undo();
       itemEditUndo.disabled = undoBuffer.curAction == 0;
       itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
+
+      UpdateDirty();
+      SetSelectCursor();
+      SelectionEnables();
+
       if(savedAction == undoBuffer.curAction)
       {
          modifiedDocument = false;
@@ -5180,6 +5307,11 @@ public:
       undoBuffer.Redo();
       itemEditUndo.disabled = undoBuffer.curAction == 0;
       itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
+
+      UpdateDirty();
+      SetSelectCursor();
+      SelectionEnables();
+
       if(savedAction == undoBuffer.curAction)
       {
          modifiedDocument = false;
@@ -5232,14 +5364,14 @@ public:
    }
 
    // BASIC OUTPUT
-   bool AddS(char * string)
+   bool AddS(const char * string)
    {
       if(this)
       {
          bool ret = true;
-         char * line;
+         const char * line;
          int c, count;
-         int addedSpaces = 0, addedTabs = 0;
+         int addedSpaces = 0, addedTabs = 0, xAdjustment = 0;
          AddTextAction action = null;
          ReplaceTextAction replaceAction = null;
 
@@ -5312,7 +5444,7 @@ public:
          {
             if(string[c] == '\n' || string[c] == '\r')
             {
-               if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
+               if(!AddToLine(line, count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs, xAdjustment ? null : &xAdjustment))
                {
                   ret = false;
                   break;
@@ -5347,7 +5479,7 @@ public:
 
          // Add the line here
          if(ret && count)
-            if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
+            if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs, xAdjustment ? null : &xAdjustment))
             {
                ret = false;
             }
@@ -5360,6 +5492,7 @@ public:
             action.x2 = x;
             action.addedSpaces = addedSpaces;
             action.addedTabs = addedTabs;
+            action.xAdjustment = xAdjustment;
          }
          else if(replaceAction)
          {
@@ -5367,6 +5500,7 @@ public:
             replaceAction.x3 = x;
             replaceAction.addedSpaces = addedSpaces;
             replaceAction.addedTabs = addedTabs;
+            replaceAction.xAdjustment = xAdjustment;
          }
 
          UpdateCaretPosition(true);
@@ -5395,10 +5529,10 @@ public:
    {
       bool result;
 
-      if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
+      if((ch >= 32 /*&& ch <=126*/) || ch == '\n' || ch == '\t')
       //if((ch >= 32) || ch == '\n')
       {
-         int addedSpaces = 0, addedTabs = 0;
+         int addedSpaces = 0, addedTabs = 0, xAdjustment = 0;
          ReplaceTextAction replaceAction = null;
          AddCharAction addCharAction = null;
 
@@ -5462,19 +5596,20 @@ public:
             }
          }
          undoBuffer.dontRecord++;
-         result = _AddCh(ch, &addedSpaces, &addedTabs);
+         result = _AddCh(ch, &addedSpaces, &addedTabs, &xAdjustment);
          if(replaceAction)
          {
             replaceAction.x3 = x;
             replaceAction.y3 = y;
             replaceAction.addedSpaces = addedSpaces;
             replaceAction.addedTabs = addedTabs;
+            replaceAction.addedTabs = xAdjustment;
          }
          if(addCharAction)
          {
-            addCharAction.x -= addedTabs * (tabSize-1);
             addCharAction.addedSpaces = addedSpaces;
             addCharAction.addedTabs = addedTabs;
+            addCharAction.xAdjustment = xAdjustment;
          }
          undoBuffer.dontRecord--;
          if(ch == '\n')
@@ -5484,7 +5619,7 @@ public:
       }
    }
 
-   void PutS(char * string)
+   void PutS(const char * string)
    {
       if(this)
       {
@@ -5494,7 +5629,7 @@ public:
       }
    }
 
-   void Printf(char * format, ...)
+   void Printf(const char * format, ...)
    {
       if(this)
       {
@@ -5508,7 +5643,7 @@ public:
       }
    }
 
-   void SetContents(char * format, ...)
+   void SetContents(const char * format, ...)
    {
       if(this)
       {
@@ -5657,8 +5792,7 @@ public:
    {
       if(created)
       {
-         int w;
-         int c, numLines;
+         int numLines;
          EditLine line;
          int x;
          int checkX, checkY;
@@ -5821,7 +5955,7 @@ public:
       }
       else
       {
-         EditLine oldLine = this.line;
+         //EditLine oldLine = this.line;
          bool lastOne = false;
          EditLine oldViewLine = this.viewLine;
          bool figureSyntax = false;
@@ -5869,8 +6003,6 @@ public:
       }
       else
       {
-         EditLine oldLine = this.line;
-
          for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
          {
             this.line = line;
@@ -6235,7 +6367,7 @@ public:
       itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
    }
 
-   EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
+   EditBoxFindResult Find(const char * text, bool matchWord, bool matchCase, bool isSearchDown)
    {
       EditLine line;
       int num;
@@ -6293,7 +6425,7 @@ public:
       return notFound;
    }
 
-   EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
+   EditBoxFindResult FindInSelection(const char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
    {
       EditLine line;
       int y;
@@ -6528,7 +6660,7 @@ public:
       return result;
    }
 
-   bool Puts(char * string)
+   bool Puts(const char * string)
    {
       EditBox editBox = this.editBox;
       BufferLocation start { editBox.line, editBox.y, editBox.x };
@@ -6558,7 +6690,7 @@ public:
       {
          utf8Bytes[numBytes++] = ch;
          utf8Bytes[numBytes] = 0;
-         if(UTF8Validate(utf8Bytes))
+         if(UTF8Validate((char *)utf8Bytes))
          {
             editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
             numBytes = 0;