    {-# LANGUAGE OverloadedStrings #-}
    {-# OPTIONS_HADDOCK show-extensions #-}

    -- |
    -- Module      :  Yi.Keymap.Vim.Ex.Commands.Copy
    -- License     :  GPL-2
    -- Maintainer  :  yi-devel@googlegroups.com
    -- Stability   :  experimental
    -- Portability :  portable
    --
    -- :copy ex command to copy selection to the clipboard.
    module Yi.Keymap.Vim.Ex.Commands.Copy (parse) where

    import           Control.Monad                    (void)
    import qualified Data.Attoparsec.Text             as P (match, string)
    import           Data.Monoid                      ((<>))
    import           Yi.Editor                        (withCurrentBuffer)
    import           Yi.Keymap                        (Action (YiA))
    import qualified Yi.Keymap.Vim.Ex.Commands.Common as Common (parse, impureExCommand, parseRange)
    import           Yi.Keymap.Vim.Ex.Types           (ExCommand (cmdAction, cmdShow))
    import           Yi.Keymap.Vim.Common             (EventString)
    import           Yi.Types                         (YiM, BufferM)
    import           Yi.Rope                          (toString)
    import           Yi.Buffer.Region                 (readRegionB, Region)
    import           Control.Monad.Base               (liftBase)
    import           System.Hclip                     (setClipboard)
    import           Yi.Core                          (errorEditor)

    parse :: EventString -> Maybe ExCommand
    parse :: EventString -> Maybe ExCommand
    parse = Parser ExCommand -> EventString -> Maybe ExCommand
    Common.parse (Parser ExCommand -> EventString -> Maybe ExCommand)
    -> Parser ExCommand -> EventString -> Maybe ExCommand
    forall a b. (a -> b) -> a -> b
    $ do
        (regionText, region) <- Parser (Maybe (BufferM Region))
    -> Parser (Text, Maybe (BufferM Region))
    forall a. Parser a -> Parser (Text, a)
    P.match Parser (Maybe (BufferM Region))
    Common.parseRange
        void (P.string "copy")
        return $ Common.impureExCommand {
            cmdShow = regionText <> "copy"
          , cmdAction = YiA (copy region)
          }

    copy :: Maybe (BufferM Region) -> YiM ()
    copy :: Maybe (BufferM Region) -> YiM ()
    copy Maybe (BufferM Region)
    maybeGetRegion = case Maybe (BufferM Region)
    maybeGetRegion of
        Maybe (BufferM Region)
    Nothing -> Text -> YiM ()
    errorEditor Text
    "Cannot copy: No region"
        Just BufferM Region
    getRegion -> IO () -> YiM ()
    forall α. IO α -> YiM α
    forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
    liftBase (IO () -> YiM ()) -> (YiString -> IO ()) -> YiString -> YiM ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> IO ()
    setClipboard (String -> IO ()) -> (YiString -> String) -> YiString -> IO ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . YiString -> String
    toString
          (YiString -> YiM ()) -> YiM YiString -> YiM ()
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
    =<< BufferM YiString -> YiM YiString
    forall (m :: * -> *) a. MonadEditor m => BufferM a -> m a
    withCurrentBuffer (Region -> BufferM YiString
    readRegionB (Region -> BufferM YiString) -> BufferM Region -> BufferM YiString
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
    =<< BufferM Region
    getRegion)