blob: 44adf22b63f0624801bf2b2e18ab0264b3f578b6 [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 Rossum290900a1997-09-26 21:51:21 +000015/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000016#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000017#include <readline/readline.h>
18#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000019
Guido van Rossum353ae582001-07-10 16:45:32 +000020#ifdef HAVE_RL_COMPLETION_MATCHES
21#define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
22#endif
23
Guido van Rossum290900a1997-09-26 21:51:21 +000024/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum5a530192001-01-10 21:03:32 +000025extern DL_IMPORT(int) (*PyOS_InputHook)(void);
26extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(char *);
Guido van Rossum0969d361997-08-05 21:27:50 +000027
Guido van Rossum0969d361997-08-05 21:27:50 +000028
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* Exported function to send one line to readline's init file parser */
30
31static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000032parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000033{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000034 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000035 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000036 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000037 /* Make a copy -- rl_parse_and_bind() modifies its argument */
38 /* Bernard Herzog */
39 copy = malloc(1 + strlen(s));
40 if (copy == NULL)
41 return PyErr_NoMemory();
42 strcpy(copy, s);
43 rl_parse_and_bind(copy);
44 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000045 Py_INCREF(Py_None);
46 return Py_None;
47}
48
49static char doc_parse_and_bind[] = "\
50parse_and_bind(string) -> None\n\
51Parse and execute single line of a readline init file.\
52";
53
54
55/* Exported function to parse a readline init file */
56
57static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000058read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000059{
60 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000061 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000062 return NULL;
63 errno = rl_read_init_file(s);
64 if (errno)
65 return PyErr_SetFromErrno(PyExc_IOError);
66 Py_INCREF(Py_None);
67 return Py_None;
68}
69
70static char doc_read_init_file[] = "\
71read_init_file([filename]) -> None\n\
72Parse a readline initialization file.\n\
73The default filename is the last filename used.\
74";
75
76
Skip Montanaro28067822000-07-06 18:55:12 +000077/* Exported function to load a readline history file */
78
79static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000080read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000081{
82 char *s = NULL;
83 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
84 return NULL;
85 errno = read_history(s);
86 if (errno)
87 return PyErr_SetFromErrno(PyExc_IOError);
88 Py_INCREF(Py_None);
89 return Py_None;
90}
91
Skip Montanaro49bd24d2000-07-19 16:54:53 +000092static int history_length = -1; /* do not truncate history by default */
Skip Montanaro28067822000-07-06 18:55:12 +000093static char doc_read_history_file[] = "\
94read_history_file([filename]) -> None\n\
95Load a readline history file.\n\
96The default filename is ~/.history.\
97";
98
99
100/* Exported function to save a readline history file */
101
102static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000103write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000104{
105 char *s = NULL;
106 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
107 return NULL;
108 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000109 if (!errno && history_length >= 0)
110 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000111 if (errno)
112 return PyErr_SetFromErrno(PyExc_IOError);
113 Py_INCREF(Py_None);
114 return Py_None;
115}
116
117static char doc_write_history_file[] = "\
118write_history_file([filename]) -> None\n\
119Save a readline history file.\n\
120The default filename is ~/.history.\
121";
122
123
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000124static char set_history_length_doc[] = "\
125set_history_length(length) -> None\n\
126set the maximal number of items which will be written to\n\
127the history file. A negative length is used to inhibit\n\
128history truncation.\n\
129";
130
131static PyObject*
132set_history_length(PyObject *self, PyObject *args)
133{
134 int length = history_length;
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000135 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
136 return NULL;
137 history_length = length;
138 Py_INCREF(Py_None);
139 return Py_None;
140}
141
142
143
144static char get_history_length_doc[] = "\
145get_history_length() -> int\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000146return the maximum number of items that will be written to\n\
147the history file.\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000148";
149
150static PyObject*
151get_history_length(PyObject *self, PyObject *args)
152{
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000153 if (!PyArg_ParseTuple(args, ":get_history_length"))
154 return NULL;
155 return Py_BuildValue("i", history_length);
156}
157
Martin v. Löwis0daad592001-09-30 21:09:59 +0000158/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000159
Martin v. Löwis0daad592001-09-30 21:09:59 +0000160static PyObject *
161set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args)
162{
163 PyObject *function = Py_None;
164 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000165 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000166 if (!PyArg_ParseTuple(args, buf, &function))
167 return NULL;
168 if (function == Py_None) {
169 Py_XDECREF(*hook_var);
170 *hook_var = NULL;
171 *tstate = NULL;
172 }
173 else if (PyCallable_Check(function)) {
174 PyObject *tmp = *hook_var;
175 Py_INCREF(function);
176 *hook_var = function;
177 Py_XDECREF(tmp);
178 *tstate = PyThreadState_Get();
179 }
180 else {
Tim Peters885d4572001-11-28 20:27:42 +0000181 PyOS_snprintf(buf, sizeof(buf),
182 "set_%.50s(func): argument not callable",
183 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000184 PyErr_SetString(PyExc_TypeError, buf);
185 return NULL;
186 }
187 Py_INCREF(Py_None);
188 return Py_None;
189}
190
191/* Exported functions to specify hook functions in Python */
192
193static PyObject *startup_hook = NULL;
194static PyThreadState *startup_hook_tstate = NULL;
195
196#ifdef HAVE_RL_PRE_INPUT_HOOK
197static PyObject *pre_input_hook = NULL;
198static PyThreadState *pre_input_hook_tstate = NULL;
199#endif
200
201static PyObject *
202set_startup_hook(PyObject *self, PyObject *args)
203{
204 return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args);
205}
206
207static char doc_set_startup_hook[] = "\
208set_startup_hook([function]) -> None\n\
209Set or remove the startup_hook function.\n\
210The function is called with no arguments just\n\
211before readline prints the first prompt.\n\
212";
213
214#ifdef HAVE_RL_PRE_INPUT_HOOK
215static PyObject *
216set_pre_input_hook(PyObject *self, PyObject *args)
217{
218 return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args);
219}
220
221static char doc_set_pre_input_hook[] = "\
222set_pre_input_hook([function]) -> None\n\
223Set or remove the pre_input_hook function.\n\
224The function is called with no arguments after the first prompt\n\
225has been printed and just before readline starts reading input\n\
226characters.\n\
227";
228#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000229
Guido van Rossum290900a1997-09-26 21:51:21 +0000230/* Exported function to specify a word completer in Python */
231
232static PyObject *completer = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000233static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000234
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000235static PyObject *begidx = NULL;
236static PyObject *endidx = NULL;
237
238/* get the beginning index for the scope of the tab-completion */
239static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000240get_begidx(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000241{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000242 Py_INCREF(begidx);
243 return begidx;
244}
245
246static char doc_get_begidx[] = "\
247get_begidx() -> int\n\
248get the beginning index of the readline tab-completion scope";
249
250/* get the ending index for the scope of the tab-completion */
251static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000252get_endidx(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000253{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000254 Py_INCREF(endidx);
255 return endidx;
256}
257
258static char doc_get_endidx[] = "\
259get_endidx() -> int\n\
260get the ending index of the readline tab-completion scope";
261
262
263/* set the tab-completion word-delimiters that readline uses */
264
265static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000266set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000267{
268 char *break_chars;
269
Guido van Rossum43713e52000-02-29 13:59:29 +0000270 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000271 return NULL;
272 }
273 free(rl_completer_word_break_characters);
274 rl_completer_word_break_characters = strdup(break_chars);
275 Py_INCREF(Py_None);
276 return Py_None;
277}
278
279static char doc_set_completer_delims[] = "\
280set_completer_delims(string) -> None\n\
281set the readline word delimiters for tab-completion";
282
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000283static PyObject *
284py_add_history(PyObject *self, PyObject *args)
285{
286 char *line;
287
288 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
289 return NULL;
290 }
291 add_history(line);
292 Py_INCREF(Py_None);
293 return Py_None;
294}
295
296static char doc_add_history[] = "\
297add_history(string) -> None\n\
298add a line to the history buffer";
299
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000300
301/* get the tab-completion word-delimiters that readline uses */
302
303static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000304get_completer_delims(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000305{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000306 return PyString_FromString(rl_completer_word_break_characters);
307}
308
309static char doc_get_completer_delims[] = "\
310get_completer_delims() -> string\n\
311get the readline word delimiters for tab-completion";
312
Guido van Rossum290900a1997-09-26 21:51:21 +0000313static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000314set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000315{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000316 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000317}
318
319static char doc_set_completer[] = "\
320set_completer([function]) -> None\n\
321Set or remove the completer function.\n\
322The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000323for state in 0, 1, 2, ..., until it returns a non-string.\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000324It should return the next possible completion starting with 'text'.\
325";
326
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000327/* Exported function to get any element of history */
328
329static PyObject *
330get_history_item(PyObject *self, PyObject *args)
331{
332 int idx = 0;
333 HIST_ENTRY *hist_ent;
334
335 if (!PyArg_ParseTuple(args, "i:index", &idx))
336 return NULL;
337 if ((hist_ent = history_get(idx)))
338 return PyString_FromString(hist_ent->line);
339 else {
340 Py_INCREF(Py_None);
341 return Py_None;
342 }
343}
344
345static char doc_get_history_item[] = "\
346get_history_item() -> string\n\
347return the current contents of history item at index.\
348";
349
350/* Exported function to get current length of history */
351
352static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000353get_current_history_length(PyObject *self)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000354{
355 HISTORY_STATE *hist_st;
356
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000357 hist_st = history_get_history_state();
358 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
359}
360
361static char doc_get_current_history_length[] = "\
362get_current_history_length() -> integer\n\
363return the current (not the maximum) length of history.\
364";
365
Guido van Rossum79378ff1997-10-07 14:53:21 +0000366/* Exported function to read the current line buffer */
367
368static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000369get_line_buffer(PyObject *self)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000370{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000371 return PyString_FromString(rl_line_buffer);
372}
373
374static char doc_get_line_buffer[] = "\
Guido van Rossuma88c5f31998-05-20 15:50:56 +0000375get_line_buffer() -> string\n\
Guido van Rossum79378ff1997-10-07 14:53:21 +0000376return the current contents of the line buffer.\
377";
378
379/* Exported function to insert text into the line buffer */
380
381static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000382insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000383{
384 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000385 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000386 return NULL;
387 rl_insert_text(s);
388 Py_INCREF(Py_None);
389 return Py_None;
390}
391
Guido van Rossum79378ff1997-10-07 14:53:21 +0000392static char doc_insert_text[] = "\
393insert_text(string) -> None\n\
394Insert text into the command line.\
395";
396
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000397static PyObject *
398redisplay(PyObject *self)
399{
400 rl_redisplay();
401 Py_INCREF(Py_None);
402 return Py_None;
403}
404
405static char doc_redisplay[] = "\
406redisplay() -> None\n\
407Change what's displayed on the screen to reflect the current\n\
408contents of the line buffer.\
409";
Guido van Rossum290900a1997-09-26 21:51:21 +0000410
411/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000412
413static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000414{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000415 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Neal Norwitz767f8352002-03-31 16:13:39 +0000416 {"get_line_buffer", (PyCFunction)get_line_buffer,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000417 METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000418 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000419 {"redisplay", (PyCFunction)redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000420 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
421 {"read_history_file", read_history_file,
422 METH_VARARGS, doc_read_history_file},
423 {"write_history_file", write_history_file,
424 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000425 {"get_history_item", get_history_item,
426 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000427 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000428 METH_NOARGS, doc_get_current_history_length},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000429 {"set_history_length", set_history_length,
430 METH_VARARGS, set_history_length_doc},
431 {"get_history_length", get_history_length,
432 METH_VARARGS, get_history_length_doc},
433 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Neal Norwitz767f8352002-03-31 16:13:39 +0000434 {"get_begidx", (PyCFunction)get_begidx, METH_NOARGS, doc_get_begidx},
435 {"get_endidx", (PyCFunction)get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000436
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000437 {"set_completer_delims", set_completer_delims,
438 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000439 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Neal Norwitz767f8352002-03-31 16:13:39 +0000440 {"get_completer_delims", (PyCFunction)get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000441 METH_NOARGS, doc_get_completer_delims},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000442
443 {"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook},
444#ifdef HAVE_RL_PRE_INPUT_HOOK
445 {"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook},
446#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000447 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000448};
449
Martin v. Löwis0daad592001-09-30 21:09:59 +0000450/* C function to call the Python hooks. */
451
452static int
453on_hook(PyObject *func, PyThreadState *tstate)
454{
455 int result = 0;
456 if (func != NULL) {
457 PyObject *r;
458 PyThreadState *save_tstate;
459 /* Note that readline is called with the interpreter
460 lock released! */
461 save_tstate = PyThreadState_Swap(NULL);
462 PyEval_RestoreThread(tstate);
463 r = PyObject_CallFunction(func, NULL);
464 if (r == NULL)
465 goto error;
466 if (r == Py_None)
467 result = 0;
468 else
469 result = PyInt_AsLong(r);
470 Py_DECREF(r);
471 goto done;
472 error:
473 PyErr_Clear();
474 Py_XDECREF(r);
475 done:
476 PyEval_SaveThread();
477 PyThreadState_Swap(save_tstate);
478 }
479 return result;
480}
481
482static int
483on_startup_hook(void)
484{
485 return on_hook(startup_hook, startup_hook_tstate);
486}
487
488#ifdef HAVE_RL_PRE_INPUT_HOOK
489static int
490on_pre_input_hook(void)
491{
492 return on_hook(pre_input_hook, pre_input_hook_tstate);
493}
494#endif
495
Guido van Rossum290900a1997-09-26 21:51:21 +0000496/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000497
Guido van Rossum290900a1997-09-26 21:51:21 +0000498static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000499on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000500{
Guido van Rossum290900a1997-09-26 21:51:21 +0000501 char *result = NULL;
502 if (completer != NULL) {
503 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000504 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000505 /* Note that readline is called with the interpreter
506 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000507 save_tstate = PyThreadState_Swap(NULL);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000508 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000509 /* Don't use the default filename completion if we
510 * have a custom completion function... */
511 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000512 r = PyObject_CallFunction(completer, "si", text, state);
513 if (r == NULL)
514 goto error;
515 if (r == Py_None) {
516 result = NULL;
517 }
518 else {
519 char *s = PyString_AsString(r);
520 if (s == NULL)
521 goto error;
522 result = strdup(s);
523 }
524 Py_DECREF(r);
525 goto done;
526 error:
527 PyErr_Clear();
528 Py_XDECREF(r);
529 done:
530 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000531 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000532 }
533 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000534}
535
Guido van Rossum290900a1997-09-26 21:51:21 +0000536
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000537/* a more flexible constructor that saves the "begidx" and "endidx"
538 * before calling the normal completer */
539
540char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000541flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000542{
543 Py_XDECREF(begidx);
544 Py_XDECREF(endidx);
545 begidx = PyInt_FromLong((long) start);
546 endidx = PyInt_FromLong((long) end);
547 return completion_matches(text, *on_completion);
548}
549
Guido van Rossum290900a1997-09-26 21:51:21 +0000550/* Helper to initialize GNU readline properly. */
551
552static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000553setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000554{
555 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000556#if defined(PYOS_OS2) && defined(PYCC_GCC)
557 /* Allow $if term= in .inputrc to work */
558 rl_terminal_name = getenv("TERM");
559#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000560 /* Force rebind of TAB to insert-tab */
561 rl_bind_key('\t', rl_insert);
562 /* Bind both ESC-TAB and ESC-ESC to the completion function */
563 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
564 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000565 /* Set our hook functions */
566 rl_startup_hook = (Function *)on_startup_hook;
567#ifdef HAVE_RL_PRE_INPUT_HOOK
568 rl_pre_input_hook = (Function *)on_pre_input_hook;
569#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000570 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000571 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000572 /* Set Python word break characters */
573 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000574 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000575 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000576
577 begidx = PyInt_FromLong(0L);
578 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000579 /* Initialize (allows .inputrc to override)
580 *
581 * XXX: A bug in the readline-2.2 library causes a memory leak
582 * inside this function. Nothing we can do about it.
583 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000584 rl_initialize();
585}
586
587
588/* Interrupt handler */
589
590static jmp_buf jbuf;
591
Guido van Rossum0969d361997-08-05 21:27:50 +0000592/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000593static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000594onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000595{
Guido van Rossum290900a1997-09-26 21:51:21 +0000596 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000597}
598
Guido van Rossum290900a1997-09-26 21:51:21 +0000599
600/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000601
602static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000603call_readline(char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000604{
Guido van Rossum26418a92000-06-28 21:30:31 +0000605 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000606 char *p, *q;
Guido van Rossum174efc92000-09-16 16:37:53 +0000607 PyOS_sighandler_t old_inthandler;
608
609 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000610 if (setjmp(jbuf)) {
611#ifdef HAVE_SIGRELSE
612 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
613 sigrelse(SIGINT);
614#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000615 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossum0969d361997-08-05 21:27:50 +0000616 return NULL;
617 }
Guido van Rossum44620641997-08-11 18:57:29 +0000618 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000619 p = readline(prompt);
Guido van Rossum174efc92000-09-16 16:37:53 +0000620 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000621
622 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000623 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000624 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000625 if (p != NULL)
626 *p = '\0';
627 return p;
628 }
629 n = strlen(p);
630 if (n > 0)
631 add_history(p);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000632 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
633 release the original. */
634 q = p;
635 p = PyMem_Malloc(n+2);
636 if (p != NULL) {
637 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000638 p[n] = '\n';
639 p[n+1] = '\0';
640 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000641 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000642 return p;
643}
644
Guido van Rossum290900a1997-09-26 21:51:21 +0000645
646/* Initialize the module */
647
648static char doc_module[] =
649"Importing this module enables command line editing using GNU readline.";
650
Guido van Rossum3886bb61998-12-04 18:50:17 +0000651DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000652initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000653{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000654 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000655
656 m = Py_InitModule4("readline", readline_methods, doc_module,
657 (PyObject *)NULL, PYTHON_API_VERSION);
658 if (isatty(fileno(stdin))) {
659 PyOS_ReadlineFunctionPointer = call_readline;
660 setup_readline();
661 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000662}