module Yi.File (
editFile,
openingNewFile,
viWrite, viWriteTo, viSafeWriteTo,
fwriteE,
fwriteBufferE,
fwriteAllE,
fwriteToE,
backupE,
revertE,
setFileName,
deservesSave
) where
import Control.Applicative
import Control.Lens hiding (act)
import Control.Monad (void)
import Control.Monad.Base
import Data.Monoid
import qualified Data.Text as T
import Data.Time
import System.Directory
import System.FriendlyPath
import Yi.Buffer
import Yi.Core
import Yi.Dired
import Yi.Editor
import Yi.Keymap
import Yi.Monad
import qualified Yi.Rope as R
import Yi.String
import Yi.Utils
openingNewFile :: FilePath -> BufferM a -> YiM ()
openingNewFile fp act = editFile fp >>= \case
Left m -> printMsg m
Right ref -> void $ withGivenBuffer ref act
revertE :: YiM ()
revertE = do
withCurrentBuffer (gets file) >>= \case
Just fp -> do
now <- io getCurrentTime
rf <- liftBase $ R.readFile fp >>= \case
Left m -> print ("Can't revert: " <> m) >> return Nothing
Right (c, cv) -> return $ Just (c, Just cv)
case rf of
Nothing -> return ()
Just (s, conv) -> do
withCurrentBuffer $ revertB s conv now
printMsg ("Reverted from " <> showT fp)
Nothing -> printMsg "Can't revert, no file associated with buffer."
viWrite :: YiM ()
viWrite = do
withCurrentBuffer (gets file) >>= \case
Nothing -> errorEditor "no file name associate with buffer"
Just f -> do
bufInfo <- withCurrentBuffer bufInfoB
let s = bufInfoFileName bufInfo
fwriteE
let message = (showT f <>) (if f == s
then " written"
else " " <> showT s <> " written")
printMsg message
viWriteTo :: T.Text -> YiM ()
viWriteTo f = do
bufInfo <- withCurrentBuffer bufInfoB
let s = T.pack $ bufInfoFileName bufInfo
fwriteToE f
let message = f `T.append` if f == s
then " written"
else ' ' `T.cons` s `T.append` " written"
printMsg message
viSafeWriteTo :: T.Text -> YiM ()
viSafeWriteTo f = do
existsF <- liftBase $ doesFileExist (T.unpack 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 $ do
fl <- gets file
st <- streamB Forward 0
conv <- use encodingConverterNameA
return (fl, st, conv)
case nameContents of
(Just f, contents, conv) -> io (doesDirectoryExist f) >>= \case
True -> printMsg "Can't save over a directory, doing nothing."
False -> do
liftBase $ case conv of
Nothing -> R.writeFileUsingText f contents
Just cn -> R.writeFile f contents cn
io getCurrentTime >>= withGivenBuffer bufferKey . markSavedB
(Nothing, _, _) -> printMsg "Buffer not associated with a file"
fwriteToE :: T.Text -> YiM ()
fwriteToE f = do
b <- gets currentBuffer
setFileName b (T.unpack 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 <- liftBase $ userToCanonPath filename
withGivenBuffer b $ assign identA $ FileBuffer cfn
deservesSave :: FBuffer -> YiM Bool
deservesSave b
| isUnchangedBuffer b = return False
| otherwise = isFileBuffer b
isFileBuffer :: FBuffer -> YiM Bool
isFileBuffer b = case b ^. identA of
MemBuffer _ -> return False
FileBuffer fn -> not <$> liftBase (doesDirectoryExist fn)