[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Readline patch?
- From: Reuben Thomas <rrt1001@...>
- Date: Mon, 12 Feb 2001 12:23:36 +0000 (GMT)
On Mon, 12 Feb 2001, Reuben Thomas wrote:
> > Does anyone have a patch integrating the GNU readline library
> > with the interactive lua interpreter? I tried doing one
> > myself but didn't do too well ;)
>
> Yes, I've done this. Here's the diff (-u):
Woops. I hadn't yet seen the "official" version. So to atone, here's a
better patch: first, I call using_history, which I forgot before, secondly,
the lines you type are only entered into the history if they are non-empty,
and thirdly, if you enter a line that you've already entered, then the
previous copy is deleted from the history, and a new one added to the end,
so if you type:
this
that
theother
this
then the history is now
that
theother
this
rather than
this
that
theother
this
which is like setting IGNOREDUPS in bash, and can be most helpful for
navigating in long histories.
Here's the diff:
--- /home/rrt/comp/lang/lua/archives/lua/src/lua/lua.c Fri Oct 20 17:36:32 2000
+++ lua.c Mon Feb 12 12:20:09 2001
@@ -10,6 +10,9 @@
#include <stdlib.h>
#include <string.h>
+#include <readline/readline.h>
+#include <readline/history.h>
+
#include "lua.h"
#include "luadebug.h"
@@ -55,6 +58,8 @@
lua_mathlibopen(L);
lua_dblibopen(L);
/* add your libraries here */
+ lua_rexlibopen(L);
+ lua_bitlibopen(L);
}
@@ -170,40 +175,32 @@
#endif
static void manual_input (int version, int prompt) {
- int cont = 1;
+ char *buffer;
if (version) print_version();
- while (cont) {
- char buffer[MAXINPUT];
- int i = 0;
+ do {
+ const char *s = NULL;
if (prompt) {
- const char *s;
lua_getglobal(L, "_PROMPT");
s = lua_tostring(L, -1);
if (!s) s = PROMPT;
- fputs(s, stdout);
lua_pop(L, 1); /* remove global */
}
- for(;;) {
- int c = getchar();
- if (c == EOF) {
- cont = 0;
- break;
- }
- else if (c == '\n') {
- if (i>0 && buffer[i-1] == '\\')
- buffer[i-1] = '\n';
- else break;
- }
- else if (i >= MAXINPUT-1) {
- fprintf(stderr, "lua: input line too long\n");
- break;
+ buffer = readline(s);
+ if (buffer) {
+ if (buffer[0]) {
+ int pos = where_history(), newpos = history_search(buffer, -1);
+ if (newpos > -1) {
+ HIST_ENTRY *h = remove_history(newpos);
+ free(h->line); free(h->data); free(h);
+ history_set_pos(pos);
+ }
+ add_history(buffer);
}
- else buffer[i++] = (char)c;
+ ldo(lua_dostring, buffer);
+ free(buffer);
}
- buffer[i] = '\0';
- ldo(lua_dostring, buffer);
lua_settop(L, 0); /* remove eventual results */
- }
+ } while (buffer);
printf("\n");
}
@@ -313,10 +310,10 @@
getstacksize(argc, argv, &opt); /* handle option `-s' */
L = lua_open(opt.stacksize); /* create state */
userinit(); /* open libraries */
+ using_history(); /* init history library */
register_getargs(argv); /* create `getargs' function */
status = handle_argv(argv+1, &opt);
if (opt.toclose)
lua_close(L);
return status;
}
-
--
http://sc3d.org/rrt/ | humour, n. unexpected recognition