blob: 4f8969ba61a82a97f4e8b02e6bb330758da24a19 [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;
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
152 return NULL;
153 history_length = length;
154 Py_INCREF(Py_None);
155 return Py_None;
156}
157
158
159
160static char get_history_length_doc[] = "\
161get_history_length() -> int\n\
162return the current history length value.\n\
163";
164
165static PyObject*
166get_history_length(PyObject *self, PyObject *args)
167{
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000168 if (!PyArg_ParseTuple(args, ":get_history_length"))
169 return NULL;
170 return Py_BuildValue("i", history_length);
171}
172
173
174
Guido van Rossum290900a1997-09-26 21:51:21 +0000175/* Exported function to specify a word completer in Python */
176
177static PyObject *completer = NULL;
178static PyThreadState *tstate = NULL;
179
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000180static PyObject *begidx = NULL;
181static PyObject *endidx = NULL;
182
183/* get the beginning index for the scope of the tab-completion */
184static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000185get_begidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000186{
187 if(!PyArg_NoArgs(args)) {
188 return NULL;
189 }
190 Py_INCREF(begidx);
191 return begidx;
192}
193
194static char doc_get_begidx[] = "\
195get_begidx() -> int\n\
196get the beginning index of the readline tab-completion scope";
197
198/* get the ending index for the scope of the tab-completion */
199static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000200get_endidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000201{
202 if(!PyArg_NoArgs(args)) {
203 return NULL;
204 }
205 Py_INCREF(endidx);
206 return endidx;
207}
208
209static char doc_get_endidx[] = "\
210get_endidx() -> int\n\
211get the ending index of the readline tab-completion scope";
212
213
214/* set the tab-completion word-delimiters that readline uses */
215
216static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000217set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000218{
219 char *break_chars;
220
Guido van Rossum43713e52000-02-29 13:59:29 +0000221 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000222 return NULL;
223 }
224 free(rl_completer_word_break_characters);
225 rl_completer_word_break_characters = strdup(break_chars);
226 Py_INCREF(Py_None);
227 return Py_None;
228}
229
230static char doc_set_completer_delims[] = "\
231set_completer_delims(string) -> None\n\
232set the readline word delimiters for tab-completion";
233
234
235/* get the tab-completion word-delimiters that readline uses */
236
237static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000238get_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000239{
240 if(!PyArg_NoArgs(args)) {
241 return NULL;
242 }
243 return PyString_FromString(rl_completer_word_break_characters);
244}
245
246static char doc_get_completer_delims[] = "\
247get_completer_delims() -> string\n\
248get the readline word delimiters for tab-completion";
249
Guido van Rossum290900a1997-09-26 21:51:21 +0000250static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000251set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000252{
253 PyObject *function = Py_None;
Guido van Rossum43713e52000-02-29 13:59:29 +0000254 if (!PyArg_ParseTuple(args, "|O:set_completer", &function))
Guido van Rossum290900a1997-09-26 21:51:21 +0000255 return NULL;
256 if (function == Py_None) {
257 Py_XDECREF(completer);
258 completer = NULL;
259 tstate = NULL;
260 }
261 else if (PyCallable_Check(function)) {
262 PyObject *tmp = completer;
263 Py_INCREF(function);
264 completer = function;
265 Py_XDECREF(tmp);
266 tstate = PyThreadState_Get();
267 }
268 else {
269 PyErr_SetString(PyExc_TypeError,
270 "set_completer(func): argument not callable");
271 return NULL;
272 }
273 Py_INCREF(Py_None);
274 return Py_None;
275}
276
277static char doc_set_completer[] = "\
278set_completer([function]) -> None\n\
279Set or remove the completer function.\n\
280The function is called as function(text, state),\n\
281for i in [0, 1, 2, ...] until it returns a non-string.\n\
282It should return the next possible completion starting with 'text'.\
283";
284
Guido van Rossum79378ff1997-10-07 14:53:21 +0000285/* Exported function to read the current line buffer */
286
287static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000288get_line_buffer(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000289{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000290 if (!PyArg_NoArgs(args))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000291 return NULL;
292 return PyString_FromString(rl_line_buffer);
293}
294
295static char doc_get_line_buffer[] = "\
Guido van Rossuma88c5f31998-05-20 15:50:56 +0000296get_line_buffer() -> string\n\
Guido van Rossum79378ff1997-10-07 14:53:21 +0000297return the current contents of the line buffer.\
298";
299
300/* Exported function to insert text into the line buffer */
301
302static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000303insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000304{
305 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000306 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000307 return NULL;
308 rl_insert_text(s);
309 Py_INCREF(Py_None);
310 return Py_None;
311}
312
313
314static char doc_insert_text[] = "\
315insert_text(string) -> None\n\
316Insert text into the command line.\
317";
318
Guido van Rossum290900a1997-09-26 21:51:21 +0000319
320/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000321
322static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000323{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000324 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000325 {"get_line_buffer", get_line_buffer,
326 METH_OLDARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000327 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
328 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
329 {"read_history_file", read_history_file,
330 METH_VARARGS, doc_read_history_file},
331 {"write_history_file", write_history_file,
332 METH_VARARGS, doc_write_history_file},
333 {"set_history_length", set_history_length,
334 METH_VARARGS, set_history_length_doc},
335 {"get_history_length", get_history_length,
336 METH_VARARGS, get_history_length_doc},
337 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000338 {"get_begidx", get_begidx, METH_OLDARGS, doc_get_begidx},
339 {"get_endidx", get_endidx, METH_OLDARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000340
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000341 {"set_completer_delims", set_completer_delims,
342 METH_VARARGS, doc_set_completer_delims},
343 {"get_completer_delims", get_completer_delims,
344 METH_OLDARGS, doc_get_completer_delims},
Guido van Rossum290900a1997-09-26 21:51:21 +0000345 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000346};
347
Guido van Rossum290900a1997-09-26 21:51:21 +0000348/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000349
Guido van Rossum290900a1997-09-26 21:51:21 +0000350static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000351on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000352{
Guido van Rossum290900a1997-09-26 21:51:21 +0000353 char *result = NULL;
354 if (completer != NULL) {
355 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000356 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000357 /* Note that readline is called with the interpreter
358 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000359 save_tstate = PyThreadState_Swap(NULL);
Guido van Rossum290900a1997-09-26 21:51:21 +0000360 PyEval_RestoreThread(tstate);
361 r = PyObject_CallFunction(completer, "si", text, state);
362 if (r == NULL)
363 goto error;
364 if (r == Py_None) {
365 result = NULL;
366 }
367 else {
368 char *s = PyString_AsString(r);
369 if (s == NULL)
370 goto error;
371 result = strdup(s);
372 }
373 Py_DECREF(r);
374 goto done;
375 error:
376 PyErr_Clear();
377 Py_XDECREF(r);
378 done:
379 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000380 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000381 }
382 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000383}
384
Guido van Rossum290900a1997-09-26 21:51:21 +0000385
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000386/* a more flexible constructor that saves the "begidx" and "endidx"
387 * before calling the normal completer */
388
389char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000390flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000391{
392 Py_XDECREF(begidx);
393 Py_XDECREF(endidx);
394 begidx = PyInt_FromLong((long) start);
395 endidx = PyInt_FromLong((long) end);
396 return completion_matches(text, *on_completion);
397}
398
Guido van Rossum290900a1997-09-26 21:51:21 +0000399/* Helper to initialize GNU readline properly. */
400
401static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000402setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000403{
404 rl_readline_name = "python";
405 /* Force rebind of TAB to insert-tab */
406 rl_bind_key('\t', rl_insert);
407 /* Bind both ESC-TAB and ESC-ESC to the completion function */
408 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
409 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
410 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000411 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000412 /* Set Python word break characters */
413 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000414 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000415 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000416
417 begidx = PyInt_FromLong(0L);
418 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000419 /* Initialize (allows .inputrc to override)
420 *
421 * XXX: A bug in the readline-2.2 library causes a memory leak
422 * inside this function. Nothing we can do about it.
423 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000424 rl_initialize();
425}
426
427
428/* Interrupt handler */
429
430static jmp_buf jbuf;
431
Guido van Rossum0969d361997-08-05 21:27:50 +0000432/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000433static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000434onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000435{
Guido van Rossum290900a1997-09-26 21:51:21 +0000436 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000437}
438
Guido van Rossum290900a1997-09-26 21:51:21 +0000439
440/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000441
442static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000443call_readline(char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000444{
Guido van Rossum26418a92000-06-28 21:30:31 +0000445 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000446 char *p, *q;
Tim Peters4f1b2082000-07-23 21:18:09 +0000447 void (*old_inthandler)(int);
Guido van Rossum0969d361997-08-05 21:27:50 +0000448 old_inthandler = signal(SIGINT, onintr);
449 if (setjmp(jbuf)) {
450#ifdef HAVE_SIGRELSE
451 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
452 sigrelse(SIGINT);
453#endif
454 signal(SIGINT, old_inthandler);
455 return NULL;
456 }
Guido van Rossum44620641997-08-11 18:57:29 +0000457 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000458 p = readline(prompt);
459 signal(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000460
461 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000462 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000463 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000464 if (p != NULL)
465 *p = '\0';
466 return p;
467 }
468 n = strlen(p);
469 if (n > 0)
470 add_history(p);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000471 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
472 release the original. */
473 q = p;
474 p = PyMem_Malloc(n+2);
475 if (p != NULL) {
476 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000477 p[n] = '\n';
478 p[n+1] = '\0';
479 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000480 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000481 return p;
482}
483
Guido van Rossum290900a1997-09-26 21:51:21 +0000484
485/* Initialize the module */
486
487static char doc_module[] =
488"Importing this module enables command line editing using GNU readline.";
489
Guido van Rossum3886bb61998-12-04 18:50:17 +0000490DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000491initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000492{
493 PyObject *m;
494
495 m = Py_InitModule4("readline", readline_methods, doc_module,
496 (PyObject *)NULL, PYTHON_API_VERSION);
497 if (isatty(fileno(stdin))) {
498 PyOS_ReadlineFunctionPointer = call_readline;
499 setup_readline();
500 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000501}