v0.4
[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 historySetPos :: !Int !*env -> (!Int, !*env)
183 historySetPos i e = code {
184 ccall cleanHistorySetPos "I:I:A"
185 }
186
187 previousHistory :: !*env -> (!Maybe HistoryItem, !*env)
188 previousHistory e
189 # (line, timestamp, null, e) = previousHistoryItem e
190 = (if null Nothing (Just {HistoryItem | line=line, timestamp=timestamp}), e)
191 where
192 previousHistoryItem :: !*env -> (!String, !String, !Bool, !*env)
193 previousHistoryItem e = code {
194 ccall cleanPreviousHistory ":VSSI:A"
195 }
196
197 nextHistory :: !*env -> (!Maybe HistoryItem, !*env)
198 nextHistory e
199 # (line, timestamp, null, e) = nextHistoryItem e
200 = (if null Nothing (Just {HistoryItem | line=line, timestamp=timestamp}), e)
201 where
202 nextHistoryItem :: !*env -> (!String, !String, !Bool, !*env)
203 nextHistoryItem e = code {
204 ccall cleanNextHistory ":VSSI:A"
205 }
206
207 //Searching the History List
208 historySearch :: !String !Int !*env-> (!Int, !*env)
209 historySearch s i e = code {
210 ccall cleanHistorySearch "SI:I:A"
211 }
212
213 historySearchPrefix :: !String !Int !*env-> (!Int, !*env)
214 historySearchPrefix s i e = code {
215 ccall cleanHistorySearchPrefix "SI:I:A"
216 }
217
218 historySearchPos :: !String !Int !Int !*env-> (!Int, !*env)
219 historySearchPos s i1 i2 e = code {
220 ccall cleanHistorySearchPos "SI:I:A"
221 }
222
223
224 //Managing the History File
225 readHistory :: !String !*env -> (!Bool, !*env)
226 readHistory s e = code {
227 ccall cleanReadHistory "S:I:A"
228 }
229
230 readHistoryRange :: !String !Int !Int !*env -> (!Bool, !*env)
231 readHistoryRange s i1 i2 e = code {
232 ccall cleanReadHistoryRange "SII:I:A"
233 }
234
235 writeHistory :: !String !*env -> (!Bool, !*env)
236 writeHistory s e = code {
237 ccall cleanWriteHistory "S:I:A"
238 }
239
240 appendHistory :: !Int !String !*env -> (!Bool, !*env)
241 appendHistory i s e = code {
242 ccall cleanWriteHistory "IS:I:A"
243 }
244
245 historyTruncateFile :: !String !Int !*env -> (!Bool, !*env)
246 historyTruncateFile i s e = code {
247 ccall cleanWriteHistory "SI:I:A"
248 }
249
250 historyExpand :: !String !*env -> (!String, !Int, !*env)
251 historyExpand s e = code {
252 ccall cleanHistoryExpand "S:VSI:A"
253 }
254
255 getHistoryEvent :: !String !Int !Int !*env -> (!Maybe String, !Int, !*env)
256 getHistoryEvent s i1 i2 e
257 # (string, cindex, null, e) = getHistoryEventItem s i1 i2 e
258 = (if null Nothing (Just string), cindex, e)
259 where
260 getHistoryEventItem :: !String !Int !Int !*env -> (!String, !Int, !Bool, !*env)
261 getHistoryEventItem s i1 i2 e = code {
262 ccall cleanGetHistoryEvent "SII:VSII:A"
263 }
264
265 historyTokenize :: !String !*env -> ([String], !*env)
266 historyTokenize s e
267 # (i, e) = historyTokenizeInit s e
268 = historyTokenizeItems i e
269 //= ([], e)
270 where
271 historyTokenizeInit :: !String !*env -> (!Int, !*env)
272 historyTokenizeInit s e = code {
273 ccall cleanHistoryTokenizeInit "S:I:A"
274 }
275
276 historyTokenizeItems :: !Int !*env -> (![String], !*env)
277 historyTokenizeItems 0 e = ([], e)
278 historyTokenizeItems i e
279 # (token, e) = historyTokenizeItem (i-1) e
280 # (rest, e) = historyTokenizeItems (i-1) e
281 = ([token:rest], e)
282
283 historyTokenizeItem :: !Int !*env -> (!String, !*env)
284 historyTokenizeItem i e = code {
285 ccall cleanHistoryTokenizeItem "I:S:A"
286 }
287
288 historyArgExtract :: !Int !Int !String !*env -> (!Maybe String, !*env)
289 historyArgExtract i1 i2 s e
290 # (string, null, e) = historyArgExtractItem i1 i2 s e
291 = (if null Nothing (Just string), e)
292 where
293 historyArgExtractItem :: !Int !Int !String !*env -> (!String, !Bool, !*env)
294 historyArgExtractItem i1 i2 s e = code {
295 ccall cleanHistoryArgExtract "IIS:VSI:A"
296 }