readline completion

This commit is contained in:
Vitor Santos Costa 2016-04-19 22:42:44 +01:00
parent 96f03e26e8
commit 3d68f0e06b

View File

@ -25,8 +25,8 @@ static char SccsId[] = "%W% %G%";
*/
#include "Yap.h"
#include "Yatom.h"
#include "YapHeap.h"
#include "Yatom.h"
#include "yapio.h"
#include <stdlib.h>
#if HAVE_UNISTD_H
@ -53,8 +53,8 @@ static char SccsId[] = "%W% %G%";
#if USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#include <readline/readline.h>
static int ReadlineGetc(int);
static int ReadlinePutc(int, int);
@ -129,27 +129,130 @@ static char *atom_generator(const char *prefix, int state) {
return s;
}
static char **prolog_completion(const char *text, int start, int end) {
char **matches = NULL;
typedef struct chain {
struct chain *next;
char data[2];
} chain_t;
if ((start == 1 && rl_line_buffer[0] == '[') || /* [file */
(start == 2 && strncmp(rl_line_buffer, "['", 2)))
matches = rl_completion_matches((char *)text, /* for pre-4.2 */
rl_filename_completion_function);
else
matches = rl_completion_matches((char *)text, atom_generator);
static char *predicate_enumerate(const char *prefix, int state) {
CACHE_REGS
PredEntry *p;
ModEntry m0, *mod;
AtomEntry *ap;
return matches;
if (!state) {
p = NULL;
mod = &m0;
m0.NextME = CurrentModules;
if (mod->AtomOfME == AtomIDB)
mod = mod->NextME;
} else {
Term cmod;
p = LOCAL_SearchPreds;
cmod = (p->ModuleOfPred != PROLOG_MODULE ? p->ModuleOfPred : TermProlog );
mod = Yap_GetModuleEntry(cmod);
}
while (mod) {
// move to next o;
if (p)
p = p->NextPredOfModule;
while (p == NULL) {
mod = mod->NextME;
if (!mod) {
// done
LOCAL_SearchPreds = NULL;
return NULL;
}
if (mod->AtomOfME == AtomIDB)
mod = mod->NextME;
p = mod->PredForME;
}
char *c = RepAtom(ap = NameOfPred(p))->StrOfAE;
if (strlen(c) > strlen(prefix) &&
strstr(c, prefix) == c &&
!(p->PredFlags & HiddenPredFlag)) {
LOCAL_SearchPreds = p;
arity_t ar = p->ArityOfPE;
int l, r;
if (Yap_IsPrefixOp(AbsAtom(ap), &l, &r) && ar == 1) {
return c;
}
size_t sz = strlen(c);
chain_t *el = (chain_t *)malloc(sizeof(chain_t)+sz);
strncpy(LOCAL_FileNameBuf, c, YAP_FILENAME_MAX);
strncat(LOCAL_FileNameBuf, "(", YAP_FILENAME_MAX);
return LOCAL_FileNameBuf;
}
}
LOCAL_SearchPreds = NULL;
return NULL;
}
void Yap_ReadlineFlush(int sno) {
static char *predicate_generator(const char *prefix, int state) {
char *s = predicate_enumerate(prefix, state);
if (s) {
char *copy = malloc(1 + strlen(s));
if (copy) /* else pretend no completion */
strcpy(copy, s);
s = copy;
}
return s;
}
static char **prolog_completion(const char *text, int start, int end) {
char **matches = NULL;
if (start == 0 && isalpha(text[0])) {
int i = 0;
while (i < end) {
if (isalnum(text[i]))
i++;
else
break;
}
if (i == end) {
matches = rl_completion_matches((char *)text, predicate_generator);
}
return matches;
} else if (start == 0) {
int i = 0;
const char *p;
while (isblank(text[i++]) && i <= end)
;
p = text + i;
if ((strstr(p,"[") == p) || (strstr(p,"compile(") == p) ||
(strstr(p,"consult(") == p) || (strstr(p,"load_files(") == p) ||
(strstr(p,"reconsult(") == p) || (strstr(p,"use_module(") == p))
matches = rl_completion_matches((char *)text, /* for pre-4.2 */
rl_filename_completion_function);
return matches;
}
int i = end, ch = '\0';
while (i > start) {
ch = text[-i];
if (isalnum(text[i]))
continue;
break;
}
if (islower(ch))
return rl_completion_matches((char *)text, atom_generator);
return NULL;
}
void Yap_ReadlineFlush(int sno) {
if (GLOBAL_Stream[sno].status & Tty_Stream_f &&
GLOBAL_Stream[sno].status & Output_Stream_f) {
rl_redisplay();
}
}
}
bool Yap_ReadlinePrompt(StreamDesc *s) {
bool Yap_ReadlinePrompt(StreamDesc * s) {
if (s->status & Tty_Stream_f) {
s->stream_getc = ReadlineGetc;
if (GLOBAL_Stream[0].status & Tty_Stream_f &&
@ -158,9 +261,9 @@ bool Yap_ReadlinePrompt(StreamDesc *s) {
return true;
}
return false;
}
}
bool Yap_ReadlineOps(StreamDesc *s) {
bool Yap_ReadlineOps(StreamDesc * s) {
if (s->status & Tty_Stream_f) {
if (GLOBAL_Stream[0].status & Tty_Stream_f &&
is_same_tty(s->file, GLOBAL_Stream[0].file))
@ -170,9 +273,9 @@ bool Yap_ReadlineOps(StreamDesc *s) {
return true;
}
return false;
}
}
static int prolog_complete(int ignore, int key) {
static int prolog_complete(int ignore, int key) {
if (rl_point > 0 && rl_line_buffer[rl_point - 1] != ' ') {
#if HAVE_DECL_RL_CATCH_SIGNALS_ /* actually version >= 1.2, or true readline \
*/
@ -189,9 +292,9 @@ static int prolog_complete(int ignore, int key) {
rl_complete(ignore, key);
return 0;
}
}
bool Yap_InitReadline(Term enable) {
bool Yap_InitReadline(Term enable) {
// don't call readline within emacs
// if (getenv("ËMACS"))
// return;
@ -206,11 +309,10 @@ bool Yap_InitReadline(Term enable) {
rl_outstream = stderr;
using_history();
const char *s = Yap_AbsoluteFile("~/.YAP.history", NULL, true);
if (!read_history(s)) {
FILE *f = fopen(s, "w");
if (read_history(s) != 0) {
FILE *f = fopen(s, "a");
if (f) {
fclose(f);
read_history(s);
}
}
rl_readline_name = "Prolog";
@ -221,9 +323,9 @@ bool Yap_InitReadline(Term enable) {
rl_add_defun("prolog-complete", (void *)prolog_complete, '\t');
#endif
return Yap_ReadlineOps(GLOBAL_Stream + StdInStream);
}
}
static bool getLine(int inp, int out) {
static bool getLine(int inp, int out) {
CACHE_REGS
rl_instream = GLOBAL_Stream[inp].file;
rl_outstream = GLOBAL_Stream[out].file;
@ -260,13 +362,14 @@ static bool getLine(int inp, int out) {
return false;
if (myrl_line[0] != '\0' && myrl_line[1] != '\0') {
add_history((char *)myrl_line);
append_history(1, history_file);
write_history(history_file);
fflush(NULL);
}
s->u.irl.ptr = s->u.irl.buf = myrl_line;
return true;
}
}
static int ReadlinePutc(int sno, int ch) {
static int ReadlinePutc(int sno, int ch) {
CACHE_REGS
StreamDesc *s = &GLOBAL_Stream[sno];
#if MAC || _MSC_VER || defined(__MINGW32__)
@ -281,15 +384,15 @@ static int ReadlinePutc(int sno, int ch) {
LOCAL_newline = true;
}
return ((int)ch);
}
}
/**
/**
@brief reading from the console is complicated because we need to
know whether to prompt and so on...
EOF must be handled by resetting the file.
*/
static int ReadlineGetc(int sno) {
*/
static int ReadlineGetc(int sno) {
StreamDesc *s = &GLOBAL_Stream[sno];
int ch;
bool fetch = (s->u.irl.buf == NULL);
@ -306,17 +409,17 @@ static int ReadlineGetc(int sno) {
return EOF;
}
return console_post_process_read_char(ch, s);
}
}
/**
/**
@brief Yap_ReadlinePeekChar peeks the next char from the
readline buffer, but does not actually grab it.
The idea is to take advantage of the buffering. Special care must be taken
with EOF, though.
*/
Int Yap_ReadlinePeekChar(int sno) {
*/
Int Yap_ReadlinePeekChar(int sno) {
StreamDesc *s = &GLOBAL_Stream[sno];
int ch;
@ -343,9 +446,9 @@ Int Yap_ReadlinePeekChar(int sno) {
return EOF;
}
return ch;
}
}
int Yap_ReadlineForSIGINT(void) {
int Yap_ReadlineForSIGINT(void) {
CACHE_REGS
int ch;
StreamDesc *s = &GLOBAL_Stream[StdInStream];
@ -368,20 +471,20 @@ int Yap_ReadlineForSIGINT(void) {
return ch;
}
}
}
}
static Int has_readline(USES_REGS1) {
static Int has_readline(USES_REGS1) {
#if USE_READLINE
return true;
#else
return false;
#endif
}
}
void Yap_InitReadlinePreds(void) {
void Yap_InitReadlinePreds(void) {
Yap_InitCPred("$has_readline", 0, has_readline,
SafePredFlag | HiddenPredFlag);
}
}
#else
bool Yap_InitReadline(Term enable) {