Skip to content

Commit

Permalink
Allow vim to close dired &c buffers
Browse files Browse the repository at this point in the history
In general it can now close more stuff

Fixes #649
  • Loading branch information
Fuuzetsu committed Oct 24, 2014
1 parent 65ee5f1 commit 2369518
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -6,6 +6,7 @@
* Fix eolPoint' when on last, empty line (#667, #662)
* More intuitive justOneSep (emacs M-SPC) (#665)
* Closer behaviour to emacs C-x 0 (#642)
* Allow vim to close dired &c buffers (#649)

0.10.0
------
Expand Down
18 changes: 17 additions & 1 deletion yi/src/library/Yi/File.hs
Expand Up @@ -23,9 +23,11 @@ module Yi.File (
revertE, -- :: YiM ()

-- * Helper functions
setFileName
setFileName,
deservesSave
) where

import Control.Applicative
import Control.Lens hiding (act)
import Control.Monad (void)
import Control.Monad.Base
Expand Down Expand Up @@ -151,3 +153,17 @@ setFileName :: BufferRef -> FilePath -> YiM ()
setFileName b filename = do
cfn <- liftBase $ userToCanonPath filename
withGivenBuffer b $ assign identA $ FileBuffer cfn

-- | Checks if the given buffer deserves a save: whether it's a file
-- buffer and whether it's pointing at a file rather than a directory.
deservesSave :: FBuffer -> YiM Bool
deservesSave b
| isUnchangedBuffer b = return False
| otherwise = isFileBuffer b

-- | Is there a proper file associated with the buffer?
-- In other words, does it make sense to offer to save it?
isFileBuffer :: FBuffer -> YiM Bool
isFileBuffer b = case b ^. identA of
MemBuffer _ -> return False
FileBuffer fn -> not <$> liftBase (doesDirectoryExist fn)
12 changes: 0 additions & 12 deletions yi/src/library/Yi/Keymap/Emacs/Utils.hs
Expand Up @@ -98,18 +98,6 @@ askSaveEditor = askIndividualSave False =<< getModifiedBuffers
getModifiedBuffers :: YiM [FBuffer]
getModifiedBuffers = filterM deservesSave =<< gets bufferSet

deservesSave :: FBuffer -> YiM Bool
deservesSave b
| isUnchangedBuffer b = return False
| otherwise = isFileBuffer b

-- | Is there a proper file associated with the buffer?
-- In other words, does it make sense to offer to save it?
isFileBuffer :: (Functor m, MonadBase IO m) => FBuffer -> m Bool
isFileBuffer b = case b ^. identA of
MemBuffer _ -> return False
FileBuffer fn -> not <$> liftBase (doesDirectoryExist fn)

--------------------------------------------------
-- Takes in a list of buffers which have been identified
-- as modified since their last save.
Expand Down
19 changes: 7 additions & 12 deletions yi/src/library/Yi/Keymap/Vim/Ex/Commands/Quit.hs
Expand Up @@ -20,6 +20,7 @@ import Control.Monad
import Data.Foldable (find)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.PointedList.Circular as PL
import Data.Maybe
import Data.Monoid
import qualified Data.Text as T
import qualified Text.ParserCombinators.Parsec as P
Expand Down Expand Up @@ -71,7 +72,7 @@ action True True True = saveAndQuitAllE

quitWindowE :: YiM ()
quitWindowE = do
nw <- withCurrentBuffer needsAWindowB
nw <- gets currentBuffer >>= needsSaving
ws <- withEditor $ use currentWindowA >>= windowsOnBufferE . bufkey
if length ws == 1 && nw
then errorEditor "No write since last change (add ! to override)"
Expand All @@ -85,25 +86,19 @@ quitWindowE = do

quitAllE :: YiM ()
quitAllE = do
a :| as <- readEditor bufferStack
let needsWindow b = (b,) <$> withEditor (withGivenBuffer b needsAWindowB)
bs <- mapM needsWindow (a:as)
let needsWindow b = (b,) <$> deservesSave b
bs <- readEditor bufferSet >>= mapM needsWindow
-- Vim only shows the first modified buffer in the error.
case find snd bs of
Nothing -> quitEditor
Just (b, _) -> do
bufferName <- withEditor $ withGivenBuffer b $ gets file
bufferName <- withEditor $ withGivenBuffer (bkey b) $ gets file
errorEditor $ "No write since last change for buffer "
<> showT bufferName
<> " (add ! to override)"

saveAndQuitAllE :: YiM ()
saveAndQuitAllE = Common.forAllBuffers fwriteBufferE >> quitEditor

needsAWindowB :: BufferM Bool
needsAWindowB = do
isWorthless <- gets (^. identA) >>= return . \case
MemBuffer _ -> True
FileBuffer _ -> False
canClose <- gets isUnchangedBuffer
return (not (isWorthless || canClose))
needsSaving :: BufferRef -> YiM Bool
needsSaving = findBuffer >=> maybe (return False) deservesSave

0 comments on commit 2369518

Please sign in to comment.