From fc0342261a4c8d3a5c6ba175a999c9dd52609f75 Mon Sep 17 00:00:00 2001 From: Nikolay Yakimov Date: Tue, 9 Feb 2016 12:37:56 +0300 Subject: [PATCH 1/3] Add option for stdio encoding, set stdin encoding --- Language/Haskell/GhcMod/Types.hs | 2 ++ src/GHCMod.hs | 10 ++++++---- src/GHCMod/Options.hs | 6 ++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Language/Haskell/GhcMod/Types.hs b/Language/Haskell/GhcMod/Types.hs index 779c5c93a..42dac13e6 100644 --- a/Language/Haskell/GhcMod/Types.hs +++ b/Language/Haskell/GhcMod/Types.hs @@ -105,6 +105,7 @@ data Options = Options { -- | GHC command line options set on the @ghc-mod@ command line , optGhcUserOptions :: [GHCOption] , optFileMappings :: [(FilePath, Maybe FilePath)] + , optEncoding :: String } deriving (Show) -- | A default 'Options'. @@ -124,6 +125,7 @@ defaultOptions = Options { } , optGhcUserOptions = [] , optFileMappings = [] + , optEncoding = "UTF-8" } ---------------------------------------------------------------- diff --git a/src/GHCMod.hs b/src/GHCMod.hs index fdade72c8..531f7dec4 100644 --- a/src/GHCMod.hs +++ b/src/GHCMod.hs @@ -34,9 +34,12 @@ handler = flip gcatches ] main :: IO () -main = do - hSetEncoding stdout utf8 - parseArgs >>= \res@(globalOptions, _) -> +main = + parseArgs >>= \res@(globalOptions, _) -> do + enc <- mkTextEncoding $ optEncoding globalOptions + hSetEncoding stdout enc + hSetEncoding stderr enc + hSetEncoding stdin enc catches (progMain res) [ Handler $ \(e :: GhcModError) -> runGmOutT globalOptions $ exitError $ renderStyle ghcModStyle (gmeDoc e) @@ -107,7 +110,6 @@ getFileSourceFromStdin = do then fmap (x:) readStdin' else return [] --- Someone please already rewrite the cmdline parsing code *weep* :'( wrapGhcCommands :: (IOish m, GmOut m) => Options -> GhcModCommands -> m () wrapGhcCommands _opts CmdRoot = gmPutStr =<< rootInfo wrapGhcCommands opts cmd = diff --git a/src/GHCMod/Options.hs b/src/GHCMod/Options.hs index 0a2a73ed0..40c8fdd1d 100644 --- a/src/GHCMod/Options.hs +++ b/src/GHCMod/Options.hs @@ -174,6 +174,12 @@ globalArgSpec = Options <=> metavar "OPT" <=> help "Option to be passed to GHC" <*> many fileMappingSpec + <*> strOption + $$ long "encoding" + <=> short 'e' + <=> value "UTF-8" + <=> showDefault + <=> help "I/O encoding" where fileMappingSpec = getFileMapping . splitOn '=' <$> strOption From 19f0b0215c270db7cf218b877a60adb04754d29a Mon Sep 17 00:00:00 2001 From: Nikolay Yakimov Date: Tue, 9 Feb 2016 12:42:47 +0300 Subject: [PATCH 2/3] Since a backend option, remove encoding short opt --- src/GHCMod/Options.hs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/GHCMod/Options.hs b/src/GHCMod/Options.hs index 40c8fdd1d..e654c7eed 100644 --- a/src/GHCMod/Options.hs +++ b/src/GHCMod/Options.hs @@ -176,7 +176,6 @@ globalArgSpec = Options <*> many fileMappingSpec <*> strOption $$ long "encoding" - <=> short 'e' <=> value "UTF-8" <=> showDefault <=> help "I/O encoding" From 1e1bdc5743128162c6ea7eb6c6ee9dbec169ca2a Mon Sep 17 00:00:00 2001 From: Nikolay Yakimov Date: Tue, 9 Feb 2016 16:25:30 +0300 Subject: [PATCH 3/3] Fix output encoding in loadMappedFileSource --- Language/Haskell/GhcMod/FileMapping.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Language/Haskell/GhcMod/FileMapping.hs b/Language/Haskell/GhcMod/FileMapping.hs index 1806017f8..a3f2f97d5 100644 --- a/Language/Haskell/GhcMod/FileMapping.hs +++ b/Language/Haskell/GhcMod/FileMapping.hs @@ -46,8 +46,10 @@ loadMappedFileSource :: IOish m -> GhcModT m () loadMappedFileSource from src = do tmpdir <- cradleTempDir `fmap` cradle + enc <- liftIO . mkTextEncoding . optEncoding =<< options to <- liftIO $ do (fn, h) <- openTempFile tmpdir (takeFileName from) + hSetEncoding h enc hPutStr h src hClose h return fn