blob: 7535ecf5b976411ac844631a819513a7689b8f51 [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 Rossum60c8a3a2002-10-09 21:27:33 +000015#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
16/* GNU readline() mistakenly sets the LC_CTYPE locale.
17 * This is evil. Only the user or the app's main() should do this!
18 * We must save and restore the locale around the rl_initialize() call.
19 */
20#define SAVE_LOCALE
21#include <locale.h>
22#endif
23
Guido van Rossum290900a1997-09-26 21:51:21 +000024/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000025#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000026#include <readline/readline.h>
27#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000028
Guido van Rossum353ae582001-07-10 16:45:32 +000029#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000030#define completion_matches(x, y) \
31 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossum353ae582001-07-10 16:45:32 +000032#endif
33
Guido van Rossum0969d361997-08-05 21:27:50 +000034
Guido van Rossum290900a1997-09-26 21:51:21 +000035/* Exported function to send one line to readline's init file parser */
36
37static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000038parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000039{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000040 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000041 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000042 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000043 /* Make a copy -- rl_parse_and_bind() modifies its argument */
44 /* Bernard Herzog */
45 copy = malloc(1 + strlen(s));
46 if (copy == NULL)
47 return PyErr_NoMemory();
48 strcpy(copy, s);
49 rl_parse_and_bind(copy);
50 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000051 Py_INCREF(Py_None);
52 return Py_None;
53}
54
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055PyDoc_STRVAR(doc_parse_and_bind,
56"parse_and_bind(string) -> None\n\
57Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000058
59
60/* Exported function to parse a readline init file */
61
62static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000063read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000064{
65 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000066 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000067 return NULL;
68 errno = rl_read_init_file(s);
69 if (errno)
70 return PyErr_SetFromErrno(PyExc_IOError);
71 Py_INCREF(Py_None);
72 return Py_None;
73}
74
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075PyDoc_STRVAR(doc_read_init_file,
76"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000077Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000078The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000079
80
Skip Montanaro28067822000-07-06 18:55:12 +000081/* Exported function to load a readline history file */
82
83static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000084read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000085{
86 char *s = NULL;
87 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
88 return NULL;
89 errno = read_history(s);
90 if (errno)
91 return PyErr_SetFromErrno(PyExc_IOError);
92 Py_INCREF(Py_None);
93 return Py_None;
94}
95
Skip Montanaro49bd24d2000-07-19 16:54:53 +000096static int history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000097PyDoc_STRVAR(doc_read_history_file,
98"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +000099Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000100The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000101
102
103/* Exported function to save a readline history file */
104
105static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000106write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000107{
108 char *s = NULL;
109 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
110 return NULL;
111 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000112 if (!errno && history_length >= 0)
113 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000114 if (errno)
115 return PyErr_SetFromErrno(PyExc_IOError);
116 Py_INCREF(Py_None);
117 return Py_None;
118}
119
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000120PyDoc_STRVAR(doc_write_history_file,
121"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000122Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000123The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000124
125
Guido van Rossum74f31432003-01-07 20:01:29 +0000126/* Set history length */
127
128static PyObject*
129set_history_length(PyObject *self, PyObject *args)
130{
131 int length = history_length;
132 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
133 return NULL;
134 history_length = length;
135 Py_INCREF(Py_None);
136 return Py_None;
137}
138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000139PyDoc_STRVAR(set_history_length_doc,
140"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000141set the maximal number of items which will be written to\n\
142the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000143history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000144
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000145
Guido van Rossum74f31432003-01-07 20:01:29 +0000146/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000147
148static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000149get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000150{
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000151 return PyInt_FromLong(history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000152}
153
Guido van Rossum74f31432003-01-07 20:01:29 +0000154PyDoc_STRVAR(get_history_length_doc,
155"get_history_length() -> int\n\
156return the maximum number of items that will be written to\n\
157the history file.");
158
159
Martin v. Löwis0daad592001-09-30 21:09:59 +0000160/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000161
Martin v. Löwis0daad592001-09-30 21:09:59 +0000162static PyObject *
Guido van Rossum74f31432003-01-07 20:01:29 +0000163set_hook(const char *funcname, PyObject **hook_var,
164 PyThreadState **tstate, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000165{
166 PyObject *function = Py_None;
167 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000168 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000169 if (!PyArg_ParseTuple(args, buf, &function))
170 return NULL;
171 if (function == Py_None) {
172 Py_XDECREF(*hook_var);
173 *hook_var = NULL;
174 *tstate = NULL;
175 }
176 else if (PyCallable_Check(function)) {
177 PyObject *tmp = *hook_var;
178 Py_INCREF(function);
179 *hook_var = function;
180 Py_XDECREF(tmp);
181 *tstate = PyThreadState_Get();
182 }
183 else {
Tim Peters885d4572001-11-28 20:27:42 +0000184 PyOS_snprintf(buf, sizeof(buf),
185 "set_%.50s(func): argument not callable",
186 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000187 PyErr_SetString(PyExc_TypeError, buf);
188 return NULL;
189 }
190 Py_INCREF(Py_None);
191 return Py_None;
192}
193
Guido van Rossum74f31432003-01-07 20:01:29 +0000194
Martin v. Löwis0daad592001-09-30 21:09:59 +0000195/* Exported functions to specify hook functions in Python */
196
197static PyObject *startup_hook = NULL;
198static PyThreadState *startup_hook_tstate = NULL;
199
200#ifdef HAVE_RL_PRE_INPUT_HOOK
201static PyObject *pre_input_hook = NULL;
202static PyThreadState *pre_input_hook_tstate = NULL;
203#endif
204
205static PyObject *
206set_startup_hook(PyObject *self, PyObject *args)
207{
Guido van Rossum74f31432003-01-07 20:01:29 +0000208 return set_hook("startup_hook", &startup_hook,
209 &startup_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000210}
211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000212PyDoc_STRVAR(doc_set_startup_hook,
213"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000214Set or remove the startup_hook function.\n\
215The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000216before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000217
Guido van Rossum74f31432003-01-07 20:01:29 +0000218
Martin v. Löwis0daad592001-09-30 21:09:59 +0000219#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000220
221/* Set pre-input hook */
222
Martin v. Löwis0daad592001-09-30 21:09:59 +0000223static PyObject *
224set_pre_input_hook(PyObject *self, PyObject *args)
225{
Guido van Rossum74f31432003-01-07 20:01:29 +0000226 return set_hook("pre_input_hook", &pre_input_hook,
227 &pre_input_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000228}
229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000230PyDoc_STRVAR(doc_set_pre_input_hook,
231"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000232Set or remove the pre_input_hook function.\n\
233The function is called with no arguments after the first prompt\n\
234has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000236
Martin v. Löwis0daad592001-09-30 21:09:59 +0000237#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000238
Guido van Rossum74f31432003-01-07 20:01:29 +0000239
Guido van Rossum290900a1997-09-26 21:51:21 +0000240/* Exported function to specify a word completer in Python */
241
242static PyObject *completer = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000243static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000244
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000245static PyObject *begidx = NULL;
246static PyObject *endidx = NULL;
247
Guido van Rossum74f31432003-01-07 20:01:29 +0000248
249/* Get the beginning index for the scope of the tab-completion */
250
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000251static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000252get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000253{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000254 Py_INCREF(begidx);
255 return begidx;
256}
257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000258PyDoc_STRVAR(doc_get_begidx,
259"get_begidx() -> int\n\
260get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000261
Guido van Rossum74f31432003-01-07 20:01:29 +0000262
263/* Get the ending index for the scope of the tab-completion */
264
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000265static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000266get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000267{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000268 Py_INCREF(endidx);
269 return endidx;
270}
271
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000272PyDoc_STRVAR(doc_get_endidx,
273"get_endidx() -> int\n\
274get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000275
276
Guido van Rossum74f31432003-01-07 20:01:29 +0000277/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000278
279static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000280set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000281{
282 char *break_chars;
283
Guido van Rossum43713e52000-02-29 13:59:29 +0000284 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000285 return NULL;
286 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000287 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000288 rl_completer_word_break_characters = strdup(break_chars);
289 Py_INCREF(Py_None);
290 return Py_None;
291}
292
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293PyDoc_STRVAR(doc_set_completer_delims,
294"set_completer_delims(string) -> None\n\
295set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000296
Guido van Rossum74f31432003-01-07 20:01:29 +0000297
298/* Add a line to the history buffer */
299
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000300static PyObject *
301py_add_history(PyObject *self, PyObject *args)
302{
303 char *line;
304
305 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
306 return NULL;
307 }
308 add_history(line);
309 Py_INCREF(Py_None);
310 return Py_None;
311}
312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000313PyDoc_STRVAR(doc_add_history,
314"add_history(string) -> None\n\
315add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000316
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000317
Guido van Rossum74f31432003-01-07 20:01:29 +0000318/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000319
320static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000321get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000322{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000323 return PyString_FromString(rl_completer_word_break_characters);
324}
Guido van Rossum74f31432003-01-07 20:01:29 +0000325
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000326PyDoc_STRVAR(doc_get_completer_delims,
327"get_completer_delims() -> string\n\
328get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000329
Guido van Rossum74f31432003-01-07 20:01:29 +0000330
331/* Set the completer function */
332
Guido van Rossum290900a1997-09-26 21:51:21 +0000333static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000334set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000335{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000336 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000337}
338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000339PyDoc_STRVAR(doc_set_completer,
340"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000341Set or remove the completer function.\n\
342The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000343for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000344It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000345
Guido van Rossum74f31432003-01-07 20:01:29 +0000346
Michael W. Hudson796df152003-01-30 10:12:51 +0000347static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000348get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000349{
350 if (completer == NULL) {
351 Py_INCREF(Py_None);
352 return Py_None;
353 }
354 Py_INCREF(completer);
355 return completer;
356}
357
358PyDoc_STRVAR(doc_get_completer,
359"get_completer() -> function\n\
360\n\
361Returns current completer function.");
362
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000363/* Exported function to get any element of history */
364
365static PyObject *
366get_history_item(PyObject *self, PyObject *args)
367{
368 int idx = 0;
369 HIST_ENTRY *hist_ent;
370
371 if (!PyArg_ParseTuple(args, "i:index", &idx))
372 return NULL;
373 if ((hist_ent = history_get(idx)))
Guido van Rossum05ac4492003-01-07 20:04:12 +0000374 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000375 else {
376 Py_INCREF(Py_None);
377 return Py_None;
378 }
379}
380
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000381PyDoc_STRVAR(doc_get_history_item,
382"get_history_item() -> string\n\
383return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000384
Guido van Rossum74f31432003-01-07 20:01:29 +0000385
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000386/* Exported function to get current length of history */
387
388static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000389get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000390{
391 HISTORY_STATE *hist_st;
392
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000393 hist_st = history_get_history_state();
394 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
395}
396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000397PyDoc_STRVAR(doc_get_current_history_length,
398"get_current_history_length() -> integer\n\
399return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000400
Guido van Rossum74f31432003-01-07 20:01:29 +0000401
Guido van Rossum79378ff1997-10-07 14:53:21 +0000402/* Exported function to read the current line buffer */
403
404static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000405get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000406{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000407 return PyString_FromString(rl_line_buffer);
408}
409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000410PyDoc_STRVAR(doc_get_line_buffer,
411"get_line_buffer() -> string\n\
412return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000413
Guido van Rossum74f31432003-01-07 20:01:29 +0000414
Martin v. Löwise7a97962003-09-20 16:08:33 +0000415#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
416
417/* Exported function to clear the current history */
418
419static PyObject *
420py_clear_history(PyObject *self, PyObject *noarg)
421{
422 clear_history();
423 Py_INCREF(Py_None);
424 return Py_None;
425}
426
427PyDoc_STRVAR(doc_clear_history,
428"clear_history() -> None\n\
429Clear the current readline history.");
430#endif
431
432
Guido van Rossum79378ff1997-10-07 14:53:21 +0000433/* Exported function to insert text into the line buffer */
434
435static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000436insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000437{
438 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000439 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000440 return NULL;
441 rl_insert_text(s);
442 Py_INCREF(Py_None);
443 return Py_None;
444}
445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000446PyDoc_STRVAR(doc_insert_text,
447"insert_text(string) -> None\n\
448Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000449
Guido van Rossum74f31432003-01-07 20:01:29 +0000450
451/* Redisplay the line buffer */
452
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000453static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000454redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000455{
456 rl_redisplay();
457 Py_INCREF(Py_None);
458 return Py_None;
459}
460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000461PyDoc_STRVAR(doc_redisplay,
462"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000463Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000464contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000465
Guido van Rossum74f31432003-01-07 20:01:29 +0000466
Guido van Rossum290900a1997-09-26 21:51:21 +0000467/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000468
469static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000470{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000471 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000472 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000473 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000474 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000475 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000476 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000477 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000478 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000479 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000480 {"get_history_item", get_history_item,
481 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000482 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000483 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000484 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000485 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000486 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000487 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000488 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000489 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000490 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
491 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000492
Guido van Rossum74f31432003-01-07 20:01:29 +0000493 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000494 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000495 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000496 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000497 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000498
499 {"set_startup_hook", set_startup_hook,
500 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000501#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000502 {"set_pre_input_hook", set_pre_input_hook,
503 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000504#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000505#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
506 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
507#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000508 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000509};
510
Guido van Rossum05ac4492003-01-07 20:04:12 +0000511
Martin v. Löwis0daad592001-09-30 21:09:59 +0000512/* C function to call the Python hooks. */
513
514static int
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000515on_hook(PyObject *func, PyThreadState **tstate)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000516{
517 int result = 0;
518 if (func != NULL) {
519 PyObject *r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000520 /* Note that readline is called with the interpreter
521 lock released! */
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000522 PyEval_RestoreThread(*tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000523 r = PyObject_CallFunction(func, NULL);
524 if (r == NULL)
525 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000526 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000527 result = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000528 else
Martin v. Löwis0daad592001-09-30 21:09:59 +0000529 result = PyInt_AsLong(r);
530 Py_DECREF(r);
531 goto done;
532 error:
533 PyErr_Clear();
534 Py_XDECREF(r);
535 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000536 *tstate = PyEval_SaveThread();
Martin v. Löwis0daad592001-09-30 21:09:59 +0000537 }
538 return result;
539}
540
541static int
542on_startup_hook(void)
543{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000544 return on_hook(startup_hook, &startup_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000545}
546
547#ifdef HAVE_RL_PRE_INPUT_HOOK
548static int
549on_pre_input_hook(void)
550{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000551 return on_hook(pre_input_hook, &pre_input_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000552}
553#endif
554
Guido van Rossum05ac4492003-01-07 20:04:12 +0000555
Guido van Rossum290900a1997-09-26 21:51:21 +0000556/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000557
Guido van Rossum290900a1997-09-26 21:51:21 +0000558static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000559on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000560{
Guido van Rossum290900a1997-09-26 21:51:21 +0000561 char *result = NULL;
562 if (completer != NULL) {
563 PyObject *r;
564 /* Note that readline is called with the interpreter
565 lock released! */
Martin v. Löwis0daad592001-09-30 21:09:59 +0000566 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000567 /* Don't use the default filename completion if we
568 * have a custom completion function... */
569 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000570 r = PyObject_CallFunction(completer, "si", text, state);
571 if (r == NULL)
572 goto error;
573 if (r == Py_None) {
574 result = NULL;
575 }
576 else {
577 char *s = PyString_AsString(r);
578 if (s == NULL)
579 goto error;
580 result = strdup(s);
581 }
582 Py_DECREF(r);
583 goto done;
584 error:
585 PyErr_Clear();
586 Py_XDECREF(r);
587 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000588 completer_tstate = PyEval_SaveThread();
Guido van Rossum290900a1997-09-26 21:51:21 +0000589 }
590 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000591}
592
Guido van Rossum290900a1997-09-26 21:51:21 +0000593
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000594/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000595 * before calling the normal completer */
596
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000597static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000598flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000599{
600 Py_XDECREF(begidx);
601 Py_XDECREF(endidx);
602 begidx = PyInt_FromLong((long) start);
603 endidx = PyInt_FromLong((long) end);
604 return completion_matches(text, *on_completion);
605}
606
Guido van Rossum05ac4492003-01-07 20:04:12 +0000607
Guido van Rossum290900a1997-09-26 21:51:21 +0000608/* Helper to initialize GNU readline properly. */
609
610static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000611setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000612{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000613#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000614 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000615#endif
616
Skip Montanaroa0392742002-06-11 14:32:46 +0000617 using_history();
618
Guido van Rossum290900a1997-09-26 21:51:21 +0000619 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000620#if defined(PYOS_OS2) && defined(PYCC_GCC)
621 /* Allow $if term= in .inputrc to work */
622 rl_terminal_name = getenv("TERM");
623#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000624 /* Force rebind of TAB to insert-tab */
625 rl_bind_key('\t', rl_insert);
626 /* Bind both ESC-TAB and ESC-ESC to the completion function */
627 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
628 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000629 /* Set our hook functions */
630 rl_startup_hook = (Function *)on_startup_hook;
631#ifdef HAVE_RL_PRE_INPUT_HOOK
632 rl_pre_input_hook = (Function *)on_pre_input_hook;
633#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000634 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000635 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000636 /* Set Python word break characters */
637 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000638 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000639 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000640#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000641 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000642#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000643
644 begidx = PyInt_FromLong(0L);
645 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000646 /* Initialize (allows .inputrc to override)
647 *
648 * XXX: A bug in the readline-2.2 library causes a memory leak
649 * inside this function. Nothing we can do about it.
650 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000651 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000652
653#ifdef SAVE_LOCALE
654 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000655 free(saved_locale);
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000656#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000657}
658
659
660/* Interrupt handler */
661
662static jmp_buf jbuf;
663
Guido van Rossum0969d361997-08-05 21:27:50 +0000664/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000665static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000666onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000667{
Guido van Rossum290900a1997-09-26 21:51:21 +0000668 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000669}
670
Guido van Rossum290900a1997-09-26 21:51:21 +0000671
672/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000673
674static char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000675call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000676{
Guido van Rossum26418a92000-06-28 21:30:31 +0000677 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000678 char *p, *q;
Guido van Rossum174efc92000-09-16 16:37:53 +0000679 PyOS_sighandler_t old_inthandler;
Guido van Rossum74f31432003-01-07 20:01:29 +0000680
Guido van Rossum174efc92000-09-16 16:37:53 +0000681 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000682 if (setjmp(jbuf)) {
683#ifdef HAVE_SIGRELSE
684 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
685 sigrelse(SIGINT);
686#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000687 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossum0969d361997-08-05 21:27:50 +0000688 return NULL;
689 }
Guido van Rossum44620641997-08-11 18:57:29 +0000690 rl_event_hook = PyOS_InputHook;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000691
Guido van Rossum74f31432003-01-07 20:01:29 +0000692 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
693 rl_instream = sys_stdin;
694 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000695#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000696 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000697#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000698 }
699
Guido van Rossum0969d361997-08-05 21:27:50 +0000700 p = readline(prompt);
Guido van Rossum174efc92000-09-16 16:37:53 +0000701 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000702
703 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000704 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000705 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000706 if (p != NULL)
707 *p = '\0';
708 return p;
709 }
710 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000711 if (n > 0) {
712 char *line;
713 HISTORY_STATE *state = history_get_history_state();
714 if (state->length > 0)
715 line = history_get(state->length)->line;
716 else
717 line = "";
718 if (strcmp(p, line))
719 add_history(p);
720 /* the history docs don't say so, but the address of state
721 changes each time history_get_history_state is called
722 which makes me think it's freshly malloc'd memory...
723 on the other hand, the address of the last line stays the
724 same as long as history isn't extended, so it appears to
725 be malloc'd but managed by the history package... */
726 free(state);
727 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000728 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
729 release the original. */
730 q = p;
731 p = PyMem_Malloc(n+2);
732 if (p != NULL) {
733 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000734 p[n] = '\n';
735 p[n+1] = '\0';
736 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000737 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000738 return p;
739}
740
Guido van Rossum290900a1997-09-26 21:51:21 +0000741
742/* Initialize the module */
743
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000744PyDoc_STRVAR(doc_module,
745"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000746
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000747PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000748initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000749{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000750 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000751
752 m = Py_InitModule4("readline", readline_methods, doc_module,
753 (PyObject *)NULL, PYTHON_API_VERSION);
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000754
Guido van Rossum74f31432003-01-07 20:01:29 +0000755 PyOS_ReadlineFunctionPointer = call_readline;
756 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000757}