From 7db925b0daccb9e41a6089624e29ccf5fb3ff1fe Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Thu, 28 Jan 2016 16:47:55 +0100 Subject: [PATCH] full history API implemented --- Clean System Files/readLine.c | 58 ++++++++++++++++++++++++++++++++++- README.md | 2 ++ ReadLine.dcl | 11 +++---- ReadLine.icl | 24 +++++++++++++++ test.icl | 7 +++-- 5 files changed, 91 insertions(+), 11 deletions(-) diff --git a/Clean System Files/readLine.c b/Clean System Files/readLine.c index e3f4def..814541c 100644 --- a/Clean System Files/readLine.c +++ b/Clean System Files/readLine.c @@ -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) diff --git a/README.md b/README.md index 9a5e329..8a70160 100644 --- 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) diff --git a/ReadLine.dcl b/ReadLine.dcl index 7828e88..61f7c15 100644 --- a/ReadLine.dcl +++ b/ReadLine.dcl @@ -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) diff --git a/ReadLine.icl b/ReadLine.icl index 4faf3ba..64a7fa4 100644 --- a/ReadLine.icl +++ b/ReadLine.icl @@ -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) diff --git a/test.icl b/test.icl index 6dd1f5a..66a4996 100644 --- 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) -- 2.20.1