blob: 705d766b9704c87cfea3340b2638a1d95f8fbe29 [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
Martin v. Löwisb37509b2008-11-04 20:45:29 +000038#if defined(_RL_FUNCTION_TYPEDEF)
Guido van Rossumd8faa362007-04-27 19:54:29 +000039extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisb37509b2008-11-04 20:45:29 +000040#else
41extern char **completion_matches(char *, CPFunction *);
42#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000043#endif
44
Ronald Oussoren2efd9242009-09-20 14:53:22 +000045#ifdef __APPLE__
46/*
47 * It is possible to link the readline module to the readline
48 * emulation library of editline/libedit.
49 *
50 * On OSX this emulation library is not 100% API compatible
51 * with the "real" readline and cannot be detected at compile-time,
52 * hence we use a runtime check to detect if we're using libedit
53 *
54 * Currently there is one know API incompatibility:
55 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
56 * index with libedit's emulation.
57 * - Note that replace_history and remove_history use a 0-based index
58 * with both implementation.
59 */
60static int using_libedit_emulation = 0;
61static const char libedit_version_tag[] = "EditLine wrapper";
62#endif /* __APPLE__ */
63
Christian Heimes32fbe592007-11-12 15:01:33 +000064static void
65on_completion_display_matches_hook(char **matches,
66 int num_matches, int max_length);
67
Guido van Rossum0969d361997-08-05 21:27:50 +000068
Guido van Rossum290900a1997-09-26 21:51:21 +000069/* Exported function to send one line to readline's init file parser */
70
71static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000072parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000073{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000074 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000075 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000076 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000077 /* Make a copy -- rl_parse_and_bind() modifies its argument */
78 /* Bernard Herzog */
79 copy = malloc(1 + strlen(s));
80 if (copy == NULL)
81 return PyErr_NoMemory();
82 strcpy(copy, s);
83 rl_parse_and_bind(copy);
84 free(copy); /* Free the copy */
Guido van Rossum3d392eb2007-11-16 00:35:22 +000085 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000086}
87
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000088PyDoc_STRVAR(doc_parse_and_bind,
89"parse_and_bind(string) -> None\n\
90Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000091
92
93/* Exported function to parse a readline init file */
94
95static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000096read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000097{
98 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000099 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +0000100 return NULL;
101 errno = rl_read_init_file(s);
102 if (errno)
103 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000104 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000105}
106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000107PyDoc_STRVAR(doc_read_init_file,
108"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000109Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000110The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000111
112
Skip Montanaro28067822000-07-06 18:55:12 +0000113/* Exported function to load a readline history file */
114
115static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000116read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000117{
118 char *s = NULL;
119 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
120 return NULL;
121 errno = read_history(s);
122 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
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000127static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000128PyDoc_STRVAR(doc_read_history_file,
129"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000130Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000131The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000132
133
134/* Exported function to save a readline history file */
135
136static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000137write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000138{
139 char *s = NULL;
140 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
141 return NULL;
142 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000143 if (!errno && _history_length >= 0)
144 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000145 if (errno)
146 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000147 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000148}
149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000150PyDoc_STRVAR(doc_write_history_file,
151"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000152Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000154
155
Guido van Rossum74f31432003-01-07 20:01:29 +0000156/* Set history length */
157
158static PyObject*
159set_history_length(PyObject *self, PyObject *args)
160{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000161 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000162 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
163 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000164 _history_length = length;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000165 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000166}
167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000168PyDoc_STRVAR(set_history_length_doc,
169"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000170set the maximal number of items which will be written to\n\
171the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000172history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000173
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000174
Guido van Rossum74f31432003-01-07 20:01:29 +0000175/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000176
177static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000178get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000179{
Christian Heimes217cfd12007-12-02 14:31:20 +0000180 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000181}
182
Guido van Rossum74f31432003-01-07 20:01:29 +0000183PyDoc_STRVAR(get_history_length_doc,
184"get_history_length() -> int\n\
185return the maximum number of items that will be written to\n\
186the history file.");
187
188
Martin v. Löwis0daad592001-09-30 21:09:59 +0000189/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000190
Martin v. Löwis0daad592001-09-30 21:09:59 +0000191static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000192set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000193{
194 PyObject *function = Py_None;
195 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000196 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000197 if (!PyArg_ParseTuple(args, buf, &function))
198 return NULL;
199 if (function == Py_None) {
200 Py_XDECREF(*hook_var);
201 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000202 }
203 else if (PyCallable_Check(function)) {
204 PyObject *tmp = *hook_var;
205 Py_INCREF(function);
206 *hook_var = function;
207 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000208 }
209 else {
Tim Peters885d4572001-11-28 20:27:42 +0000210 PyOS_snprintf(buf, sizeof(buf),
211 "set_%.50s(func): argument not callable",
212 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000213 PyErr_SetString(PyExc_TypeError, buf);
214 return NULL;
215 }
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000216 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000217}
218
Guido van Rossum74f31432003-01-07 20:01:29 +0000219
Martin v. Löwis0daad592001-09-30 21:09:59 +0000220/* Exported functions to specify hook functions in Python */
221
Thomas Wouters89d996e2007-09-08 17:39:28 +0000222static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000223static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000224
225#ifdef HAVE_RL_PRE_INPUT_HOOK
226static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000227#endif
228
229static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000230set_completion_display_matches_hook(PyObject *self, PyObject *args)
231{
Christian Heimes32fbe592007-11-12 15:01:33 +0000232 PyObject *result = set_hook("completion_display_matches_hook",
Thomas Wouters89d996e2007-09-08 17:39:28 +0000233 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000234#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
235 /* We cannot set this hook globally, since it replaces the
236 default completion display. */
237 rl_completion_display_matches_hook =
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000238 completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000239#if defined(_RL_FUNCTION_TYPEDEF)
Christian Heimes32fbe592007-11-12 15:01:33 +0000240 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000241#else
242 (VFunction *)on_completion_display_matches_hook : 0;
243#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000244#endif
245 return result;
246
Thomas Wouters89d996e2007-09-08 17:39:28 +0000247}
248
249PyDoc_STRVAR(doc_set_completion_display_matches_hook,
250"set_completion_display_matches_hook([function]) -> None\n\
251Set or remove the completion display function.\n\
252The function is called as\n\
253 function(substitution, [matches], longest_match_length)\n\
254once each time matches need to be displayed.");
255
256static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000257set_startup_hook(PyObject *self, PyObject *args)
258{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000259 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000260}
261
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000262PyDoc_STRVAR(doc_set_startup_hook,
263"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000264Set or remove the startup_hook function.\n\
265The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000266before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000267
Guido van Rossum74f31432003-01-07 20:01:29 +0000268
Martin v. Löwis0daad592001-09-30 21:09:59 +0000269#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000270
271/* Set pre-input hook */
272
Martin v. Löwis0daad592001-09-30 21:09:59 +0000273static PyObject *
274set_pre_input_hook(PyObject *self, PyObject *args)
275{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000276 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000277}
278
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000279PyDoc_STRVAR(doc_set_pre_input_hook,
280"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000281Set or remove the pre_input_hook function.\n\
282The function is called with no arguments after the first prompt\n\
283has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000284characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000285
Martin v. Löwis0daad592001-09-30 21:09:59 +0000286#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000287
Guido van Rossum74f31432003-01-07 20:01:29 +0000288
Guido van Rossum290900a1997-09-26 21:51:21 +0000289/* Exported function to specify a word completer in Python */
290
291static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000292
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000293static PyObject *begidx = NULL;
294static PyObject *endidx = NULL;
295
Guido van Rossum74f31432003-01-07 20:01:29 +0000296
Thomas Wouters89d996e2007-09-08 17:39:28 +0000297/* Get the completion type for the scope of the tab-completion */
298static PyObject *
299get_completion_type(PyObject *self, PyObject *noarg)
300{
Christian Heimes217cfd12007-12-02 14:31:20 +0000301 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000302}
303
304PyDoc_STRVAR(doc_get_completion_type,
305"get_completion_type() -> int\n\
306Get the type of completion being attempted.");
307
308
Guido van Rossum74f31432003-01-07 20:01:29 +0000309/* Get the beginning index for the scope of the tab-completion */
310
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000311static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000312get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000313{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000314 Py_INCREF(begidx);
315 return begidx;
316}
317
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000318PyDoc_STRVAR(doc_get_begidx,
319"get_begidx() -> int\n\
320get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000321
Guido van Rossum74f31432003-01-07 20:01:29 +0000322
323/* Get the ending index for the scope of the tab-completion */
324
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000325static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000326get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000327{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000328 Py_INCREF(endidx);
329 return endidx;
330}
331
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000332PyDoc_STRVAR(doc_get_endidx,
333"get_endidx() -> int\n\
334get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000335
336
Guido van Rossum74f31432003-01-07 20:01:29 +0000337/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000338
339static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000340set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000341{
342 char *break_chars;
343
Guido van Rossum43713e52000-02-29 13:59:29 +0000344 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000345 return NULL;
346 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000347 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000348 rl_completer_word_break_characters = strdup(break_chars);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000349 Py_RETURN_NONE;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000350}
351
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000352PyDoc_STRVAR(doc_set_completer_delims,
353"set_completer_delims(string) -> None\n\
354set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000355
Skip Montanaroe5069012004-08-15 14:32:06 +0000356static PyObject *
357py_remove_history(PyObject *self, PyObject *args)
358{
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000359 int entry_number;
360 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000361
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000362 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
363 return NULL;
364 if (entry_number < 0) {
365 PyErr_SetString(PyExc_ValueError,
366 "History index cannot be negative");
367 return NULL;
368 }
369 entry = remove_history(entry_number);
370 if (!entry) {
371 PyErr_Format(PyExc_ValueError,
372 "No history item at position %d",
373 entry_number);
374 return NULL;
375 }
376 /* free memory allocated for the history entry */
377 if (entry->line)
378 free(entry->line);
379 if (entry->data)
380 free(entry->data);
381 free(entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000382
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000383 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000384}
385
386PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000387"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000388remove history item given by its position");
389
390static PyObject *
391py_replace_history(PyObject *self, PyObject *args)
392{
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000393 int entry_number;
394 char *line;
395 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000396
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000397 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
398 &line)) {
399 return NULL;
400 }
401 if (entry_number < 0) {
402 PyErr_SetString(PyExc_ValueError,
403 "History index cannot be negative");
404 return NULL;
405 }
406 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
407 if (!old_entry) {
408 PyErr_Format(PyExc_ValueError,
409 "No history item at position %d",
410 entry_number);
411 return NULL;
412 }
413 /* free memory allocated for the old history entry */
414 if (old_entry->line)
415 free(old_entry->line);
416 if (old_entry->data)
417 free(old_entry->data);
418 free(old_entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000419
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000420 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000421}
422
423PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000424"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000425replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000426
427/* Add a line to the history buffer */
428
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000429static PyObject *
430py_add_history(PyObject *self, PyObject *args)
431{
432 char *line;
433
434 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
435 return NULL;
436 }
437 add_history(line);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000438 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000439}
440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000441PyDoc_STRVAR(doc_add_history,
442"add_history(string) -> None\n\
443add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000444
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000445
Guido van Rossum74f31432003-01-07 20:01:29 +0000446/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000447
448static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000449get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000450{
Christian Heimesaec75c32007-11-11 22:42:36 +0000451 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000452}
Guido van Rossum74f31432003-01-07 20:01:29 +0000453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000454PyDoc_STRVAR(doc_get_completer_delims,
455"get_completer_delims() -> string\n\
456get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000457
Guido van Rossum74f31432003-01-07 20:01:29 +0000458
459/* Set the completer function */
460
Guido van Rossum290900a1997-09-26 21:51:21 +0000461static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000462set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000463{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000464 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000465}
466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000467PyDoc_STRVAR(doc_set_completer,
468"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000469Set or remove the completer function.\n\
470The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000471for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000472It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000473
Guido van Rossum74f31432003-01-07 20:01:29 +0000474
Michael W. Hudson796df152003-01-30 10:12:51 +0000475static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000476get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000477{
478 if (completer == NULL) {
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000479 Py_RETURN_NONE;
Michael W. Hudson796df152003-01-30 10:12:51 +0000480 }
481 Py_INCREF(completer);
482 return completer;
483}
484
485PyDoc_STRVAR(doc_get_completer,
486"get_completer() -> function\n\
487\n\
488Returns current completer function.");
489
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000490/* Exported function to get any element of history */
491
492static PyObject *
493get_history_item(PyObject *self, PyObject *args)
494{
495 int idx = 0;
496 HIST_ENTRY *hist_ent;
497
498 if (!PyArg_ParseTuple(args, "i:index", &idx))
499 return NULL;
Ronald Oussoren2efd9242009-09-20 14:53:22 +0000500#ifdef __APPLE__
501 if (using_libedit_emulation) {
502 /* Libedit emulation uses 0-based indexes,
503 * the real one uses 1-based indexes,
504 * adjust the index to ensure that Python
505 * code doesn't have to worry about the
506 * difference.
507 */
508 HISTORY_STATE *hist_st;
509 hist_st = history_get_history_state();
510
511 idx --;
512
513 /*
514 * Apple's readline emulation crashes when
515 * the index is out of range, therefore
516 * test for that and fail gracefully.
517 */
518 if (idx < 0 || idx >= hist_st->length) {
519 Py_RETURN_NONE;
520 }
521 }
522#endif /* __APPLE__ */
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000523 if ((hist_ent = history_get(idx)))
Christian Heimesaec75c32007-11-11 22:42:36 +0000524 return PyUnicode_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000525 else {
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000526 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000527 }
528}
529
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000530PyDoc_STRVAR(doc_get_history_item,
531"get_history_item() -> string\n\
532return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000533
Guido van Rossum74f31432003-01-07 20:01:29 +0000534
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000535/* Exported function to get current length of history */
536
537static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000538get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000539{
540 HISTORY_STATE *hist_st;
541
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000542 hist_st = history_get_history_state();
Christian Heimes217cfd12007-12-02 14:31:20 +0000543 return PyLong_FromLong(hist_st ? (long) hist_st->length : (long) 0);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000544}
545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000546PyDoc_STRVAR(doc_get_current_history_length,
547"get_current_history_length() -> integer\n\
548return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000549
Guido van Rossum74f31432003-01-07 20:01:29 +0000550
Guido van Rossum79378ff1997-10-07 14:53:21 +0000551/* Exported function to read the current line buffer */
552
553static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000554get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000555{
Christian Heimesaec75c32007-11-11 22:42:36 +0000556 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000557}
558
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000559PyDoc_STRVAR(doc_get_line_buffer,
560"get_line_buffer() -> string\n\
561return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000562
Guido van Rossum74f31432003-01-07 20:01:29 +0000563
Martin v. Löwise7a97962003-09-20 16:08:33 +0000564#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
565
566/* Exported function to clear the current history */
567
568static PyObject *
569py_clear_history(PyObject *self, PyObject *noarg)
570{
571 clear_history();
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000572 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000573}
574
575PyDoc_STRVAR(doc_clear_history,
576"clear_history() -> None\n\
577Clear the current readline history.");
578#endif
579
580
Guido van Rossum79378ff1997-10-07 14:53:21 +0000581/* Exported function to insert text into the line buffer */
582
583static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000584insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000585{
586 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000587 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000588 return NULL;
589 rl_insert_text(s);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000590 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000591}
592
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000593PyDoc_STRVAR(doc_insert_text,
594"insert_text(string) -> None\n\
595Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000596
Guido van Rossum74f31432003-01-07 20:01:29 +0000597
598/* Redisplay the line buffer */
599
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000600static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000601redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000602{
603 rl_redisplay();
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000604 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000605}
606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000607PyDoc_STRVAR(doc_redisplay,
608"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000609Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000610contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000611
Guido van Rossum74f31432003-01-07 20:01:29 +0000612
Guido van Rossum290900a1997-09-26 21:51:21 +0000613/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000614
615static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000616{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000617 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000618 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000619 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000620 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000621 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000622 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000623 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000624 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000625 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000626 {"get_history_item", get_history_item,
627 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000628 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000629 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000630 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000631 METH_VARARGS, set_history_length_doc},
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000632 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000633 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000634 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000635 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Thomas Wouters89d996e2007-09-08 17:39:28 +0000636 {"get_completion_type", get_completion_type,
637 METH_NOARGS, doc_get_completion_type},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000638 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
639 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000640
Guido van Rossum74f31432003-01-07 20:01:29 +0000641 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000642 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000643 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000644 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
645 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000646 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000647 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000648
Thomas Wouters89d996e2007-09-08 17:39:28 +0000649 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
650 METH_VARARGS, doc_set_completion_display_matches_hook},
Guido van Rossum74f31432003-01-07 20:01:29 +0000651 {"set_startup_hook", set_startup_hook,
652 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000653#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000654 {"set_pre_input_hook", set_pre_input_hook,
655 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000656#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000657#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
658 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
659#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000660 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000661};
662
Guido van Rossum05ac4492003-01-07 20:04:12 +0000663
Martin v. Löwis0daad592001-09-30 21:09:59 +0000664/* C function to call the Python hooks. */
665
666static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000667on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000668{
669 int result = 0;
670 if (func != NULL) {
671 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000672#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000673 PyGILState_STATE gilstate = PyGILState_Ensure();
674#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000675 r = PyObject_CallFunction(func, NULL);
676 if (r == NULL)
677 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000678 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000679 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000680 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000681 result = PyLong_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000682 if (result == -1 && PyErr_Occurred())
683 goto error;
684 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000685 Py_DECREF(r);
686 goto done;
687 error:
688 PyErr_Clear();
689 Py_XDECREF(r);
690 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000691#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000692 PyGILState_Release(gilstate);
693#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000694 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000695 }
696 return result;
697}
698
699static int
700on_startup_hook(void)
701{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000702 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000703}
704
705#ifdef HAVE_RL_PRE_INPUT_HOOK
706static int
707on_pre_input_hook(void)
708{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000709 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000710}
711#endif
712
Guido van Rossum05ac4492003-01-07 20:04:12 +0000713
Thomas Wouters89d996e2007-09-08 17:39:28 +0000714/* C function to call the Python completion_display_matches */
715
716static void
717on_completion_display_matches_hook(char **matches,
718 int num_matches, int max_length)
719{
Christian Heimes32fbe592007-11-12 15:01:33 +0000720 int i;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000721 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000722#ifdef WITH_THREAD
Christian Heimes32fbe592007-11-12 15:01:33 +0000723 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000724#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000725 m = PyList_New(num_matches);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000726 if (m == NULL)
727 goto error;
Christian Heimes32fbe592007-11-12 15:01:33 +0000728 for (i = 0; i < num_matches; i++) {
729 s = PyUnicode_FromString(matches[i+1]);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000730 if (s == NULL)
Christian Heimes32fbe592007-11-12 15:01:33 +0000731 goto error;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000732 if (PyList_SetItem(m, i, s) == -1)
733 goto error;
Thomas Wouters89d996e2007-09-08 17:39:28 +0000734 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000735 r = PyObject_CallFunction(completion_display_matches_hook,
736 "sOi", matches[0], m, max_length);
737
Matthias Klose091c7b12009-04-07 13:24:27 +0000738 Py_DECREF(m); m=NULL;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000739
Christian Heimes32fbe592007-11-12 15:01:33 +0000740 if (r == NULL ||
Christian Heimes217cfd12007-12-02 14:31:20 +0000741 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
Christian Heimes32fbe592007-11-12 15:01:33 +0000742 goto error;
743 }
Matthias Klose091c7b12009-04-07 13:24:27 +0000744 Py_XDECREF(r); r=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000745
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000746 if (0) {
747 error:
748 PyErr_Clear();
749 Py_XDECREF(m);
750 Py_XDECREF(r);
751 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000752#ifdef WITH_THREAD
753 PyGILState_Release(gilstate);
754#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000755}
756
757
Guido van Rossum290900a1997-09-26 21:51:21 +0000758/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000759
Guido van Rossum290900a1997-09-26 21:51:21 +0000760static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000761on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000762{
Guido van Rossum290900a1997-09-26 21:51:21 +0000763 char *result = NULL;
764 if (completer != NULL) {
765 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000766#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000767 PyGILState_STATE gilstate = PyGILState_Ensure();
768#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000769 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000770 r = PyObject_CallFunction(completer, "si", text, state);
771 if (r == NULL)
772 goto error;
773 if (r == Py_None) {
774 result = NULL;
775 }
776 else {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000777 char *s = _PyUnicode_AsString(r);
Guido van Rossum290900a1997-09-26 21:51:21 +0000778 if (s == NULL)
779 goto error;
780 result = strdup(s);
781 }
782 Py_DECREF(r);
783 goto done;
784 error:
785 PyErr_Clear();
786 Py_XDECREF(r);
787 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000788#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000789 PyGILState_Release(gilstate);
790#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000791 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000792 }
793 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000794}
795
Guido van Rossum290900a1997-09-26 21:51:21 +0000796
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000797/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000798 * before calling the normal completer */
799
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000800static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000801flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000802{
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000803#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
804 rl_completion_append_character ='\0';
Antoine Pitroud5131772009-10-26 19:22:14 +0000805#endif
806#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitroudc0900b2009-10-19 18:22:37 +0000807 rl_completion_suppress_append = 0;
808#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000809 Py_XDECREF(begidx);
810 Py_XDECREF(endidx);
Christian Heimes217cfd12007-12-02 14:31:20 +0000811 begidx = PyLong_FromLong((long) start);
812 endidx = PyLong_FromLong((long) end);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000813 return completion_matches(text, *on_completion);
814}
815
Guido van Rossum05ac4492003-01-07 20:04:12 +0000816
Guido van Rossum290900a1997-09-26 21:51:21 +0000817/* Helper to initialize GNU readline properly. */
818
819static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000820setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000821{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000822#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000823 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000824 if (!saved_locale)
825 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000826#endif
827
Skip Montanaroa0392742002-06-11 14:32:46 +0000828 using_history();
829
Guido van Rossum290900a1997-09-26 21:51:21 +0000830 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000831#if defined(PYOS_OS2) && defined(PYCC_GCC)
832 /* Allow $if term= in .inputrc to work */
833 rl_terminal_name = getenv("TERM");
834#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000835 /* Force rebind of TAB to insert-tab */
836 rl_bind_key('\t', rl_insert);
837 /* Bind both ESC-TAB and ESC-ESC to the completion function */
838 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
839 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000840 /* Set our hook functions */
841 rl_startup_hook = (Function *)on_startup_hook;
842#ifdef HAVE_RL_PRE_INPUT_HOOK
843 rl_pre_input_hook = (Function *)on_pre_input_hook;
844#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000845 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000846 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000847 /* Set Python word break characters */
848 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000849 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000850 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000851
Christian Heimes217cfd12007-12-02 14:31:20 +0000852 begidx = PyLong_FromLong(0L);
853 endidx = PyLong_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000854 /* Initialize (allows .inputrc to override)
855 *
856 * XXX: A bug in the readline-2.2 library causes a memory leak
857 * inside this function. Nothing we can do about it.
858 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000859 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000860
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000861 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000862}
863
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000864/* Wrapper around GNU readline that handles signals differently. */
865
866
867#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
868
869static char *completed_input_string;
870static void
871rlhandler(char *text)
872{
873 completed_input_string = text;
874 rl_callback_handler_remove();
875}
876
877extern PyThreadState* _PyOS_ReadlineTState;
878
879static char *
880readline_until_enter_or_signal(char *prompt, int *signal)
881{
882 char * not_done_reading = "";
883 fd_set selectset;
884
885 *signal = 0;
886#ifdef HAVE_RL_CATCH_SIGNAL
887 rl_catch_signals = 0;
888#endif
889
890 rl_callback_handler_install (prompt, rlhandler);
891 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000892
893 completed_input_string = not_done_reading;
894
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000895 while (completed_input_string == not_done_reading) {
896 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000897
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000898 while (!has_input)
899 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000900
901 /* [Bug #1552726] Only limit the pause if an input hook has been
902 defined. */
903 struct timeval *timeoutp = NULL;
904 if (PyOS_InputHook)
905 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000906 FD_SET(fileno(rl_instream), &selectset);
907 /* select resets selectset if no input was available */
908 has_input = select(fileno(rl_instream) + 1, &selectset,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000909 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000910 if(PyOS_InputHook) PyOS_InputHook();
911 }
912
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000913 if(has_input > 0) {
914 rl_callback_read_char();
915 }
916 else if (errno == EINTR) {
917 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000918#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000919 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000920#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000921 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000922#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000923 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000924#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000925 if (s < 0) {
926 rl_free_line_state();
927 rl_cleanup_after_signal();
928 rl_callback_handler_remove();
929 *signal = 1;
930 completed_input_string = NULL;
931 }
932 }
933 }
934
935 return completed_input_string;
936}
937
938
939#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000940
941/* Interrupt handler */
942
943static jmp_buf jbuf;
944
Guido van Rossum0969d361997-08-05 21:27:50 +0000945/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000946static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000947onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000948{
Guido van Rossum290900a1997-09-26 21:51:21 +0000949 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000950}
951
Guido van Rossum290900a1997-09-26 21:51:21 +0000952
Guido van Rossum0969d361997-08-05 21:27:50 +0000953static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000954readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000955{
Guido van Rossum174efc92000-09-16 16:37:53 +0000956 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000957 char *p;
958
959 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000960
Guido van Rossum174efc92000-09-16 16:37:53 +0000961 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000962 if (setjmp(jbuf)) {
963#ifdef HAVE_SIGRELSE
964 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
965 sigrelse(SIGINT);
966#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000967 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000968 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000969 return NULL;
970 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000971 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000972 p = readline(prompt);
973 PyOS_setsig(SIGINT, old_inthandler);
974
975 return p;
976}
977#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
978
979
980static char *
981call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
982{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000983 size_t n;
984 char *p, *q;
985 int signal;
986
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000987#ifdef SAVE_LOCALE
988 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000989 if (!saved_locale)
990 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000991 setlocale(LC_CTYPE, "");
992#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000993
Guido van Rossum74f31432003-01-07 20:01:29 +0000994 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
995 rl_instream = sys_stdin;
996 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000997#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000998 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000999#endif
Guido van Rossum74f31432003-01-07 20:01:29 +00001000 }
1001
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001002 p = readline_until_enter_or_signal(prompt, &signal);
1003
1004 /* we got an interrupt signal */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001005 if (signal) {
1006 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001007 return NULL;
1008 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001009
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001010 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +00001011 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001012 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001013 if (p != NULL)
1014 *p = '\0';
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001015 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +00001016 return p;
1017 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001018
1019 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +00001020 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +00001021 if (n > 0) {
1022 char *line;
1023 HISTORY_STATE *state = history_get_history_state();
1024 if (state->length > 0)
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001025#ifdef __APPLE__
1026 if (using_libedit_emulation) {
1027 /*
1028 * Libedit's emulation uses 0-based indexes,
1029 * the real readline uses 1-based indexes.
1030 */
1031 line = history_get(state->length - 1)->line;
1032 } else
1033#endif /* __APPLE__ */
Skip Montanaroa0392742002-06-11 14:32:46 +00001034 line = history_get(state->length)->line;
1035 else
1036 line = "";
1037 if (strcmp(p, line))
1038 add_history(p);
1039 /* the history docs don't say so, but the address of state
1040 changes each time history_get_history_state is called
1041 which makes me think it's freshly malloc'd memory...
1042 on the other hand, the address of the last line stays the
1043 same as long as history isn't extended, so it appears to
1044 be malloc'd but managed by the history package... */
1045 free(state);
1046 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001047 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1048 release the original. */
1049 q = p;
1050 p = PyMem_Malloc(n+2);
1051 if (p != NULL) {
1052 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +00001053 p[n] = '\n';
1054 p[n+1] = '\0';
1055 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001056 free(q);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001057 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +00001058 return p;
1059}
1060
Guido van Rossum290900a1997-09-26 21:51:21 +00001061
1062/* Initialize the module */
1063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001064PyDoc_STRVAR(doc_module,
1065"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001066
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001067#ifdef __APPLE__
1068PyDoc_STRVAR(doc_module_le,
1069"Importing this module enables command line editing using libedit readline.");
1070#endif /* __APPLE__ */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001071
1072static struct PyModuleDef readlinemodule = {
1073 PyModuleDef_HEAD_INIT,
1074 "readline",
1075 doc_module,
1076 -1,
1077 readline_methods,
1078 NULL,
1079 NULL,
1080 NULL,
1081 NULL
1082};
1083
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001084
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001085PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001086PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001087{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +00001088 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001089
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001090#ifdef __APPLE__
1091 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1092 using_libedit_emulation = 1;
1093 }
1094
1095 if (using_libedit_emulation)
1096 readlinemodule.m_doc = doc_module_le;
1097
1098#endif /* __APPLE__ */
1099
Martin v. Löwis1a214512008-06-11 05:26:20 +00001100 m = PyModule_Create(&readlinemodule);
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001101
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001102 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001103 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001104
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001105
1106
Guido van Rossum74f31432003-01-07 20:01:29 +00001107 PyOS_ReadlineFunctionPointer = call_readline;
1108 setup_readline();
Martin v. Löwis1a214512008-06-11 05:26:20 +00001109 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001110}