blob: 837194208ff546a1b623fe2a187f4659ef45b1a7 [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
Michael W. Hudson9a8c3142005-03-30 10:09:12 +00003 * Center. The completer interface was inspired by Lele Gaifax. More
4 * recently, it was largely rewritten by Guido van Rossum.
Guido van Rossum0969d361997-08-05 21:27:50 +00005 */
6
Guido van Rossum290900a1997-09-26 21:51:21 +00007/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +00008#include "Python.h"
9#include <setjmp.h>
10#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000011#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000012#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000013
Skip Montanaro7befb992004-02-10 16:50:21 +000014#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000015/* GNU readline() mistakenly sets the LC_CTYPE locale.
16 * This is evil. Only the user or the app's main() should do this!
17 * We must save and restore the locale around the rl_initialize() call.
18 */
19#define SAVE_LOCALE
20#include <locale.h>
21#endif
22
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
26# define RESTORE_LOCALE(sl)
27#endif
28
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
36 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossumd8faa362007-04-27 19:54:29 +000037#else
38extern char **completion_matches(char *, rl_compentry_func_t *);
Guido van Rossum353ae582001-07-10 16:45:32 +000039#endif
40
Christian Heimes32fbe592007-11-12 15:01:33 +000041static void
42on_completion_display_matches_hook(char **matches,
43 int num_matches, int max_length);
44
Guido van Rossum0969d361997-08-05 21:27:50 +000045
Guido van Rossum290900a1997-09-26 21:51:21 +000046/* Exported function to send one line to readline's init file parser */
47
48static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000049parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000050{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000051 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000052 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000053 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000054 /* Make a copy -- rl_parse_and_bind() modifies its argument */
55 /* Bernard Herzog */
56 copy = malloc(1 + strlen(s));
57 if (copy == NULL)
58 return PyErr_NoMemory();
59 strcpy(copy, s);
60 rl_parse_and_bind(copy);
61 free(copy); /* Free the copy */
Guido van Rossum3d392eb2007-11-16 00:35:22 +000062 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000063}
64
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000065PyDoc_STRVAR(doc_parse_and_bind,
66"parse_and_bind(string) -> None\n\
67Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000068
69
70/* Exported function to parse a readline init file */
71
72static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000073read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000074{
75 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000076 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000077 return NULL;
78 errno = rl_read_init_file(s);
79 if (errno)
80 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3d392eb2007-11-16 00:35:22 +000081 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000082}
83
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084PyDoc_STRVAR(doc_read_init_file,
85"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000086Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000087The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000088
89
Skip Montanaro28067822000-07-06 18:55:12 +000090/* Exported function to load a readline history file */
91
92static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000093read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000094{
95 char *s = NULL;
96 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
97 return NULL;
98 errno = read_history(s);
99 if (errno)
100 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000101 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000102}
103
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000104static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000105PyDoc_STRVAR(doc_read_history_file,
106"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000107Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000108The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000109
110
111/* Exported function to save a readline history file */
112
113static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000114write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000115{
116 char *s = NULL;
117 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
118 return NULL;
119 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000120 if (!errno && _history_length >= 0)
121 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000122 if (errno)
123 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000124 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000125}
126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127PyDoc_STRVAR(doc_write_history_file,
128"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000129Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000130The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000131
132
Guido van Rossum74f31432003-01-07 20:01:29 +0000133/* Set history length */
134
135static PyObject*
136set_history_length(PyObject *self, PyObject *args)
137{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000138 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000139 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
140 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000141 _history_length = length;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000142 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000143}
144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000145PyDoc_STRVAR(set_history_length_doc,
146"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000147set the maximal number of items which will be written to\n\
148the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000149history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000150
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151
Guido van Rossum74f31432003-01-07 20:01:29 +0000152/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000153
154static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000155get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000156{
Christian Heimes217cfd12007-12-02 14:31:20 +0000157 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000158}
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 *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000169set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000170{
171 PyObject *function = Py_None;
172 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000173 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000174 if (!PyArg_ParseTuple(args, buf, &function))
175 return NULL;
176 if (function == Py_None) {
177 Py_XDECREF(*hook_var);
178 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000179 }
180 else if (PyCallable_Check(function)) {
181 PyObject *tmp = *hook_var;
182 Py_INCREF(function);
183 *hook_var = function;
184 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000185 }
186 else {
Tim Peters885d4572001-11-28 20:27:42 +0000187 PyOS_snprintf(buf, sizeof(buf),
188 "set_%.50s(func): argument not callable",
189 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000190 PyErr_SetString(PyExc_TypeError, buf);
191 return NULL;
192 }
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000193 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000194}
195
Guido van Rossum74f31432003-01-07 20:01:29 +0000196
Martin v. Löwis0daad592001-09-30 21:09:59 +0000197/* Exported functions to specify hook functions in Python */
198
Thomas Wouters89d996e2007-09-08 17:39:28 +0000199static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000200static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000201
202#ifdef HAVE_RL_PRE_INPUT_HOOK
203static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000204#endif
205
206static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000207set_completion_display_matches_hook(PyObject *self, PyObject *args)
208{
Christian Heimes32fbe592007-11-12 15:01:33 +0000209 PyObject *result = set_hook("completion_display_matches_hook",
Thomas Wouters89d996e2007-09-08 17:39:28 +0000210 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000211#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
212 /* We cannot set this hook globally, since it replaces the
213 default completion display. */
214 rl_completion_display_matches_hook =
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000215 completion_display_matches_hook ?
Christian Heimes32fbe592007-11-12 15:01:33 +0000216 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
217#endif
218 return result;
219
Thomas Wouters89d996e2007-09-08 17:39:28 +0000220}
221
222PyDoc_STRVAR(doc_set_completion_display_matches_hook,
223"set_completion_display_matches_hook([function]) -> None\n\
224Set or remove the completion display function.\n\
225The function is called as\n\
226 function(substitution, [matches], longest_match_length)\n\
227once each time matches need to be displayed.");
228
229static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000230set_startup_hook(PyObject *self, PyObject *args)
231{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000232 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000233}
234
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235PyDoc_STRVAR(doc_set_startup_hook,
236"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000237Set or remove the startup_hook function.\n\
238The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000240
Guido van Rossum74f31432003-01-07 20:01:29 +0000241
Martin v. Löwis0daad592001-09-30 21:09:59 +0000242#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000243
244/* Set pre-input hook */
245
Martin v. Löwis0daad592001-09-30 21:09:59 +0000246static PyObject *
247set_pre_input_hook(PyObject *self, PyObject *args)
248{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000249 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000250}
251
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000252PyDoc_STRVAR(doc_set_pre_input_hook,
253"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000254Set or remove the pre_input_hook function.\n\
255The function is called with no arguments after the first prompt\n\
256has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000258
Martin v. Löwis0daad592001-09-30 21:09:59 +0000259#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000260
Guido van Rossum74f31432003-01-07 20:01:29 +0000261
Guido van Rossum290900a1997-09-26 21:51:21 +0000262/* Exported function to specify a word completer in Python */
263
264static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000265
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000266static PyObject *begidx = NULL;
267static PyObject *endidx = NULL;
268
Guido van Rossum74f31432003-01-07 20:01:29 +0000269
Thomas Wouters89d996e2007-09-08 17:39:28 +0000270/* Get the completion type for the scope of the tab-completion */
271static PyObject *
272get_completion_type(PyObject *self, PyObject *noarg)
273{
Christian Heimes217cfd12007-12-02 14:31:20 +0000274 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000275}
276
277PyDoc_STRVAR(doc_get_completion_type,
278"get_completion_type() -> int\n\
279Get the type of completion being attempted.");
280
281
Guido van Rossum74f31432003-01-07 20:01:29 +0000282/* Get the beginning index for the scope of the tab-completion */
283
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000284static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000285get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000286{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000287 Py_INCREF(begidx);
288 return begidx;
289}
290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291PyDoc_STRVAR(doc_get_begidx,
292"get_begidx() -> int\n\
293get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000294
Guido van Rossum74f31432003-01-07 20:01:29 +0000295
296/* Get the ending index for the scope of the tab-completion */
297
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000298static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000299get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000300{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000301 Py_INCREF(endidx);
302 return endidx;
303}
304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000305PyDoc_STRVAR(doc_get_endidx,
306"get_endidx() -> int\n\
307get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000308
309
Guido van Rossum74f31432003-01-07 20:01:29 +0000310/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000311
312static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000313set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000314{
315 char *break_chars;
316
Guido van Rossum43713e52000-02-29 13:59:29 +0000317 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000318 return NULL;
319 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000320 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000321 rl_completer_word_break_characters = strdup(break_chars);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000322 Py_RETURN_NONE;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000323}
324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000325PyDoc_STRVAR(doc_set_completer_delims,
326"set_completer_delims(string) -> None\n\
327set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000328
Skip Montanaroe5069012004-08-15 14:32:06 +0000329static PyObject *
330py_remove_history(PyObject *self, PyObject *args)
331{
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000332 int entry_number;
333 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000334
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000335 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
336 return NULL;
337 if (entry_number < 0) {
338 PyErr_SetString(PyExc_ValueError,
339 "History index cannot be negative");
340 return NULL;
341 }
342 entry = remove_history(entry_number);
343 if (!entry) {
344 PyErr_Format(PyExc_ValueError,
345 "No history item at position %d",
346 entry_number);
347 return NULL;
348 }
349 /* free memory allocated for the history entry */
350 if (entry->line)
351 free(entry->line);
352 if (entry->data)
353 free(entry->data);
354 free(entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000355
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000356 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000357}
358
359PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000360"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000361remove history item given by its position");
362
363static PyObject *
364py_replace_history(PyObject *self, PyObject *args)
365{
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000366 int entry_number;
367 char *line;
368 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000369
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000370 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
371 &line)) {
372 return NULL;
373 }
374 if (entry_number < 0) {
375 PyErr_SetString(PyExc_ValueError,
376 "History index cannot be negative");
377 return NULL;
378 }
379 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
380 if (!old_entry) {
381 PyErr_Format(PyExc_ValueError,
382 "No history item at position %d",
383 entry_number);
384 return NULL;
385 }
386 /* free memory allocated for the old history entry */
387 if (old_entry->line)
388 free(old_entry->line);
389 if (old_entry->data)
390 free(old_entry->data);
391 free(old_entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000392
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000393 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000394}
395
396PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000397"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000398replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000399
400/* Add a line to the history buffer */
401
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000402static PyObject *
403py_add_history(PyObject *self, PyObject *args)
404{
405 char *line;
406
407 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
408 return NULL;
409 }
410 add_history(line);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000411 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000412}
413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000414PyDoc_STRVAR(doc_add_history,
415"add_history(string) -> None\n\
416add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000417
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000418
Guido van Rossum74f31432003-01-07 20:01:29 +0000419/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000420
421static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000422get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000423{
Christian Heimesaec75c32007-11-11 22:42:36 +0000424 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000425}
Guido van Rossum74f31432003-01-07 20:01:29 +0000426
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000427PyDoc_STRVAR(doc_get_completer_delims,
428"get_completer_delims() -> string\n\
429get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000430
Guido van Rossum74f31432003-01-07 20:01:29 +0000431
432/* Set the completer function */
433
Guido van Rossum290900a1997-09-26 21:51:21 +0000434static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000435set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000436{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000437 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000438}
439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000440PyDoc_STRVAR(doc_set_completer,
441"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000442Set or remove the completer function.\n\
443The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000444for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000445It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000446
Guido van Rossum74f31432003-01-07 20:01:29 +0000447
Michael W. Hudson796df152003-01-30 10:12:51 +0000448static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000449get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000450{
451 if (completer == NULL) {
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000452 Py_RETURN_NONE;
Michael W. Hudson796df152003-01-30 10:12:51 +0000453 }
454 Py_INCREF(completer);
455 return completer;
456}
457
458PyDoc_STRVAR(doc_get_completer,
459"get_completer() -> function\n\
460\n\
461Returns current completer function.");
462
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000463/* Exported function to get any element of history */
464
465static PyObject *
466get_history_item(PyObject *self, PyObject *args)
467{
468 int idx = 0;
469 HIST_ENTRY *hist_ent;
470
471 if (!PyArg_ParseTuple(args, "i:index", &idx))
472 return NULL;
473 if ((hist_ent = history_get(idx)))
Christian Heimesaec75c32007-11-11 22:42:36 +0000474 return PyUnicode_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000475 else {
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000476 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000477 }
478}
479
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000480PyDoc_STRVAR(doc_get_history_item,
481"get_history_item() -> string\n\
482return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000483
Guido van Rossum74f31432003-01-07 20:01:29 +0000484
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000485/* Exported function to get current length of history */
486
487static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000488get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000489{
490 HISTORY_STATE *hist_st;
491
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000492 hist_st = history_get_history_state();
Christian Heimes217cfd12007-12-02 14:31:20 +0000493 return PyLong_FromLong(hist_st ? (long) hist_st->length : (long) 0);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000494}
495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000496PyDoc_STRVAR(doc_get_current_history_length,
497"get_current_history_length() -> integer\n\
498return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000499
Guido van Rossum74f31432003-01-07 20:01:29 +0000500
Guido van Rossum79378ff1997-10-07 14:53:21 +0000501/* Exported function to read the current line buffer */
502
503static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000504get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000505{
Christian Heimesaec75c32007-11-11 22:42:36 +0000506 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000507}
508
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000509PyDoc_STRVAR(doc_get_line_buffer,
510"get_line_buffer() -> string\n\
511return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000512
Guido van Rossum74f31432003-01-07 20:01:29 +0000513
Martin v. Löwise7a97962003-09-20 16:08:33 +0000514#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
515
516/* Exported function to clear the current history */
517
518static PyObject *
519py_clear_history(PyObject *self, PyObject *noarg)
520{
521 clear_history();
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000522 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000523}
524
525PyDoc_STRVAR(doc_clear_history,
526"clear_history() -> None\n\
527Clear the current readline history.");
528#endif
529
530
Guido van Rossum79378ff1997-10-07 14:53:21 +0000531/* Exported function to insert text into the line buffer */
532
533static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000534insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000535{
536 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000537 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000538 return NULL;
539 rl_insert_text(s);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000540 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000541}
542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000543PyDoc_STRVAR(doc_insert_text,
544"insert_text(string) -> None\n\
545Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000546
Guido van Rossum74f31432003-01-07 20:01:29 +0000547
548/* Redisplay the line buffer */
549
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000550static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000551redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000552{
553 rl_redisplay();
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000554 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000555}
556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000557PyDoc_STRVAR(doc_redisplay,
558"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000559Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000560contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000561
Guido van Rossum74f31432003-01-07 20:01:29 +0000562
Guido van Rossum290900a1997-09-26 21:51:21 +0000563/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000564
565static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000566{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000567 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000568 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000569 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000570 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000571 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000572 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000573 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000574 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000575 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000576 {"get_history_item", get_history_item,
577 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000578 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000579 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000580 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000581 METH_VARARGS, set_history_length_doc},
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000582 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000583 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000584 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000585 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Thomas Wouters89d996e2007-09-08 17:39:28 +0000586 {"get_completion_type", get_completion_type,
587 METH_NOARGS, doc_get_completion_type},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000588 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
589 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000590
Guido van Rossum74f31432003-01-07 20:01:29 +0000591 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000592 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000593 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000594 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
595 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000596 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000597 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000598
Thomas Wouters89d996e2007-09-08 17:39:28 +0000599 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
600 METH_VARARGS, doc_set_completion_display_matches_hook},
Guido van Rossum74f31432003-01-07 20:01:29 +0000601 {"set_startup_hook", set_startup_hook,
602 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000603#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000604 {"set_pre_input_hook", set_pre_input_hook,
605 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000606#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000607#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
608 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
609#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000610 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000611};
612
Guido van Rossum05ac4492003-01-07 20:04:12 +0000613
Martin v. Löwis0daad592001-09-30 21:09:59 +0000614/* C function to call the Python hooks. */
615
616static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000617on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000618{
619 int result = 0;
620 if (func != NULL) {
621 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000622#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000623 PyGILState_STATE gilstate = PyGILState_Ensure();
624#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000625 r = PyObject_CallFunction(func, NULL);
626 if (r == NULL)
627 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000628 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000629 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000630 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000631 result = PyLong_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000632 if (result == -1 && PyErr_Occurred())
633 goto error;
634 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000635 Py_DECREF(r);
636 goto done;
637 error:
638 PyErr_Clear();
639 Py_XDECREF(r);
640 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000641#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000642 PyGILState_Release(gilstate);
643#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000644 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000645 }
646 return result;
647}
648
649static int
650on_startup_hook(void)
651{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000652 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000653}
654
655#ifdef HAVE_RL_PRE_INPUT_HOOK
656static int
657on_pre_input_hook(void)
658{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000659 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000660}
661#endif
662
Guido van Rossum05ac4492003-01-07 20:04:12 +0000663
Thomas Wouters89d996e2007-09-08 17:39:28 +0000664/* C function to call the Python completion_display_matches */
665
666static void
667on_completion_display_matches_hook(char **matches,
668 int num_matches, int max_length)
669{
Christian Heimes32fbe592007-11-12 15:01:33 +0000670 int i;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000671 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000672#ifdef WITH_THREAD
Christian Heimes32fbe592007-11-12 15:01:33 +0000673 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000674#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000675 m = PyList_New(num_matches);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000676 if (m == NULL)
677 goto error;
Christian Heimes32fbe592007-11-12 15:01:33 +0000678 for (i = 0; i < num_matches; i++) {
679 s = PyUnicode_FromString(matches[i+1]);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000680 if (s == NULL)
Christian Heimes32fbe592007-11-12 15:01:33 +0000681 goto error;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000682 if (PyList_SetItem(m, i, s) == -1)
683 goto error;
Thomas Wouters89d996e2007-09-08 17:39:28 +0000684 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000685 r = PyObject_CallFunction(completion_display_matches_hook,
686 "sOi", matches[0], m, max_length);
687
688 Py_DECREF(m), m=NULL;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000689
Christian Heimes32fbe592007-11-12 15:01:33 +0000690 if (r == NULL ||
Christian Heimes217cfd12007-12-02 14:31:20 +0000691 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
Christian Heimes32fbe592007-11-12 15:01:33 +0000692 goto error;
693 }
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000694 Py_XDECREF(r), r=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000695
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000696 if (0) {
697 error:
698 PyErr_Clear();
699 Py_XDECREF(m);
700 Py_XDECREF(r);
701 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000702#ifdef WITH_THREAD
703 PyGILState_Release(gilstate);
704#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000705}
706
707
Guido van Rossum290900a1997-09-26 21:51:21 +0000708/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000709
Guido van Rossum290900a1997-09-26 21:51:21 +0000710static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000711on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000712{
Guido van Rossum290900a1997-09-26 21:51:21 +0000713 char *result = NULL;
714 if (completer != NULL) {
715 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000716#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000717 PyGILState_STATE gilstate = PyGILState_Ensure();
718#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000719 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000720 r = PyObject_CallFunction(completer, "si", text, state);
721 if (r == NULL)
722 goto error;
723 if (r == Py_None) {
724 result = NULL;
725 }
726 else {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000727 char *s = _PyUnicode_AsString(r);
Guido van Rossum290900a1997-09-26 21:51:21 +0000728 if (s == NULL)
729 goto error;
730 result = strdup(s);
731 }
732 Py_DECREF(r);
733 goto done;
734 error:
735 PyErr_Clear();
736 Py_XDECREF(r);
737 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000738#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000739 PyGILState_Release(gilstate);
740#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000741 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000742 }
743 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000744}
745
Guido van Rossum290900a1997-09-26 21:51:21 +0000746
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000747/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000748 * before calling the normal completer */
749
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000750static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000751flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000752{
753 Py_XDECREF(begidx);
754 Py_XDECREF(endidx);
Christian Heimes217cfd12007-12-02 14:31:20 +0000755 begidx = PyLong_FromLong((long) start);
756 endidx = PyLong_FromLong((long) end);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000757 return completion_matches(text, *on_completion);
758}
759
Guido van Rossum05ac4492003-01-07 20:04:12 +0000760
Guido van Rossum290900a1997-09-26 21:51:21 +0000761/* Helper to initialize GNU readline properly. */
762
763static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000764setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000765{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000766#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000767 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000768 if (!saved_locale)
769 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000770#endif
771
Skip Montanaroa0392742002-06-11 14:32:46 +0000772 using_history();
773
Guido van Rossum290900a1997-09-26 21:51:21 +0000774 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000775#if defined(PYOS_OS2) && defined(PYCC_GCC)
776 /* Allow $if term= in .inputrc to work */
777 rl_terminal_name = getenv("TERM");
778#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000779 /* Force rebind of TAB to insert-tab */
780 rl_bind_key('\t', rl_insert);
781 /* Bind both ESC-TAB and ESC-ESC to the completion function */
782 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
783 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000784 /* Set our hook functions */
785 rl_startup_hook = (Function *)on_startup_hook;
786#ifdef HAVE_RL_PRE_INPUT_HOOK
787 rl_pre_input_hook = (Function *)on_pre_input_hook;
788#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000789 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000790 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000791 /* Set Python word break characters */
792 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000793 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000794 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000795#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000796 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000797#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000798
Christian Heimes217cfd12007-12-02 14:31:20 +0000799 begidx = PyLong_FromLong(0L);
800 endidx = PyLong_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000801 /* Initialize (allows .inputrc to override)
802 *
803 * XXX: A bug in the readline-2.2 library causes a memory leak
804 * inside this function. Nothing we can do about it.
805 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000806 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000807
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000808 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000809}
810
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000811/* Wrapper around GNU readline that handles signals differently. */
812
813
814#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
815
816static char *completed_input_string;
817static void
818rlhandler(char *text)
819{
820 completed_input_string = text;
821 rl_callback_handler_remove();
822}
823
824extern PyThreadState* _PyOS_ReadlineTState;
825
826static char *
827readline_until_enter_or_signal(char *prompt, int *signal)
828{
829 char * not_done_reading = "";
830 fd_set selectset;
831
832 *signal = 0;
833#ifdef HAVE_RL_CATCH_SIGNAL
834 rl_catch_signals = 0;
835#endif
836
837 rl_callback_handler_install (prompt, rlhandler);
838 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000839
840 completed_input_string = not_done_reading;
841
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000842 while (completed_input_string == not_done_reading) {
843 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000844
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000845 while (!has_input)
846 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000847
848 /* [Bug #1552726] Only limit the pause if an input hook has been
849 defined. */
850 struct timeval *timeoutp = NULL;
851 if (PyOS_InputHook)
852 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000853 FD_SET(fileno(rl_instream), &selectset);
854 /* select resets selectset if no input was available */
855 has_input = select(fileno(rl_instream) + 1, &selectset,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000856 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000857 if(PyOS_InputHook) PyOS_InputHook();
858 }
859
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000860 if(has_input > 0) {
861 rl_callback_read_char();
862 }
863 else if (errno == EINTR) {
864 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000865#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000866 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000867#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000868 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000869#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000870 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000871#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000872 if (s < 0) {
873 rl_free_line_state();
874 rl_cleanup_after_signal();
875 rl_callback_handler_remove();
876 *signal = 1;
877 completed_input_string = NULL;
878 }
879 }
880 }
881
882 return completed_input_string;
883}
884
885
886#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000887
888/* Interrupt handler */
889
890static jmp_buf jbuf;
891
Guido van Rossum0969d361997-08-05 21:27:50 +0000892/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000893static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000894onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000895{
Guido van Rossum290900a1997-09-26 21:51:21 +0000896 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000897}
898
Guido van Rossum290900a1997-09-26 21:51:21 +0000899
Guido van Rossum0969d361997-08-05 21:27:50 +0000900static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000901readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000902{
Guido van Rossum174efc92000-09-16 16:37:53 +0000903 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000904 char *p;
905
906 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000907
Guido van Rossum174efc92000-09-16 16:37:53 +0000908 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000909 if (setjmp(jbuf)) {
910#ifdef HAVE_SIGRELSE
911 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
912 sigrelse(SIGINT);
913#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000914 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000915 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000916 return NULL;
917 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000918 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000919 p = readline(prompt);
920 PyOS_setsig(SIGINT, old_inthandler);
921
922 return p;
923}
924#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
925
926
927static char *
928call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
929{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000930 size_t n;
931 char *p, *q;
932 int signal;
933
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000934#ifdef SAVE_LOCALE
935 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000936 if (!saved_locale)
937 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000938 setlocale(LC_CTYPE, "");
939#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000940
Guido van Rossum74f31432003-01-07 20:01:29 +0000941 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
942 rl_instream = sys_stdin;
943 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000944#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000945 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000946#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000947 }
948
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000949 p = readline_until_enter_or_signal(prompt, &signal);
950
951 /* we got an interrupt signal */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000952 if (signal) {
953 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000954 return NULL;
955 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000956
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000957 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000958 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000959 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000960 if (p != NULL)
961 *p = '\0';
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000962 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000963 return p;
964 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000965
966 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000967 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000968 if (n > 0) {
969 char *line;
970 HISTORY_STATE *state = history_get_history_state();
971 if (state->length > 0)
972 line = history_get(state->length)->line;
973 else
974 line = "";
975 if (strcmp(p, line))
976 add_history(p);
977 /* the history docs don't say so, but the address of state
978 changes each time history_get_history_state is called
979 which makes me think it's freshly malloc'd memory...
980 on the other hand, the address of the last line stays the
981 same as long as history isn't extended, so it appears to
982 be malloc'd but managed by the history package... */
983 free(state);
984 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000985 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
986 release the original. */
987 q = p;
988 p = PyMem_Malloc(n+2);
989 if (p != NULL) {
990 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000991 p[n] = '\n';
992 p[n+1] = '\0';
993 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000994 free(q);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000995 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000996 return p;
997}
998
Guido van Rossum290900a1997-09-26 21:51:21 +0000999
1000/* Initialize the module */
1001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001002PyDoc_STRVAR(doc_module,
1003"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001004
Martin v. Löwis1a214512008-06-11 05:26:20 +00001005
1006static struct PyModuleDef readlinemodule = {
1007 PyModuleDef_HEAD_INIT,
1008 "readline",
1009 doc_module,
1010 -1,
1011 readline_methods,
1012 NULL,
1013 NULL,
1014 NULL,
1015 NULL
1016};
1017
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001018PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001019PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001020{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +00001021 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001022
Martin v. Löwis1a214512008-06-11 05:26:20 +00001023 m = PyModule_Create(&readlinemodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001024 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001025 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001026
Guido van Rossum74f31432003-01-07 20:01:29 +00001027 PyOS_ReadlineFunctionPointer = call_readline;
1028 setup_readline();
Martin v. Löwis1a214512008-06-11 05:26:20 +00001029 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001030}