blob: 79b8870ce2aaf06b9c4ce89edd6c58024d6bcd5f [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 Rossum73bacfc1998-01-19 22:05:22 +000015#ifdef HAVE_UNISTD_H
16#include <unistd.h> /* For isatty() */
17#endif
18
Guido van Rossum290900a1997-09-26 21:51:21 +000019/* GNU readline definitions */
20#include <readline/readline.h> /* You may need to add an -I option to Setup */
21
Guido van Rossum730806d1998-04-10 22:27:42 +000022extern int rl_parse_and_bind();
23extern int rl_read_init_file();
24extern int rl_insert_text();
25extern int rl_bind_key();
26extern int rl_bind_key_in_map();
27extern int rl_initialize();
28extern int add_history();
29
Guido van Rossum290900a1997-09-26 21:51:21 +000030/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum44620641997-08-11 18:57:29 +000031extern int (*PyOS_InputHook)();
Guido van Rossum0969d361997-08-05 21:27:50 +000032extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
33
Guido van Rossum0969d361997-08-05 21:27:50 +000034
Guido van Rossum290900a1997-09-26 21:51:21 +000035/* Exported function to send one line to readline's init file parser */
36
37static PyObject *
38parse_and_bind(self, args)
39 PyObject *self;
40 PyObject *args;
41{
42 char *s;
43 if (!PyArg_ParseTuple(args, "s", &s))
44 return NULL;
45 rl_parse_and_bind(s);
46 Py_INCREF(Py_None);
47 return Py_None;
48}
49
50static char doc_parse_and_bind[] = "\
51parse_and_bind(string) -> None\n\
52Parse and execute single line of a readline init file.\
53";
54
55
56/* Exported function to parse a readline init file */
57
58static PyObject *
59read_init_file(self, args)
60 PyObject *self;
61 PyObject *args;
62{
63 char *s = NULL;
64 if (!PyArg_ParseTuple(args, "|z", &s))
65 return NULL;
66 errno = rl_read_init_file(s);
67 if (errno)
68 return PyErr_SetFromErrno(PyExc_IOError);
69 Py_INCREF(Py_None);
70 return Py_None;
71}
72
73static char doc_read_init_file[] = "\
74read_init_file([filename]) -> None\n\
75Parse a readline initialization file.\n\
76The default filename is the last filename used.\
77";
78
79
80/* Exported function to specify a word completer in Python */
81
82static PyObject *completer = NULL;
83static PyThreadState *tstate = NULL;
84
85static PyObject *
86set_completer(self, args)
87 PyObject *self;
88 PyObject *args;
89{
90 PyObject *function = Py_None;
91 if (!PyArg_ParseTuple(args, "|O", &function))
92 return NULL;
93 if (function == Py_None) {
94 Py_XDECREF(completer);
95 completer = NULL;
96 tstate = NULL;
97 }
98 else if (PyCallable_Check(function)) {
99 PyObject *tmp = completer;
100 Py_INCREF(function);
101 completer = function;
102 Py_XDECREF(tmp);
103 tstate = PyThreadState_Get();
104 }
105 else {
106 PyErr_SetString(PyExc_TypeError,
107 "set_completer(func): argument not callable");
108 return NULL;
109 }
110 Py_INCREF(Py_None);
111 return Py_None;
112}
113
114static char doc_set_completer[] = "\
115set_completer([function]) -> None\n\
116Set or remove the completer function.\n\
117The function is called as function(text, state),\n\
118for i in [0, 1, 2, ...] until it returns a non-string.\n\
119It should return the next possible completion starting with 'text'.\
120";
121
Guido van Rossum79378ff1997-10-07 14:53:21 +0000122/* Exported function to read the current line buffer */
123
124static PyObject *
125get_line_buffer(self, args)
126 PyObject *self;
127 PyObject *args;
128{
129 if (PyArg_NoArgs(args))
130 return NULL;
131 return PyString_FromString(rl_line_buffer);
132}
133
134static char doc_get_line_buffer[] = "\
Guido van Rossuma88c5f31998-05-20 15:50:56 +0000135get_line_buffer() -> string\n\
Guido van Rossum79378ff1997-10-07 14:53:21 +0000136return the current contents of the line buffer.\
137";
138
139/* Exported function to insert text into the line buffer */
140
141static PyObject *
142insert_text(self, args)
143 PyObject *self;
144 PyObject *args;
145{
146 char *s;
147 if (!PyArg_ParseTuple(args, "s", &s))
148 return NULL;
149 rl_insert_text(s);
150 Py_INCREF(Py_None);
151 return Py_None;
152}
153
154
155static char doc_insert_text[] = "\
156insert_text(string) -> None\n\
157Insert text into the command line.\
158";
159
Guido van Rossum290900a1997-09-26 21:51:21 +0000160
161/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000162
163static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000164{
165 {"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind},
Guido van Rossum79378ff1997-10-07 14:53:21 +0000166 {"get_line_buffer", get_line_buffer, 1, doc_get_line_buffer},
167 {"insert_text", insert_text, 1, doc_insert_text},
Guido van Rossum290900a1997-09-26 21:51:21 +0000168 {"read_init_file", read_init_file, 1, doc_read_init_file},
169 {"set_completer", set_completer, 1, doc_set_completer},
170 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000171};
172
Guido van Rossum290900a1997-09-26 21:51:21 +0000173/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000174
Guido van Rossum290900a1997-09-26 21:51:21 +0000175static char *
176on_completion(text, state)
177 char *text;
178 int state;
Guido van Rossum0969d361997-08-05 21:27:50 +0000179{
Guido van Rossum290900a1997-09-26 21:51:21 +0000180 char *result = NULL;
181 if (completer != NULL) {
182 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000183 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000184 /* Note that readline is called with the interpreter
185 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000186 save_tstate = PyThreadState_Swap(NULL);
Guido van Rossum290900a1997-09-26 21:51:21 +0000187 PyEval_RestoreThread(tstate);
188 r = PyObject_CallFunction(completer, "si", text, state);
189 if (r == NULL)
190 goto error;
191 if (r == Py_None) {
192 result = NULL;
193 }
194 else {
195 char *s = PyString_AsString(r);
196 if (s == NULL)
197 goto error;
198 result = strdup(s);
199 }
200 Py_DECREF(r);
201 goto done;
202 error:
203 PyErr_Clear();
204 Py_XDECREF(r);
205 done:
206 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000207 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000208 }
209 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000210}
211
Guido van Rossum290900a1997-09-26 21:51:21 +0000212
213/* Helper to initialize GNU readline properly. */
214
215static void
216setup_readline()
217{
218 rl_readline_name = "python";
219 /* Force rebind of TAB to insert-tab */
220 rl_bind_key('\t', rl_insert);
221 /* Bind both ESC-TAB and ESC-ESC to the completion function */
222 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
223 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
224 /* Set our completion function */
225 rl_completion_entry_function = (Function *) on_completion;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000226 /* Set Python word break characters */
227 rl_completer_word_break_characters =
228 " \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?";
229 /* All nonalphanums except '.' */
Guido van Rossum290900a1997-09-26 21:51:21 +0000230 /* Initialize (allows .inputrc to override) */
231 rl_initialize();
232}
233
234
235/* Interrupt handler */
236
237static jmp_buf jbuf;
238
Guido van Rossum0969d361997-08-05 21:27:50 +0000239/* ARGSUSED */
240static RETSIGTYPE
241onintr(sig)
Guido van Rossum290900a1997-09-26 21:51:21 +0000242 int sig;
Guido van Rossum0969d361997-08-05 21:27:50 +0000243{
Guido van Rossum290900a1997-09-26 21:51:21 +0000244 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000245}
246
Guido van Rossum290900a1997-09-26 21:51:21 +0000247
248/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000249
250static char *
Guido van Rossum290900a1997-09-26 21:51:21 +0000251call_readline(prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000252 char *prompt;
253{
254 int n;
255 char *p;
256 RETSIGTYPE (*old_inthandler)();
257 old_inthandler = signal(SIGINT, onintr);
258 if (setjmp(jbuf)) {
259#ifdef HAVE_SIGRELSE
260 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
261 sigrelse(SIGINT);
262#endif
263 signal(SIGINT, old_inthandler);
264 return NULL;
265 }
Guido van Rossum44620641997-08-11 18:57:29 +0000266 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000267 p = readline(prompt);
268 signal(SIGINT, old_inthandler);
269 if (p == NULL) {
270 p = malloc(1);
271 if (p != NULL)
272 *p = '\0';
273 return p;
274 }
275 n = strlen(p);
276 if (n > 0)
277 add_history(p);
278 if ((p = realloc(p, n+2)) != NULL) {
279 p[n] = '\n';
280 p[n+1] = '\0';
281 }
282 return p;
283}
284
Guido van Rossum290900a1997-09-26 21:51:21 +0000285
286/* Initialize the module */
287
288static char doc_module[] =
289"Importing this module enables command line editing using GNU readline.";
290
291void
292initreadline()
293{
294 PyObject *m;
295
296 m = Py_InitModule4("readline", readline_methods, doc_module,
297 (PyObject *)NULL, PYTHON_API_VERSION);
298 if (isatty(fileno(stdin))) {
299 PyOS_ReadlineFunctionPointer = call_readline;
300 setup_readline();
301 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000302}