blob: aef51ca97734715b7958d9a080392286c426f98c [file] [log] [blame]
Guido van Rossum290900a1997-09-26 21:51:21 +00001/* This module makes GNU readline available to Python. It has ideas
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
3 * Center. The completer interface was inspired by Lele Gaifax.
4 *
5 * More recently, it was largely rewritten by Guido van Rossum who is
6 * now maintaining it.
Guido van Rossum0969d361997-08-05 21:27:50 +00007 */
8
Guido van Rossum290900a1997-09-26 21:51:21 +00009/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +000010#include "Python.h"
11#include <setjmp.h>
12#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000013#include <errno.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000014
Guido van Rossum290900a1997-09-26 21:51:21 +000015/* GNU readline definitions */
16#include <readline/readline.h> /* You may need to add an -I option to Setup */
17
18/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum44620641997-08-11 18:57:29 +000019extern int (*PyOS_InputHook)();
Guido van Rossum0969d361997-08-05 21:27:50 +000020extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
21
Guido van Rossum0969d361997-08-05 21:27:50 +000022
Guido van Rossum290900a1997-09-26 21:51:21 +000023/* Exported function to send one line to readline's init file parser */
24
25static PyObject *
26parse_and_bind(self, args)
27 PyObject *self;
28 PyObject *args;
29{
30 char *s;
31 if (!PyArg_ParseTuple(args, "s", &s))
32 return NULL;
33 rl_parse_and_bind(s);
34 Py_INCREF(Py_None);
35 return Py_None;
36}
37
38static char doc_parse_and_bind[] = "\
39parse_and_bind(string) -> None\n\
40Parse and execute single line of a readline init file.\
41";
42
43
44/* Exported function to parse a readline init file */
45
46static PyObject *
47read_init_file(self, args)
48 PyObject *self;
49 PyObject *args;
50{
51 char *s = NULL;
52 if (!PyArg_ParseTuple(args, "|z", &s))
53 return NULL;
54 errno = rl_read_init_file(s);
55 if (errno)
56 return PyErr_SetFromErrno(PyExc_IOError);
57 Py_INCREF(Py_None);
58 return Py_None;
59}
60
61static char doc_read_init_file[] = "\
62read_init_file([filename]) -> None\n\
63Parse a readline initialization file.\n\
64The default filename is the last filename used.\
65";
66
67
68/* Exported function to specify a word completer in Python */
69
70static PyObject *completer = NULL;
71static PyThreadState *tstate = NULL;
72
73static PyObject *
74set_completer(self, args)
75 PyObject *self;
76 PyObject *args;
77{
78 PyObject *function = Py_None;
79 if (!PyArg_ParseTuple(args, "|O", &function))
80 return NULL;
81 if (function == Py_None) {
82 Py_XDECREF(completer);
83 completer = NULL;
84 tstate = NULL;
85 }
86 else if (PyCallable_Check(function)) {
87 PyObject *tmp = completer;
88 Py_INCREF(function);
89 completer = function;
90 Py_XDECREF(tmp);
91 tstate = PyThreadState_Get();
92 }
93 else {
94 PyErr_SetString(PyExc_TypeError,
95 "set_completer(func): argument not callable");
96 return NULL;
97 }
98 Py_INCREF(Py_None);
99 return Py_None;
100}
101
102static char doc_set_completer[] = "\
103set_completer([function]) -> None\n\
104Set or remove the completer function.\n\
105The function is called as function(text, state),\n\
106for i in [0, 1, 2, ...] until it returns a non-string.\n\
107It should return the next possible completion starting with 'text'.\
108";
109
110
111/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000112
113static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000114{
115 {"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind},
116 {"read_init_file", read_init_file, 1, doc_read_init_file},
117 {"set_completer", set_completer, 1, doc_set_completer},
118 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000119};
120
Guido van Rossum290900a1997-09-26 21:51:21 +0000121/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000122
Guido van Rossum290900a1997-09-26 21:51:21 +0000123static char *
124on_completion(text, state)
125 char *text;
126 int state;
Guido van Rossum0969d361997-08-05 21:27:50 +0000127{
Guido van Rossum290900a1997-09-26 21:51:21 +0000128 char *result = NULL;
129 if (completer != NULL) {
130 PyObject *r;
131 /* Note that readline is called with the interpreter
132 lock released! */
133 PyEval_RestoreThread(tstate);
134 r = PyObject_CallFunction(completer, "si", text, state);
135 if (r == NULL)
136 goto error;
137 if (r == Py_None) {
138 result = NULL;
139 }
140 else {
141 char *s = PyString_AsString(r);
142 if (s == NULL)
143 goto error;
144 result = strdup(s);
145 }
146 Py_DECREF(r);
147 goto done;
148 error:
149 PyErr_Clear();
150 Py_XDECREF(r);
151 done:
152 PyEval_SaveThread();
153 }
154 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000155}
156
Guido van Rossum290900a1997-09-26 21:51:21 +0000157
158/* Helper to initialize GNU readline properly. */
159
160static void
161setup_readline()
162{
163 rl_readline_name = "python";
164 /* Force rebind of TAB to insert-tab */
165 rl_bind_key('\t', rl_insert);
166 /* Bind both ESC-TAB and ESC-ESC to the completion function */
167 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
168 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
169 /* Set our completion function */
170 rl_completion_entry_function = (Function *) on_completion;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000171 /* Set Python word break characters */
172 rl_completer_word_break_characters =
173 " \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?";
174 /* All nonalphanums except '.' */
Guido van Rossum290900a1997-09-26 21:51:21 +0000175 /* Initialize (allows .inputrc to override) */
176 rl_initialize();
177}
178
179
180/* Interrupt handler */
181
182static jmp_buf jbuf;
183
Guido van Rossum0969d361997-08-05 21:27:50 +0000184/* ARGSUSED */
185static RETSIGTYPE
186onintr(sig)
Guido van Rossum290900a1997-09-26 21:51:21 +0000187 int sig;
Guido van Rossum0969d361997-08-05 21:27:50 +0000188{
Guido van Rossum290900a1997-09-26 21:51:21 +0000189 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000190}
191
Guido van Rossum290900a1997-09-26 21:51:21 +0000192
193/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000194
195static char *
Guido van Rossum290900a1997-09-26 21:51:21 +0000196call_readline(prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000197 char *prompt;
198{
199 int n;
200 char *p;
201 RETSIGTYPE (*old_inthandler)();
202 old_inthandler = signal(SIGINT, onintr);
203 if (setjmp(jbuf)) {
204#ifdef HAVE_SIGRELSE
205 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
206 sigrelse(SIGINT);
207#endif
208 signal(SIGINT, old_inthandler);
209 return NULL;
210 }
Guido van Rossum44620641997-08-11 18:57:29 +0000211 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000212 p = readline(prompt);
213 signal(SIGINT, old_inthandler);
214 if (p == NULL) {
215 p = malloc(1);
216 if (p != NULL)
217 *p = '\0';
218 return p;
219 }
220 n = strlen(p);
221 if (n > 0)
222 add_history(p);
223 if ((p = realloc(p, n+2)) != NULL) {
224 p[n] = '\n';
225 p[n+1] = '\0';
226 }
227 return p;
228}
229
Guido van Rossum290900a1997-09-26 21:51:21 +0000230
231/* Initialize the module */
232
233static char doc_module[] =
234"Importing this module enables command line editing using GNU readline.";
235
236void
237initreadline()
238{
239 PyObject *m;
240
241 m = Py_InitModule4("readline", readline_methods, doc_module,
242 (PyObject *)NULL, PYTHON_API_VERSION);
243 if (isatty(fileno(stdin))) {
244 PyOS_ReadlineFunctionPointer = call_readline;
245 setup_readline();
246 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000247}