Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disable-hclip function #1089

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions yi-core/src/Yi/Clip.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{-# OPTIONS_HADDOCK show-extensions #-}
{-# LANGUAGE OverloadedStrings #-}

-- |
-- 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)
import qualified Yi.Rope as R (YiString, fromString, toString)


-- |
-- This is just used for emulating system clipbord.
-- Do not release this IORef variable outside this module.
clipboard :: IORef R.YiString
clipboard = unsafePerformIO $ newIORef ""

getClipboard' :: IO R.YiString
getClipboard' = readIORef clipboard

setClipboard' :: R.YiString -> IO ()
setClipboard' = writeIORef clipboard

getClipboard :: YiM R.YiString
getClipboard = do
config <- askCfg
if configDisableSystemClipboard config
then io getClipboard'
else io $ fmap R.fromString $ H.getClipboard

setClipboard :: R.YiString -> YiM ()
setClipboard text = do
config <- askCfg
if configDisableSystemClipboard config
then io $ setClipboard' text
else io $ H.setClipboard $ R.toString 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
8 changes: 4 additions & 4 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 qualified Yi.Rope as R (YiString, fromString, toString)
import Yi.Clip (getClipboard, setClipboard)
import qualified Yi.Rope as R (YiString)
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 <- 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 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
=<< 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 <- getClipboard
withCurrentBuffer $ insertRopeWithStyleB text Inclusive

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

setClipboard $ maybe "" 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