Package jexer.teditor
Class Document
- java.lang.Object
-
- jexer.teditor.Document
-
public class Document extends java.lang.Object
A Document represents a text file, as a collection of lines.
-
-
Constructor Summary
Constructors Constructor Description Document(java.lang.String str, CellAttributes defaultColor)
Construct a new Document from an existing text string.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addChar(int ch)
Replace or insert a character at the cursor, depending on overwrite flag.void
backspace()
Delete the character immediately preceeding the cursor.void
backTab()
Handle the backtab (shift-tab) character.void
backwardsWord()
Go back to the beginning of this word if in the middle, or the beginning of the previous word.void
cleanWhitespace()
Trim trailing whitespace from lines and trailing empty lines from the document.void
del()
Delete the character under the cursor.boolean
down()
Increment the line number by one.boolean
down(int n)
Increment the line number by n.Document
dup()
Create a duplicate instance.boolean
end()
Go to the last column of this line.void
enter()
Split the current line into two, like pressing the enter key.void
forwardsWord()
Go to the beginning of the next word.int
getChar()
Get the character at the current cursor position in the text.Line
getCurrentLine()
Get the current editing line.int
getCursor()
Get the current cursor position of the editing line.Line
getLine(int lineNumber)
Get a specific line by number.int
getLineCount()
Get the number of lines.int
getLineLength()
Get the current line length.int
getLineLengthMax()
Compute the maximum line length for this document.int
getLineNumber()
Get the current line number being edited.java.util.List<Line>
getLines()
Get a (shallow) copy of the list of lines.java.lang.String
getRawLine()
Get the raw string that matches this line.int
getTabSize()
Get the tab stop size.java.lang.String
getText()
Get the entire contents of the document as one string.boolean
home()
Go to the first column of this line.boolean
isDirty()
Get the dirty value.boolean
isOverwrite()
Get the overwrite flag.boolean
left()
Decrement the cursor by one.boolean
right()
Increment the cursor by one.void
saveToFilename(java.lang.String filename)
Save contents to file.void
setBackspaceUnindents(boolean backspaceUnindents)
Set the backspace unindent option.void
setCursor(int cursor)
Set the current cursor position of the editing line.void
setHighlighting(boolean enabled)
Set keyword highlighting.void
setLineNumber(int n)
Set the current line number being edited.void
setNotDirty()
Unset the dirty flag.void
setOverwrite(boolean overwrite)
Set the overwrite flag.void
setSaveWithTabs(boolean saveWithTabs)
Set the save with tabs option.void
setTabSize(int tabSize)
Set the tab stop size.void
setText(java.lang.String text)
Set the entire contents of the document from one string.void
tab()
Handle the tab character.boolean
up()
Decrement the line number by one.boolean
up(int n)
Decrement the line number by n.void
wrapText(int width)
Wrap the document text to fit in a new width.
-
-
-
Constructor Detail
-
Document
public Document(java.lang.String str, CellAttributes defaultColor)
Construct a new Document from an existing text string.- Parameters:
str
- the text stringdefaultColor
- the color for unhighlighted text
-
-
Method Detail
-
setText
public void setText(java.lang.String text)
Set the entire contents of the document from one string.- Parameters:
text
- the new contents
-
dup
public Document dup()
Create a duplicate instance.- Returns:
- duplicate intance
-
isOverwrite
public boolean isOverwrite()
Get the overwrite flag.- Returns:
- true if addChar() overwrites data, false if it inserts
-
isDirty
public boolean isDirty()
Get the dirty value.- Returns:
- true if the buffer is dirty
-
setNotDirty
public void setNotDirty()
Unset the dirty flag.
-
saveToFilename
public void saveToFilename(java.lang.String filename) throws java.io.IOException
Save contents to file.- Parameters:
filename
- file to save to- Throws:
java.io.IOException
- if a java.io operation throws
-
setOverwrite
public void setOverwrite(boolean overwrite)
Set the overwrite flag.- Parameters:
overwrite
- true if addChar() should overwrite data, false if it should insert
-
getLineNumber
public int getLineNumber()
Get the current line number being edited.- Returns:
- the line number. Note that this is 0-based: 0 is the first line.
-
getCurrentLine
public Line getCurrentLine()
Get the current editing line.- Returns:
- the line
-
getLine
public Line getLine(int lineNumber)
Get a specific line by number.- Parameters:
lineNumber
- the line number. Note that this is 0-based: 0 is the first line.- Returns:
- the line
-
setLineNumber
public void setLineNumber(int n)
Set the current line number being edited.- Parameters:
n
- the line number. Note that this is 0-based: 0 is the first line.
-
getCursor
public int getCursor()
Get the current cursor position of the editing line.- Returns:
- the cursor position
-
getChar
public int getChar()
Get the character at the current cursor position in the text.- Returns:
- the character, or -1 if the cursor is at the end of the line
-
setCursor
public void setCursor(int cursor)
Set the current cursor position of the editing line. 0-based.- Parameters:
cursor
- the new cursor position
-
down
public boolean down()
Increment the line number by one. If at the last line, do nothing.- Returns:
- true if the editing line changed
-
down
public boolean down(int n)
Increment the line number by n. If n would go past the last line, increment only to the last line.- Parameters:
n
- the number of lines to increment by- Returns:
- true if the editing line changed
-
up
public boolean up()
Decrement the line number by one. If at the first line, do nothing.- Returns:
- true if the editing line changed
-
up
public boolean up(int n)
Decrement the line number by n. If n would go past the first line, decrement only to the first line.- Parameters:
n
- the number of lines to decrement by- Returns:
- true if the editing line changed
-
left
public boolean left()
Decrement the cursor by one. If at the first column on the first line, do nothing.- Returns:
- true if the cursor position changed
-
right
public boolean right()
Increment the cursor by one. If at the last column on the last line, do nothing.- Returns:
- true if the cursor position changed
-
backwardsWord
public void backwardsWord()
Go back to the beginning of this word if in the middle, or the beginning of the previous word.
-
forwardsWord
public void forwardsWord()
Go to the beginning of the next word.
-
getRawLine
public java.lang.String getRawLine()
Get the raw string that matches this line.- Returns:
- the string
-
home
public boolean home()
Go to the first column of this line.- Returns:
- true if the cursor position changed
-
end
public boolean end()
Go to the last column of this line.- Returns:
- true if the cursor position changed
-
del
public void del()
Delete the character under the cursor.
-
backspace
public void backspace()
Delete the character immediately preceeding the cursor.
-
enter
public void enter()
Split the current line into two, like pressing the enter key.
-
addChar
public void addChar(int ch)
Replace or insert a character at the cursor, depending on overwrite flag.- Parameters:
ch
- the character to replace or insert
-
getTabSize
public int getTabSize()
Get the tab stop size.- Returns:
- the tab stop size
-
setTabSize
public void setTabSize(int tabSize)
Set the tab stop size.- Parameters:
tabSize
- the new tab stop size
-
setBackspaceUnindents
public void setBackspaceUnindents(boolean backspaceUnindents)
Set the backspace unindent option.- Parameters:
backspaceUnindents
- If true, backspace at an indent level goes back a full indent level. If false, backspace always goes back one column.
-
setSaveWithTabs
public void setSaveWithTabs(boolean saveWithTabs)
Set the save with tabs option.- Parameters:
saveWithTabs
- If true, save files with tab characters. If false, convert tabs to spaces when saving files.
-
tab
public void tab()
Handle the tab character.
-
backTab
public void backTab()
Handle the backtab (shift-tab) character.
-
getLines
public java.util.List<Line> getLines()
Get a (shallow) copy of the list of lines.- Returns:
- the list of lines
-
getLineCount
public int getLineCount()
Get the number of lines.- Returns:
- the number of lines
-
getLineLengthMax
public int getLineLengthMax()
Compute the maximum line length for this document.- Returns:
- the number of cells needed to display the longest line
-
getLineLength
public int getLineLength()
Get the current line length.- Returns:
- the number of cells needed to display the current line
-
getText
public java.lang.String getText()
Get the entire contents of the document as one string.- Returns:
- the document contents
-
cleanWhitespace
public void cleanWhitespace()
Trim trailing whitespace from lines and trailing empty lines from the document.
-
setHighlighting
public void setHighlighting(boolean enabled)
Set keyword highlighting.- Parameters:
enabled
- if true, enable keyword highlighting
-
wrapText
public void wrapText(int width)
Wrap the document text to fit in a new width.- Parameters:
width
- the width to fit to
-
-