blob: fe6bf22362f554611ae4865eb4d087e964e2dfd9 [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). */
Thomas Wouters2c46eaf2000-07-22 23:51:19 +000041extern int (*PyOS_InputHook)(void);
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
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000108static int history_length = -1; /* do not truncate history by default */
Skip Montanaro28067822000-07-06 18:55:12 +0000109static char doc_read_history_file[] = "\
110read_history_file([filename]) -> None\n\
111Load a readline history file.\n\
112The default filename is ~/.history.\
113";
114
115
116/* Exported function to save a readline history file */
117
118static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000119write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000120{
121 char *s = NULL;
122 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
123 return NULL;
124 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000125 if (!errno && history_length >= 0)
126 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000127 if (errno)
128 return PyErr_SetFromErrno(PyExc_IOError);
129 Py_INCREF(Py_None);
130 return Py_None;
131}
132
133static char doc_write_history_file[] = "\
134write_history_file([filename]) -> None\n\
135Save a readline history file.\n\
136The default filename is ~/.history.\
137";
138
139
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000140static char set_history_length_doc[] = "\
141set_history_length(length) -> None\n\
142set the maximal number of items which will be written to\n\
143the history file. A negative length is used to inhibit\n\
144history truncation.\n\
145";
146
147static PyObject*
148set_history_length(PyObject *self, PyObject *args)
149{
150 int length = history_length;
151 PyObject* ob;
152 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
153 return NULL;
154 history_length = length;
155 Py_INCREF(Py_None);
156 return Py_None;
157}
158
159
160
161static char get_history_length_doc[] = "\
162get_history_length() -> int\n\
163return the current history length value.\n\
164";
165
166static PyObject*
167get_history_length(PyObject *self, PyObject *args)
168{
169 PyObject* ob;
170 if (!PyArg_ParseTuple(args, ":get_history_length"))
171 return NULL;
172 return Py_BuildValue("i", history_length);
173}
174
175
176
Guido van Rossum290900a1997-09-26 21:51:21 +0000177/* Exported function to specify a word completer in Python */
178
179static PyObject *completer = NULL;
180static PyThreadState *tstate = NULL;
181
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000182static PyObject *begidx = NULL;
183static PyObject *endidx = NULL;
184
185/* get the beginning index for the scope of the tab-completion */
186static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000187get_begidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000188{
189 if(!PyArg_NoArgs(args)) {
190 return NULL;
191 }
192 Py_INCREF(begidx);
193 return begidx;
194}
195
196static char doc_get_begidx[] = "\
197get_begidx() -> int\n\
198get the beginning index of the readline tab-completion scope";
199
200/* get the ending index for the scope of the tab-completion */
201static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000202get_endidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000203{
204 if(!PyArg_NoArgs(args)) {
205 return NULL;
206 }
207 Py_INCREF(endidx);
208 return endidx;
209}
210
211static char doc_get_endidx[] = "\
212get_endidx() -> int\n\
213get the ending index of the readline tab-completion scope";
214
215
216/* set the tab-completion word-delimiters that readline uses */
217
218static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000219set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000220{
221 char *break_chars;
222
Guido van Rossum43713e52000-02-29 13:59:29 +0000223 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000224 return NULL;
225 }
226 free(rl_completer_word_break_characters);
227 rl_completer_word_break_characters = strdup(break_chars);
228 Py_INCREF(Py_None);
229 return Py_None;
230}
231
232static char doc_set_completer_delims[] = "\
233set_completer_delims(string) -> None\n\
234set the readline word delimiters for tab-completion";
235
236
237/* get the tab-completion word-delimiters that readline uses */
238
239static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000240get_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000241{
242 if(!PyArg_NoArgs(args)) {
243 return NULL;
244 }
245 return PyString_FromString(rl_completer_word_break_characters);
246}
247
248static char doc_get_completer_delims[] = "\
249get_completer_delims() -> string\n\
250get the readline word delimiters for tab-completion";
251
Guido van Rossum290900a1997-09-26 21:51:21 +0000252static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000253set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000254{
255 PyObject *function = Py_None;
Guido van Rossum43713e52000-02-29 13:59:29 +0000256 if (!PyArg_ParseTuple(args, "|O:set_completer", &function))
Guido van Rossum290900a1997-09-26 21:51:21 +0000257 return NULL;
258 if (function == Py_None) {
259 Py_XDECREF(completer);
260 completer = NULL;
261 tstate = NULL;
262 }
263 else if (PyCallable_Check(function)) {
264 PyObject *tmp = completer;
265 Py_INCREF(function);
266 completer = function;
267 Py_XDECREF(tmp);
268 tstate = PyThreadState_Get();
269 }
270 else {
271 PyErr_SetString(PyExc_TypeError,
272 "set_completer(func): argument not callable");
273 return NULL;
274 }
275 Py_INCREF(Py_None);
276 return Py_None;
277}
278
279static char doc_set_completer[] = "\
280set_completer([function]) -> None\n\
281Set or remove the completer function.\n\
282The function is called as function(text, state),\n\
283for i in [0, 1, 2, ...] until it returns a non-string.\n\
284It should return the next possible completion starting with 'text'.\
285";
286
Guido van Rossum79378ff1997-10-07 14:53:21 +0000287/* Exported function to read the current line buffer */
288
289static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000290get_line_buffer(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000291{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000292 if (!PyArg_NoArgs(args))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000293 return NULL;
294 return PyString_FromString(rl_line_buffer);
295}
296
297static char doc_get_line_buffer[] = "\
Guido van Rossuma88c5f31998-05-20 15:50:56 +0000298get_line_buffer() -> string\n\
Guido van Rossum79378ff1997-10-07 14:53:21 +0000299return the current contents of the line buffer.\
300";
301
302/* Exported function to insert text into the line buffer */
303
304static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000305insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000306{
307 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000308 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000309 return NULL;
310 rl_insert_text(s);
311 Py_INCREF(Py_None);
312 return Py_None;
313}
314
315
316static char doc_insert_text[] = "\
317insert_text(string) -> None\n\
318Insert text into the command line.\
319";
320
Guido van Rossum290900a1997-09-26 21:51:21 +0000321
322/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000323
324static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000325{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000326 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000327 {"get_line_buffer", get_line_buffer,
328 METH_OLDARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000329 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
330 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
331 {"read_history_file", read_history_file,
332 METH_VARARGS, doc_read_history_file},
333 {"write_history_file", write_history_file,
334 METH_VARARGS, doc_write_history_file},
335 {"set_history_length", set_history_length,
336 METH_VARARGS, set_history_length_doc},
337 {"get_history_length", get_history_length,
338 METH_VARARGS, get_history_length_doc},
339 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000340 {"get_begidx", get_begidx, METH_OLDARGS, doc_get_begidx},
341 {"get_endidx", get_endidx, METH_OLDARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000342
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000343 {"set_completer_delims", set_completer_delims,
344 METH_VARARGS, doc_set_completer_delims},
345 {"get_completer_delims", get_completer_delims,
346 METH_OLDARGS, doc_get_completer_delims},
Guido van Rossum290900a1997-09-26 21:51:21 +0000347 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000348};
349
Guido van Rossum290900a1997-09-26 21:51:21 +0000350/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000351
Guido van Rossum290900a1997-09-26 21:51:21 +0000352static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000353on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000354{
Guido van Rossum290900a1997-09-26 21:51:21 +0000355 char *result = NULL;
356 if (completer != NULL) {
357 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000358 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000359 /* Note that readline is called with the interpreter
360 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000361 save_tstate = PyThreadState_Swap(NULL);
Guido van Rossum290900a1997-09-26 21:51:21 +0000362 PyEval_RestoreThread(tstate);
363 r = PyObject_CallFunction(completer, "si", text, state);
364 if (r == NULL)
365 goto error;
366 if (r == Py_None) {
367 result = NULL;
368 }
369 else {
370 char *s = PyString_AsString(r);
371 if (s == NULL)
372 goto error;
373 result = strdup(s);
374 }
375 Py_DECREF(r);
376 goto done;
377 error:
378 PyErr_Clear();
379 Py_XDECREF(r);
380 done:
381 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000382 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000383 }
384 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000385}
386
Guido van Rossum290900a1997-09-26 21:51:21 +0000387
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000388/* a more flexible constructor that saves the "begidx" and "endidx"
389 * before calling the normal completer */
390
391char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000392flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000393{
394 Py_XDECREF(begidx);
395 Py_XDECREF(endidx);
396 begidx = PyInt_FromLong((long) start);
397 endidx = PyInt_FromLong((long) end);
398 return completion_matches(text, *on_completion);
399}
400
Guido van Rossum290900a1997-09-26 21:51:21 +0000401/* Helper to initialize GNU readline properly. */
402
403static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000404setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000405{
406 rl_readline_name = "python";
407 /* Force rebind of TAB to insert-tab */
408 rl_bind_key('\t', rl_insert);
409 /* Bind both ESC-TAB and ESC-ESC to the completion function */
410 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
411 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
412 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000413 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000414 /* Set Python word break characters */
415 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000416 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000417 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000418
419 begidx = PyInt_FromLong(0L);
420 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000421 /* Initialize (allows .inputrc to override)
422 *
423 * XXX: A bug in the readline-2.2 library causes a memory leak
424 * inside this function. Nothing we can do about it.
425 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000426 rl_initialize();
427}
428
429
430/* Interrupt handler */
431
432static jmp_buf jbuf;
433
Guido van Rossum0969d361997-08-05 21:27:50 +0000434/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000435static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000436onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000437{
Guido van Rossum290900a1997-09-26 21:51:21 +0000438 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000439}
440
Guido van Rossum290900a1997-09-26 21:51:21 +0000441
442/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000443
444static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000445call_readline(char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000446{
Guido van Rossum26418a92000-06-28 21:30:31 +0000447 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000448 char *p, *q;
Tim Peters4f1b2082000-07-23 21:18:09 +0000449 void (*old_inthandler)(int);
Guido van Rossum0969d361997-08-05 21:27:50 +0000450 old_inthandler = signal(SIGINT, onintr);
451 if (setjmp(jbuf)) {
452#ifdef HAVE_SIGRELSE
453 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
454 sigrelse(SIGINT);
455#endif
456 signal(SIGINT, old_inthandler);
457 return NULL;
458 }
Guido van Rossum44620641997-08-11 18:57:29 +0000459 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000460 p = readline(prompt);
461 signal(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000462
463 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000464 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000465 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000466 if (p != NULL)
467 *p = '\0';
468 return p;
469 }
470 n = strlen(p);
471 if (n > 0)
472 add_history(p);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000473 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
474 release the original. */
475 q = p;
476 p = PyMem_Malloc(n+2);
477 if (p != NULL) {
478 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000479 p[n] = '\n';
480 p[n+1] = '\0';
481 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000482 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000483 return p;
484}
485
Guido van Rossum290900a1997-09-26 21:51:21 +0000486
487/* Initialize the module */
488
489static char doc_module[] =
490"Importing this module enables command line editing using GNU readline.";
491
Guido van Rossum3886bb61998-12-04 18:50:17 +0000492DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000493initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000494{
495 PyObject *m;
496
497 m = Py_InitModule4("readline", readline_methods, doc_module,
498 (PyObject *)NULL, PYTHON_API_VERSION);
499 if (isatty(fileno(stdin))) {
500 PyOS_ReadlineFunctionPointer = call_readline;
501 setup_readline();
502 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000503}