lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>Does anyone have a patch integrating the GNU readline library
>with the interactive lua interpreter?

Here it is. I don't know much about readline, but at least this supports
history, which I think is what most people want.

1. Add -lreadline in the Makefile:

$T: $(OBJS) $(LIB)/liblua.a $(LIB)/liblualib.a
	$(CC) -o $@ $(OBJS) -L$(LIB) -llua -llualib $(EXTRA_LIBS) -lreadline

2. Replace manual_input in lua.c by this:

#include <readline/readline.h>
#include <readline/history.h>

static void manual_input (int version, int prompt) {
  if (version) print_version();
  for (;;) {
    char *buffer;
    if (prompt) {
      const char *s;
      lua_getglobal(L, "_PROMPT");
      s = lua_tostring(L, -1);
      if (!s) s = PROMPT;
      buffer=readline(s);
      lua_pop(L, 1);  /* remove global */
    }
    else
      buffer=readline(NULL);
    if (buffer==NULL) break;
    add_history(buffer);
    ldo(lua_dostring, buffer);
    free(buffer);
    lua_settop(L, 0);  /* remove eventual results */
  }
  printf("\n");
}


--lhf