summaryrefslogtreecommitdiff
blob: c0187e0aa6f17577c75938042e986a7bd6098093 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Tue Dec  1 19:30:23 EET 2009  Sergei Trofimovich <slyfox@inbox.ru>
  * add UTF8 I/O when built against ghc6.12+
  
  This patch fixes following usecase:
  NONASCII-utf8-rich-project: LANG=C cabal hscolour
  (realworld example!)
  
  By default ghc presumes locale I/O and breaks horribly.
  This patch switches to explicit UTF8 when deals with files.
Mon Nov 30 13:47:23 EET 2009  Sergei Trofimovich <slyfox@inbox.ru>
  * Print newline when output usage banner.
Mon Nov 30 12:35:39 EET 2009  Sergei Trofimovich <slyfox@inbox.ru>
  * Use Cabal instead of hardcoding version info (stolen from highlighting-kate)
diff -rN -u old-hscolour/hscolour.cabal new-hscolour/hscolour.cabal
--- old-hscolour/hscolour.cabal	2009-12-01 19:41:01.145689639 +0200
+++ new-hscolour/hscolour.cabal	2009-12-01 19:41:01.155689306 +0200
@@ -6,6 +6,7 @@
 Homepage: http://www.cs.york.ac.uk/fp/darcs/hscolour/
 License: GPL
 License-file: LICENCE-GPL
+Cabal-Version:  >= 1.6
 Build-depends: haskell98, base < 10
 Extensions: 
 Synopsis: Colourise Haskell code.
@@ -35,6 +36,9 @@
   Language.Haskell.HsColour.Options
   Language.Haskell.HsColour.Output
   Language.Haskell.HsColour.TTY
+Other-Modules:
+  Paths_hscolour
+  Util
 data-files: hscolour.css
 --ghc-options: -O -W
 Build-Type: Simple
diff -rN -u old-hscolour/HsColour.hs new-hscolour/HsColour.hs
--- old-hscolour/HsColour.hs	2009-12-01 19:41:01.145689639 +0200
+++ new-hscolour/HsColour.hs	2009-12-01 19:41:01.148689183 +0200
@@ -6,11 +6,17 @@
 import Language.Haskell.HsColour.Options
 import System
 import IO
+import System.IO (withFile)
 import Monad (when)
 import List  (intersperse, isSuffixOf)
 import Debug.Trace
 
-version = "1.15"
+import Util (set_utf8_io_enc)
+
+import Data.Version (showVersion)
+import qualified Paths_hscolour (version)
+
+version = showVersion Paths_hscolour.version
 
 optionTable :: [(String,Option)]
 optionTable = [ ("help",    Help)
@@ -68,27 +74,43 @@
   ioWrapper (HSColour.hscolour output pref anchors partial title)
 
   where
+    --
+    -- Implement follow such I/O codepage rules:
+    -- FILE I(unput) / O(utput) is in UTF8
+    -- TTY  I(unput) / O(utput) is in locale
+    --   (may have problems with HsColour IFILE >OFILE, as it differs from HsColour IFILE -oOFILE)
+    -- TTY stderr is alwais in locale (always used for user interaction)
+    --
+    -- Some common use cases:
+    -- File I / FILE O (HsColour -css -anchor -oOFILE IFILE) : are both always done in UTF8 mode (cabal hscolour mode)
+    -- File I / TTY  O (HsColour IFILE) : file is read in UTF-8 written in locale
+    -- TTY  I / TTY  O (HsColour) : stdin/stdout are both in locale
+
+    -- fully mimic Prelude analogues
+    writeUTF8File f txt = withFile f WriteMode (\hdl -> set_utf8_io_enc hdl >> hPutStr hdl txt)
+    readUTF8File name = openFile name ReadMode >>= set_utf8_io_enc >>= hGetContents
+
     writeResult outF s = do if null outF then putStr s
-                                         else writeFile (last outF) s
+                                         else writeUTF8File (last outF) s
                             exitSuccess
     fileInteract out inFs u = do h <- case out of
                                           []     -> return stdout
-                                          [outF] -> openFile outF WriteMode
+                                          [outF] -> openFile outF WriteMode >>= set_utf8_io_enc
                                  mapM_ (\ (f,lit)->
-                                           readFile f >>= hPutStr h . u lit)
+                                           readUTF8File f >>= hPutStr h . u lit)
                                        inFs
                                  hClose h
     ttyInteract []     lit u = do hSetBuffering stdout NoBuffering
                                   Prelude.interact (u lit)
     ttyInteract [outF] lit u = do c <- hGetContents stdin
-                                  writeFile outF (u lit c)
+                                  writeUTF8File outF (u lit c)
     exitSuccess = exitWith ExitSuccess
     errorOut s = hPutStrLn stderr s >> hFlush stderr >> exitFailure
     usage prog = "Usage: "++prog
                  ++" options [file.hs]\n    where\n      options = [ "
                  ++ (indent 15 . unwords . width 58 58 . intersperse "|"
                      . ("-oOUTPUT":)
-                     . map (('-':) . fst)) optionTable ++ " ]"
+                     . map (('-':) . fst)) optionTable ++ " ]\n"
     useDefault d f list | null list = d
                         | otherwise = f (head list)
     useDefaults d f list | null list = d
diff -rN -u old-hscolour/Util.hs new-hscolour/Util.hs
--- old-hscolour/Util.hs	1970-01-01 03:00:00.000000000 +0300
+++ new-hscolour/Util.hs	2009-12-01 19:41:01.153688353 +0200
@@ -0,0 +1,12 @@
+{-# LANGUAGE CPP #-}
+
+module Util where
+
+import System.IO
+
+set_utf8_io_enc :: Handle -> IO Handle
+set_utf8_io_enc h =
+#if MIN_VERSION_base(4,2,0)
+    hSetEncoding h utf8 >>
+#endif
+    return h