correct errno to boolean
[CleanReadLine.git] / ReadLine.icl
1 implementation module ReadLine
2
3 import StdEnv
4
5 import code from "readLine.o"
6
7 //Maybe
8 isNothing :: !(Maybe .x) -> Bool
9 isNothing Nothing = True
10 isNothing _ = False
11
12 isJust :: !(Maybe .x) -> Bool
13 isJust Nothing = False
14 isJust _ = True
15
16 fromJust :: !(Maybe .x) -> .x
17 fromJust (Just x) = x
18
19 instance toString HistoryItem where
20 toString {line,timestamp} = line +++ " (" +++ timestamp +++ ")"
21 instance toString HistoryState where
22 toString {entries,offset,flags} = "[" +++ toS entries +++ "]\n" +++
23 "offset: " +++ toString offset +++ "\nflags: " +++ toString flags
24 where
25 toS :: [HistoryItem] -> String
26 toS [] = "--empty--"
27 toS [x] = toString x
28 toS [x:xs] = toString x +++ ","
29
30 //Readline
31 readLine :: !String !Bool !*env -> (!Maybe String, !*env)
32 readLine s h e
33 # (s, eof, e) = readLineEof s h e
34 = (if eof Nothing (Just s), e)
35 where
36 readLineEof :: !String !Bool !*env -> (!String, !Bool, !*env)
37 readLineEof s h e = code {
38 ccall cleanReadLine "SI:VSI:A"
39 }
40
41 setReadLineName :: !String !*env -> *env
42 setReadLineName s e = code {
43 ccall cleanSetReadLineName "S:V:A"
44 }
45
46 //Initializing History and State Management
47 usingHistory :: !*env -> *env
48 usingHistory e = code {
49 ccall cleanUsingHistory ":V:A"
50 }
51
52 historyGetHistoryState :: !*env -> (!HistoryState, !*env)
53 historyGetHistoryState e
54 # (offset, num, flags, e) = getState e
55 # (entries, e) = getItems num e
56 = ({HistoryState | entries=entries, offset=offset, flags=flags}, e)
57 where
58 getState :: !*env -> (!Int, !Int, !Int, !*env)
59 getState e= code {
60 ccall cleanGetState ":VIII:A"
61 }
62 getItems :: !Int !*env -> (![HistoryItem], !*env)
63 getItems 0 e = ([], e)
64 getItems i e
65 # (line, timestamp, e) = getItem (i-1) e
66 # (rest, e) = getItems (i-1) e
67 = ([{line=line,timestamp=timestamp}:rest], e)
68
69 getItem :: !Int !*env -> (!String, !String, !*env)
70 getItem i e = code {
71 ccall cleanGetHistoryItem "I:VSS:A"
72 }
73
74
75 historySetHistoryState :: !HistoryState !*env -> *env
76 historySetHistoryState {entries,offset,flags} e
77 # e = initNewHistoryState offset flags (length entries) e
78 # e = setItems entries 0 e
79 = commit e
80 where
81 initNewHistoryState :: !Int !Int !Int !*env -> *env
82 initNewHistoryState o f l e = code {
83 ccall cleanInitNewHistoryState "III:V:A"
84 }
85 setItems :: ![HistoryItem] !Int !*env -> *env
86 setItems [] _ e = e
87 setItems [x:xs] i e
88 # e = setItem i x.line x.timestamp e
89 = setItems xs (i+1) e
90
91 setItem :: !Int !String !String !*env -> *env
92 setItem i l t e = code {
93 ccall cleanSetNewHistoryEntry "ISS:V:A"
94 }
95
96 commit :: !*env -> *env
97 commit e = code {
98 ccall cleanCommitSetHistory ":V:A"
99 }
100
101 //History List Management
102 addHistory :: !String !*env -> *env
103 addHistory s e = code {
104 ccall cleanAddHistory "S:V:A"
105 }
106
107 addHistoryTime :: !String !*env -> *env
108 addHistoryTime s e = code {
109 ccall cleanAddHistoryTime "S:V:A"
110 }
111
112 removeHistory :: !Int !*env -> (!HistoryItem, !*env)
113 removeHistory i e
114 # (line, timestamp, e) = removeHistoryItem i e
115 = ({HistoryItem | line=line, timestamp=timestamp}, e)
116 where
117 removeHistoryItem :: !Int !*env -> (!String, !String, !*env)
118 removeHistoryItem i e = code {
119 ccall cleanRemoveHistory "I:VSS:A"
120 }
121
122 replaceHistoryEntry :: !Int !String !*env -> (!HistoryItem, !*env)
123 replaceHistoryEntry i s e
124 # (line, timestamp, e) = replaceItem i s e
125 = ({HistoryItem | line=line, timestamp=timestamp}, e)
126 where
127 replaceItem :: !Int !String !*env -> (!String, !String, !*env)
128 replaceItem i s e = code {
129 ccall cleanReplaceHistoryEntry "IS:VSS:A"
130 }
131
132 clearHistory :: !*env -> *env
133 clearHistory e = code {
134 ccall cleanClearHistory ":V:A"
135 }
136
137 stifleHistory :: !Int !*env -> *env
138 stifleHistory i e = code {
139 ccall cleanStifleHistory "I:V:A"
140 }
141
142 unstifleHistory :: !*env -> *env
143 unstifleHistory e = code {
144 ccall cleanUnstifleHistory ":V:A"
145 }
146
147 historyIsStifled :: !*env -> (!Int, !*env)
148 historyIsStifled e = code {
149 ccall cleanHistoryIsStifled ":I:A"
150 }
151
152 //Information About the History List
153 historyList :: !*env -> (![HistoryItem], !*env)
154 historyList e
155 # ({entries,offset,flags}, e) = historyGetHistoryState e
156 = (entries, e)
157
158 whereHistory :: !*env -> (!Int, !*env)
159 whereHistory e
160 # ({entries,offset,flags}, e) = historyGetHistoryState e
161 = (offset, e)
162
163 currentHistory :: !*env -> (!Maybe HistoryItem, !*env)
164 currentHistory e
165 # ({entries,offset,flags}, e) = historyGetHistoryState e
166 = (if (isEmpty entries) Nothing (Just (entries!!offset)), e)
167
168 historyGet :: !Int !*env -> (!Maybe HistoryItem, !*env)
169 historyGet i e
170 # ({entries,offset,flags}, e) = historyGetHistoryState e
171 = (if (isEmpty entries) Nothing (Just (entries!!i)), e)
172
173 historyGetTime :: HistoryItem -> String
174 historyGetTime {line,timestamp} = timestamp
175
176 historyTotalBytes :: !*env -> (!Int, !*env)
177 historyTotalBytes e = code {
178 ccall cleanHistoryTotalBytes ":I:A"
179 }
180
181 //Moving Around the History List
182
183 //Searching the History List
184 historySearch :: !String !Int !*env-> (!Int, !*env)
185 historySearch s i e = code {
186 ccall cleanHistorySearch "SI:I:A"
187 }
188
189 historySearchPrefix :: !String !Int !*env-> (!Int, !*env)
190 historySearchPrefix s i e = code {
191 ccall cleanHistorySearchPrefix "SI:I:A"
192 }
193
194 historySearchPos :: !String !Int !Int !*env-> (!Int, !*env)
195 historySearchPos s i1 i2 e = code {
196 ccall cleanHistorySearchPos "SI:I:A"
197 }
198
199
200 //Managing the History File
201 readHistory :: !String !*env -> (!Bool, !*env)
202 readHistory s e = code {
203 ccall cleanReadHistory "S:I:A"
204 }
205
206 readHistoryRange :: !String !Int !Int !*env -> (!Bool, !*env)
207 readHistoryRange s i1 i2 e = code {
208 ccall cleanReadHistoryRange "SII:I:A"
209 }
210
211 writeHistory :: !String !*env -> (!Bool, !*env)
212 writeHistory s e = code {
213 ccall cleanWriteHistory "S:I:A"
214 }
215
216 appendHistory :: !Int !String !*env -> (!Bool, !*env)
217 appendHistory i s e = code {
218 ccall cleanWriteHistory "IS:I:A"
219 }
220
221 historyTruncateFile :: !String !Int !*env -> (!Bool, !*env)
222 historyTruncateFile i s e = code {
223 ccall cleanWriteHistory "SI:I:A"
224 }