{-# 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
    (Text
regionText, Maybe (BufferM Region)
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
    Parser Text Text -> Parser Text ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Text -> Parser Text Text
P.string Text
"copy")
    ExCommand -> Parser ExCommand
forall (m :: * -> *) a. Monad m => a -> m a
return (ExCommand -> Parser ExCommand) -> ExCommand -> Parser ExCommand
forall a b. (a -> b) -> a -> b
$ ExCommand
Common.impureExCommand {
        cmdShow :: Text
cmdShow = Text
regionText Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"copy"
      , cmdAction :: Action
cmdAction = YiM () -> Action
forall a. Show a => YiM a -> Action
YiA (Maybe (BufferM Region) -> YiM ()
copy Maybe (BufferM Region)
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 (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)