blob: 2986b9de7f2cd7229fe194a34ec4c5248b2bb754 [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 */
Guido van Rossumbcc20741998-08-04 22:53:56 +000020/* If you have string.h, you might need to add yourself to this #if... [cjh] */
21#if defined(__BEOS__)
22#undef HAVE_CONFIG_H
23/* At max warnings, we need protos for everything. [cjh] */
24#include <readline/readline.h>
25#include <readline/history.h>
26#include <unistd.h>
27#else
Guido van Rossum290900a1997-09-26 21:51:21 +000028#include <readline/readline.h> /* You may need to add an -I option to Setup */
29
Guido van Rossum730806d1998-04-10 22:27:42 +000030extern int rl_parse_and_bind();
31extern int rl_read_init_file();
32extern int rl_insert_text();
33extern int rl_bind_key();
34extern int rl_bind_key_in_map();
35extern int rl_initialize();
36extern int add_history();
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000037extern Function *rl_event_hook;
Guido van Rossumbcc20741998-08-04 22:53:56 +000038#endif
Guido van Rossum730806d1998-04-10 22:27:42 +000039
Guido van Rossum290900a1997-09-26 21:51:21 +000040/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum44620641997-08-11 18:57:29 +000041extern int (*PyOS_InputHook)();
Tim Petersdbd9ba62000-07-09 03:09:57 +000042extern char *(*PyOS_ReadlineFunctionPointer)(char *);
Guido van Rossum0969d361997-08-05 21:27:50 +000043
Guido van Rossum0969d361997-08-05 21:27:50 +000044
Guido van Rossum290900a1997-09-26 21:51:21 +000045/* Exported function to send one line to readline's init file parser */
46
47static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000048parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000049{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000050 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000051 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000052 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000053 /* Make a copy -- rl_parse_and_bind() modifies its argument */
54 /* Bernard Herzog */
55 copy = malloc(1 + strlen(s));
56 if (copy == NULL)
57 return PyErr_NoMemory();
58 strcpy(copy, s);
59 rl_parse_and_bind(copy);
60 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000061 Py_INCREF(Py_None);
62 return Py_None;
63}
64
65static char doc_parse_and_bind[] = "\
66parse_and_bind(string) -> None\n\
67Parse and execute single line of a readline init file.\
68";
69
70
71/* Exported function to parse a readline init file */
72
73static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000074read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000075{
76 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000077 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000078 return NULL;
79 errno = rl_read_init_file(s);
80 if (errno)
81 return PyErr_SetFromErrno(PyExc_IOError);
82 Py_INCREF(Py_None);
83 return Py_None;
84}
85
86static char doc_read_init_file[] = "\
87read_init_file([filename]) -> None\n\
88Parse a readline initialization file.\n\
89The default filename is the last filename used.\
90";
91
92
Skip Montanaro28067822000-07-06 18:55:12 +000093/* Exported function to load a readline history file */
94
95static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000096read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000097{
98 char *s = NULL;
99 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
100 return NULL;
101 errno = read_history(s);
102 if (errno)
103 return PyErr_SetFromErrno(PyExc_IOError);
104 Py_INCREF(Py_None);
105 return Py_None;
106}
107
108static char doc_read_history_file[] = "\
109read_history_file([filename]) -> None\n\
110Load a readline history file.\n\
111The default filename is ~/.history.\
112";
113
114
115/* Exported function to save a readline history file */
116
117static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000118write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000119{
120 char *s = NULL;
121 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
122 return NULL;
123 errno = write_history(s);
124 if (errno)
125 return PyErr_SetFromErrno(PyExc_IOError);
126 Py_INCREF(Py_None);
127 return Py_None;
128}
129
130static char doc_write_history_file[] = "\
131write_history_file([filename]) -> None\n\
132Save a readline history file.\n\
133The default filename is ~/.history.\
134";
135
136
Guido van Rossum290900a1997-09-26 21:51:21 +0000137/* Exported function to specify a word completer in Python */
138
139static PyObject *completer = NULL;
140static PyThreadState *tstate = NULL;
141
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000142static PyObject *begidx = NULL;
143static PyObject *endidx = NULL;
144
145/* get the beginning index for the scope of the tab-completion */
146static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000147get_begidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000148{
149 if(!PyArg_NoArgs(args)) {
150 return NULL;
151 }
152 Py_INCREF(begidx);
153 return begidx;
154}
155
156static char doc_get_begidx[] = "\
157get_begidx() -> int\n\
158get the beginning index of the readline tab-completion scope";
159
160/* get the ending index for the scope of the tab-completion */
161static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000162get_endidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000163{
164 if(!PyArg_NoArgs(args)) {
165 return NULL;
166 }
167 Py_INCREF(endidx);
168 return endidx;
169}
170
171static char doc_get_endidx[] = "\
172get_endidx() -> int\n\
173get the ending index of the readline tab-completion scope";
174
175
176/* set the tab-completion word-delimiters that readline uses */
177
178static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000179set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000180{
181 char *break_chars;
182
Guido van Rossum43713e52000-02-29 13:59:29 +0000183 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000184 return NULL;
185 }
186 free(rl_completer_word_break_characters);
187 rl_completer_word_break_characters = strdup(break_chars);
188 Py_INCREF(Py_None);
189 return Py_None;
190}
191
192static char doc_set_completer_delims[] = "\
193set_completer_delims(string) -> None\n\
194set the readline word delimiters for tab-completion";
195
196
197/* get the tab-completion word-delimiters that readline uses */
198
199static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000200get_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000201{
202 if(!PyArg_NoArgs(args)) {
203 return NULL;
204 }
205 return PyString_FromString(rl_completer_word_break_characters);
206}
207
208static char doc_get_completer_delims[] = "\
209get_completer_delims() -> string\n\
210get the readline word delimiters for tab-completion";
211
Guido van Rossum290900a1997-09-26 21:51:21 +0000212static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000213set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000214{
215 PyObject *function = Py_None;
Guido van Rossum43713e52000-02-29 13:59:29 +0000216 if (!PyArg_ParseTuple(args, "|O:set_completer", &function))
Guido van Rossum290900a1997-09-26 21:51:21 +0000217 return NULL;
218 if (function == Py_None) {
219 Py_XDECREF(completer);
220 completer = NULL;
221 tstate = NULL;
222 }
223 else if (PyCallable_Check(function)) {
224 PyObject *tmp = completer;
225 Py_INCREF(function);
226 completer = function;
227 Py_XDECREF(tmp);
228 tstate = PyThreadState_Get();
229 }
230 else {
231 PyErr_SetString(PyExc_TypeError,
232 "set_completer(func): argument not callable");
233 return NULL;
234 }
235 Py_INCREF(Py_None);
236 return Py_None;
237}
238
239static char doc_set_completer[] = "\
240set_completer([function]) -> None\n\
241Set or remove the completer function.\n\
242The function is called as function(text, state),\n\
243for i in [0, 1, 2, ...] until it returns a non-string.\n\
244It should return the next possible completion starting with 'text'.\
245";
246
Guido van Rossum79378ff1997-10-07 14:53:21 +0000247/* Exported function to read the current line buffer */
248
249static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000250get_line_buffer(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000251{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000252 if (!PyArg_NoArgs(args))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000253 return NULL;
254 return PyString_FromString(rl_line_buffer);
255}
256
257static char doc_get_line_buffer[] = "\
Guido van Rossuma88c5f31998-05-20 15:50:56 +0000258get_line_buffer() -> string\n\
Guido van Rossum79378ff1997-10-07 14:53:21 +0000259return the current contents of the line buffer.\
260";
261
262/* Exported function to insert text into the line buffer */
263
264static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000265insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000266{
267 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000268 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000269 return NULL;
270 rl_insert_text(s);
271 Py_INCREF(Py_None);
272 return Py_None;
273}
274
275
276static char doc_insert_text[] = "\
277insert_text(string) -> None\n\
278Insert text into the command line.\
279";
280
Guido van Rossum290900a1997-09-26 21:51:21 +0000281
282/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000283
284static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000285{
286 {"parse_and_bind", parse_and_bind, 1, doc_parse_and_bind},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000287 {"get_line_buffer", get_line_buffer, 0, doc_get_line_buffer},
Guido van Rossum79378ff1997-10-07 14:53:21 +0000288 {"insert_text", insert_text, 1, doc_insert_text},
Guido van Rossum290900a1997-09-26 21:51:21 +0000289 {"read_init_file", read_init_file, 1, doc_read_init_file},
Skip Montanaro28067822000-07-06 18:55:12 +0000290 {"read_history_file", read_history_file, 1, doc_read_history_file},
291 {"write_history_file", write_history_file, 1, doc_write_history_file},
Guido van Rossum290900a1997-09-26 21:51:21 +0000292 {"set_completer", set_completer, 1, doc_set_completer},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000293 {"get_begidx", get_begidx, 0, doc_get_begidx},
294 {"get_endidx", get_endidx, 0, doc_get_endidx},
295
296 {"set_completer_delims", set_completer_delims, METH_VARARGS,
297 doc_set_completer_delims},
298 {"get_completer_delims", get_completer_delims, 0,
299 doc_get_completer_delims},
Guido van Rossum290900a1997-09-26 21:51:21 +0000300 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000301};
302
Guido van Rossum290900a1997-09-26 21:51:21 +0000303/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000304
Guido van Rossum290900a1997-09-26 21:51:21 +0000305static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000306on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000307{
Guido van Rossum290900a1997-09-26 21:51:21 +0000308 char *result = NULL;
309 if (completer != NULL) {
310 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000311 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000312 /* Note that readline is called with the interpreter
313 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000314 save_tstate = PyThreadState_Swap(NULL);
Guido van Rossum290900a1997-09-26 21:51:21 +0000315 PyEval_RestoreThread(tstate);
316 r = PyObject_CallFunction(completer, "si", text, state);
317 if (r == NULL)
318 goto error;
319 if (r == Py_None) {
320 result = NULL;
321 }
322 else {
323 char *s = PyString_AsString(r);
324 if (s == NULL)
325 goto error;
326 result = strdup(s);
327 }
328 Py_DECREF(r);
329 goto done;
330 error:
331 PyErr_Clear();
332 Py_XDECREF(r);
333 done:
334 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000335 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000336 }
337 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000338}
339
Guido van Rossum290900a1997-09-26 21:51:21 +0000340
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000341/* a more flexible constructor that saves the "begidx" and "endidx"
342 * before calling the normal completer */
343
344char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000345flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000346{
347 Py_XDECREF(begidx);
348 Py_XDECREF(endidx);
349 begidx = PyInt_FromLong((long) start);
350 endidx = PyInt_FromLong((long) end);
351 return completion_matches(text, *on_completion);
352}
353
Guido van Rossum290900a1997-09-26 21:51:21 +0000354/* Helper to initialize GNU readline properly. */
355
356static void
357setup_readline()
358{
359 rl_readline_name = "python";
360 /* Force rebind of TAB to insert-tab */
361 rl_bind_key('\t', rl_insert);
362 /* Bind both ESC-TAB and ESC-ESC to the completion function */
363 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
364 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
365 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000366 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000367 /* Set Python word break characters */
368 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000369 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000370 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000371
372 begidx = PyInt_FromLong(0L);
373 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000374 /* Initialize (allows .inputrc to override)
375 *
376 * XXX: A bug in the readline-2.2 library causes a memory leak
377 * inside this function. Nothing we can do about it.
378 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000379 rl_initialize();
380}
381
382
383/* Interrupt handler */
384
385static jmp_buf jbuf;
386
Guido van Rossum0969d361997-08-05 21:27:50 +0000387/* ARGSUSED */
388static RETSIGTYPE
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000389onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000390{
Guido van Rossum290900a1997-09-26 21:51:21 +0000391 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000392}
393
Guido van Rossum290900a1997-09-26 21:51:21 +0000394
395/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000396
397static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000398call_readline(char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000399{
Guido van Rossum26418a92000-06-28 21:30:31 +0000400 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000401 char *p, *q;
Guido van Rossum0969d361997-08-05 21:27:50 +0000402 RETSIGTYPE (*old_inthandler)();
403 old_inthandler = signal(SIGINT, onintr);
404 if (setjmp(jbuf)) {
405#ifdef HAVE_SIGRELSE
406 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
407 sigrelse(SIGINT);
408#endif
409 signal(SIGINT, old_inthandler);
410 return NULL;
411 }
Guido van Rossum44620641997-08-11 18:57:29 +0000412 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000413 p = readline(prompt);
414 signal(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000415
416 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000417 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000418 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000419 if (p != NULL)
420 *p = '\0';
421 return p;
422 }
423 n = strlen(p);
424 if (n > 0)
425 add_history(p);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000426 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
427 release the original. */
428 q = p;
429 p = PyMem_Malloc(n+2);
430 if (p != NULL) {
431 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000432 p[n] = '\n';
433 p[n+1] = '\0';
434 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000435 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000436 return p;
437}
438
Guido van Rossum290900a1997-09-26 21:51:21 +0000439
440/* Initialize the module */
441
442static char doc_module[] =
443"Importing this module enables command line editing using GNU readline.";
444
Guido van Rossum3886bb61998-12-04 18:50:17 +0000445DL_EXPORT(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000446initreadline()
447{
448 PyObject *m;
449
450 m = Py_InitModule4("readline", readline_methods, doc_module,
451 (PyObject *)NULL, PYTHON_API_VERSION);
452 if (isatty(fileno(stdin))) {
453 PyOS_ReadlineFunctionPointer = call_readline;
454 setup_readline();
455 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000456}