Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[fanfix.git] / src / jexer / teditor / Line.java
index d6016c22ee701c478bf86bd18597f87117b7260a..7cd5febabee8462f6c51bc66e886903795aff698 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -32,6 +32,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import jexer.bits.CellAttributes;
+import jexer.bits.StringUtils;
 
 /**
  * A Line represents a single line of text on the screen, as a collection of
@@ -39,6 +40,10 @@ import jexer.bits.CellAttributes;
  */
 public class Line {
 
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * The list of words.
      */
@@ -55,9 +60,14 @@ public class Line {
     private Highlighter highlighter = null;
 
     /**
-     * The current cursor position on this line.
+     * The current edition position on this line.
+     */
+    private int position = 0;
+
+    /**
+     * The current editing position screen column number.
      */
-    private int cursor = 0;
+    private int screenPosition = 0;
 
     /**
      * The raw text of this line, what is passed to Word to determine
@@ -65,6 +75,42 @@ public class Line {
      */
     private StringBuilder rawText;
 
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Construct a new Line from an existing text string, and highlight
+     * certain strings.
+     *
+     * @param str the text string
+     * @param defaultColor the color for unhighlighted text
+     * @param highlighter the highlighter to use
+     */
+    public Line(final String str, final CellAttributes defaultColor,
+        final Highlighter highlighter) {
+
+        this.defaultColor = defaultColor;
+        this.highlighter = highlighter;
+        this.rawText = new StringBuilder(str);
+
+        scanLine();
+    }
+
+    /**
+     * Construct a new Line from an existing text string.
+     *
+     * @param str the text string
+     * @param defaultColor the color for unhighlighted text
+     */
+    public Line(final String str, final CellAttributes defaultColor) {
+        this(str, defaultColor, null);
+    }
+
+    // ------------------------------------------------------------------------
+    // Line -------------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Get a (shallow) copy of the words in this line.
      *
@@ -75,12 +121,21 @@ public class Line {
     }
 
     /**
-     * Get the current cursor position.
+     * Get the current cursor position in the text.
+     *
+     * @return the cursor position
+     */
+    public int getRawCursor() {
+        return position;
+    }
+
+    /**
+     * Get the current cursor position on screen.
      *
      * @return the cursor position
      */
     public int getCursor() {
-        return cursor;
+        return screenPosition;
     }
 
     /**
@@ -96,7 +151,20 @@ public class Line {
             throw new IndexOutOfBoundsException("Max length is " +
                 getDisplayLength() + ", requested position " + cursor);
         }
-        this.cursor = cursor;
+        screenPosition = cursor;
+        position = screenToTextPosition(screenPosition);
+    }
+
+    /**
+     * Get the character at the current cursor position in the text.
+     *
+     * @return the character, or -1 if the cursor is at the end of the line
+     */
+    public int getChar() {
+        if (position == rawText.length()) {
+            return -1;
+        }
+        return rawText.codePointAt(position);
     }
 
     /**
@@ -105,12 +173,11 @@ public class Line {
      * @return the number of cells needed to display this line
      */
     public int getDisplayLength() {
-        int n = rawText.length();
+        int n = StringUtils.width(rawText.toString());
 
-        // For now just return the raw text length.
         if (n > 0) {
             // If we have any visible characters, add one to the display so
-            // that the cursor is immediately after the data.
+            // that the position is immediately after the data.
             return n + 1;
         }
         return n;
@@ -132,8 +199,9 @@ public class Line {
         words.clear();
         Word word = new Word(this.defaultColor, this.highlighter);
         words.add(word);
-        for (int i = 0; i < rawText.length(); i++) {
-            char ch = rawText.charAt(i);
+        for (int i = 0; i < rawText.length();) {
+            int ch = rawText.codePointAt(i);
+            i += Character.charCount(ch);
             Word newWord = word.addChar(ch);
             if (newWord != word) {
                 words.add(newWord);
@@ -145,44 +213,17 @@ public class Line {
         }
     }
 
-    /**
-     * Construct a new Line from an existing text string, and highlight
-     * certain strings.
-     *
-     * @param str the text string
-     * @param defaultColor the color for unhighlighted text
-     * @param highlighter the highlighter to use
-     */
-    public Line(final String str, final CellAttributes defaultColor,
-        final Highlighter highlighter) {
-
-        this.defaultColor = defaultColor;
-        this.highlighter = highlighter;
-        this.rawText = new StringBuilder(str);
-
-        scanLine();
-    }
-
-    /**
-     * Construct a new Line from an existing text string.
-     *
-     * @param str the text string
-     * @param defaultColor the color for unhighlighted text
-     */
-    public Line(final String str, final CellAttributes defaultColor) {
-        this(str, defaultColor, null);
-    }
-
     /**
      * Decrement the cursor by one.  If at the first column, do nothing.
      *
      * @return true if the cursor position changed
      */
     public boolean left() {
-        if (cursor == 0) {
+        if (position == 0) {
             return false;
         }
-        cursor--;
+        screenPosition -= StringUtils.width(rawText.codePointBefore(position));
+        position -= Character.charCount(rawText.codePointBefore(position));
         return true;
     }
 
@@ -195,10 +236,14 @@ public class Line {
         if (getDisplayLength() == 0) {
             return false;
         }
-        if (cursor == getDisplayLength() - 1) {
+        if (position == getDisplayLength() - 1) {
             return false;
         }
-        cursor++;
+        if (position < rawText.length()) {
+            screenPosition += StringUtils.width(rawText.codePointAt(position));
+            position += Character.charCount(rawText.codePointAt(position));
+        }
+        assert (position <= rawText.length());
         return true;
     }
 
@@ -208,8 +253,9 @@ public class Line {
      * @return true if the cursor position changed
      */
     public boolean home() {
-        if (cursor > 0) {
-            cursor = 0;
+        if (position > 0) {
+            position = 0;
+            screenPosition = 0;
             return true;
         }
         return false;
@@ -221,11 +267,9 @@ public class Line {
      * @return true if the cursor position changed
      */
     public boolean end() {
-        if (cursor != getDisplayLength() - 1) {
-            cursor = getDisplayLength() - 1;
-            if (cursor < 0) {
-                cursor = 0;
-            }
+        if (position != getDisplayLength() - 1) {
+            position = rawText.length();
+            screenPosition = StringUtils.width(rawText.toString());
             return true;
         }
         return false;
@@ -237,8 +281,11 @@ public class Line {
     public void del() {
         assert (words.size() > 0);
 
-        if (cursor < getDisplayLength()) {
-            rawText.deleteCharAt(cursor);
+        if (position < getDisplayLength()) {
+            int n = Character.charCount(rawText.codePointAt(position));
+            for (int i = 0; i < n; i++) {
+                rawText.deleteCharAt(position);
+            }
         }
 
         // Re-scan the line to determine the new word boundaries.
@@ -259,14 +306,15 @@ public class Line {
      *
      * @param ch the character to insert
      */
-    public void addChar(final char ch) {
-        if (cursor < getDisplayLength() - 1) {
-            rawText.insert(cursor, ch);
+    public void addChar(final int ch) {
+        if (position < getDisplayLength() - 1) {
+            rawText.insert(position, Character.toChars(ch));
         } else {
-            rawText.append(ch);
+            rawText.append(Character.toChars(ch));
         }
+        position += Character.charCount(ch);
+        screenPosition += StringUtils.width(ch);
         scanLine();
-        cursor++;
     }
 
     /**
@@ -274,14 +322,44 @@ public class Line {
      *
      * @param ch the character to replace
      */
-    public void replaceChar(final char ch) {
-        if (cursor < getDisplayLength() - 1) {
-            rawText.setCharAt(cursor, ch);
+    public void replaceChar(final int ch) {
+        if (position < getDisplayLength() - 1) {
+            // Replace character
+            String oldText = rawText.toString();
+            rawText = new StringBuilder(oldText.substring(0, position));
+            rawText.append(Character.toChars(ch));
+            rawText.append(oldText.substring(position + 1));
+            screenPosition += StringUtils.width(rawText.codePointAt(position));
+            position += Character.charCount(ch);
         } else {
-            rawText.append(ch);
+            rawText.append(Character.toChars(ch));
+            position += Character.charCount(ch);
+            screenPosition += StringUtils.width(ch);
         }
         scanLine();
-        cursor++;
+    }
+
+    /**
+     * Determine string position from screen position.
+     *
+     * @param screenPosition the position on screen
+     * @return the equivalent position in text
+     */
+    protected int screenToTextPosition(final int screenPosition) {
+        if (screenPosition == 0) {
+            return 0;
+        }
+
+        int n = 0;
+        for (int i = 0; i < rawText.length(); i++) {
+            n += StringUtils.width(rawText.codePointAt(i));
+            if (n >= screenPosition) {
+                return i + 1;
+            }
+        }
+        // screenPosition exceeds the available text length.
+        throw new IndexOutOfBoundsException("screenPosition " + screenPosition +
+            " exceeds available text length " + rawText.length());
     }
 
 }