blob: 2ae996c0765d4c717a9c839cb99037d502e99daa [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 Rossum290900a1997-09-26 21:51:21 +000034/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum5a530192001-01-10 21:03:32 +000035extern DL_IMPORT(int) (*PyOS_InputHook)(void);
Martin v. Löwis566f6af2002-10-26 14:39:10 +000036extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *,char *);
Guido van Rossum0969d361997-08-05 21:27:50 +000037
Guido van Rossum0969d361997-08-05 21:27:50 +000038
Guido van Rossum290900a1997-09-26 21:51:21 +000039/* Exported function to send one line to readline's init file parser */
40
41static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000042parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000043{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000044 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000045 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000046 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000047 /* Make a copy -- rl_parse_and_bind() modifies its argument */
48 /* Bernard Herzog */
49 copy = malloc(1 + strlen(s));
50 if (copy == NULL)
51 return PyErr_NoMemory();
52 strcpy(copy, s);
53 rl_parse_and_bind(copy);
54 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000055 Py_INCREF(Py_None);
56 return Py_None;
57}
58
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000059PyDoc_STRVAR(doc_parse_and_bind,
60"parse_and_bind(string) -> None\n\
61Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000062
63
64/* Exported function to parse a readline init file */
65
66static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000067read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000068{
69 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000070 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000071 return NULL;
72 errno = rl_read_init_file(s);
73 if (errno)
74 return PyErr_SetFromErrno(PyExc_IOError);
75 Py_INCREF(Py_None);
76 return Py_None;
77}
78
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000079PyDoc_STRVAR(doc_read_init_file,
80"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000081Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000082The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000083
84
Skip Montanaro28067822000-07-06 18:55:12 +000085/* Exported function to load a readline history file */
86
87static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000088read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000089{
90 char *s = NULL;
91 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
92 return NULL;
93 errno = read_history(s);
94 if (errno)
95 return PyErr_SetFromErrno(PyExc_IOError);
96 Py_INCREF(Py_None);
97 return Py_None;
98}
99
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000100static int history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000101PyDoc_STRVAR(doc_read_history_file,
102"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000103Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000105
106
107/* Exported function to save a readline history file */
108
109static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000110write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000111{
112 char *s = NULL;
113 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
114 return NULL;
115 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000116 if (!errno && history_length >= 0)
117 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000118 if (errno)
119 return PyErr_SetFromErrno(PyExc_IOError);
120 Py_INCREF(Py_None);
121 return Py_None;
122}
123
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000124PyDoc_STRVAR(doc_write_history_file,
125"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000126Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000128
129
Guido van Rossum74f31432003-01-07 20:01:29 +0000130/* Set history length */
131
132static PyObject*
133set_history_length(PyObject *self, PyObject *args)
134{
135 int length = history_length;
136 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
137 return NULL;
138 history_length = length;
139 Py_INCREF(Py_None);
140 return Py_None;
141}
142
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000143PyDoc_STRVAR(set_history_length_doc,
144"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000145set the maximal number of items which will be written to\n\
146the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000147history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000148
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000149
Guido van Rossum74f31432003-01-07 20:01:29 +0000150/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151
152static PyObject*
153get_history_length(PyObject *self, PyObject *args)
154{
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000155 if (!PyArg_ParseTuple(args, ":get_history_length"))
156 return NULL;
157 return Py_BuildValue("i", history_length);
158}
159
Guido van Rossum74f31432003-01-07 20:01:29 +0000160PyDoc_STRVAR(get_history_length_doc,
161"get_history_length() -> int\n\
162return the maximum number of items that will be written to\n\
163the history file.");
164
165
Martin v. Löwis0daad592001-09-30 21:09:59 +0000166/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000167
Martin v. Löwis0daad592001-09-30 21:09:59 +0000168static PyObject *
Guido van Rossum74f31432003-01-07 20:01:29 +0000169set_hook(const char *funcname, PyObject **hook_var,
170 PyThreadState **tstate, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000171{
172 PyObject *function = Py_None;
173 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000174 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000175 if (!PyArg_ParseTuple(args, buf, &function))
176 return NULL;
177 if (function == Py_None) {
178 Py_XDECREF(*hook_var);
179 *hook_var = NULL;
180 *tstate = NULL;
181 }
182 else if (PyCallable_Check(function)) {
183 PyObject *tmp = *hook_var;
184 Py_INCREF(function);
185 *hook_var = function;
186 Py_XDECREF(tmp);
187 *tstate = PyThreadState_Get();
188 }
189 else {
Tim Peters885d4572001-11-28 20:27:42 +0000190 PyOS_snprintf(buf, sizeof(buf),
191 "set_%.50s(func): argument not callable",
192 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000193 PyErr_SetString(PyExc_TypeError, buf);
194 return NULL;
195 }
196 Py_INCREF(Py_None);
197 return Py_None;
198}
199
Guido van Rossum74f31432003-01-07 20:01:29 +0000200
Martin v. Löwis0daad592001-09-30 21:09:59 +0000201/* Exported functions to specify hook functions in Python */
202
203static PyObject *startup_hook = NULL;
204static PyThreadState *startup_hook_tstate = NULL;
205
206#ifdef HAVE_RL_PRE_INPUT_HOOK
207static PyObject *pre_input_hook = NULL;
208static PyThreadState *pre_input_hook_tstate = NULL;
209#endif
210
211static PyObject *
212set_startup_hook(PyObject *self, PyObject *args)
213{
Guido van Rossum74f31432003-01-07 20:01:29 +0000214 return set_hook("startup_hook", &startup_hook,
215 &startup_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000216}
217
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000218PyDoc_STRVAR(doc_set_startup_hook,
219"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000220Set or remove the startup_hook function.\n\
221The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000222before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000223
Guido van Rossum74f31432003-01-07 20:01:29 +0000224
Martin v. Löwis0daad592001-09-30 21:09:59 +0000225#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000226
227/* Set pre-input hook */
228
Martin v. Löwis0daad592001-09-30 21:09:59 +0000229static PyObject *
230set_pre_input_hook(PyObject *self, PyObject *args)
231{
Guido van Rossum74f31432003-01-07 20:01:29 +0000232 return set_hook("pre_input_hook", &pre_input_hook,
233 &pre_input_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000234}
235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000236PyDoc_STRVAR(doc_set_pre_input_hook,
237"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000238Set or remove the pre_input_hook function.\n\
239The function is called with no arguments after the first prompt\n\
240has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000241characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000242
Martin v. Löwis0daad592001-09-30 21:09:59 +0000243#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000244
Guido van Rossum74f31432003-01-07 20:01:29 +0000245
Guido van Rossum290900a1997-09-26 21:51:21 +0000246/* Exported function to specify a word completer in Python */
247
248static PyObject *completer = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000249static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000250
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000251static PyObject *begidx = NULL;
252static PyObject *endidx = NULL;
253
Guido van Rossum74f31432003-01-07 20:01:29 +0000254
255/* Get the beginning index for the scope of the tab-completion */
256
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000257static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000258get_begidx(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000259{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000260 Py_INCREF(begidx);
261 return begidx;
262}
263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000264PyDoc_STRVAR(doc_get_begidx,
265"get_begidx() -> int\n\
266get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000267
Guido van Rossum74f31432003-01-07 20:01:29 +0000268
269/* Get the ending index for the scope of the tab-completion */
270
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000271static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000272get_endidx(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000273{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000274 Py_INCREF(endidx);
275 return endidx;
276}
277
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000278PyDoc_STRVAR(doc_get_endidx,
279"get_endidx() -> int\n\
280get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000281
282
Guido van Rossum74f31432003-01-07 20:01:29 +0000283/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000284
285static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000286set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000287{
288 char *break_chars;
289
Guido van Rossum43713e52000-02-29 13:59:29 +0000290 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000291 return NULL;
292 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000293 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000294 rl_completer_word_break_characters = strdup(break_chars);
295 Py_INCREF(Py_None);
296 return Py_None;
297}
298
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000299PyDoc_STRVAR(doc_set_completer_delims,
300"set_completer_delims(string) -> None\n\
301set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000302
Guido van Rossum74f31432003-01-07 20:01:29 +0000303
304/* Add a line to the history buffer */
305
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000306static PyObject *
307py_add_history(PyObject *self, PyObject *args)
308{
309 char *line;
310
311 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
312 return NULL;
313 }
314 add_history(line);
315 Py_INCREF(Py_None);
316 return Py_None;
317}
318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000319PyDoc_STRVAR(doc_add_history,
320"add_history(string) -> None\n\
321add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000322
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000323
Guido van Rossum74f31432003-01-07 20:01:29 +0000324/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000325
326static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000327get_completer_delims(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000328{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000329 return PyString_FromString(rl_completer_word_break_characters);
330}
Guido van Rossum74f31432003-01-07 20:01:29 +0000331
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000332PyDoc_STRVAR(doc_get_completer_delims,
333"get_completer_delims() -> string\n\
334get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000335
Guido van Rossum74f31432003-01-07 20:01:29 +0000336
337/* Set the completer function */
338
Guido van Rossum290900a1997-09-26 21:51:21 +0000339static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000340set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000341{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000342 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000343}
344
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000345PyDoc_STRVAR(doc_set_completer,
346"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000347Set or remove the completer function.\n\
348The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000349for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000350It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000351
Guido van Rossum74f31432003-01-07 20:01:29 +0000352
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000353/* Exported function to get any element of history */
354
355static PyObject *
356get_history_item(PyObject *self, PyObject *args)
357{
358 int idx = 0;
359 HIST_ENTRY *hist_ent;
360
361 if (!PyArg_ParseTuple(args, "i:index", &idx))
362 return NULL;
363 if ((hist_ent = history_get(idx)))
Guido van Rossum05ac4492003-01-07 20:04:12 +0000364 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000365 else {
366 Py_INCREF(Py_None);
367 return Py_None;
368 }
369}
370
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000371PyDoc_STRVAR(doc_get_history_item,
372"get_history_item() -> string\n\
373return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000374
Guido van Rossum74f31432003-01-07 20:01:29 +0000375
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000376/* Exported function to get current length of history */
377
378static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000379get_current_history_length(PyObject *self)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000380{
381 HISTORY_STATE *hist_st;
382
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000383 hist_st = history_get_history_state();
384 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
385}
386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387PyDoc_STRVAR(doc_get_current_history_length,
388"get_current_history_length() -> integer\n\
389return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000390
Guido van Rossum74f31432003-01-07 20:01:29 +0000391
Guido van Rossum79378ff1997-10-07 14:53:21 +0000392/* Exported function to read the current line buffer */
393
394static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000395get_line_buffer(PyObject *self)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000396{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000397 return PyString_FromString(rl_line_buffer);
398}
399
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000400PyDoc_STRVAR(doc_get_line_buffer,
401"get_line_buffer() -> string\n\
402return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000403
Guido van Rossum74f31432003-01-07 20:01:29 +0000404
Guido van Rossum79378ff1997-10-07 14:53:21 +0000405/* Exported function to insert text into the line buffer */
406
407static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000408insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000409{
410 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000411 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000412 return NULL;
413 rl_insert_text(s);
414 Py_INCREF(Py_None);
415 return Py_None;
416}
417
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000418PyDoc_STRVAR(doc_insert_text,
419"insert_text(string) -> None\n\
420Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000421
Guido van Rossum74f31432003-01-07 20:01:29 +0000422
423/* Redisplay the line buffer */
424
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000425static PyObject *
426redisplay(PyObject *self)
427{
428 rl_redisplay();
429 Py_INCREF(Py_None);
430 return Py_None;
431}
432
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000433PyDoc_STRVAR(doc_redisplay,
434"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000435Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000436contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000437
Guido van Rossum74f31432003-01-07 20:01:29 +0000438
Guido van Rossum290900a1997-09-26 21:51:21 +0000439/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000440
441static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000442{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000443 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Guido van Rossum74f31432003-01-07 20:01:29 +0000444 {"get_line_buffer", (PyCFunction)get_line_buffer,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000445 METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000446 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000447 {"redisplay", (PyCFunction)redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000448 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000449 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000450 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000451 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000452 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000453 {"get_history_item", get_history_item,
454 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000455 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000456 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000457 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000458 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000459 {"get_history_length", get_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000460 METH_VARARGS, get_history_length_doc},
461 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Neal Norwitz767f8352002-03-31 16:13:39 +0000462 {"get_begidx", (PyCFunction)get_begidx, METH_NOARGS, doc_get_begidx},
463 {"get_endidx", (PyCFunction)get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000464
Guido van Rossum74f31432003-01-07 20:01:29 +0000465 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000466 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000467 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Guido van Rossum74f31432003-01-07 20:01:29 +0000468 {"get_completer_delims", (PyCFunction)get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000469 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000470
471 {"set_startup_hook", set_startup_hook,
472 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000473#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000474 {"set_pre_input_hook", set_pre_input_hook,
475 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000476#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000477 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000478};
479
Guido van Rossum05ac4492003-01-07 20:04:12 +0000480
Martin v. Löwis0daad592001-09-30 21:09:59 +0000481/* C function to call the Python hooks. */
482
483static int
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000484on_hook(PyObject *func, PyThreadState **tstate)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000485{
486 int result = 0;
487 if (func != NULL) {
488 PyObject *r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000489 /* Note that readline is called with the interpreter
490 lock released! */
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000491 PyEval_RestoreThread(*tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000492 r = PyObject_CallFunction(func, NULL);
493 if (r == NULL)
494 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000495 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000496 result = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000497 else
Martin v. Löwis0daad592001-09-30 21:09:59 +0000498 result = PyInt_AsLong(r);
499 Py_DECREF(r);
500 goto done;
501 error:
502 PyErr_Clear();
503 Py_XDECREF(r);
504 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000505 *tstate = PyEval_SaveThread();
Martin v. Löwis0daad592001-09-30 21:09:59 +0000506 }
507 return result;
508}
509
510static int
511on_startup_hook(void)
512{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000513 return on_hook(startup_hook, &startup_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000514}
515
516#ifdef HAVE_RL_PRE_INPUT_HOOK
517static int
518on_pre_input_hook(void)
519{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000520 return on_hook(pre_input_hook, &pre_input_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000521}
522#endif
523
Guido van Rossum05ac4492003-01-07 20:04:12 +0000524
Guido van Rossum290900a1997-09-26 21:51:21 +0000525/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000526
Guido van Rossum290900a1997-09-26 21:51:21 +0000527static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000528on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000529{
Guido van Rossum290900a1997-09-26 21:51:21 +0000530 char *result = NULL;
531 if (completer != NULL) {
532 PyObject *r;
533 /* Note that readline is called with the interpreter
534 lock released! */
Martin v. Löwis0daad592001-09-30 21:09:59 +0000535 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000536 /* Don't use the default filename completion if we
537 * have a custom completion function... */
538 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000539 r = PyObject_CallFunction(completer, "si", text, state);
540 if (r == NULL)
541 goto error;
542 if (r == Py_None) {
543 result = NULL;
544 }
545 else {
546 char *s = PyString_AsString(r);
547 if (s == NULL)
548 goto error;
549 result = strdup(s);
550 }
551 Py_DECREF(r);
552 goto done;
553 error:
554 PyErr_Clear();
555 Py_XDECREF(r);
556 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000557 completer_tstate = PyEval_SaveThread();
Guido van Rossum290900a1997-09-26 21:51:21 +0000558 }
559 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000560}
561
Guido van Rossum290900a1997-09-26 21:51:21 +0000562
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000563/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000564 * before calling the normal completer */
565
566char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000567flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000568{
569 Py_XDECREF(begidx);
570 Py_XDECREF(endidx);
571 begidx = PyInt_FromLong((long) start);
572 endidx = PyInt_FromLong((long) end);
573 return completion_matches(text, *on_completion);
574}
575
Guido van Rossum05ac4492003-01-07 20:04:12 +0000576
Guido van Rossum290900a1997-09-26 21:51:21 +0000577/* Helper to initialize GNU readline properly. */
578
579static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000580setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000581{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000582#ifdef SAVE_LOCALE
583 char *saved_locale = setlocale(LC_CTYPE, NULL);
584#endif
585
Skip Montanaroa0392742002-06-11 14:32:46 +0000586 using_history();
587
Guido van Rossum290900a1997-09-26 21:51:21 +0000588 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000589#if defined(PYOS_OS2) && defined(PYCC_GCC)
590 /* Allow $if term= in .inputrc to work */
591 rl_terminal_name = getenv("TERM");
592#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000593 /* Force rebind of TAB to insert-tab */
594 rl_bind_key('\t', rl_insert);
595 /* Bind both ESC-TAB and ESC-ESC to the completion function */
596 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
597 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000598 /* Set our hook functions */
599 rl_startup_hook = (Function *)on_startup_hook;
600#ifdef HAVE_RL_PRE_INPUT_HOOK
601 rl_pre_input_hook = (Function *)on_pre_input_hook;
602#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000603 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000604 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000605 /* Set Python word break characters */
606 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000607 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000608 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000609#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000610 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000611#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000612
613 begidx = PyInt_FromLong(0L);
614 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000615 /* Initialize (allows .inputrc to override)
616 *
617 * XXX: A bug in the readline-2.2 library causes a memory leak
618 * inside this function. Nothing we can do about it.
619 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000620 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000621
622#ifdef SAVE_LOCALE
623 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
624#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000625}
626
627
628/* Interrupt handler */
629
630static jmp_buf jbuf;
631
Guido van Rossum0969d361997-08-05 21:27:50 +0000632/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000633static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000634onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000635{
Guido van Rossum290900a1997-09-26 21:51:21 +0000636 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000637}
638
Guido van Rossum290900a1997-09-26 21:51:21 +0000639
640/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000641
642static char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000643call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000644{
Guido van Rossum26418a92000-06-28 21:30:31 +0000645 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000646 char *p, *q;
Guido van Rossum174efc92000-09-16 16:37:53 +0000647 PyOS_sighandler_t old_inthandler;
Guido van Rossum74f31432003-01-07 20:01:29 +0000648
Guido van Rossum174efc92000-09-16 16:37:53 +0000649 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000650 if (setjmp(jbuf)) {
651#ifdef HAVE_SIGRELSE
652 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
653 sigrelse(SIGINT);
654#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000655 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossum0969d361997-08-05 21:27:50 +0000656 return NULL;
657 }
Guido van Rossum44620641997-08-11 18:57:29 +0000658 rl_event_hook = PyOS_InputHook;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000659
Guido van Rossum74f31432003-01-07 20:01:29 +0000660 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
661 rl_instream = sys_stdin;
662 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000663#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000664 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000665#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000666 }
667
Guido van Rossum0969d361997-08-05 21:27:50 +0000668 p = readline(prompt);
Guido van Rossum174efc92000-09-16 16:37:53 +0000669 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000670
671 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000672 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000673 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000674 if (p != NULL)
675 *p = '\0';
676 return p;
677 }
678 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000679 if (n > 0) {
680 char *line;
681 HISTORY_STATE *state = history_get_history_state();
682 if (state->length > 0)
683 line = history_get(state->length)->line;
684 else
685 line = "";
686 if (strcmp(p, line))
687 add_history(p);
688 /* the history docs don't say so, but the address of state
689 changes each time history_get_history_state is called
690 which makes me think it's freshly malloc'd memory...
691 on the other hand, the address of the last line stays the
692 same as long as history isn't extended, so it appears to
693 be malloc'd but managed by the history package... */
694 free(state);
695 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000696 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
697 release the original. */
698 q = p;
699 p = PyMem_Malloc(n+2);
700 if (p != NULL) {
701 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000702 p[n] = '\n';
703 p[n+1] = '\0';
704 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000705 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000706 return p;
707}
708
Guido van Rossum290900a1997-09-26 21:51:21 +0000709
710/* Initialize the module */
711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000712PyDoc_STRVAR(doc_module,
713"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000714
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000715PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000716initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000717{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000718 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000719
720 m = Py_InitModule4("readline", readline_methods, doc_module,
721 (PyObject *)NULL, PYTHON_API_VERSION);
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000722
Guido van Rossum74f31432003-01-07 20:01:29 +0000723 PyOS_ReadlineFunctionPointer = call_readline;
724 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000725}