module Data.Text.Zipper.Generic.Words
( moveWordLeft
, moveWordRight
, deletePrevWord
, deleteWord
)
where
import Data.Char
import Data.Text.Zipper
import qualified Data.Text.Zipper.Generic as TZ
moveWordLeft :: TZ.GenericTextZipper a => TextZipper a -> TextZipper a
moveWordLeft :: forall a. GenericTextZipper a => TextZipper a -> TextZipper a
moveWordLeft = forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordLeft Bool
False forall a. Monoid a => TextZipper a -> TextZipper a
moveLeft
deletePrevWord :: (Eq a, TZ.GenericTextZipper a) => TextZipper a -> TextZipper a
deletePrevWord :: forall a.
(Eq a, GenericTextZipper a) =>
TextZipper a -> TextZipper a
deletePrevWord = forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordLeft Bool
False forall a. (Eq a, Monoid a) => TextZipper a -> TextZipper a
deletePrevChar
doWordLeft :: TZ.GenericTextZipper a
=> Bool
-> (TextZipper a -> TextZipper a)
-> TextZipper a
-> TextZipper a
doWordLeft :: forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordLeft Bool
inWord TextZipper a -> TextZipper a
transform TextZipper a
zipper = case forall a. GenericTextZipper a => TextZipper a -> Maybe Char
charToTheLeft TextZipper a
zipper of
Maybe Char
Nothing -> TextZipper a
zipper
Just Char
c
| Char -> Bool
isSpace Char
c Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
inWord ->
forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordLeft Bool
False TextZipper a -> TextZipper a
transform (TextZipper a -> TextZipper a
transform TextZipper a
zipper)
| Bool -> Bool
not (Char -> Bool
isSpace Char
c) Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
inWord ->
forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordLeft Bool
True TextZipper a -> TextZipper a
transform TextZipper a
zipper
| Bool -> Bool
not (Char -> Bool
isSpace Char
c) Bool -> Bool -> Bool
&& Bool
inWord ->
forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordLeft Bool
True TextZipper a -> TextZipper a
transform (TextZipper a -> TextZipper a
transform TextZipper a
zipper)
| Bool
otherwise ->
TextZipper a
zipper
moveWordRight :: TZ.GenericTextZipper a => TextZipper a -> TextZipper a
moveWordRight :: forall a. GenericTextZipper a => TextZipper a -> TextZipper a
moveWordRight = forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordRight Bool
False forall a. Monoid a => TextZipper a -> TextZipper a
moveRight
deleteWord :: TZ.GenericTextZipper a => TextZipper a -> TextZipper a
deleteWord :: forall a. GenericTextZipper a => TextZipper a -> TextZipper a
deleteWord = forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordRight Bool
False forall a. Monoid a => TextZipper a -> TextZipper a
deleteChar
doWordRight :: TZ.GenericTextZipper a
=> Bool
-> (TextZipper a -> TextZipper a)
-> TextZipper a
-> TextZipper a
doWordRight :: forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordRight Bool
inWord TextZipper a -> TextZipper a
transform TextZipper a
zipper = case forall a. GenericTextZipper a => TextZipper a -> Maybe Char
charToTheRight TextZipper a
zipper of
Maybe Char
Nothing -> TextZipper a
zipper
Just Char
c
| Char -> Bool
isSpace Char
c Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
inWord ->
forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordRight Bool
False TextZipper a -> TextZipper a
transform (TextZipper a -> TextZipper a
transform TextZipper a
zipper)
| Bool -> Bool
not (Char -> Bool
isSpace Char
c) Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
inWord ->
forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordRight Bool
True TextZipper a -> TextZipper a
transform TextZipper a
zipper
| Bool -> Bool
not (Char -> Bool
isSpace Char
c) Bool -> Bool -> Bool
&& Bool
inWord ->
forall a.
GenericTextZipper a =>
Bool
-> (TextZipper a -> TextZipper a) -> TextZipper a -> TextZipper a
doWordRight Bool
True TextZipper a -> TextZipper a
transform (TextZipper a -> TextZipper a
transform TextZipper a
zipper)
| Bool
otherwise ->
TextZipper a
zipper
charToTheLeft :: TZ.GenericTextZipper a => TextZipper a -> Maybe Char
charToTheLeft :: forall a. GenericTextZipper a => TextZipper a -> Maybe Char
charToTheLeft TextZipper a
zipper = case forall a. TextZipper a -> (Int, Int)
cursorPosition TextZipper a
zipper of
(Int
0, Int
0) -> forall a. Maybe a
Nothing
(Int
_, Int
0) -> forall a. a -> Maybe a
Just Char
'\n'
(Int
_, Int
x) -> forall a. a -> Maybe a
Just (forall a. GenericTextZipper a => a -> [Char]
TZ.toList (forall a. Monoid a => TextZipper a -> a
currentLine TextZipper a
zipper) forall a. [a] -> Int -> a
!! (Int
xforall a. Num a => a -> a -> a
-Int
1))
charToTheRight :: TZ.GenericTextZipper a => TextZipper a -> Maybe Char
charToTheRight :: forall a. GenericTextZipper a => TextZipper a -> Maybe Char
charToTheRight TextZipper a
zipper
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null (forall a. Monoid a => TextZipper a -> [a]
getText TextZipper a
zipper) = forall a. Maybe a
Nothing
| Bool
otherwise =
let
(Int
row, Int
col) = forall a. TextZipper a -> (Int, Int)
cursorPosition TextZipper a
zipper
content :: [a]
content = forall a. Monoid a => TextZipper a -> [a]
getText TextZipper a
zipper
curLine :: a
curLine = [a]
content forall a. [a] -> Int -> a
!! Int
row
numLines :: Int
numLines = forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
content
in
if Int
row forall a. Eq a => a -> a -> Bool
== Int
numLines forall a. Num a => a -> a -> a
- Int
1 Bool -> Bool -> Bool
&& Int
col forall a. Eq a => a -> a -> Bool
== (forall a. GenericTextZipper a => a -> Int
TZ.length a
curLine) then
forall a. Maybe a
Nothing
else if Int
col forall a. Eq a => a -> a -> Bool
== (forall a. GenericTextZipper a => a -> Int
TZ.length a
curLine) then
forall a. a -> Maybe a
Just Char
'\n'
else
forall a. a -> Maybe a
Just (forall a. GenericTextZipper a => a -> [Char]
TZ.toList a
curLine forall a. [a] -> Int -> a
!! Int
col)