blob: e12ae1d70e91bfac6b4cc127f1bdde877235b50c [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
Guido van Rossum79378ff1997-10-07 14:53:21 +0000110/* Exported function to read the current line buffer */
111
112static PyObject *
113get_line_buffer(self, args)
114 PyObject *self;
115 PyObject *args;
116{
117 if (PyArg_NoArgs(args))
118 return NULL;
119 return PyString_FromString(rl_line_buffer);
120}
121
122static char doc_get_line_buffer[] = "\
123get_line_buffer([function]) -> string\n\
124return the current contents of the line buffer.\
125";
126
127/* Exported function to insert text into the line buffer */
128
129static PyObject *
130insert_text(self, args)
131 PyObject *self;
132 PyObject *args;
133{
134 char *s;
135 if (!PyArg_ParseTuple(args, "s", &s))
136 return NULL;
137 rl_insert_text(s);
138 Py_INCREF(Py_None);
139 return Py_None;
140}
141
142
143static char doc_insert_text[] = "\
144insert_text(string) -> None\n\
145Insert text into the command line.\
146";
147
Guido van Rossum290900a1997-09-26 21:51:21 +0000148
149/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000150
151static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000152{
153 {"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind},
Guido van Rossum79378ff1997-10-07 14:53:21 +0000154 {"get_line_buffer", get_line_buffer, 1, doc_get_line_buffer},
155 {"insert_text", insert_text, 1, doc_insert_text},
Guido van Rossum290900a1997-09-26 21:51:21 +0000156 {"read_init_file", read_init_file, 1, doc_read_init_file},
157 {"set_completer", set_completer, 1, doc_set_completer},
158 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000159};
160
Guido van Rossum290900a1997-09-26 21:51:21 +0000161/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000162
Guido van Rossum290900a1997-09-26 21:51:21 +0000163static char *
164on_completion(text, state)
165 char *text;
166 int state;
Guido van Rossum0969d361997-08-05 21:27:50 +0000167{
Guido van Rossum290900a1997-09-26 21:51:21 +0000168 char *result = NULL;
169 if (completer != NULL) {
170 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000171 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000172 /* Note that readline is called with the interpreter
173 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000174 save_tstate = PyThreadState_Swap(NULL);
Guido van Rossum290900a1997-09-26 21:51:21 +0000175 PyEval_RestoreThread(tstate);
176 r = PyObject_CallFunction(completer, "si", text, state);
177 if (r == NULL)
178 goto error;
179 if (r == Py_None) {
180 result = NULL;
181 }
182 else {
183 char *s = PyString_AsString(r);
184 if (s == NULL)
185 goto error;
186 result = strdup(s);
187 }
188 Py_DECREF(r);
189 goto done;
190 error:
191 PyErr_Clear();
192 Py_XDECREF(r);
193 done:
194 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000195 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000196 }
197 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000198}
199
Guido van Rossum290900a1997-09-26 21:51:21 +0000200
201/* Helper to initialize GNU readline properly. */
202
203static void
204setup_readline()
205{
206 rl_readline_name = "python";
207 /* Force rebind of TAB to insert-tab */
208 rl_bind_key('\t', rl_insert);
209 /* Bind both ESC-TAB and ESC-ESC to the completion function */
210 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
211 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
212 /* Set our completion function */
213 rl_completion_entry_function = (Function *) on_completion;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000214 /* Set Python word break characters */
215 rl_completer_word_break_characters =
216 " \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?";
217 /* All nonalphanums except '.' */
Guido van Rossum290900a1997-09-26 21:51:21 +0000218 /* Initialize (allows .inputrc to override) */
219 rl_initialize();
220}
221
222
223/* Interrupt handler */
224
225static jmp_buf jbuf;
226
Guido van Rossum0969d361997-08-05 21:27:50 +0000227/* ARGSUSED */
228static RETSIGTYPE
229onintr(sig)
Guido van Rossum290900a1997-09-26 21:51:21 +0000230 int sig;
Guido van Rossum0969d361997-08-05 21:27:50 +0000231{
Guido van Rossum290900a1997-09-26 21:51:21 +0000232 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000233}
234
Guido van Rossum290900a1997-09-26 21:51:21 +0000235
236/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000237
238static char *
Guido van Rossum290900a1997-09-26 21:51:21 +0000239call_readline(prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000240 char *prompt;
241{
242 int n;
243 char *p;
244 RETSIGTYPE (*old_inthandler)();
245 old_inthandler = signal(SIGINT, onintr);
246 if (setjmp(jbuf)) {
247#ifdef HAVE_SIGRELSE
248 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
249 sigrelse(SIGINT);
250#endif
251 signal(SIGINT, old_inthandler);
252 return NULL;
253 }
Guido van Rossum44620641997-08-11 18:57:29 +0000254 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000255 p = readline(prompt);
256 signal(SIGINT, old_inthandler);
257 if (p == NULL) {
258 p = malloc(1);
259 if (p != NULL)
260 *p = '\0';
261 return p;
262 }
263 n = strlen(p);
264 if (n > 0)
265 add_history(p);
266 if ((p = realloc(p, n+2)) != NULL) {
267 p[n] = '\n';
268 p[n+1] = '\0';
269 }
270 return p;
271}
272
Guido van Rossum290900a1997-09-26 21:51:21 +0000273
274/* Initialize the module */
275
276static char doc_module[] =
277"Importing this module enables command line editing using GNU readline.";
278
279void
280initreadline()
281{
282 PyObject *m;
283
284 m = Py_InitModule4("readline", readline_methods, doc_module,
285 (PyObject *)NULL, PYTHON_API_VERSION);
286 if (isatty(fileno(stdin))) {
287 PyOS_ReadlineFunctionPointer = call_readline;
288 setup_readline();
289 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000290}