]> git.lizzy.rs Git - linenoise.git/commitdiff
linenoise: Allow a completion callback user parameter
authorSteve Bennett <steveb@workware.net.au>
Mon, 19 May 2014 10:32:28 +0000 (20:32 +1000)
committerSteve Bennett <steveb@workware.net.au>
Wed, 17 Aug 2016 08:23:06 +0000 (18:23 +1000)
Signed-off-by: Steve Bennett <steveb@workware.net.au>
example.c
linenoise.c
linenoise.h

index b436ed313be8db96b991396807a0c424f168e1fe..f6ce462e6b10710997ba3b3804277f9859bbd652 100644 (file)
--- a/example.c
+++ b/example.c
@@ -4,7 +4,7 @@
 #include "linenoise.h"
 
 #ifndef NO_COMPLETION
-void completion(const char *buf, linenoiseCompletions *lc) {
+void completion(const char *buf, linenoiseCompletions *lc, void *userdata) {
     if (buf[0] == 'h') {
         linenoiseAddCompletion(lc,"hello");
         linenoiseAddCompletion(lc,"hello there");
@@ -35,7 +35,7 @@ int main(int argc, char *argv[]) {
 #ifndef NO_COMPLETION
     /* Set the completion callback. This will be called every time the
      * user uses the <tab> key. */
-    linenoiseSetCompletionCallback(completion);
+    linenoiseSetCompletionCallback(completion, NULL);
 #endif
 
     /* Load history from file. The history file is just a plain text file
index e6f01bc707712dd76f683199ad2b878412ae5d4c..de4f58dcda70c800459de510f96f75779b744e2f 100644 (file)
@@ -1063,6 +1063,7 @@ static int insert_chars(struct current *current, int pos, const char *chars)
 
 #ifndef NO_COMPLETION
 static linenoiseCompletionCallback *completionCallback = NULL;
+static void *completionUserdata = NULL;
 
 static void beep() {
 #ifdef USE_TERMIOS
@@ -1082,7 +1083,7 @@ static int completeLine(struct current *current) {
     linenoiseCompletions lc = { 0, NULL };
     int c = 0;
 
-    completionCallback(current->buf,&lc);
+    completionCallback(current->buf,&lc,completionUserdata);
     if (lc.len == 0) {
         beep();
     } else {
@@ -1135,9 +1136,10 @@ static int completeLine(struct current *current) {
 /* Register a callback function to be called for tab-completion.
    Returns the prior callback so that the caller may (if needed)
    restore it when done. */
-linenoiseCompletionCallback * linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) {
+linenoiseCompletionCallback * linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn, void *userdata) {
     linenoiseCompletionCallback * old = completionCallback;
     completionCallback = fn;
+    completionUserdata = userdata;
     return old;
 }
 
index ed5d81f430346aed96afdbf374c253b315a49a04..ba1b041eae45484911e16aa7e53084ad72cf5fef 100644 (file)
@@ -46,19 +46,19 @@ typedef struct linenoiseCompletions {
 /*
  * The callback type for tab completion handlers.
  */
-typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
+typedef void(linenoiseCompletionCallback)(const char *prefix, linenoiseCompletions *comp, void *userdata);
 
 /*
  * Sets the current tab completion handler and returns the previous one, or NULL
  * if no prior one has been set.
  */
-linenoiseCompletionCallback * linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
+linenoiseCompletionCallback * linenoiseSetCompletionCallback(linenoiseCompletionCallback *comp, void *userdata);
 
 /*
  * Adds a copy of the given string to the given completion list. The copy is owned
  * by the linenoiseCompletions object.
  */
-void linenoiseAddCompletion(linenoiseCompletions *, const char *);
+void linenoiseAddCompletion(linenoiseCompletions *comp, const char *str);
 #endif
 
 /*