module Yi.File
(
viWrite, viWriteTo, viSafeWriteTo,
fwriteE,
fwriteBufferE,
fwriteAllE,
fwriteToE,
backupE,
revertE,
setFileName,
) where
import Data.Time
import Control.Monad.Trans
import Prelude (filter)
import Yi.Core
import Yi.Buffer (file)
import Control.Monad.State (gets)
import System.Directory
import System.IO.UTF8 as UTF8
import System.FilePath
import System.FriendlyPath
import qualified Data.Rope as R
revertE :: YiM ()
revertE = do
mfp <- withBuffer $ gets file
case mfp of
Just fp -> do
now <- io getCurrentTime
s <- liftIO $ UTF8.readFile fp
withBuffer $ revertB s now
msgEditor ("Reverted from " ++ show fp)
Nothing -> do
msgEditor "Can't revert, no file associated with buffer."
return ()
viWrite :: YiM ()
viWrite = do
mf <- withBuffer $ gets file
case mf of
Nothing -> errorEditor "no file name associate with buffer"
Just f -> do
bufInfo <- withBuffer bufInfoB
let s = bufInfoFileName bufInfo
fwriteE
msgEditor $ show f ++ " " ++ show s ++ " written"
viWriteTo :: String -> YiM ()
viWriteTo f = do
bufInfo <- withBuffer bufInfoB
let s = bufInfoFileName bufInfo
fwriteToE f
msgEditor $ show f++" "++show s ++ " written"
viSafeWriteTo :: String -> YiM ()
viSafeWriteTo f = do
existsF <- liftIO $ doesFileExist f
if existsF
then errorEditor $ f ++ ": File exists (add '!' to override)"
else viWriteTo f
fwriteE :: YiM ()
fwriteE = fwriteBufferE =<< gets currentBuffer
fwriteBufferE :: BufferRef -> YiM ()
fwriteBufferE bufferKey =
do nameContents <- withGivenBuffer bufferKey ((,) <$> gets file <*> streamB Forward 0)
case nameContents of
(Just f, contents) -> do now <- io getCurrentTime
withGivenBuffer bufferKey (markSavedB now)
liftIO $ R.writeFile f contents
(Nothing, _c) -> msgEditor "Buffer not associated with a file"
fwriteToE :: String -> YiM ()
fwriteToE f = do
b <- gets currentBuffer
setFileName b f
fwriteBufferE b
fwriteAllE :: YiM ()
fwriteAllE =
do allBuffs <- gets bufferSet
let modifiedBuffers = filter (not . isUnchangedBuffer) allBuffs
mapM_ fwriteBufferE (fmap bkey modifiedBuffers)
backupE :: FilePath -> YiM ()
backupE = error "backupE not implemented"
setFileName :: BufferRef -> FilePath -> YiM ()
setFileName b filename = do
cfn <- liftIO $ userToCanonPath filename
withGivenBuffer b $ putA identA $ Right cfn