Skip to content

Commit

Permalink
Move Hclip to yi-core
Browse files Browse the repository at this point in the history
Add disable-hclip function
  • Loading branch information
junjihashimoto committed Jun 12, 2018
1 parent 2fa57e3 commit d151e47
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 14 deletions.
46 changes: 46 additions & 0 deletions yi-core/src/Yi/Clip.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{-# OPTIONS_HADDOCK show-extensions #-}

-- |
-- Module : Yi.Clip
-- License : GPL-2
-- Maintainer : [email protected]
-- Stability : experimental
-- Portability : portable
--
-- A proxy of clipboard.

module Yi.Clip ( setClipboard
, getClipboard
)
where

import qualified System.Hclip as H (getClipboard, setClipboard)
import Data.IORef
import System.IO.Unsafe

import Yi.Types (configDisableSystemClipboard, askCfg)
import Yi.Utils (io)
import Yi.Keymap (YiM)

clipboard :: IORef String
clipboard = unsafePerformIO $ newIORef ""

getClipboard' :: IO String
getClipboard' = readIORef clipboard

setClipboard' :: String -> IO ()
setClipboard' = writeIORef clipboard

getClipboard :: YiM String
getClipboard = do
config <- askCfg
if configDisableSystemClipboard config
then io getClipboard'
else io H.getClipboard

setClipboard :: String -> YiM ()
setClipboard text = do
config <- askCfg
if configDisableSystemClipboard config
then io $ setClipboard' text
else io $ H.setClipboard text
1 change: 1 addition & 0 deletions yi-core/src/Yi/Config/Default.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ defaultConfig =
, bufferUpdateHandler = mempty
, layoutManagers = [hPairNStack 1, vPairNStack 1, tall, wide]
, configVars = mempty
, configDisableSystemClipboard = False
}

nilKeymap :: Keymap
Expand Down
3 changes: 2 additions & 1 deletion yi-core/src/Yi/Config/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ import Yi.Config(Config, UIConfig, startFrontEndA, configUIA,
CursorStyle(..), configLeftSideScrollBarA,
configAutoHideScrollBarA, configAutoHideTabBarA,
configLineWrapA, configWindowFillA, configThemeA,
layoutManagersA, configVarsA, configLineNumbersA
layoutManagersA, configVarsA, configLineNumbersA,
configDisableSystemClipboardA
)


Expand Down
5 changes: 4 additions & 1 deletion yi-core/src/Yi/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,11 @@ data Config = Config {startFrontEnd :: UIBoot,
bufferUpdateHandler :: !(S.Seq (S.Seq Update -> BufferM ())),
layoutManagers :: ![AnyLayoutManager],
-- ^ List of layout managers for 'cycleLayoutManagersNext'
configVars :: !ConfigState.DynamicState
configVars :: !ConfigState.DynamicState,
-- ^ Custom configuration, containing the 'YiConfigVariable's. Configure with 'configVariableA'.
configDisableSystemClipboard :: !Bool
-- ^ Set to 'True' not to use system clipboard.
-- When vty-mode, system clipboard is not available in some environments.
}


Expand Down
2 changes: 2 additions & 0 deletions yi-core/yi-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ library
, yi-language >= 0.17
, yi-rope >= 0.10
, exceptions
, Hclip
if flag(hint)
cpp-options: -DHINT
build-depends:
Expand All @@ -74,6 +75,7 @@ library
Yi.Buffer.Region
Yi.Buffer.TextUnit
Yi.Buffer.Undo
Yi.Clip
Yi.Command
Yi.Command.Help
Yi.Completion
Expand Down
6 changes: 3 additions & 3 deletions yi-keymap-emacs/src/Yi/Keymap/Emacs/KillRing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import Yi.Buffer
import Yi.Editor (EditorM, killringA, withCurrentBuffer)
import Yi.Keymap (YiM)
import Yi.KillRing (Killring (_krContents), krKilled, krPut)
import Yi.Clip (getClipboard, setClipboard)
import qualified Yi.Rope as R (YiString, fromString, toString)
import Yi.Types (withEditor)
import Yi.Utils (io)
import System.Hclip (getClipboard, setClipboard)

uses :: forall a b f s. MonadState s f => Getting a s a -> (a -> b) -> f b
uses l f = f <$> use l
Expand All @@ -32,7 +32,7 @@ uses l f = f <$> use l
-- | Adds system clipboard's contents on top of the killring if not already there
clipboardToKillring :: YiM ()
clipboardToKillring = do
text <- fmap R.fromString $ io getClipboard
text <- fmap R.fromString $ getClipboard
withEditor $ do
text' <- killringGet
when (text' /= text) $ killringPut Forward text
Expand All @@ -41,7 +41,7 @@ clipboardToKillring = do
killringToClipboard :: YiM ()
killringToClipboard = do
text <- withEditor killringGet
io . setClipboard $ R.toString text
setClipboard $ R.toString text

-- This is like @kill-region-or-backward-word@.
killRegionB :: BufferM ()
Expand Down
1 change: 0 additions & 1 deletion yi-keymap-emacs/yi-keymap-emacs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ library
base >= 4.8 && < 5
, containers
, filepath
, Hclip
, microlens-platform
, mtl
, oo-prototypes
Expand Down
4 changes: 2 additions & 2 deletions yi-keymap-vim/src/Yi/Keymap/Vim/Ex/Commands/Copy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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.Clip (setClipboard)
import Yi.Core (errorEditor)

parse :: EventString -> Maybe ExCommand
Expand All @@ -38,5 +38,5 @@ parse = Common.parse $ do
copy :: Maybe (BufferM Region) -> YiM ()
copy maybeGetRegion = case maybeGetRegion of
Nothing -> errorEditor "Cannot copy: No region"
Just getRegion -> liftBase . setClipboard . toString
Just getRegion -> setClipboard . toString
=<< withCurrentBuffer (readRegionB =<< getRegion)
7 changes: 3 additions & 4 deletions yi-keymap-vim/src/Yi/Keymap/Vim/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import Yi.Monad (whenM)
import Yi.Rope (YiString, countNewLines, last)
import qualified Yi.Rope as R (replicateChar, snoc, toString, fromString)
import Yi.Utils (io)
import System.Hclip (getClipboard, setClipboard)
import Yi.Clip (getClipboard, setClipboard)

-- 'mkBindingE' and 'mkBindingY' are helper functions for bindings
-- where VimState mutation is not dependent on action performed
Expand Down Expand Up @@ -208,11 +208,10 @@ addNewLineIfNecessary rope =

pasteFromClipboard :: YiM ()
pasteFromClipboard = do
text <- fmap R.fromString $ io getClipboard
text <- fmap R.fromString $ getClipboard
withCurrentBuffer $ insertRopeWithStyleB text Inclusive

exportRegisterToClipboard :: RegisterName -> YiM ()
exportRegisterToClipboard name = do
mbr <- withEditor $ getRegisterE name
io . setClipboard $ maybe "" (R.toString . regContent) mbr

setClipboard $ maybe "" (R.toString . regContent) mbr
2 changes: 0 additions & 2 deletions yi-keymap-vim/yi-keymap-vim.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ library
, data-default
, directory
, filepath
, Hclip
, microlens-platform
, mtl
, oo-prototypes
Expand Down Expand Up @@ -141,7 +140,6 @@ test-suite spec
, data-default
, directory
, filepath
, Hclip
, microlens-platform
, mtl
, oo-prototypes
Expand Down

0 comments on commit d151e47

Please sign in to comment.