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