Merge pull request #3 from dopefishh/standard-maybe
[CleanReadLine.git] / ReadLine.icl
index 4faf3ba..4d15493 100644 (file)
@@ -1,21 +1,10 @@
 implementation module ReadLine
 
 import StdEnv
+import Data.Maybe
 
 import code from "readLine.o"
 
-//Maybe
-isNothing :: !(Maybe .x) -> Bool
-isNothing Nothing = True
-isNothing _ = False
-
-isJust    :: !(Maybe .x) -> Bool
-isJust Nothing = False
-isJust _ = True
-
-fromJust  :: !(Maybe .x) -> .x
-fromJust (Just x) = x
-
 instance toString HistoryItem where
        toString {line,timestamp} = line +++ " (" +++ timestamp +++ ")"
 instance toString HistoryState where
@@ -179,6 +168,30 @@ historyTotalBytes e = code {
        }
 
 //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)
@@ -222,3 +235,51 @@ 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"
+                       }