Merge pull request #3 from dopefishh/standard-maybe
[CleanReadLine.git] / ReadLine.icl
1 implementation module ReadLine
2
3 import StdEnv
4 import Data.Maybe
5
6 import code from "readLine.o"
7
8 instance toString HistoryItem where
9 toString {line,timestamp} = line +++ " (" +++ timestamp +++ ")"
10 instance toString HistoryState where
11 toString {entries,offset,flags} = "[" +++ toS entries +++ "]\n" +++
12 "offset: " +++ toString offset +++ "\nflags: " +++ toString flags
13 where
14 toS :: [HistoryItem] -> String
15 toS [] = "--empty--"
16 toS [x] = toString x
17 toS [x:xs] = toString x +++ ","
18
19 //Readline
20 readLine :: !String !Bool !*env -> (!Maybe String, !*env)
21 readLine s h e
22 # (s, eof, e) = readLineEof s h e
23 = (if eof Nothing (Just s), e)
24 where
25 readLineEof :: !String !Bool !*env -> (!String, !Bool, !*env)
26 readLineEof s h e = code {
27 ccall cleanReadLine "SI:VSI:A"
28 }
29
30 setReadLineName :: !String !*env -> *env
31 setReadLineName s e = code {
32 ccall cleanSetReadLineName "S:V:A"
33 }
34
35 //Initializing History and State Management
36 usingHistory :: !*env -> *env
37 usingHistory e = code {
38 ccall cleanUsingHistory ":V:A"
39 }
40
41 historyGetHistoryState :: !*env -> (!HistoryState, !*env)
42 historyGetHistoryState e
43 # (offset, num, flags, e) = getState e
44 # (entries, e) = getItems num e
45 = ({HistoryState | entries=entries, offset=offset, flags=flags}, e)
46 where
47 getState :: !*env -> (!Int, !Int, !Int, !*env)
48 getState e= code {
49 ccall cleanGetState ":VIII:A"
50 }
51 getItems :: !Int !*env -> (![HistoryItem], !*env)
52 getItems 0 e = ([], e)
53 getItems i e
54 # (line, timestamp, e) = getItem (i-1) e
55 # (rest, e) = getItems (i-1) e
56 = ([{line=line,timestamp=timestamp}:rest], e)
57
58 getItem :: !Int !*env -> (!String, !String, !*env)
59 getItem i e = code {
60 ccall cleanGetHistoryItem "I:VSS:A"
61 }
62
63
64 historySetHistoryState :: !HistoryState !*env -> *env
65 historySetHistoryState {entries,offset,flags} e
66 # e = initNewHistoryState offset flags (length entries) e
67 # e = setItems entries 0 e
68 = commit e
69 where
70 initNewHistoryState :: !Int !Int !Int !*env -> *env
71 initNewHistoryState o f l e = code {
72 ccall cleanInitNewHistoryState "III:V:A"
73 }
74 setItems :: ![HistoryItem] !Int !*env -> *env
75 setItems [] _ e = e
76 setItems [x:xs] i e
77 # e = setItem i x.line x.timestamp e
78 = setItems xs (i+1) e
79
80 setItem :: !Int !String !String !*env -> *env
81 setItem i l t e = code {
82 ccall cleanSetNewHistoryEntry "ISS:V:A"
83 }
84
85 commit :: !*env -> *env
86 commit e = code {
87 ccall cleanCommitSetHistory ":V:A"
88 }
89
90 //History List Management
91 addHistory :: !String !*env -> *env
92 addHistory s e = code {
93 ccall cleanAddHistory "S:V:A"
94 }
95
96 addHistoryTime :: !String !*env -> *env
97 addHistoryTime s e = code {
98 ccall cleanAddHistoryTime "S:V:A"
99 }
100
101 removeHistory :: !Int !*env -> (!HistoryItem, !*env)
102 removeHistory i e
103 # (line, timestamp, e) = removeHistoryItem i e
104 = ({HistoryItem | line=line, timestamp=timestamp}, e)
105 where
106 removeHistoryItem :: !Int !*env -> (!String, !String, !*env)
107 removeHistoryItem i e = code {
108 ccall cleanRemoveHistory "I:VSS:A"
109 }
110
111 replaceHistoryEntry :: !Int !String !*env -> (!HistoryItem, !*env)
112 replaceHistoryEntry i s e
113 # (line, timestamp, e) = replaceItem i s e
114 = ({HistoryItem | line=line, timestamp=timestamp}, e)
115 where
116 replaceItem :: !Int !String !*env -> (!String, !String, !*env)
117 replaceItem i s e = code {
118 ccall cleanReplaceHistoryEntry "IS:VSS:A"
119 }
120
121 clearHistory :: !*env -> *env
122 clearHistory e = code {
123 ccall cleanClearHistory ":V:A"
124 }
125
126 stifleHistory :: !Int !*env -> *env
127 stifleHistory i e = code {
128 ccall cleanStifleHistory "I:V:A"
129 }
130
131 unstifleHistory :: !*env -> *env
132 unstifleHistory e = code {
133 ccall cleanUnstifleHistory ":V:A"
134 }
135
136 historyIsStifled :: !*env -> (!Int, !*env)
137 historyIsStifled e = code {
138 ccall cleanHistoryIsStifled ":I:A"
139 }
140
141 //Information About the History List
142 historyList :: !*env -> (![HistoryItem], !*env)
143 historyList e
144 # ({entries,offset,flags}, e) = historyGetHistoryState e
145 = (entries, e)
146
147 whereHistory :: !*env -> (!Int, !*env)
148 whereHistory e
149 # ({entries,offset,flags}, e) = historyGetHistoryState e
150 = (offset, e)
151
152 currentHistory :: !*env -> (!Maybe HistoryItem, !*env)
153 currentHistory e
154 # ({entries,offset,flags}, e) = historyGetHistoryState e
155 = (if (isEmpty entries) Nothing (Just (entries!!offset)), e)
156
157 historyGet :: !Int !*env -> (!Maybe HistoryItem, !*env)
158 historyGet i e
159 # ({entries,offset,flags}, e) = historyGetHistoryState e
160 = (if (isEmpty entries) Nothing (Just (entries!!i)), e)
161
162 historyGetTime :: HistoryItem -> String
163 historyGetTime {line,timestamp} = timestamp
164
165 historyTotalBytes :: !*env -> (!Int, !*env)
166 historyTotalBytes e = code {
167 ccall cleanHistoryTotalBytes ":I:A"
168 }
169
170 //Moving Around the History List
171 historySetPos :: !Int !*env -> (!Int, !*env)
172 historySetPos i e = code {
173 ccall cleanHistorySetPos "I:I:A"
174 }
175
176 previousHistory :: !*env -> (!Maybe HistoryItem, !*env)
177 previousHistory e
178 # (line, timestamp, null, e) = previousHistoryItem e
179 = (if null Nothing (Just {HistoryItem | line=line, timestamp=timestamp}), e)
180 where
181 previousHistoryItem :: !*env -> (!String, !String, !Bool, !*env)
182 previousHistoryItem e = code {
183 ccall cleanPreviousHistory ":VSSI:A"
184 }
185
186 nextHistory :: !*env -> (!Maybe HistoryItem, !*env)
187 nextHistory e
188 # (line, timestamp, null, e) = nextHistoryItem e
189 = (if null Nothing (Just {HistoryItem | line=line, timestamp=timestamp}), e)
190 where
191 nextHistoryItem :: !*env -> (!String, !String, !Bool, !*env)
192 nextHistoryItem e = code {
193 ccall cleanNextHistory ":VSSI:A"
194 }
195
196 //Searching the History List
197 historySearch :: !String !Int !*env-> (!Int, !*env)
198 historySearch s i e = code {
199 ccall cleanHistorySearch "SI:I:A"
200 }
201
202 historySearchPrefix :: !String !Int !*env-> (!Int, !*env)
203 historySearchPrefix s i e = code {
204 ccall cleanHistorySearchPrefix "SI:I:A"
205 }
206
207 historySearchPos :: !String !Int !Int !*env-> (!Int, !*env)
208 historySearchPos s i1 i2 e = code {
209 ccall cleanHistorySearchPos "SI:I:A"
210 }
211
212
213 //Managing the History File
214 readHistory :: !String !*env -> (!Bool, !*env)
215 readHistory s e = code {
216 ccall cleanReadHistory "S:I:A"
217 }
218
219 readHistoryRange :: !String !Int !Int !*env -> (!Bool, !*env)
220 readHistoryRange s i1 i2 e = code {
221 ccall cleanReadHistoryRange "SII:I:A"
222 }
223
224 writeHistory :: !String !*env -> (!Bool, !*env)
225 writeHistory s e = code {
226 ccall cleanWriteHistory "S:I:A"
227 }
228
229 appendHistory :: !Int !String !*env -> (!Bool, !*env)
230 appendHistory i s e = code {
231 ccall cleanWriteHistory "IS:I:A"
232 }
233
234 historyTruncateFile :: !String !Int !*env -> (!Bool, !*env)
235 historyTruncateFile i s e = code {
236 ccall cleanWriteHistory "SI:I:A"
237 }
238
239 historyExpand :: !String !*env -> (!String, !Int, !*env)
240 historyExpand s e = code {
241 ccall cleanHistoryExpand "S:VSI:A"
242 }
243
244 getHistoryEvent :: !String !Int !Int !*env -> (!Maybe String, !Int, !*env)
245 getHistoryEvent s i1 i2 e
246 # (string, cindex, null, e) = getHistoryEventItem s i1 i2 e
247 = (if null Nothing (Just string), cindex, e)
248 where
249 getHistoryEventItem :: !String !Int !Int !*env -> (!String, !Int, !Bool, !*env)
250 getHistoryEventItem s i1 i2 e = code {
251 ccall cleanGetHistoryEvent "SII:VSII:A"
252 }
253
254 historyTokenize :: !String !*env -> ([String], !*env)
255 historyTokenize s e
256 # (i, e) = historyTokenizeInit s e
257 = historyTokenizeItems i e
258 //= ([], e)
259 where
260 historyTokenizeInit :: !String !*env -> (!Int, !*env)
261 historyTokenizeInit s e = code {
262 ccall cleanHistoryTokenizeInit "S:I:A"
263 }
264
265 historyTokenizeItems :: !Int !*env -> (![String], !*env)
266 historyTokenizeItems 0 e = ([], e)
267 historyTokenizeItems i e
268 # (token, e) = historyTokenizeItem (i-1) e
269 # (rest, e) = historyTokenizeItems (i-1) e
270 = ([token:rest], e)
271
272 historyTokenizeItem :: !Int !*env -> (!String, !*env)
273 historyTokenizeItem i e = code {
274 ccall cleanHistoryTokenizeItem "I:S:A"
275 }
276
277 historyArgExtract :: !Int !Int !String !*env -> (!Maybe String, !*env)
278 historyArgExtract i1 i2 s e
279 # (string, null, e) = historyArgExtractItem i1 i2 s e
280 = (if null Nothing (Just string), e)
281 where
282 historyArgExtractItem :: !Int !Int !String !*env -> (!String, !Bool, !*env)
283 historyArgExtractItem i1 i2 s e = code {
284 ccall cleanHistoryArgExtract "IIS:VSI:A"
285 }