blob: 64935c6efef7684c28938cfb45c13d50ee3bf506 [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
Guido van Rossum79378ff1997-10-07 14:53:21 +0000415/* Exported function to insert text into the line buffer */
416
417static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000418insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000419{
420 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000421 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000422 return NULL;
423 rl_insert_text(s);
424 Py_INCREF(Py_None);
425 return Py_None;
426}
427
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000428PyDoc_STRVAR(doc_insert_text,
429"insert_text(string) -> None\n\
430Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000431
Guido van Rossum74f31432003-01-07 20:01:29 +0000432
433/* Redisplay the line buffer */
434
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000435static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000436redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000437{
438 rl_redisplay();
439 Py_INCREF(Py_None);
440 return Py_None;
441}
442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000443PyDoc_STRVAR(doc_redisplay,
444"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000445Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000446contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000447
Guido van Rossum74f31432003-01-07 20:01:29 +0000448
Guido van Rossum290900a1997-09-26 21:51:21 +0000449/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000450
451static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000452{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000453 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000454 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000455 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000456 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000457 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000458 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000459 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000460 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000461 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000462 {"get_history_item", get_history_item,
463 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000464 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000465 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000466 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000467 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000468 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000469 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000470 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000471 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000472 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
473 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000474
Guido van Rossum74f31432003-01-07 20:01:29 +0000475 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000476 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000477 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000478 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000479 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000480
481 {"set_startup_hook", set_startup_hook,
482 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000483#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000484 {"set_pre_input_hook", set_pre_input_hook,
485 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000486#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000487 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000488};
489
Guido van Rossum05ac4492003-01-07 20:04:12 +0000490
Martin v. Löwis0daad592001-09-30 21:09:59 +0000491/* C function to call the Python hooks. */
492
493static int
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000494on_hook(PyObject *func, PyThreadState **tstate)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000495{
496 int result = 0;
497 if (func != NULL) {
498 PyObject *r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000499 /* Note that readline is called with the interpreter
500 lock released! */
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000501 PyEval_RestoreThread(*tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000502 r = PyObject_CallFunction(func, NULL);
503 if (r == NULL)
504 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000505 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000506 result = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000507 else
Martin v. Löwis0daad592001-09-30 21:09:59 +0000508 result = PyInt_AsLong(r);
509 Py_DECREF(r);
510 goto done;
511 error:
512 PyErr_Clear();
513 Py_XDECREF(r);
514 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000515 *tstate = PyEval_SaveThread();
Martin v. Löwis0daad592001-09-30 21:09:59 +0000516 }
517 return result;
518}
519
520static int
521on_startup_hook(void)
522{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000523 return on_hook(startup_hook, &startup_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000524}
525
526#ifdef HAVE_RL_PRE_INPUT_HOOK
527static int
528on_pre_input_hook(void)
529{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000530 return on_hook(pre_input_hook, &pre_input_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000531}
532#endif
533
Guido van Rossum05ac4492003-01-07 20:04:12 +0000534
Guido van Rossum290900a1997-09-26 21:51:21 +0000535/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000536
Guido van Rossum290900a1997-09-26 21:51:21 +0000537static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000538on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000539{
Guido van Rossum290900a1997-09-26 21:51:21 +0000540 char *result = NULL;
541 if (completer != NULL) {
542 PyObject *r;
543 /* Note that readline is called with the interpreter
544 lock released! */
Martin v. Löwis0daad592001-09-30 21:09:59 +0000545 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000546 /* Don't use the default filename completion if we
547 * have a custom completion function... */
548 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000549 r = PyObject_CallFunction(completer, "si", text, state);
550 if (r == NULL)
551 goto error;
552 if (r == Py_None) {
553 result = NULL;
554 }
555 else {
556 char *s = PyString_AsString(r);
557 if (s == NULL)
558 goto error;
559 result = strdup(s);
560 }
561 Py_DECREF(r);
562 goto done;
563 error:
564 PyErr_Clear();
565 Py_XDECREF(r);
566 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000567 completer_tstate = PyEval_SaveThread();
Guido van Rossum290900a1997-09-26 21:51:21 +0000568 }
569 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000570}
571
Guido van Rossum290900a1997-09-26 21:51:21 +0000572
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000573/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000574 * before calling the normal completer */
575
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000576static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000577flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000578{
579 Py_XDECREF(begidx);
580 Py_XDECREF(endidx);
581 begidx = PyInt_FromLong((long) start);
582 endidx = PyInt_FromLong((long) end);
583 return completion_matches(text, *on_completion);
584}
585
Guido van Rossum05ac4492003-01-07 20:04:12 +0000586
Guido van Rossum290900a1997-09-26 21:51:21 +0000587/* Helper to initialize GNU readline properly. */
588
589static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000590setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000591{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000592#ifdef SAVE_LOCALE
593 char *saved_locale = setlocale(LC_CTYPE, NULL);
594#endif
595
Skip Montanaroa0392742002-06-11 14:32:46 +0000596 using_history();
597
Guido van Rossum290900a1997-09-26 21:51:21 +0000598 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000599#if defined(PYOS_OS2) && defined(PYCC_GCC)
600 /* Allow $if term= in .inputrc to work */
601 rl_terminal_name = getenv("TERM");
602#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000603 /* Force rebind of TAB to insert-tab */
604 rl_bind_key('\t', rl_insert);
605 /* Bind both ESC-TAB and ESC-ESC to the completion function */
606 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
607 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000608 /* Set our hook functions */
609 rl_startup_hook = (Function *)on_startup_hook;
610#ifdef HAVE_RL_PRE_INPUT_HOOK
611 rl_pre_input_hook = (Function *)on_pre_input_hook;
612#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000613 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000614 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000615 /* Set Python word break characters */
616 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000617 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000618 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000619#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000620 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000621#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000622
623 begidx = PyInt_FromLong(0L);
624 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000625 /* Initialize (allows .inputrc to override)
626 *
627 * XXX: A bug in the readline-2.2 library causes a memory leak
628 * inside this function. Nothing we can do about it.
629 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000630 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000631
632#ifdef SAVE_LOCALE
633 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
634#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000635}
636
637
638/* Interrupt handler */
639
640static jmp_buf jbuf;
641
Guido van Rossum0969d361997-08-05 21:27:50 +0000642/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000643static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000644onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000645{
Guido van Rossum290900a1997-09-26 21:51:21 +0000646 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000647}
648
Guido van Rossum290900a1997-09-26 21:51:21 +0000649
650/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000651
652static char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000653call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000654{
Guido van Rossum26418a92000-06-28 21:30:31 +0000655 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000656 char *p, *q;
Guido van Rossum174efc92000-09-16 16:37:53 +0000657 PyOS_sighandler_t old_inthandler;
Guido van Rossum74f31432003-01-07 20:01:29 +0000658
Guido van Rossum174efc92000-09-16 16:37:53 +0000659 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000660 if (setjmp(jbuf)) {
661#ifdef HAVE_SIGRELSE
662 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
663 sigrelse(SIGINT);
664#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000665 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossum0969d361997-08-05 21:27:50 +0000666 return NULL;
667 }
Guido van Rossum44620641997-08-11 18:57:29 +0000668 rl_event_hook = PyOS_InputHook;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000669
Guido van Rossum74f31432003-01-07 20:01:29 +0000670 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
671 rl_instream = sys_stdin;
672 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000673#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000674 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000675#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000676 }
677
Guido van Rossum0969d361997-08-05 21:27:50 +0000678 p = readline(prompt);
Guido van Rossum174efc92000-09-16 16:37:53 +0000679 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000680
681 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000682 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000683 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000684 if (p != NULL)
685 *p = '\0';
686 return p;
687 }
688 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000689 if (n > 0) {
690 char *line;
691 HISTORY_STATE *state = history_get_history_state();
692 if (state->length > 0)
693 line = history_get(state->length)->line;
694 else
695 line = "";
696 if (strcmp(p, line))
697 add_history(p);
698 /* the history docs don't say so, but the address of state
699 changes each time history_get_history_state is called
700 which makes me think it's freshly malloc'd memory...
701 on the other hand, the address of the last line stays the
702 same as long as history isn't extended, so it appears to
703 be malloc'd but managed by the history package... */
704 free(state);
705 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000706 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
707 release the original. */
708 q = p;
709 p = PyMem_Malloc(n+2);
710 if (p != NULL) {
711 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000712 p[n] = '\n';
713 p[n+1] = '\0';
714 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000715 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000716 return p;
717}
718
Guido van Rossum290900a1997-09-26 21:51:21 +0000719
720/* Initialize the module */
721
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000722PyDoc_STRVAR(doc_module,
723"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000724
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000725PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000726initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000727{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000728 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000729
730 m = Py_InitModule4("readline", readline_methods, doc_module,
731 (PyObject *)NULL, PYTHON_API_VERSION);
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000732
Guido van Rossum74f31432003-01-07 20:01:29 +0000733 PyOS_ReadlineFunctionPointer = call_readline;
734 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000735}