Enable input editing with the linenoise library.
authorEric S. Raymond <esr@thyrsus.com>
Sat, 10 Jun 2017 09:11:29 +0000 (05:11 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Sat, 10 Jun 2017 09:11:29 +0000 (05:11 -0400)
NEWS
advent.adoc
advent.h
main.c
misc.c

diff --git a/NEWS b/NEWS
index 46acfa3eb49dc68aab156eca097695a628e0e1f6..0614e59643159874f060cc4ae79054f7bc728498 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@
 
 Repository head::
   Include tests directory in generated tarball.
 
 Repository head::
   Include tests directory in generated tarball.
+  Support command-line editing with arrow keys and Emacs keystrokes.
 
 1.0: 2016-06-05::
   Forward port of Crowther & Woods's 430-point Adventure 2.5.
 
 1.0: 2016-06-05::
   Forward port of Crowther & Woods's 430-point Adventure 2.5.
index a53ccd3c77eb56287740c10f046ea869dca1bf89..1005809073ec99cb3675f83c9e1d301cfa374a88 100644 (file)
@@ -25,11 +25,19 @@ PDP-10 on which it originally ran limited filenames to 6 characters.
 This version is released as open source with the permission and
 encouragement of the original authors.
 
 This version is released as open source with the permission and
 encouragement of the original authors.
 
+Unlike the original, this version supports use of your arrow keys to edit
+your command line in place.  Basic Emacs keystrokes are supported, and
+your up/down arrows access a command history.
+
+To exit the game, type Ctrl-D (EOF).
+
+There have been no gameplay changes.
+
 == OPTIONS ==
 
 -l:: Log commands to specified file.
 
 == OPTIONS ==
 
 -l:: Log commands to specified file.
 
--o:: Old-style.  Restores original interface, no prompt.
+-o:: Old-style.  Restores original interface, no prompt or command history.
 
 == BUGS ==
 
 
 == BUGS ==
 
index 51e7317dc9fdab1a535a8997b4b92b20585d581a..9e50d9ce45254a4dc729bea06a263b0078b7682f 100644 (file)
--- a/advent.h
+++ b/advent.h
@@ -118,7 +118,7 @@ extern long SETBIT(long);
 extern bool TSTBIT(long,int);
 extern long RNDVOC(long,long);
 extern void BUG(long) __attribute__((noreturn));
 extern bool TSTBIT(long,int);
 extern long RNDVOC(long,long);
 extern void BUG(long) __attribute__((noreturn));
-extern void MAPLIN(FILE *);
+extern bool MAPLIN(FILE *);
 extern void TYPE(void);
 
 extern void fSAVEIO(long,long,long*);
 extern void TYPE(void);
 
 extern void fSAVEIO(long,long,long*);
diff --git a/main.c b/main.c
index 4a5d3c5deb797dd22e4d5a9017c03c6109aa85f5..31eaf4691e9e68131b9f9530941e7a750204dd65 100644 (file)
--- a/main.c
+++ b/main.c
@@ -11,6 +11,7 @@
 #include <time.h>
 #include "advent.h"
 #include "database.h"
 #include <time.h>
 #include "advent.h"
 #include "database.h"
+#include "linenoise/linenoise.h"
 
 struct game_t game;
 
 
 struct game_t game;
 
@@ -82,6 +83,8 @@ case 'l':
        }
     }
 
        }
     }
 
+    linenoiseHistorySetMaxLen(350);
+
     /* Logical variables:
      *
      *  game.closed says whether we're all the way closed
     /* Logical variables:
      *
      *  game.closed says whether we're all the way closed
diff --git a/misc.c b/misc.c
index b991117222a3ccd9e4dd4263ae7b1ec8610aad2e..0eae1623a25bc4c95539df1825435ff628087f5d 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -703,6 +703,7 @@ void BUG(long num)
 bool MAPLIN(FILE *fp)
 {
     long i, val;
 bool MAPLIN(FILE *fp)
 {
     long i, val;
+    bool eof;
 
     /*  Read a line of input, from the specified input source,
      *  translate the chars to integers in the range 0-126 and store
 
     /*  Read a line of input, from the specified input source,
      *  translate the chars to integers in the range 0-126 and store
@@ -729,13 +730,23 @@ bool MAPLIN(FILE *fp)
      *  and is not changed thereafter unless the routines on this page choose
      *  to do so. */
 
      *  and is not changed thereafter unless the routines on this page choose
      *  to do so. */
 
-    if (!oldstyle && fp == stdin)
-       fputs("> ", stdout);
     do {
     do {
-       IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp));
+       if (oldstyle) {
+           IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp));
+           eof = (feof(fp));
+       } else {
+           char *cp = linenoise("> ");
+           eof = (cp == NULL);
+           if (!eof) {
+               strncpy(rawbuf, cp, sizeof(rawbuf)-1);
+               linenoiseHistoryAdd(rawbuf);
+               strncat(rawbuf, "\n", sizeof(rawbuf)-1);
+               linenoiseFree(cp);
+           }
+       }
     } while
     } while
-           (!feof(fp) && rawbuf[0] == '#');
-    if (feof(fp)) {
+           (!eof && rawbuf[0] == '#');
+    if (eof) {
        if (logfp && fp == stdin)
            fclose(logfp);
        return false;
        if (logfp && fp == stdin)
            fclose(logfp);
        return false;