While perusing through readline's source code, I went ahead and added a couple of functions that expose more of the API(? if that's the right term).

Specifically, they expose the where_history(), current_history(), previous_history(), next_history(), history_search(), and history_set_pos()functions, as well as the history_length variable.

I'm not really sure where to send the patches... I thought that originally I was supposed to send them to this mailing list, but then I read somewhere that they should be sent to chicken-hackers, but we're supposed to report bugs to chicken-users and send patches and what-not :S

I even ran salmonella and it seemed to check out okay.

--
Alexej Magura

--- readline.scm	2014-12-11 01:57:27.105532179 -0700
+++ readline.scm.new	2014-12-11 01:41:14.946431613 -0700
@@ -102,6 +102,12 @@
      gnu-readline-truncate-history
      gnu-history-new-lines
      gnu-history-install-file-manager
+     gnu-history-line
+     gnu-history-position
+     gnu-search-history
+     gnu-search-history-forward
+     gnu-search-history-backward
+     gnu-history-list-length
  
      gnu-readline-parse-and-bind
      gnu-readline-set-bounce-ms
@@ -169,6 +175,43 @@
 (define gnu-history-new-lines
     (foreign-lambda int "gnu_history_new_lines"))
 
+;; (gnu-history-line (> offset 0)) -> next history line
+;; (gnu-history-line (< offset 0)) -> previous history line
+;; (gnu-history-line) -> current history line
+;; returns #f on fail
+(define (gnu-history-line #!optional offset)
+  ((foreign-lambda c-string "gnu_history_get_line" int) (or offset 0)))
+
+;; (gnu-history-search "string" -1) -> search through previous entries
+;; (gnu-history-search "string" 0+) -> search through subsequent entries.
+;; returns match on succ, #f on fail.
+;; XXX use `(get-keyword)' to access offset:, match:, and index:
+;; XXX `index:' corresponds to the history-position within history_list of the match
+(define (gnu-search-history string direction)
+  (define offset ((foreign-lambda int "history_search" c-string int)
+                string direction))
+  (if (= offset -1)
+    #f
+    (list offset: offset
+          match: (gnu-history-line 0)
+          index: (gnu-history-position))))
+
+(define (gnu-search-history-backward string)
+  (gnu-search-history string -1))
+
+(define (gnu-search-history-forward string)
+  (gnu-search-history string 0))
+
+(define gnu-history-list-length
+  (foreign-lambda int "gnu_history_list_length"))
+
+;; (gnu-history-position pos) -> sets the current history position: 0 succ, 1 fail
+;; (gnu-history-position) -> current history position within history_list
+(define (gnu-history-position #!optional pos)
+  (if pos
+    (= 1 ((foreign-lambda int "history_set_pos" int) pos))
+    ((foreign-lambda int "where_history"))))
+
 ;; Useful...
 (define gnu-readline-parse-and-bind
   (foreign-lambda int "rl_parse_and_bind" c-string))
--- readline-egg.c	2014-12-11 01:57:27.105532179 -0700
+++ readline-egg.c.new	2014-12-11 01:41:21.633164305 -0700
@@ -63,7 +63,7 @@
 		return pos + 1;
 
 	while (pos >= 0) {
-		open++;
+		++open;
 		pos = gnu_readline_skip(pos, open_key, close_key);
 	}
 
@@ -90,7 +90,7 @@
 }
 
 // Delays, but returns early if key press occurs 
-void gnu_readline_timid_delay(ms)
+void gnu_readline_timid_delay(int ms)
 {
 	struct pollfd pfd;
 	
@@ -268,7 +268,7 @@
 		h = history_get(history_base + history_length - 1);
 		if (NULL == h || 0 != strcmp(h->line, gnu_readline_buf)) {
 			add_history(gnu_readline_buf);
-                        gnu_history_newlines++;
+                        ++gnu_history_newlines;
 		}
 	}
 
@@ -288,3 +288,24 @@
     rl_free_line_state();
     rl_cleanup_after_signal();
 }
+
+char *gnu_history_get_line(int offset)
+{
+  HIST_ENTRY *entry = NULL;
+  if (offset < 0)
+    entry = previous_history();
+  else if (offset > 0)
+    entry = next_history();
+  else
+    entry = current_history();
+
+  // did the operation succeed?
+  if (entry == NULL)
+    return NULL; // no
+  return entry->line; // yes
+}
+
+int gnu_history_list_length()
+{
+  return history_length;
+}
_______________________________________________
Chicken-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to