{-# LANGUAGE TemplateHaskell, CPP #-}
module Brick.Widgets.Edit.EmacsBindings
( Editor
, editorText
, getEditContents
, applyEditM
, applyEdit
, editContentsL
, handleEditorEvent
, renderEditor
) where
import Brick
import Graphics.Vty
import qualified Brick.Widgets.Edit as E
import Data.Text.Zipper
import Data.Text (Text)
import Lens.Micro.TH
import Lens.Micro
import Lens.Micro.Mtl
import Data.Text.Zipper.Generic.Words
data Editor n = Editor {
forall n. Editor n -> Editor Text n
_origEditor :: E.Editor Text n,
forall n. Editor n -> [Text] -> Widget n
_drawingFunction :: [Text] -> Widget n
}
makeLenses ''Editor
editorText :: n -> ([Text] -> Widget n)-> Maybe Int -> Text -> Editor n
editorText :: forall n.
n -> ([Text] -> Widget n) -> Maybe Int -> Text -> Editor n
editorText n
name [Text] -> Widget n
draw Maybe Int
linum Text
content =
#if MIN_VERSION_brick(0,19,0)
forall n. Editor Text n -> ([Text] -> Widget n) -> Editor n
Editor (forall n. n -> Maybe Int -> Text -> Editor Text n
E.editorText n
name Maybe Int
linum Text
content) [Text] -> Widget n
draw
#else
Editor (E.editorText name draw linum content) draw
#endif
getEditContents :: Editor n -> [Text]
getEditContents :: forall n. Editor n -> [Text]
getEditContents Editor n
edit = Editor n
edit forall s a. s -> Getting a s a -> a
^. forall n. Lens' (Editor n) (Editor Text n)
origEditor forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. (s -> a) -> SimpleGetter s a
to forall t n. Monoid t => Editor t n -> [t]
E.getEditContents
applyEdit :: (TextZipper Text -> TextZipper Text) -> Editor n -> Editor n
applyEdit :: forall n.
(TextZipper Text -> TextZipper Text) -> Editor n -> Editor n
applyEdit TextZipper Text -> TextZipper Text
f Editor n
edit = Editor n
edit forall a b. a -> (a -> b) -> b
& forall n. Lens' (Editor n) (Editor Text n)
origEditor forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ forall t n.
(TextZipper t -> TextZipper t) -> Editor t n -> Editor t n
E.applyEdit TextZipper Text -> TextZipper Text
f
applyEditM :: (TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM :: forall n.
(TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM TextZipper Text -> TextZipper Text
f = forall n. Lens' (Editor n) (Editor Text n)
origEditor forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= forall t n.
(TextZipper t -> TextZipper t) -> Editor t n -> Editor t n
E.applyEdit TextZipper Text -> TextZipper Text
f
editContentsL :: Lens (Editor n) (Editor n) (TextZipper Text) (TextZipper Text)
editContentsL :: forall n.
Lens (Editor n) (Editor n) (TextZipper Text) (TextZipper Text)
editContentsL = forall n. Lens' (Editor n) (Editor Text n)
origEditor forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t1 n t2.
Lens (Editor t1 n) (Editor t2 n) (TextZipper t1) (TextZipper t2)
E.editContentsL
handleEditorEvent :: Eq n => Event -> EventM n (Editor n) ()
handleEditorEvent :: forall n. Eq n => Event -> EventM n (Editor n) ()
handleEditorEvent Event
event = case Event
event of
EvKey (KChar Char
'f') [Modifier
MCtrl] -> forall n.
(TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM forall a. Monoid a => TextZipper a -> TextZipper a
moveRight
EvKey (KChar Char
'b') [Modifier
MCtrl] -> forall n.
(TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM forall a. Monoid a => TextZipper a -> TextZipper a
moveLeft
EvKey (KChar Char
'f') [Modifier
MMeta] -> forall n.
(TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM forall a. GenericTextZipper a => TextZipper a -> TextZipper a
moveWordRight
EvKey (KChar Char
'b') [Modifier
MMeta] -> forall n.
(TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM forall a. GenericTextZipper a => TextZipper a -> TextZipper a
moveWordLeft
EvKey Key
KBS [Modifier
MMeta] -> forall n.
(TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM forall a.
(Eq a, GenericTextZipper a) =>
TextZipper a -> TextZipper a
deletePrevWord
EvKey (KChar Char
'w') [Modifier
MCtrl] -> forall n.
(TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM forall a.
(Eq a, GenericTextZipper a) =>
TextZipper a -> TextZipper a
deletePrevWord
EvKey (KChar Char
'd') [Modifier
MMeta] -> forall n.
(TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM forall a. GenericTextZipper a => TextZipper a -> TextZipper a
deleteWord
EvKey Key
KHome [] -> forall n.
(TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM forall a. Monoid a => TextZipper a -> TextZipper a
gotoBOL
EvKey Key
KEnd [] -> forall n.
(TextZipper Text -> TextZipper Text) -> EventM n (Editor n) ()
applyEditM forall a. Monoid a => TextZipper a -> TextZipper a
gotoEOL
Event
_ -> forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom forall n. Lens' (Editor n) (Editor Text n)
origEditor forall a b. (a -> b) -> a -> b
$ forall n t e.
(Eq n, DecodeUtf8 t, Eq t, GenericTextZipper t) =>
BrickEvent n e -> EventM n (Editor t n) ()
E.handleEditorEvent (forall n e. Event -> BrickEvent n e
VtyEvent Event
event)
renderEditor :: (Ord n, Show n) => Bool -> Editor n -> Widget n
renderEditor :: forall n. (Ord n, Show n) => Bool -> Editor n -> Widget n
renderEditor Bool
focus Editor n
edit =
#if MIN_VERSION_brick(0,19,0)
forall n t.
(Ord n, Show n, Monoid t, TextWidth t, GenericTextZipper t) =>
([t] -> Widget n) -> Bool -> Editor t n -> Widget n
E.renderEditor (Editor n
editforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Editor n) ([Text] -> Widget n)
drawingFunction) Bool
focus (Editor n
editforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Editor n) (Editor Text n)
origEditor)
#else
E.renderEditor focus (edit^.origEditor)
#endif