Merge pull request #3 from dopefishh/standard-maybe
[CleanReadLine.git] / ReadLine.icl
index b8c3053..4d15493 100644 (file)
 implementation module ReadLine
 
 import StdEnv
+import Data.Maybe
 
 import code from "readLine.o"
 
-readLine :: !String !Bool !*env -> (!String, !*env)
-readLine s h e = code {
-                       ccall cleanReadLine "SI:S:A"
+instance toString HistoryItem where
+       toString {line,timestamp} = line +++ " (" +++ timestamp +++ ")"
+instance toString HistoryState where
+       toString {entries,offset,flags} = "[" +++ toS entries +++ "]\n" +++ 
+                       "offset: " +++ toString offset +++ "\nflags: " +++ toString flags
+               where
+                       toS :: [HistoryItem] -> String
+                       toS [] = "--empty--"
+                       toS [x] = toString x
+                       toS [x:xs] = toString x +++ ","
+
+//Readline
+readLine :: !String !Bool !*env -> (!Maybe String, !*env)
+readLine s h e
+# (s, eof, e) = readLineEof s h e
+= (if eof Nothing (Just s), e)
+       where
+               readLineEof :: !String !Bool !*env -> (!String, !Bool, !*env)
+               readLineEof s h e = code {
+                               ccall cleanReadLine "SI:VSI:A"
+                       }
+
+setReadLineName :: !String !*env -> *env
+setReadLineName s e = code {
+               ccall cleanSetReadLineName "S:V:A"
+       }
+
+//Initializing History and State Management
+usingHistory :: !*env -> *env
+usingHistory e = code {
+               ccall cleanUsingHistory ":V:A"
+       }
+
+historyGetHistoryState :: !*env -> (!HistoryState, !*env)
+historyGetHistoryState e
+# (offset, num, flags, e) = getState e
+# (entries, e) = getItems num e
+= ({HistoryState | entries=entries, offset=offset, flags=flags}, e)
+       where
+               getState :: !*env -> (!Int, !Int, !Int, !*env)
+               getState e= code {
+                               ccall cleanGetState ":VIII:A"
+                       }
+               getItems :: !Int !*env -> (![HistoryItem], !*env)
+               getItems 0 e = ([], e)
+               getItems i e
+               # (line, timestamp, e) = getItem (i-1) e
+               # (rest, e) = getItems (i-1) e
+               = ([{line=line,timestamp=timestamp}:rest], e)
+
+               getItem :: !Int !*env -> (!String, !String, !*env)
+               getItem i e = code {
+                               ccall cleanGetHistoryItem "I:VSS:A"
+                       }
+               
+
+historySetHistoryState :: !HistoryState !*env -> *env
+historySetHistoryState {entries,offset,flags} e
+# e = initNewHistoryState offset flags (length entries) e
+# e = setItems entries 0 e
+= commit e
+       where
+               initNewHistoryState :: !Int !Int !Int !*env -> *env
+               initNewHistoryState o f l e = code {
+                               ccall cleanInitNewHistoryState "III:V:A"
+                       }
+               setItems :: ![HistoryItem] !Int !*env -> *env
+               setItems [] _ e = e
+               setItems [x:xs] i e
+               # e = setItem i x.line x.timestamp e
+               = setItems xs (i+1) e
+
+               setItem :: !Int !String !String !*env -> *env
+               setItem i l t e = code {
+                               ccall cleanSetNewHistoryEntry "ISS:V:A"
+                       }
+               
+               commit :: !*env -> *env
+               commit e = code {
+                               ccall cleanCommitSetHistory ":V:A"
+                       }
+
+//History List Management
+addHistory :: !String !*env -> *env
+addHistory s e = code {
+               ccall cleanAddHistory "S:V:A"
        }
 
+addHistoryTime :: !String !*env -> *env
+addHistoryTime s e = code {
+               ccall cleanAddHistoryTime "S:V:A"
+       }
+
+removeHistory :: !Int !*env -> (!HistoryItem, !*env)
+removeHistory i e
+# (line, timestamp, e) = removeHistoryItem i e
+= ({HistoryItem | line=line, timestamp=timestamp}, e)
+       where
+               removeHistoryItem :: !Int !*env -> (!String, !String, !*env)
+               removeHistoryItem i e = code {
+                               ccall cleanRemoveHistory "I:VSS:A"
+                       }
+
+replaceHistoryEntry :: !Int !String !*env -> (!HistoryItem, !*env)
+replaceHistoryEntry i s e
+# (line, timestamp, e) = replaceItem i s e
+= ({HistoryItem | line=line, timestamp=timestamp}, e)
+       where
+               replaceItem :: !Int !String !*env -> (!String, !String, !*env)
+               replaceItem i s e = code {
+                               ccall cleanReplaceHistoryEntry "IS:VSS:A"
+                       }
+
+clearHistory :: !*env -> *env
+clearHistory e = code {
+               ccall cleanClearHistory ":V:A"
+       }
+
+stifleHistory :: !Int !*env -> *env
+stifleHistory i e = code {
+               ccall cleanStifleHistory "I:V:A"
+       }
+
+unstifleHistory :: !*env -> *env
+unstifleHistory e = code {
+               ccall cleanUnstifleHistory ":V:A"
+       }
+
+historyIsStifled :: !*env -> (!Int, !*env)
+historyIsStifled e = code {
+               ccall cleanHistoryIsStifled ":I:A"
+       }
+
+//Information About the History List
+historyList :: !*env -> (![HistoryItem], !*env)
+historyList e
+# ({entries,offset,flags}, e) = historyGetHistoryState e
+= (entries, e)
+
+whereHistory :: !*env -> (!Int, !*env)
+whereHistory e
+# ({entries,offset,flags}, e) = historyGetHistoryState e
+= (offset, e)
+
+currentHistory :: !*env -> (!Maybe HistoryItem, !*env)
+currentHistory e
+# ({entries,offset,flags}, e) = historyGetHistoryState e
+= (if (isEmpty entries) Nothing (Just (entries!!offset)), e)
+
+historyGet :: !Int !*env -> (!Maybe HistoryItem, !*env)
+historyGet i e
+# ({entries,offset,flags}, e) = historyGetHistoryState e
+= (if (isEmpty entries) Nothing (Just (entries!!i)), e)
+
+historyGetTime :: HistoryItem -> String
+historyGetTime {line,timestamp} = timestamp
+
+historyTotalBytes :: !*env -> (!Int, !*env)
+historyTotalBytes e = code {
+               ccall cleanHistoryTotalBytes ":I:A"
+       }
+
+//Moving Around the History List
+historySetPos :: !Int !*env -> (!Int, !*env)
+historySetPos i e = code {
+               ccall cleanHistorySetPos "I:I:A"
+       }
+
+previousHistory :: !*env -> (!Maybe HistoryItem, !*env)
+previousHistory e
+# (line, timestamp, null, e) = previousHistoryItem e
+= (if null Nothing (Just {HistoryItem | line=line, timestamp=timestamp}), e)
+       where
+               previousHistoryItem :: !*env -> (!String, !String, !Bool, !*env)
+               previousHistoryItem e = code {
+                               ccall cleanPreviousHistory ":VSSI:A"
+                       }
+
+nextHistory :: !*env -> (!Maybe HistoryItem, !*env)
+nextHistory e
+# (line, timestamp, null, e) = nextHistoryItem e
+= (if null Nothing (Just {HistoryItem | line=line, timestamp=timestamp}), e)
+       where
+               nextHistoryItem :: !*env -> (!String, !String, !Bool, !*env)
+               nextHistoryItem e = code {
+                               ccall cleanNextHistory ":VSSI:A"
+                       }
+
+//Searching the History List
+historySearch :: !String !Int !*env-> (!Int, !*env)
+historySearch s i e = code {
+               ccall cleanHistorySearch "SI:I:A"
+       }
+
+historySearchPrefix :: !String !Int !*env-> (!Int, !*env)
+historySearchPrefix s i e = code {
+               ccall cleanHistorySearchPrefix "SI:I:A"
+       }
+       
+historySearchPos :: !String !Int !Int !*env-> (!Int, !*env)
+historySearchPos s i1 i2 e = code {
+               ccall cleanHistorySearchPos "SI:I:A"
+       }
+       
+
+//Managing the History File
+readHistory :: !String !*env -> (!Bool, !*env)
+readHistory s e = code {
+               ccall cleanReadHistory "S:I:A"
+       }
+
+readHistoryRange :: !String !Int !Int !*env -> (!Bool, !*env)
+readHistoryRange s i1 i2 e = code {
+               ccall cleanReadHistoryRange "SII:I:A"
+       }
+
+writeHistory :: !String !*env -> (!Bool, !*env)
+writeHistory s e = code {
+               ccall cleanWriteHistory "S:I:A"
+       }
+
+appendHistory :: !Int !String !*env -> (!Bool, !*env)
+appendHistory i s e = code {
+               ccall cleanWriteHistory "IS:I:A"
+       }
+               
+historyTruncateFile :: !String !Int !*env -> (!Bool, !*env)
+historyTruncateFile i s e = code {
+               ccall cleanWriteHistory "SI:I:A"
+       }
+
+historyExpand :: !String !*env -> (!String, !Int, !*env)
+historyExpand s e = code {
+               ccall cleanHistoryExpand "S:VSI:A"      
+       }
+
+getHistoryEvent :: !String !Int !Int !*env -> (!Maybe String, !Int, !*env)
+getHistoryEvent s i1 i2 e
+# (string, cindex, null, e) = getHistoryEventItem s i1 i2 e
+= (if null Nothing (Just string), cindex, e)
+       where
+               getHistoryEventItem :: !String !Int !Int !*env -> (!String, !Int, !Bool, !*env)
+               getHistoryEventItem s i1 i2 e = code {
+                               ccall cleanGetHistoryEvent "SII:VSII:A"
+                       }
+
+historyTokenize :: !String !*env -> ([String], !*env)
+historyTokenize s e
+# (i, e) = historyTokenizeInit s e
+= historyTokenizeItems i e
+//= ([], e)
+       where
+               historyTokenizeInit :: !String !*env -> (!Int, !*env)
+               historyTokenizeInit s e = code {
+                               ccall cleanHistoryTokenizeInit "S:I:A"
+                       }
+
+               historyTokenizeItems :: !Int !*env -> (![String], !*env)
+               historyTokenizeItems 0 e = ([], e)
+               historyTokenizeItems i e
+               # (token, e) = historyTokenizeItem (i-1) e
+               # (rest, e) = historyTokenizeItems (i-1) e
+               = ([token:rest], e)
+
+               historyTokenizeItem :: !Int !*env -> (!String, !*env)
+               historyTokenizeItem i e = code {
+                               ccall cleanHistoryTokenizeItem "I:S:A"
+                       }
+
+historyArgExtract :: !Int !Int !String !*env -> (!Maybe String, !*env)
+historyArgExtract i1 i2 s e
+# (string, null, e) = historyArgExtractItem i1 i2 s e
+= (if null Nothing (Just string), e)
+       where
+               historyArgExtractItem :: !Int !Int !String !*env -> (!String, !Bool, !*env)
+               historyArgExtractItem i1 i2 s e = code {
+                               ccall cleanHistoryArgExtract "IIS:VSI:A"
+                       }