1 implementation module ReadLine
5 import code from "readLine.o"
8 isNothing :: !(Maybe .x) -> Bool
9 isNothing Nothing = True
12 isJust :: !(Maybe .x) -> Bool
13 isJust Nothing = False
16 fromJust :: !(Maybe .x) -> .x
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
25 toS :: [HistoryItem] -> String
28 toS [x:xs] = toString x +++ ","
31 readLine :: !String !Bool !*env -> (!Maybe String, !*env)
33 # (s, eof, e) = readLineEof s h e
34 = (if eof Nothing (Just s), e)
36 readLineEof :: !String !Bool !*env -> (!String, !Bool, !*env)
37 readLineEof s h e = code {
38 ccall cleanReadLine "SI:VSI:A"
41 setReadLineName :: !String !*env -> *env
42 setReadLineName s e = code {
43 ccall cleanSetReadLineName "S:V:A"
46 //Initializing History and State Management
47 usingHistory :: !*env -> *env
48 usingHistory e = code {
49 ccall cleanUsingHistory ":V:A"
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)
58 getState :: !*env -> (!Int, !Int, !Int, !*env)
60 ccall cleanGetState ":VIII:A"
62 getItems :: !Int !*env -> (![HistoryItem], !*env)
63 getItems 0 e = ([], e)
65 # (line, timestamp, e) = getItem (i-1) e
66 # (rest, e) = getItems (i-1) e
67 = ([{line=line,timestamp=timestamp}:rest], e)
69 getItem :: !Int !*env -> (!String, !String, !*env)
71 ccall cleanGetHistoryItem "I:VSS:A"
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
81 initNewHistoryState :: !Int !Int !Int !*env -> *env
82 initNewHistoryState o f l e = code {
83 ccall cleanInitNewHistoryState "III:V:A"
85 setItems :: ![HistoryItem] !Int !*env -> *env
88 # e = setItem i x.line x.timestamp e
91 setItem :: !Int !String !String !*env -> *env
92 setItem i l t e = code {
93 ccall cleanSetNewHistoryEntry "ISS:V:A"
96 commit :: !*env -> *env
98 ccall cleanCommitSetHistory ":V:A"
101 //History List Management
102 addHistory :: !String !*env -> *env
103 addHistory s e = code {
104 ccall cleanAddHistory "S:V:A"
107 addHistoryTime :: !String !*env -> *env
108 addHistoryTime s e = code {
109 ccall cleanAddHistoryTime "S:V:A"
112 removeHistory :: !Int !*env -> (!HistoryItem, !*env)
114 # (line, timestamp, e) = removeHistoryItem i e
115 = ({HistoryItem | line=line, timestamp=timestamp}, e)
117 removeHistoryItem :: !Int !*env -> (!String, !String, !*env)
118 removeHistoryItem i e = code {
119 ccall cleanRemoveHistory "I:VSS:A"
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)
127 replaceItem :: !Int !String !*env -> (!String, !String, !*env)
128 replaceItem i s e = code {
129 ccall cleanReplaceHistoryEntry "IS:VSS:A"
132 clearHistory :: !*env -> *env
133 clearHistory e = code {
134 ccall cleanClearHistory ":V:A"
137 stifleHistory :: !Int !*env -> *env
138 stifleHistory i e = code {
139 ccall cleanStifleHistory "I:V:A"
142 unstifleHistory :: !*env -> *env
143 unstifleHistory e = code {
144 ccall cleanUnstifleHistory ":V:A"
147 historyIsStifled :: !*env -> (!Int, !*env)
148 historyIsStifled e = code {
149 ccall cleanHistoryIsStifled ":I:A"
152 //Information About the History List
153 historyList :: !*env -> (![HistoryItem], !*env)
155 # ({entries,offset,flags}, e) = historyGetHistoryState e
158 whereHistory :: !*env -> (!Int, !*env)
160 # ({entries,offset,flags}, e) = historyGetHistoryState e
163 currentHistory :: !*env -> (!Maybe HistoryItem, !*env)
165 # ({entries,offset,flags}, e) = historyGetHistoryState e
166 = (if (isEmpty entries) Nothing (Just (entries!!offset)), e)
168 historyGet :: !Int !*env -> (!Maybe HistoryItem, !*env)
170 # ({entries,offset,flags}, e) = historyGetHistoryState e
171 = (if (isEmpty entries) Nothing (Just (entries!!i)), e)
173 historyGetTime :: HistoryItem -> String
174 historyGetTime {line,timestamp} = timestamp
176 historyTotalBytes :: !*env -> (!Int, !*env)
177 historyTotalBytes e = code {
178 ccall cleanHistoryTotalBytes ":I:A"
181 //Moving Around the History List
182 historySetPos :: !Int !*env -> (!Int, !*env)
183 historySetPos i e = code {
184 ccall cleanHistorySetPos "I:I:A"
187 previousHistory :: !*env -> (!Maybe HistoryItem, !*env)
189 # (line, timestamp, null, e) = previousHistoryItem e
190 = (if null Nothing (Just {HistoryItem | line=line, timestamp=timestamp}), e)
192 previousHistoryItem :: !*env -> (!String, !String, !Bool, !*env)
193 previousHistoryItem e = code {
194 ccall cleanPreviousHistory ":VSSI:A"
197 nextHistory :: !*env -> (!Maybe HistoryItem, !*env)
199 # (line, timestamp, null, e) = nextHistoryItem e
200 = (if null Nothing (Just {HistoryItem | line=line, timestamp=timestamp}), e)
202 nextHistoryItem :: !*env -> (!String, !String, !Bool, !*env)
203 nextHistoryItem e = code {
204 ccall cleanNextHistory ":VSSI:A"
207 //Searching the History List
208 historySearch :: !String !Int !*env-> (!Int, !*env)
209 historySearch s i e = code {
210 ccall cleanHistorySearch "SI:I:A"
213 historySearchPrefix :: !String !Int !*env-> (!Int, !*env)
214 historySearchPrefix s i e = code {
215 ccall cleanHistorySearchPrefix "SI:I:A"
218 historySearchPos :: !String !Int !Int !*env-> (!Int, !*env)
219 historySearchPos s i1 i2 e = code {
220 ccall cleanHistorySearchPos "SI:I:A"
224 //Managing the History File
225 readHistory :: !String !*env -> (!Bool, !*env)
226 readHistory s e = code {
227 ccall cleanReadHistory "S:I:A"
230 readHistoryRange :: !String !Int !Int !*env -> (!Bool, !*env)
231 readHistoryRange s i1 i2 e = code {
232 ccall cleanReadHistoryRange "SII:I:A"
235 writeHistory :: !String !*env -> (!Bool, !*env)
236 writeHistory s e = code {
237 ccall cleanWriteHistory "S:I:A"
240 appendHistory :: !Int !String !*env -> (!Bool, !*env)
241 appendHistory i s e = code {
242 ccall cleanWriteHistory "IS:I:A"
245 historyTruncateFile :: !String !Int !*env -> (!Bool, !*env)
246 historyTruncateFile i s e = code {
247 ccall cleanWriteHistory "SI:I:A"
250 historyExpand :: !String !*env -> (!String, !Int, !*env)
251 getHistoryEvent :: !String !Int !Int !*env -> (!String, !*env)
252 historyTokenize :: !String !*env -> ([String], !*env)
253 historyArgExtract :: !Int !Int !String !*env -> (!String, !*env)