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
122
123
124
125
126
module CMeta (CMeta,
                  CpcPos,
                  BoxEntry,
                  BoxLabel(..),
                  CondBox,
                  mkExpBox,
                  metaCreate,
                  readMeta',
                  metaName,
                  getBoxes,
                  mkTopBox,
                  mkLocBox,
                  mkifTBox,
                  mkifFBox,
                  mkGuardTBox,
                  mkGuardFBox,
                  position, label, idBox,
                  isExp,
                  isAlt,
                  isCond,
                  isBin,
                  boxes,
                  mkCMeta) where

import CTrack

import ReadShowTerm (readQTermFile)
import FilePath
import CPC.Files


-- Tab stops are the size of a tab in the provided /line:column/ values.

data CMeta = CMeta
       --      FilePath           -- location of original file
       --      CalendarTime            -- time of original file's last update
       --      Int                -- tab stop value.
             [BoxEntry]         -- entries

type CpcPos = (Int, Int)

type BoxEntry = (Int, (CpcPos,CpcPos), BoxLabel)

data BoxLabel = ExpBox  Bool -- isAlt
              | TopLevelBox [String]
              | LocalBox [String]
              | BinBox CondBox Bool

data CondBox = GuardBinBox
             | CondBinBox
             | QualBinBox

mkExpBox :: Bool -> BoxLabel
mkExpBox bool = ExpBox bool

mkTopBox :: [String] -> BoxLabel
mkTopBox strings = TopLevelBox strings

mkLocBox :: [String] -> BoxLabel
mkLocBox strings = LocalBox strings

mkifTBox :: BoxLabel
mkifTBox = BinBox CondBinBox True

mkifFBox :: BoxLabel
mkifFBox = BinBox CondBinBox False

mkGuardTBox :: BoxLabel
mkGuardTBox = BinBox GuardBinBox True

mkGuardFBox :: BoxLabel
mkGuardFBox = BinBox GuardBinBox False

metaCreate :: String -> CMeta -> IO ()
metaCreate modname cmeta =
  writeFile (inCpcSubdir $ replaceExtension modname "cmeta") (show cmeta)

mkCMeta :: [BoxEntry] -> CMeta
mkCMeta boxes' = CMeta boxes'

idBox :: BoxEntry -> Int
idBox (id, _, _) = id

position :: BoxEntry -> (CpcPos,CpcPos)
position (_,pos,_) = pos

label :: BoxEntry -> BoxLabel
label (_,_,label') = label'

boxes :: CMeta -> [BoxEntry]
boxes (CMeta boxes') = boxes'

readMeta' :: String -> IO CMeta
readMeta' filename =
  do
  -- print "Reading .cmeta..."
    contents <- readQTermFile $ inCpcSubdir $ replaceExtension filename "cmeta"
    return contents

metaName :: FilePath -> String -> String
metaName dirName name = dirName </> name <.> "cmeta"

getBoxes :: CMeta -> [BoxEntry]
getBoxes (CMeta b) = b

isExp:: BoxEntry -> Bool
isExp box = case box of
                  (_, _, ExpBox _) -> True
                  (_, _, _) -> False

isAlt:: BoxEntry -> Bool
isAlt box = case box of
                  (_, _, ExpBox True) -> True
                  _ -> False

isBin:: BoxEntry -> Bool
isBin box = case box of
                  (_, _,  BinBox _ _) -> True
                  _ -> False

isCond:: BoxLabel -> Bool
isCond box = case box of
                  (BinBox CondBinBox _) -> True
                  _ -> False