X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fteditor%2FDocument.java;h=c84c20787e6453e987db5fcec7f01911b356e959;hb=85c07c5e6db3a5e74f5ba2bd6e7ee2656d5b63a0;hp=5b7050fe22efd3963ca762256e163ba30c39a87b;hpb=e8a11f986bfe2556e450d7b8ad6ef0059b369bbc;p=fanfix.git diff --git a/src/jexer/teditor/Document.java b/src/jexer/teditor/Document.java index 5b7050f..c84c207 100644 --- a/src/jexer/teditor/Document.java +++ b/src/jexer/teditor/Document.java @@ -28,6 +28,9 @@ */ package jexer.teditor; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; @@ -54,6 +57,11 @@ public class Document { */ private boolean overwrite = false; + /** + * If true, the document has been edited. + */ + private boolean dirty = false; + /** * The default color for the TEditor class. */ @@ -73,6 +81,41 @@ public class Document { return overwrite; } + /** + * Get the dirty value. + * + * @return true if the buffer is dirty + */ + public boolean isDirty() { + return dirty; + } + + /** + * Save contents to file. + * + * @param filename file to save to + * @throws IOException if a java.io operation throws + */ + public void saveToFilename(final String filename) throws IOException { + OutputStreamWriter output = null; + try { + output = new OutputStreamWriter(new FileOutputStream(filename), + "UTF-8"); + + for (Line line: lines) { + output.write(line.getRawString()); + output.write("\n"); + } + + dirty = false; + } + finally { + if (output != null) { + output.close(); + } + } + } + /** * Set the overwrite flag. * @@ -136,6 +179,15 @@ public class Document { return lines.get(lineNumber).getCursor(); } + /** + * Set the current cursor position of the editing line. 0-based. + * + * @param cursor the new cursor position + */ + public void setCursor(final int cursor) { + lines.get(lineNumber).setCursor(cursor); + } + /** * Construct a new Document from an existing text string. * @@ -246,7 +298,15 @@ public class Document { * @return true if the cursor position changed */ public boolean left() { - return lines.get(lineNumber).left(); + if (!lines.get(lineNumber).left()) { + // We are on the leftmost column, wrap + if (up()) { + end(); + } else { + return false; + } + } + return true; } /** @@ -255,7 +315,15 @@ public class Document { * @return true if the cursor position changed */ public boolean right() { - return lines.get(lineNumber).right(); + if (!lines.get(lineNumber).right()) { + // We are on the rightmost column, wrap + if (down()) { + home(); + } else { + return false; + } + } + return true; } /** @@ -280,14 +348,64 @@ public class Document { * Delete the character under the cursor. */ public void del() { - lines.get(lineNumber).del(); + dirty = true; + int cursor = lines.get(lineNumber).getCursor(); + if (cursor < lines.get(lineNumber).getDisplayLength() - 1) { + lines.get(lineNumber).del(); + } else if (lineNumber < lines.size() - 2) { + // Join two lines + StringBuilder newLine = new StringBuilder(lines. + get(lineNumber).getRawString()); + newLine.append(lines.get(lineNumber + 1).getRawString()); + lines.set(lineNumber, new Line(newLine.toString(), + defaultColor, highlighter)); + lines.get(lineNumber).setCursor(cursor); + lines.remove(lineNumber + 1); + } } /** * Delete the character immediately preceeding the cursor. */ public void backspace() { - lines.get(lineNumber).backspace(); + dirty = true; + int cursor = lines.get(lineNumber).getCursor(); + if (cursor > 0) { + lines.get(lineNumber).backspace(); + } else if (lineNumber > 0) { + // Join two lines + lineNumber--; + String firstLine = lines.get(lineNumber).getRawString(); + if (firstLine.length() > 0) { + // Backspacing combining two lines + StringBuilder newLine = new StringBuilder(firstLine); + newLine.append(lines.get(lineNumber + 1).getRawString()); + lines.set(lineNumber, new Line(newLine.toString(), + defaultColor, highlighter)); + lines.get(lineNumber).setCursor(firstLine.length()); + lines.remove(lineNumber + 1); + } else { + // Backspacing an empty line + lines.remove(lineNumber); + lines.get(lineNumber).setCursor(0); + } + } + } + + /** + * Split the current line into two, like pressing the enter key. + */ + public void enter() { + dirty = true; + int cursor = lines.get(lineNumber).getCursor(); + String original = lines.get(lineNumber).getRawString(); + String firstLine = original.substring(0, cursor); + String secondLine = original.substring(cursor); + lines.add(lineNumber + 1, new Line(secondLine, defaultColor, + highlighter)); + lines.set(lineNumber, new Line(firstLine, defaultColor, highlighter)); + lineNumber++; + lines.get(lineNumber).home(); } /** @@ -297,6 +415,7 @@ public class Document { * @param ch the character to replace or insert */ public void addChar(final char ch) { + dirty = true; if (overwrite) { lines.get(lineNumber).replaceChar(ch); } else { @@ -337,4 +456,13 @@ public class Document { return n; } + /** + * Get the current line length. + * + * @return the number of cells needed to display the current line + */ + public int getLineLength() { + return lines.get(lineNumber).getDisplayLength(); + } + }