Permalink
Browse files

Lenses 2nd part

  • Loading branch information...
1 parent 4b7c140 commit e918a9003a948c97488a730fae84ff9c0e93a133 @np np committed Jan 11, 2013
Showing with 418 additions and 477 deletions.
  1. +6 −6 yi/src/library/Yi/Buffer/HighLevel.hs
  2. +1 −1 yi/src/library/Yi/Buffer/Indent.hs
  3. +50 −58 yi/src/library/Yi/Buffer/Misc.hs
  4. +2 −2 yi/src/library/Yi/Buffer/Normal.hs
  5. +1 −1 yi/src/library/Yi/Buffer/Region.hs
  6. +1 −1 yi/src/library/Yi/Command.hs
  7. +1 −1 yi/src/library/Yi/Config.hs
  8. +2 −2 yi/src/library/Yi/Config/Default.hs
  9. +17 −20 yi/src/library/Yi/Config/Simple.hs
  10. +25 −25 yi/src/library/Yi/Core.hs
  11. +80 −79 yi/src/library/Yi/Dired.hs
  12. +11 −9 yi/src/library/Yi/Dynamic.hs
  13. +58 −59 yi/src/library/Yi/Editor.hs
  14. +3 −3 yi/src/library/Yi/Eval.hs
  15. +1 −1 yi/src/library/Yi/File.hs
  16. +11 −13 yi/src/library/Yi/History.hs
  17. +3 −3 yi/src/library/Yi/IReader.hs
  18. +9 −9 yi/src/library/Yi/Keymap/Cua.hs
  19. +3 −3 yi/src/library/Yi/Keymap/Emacs.hs
  20. +7 −7 yi/src/library/Yi/Keymap/Emacs/KillRing.hs
  21. +2 −2 yi/src/library/Yi/Keymap/Emacs/Utils.hs
  22. +27 −26 yi/src/library/Yi/Keymap/Vim.hs
  23. +8 −7 yi/src/library/Yi/Layout.hs
  24. +1 −1 yi/src/library/Yi/Lexer/Alex.hs
  25. +3 −3 yi/src/library/Yi/MiniBuffer.hs
  26. +1 −1 yi/src/library/Yi/Mode/Abella.hs
  27. +4 −4 yi/src/library/Yi/Mode/Buffers.hs
  28. +1 −1 yi/src/library/Yi/Mode/Compilation.hs
  29. +3 −3 yi/src/library/Yi/Mode/Haskell/Dollarify.hs
  30. +2 −2 yi/src/library/Yi/Mode/IReader.hs
  31. +1 −1 yi/src/library/Yi/Mode/Interactive.hs
  32. +1 −1 yi/src/library/Yi/Mode/JavaScript.hs
  33. +0 −5 yi/src/library/Yi/Monad.hs
  34. +6 −6 yi/src/library/Yi/PersistentState.hs
  35. +11 −55 yi/src/library/Yi/Prelude.hs
  36. +7 −7 yi/src/library/Yi/Search.hs
  37. +10 −10 yi/src/library/Yi/Snippets.hs
  38. +1 −1 yi/src/library/Yi/Syntax/Haskell.hs
  39. +1 −1 yi/src/library/Yi/Syntax/Tree.hs
  40. +15 −15 yi/src/library/Yi/Tab.hs
  41. +4 −4 yi/src/library/Yi/UI/Pango.hs
  42. +9 −9 yi/src/library/Yi/UI/Pango/Control.hs
  43. +2 −2 yi/src/library/Yi/UI/Utils.hs
  44. +6 −7 yi/src/library/Yi/UI/Vty.hs
@@ -10,7 +10,7 @@ import qualified Data.Rope as R
import Data.Maybe (fromMaybe, listToMaybe)
import Data.Time (UTCTime)
import Prelude (FilePath, map)
-import Yi.Prelude
+import Yi.Prelude hiding (moveTo)
import Yi.Buffer.Basic
import Yi.Buffer.Misc
@@ -364,12 +364,12 @@ scrollB n = do
discard $ gotoLnFrom n
setMarkPointB fr =<< pointB
w <- askWindow wkey
- modA pointFollowsWindowA (\old w' -> if w == w' then True else old w')
+ pointFollowsWindowA %= (\old w' -> if w == w' then True else old w')
-- | Move the point to inside the viewable region
snapInsB :: BufferM ()
snapInsB = do
- movePoint <- getA pointFollowsWindowA
+ movePoint <- use pointFollowsWindowA
w <- askWindow wkey
when (movePoint w) $ do
r <- winRegionB
@@ -395,7 +395,7 @@ pointScreenRelPosition _ _ _ = Within -- just to disable the non-exhaustive patt
-- | Move the visible region to include the point
snapScreenB :: Maybe ScrollStyle ->BufferM Bool
snapScreenB style = do
- movePoint <- getA pointFollowsWindowA
+ movePoint <- use pointFollowsWindowA
w <- askWindow wkey
if movePoint w then return False else do
inWin <- pointInWindowB =<< pointB
@@ -454,15 +454,15 @@ getRawestSelectRegionB = do
-- | Return the empty region if the selection is not visible.
getRawSelectRegionB :: BufferM Region
getRawSelectRegionB = do
- s <- getA highlightSelectionA
+ s <- use highlightSelectionA
if s then getRawestSelectRegionB else do
p <- pointB
return $ mkRegion p p
-- | Get the current region boundaries. Extended to the current selection unit.
getSelectRegionB :: BufferM Region
getSelectRegionB = do
- regionStyle <- getA regionStyleA
+ regionStyle <- use regionStyleA
r <- getRawSelectRegionB
mkRegionOfStyleB (regionStart r) (regionEnd r) regionStyle
@@ -10,7 +10,7 @@ module Yi.Buffer.Indent where
import Yi.Buffer.Basic
import Yi.Buffer.Misc
import Yi.Buffer.HighLevel
-import Yi.Prelude
+import Yi.Prelude hiding (moveTo)
import Yi.Buffer.Normal
import Yi.Buffer.Region
import Prelude ()
@@ -78,8 +78,7 @@ module Yi.Buffer.Misc
, savingPrefCol
, forgetPreferCol
, movingToPrefCol
- , getPrefCol
- , setPrefCol
+ , preferColA
, markSavedB
, addOverlayB
, delOverlayB
@@ -145,7 +144,7 @@ module Yi.Buffer.Misc
where
import Prelude (drop)
-import Yi.Prelude
+import Yi.Prelude hiding (Action, moveTo)
import Yi.Region
import System.FilePath
import Yi.Buffer.Implementation
@@ -204,12 +203,12 @@ $(derive makeBinary ''MarkSet)
data SelectionStyle = SelectionStyle
- { highlightSelection :: !Bool
- , rectangleSelection :: !Bool
+ { _highlightSelection :: !Bool
+ , _rectangleSelection :: !Bool
}
deriving Typeable
-makeLensesWithSuffix "AA" ''SelectionStyle
+makeLenses ''SelectionStyle
type BufferId = Either String FilePath
-- ^ maybe a filename associated with this buffer. Filename is canonicalized.
@@ -273,7 +272,7 @@ miniIdentString b = case b ^. identA of
Right _ -> "MINIFILE:"
Left bufName -> bufName
-identA :: Accessor FBuffer BufferId
+identA :: Lens' FBuffer BufferId
identA = attrsA . identAA
-- unfortunately the dynamic stuff can't be read.
@@ -308,61 +307,55 @@ queryAndModifyRawbuf f (FBuffer f1 f5 f3) =
let (f5', x) = f f5
in (FBuffer f1 f5' f3, x)
-attrsA :: Accessor FBuffer Attributes
+attrsA :: Lens' FBuffer Attributes
attrsA f (FBuffer f1 f2 a) = (\a' -> FBuffer f1 f2 a) <$> f a
-- | Use in readonly!
-lastActiveWindowA :: Accessor FBuffer Window
+lastActiveWindowA :: Lens' FBuffer Window
lastActiveWindowA = attrsA.lastActiveWindowAA
-lastSyncTimeA :: Accessor FBuffer UTCTime
+lastSyncTimeA :: Lens' FBuffer UTCTime
lastSyncTimeA = attrsA.lastSyncTimeAA
-undosA :: Accessor FBuffer URList
+undosA :: Lens' FBuffer URList
undosA = attrsA.undosAA
-readOnlyA :: Accessor FBuffer Bool
+readOnlyA :: Lens' FBuffer Bool
readOnlyA = attrsA.readOnlyAA
-insertingA :: Accessor FBuffer Bool
+insertingA :: Lens' FBuffer Bool
insertingA = attrsA.insertingAA
-pointFollowsWindowA :: Accessor FBuffer (WindowRef -> Bool)
+pointFollowsWindowA :: Lens' FBuffer (WindowRef -> Bool)
pointFollowsWindowA = attrsA.pointFollowsWindowAA
file :: FBuffer -> (Maybe FilePath)
file b = case b^.identA of
Right f -> Just f
_ -> Nothing
-preferColA :: Accessor FBuffer (Maybe Int)
+preferColA :: Lens' FBuffer (Maybe Int)
preferColA = attrsA.preferColAA
-setPrefCol :: Maybe Int -> BufferM ()
-setPrefCol = putA preferColA
-
-getPrefCol :: BufferM (Maybe Int)
-getPrefCol = getA preferColA
-
-bufferDynamicA :: Accessor FBuffer DynamicValues
+bufferDynamicA :: Lens' FBuffer DynamicValues
bufferDynamicA = attrsA.bufferDynamicAA
-pendingUpdatesA :: Accessor FBuffer [UIUpdate]
+pendingUpdatesA :: Lens' FBuffer [UIUpdate]
pendingUpdatesA = attrsA.pendingUpdatesAA
-selectionStyleA :: Accessor FBuffer SelectionStyle
+selectionStyleA :: Lens' FBuffer SelectionStyle
selectionStyleA = attrsA.selectionStyleAA
-highlightSelectionA :: Accessor FBuffer Bool
-highlightSelectionA = selectionStyleA.highlightSelectionAA
+highlightSelectionA :: Lens' FBuffer Bool
+highlightSelectionA = selectionStyleA.highlightSelection
-rectangleSelectionA :: Accessor FBuffer Bool
-rectangleSelectionA = selectionStyleA.rectangleSelectionAA
+rectangleSelectionA :: Lens' FBuffer Bool
+rectangleSelectionA = selectionStyleA.rectangleSelection
-keymapProcessA :: Accessor FBuffer KeymapProcess
+keymapProcessA :: Lens' FBuffer KeymapProcess
keymapProcessA = attrsA.processAA
-winMarksA :: Accessor FBuffer (M.Map WindowRef WinMarks)
+winMarksA :: Lens' FBuffer (M.Map WindowRef WinMarks)
winMarksA = attrsA.winMarksAA
{- | Currently duplicates some of Vim's indent settings. Allowing a buffer to
@@ -458,7 +451,7 @@ defaultModeLine prefix = do
p <- pointB
s <- sizeB
curChar <-readB
- ro <-getA readOnlyA
+ ro <-use readOnlyA
modeNm <- gets (withMode0 modeName)
unchanged <- gets isUnchangedBuffer
let pct = if (pos == 0) || (s == 0)
@@ -505,13 +498,13 @@ queryAndModify f = getsAndModify (queryAndModifyRawbuf f)
-- | Adds an "overlay" to the buffer
addOverlayB :: Overlay -> BufferM ()
addOverlayB ov = do
- modA pendingUpdatesA (++ [overlayUpdate ov])
+ pendingUpdatesA %= (++ [overlayUpdate ov])
modifyBuffer $ addOverlayBI ov
-- | Remove an existing "overlay"
delOverlayB :: Overlay -> BufferM ()
delOverlayB ov = do
- modA pendingUpdatesA (++ [overlayUpdate ov])
+ pendingUpdatesA %= (++ [overlayUpdate ov])
modifyBuffer $ delOverlayBI ov
delOverlayLayerB :: OvlLayer -> BufferM ()
@@ -526,8 +519,7 @@ runBuffer w b f =
in (a, b')
getMarks :: Window -> BufferM (Maybe WinMarks)
-getMarks w = do
- getsA winMarksA (M.lookup $ wkey w)
+getMarks w = uses winMarksA (M.lookup $ wkey w)
runBufferFull :: Window -> FBuffer -> BufferM a -> (a, [Update], FBuffer)
@@ -544,13 +536,13 @@ runBufferFull w b f =
selMark = MarkValue 0 Backward, -- sel
fromMark = MarkValue 0 Backward } -- from
else do
- Just mrks <- getsA winMarksA (M.lookup $ wkey (b ^. lastActiveWindowA))
+ Just mrks <- uses winMarksA (M.lookup $ wkey (b ^. lastActiveWindowA))
forM mrks getMarkValueB
newMrks <- forM newMarkValues newMarkB
- modA winMarksA (M.insert (wkey w) newMrks)
- putA lastActiveWindowA w
+ winMarksA %= M.insert (wkey w) newMrks
+ lastActiveWindowA .= w
f
- in (a, updates, pendingUpdatesA ^: (++ fmap TextUpdate updates) $ b')
+ in (a, updates, b' & pendingUpdatesA %~ (++ fmap TextUpdate updates))
getMarkValueB :: Mark -> BufferM MarkValue
getMarkValueB m = fromMaybe (MarkValue 0 Forward) <$> queryBuffer (getMarkValueBI m)
@@ -569,8 +561,8 @@ runBufferDummyWindow b = fst . runBuffer (dummyWindow $ bkey b) b
-- | Mark the current point in the undo list as a saved state.
markSavedB :: UTCTime -> BufferM ()
-markSavedB t = do modA undosA setSavedFilePointU
- putA lastSyncTimeA t
+markSavedB t = do undosA %= setSavedFilePointU
+ lastSyncTimeA .= t
bkey :: FBuffer -> BufferRef
bkey = view $ attrsA.bkey__AA
@@ -582,9 +574,9 @@ isUnchangedBuffer = isAtSavedFilePointU . view undosA
undoRedo :: (forall syntax. Mark -> URList -> BufferImpl syntax -> (BufferImpl syntax, (URList, [Update])) ) -> BufferM ()
undoRedo f = do
m <- getInsMark
- ur <- getA undosA
+ ur <- use undosA
(ur', updates) <- queryAndModify (f m ur)
- putA undosA ur'
+ undosA .= ur'
tell updates
undoB :: BufferM ()
@@ -692,11 +684,11 @@ moveTo x = do
------------------------------------------------------------------------
setInserting :: Bool -> BufferM ()
-setInserting = putA insertingA
+setInserting = (insertingA .=)
checkRO :: BufferM Bool
checkRO = do
- ro <- getA readOnlyA
+ ro <- use readOnlyA
when ro (fail "Read Only Buffer")
return ro
@@ -708,14 +700,14 @@ applyUpdate update = do
forgetPreferCol
let reversed = reverseUpdateI update
modifyBuffer (applyUpdateI update)
- modA undosA $ addChangeU $ AtomicChange $ reversed
+ undosA %= addChangeU (AtomicChange reversed)
tell [update]
-- otherwise, just ignore.
-- | Revert all the pending updates; don't touch the point.
revertPendingUpdatesB :: BufferM ()
revertPendingUpdatesB = do
- updates <- getA pendingUpdatesA
+ updates <- use pendingUpdatesA
modifyBuffer (flip (foldr (\u bi -> applyUpdateI (reverseUpdateI u) bi)) [u | TextUpdate u <- updates])
-- | Write an element into the buffer at the current point.
@@ -800,15 +792,15 @@ setMode :: Mode syntax -> BufferM ()
setMode m = do
modify (setMode0 m)
-- reset the keymap process so we use the one of the new mode.
- putA keymapProcessA I.End
+ keymapProcessA .= I.End
modeOnLoad m
-- | Modify the mode
modifyMode :: (forall syntax. Mode syntax -> Mode syntax) -> BufferM ()
modifyMode f = do
modify (modifyMode0 f)
-- reset the keymap process so we use the one of the new mode.
- putA keymapProcessA I.End
+ keymapProcessA .= I.End
onMode :: (forall syntax. Mode syntax -> Mode syntax) -> AnyMode -> AnyMode
onMode f (AnyMode m) = AnyMode (f m)
@@ -827,7 +819,7 @@ withSyntax0 f wk (FBuffer bm rb _attrs) = f bm (getAst wk rb)
withSyntaxB :: (forall syntax. Mode syntax -> syntax -> a) -> BufferM a
-withSyntaxB f = withSyntax0 f <$> askWindow wkey <*> getA id
+withSyntaxB f = withSyntax0 f <$> askWindow wkey <*> use id
focusSyntax :: M.Map WindowRef Region -> FBuffer -> FBuffer
@@ -860,7 +852,7 @@ modifyMarkB m f = modifyBuffer $ modifyMarkBI m f
-- | Highlight the selection
setVisibleSelection :: Bool -> BufferM ()
-setVisibleSelection = putA highlightSelectionA
+setVisibleSelection = (highlightSelectionA .=)
getInsMark :: BufferM Mark
getInsMark = insMark <$> askMarks
@@ -912,11 +904,11 @@ lineMoveRel = movingToPrefCol . gotoLnFrom
movingToPrefCol :: BufferM a -> BufferM a
movingToPrefCol f = do
- prefCol <- getPrefCol
+ prefCol <- use preferColA
targetCol <- maybe curCol return prefCol
r <- f
moveToColB targetCol
- setPrefCol $ Just targetCol
+ preferColA .= Just targetCol
return r
moveToColB :: Int -> BufferM ()
@@ -934,13 +926,13 @@ pointOfLineColB :: Int -> Int -> BufferM Point
pointOfLineColB line col = savingPointB $ moveToLineColB line col >> pointB
forgetPreferCol :: BufferM ()
-forgetPreferCol = setPrefCol Nothing
+forgetPreferCol = preferColA .= Nothing
savingPrefCol :: BufferM a -> BufferM a
savingPrefCol f = do
- pc <- getPrefCol
+ pc <- use preferColA
result <- f
- setPrefCol pc
+ preferColA .= pc
return result
-- | Move point up one line
@@ -1016,9 +1008,9 @@ gotoLnFrom x = do
-- | Access to a value into the extensible state, keyed by its type.
-- This allows you to save or retrieve inside a 'BufferM' monad, ie:
--
--- > putA bufferDynamicValueA updatedvalue
+-- > use bufferDynamicValueA updatedvalue
-- > value <- getA bufferDynamicValueA
-bufferDynamicValueA :: YiVariable a => Accessor FBuffer a
+bufferDynamicValueA :: YiVariable a => Lens' FBuffer a
bufferDynamicValueA = bufferDynamicA.dynamicValueA
-- | perform a @BufferM a@, and return to the current point. (by using a mark)
@@ -44,7 +44,7 @@ module Yi.Buffer.Normal (TextUnit(Character, Line, VLine, Document),
) where
import Prelude(length, subtract)
-import Yi.Prelude
+import Yi.Prelude hiding (moveTo)
import Yi.Buffer.Basic
import Yi.Buffer.Misc
import Yi.Buffer.Region
@@ -415,7 +415,7 @@ instance Initializable RegionStyle where
instance YiVariable RegionStyle
-regionStyleA :: Accessor FBuffer RegionStyle
+regionStyleA :: Lens' FBuffer RegionStyle
regionStyleA = bufferDynamicValueA
mkRegionOfStyleB :: Point -> Point -> RegionStyle -> BufferM Region
@@ -23,7 +23,7 @@ where
import Data.Algorithm.Diff
import Yi.Region
import Yi.Buffer.Misc
-import Yi.Prelude
+import Yi.Prelude hiding (moveTo)
import Prelude ()
import Data.List (length, sort)
import Yi.Window (winRegion)
Oops, something went wrong.

0 comments on commit e918a90

Please sign in to comment.