text-zipper-0.13: A text editor zipper library

Safe Haskell | None
Language | Haskell2010

Data.Text.Zipper

Contents

- Construction and extraction
- Navigation functions
- Inspection functions
- Editing functions

Description

This module provides a two-dimensional text zipper data structure. This structure represents a body of text and an editing cursor which can be moved throughout the text, along with a set of editing transformations.

Text zippers are generalized over the set of data types that might be used to store lists of characters (e.g., `String`, `Text`, etc.). As a result, the most general way to create a text zipper is to use `mkZipper` and provide all of the functions required to manipulate the underlying text data.

Implementations using `Text` and `String` are provided.

Synopsis

- data TextZipper a
- mkZipper :: Monoid a => (Char -> a) -> (Int -> a -> a) -> (Int -> a -> a) -> (a -> Int) -> (a -> Char) -> (a -> a) -> (a -> Bool) -> (a -> [a]) -> (a -> [Char]) -> [a] -> Maybe Int -> TextZipper a
- textZipper :: [Text] -> Maybe Int -> TextZipper Text
- stringZipper :: [String] -> Maybe Int -> TextZipper String
- clearZipper :: Monoid a => TextZipper a -> TextZipper a
- vectorZipper :: [Vector Char] -> Maybe Int -> TextZipper (Vector Char)
- getText :: Monoid a => TextZipper a -> [a]
- currentLine :: Monoid a => TextZipper a -> a
- cursorPosition :: TextZipper a -> (Int, Int)
- lineLengths :: Monoid a => TextZipper a -> [Int]
- getLineLimit :: TextZipper a -> Maybe Int
- moveCursor :: Monoid a => (Int, Int) -> TextZipper a -> TextZipper a
- moveCursorClosest :: Monoid a => (Int, Int) -> TextZipper a -> TextZipper a
- moveRight :: Monoid a => TextZipper a -> TextZipper a
- moveLeft :: Monoid a => TextZipper a -> TextZipper a
- moveUp :: Monoid a => TextZipper a -> TextZipper a
- moveDown :: Monoid a => TextZipper a -> TextZipper a
- gotoEOL :: Monoid a => TextZipper a -> TextZipper a
- gotoBOL :: Monoid a => TextZipper a -> TextZipper a
- gotoEOF :: Monoid a => TextZipper a -> TextZipper a
- gotoBOF :: Monoid a => TextZipper a -> TextZipper a
- currentChar :: TextZipper a -> Maybe Char
- nextChar :: Monoid a => TextZipper a -> Maybe Char
- previousChar :: Monoid a => TextZipper a -> Maybe Char
- insertChar :: Monoid a => Char -> TextZipper a -> TextZipper a
- insertMany :: Monoid a => a -> TextZipper a -> TextZipper a
- deletePrevChar :: (Eq a, Monoid a) => TextZipper a -> TextZipper a
- deleteChar :: Monoid a => TextZipper a -> TextZipper a
- breakLine :: Monoid a => TextZipper a -> TextZipper a
- killToEOL :: Monoid a => TextZipper a -> TextZipper a
- killToBOL :: Monoid a => TextZipper a -> TextZipper a
- killToEOF :: Monoid a => TextZipper a -> TextZipper a
- killToBOF :: Monoid a => TextZipper a -> TextZipper a
- transposeChars :: Monoid a => TextZipper a -> TextZipper a

# Documentation

data TextZipper a Source #

#### Instances

Instances details

NFData a => NFData (TextZipper a) Source # |
Instance details Defined in Data.Text.Zipper Methods rnf :: TextZipper a -> () Source #
Show a => Show (TextZipper a) Source # |
Instance details Defined in Data.Text.Zipper Methods showsPrec :: Int -> TextZipper a -> ShowS Source # show :: TextZipper a -> String Source # showList :: [TextZipper a] -> ShowS Source #
Eq a => Eq (TextZipper a) Source # |
Instance details Defined in Data.Text.Zipper Methods (==) :: TextZipper a -> TextZipper a -> Bool Source # (/=) :: TextZipper a -> TextZipper a -> Bool Source #

# Construction and extraction

mkZipper Source #

Arguments

:: Monoid a |
=> (Char -> a) | A singleton constructor.
-> (Int -> a -> a) | `drop`.
-> (Int -> a -> a) | `take`.
-> (a -> Int) | `length`.
-> (a -> Char) | `last`.
-> (a -> a) | `init`.
-> (a -> Bool) | `null`.
-> (a -> [a]) | `lines`.
-> (a -> [Char]) | `toList`.
-> [a] | The initial lines of text.
-> Maybe Int | Limit to this many lines of text (`Nothing` means no limit).
-> TextZipper a |

Create a zipper using a custom text storage type. Takes the initial text as well as all of the functions necessary to manipulate the underlying text values.

textZipper :: [Text] -> Maybe Int -> TextZipper Text Source #

Construct a zipper from `Text` values.

stringZipper :: [String] -> Maybe Int -> TextZipper String Source #

Construct a zipper from list values.

clearZipper :: Monoid a => TextZipper a -> TextZipper a Source #

Empty a zipper.

vectorZipper :: [Vector Char] -> Maybe Int -> TextZipper (Vector Char) Source #

Construct a zipper from vectors of characters.

getText :: Monoid a => TextZipper a -> [a] Source #

Get the text contents of the zipper.

currentLine :: Monoid a => TextZipper a -> a Source #

The line of text on which the zipper's cursor currently resides.

cursorPosition :: TextZipper a -> (Int, Int) Source #

Get the cursor position of the zipper; returns `(row, col)`. `row` ranges from `[0..num_rows-1]` inclusive; `col` ranges from `[0..length of current line]` inclusive. Column values equal to line width indicate a cursor that is just past the end of a line of text.

lineLengths :: Monoid a => TextZipper a -> [Int] Source #

Return the lengths of the lines in the zipper.

getLineLimit :: TextZipper a -> Maybe Int Source #

Get the line limit, if any, for a zipper.

# Navigation functions

moveCursor :: Monoid a => (Int, Int) -> TextZipper a -> TextZipper a Source #

Move the cursor to the specified row and column. Invalid cursor positions will be ignored. Valid cursor positions range as described for `cursorPosition`.

moveCursorClosest :: Monoid a => (Int, Int) -> TextZipper a -> TextZipper a Source #

Move the cursor to the specified row and column. Invalid cursor positions will be reinterpreted as the closest valid position. Valid cursor positions range as described for `cursorPosition`.

moveRight :: Monoid a => TextZipper a -> TextZipper a Source #

Move the cursor right by one position. If the cursor is at the end of a line, the cursor is moved to the first position of the following line (if any).

moveLeft :: Monoid a => TextZipper a -> TextZipper a Source #

Move the cursor left by one position. If the cursor is at the beginning of a line, the cursor is moved to the last position of the preceding line (if any).

moveUp :: Monoid a => TextZipper a -> TextZipper a Source #

Move the cursor up by one row. If there no are rows above the current one, move to the first position of the current row. If the row above is shorter, move to the end of that row.

moveDown :: Monoid a => TextZipper a -> TextZipper a Source #

Move the cursor down by one row. If there are no rows below the current one, move to the last position of the current row. If the row below is shorter, move to the end of that row.

gotoEOL :: Monoid a => TextZipper a -> TextZipper a Source #

Move the cursor to the end of the current line.

gotoBOL :: Monoid a => TextZipper a -> TextZipper a Source #

Move the cursor to the beginning of the current line.

gotoEOF :: Monoid a => TextZipper a -> TextZipper a Source #

Move the cursor to the end of a text zipper.

gotoBOF :: Monoid a => TextZipper a -> TextZipper a Source #

Move the cursor to the beginning of a text zipper.

# Inspection functions

currentChar :: TextZipper a -> Maybe Char Source #

Get the Char on which the cursor currently resides. If the cursor is at the end of the text or the text is empty return `Nothing`.

nextChar :: Monoid a => TextZipper a -> Maybe Char Source #

Get the Char after the cursor position. If the cursor is at the end of a line return the first character of the next line, or if that one is empty as well, return `Nothing`.

previousChar :: Monoid a => TextZipper a -> Maybe Char Source #

Get the Char before the cursor position. If the cursor is at the beginning of the text, return `Nothing`

# Editing functions

insertChar :: Monoid a => Char -> TextZipper a -> TextZipper a Source #

Insert a character at the current cursor position.

If the character is a newline, break the current line.

If the character is non-printable, ignore it.

Otherwise insert the character and move the cursor one position to the right.

insertMany :: Monoid a => a -> TextZipper a -> TextZipper a Source #

Insert many characters at the current cursor position. Move the cursor to the end of the inserted text.

deletePrevChar :: (Eq a, Monoid a) => TextZipper a -> TextZipper a Source #

Delete the character preceding the cursor position, and move the cursor backwards by one character.

deleteChar :: Monoid a => TextZipper a -> TextZipper a Source #

Delete the character at the cursor position. Leaves the cursor position unchanged. If the cursor is at the end of a line of text, this combines the line with the line below.

breakLine :: Monoid a => TextZipper a -> TextZipper a Source #

Insert a line break at the current cursor position.

killToEOL :: Monoid a => TextZipper a -> TextZipper a Source #

Remove all text from the cursor position to the end of the current line. If the cursor is at the beginning of a line and the line is empty, the entire line will be removed.

killToBOL :: Monoid a => TextZipper a -> TextZipper a Source #

Remove all text from the cursor position to the beginning of the current line.

killToEOF :: Monoid a => TextZipper a -> TextZipper a Source #

Remove all text from the cursor position to the end of the text zipper. If the cursor is at the beginning of a line and the line is empty, the entire line will be removed.

killToBOF :: Monoid a => TextZipper a -> TextZipper a Source #

Remove all text from the cursor position to the beginning of the text zipper.

transposeChars :: Monoid a => TextZipper a -> TextZipper a Source #

Transpose the character before the cursor with the one at the cursor position and move the cursor one position to the right. If the cursor is at the end of the current line, transpose the current line's last two characters.