Index: example.c =================================================================== --- example.c (revision 3268) +++ example.c (working copy) @@ -45,7 +45,7 @@ int main(int argc, char **argv) { * * The typed string is returned as a malloc() allocated string by * linenoise, so the user needs to free() it. */ - while((line = linenoise("hello> ")) != NULL) { + while((line = linenoiseInitial("hello> ", "loads of promises")) != NULL) { /* Do something with the string. */ if (line[0] != '\0' && line[0] != '/') { printf("echo: '%s'\n", line); Index: linenoise.c =================================================================== --- linenoise.c (revision 3268) +++ linenoise.c (working copy) @@ -735,14 +735,11 @@ static int linenoiseEdit(int stdin_fd, int stdout_ l.buflen = buflen; l.prompt = prompt; l.plen = strlen(prompt); - l.oldpos = l.pos = 0; - l.len = 0; + l.oldpos = l.pos = l.len = strlen(buf); l.cols = getColumns(stdin_fd, stdout_fd); l.maxrows = 0; l.history_index = 0; - /* Buffer starts empty. */ - l.buf[0] = '\0'; l.buflen--; /* Make sure there is always space for the nulterm */ /* The latest history entry is always our current buffer, that @@ -750,6 +747,7 @@ static int linenoiseEdit(int stdin_fd, int stdout_ linenoiseHistoryAdd(""); if (write(l.ofd,prompt,l.plen) == -1) return -1; + if (write(l.ofd,l.buf,l.len) == -1) return -1; while(1) { char c; int nread; @@ -976,6 +974,7 @@ char *linenoise(const char *prompt) { } return strdup(buf); } else { + buf[0] = 0; count = linenoiseRaw(buf,LINENOISE_MAX_LINE,prompt); if (count == -1) return NULL; return strdup(buf); @@ -982,6 +981,23 @@ char *linenoise(const char *prompt) { } } +char *linenoiseInitial(const char *prompt, const char *initial) { + char buf[LINENOISE_MAX_LINE]; + int count; + + if (isUnsupportedTerm() || !isatty(STDIN_FILENO)) { + return NULL; + } else { + if (initial) + snprintf(buf, sizeof(buf), "%s", initial); + else + buf[0] = 0; + count = linenoiseRaw(buf,LINENOISE_MAX_LINE,prompt); + if (count == -1) return NULL; + return strdup(buf); + } +} + /* ================================ History ================================= */ /* Free the history, but does not reset it. Only used when we have to Index: linenoise.h =================================================================== --- linenoise.h (revision 3268) +++ linenoise.h (working copy) @@ -53,6 +53,7 @@ void linenoiseSetCompletionCallback(linenoiseCompl void linenoiseAddCompletion(linenoiseCompletions *, const char *); char *linenoise(const char *prompt); +char *linenoiseInitial(const char *prompt, const char *initial); int linenoiseHistoryAdd(const char *line); int linenoiseHistorySetMaxLen(int len); int linenoiseHistorySave(const char *filename);