blob: c74aee08c1676bd3c435e1add3f859222aa85e25 [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
22/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum44620641997-08-11 18:57:29 +000023extern int (*PyOS_InputHook)();
Guido van Rossum0969d361997-08-05 21:27:50 +000024extern char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
25
Guido van Rossum0969d361997-08-05 21:27:50 +000026
Guido van Rossum290900a1997-09-26 21:51:21 +000027/* Exported function to send one line to readline's init file parser */
28
29static PyObject *
30parse_and_bind(self, args)
31 PyObject *self;
32 PyObject *args;
33{
34 char *s;
35 if (!PyArg_ParseTuple(args, "s", &s))
36 return NULL;
37 rl_parse_and_bind(s);
38 Py_INCREF(Py_None);
39 return Py_None;
40}
41
42static char doc_parse_and_bind[] = "\
43parse_and_bind(string) -> None\n\
44Parse and execute single line of a readline init file.\
45";
46
47
48/* Exported function to parse a readline init file */
49
50static PyObject *
51read_init_file(self, args)
52 PyObject *self;
53 PyObject *args;
54{
55 char *s = NULL;
56 if (!PyArg_ParseTuple(args, "|z", &s))
57 return NULL;
58 errno = rl_read_init_file(s);
59 if (errno)
60 return PyErr_SetFromErrno(PyExc_IOError);
61 Py_INCREF(Py_None);
62 return Py_None;
63}
64
65static char doc_read_init_file[] = "\
66read_init_file([filename]) -> None\n\
67Parse a readline initialization file.\n\
68The default filename is the last filename used.\
69";
70
71
72/* Exported function to specify a word completer in Python */
73
74static PyObject *completer = NULL;
75static PyThreadState *tstate = NULL;
76
77static PyObject *
78set_completer(self, args)
79 PyObject *self;
80 PyObject *args;
81{
82 PyObject *function = Py_None;
83 if (!PyArg_ParseTuple(args, "|O", &function))
84 return NULL;
85 if (function == Py_None) {
86 Py_XDECREF(completer);
87 completer = NULL;
88 tstate = NULL;
89 }
90 else if (PyCallable_Check(function)) {
91 PyObject *tmp = completer;
92 Py_INCREF(function);
93 completer = function;
94 Py_XDECREF(tmp);
95 tstate = PyThreadState_Get();
96 }
97 else {
98 PyErr_SetString(PyExc_TypeError,
99 "set_completer(func): argument not callable");
100 return NULL;
101 }
102 Py_INCREF(Py_None);
103 return Py_None;
104}
105
106static char doc_set_completer[] = "\
107set_completer([function]) -> None\n\
108Set or remove the completer function.\n\
109The function is called as function(text, state),\n\
110for i in [0, 1, 2, ...] until it returns a non-string.\n\
111It should return the next possible completion starting with 'text'.\
112";
113
Guido van Rossum79378ff1997-10-07 14:53:21 +0000114/* Exported function to read the current line buffer */
115
116static PyObject *
117get_line_buffer(self, args)
118 PyObject *self;
119 PyObject *args;
120{
121 if (PyArg_NoArgs(args))
122 return NULL;
123 return PyString_FromString(rl_line_buffer);
124}
125
126static char doc_get_line_buffer[] = "\
127get_line_buffer([function]) -> string\n\
128return the current contents of the line buffer.\
129";
130
131/* Exported function to insert text into the line buffer */
132
133static PyObject *
134insert_text(self, args)
135 PyObject *self;
136 PyObject *args;
137{
138 char *s;
139 if (!PyArg_ParseTuple(args, "s", &s))
140 return NULL;
141 rl_insert_text(s);
142 Py_INCREF(Py_None);
143 return Py_None;
144}
145
146
147static char doc_insert_text[] = "\
148insert_text(string) -> None\n\
149Insert text into the command line.\
150";
151
Guido van Rossum290900a1997-09-26 21:51:21 +0000152
153/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000154
155static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000156{
157 {"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind},
Guido van Rossum79378ff1997-10-07 14:53:21 +0000158 {"get_line_buffer", get_line_buffer, 1, doc_get_line_buffer},
159 {"insert_text", insert_text, 1, doc_insert_text},
Guido van Rossum290900a1997-09-26 21:51:21 +0000160 {"read_init_file", read_init_file, 1, doc_read_init_file},
161 {"set_completer", set_completer, 1, doc_set_completer},
162 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000163};
164
Guido van Rossum290900a1997-09-26 21:51:21 +0000165/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000166
Guido van Rossum290900a1997-09-26 21:51:21 +0000167static char *
168on_completion(text, state)
169 char *text;
170 int state;
Guido van Rossum0969d361997-08-05 21:27:50 +0000171{
Guido van Rossum290900a1997-09-26 21:51:21 +0000172 char *result = NULL;
173 if (completer != NULL) {
174 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000175 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000176 /* Note that readline is called with the interpreter
177 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000178 save_tstate = PyThreadState_Swap(NULL);
Guido van Rossum290900a1997-09-26 21:51:21 +0000179 PyEval_RestoreThread(tstate);
180 r = PyObject_CallFunction(completer, "si", text, state);
181 if (r == NULL)
182 goto error;
183 if (r == Py_None) {
184 result = NULL;
185 }
186 else {
187 char *s = PyString_AsString(r);
188 if (s == NULL)
189 goto error;
190 result = strdup(s);
191 }
192 Py_DECREF(r);
193 goto done;
194 error:
195 PyErr_Clear();
196 Py_XDECREF(r);
197 done:
198 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000199 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000200 }
201 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000202}
203
Guido van Rossum290900a1997-09-26 21:51:21 +0000204
205/* Helper to initialize GNU readline properly. */
206
207static void
208setup_readline()
209{
210 rl_readline_name = "python";
211 /* Force rebind of TAB to insert-tab */
212 rl_bind_key('\t', rl_insert);
213 /* Bind both ESC-TAB and ESC-ESC to the completion function */
214 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
215 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
216 /* Set our completion function */
217 rl_completion_entry_function = (Function *) on_completion;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000218 /* Set Python word break characters */
219 rl_completer_word_break_characters =
220 " \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?";
221 /* All nonalphanums except '.' */
Guido van Rossum290900a1997-09-26 21:51:21 +0000222 /* Initialize (allows .inputrc to override) */
223 rl_initialize();
224}
225
226
227/* Interrupt handler */
228
229static jmp_buf jbuf;
230
Guido van Rossum0969d361997-08-05 21:27:50 +0000231/* ARGSUSED */
232static RETSIGTYPE
233onintr(sig)
Guido van Rossum290900a1997-09-26 21:51:21 +0000234 int sig;
Guido van Rossum0969d361997-08-05 21:27:50 +0000235{
Guido van Rossum290900a1997-09-26 21:51:21 +0000236 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000237}
238
Guido van Rossum290900a1997-09-26 21:51:21 +0000239
240/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000241
242static char *
Guido van Rossum290900a1997-09-26 21:51:21 +0000243call_readline(prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000244 char *prompt;
245{
246 int n;
247 char *p;
248 RETSIGTYPE (*old_inthandler)();
249 old_inthandler = signal(SIGINT, onintr);
250 if (setjmp(jbuf)) {
251#ifdef HAVE_SIGRELSE
252 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
253 sigrelse(SIGINT);
254#endif
255 signal(SIGINT, old_inthandler);
256 return NULL;
257 }
Guido van Rossum44620641997-08-11 18:57:29 +0000258 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000259 p = readline(prompt);
260 signal(SIGINT, old_inthandler);
261 if (p == NULL) {
262 p = malloc(1);
263 if (p != NULL)
264 *p = '\0';
265 return p;
266 }
267 n = strlen(p);
268 if (n > 0)
269 add_history(p);
270 if ((p = realloc(p, n+2)) != NULL) {
271 p[n] = '\n';
272 p[n+1] = '\0';
273 }
274 return p;
275}
276
Guido van Rossum290900a1997-09-26 21:51:21 +0000277
278/* Initialize the module */
279
280static char doc_module[] =
281"Importing this module enables command line editing using GNU readline.";
282
283void
284initreadline()
285{
286 PyObject *m;
287
288 m = Py_InitModule4("readline", readline_methods, doc_module,
289 (PyObject *)NULL, PYTHON_API_VERSION);
290 if (isatty(fileno(stdin))) {
291 PyOS_ReadlineFunctionPointer = call_readline;
292 setup_readline();
293 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000294}