full history API implemented
authorMart Lubbers <mart@martlubbers.net>
Thu, 28 Jan 2016 15:47:55 +0000 (16:47 +0100)
committerMart Lubbers <mart@martlubbers.net>
Thu, 28 Jan 2016 15:47:55 +0000 (16:47 +0100)
Clean System Files/readLine.c
README.md
ReadLine.dcl
ReadLine.icl
test.icl

index e3f4def..814541c 100644 (file)
@@ -29,7 +29,8 @@ void cleanSetReadLineName(CleanString name)
 }
 
 //Readline functions
-void cleanReadLine(CleanString prompt, int history, CleanString *result, int *eof)
+void cleanReadLine(
+               CleanString prompt, int history, CleanString *result, int *eof)
 {
        char *cs_prompt = cleanStringToCString(prompt);
 
@@ -215,6 +216,61 @@ int cleanHistoryTotalBytes()
 }
 
 //Moving Around the History List
+int cleanHistorySetPos(int pos)
+{
+       return history_set_pos(pos);
+}
+
+void cleanPreviousHistory(
+               CleanString *line, CleanString *timestamp, int *null)
+{
+       *null = 0;
+       HIST_ENTRY *entry = previous_history();
+       char *cs_line = "";
+       char *cs_stamp = "";
+       if(entry == NULL){
+               *null = 1;
+       } else {
+               *null = 0;
+               cs_line = entry->line;
+               cs_stamp = entry->timestamp;
+       }
+
+       CleanStringVariable(cleanLine, strlen(cs_line));
+       *line = (CleanString) cleanLine;
+       memcpy(CleanStringCharacters(cleanLine), cs_line, strlen(cs_line));
+       CleanStringLength(cleanLine) = strlen(cs_line);
+
+       CleanStringVariable(cleanTimestamp, strlen(cs_stamp));
+       *timestamp = (CleanString) cleanTimestamp;
+       memcpy(CleanStringCharacters(cleanTimestamp), cs_stamp, strlen(cs_stamp));
+       CleanStringLength(cleanTimestamp) = strlen(cs_stamp);
+}
+
+void cleanNextHistory(CleanString *line, CleanString *timestamp, int *null)
+{
+       *null = 0;
+       HIST_ENTRY *entry = next_history();
+       char *cs_line = "";
+       char *cs_stamp = "";
+       if(entry == NULL){
+               *null = 1;
+       } else {
+               *null = 0;
+               cs_line = entry->line;
+               cs_stamp = entry->timestamp;
+       }
+
+       CleanStringVariable(cleanLine, strlen(cs_line));
+       *line = (CleanString) cleanLine;
+       memcpy(CleanStringCharacters(cleanLine), cs_line, strlen(cs_line));
+       CleanStringLength(cleanLine) = strlen(cs_line);
+
+       CleanStringVariable(cleanTimestamp, strlen(cs_stamp));
+       *timestamp = (CleanString) cleanTimestamp;
+       memcpy(CleanStringCharacters(cleanTimestamp), cs_stamp, strlen(cs_stamp));
+       CleanStringLength(cleanTimestamp) = strlen(cs_stamp);
+}
 
 //Searching the History List
 int cleanHistorySearch(CleanString s, int dir)
index 9a5e329..8a70160 100644 (file)
--- a/README.md
+++ b/README.md
@@ -24,6 +24,8 @@ All functions try to resemble the original
 [history](http://cnswww.cns.cwru.edu/php/chet/readline/history.html) api
 functions as close as possible
 
+*Please note that some functions will crash if `usingHistory` is not called*
+
 ###Changelog
 
 - 0.3 (2016-01-26)
index 7828e88..61f7c15 100644 (file)
@@ -21,9 +21,7 @@ setReadLineName :: !String !*env -> *env
 readLine :: !String !Bool !*env -> (!Maybe String, !*env)
 
 //Initializing History and State Management
-//Note that this HAS to be executed when you want to add entries when the
-//history has not been used
-usingHistory :: !*env -> *env
+usingHistory :: !*env -> *env 
 historyGetHistoryState :: !*env -> (!HistoryState, !*env)
 historySetHistoryState :: !HistoryState !*env -> *env
 
@@ -46,12 +44,11 @@ historyGetTime :: HistoryItem -> String
 historyTotalBytes :: !*env -> (!Int, !*env)
 
 //Moving Around the History List
-//TODO
+historySetPos :: !Int !*env -> (!Int, !*env)
+previousHistory :: !*env -> (!Maybe HistoryItem, !*env)
+nextHistory :: !*env -> (!Maybe HistoryItem, !*env)
 
 //Searching the History List
-//Note that the return integers are just success flags. The actual found item
-//will be the current history position and the int is the offset of the search
-//within that item...
 historySearch :: !String !Int !*env-> (!Int, !*env)
 historySearchPrefix :: !String !Int !*env-> (!Int, !*env)
 historySearchPos :: !String !Int !Int !*env-> (!Int, !*env)
index 4faf3ba..64a7fa4 100644 (file)
@@ -179,6 +179,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)
index 6dd1f5a..66a4996 100644 (file)
--- a/test.icl
+++ b/test.icl
@@ -32,10 +32,11 @@ Start w
 #! (i, w) = historyIsStifled w
 #! w = unstifleHistory w
 #! (i, w) = historyIsStifled w
+#! (i, w) = historySetPos 1 w
+#! (h, w) = previousHistory w
+#! (h, w) = nextHistory w
 #! (oh, w) = historyGetHistoryState w
 #! w = historySetHistoryState testHistory w
 #! (h, w) = historyGetHistoryState w
 #! w = clearHistory w
-= (
-       toString h,
-       w)
+= (toString h, w)