module Yi.Command.Help(displayHelpFor) where
import Control.Applicative ((<$>))
import Data.Binary (Binary)
import Data.Default (Default)
import qualified Data.Text as T (Text, pack, unlines, unpack)
import Data.Typeable (Typeable)
import Yi.Buffer (BufferId (MemBuffer), BufferRef)
import Yi.Editor
import Yi.Eval (describeNamedAction, getAllNamesInScope)
import Yi.Keymap (YiM)
import Yi.Monad (maybeM)
import qualified Yi.Rope as R (fromText)
import Yi.Types (YiVariable)
displayHelpFor :: T.Text -> YiM ()
displayHelpFor name = helpFor name >>= displayHelpBuffer
helpFor :: T.Text -> YiM T.Text
helpFor "" = (T.unlines . map T.pack) <$> getAllNamesInScope
helpFor name = T.pack <$> describeNamedAction (T.unpack name)
newtype HelpBuffer = HelpBuffer { helpBuffer :: Maybe BufferRef }
deriving (Default, Typeable, Binary)
instance YiVariable HelpBuffer
displayHelpBuffer :: T.Text -> YiM ()
displayHelpBuffer text = withEditor $ withOtherWindow $ do
maybeM deleteBuffer =<< helpBuffer <$> getEditorDyn
b <- newBufferE (MemBuffer "*help*") $ R.fromText text
putEditorDyn $ HelpBuffer $ Just b