Reordered and reformatted, and added some cool new features:
set_completer(function) parse_and_bind(string) read_init_file(filename) The first is the most exciting feature: with an appropriate Python completer function, it can do dynamic completion based on the contents of your namespace!
This commit is contained in:
parent
ee81af8977
commit
290900a5d7
|
@ -1,70 +1,182 @@
|
||||||
/* The readline module makes GNU readline available to Python. It
|
/* This module makes GNU readline available to Python. It has ideas
|
||||||
* has ideas contributed by Lee Busby, LLNL, and William Magro,
|
* contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
|
||||||
* Cornell Theory Center.
|
* Center. The completer interface was inspired by Lele Gaifax.
|
||||||
|
*
|
||||||
|
* More recently, it was largely rewritten by Guido van Rossum who is
|
||||||
|
* now maintaining it.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef __cplusplus
|
/* Standard definitions */
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
/* Routines needed from outside (but not declared in a header file). */
|
/* GNU readline definitions */
|
||||||
|
#include <readline/readline.h> /* You may need to add an -I option to Setup */
|
||||||
|
|
||||||
|
/* Pointers needed from outside (but not declared in a header file). */
|
||||||
extern int (*PyOS_InputHook)();
|
extern int (*PyOS_InputHook)();
|
||||||
extern char *readline();
|
|
||||||
extern int rl_initialize();
|
|
||||||
extern int rl_insert();
|
|
||||||
extern int rl_bind_key();
|
|
||||||
extern void add_history();
|
|
||||||
extern char *rl_readline_name;
|
|
||||||
extern int (*rl_event_hook)();
|
|
||||||
extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
|
extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
|
||||||
|
|
||||||
/* This module's initialization routine */
|
|
||||||
void initreadline (void);
|
|
||||||
|
|
||||||
static void PyOS_ReadlineInit();
|
/* Exported function to send one line to readline's init file parser */
|
||||||
static RETSIGTYPE onintr();
|
|
||||||
static char *PyOS_GnuReadline();
|
static PyObject *
|
||||||
static jmp_buf jbuf;
|
parse_and_bind(self, args)
|
||||||
static PyObject *ReadlineError;
|
PyObject *self;
|
||||||
static int already_initialized = 0;
|
PyObject *args;
|
||||||
static char readline_module_documentation[] =
|
{
|
||||||
"Readline Module, version0.0"
|
char *s;
|
||||||
;
|
if (!PyArg_ParseTuple(args, "s", &s))
|
||||||
|
return NULL;
|
||||||
|
rl_parse_and_bind(s);
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
return Py_None;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char doc_parse_and_bind[] = "\
|
||||||
|
parse_and_bind(string) -> None\n\
|
||||||
|
Parse and execute single line of a readline init file.\
|
||||||
|
";
|
||||||
|
|
||||||
|
|
||||||
|
/* Exported function to parse a readline init file */
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
read_init_file(self, args)
|
||||||
|
PyObject *self;
|
||||||
|
PyObject *args;
|
||||||
|
{
|
||||||
|
char *s = NULL;
|
||||||
|
if (!PyArg_ParseTuple(args, "|z", &s))
|
||||||
|
return NULL;
|
||||||
|
errno = rl_read_init_file(s);
|
||||||
|
if (errno)
|
||||||
|
return PyErr_SetFromErrno(PyExc_IOError);
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
return Py_None;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char doc_read_init_file[] = "\
|
||||||
|
read_init_file([filename]) -> None\n\
|
||||||
|
Parse a readline initialization file.\n\
|
||||||
|
The default filename is the last filename used.\
|
||||||
|
";
|
||||||
|
|
||||||
|
|
||||||
|
/* Exported function to specify a word completer in Python */
|
||||||
|
|
||||||
|
static PyObject *completer = NULL;
|
||||||
|
static PyThreadState *tstate = NULL;
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
set_completer(self, args)
|
||||||
|
PyObject *self;
|
||||||
|
PyObject *args;
|
||||||
|
{
|
||||||
|
PyObject *function = Py_None;
|
||||||
|
if (!PyArg_ParseTuple(args, "|O", &function))
|
||||||
|
return NULL;
|
||||||
|
if (function == Py_None) {
|
||||||
|
Py_XDECREF(completer);
|
||||||
|
completer = NULL;
|
||||||
|
tstate = NULL;
|
||||||
|
}
|
||||||
|
else if (PyCallable_Check(function)) {
|
||||||
|
PyObject *tmp = completer;
|
||||||
|
Py_INCREF(function);
|
||||||
|
completer = function;
|
||||||
|
Py_XDECREF(tmp);
|
||||||
|
tstate = PyThreadState_Get();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"set_completer(func): argument not callable");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
return Py_None;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char doc_set_completer[] = "\
|
||||||
|
set_completer([function]) -> None\n\
|
||||||
|
Set or remove the completer function.\n\
|
||||||
|
The function is called as function(text, state),\n\
|
||||||
|
for i in [0, 1, 2, ...] until it returns a non-string.\n\
|
||||||
|
It should return the next possible completion starting with 'text'.\
|
||||||
|
";
|
||||||
|
|
||||||
|
|
||||||
|
/* Table of functions exported by the module */
|
||||||
|
|
||||||
static struct PyMethodDef readline_methods[] =
|
static struct PyMethodDef readline_methods[] =
|
||||||
{
|
{
|
||||||
|
{"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind},
|
||||||
|
{"read_init_file", read_init_file, 1, doc_read_init_file},
|
||||||
|
{"set_completer", set_completer, 1, doc_set_completer},
|
||||||
{0, 0}
|
{0, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
|
/* C function to call the Python completer. */
|
||||||
|
|
||||||
/* Initialize the module. Actually, that's all you can or need to do. */
|
static char *
|
||||||
void initreadline (void)
|
on_completion(text, state)
|
||||||
|
char *text;
|
||||||
|
int state;
|
||||||
{
|
{
|
||||||
PyObject *m, *d;
|
char *result = NULL;
|
||||||
|
if (completer != NULL) {
|
||||||
|
PyObject *r;
|
||||||
|
/* Note that readline is called with the interpreter
|
||||||
|
lock released! */
|
||||||
|
PyEval_RestoreThread(tstate);
|
||||||
|
r = PyObject_CallFunction(completer, "si", text, state);
|
||||||
|
if (r == NULL)
|
||||||
|
goto error;
|
||||||
|
if (r == Py_None) {
|
||||||
|
result = NULL;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
char *s = PyString_AsString(r);
|
||||||
|
if (s == NULL)
|
||||||
|
goto error;
|
||||||
|
result = strdup(s);
|
||||||
|
}
|
||||||
|
Py_DECREF(r);
|
||||||
|
goto done;
|
||||||
|
error:
|
||||||
|
PyErr_Clear();
|
||||||
|
Py_XDECREF(r);
|
||||||
|
done:
|
||||||
|
PyEval_SaveThread();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (already_initialized)
|
|
||||||
return;
|
/* Helper to initialize GNU readline properly. */
|
||||||
m = Py_InitModule4 ("readline", readline_methods,
|
|
||||||
readline_module_documentation,
|
static void
|
||||||
(PyObject *) 0, PYTHON_API_VERSION);
|
setup_readline()
|
||||||
d = PyModule_GetDict (m);
|
{
|
||||||
ReadlineError = PyString_FromString ("Readline.error");
|
rl_readline_name = "python";
|
||||||
PyDict_SetItemString (d, "error", ReadlineError);
|
/* Force rebind of TAB to insert-tab */
|
||||||
if (PyErr_Occurred ()) {
|
rl_bind_key('\t', rl_insert);
|
||||||
Py_FatalError ("Cannot initialize module readline");
|
/* Bind both ESC-TAB and ESC-ESC to the completion function */
|
||||||
}
|
rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
|
||||||
if (isatty(fileno(stdin))){
|
rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
|
||||||
PyOS_ReadlineFunctionPointer = PyOS_GnuReadline;
|
/* Set our completion function */
|
||||||
PyOS_ReadlineInit();
|
rl_completion_entry_function = (Function *) on_completion;
|
||||||
}
|
/* Initialize (allows .inputrc to override) */
|
||||||
already_initialized = 1;
|
rl_initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Interrupt handler */
|
||||||
|
|
||||||
|
static jmp_buf jbuf;
|
||||||
|
|
||||||
/* ARGSUSED */
|
/* ARGSUSED */
|
||||||
static RETSIGTYPE
|
static RETSIGTYPE
|
||||||
onintr(sig)
|
onintr(sig)
|
||||||
|
@ -73,17 +185,11 @@ onintr(sig)
|
||||||
longjmp(jbuf, 1);
|
longjmp(jbuf, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
PyOS_ReadlineInit()
|
/* Wrapper around GNU readline that handles signals differently. */
|
||||||
{
|
|
||||||
/* Force rebind of TAB to insert-tab */
|
|
||||||
rl_readline_name = "python";
|
|
||||||
rl_initialize();
|
|
||||||
rl_bind_key('\t', rl_insert);
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
PyOS_GnuReadline(prompt)
|
call_readline(prompt)
|
||||||
char *prompt;
|
char *prompt;
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
|
@ -117,6 +223,21 @@ PyOS_GnuReadline(prompt)
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
|
/* Initialize the module */
|
||||||
|
|
||||||
|
static char doc_module[] =
|
||||||
|
"Importing this module enables command line editing using GNU readline.";
|
||||||
|
|
||||||
|
void
|
||||||
|
initreadline()
|
||||||
|
{
|
||||||
|
PyObject *m;
|
||||||
|
|
||||||
|
m = Py_InitModule4("readline", readline_methods, doc_module,
|
||||||
|
(PyObject *)NULL, PYTHON_API_VERSION);
|
||||||
|
if (isatty(fileno(stdin))) {
|
||||||
|
PyOS_ReadlineFunctionPointer = call_readline;
|
||||||
|
setup_readline();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
Loading…
Reference in New Issue