module Yi.Mode.Buffers (listBuffers) where
import Control.Applicative ((<$>))
import Control.Category ((>>>))
import Control.Lens
import Data.List.NonEmpty (toList)
import qualified Data.Text as T
import System.FilePath (takeFileName)
import Yi.Buffer
import Yi.Editor
import Yi.Keymap
import Yi.Keymap.Keys
import qualified Yi.Rope as R
listBuffers :: YiM ()
listBuffers = do
withEditor $ do
bs <- toList <$> getBufferStack
let bufferList = R.fromText . T.intercalate "\n" $ map identString bs
bufRef <- stringToNewBuffer (MemBuffer "Buffer List") bufferList
switchToBufferE bufRef
withCurrentBuffer $ do
modifyMode $ modeKeymapA .~ topKeymapA %~ bufferKeymap
>>> modeNameA .~ "buffers"
assign readOnlyA True
switch :: YiM ()
switch = do
s <- R.toString <$> withCurrentBuffer readLnB
let short = T.pack $ if take 1 s == "/" then takeFileName s else s
withEditor $ switchToBufferWithNameE short
bufferKeymap :: Keymap -> Keymap
bufferKeymap = important $ choice
[ char 'p' ?>>! lineUp
, oneOf [ char 'n', char ' ' ] >>! lineDown
, oneOf [ spec KEnter, char 'f' ] >>! (switch >> setReadOnly False)
, char 'v' ?>>! (switch >> setReadOnly True)
]
where
setReadOnly = withCurrentBuffer . assign readOnlyA