blob: bd256ced8524a3e8b62c23d7cd0dccd905f2b513 [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
Neal Norwitz5eaf7722006-07-16 02:15:27 +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)))
Neal Norwitzbd538702007-04-19 05:52:37 +000037#else
Martin v. Löwisbb86d832008-11-04 20:40:09 +000038#if defined(_RL_FUNCTION_TYPEDEF)
Neal Norwitzbd538702007-04-19 05:52:37 +000039extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisbb86d832008-11-04 20:40:09 +000040#else
Ronald Oussoren333fca92010-02-11 13:13:08 +000041
42#if !defined(__APPLE__)
Martin v. Löwisbb86d832008-11-04 20:40:09 +000043extern char **completion_matches(char *, CPFunction *);
44#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000045#endif
Ronald Oussoren333fca92010-02-11 13:13:08 +000046#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000047
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000048#ifdef __APPLE__
49/*
50 * It is possible to link the readline module to the readline
51 * emulation library of editline/libedit.
52 *
53 * On OSX this emulation library is not 100% API compatible
54 * with the "real" readline and cannot be detected at compile-time,
55 * hence we use a runtime check to detect if we're using libedit
56 *
57 * Currently there is one know API incompatibility:
58 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
59 * index with libedit's emulation.
60 * - Note that replace_history and remove_history use a 0-based index
61 * with both implementation.
62 */
63static int using_libedit_emulation = 0;
64static const char libedit_version_tag[] = "EditLine wrapper";
65#endif /* __APPLE__ */
66
Martin v. Löwisf3548942007-11-12 04:53:02 +000067static void
68on_completion_display_matches_hook(char **matches,
69 int num_matches, int max_length);
70
Guido van Rossum0969d361997-08-05 21:27:50 +000071
Guido van Rossum290900a1997-09-26 21:51:21 +000072/* Exported function to send one line to readline's init file parser */
73
74static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000075parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000076{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000077 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000078 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000079 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000080 /* Make a copy -- rl_parse_and_bind() modifies its argument */
81 /* Bernard Herzog */
82 copy = malloc(1 + strlen(s));
83 if (copy == NULL)
84 return PyErr_NoMemory();
85 strcpy(copy, s);
86 rl_parse_and_bind(copy);
87 free(copy); /* Free the copy */
Christian Heimes1bc4af42007-11-12 18:58:08 +000088 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000089}
90
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000091PyDoc_STRVAR(doc_parse_and_bind,
92"parse_and_bind(string) -> None\n\
93Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000094
95
96/* Exported function to parse a readline init file */
97
98static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000099read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000100{
101 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +0000102 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +0000103 return NULL;
104 errno = rl_read_init_file(s);
105 if (errno)
106 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000107 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000108}
109
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000110PyDoc_STRVAR(doc_read_init_file,
111"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000112Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000113The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000114
115
Skip Montanaro28067822000-07-06 18:55:12 +0000116/* Exported function to load a readline history file */
117
118static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000119read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000120{
121 char *s = NULL;
122 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
123 return NULL;
124 errno = read_history(s);
125 if (errno)
126 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000127 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000128}
129
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000130static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000131PyDoc_STRVAR(doc_read_history_file,
132"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000133Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000134The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000135
136
137/* Exported function to save a readline history file */
138
139static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000140write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000141{
142 char *s = NULL;
143 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
144 return NULL;
145 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000146 if (!errno && _history_length >= 0)
147 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000148 if (errno)
149 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000150 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000151}
152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153PyDoc_STRVAR(doc_write_history_file,
154"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000155Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000156The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000157
158
Guido van Rossum74f31432003-01-07 20:01:29 +0000159/* Set history length */
160
161static PyObject*
162set_history_length(PyObject *self, PyObject *args)
163{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000164 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000165 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
166 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000167 _history_length = length;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000168 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000169}
170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000171PyDoc_STRVAR(set_history_length_doc,
172"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000173set the maximal number of items which will be written to\n\
174the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000175history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000176
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000177
Guido van Rossum74f31432003-01-07 20:01:29 +0000178/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000179
180static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000181get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000182{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000183 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000184}
185
Guido van Rossum74f31432003-01-07 20:01:29 +0000186PyDoc_STRVAR(get_history_length_doc,
187"get_history_length() -> int\n\
188return the maximum number of items that will be written to\n\
189the history file.");
190
191
Martin v. Löwis0daad592001-09-30 21:09:59 +0000192/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000193
Martin v. Löwis0daad592001-09-30 21:09:59 +0000194static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000195set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000196{
197 PyObject *function = Py_None;
198 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000199 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000200 if (!PyArg_ParseTuple(args, buf, &function))
201 return NULL;
202 if (function == Py_None) {
203 Py_XDECREF(*hook_var);
204 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000205 }
206 else if (PyCallable_Check(function)) {
207 PyObject *tmp = *hook_var;
208 Py_INCREF(function);
209 *hook_var = function;
210 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000211 }
212 else {
Tim Peters885d4572001-11-28 20:27:42 +0000213 PyOS_snprintf(buf, sizeof(buf),
214 "set_%.50s(func): argument not callable",
215 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000216 PyErr_SetString(PyExc_TypeError, buf);
217 return NULL;
218 }
Christian Heimes1bc4af42007-11-12 18:58:08 +0000219 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000220}
221
Guido van Rossum74f31432003-01-07 20:01:29 +0000222
Martin v. Löwis0daad592001-09-30 21:09:59 +0000223/* Exported functions to specify hook functions in Python */
224
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000225static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000226static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000227
228#ifdef HAVE_RL_PRE_INPUT_HOOK
229static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000230#endif
231
232static PyObject *
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000233set_completion_display_matches_hook(PyObject *self, PyObject *args)
234{
Martin v. Löwisf3548942007-11-12 04:53:02 +0000235 PyObject *result = set_hook("completion_display_matches_hook",
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000236 &completion_display_matches_hook, args);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000237#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
238 /* We cannot set this hook globally, since it replaces the
239 default completion display. */
240 rl_completion_display_matches_hook =
Christian Heimes1bc4af42007-11-12 18:58:08 +0000241 completion_display_matches_hook ?
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000242#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwisf3548942007-11-12 04:53:02 +0000243 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000244#else
245 (VFunction *)on_completion_display_matches_hook : 0;
246#endif
Martin v. Löwisf3548942007-11-12 04:53:02 +0000247#endif
248 return result;
249
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000250}
251
252PyDoc_STRVAR(doc_set_completion_display_matches_hook,
253"set_completion_display_matches_hook([function]) -> None\n\
254Set or remove the completion display function.\n\
255The function is called as\n\
256 function(substitution, [matches], longest_match_length)\n\
257once each time matches need to be displayed.");
258
259static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000260set_startup_hook(PyObject *self, PyObject *args)
261{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000262 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000263}
264
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000265PyDoc_STRVAR(doc_set_startup_hook,
266"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000267Set or remove the startup_hook function.\n\
268The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000269before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000270
Guido van Rossum74f31432003-01-07 20:01:29 +0000271
Martin v. Löwis0daad592001-09-30 21:09:59 +0000272#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000273
274/* Set pre-input hook */
275
Martin v. Löwis0daad592001-09-30 21:09:59 +0000276static PyObject *
277set_pre_input_hook(PyObject *self, PyObject *args)
278{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000279 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000280}
281
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000282PyDoc_STRVAR(doc_set_pre_input_hook,
283"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000284Set or remove the pre_input_hook function.\n\
285The function is called with no arguments after the first prompt\n\
286has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000287characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000288
Martin v. Löwis0daad592001-09-30 21:09:59 +0000289#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000290
Guido van Rossum74f31432003-01-07 20:01:29 +0000291
Guido van Rossum290900a1997-09-26 21:51:21 +0000292/* Exported function to specify a word completer in Python */
293
294static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000295
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000296static PyObject *begidx = NULL;
297static PyObject *endidx = NULL;
298
Guido van Rossum74f31432003-01-07 20:01:29 +0000299
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000300/* Get the completion type for the scope of the tab-completion */
301static PyObject *
302get_completion_type(PyObject *self, PyObject *noarg)
303{
304 return PyInt_FromLong(rl_completion_type);
305}
306
307PyDoc_STRVAR(doc_get_completion_type,
308"get_completion_type() -> int\n\
309Get the type of completion being attempted.");
310
311
Guido van Rossum74f31432003-01-07 20:01:29 +0000312/* Get the beginning index for the scope of the tab-completion */
313
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000314static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000315get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000316{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000317 Py_INCREF(begidx);
318 return begidx;
319}
320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000321PyDoc_STRVAR(doc_get_begidx,
322"get_begidx() -> int\n\
323get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000324
Guido van Rossum74f31432003-01-07 20:01:29 +0000325
326/* Get the ending index for the scope of the tab-completion */
327
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000328static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000329get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000330{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000331 Py_INCREF(endidx);
332 return endidx;
333}
334
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000335PyDoc_STRVAR(doc_get_endidx,
336"get_endidx() -> int\n\
337get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000338
339
Guido van Rossum74f31432003-01-07 20:01:29 +0000340/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000341
342static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000343set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000344{
345 char *break_chars;
346
Guido van Rossum43713e52000-02-29 13:59:29 +0000347 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000348 return NULL;
349 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000350 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000351 rl_completer_word_break_characters = strdup(break_chars);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000352 Py_RETURN_NONE;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000353}
354
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000355PyDoc_STRVAR(doc_set_completer_delims,
356"set_completer_delims(string) -> None\n\
357set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000358
Skip Montanaroe5069012004-08-15 14:32:06 +0000359static PyObject *
360py_remove_history(PyObject *self, PyObject *args)
361{
Christian Heimes1bc4af42007-11-12 18:58:08 +0000362 int entry_number;
363 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000364
Christian Heimes1bc4af42007-11-12 18:58:08 +0000365 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
366 return NULL;
367 if (entry_number < 0) {
368 PyErr_SetString(PyExc_ValueError,
369 "History index cannot be negative");
370 return NULL;
371 }
372 entry = remove_history(entry_number);
373 if (!entry) {
374 PyErr_Format(PyExc_ValueError,
375 "No history item at position %d",
376 entry_number);
377 return NULL;
378 }
379 /* free memory allocated for the history entry */
380 if (entry->line)
381 free(entry->line);
382 if (entry->data)
383 free(entry->data);
384 free(entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000385
Christian Heimes1bc4af42007-11-12 18:58:08 +0000386 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000387}
388
389PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000390"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000391remove history item given by its position");
392
393static PyObject *
394py_replace_history(PyObject *self, PyObject *args)
395{
Christian Heimes1bc4af42007-11-12 18:58:08 +0000396 int entry_number;
397 char *line;
398 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000399
Christian Heimes1bc4af42007-11-12 18:58:08 +0000400 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
401 &line)) {
402 return NULL;
403 }
404 if (entry_number < 0) {
405 PyErr_SetString(PyExc_ValueError,
406 "History index cannot be negative");
407 return NULL;
408 }
409 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
410 if (!old_entry) {
411 PyErr_Format(PyExc_ValueError,
412 "No history item at position %d",
413 entry_number);
414 return NULL;
415 }
416 /* free memory allocated for the old history entry */
417 if (old_entry->line)
418 free(old_entry->line);
419 if (old_entry->data)
420 free(old_entry->data);
421 free(old_entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000422
Christian Heimes1bc4af42007-11-12 18:58:08 +0000423 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000424}
425
426PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000427"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000428replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000429
430/* Add a line to the history buffer */
431
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000432static PyObject *
433py_add_history(PyObject *self, PyObject *args)
434{
435 char *line;
436
437 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
438 return NULL;
439 }
440 add_history(line);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000441 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000442}
443
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000444PyDoc_STRVAR(doc_add_history,
445"add_history(string) -> None\n\
446add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000447
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000448
Guido van Rossum74f31432003-01-07 20:01:29 +0000449/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000450
451static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000452get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000453{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000454 return PyString_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000455}
Guido van Rossum74f31432003-01-07 20:01:29 +0000456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000457PyDoc_STRVAR(doc_get_completer_delims,
458"get_completer_delims() -> string\n\
459get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000460
Guido van Rossum74f31432003-01-07 20:01:29 +0000461
462/* Set the completer function */
463
Guido van Rossum290900a1997-09-26 21:51:21 +0000464static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000465set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000466{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000467 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000468}
469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000470PyDoc_STRVAR(doc_set_completer,
471"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000472Set or remove the completer function.\n\
473The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000474for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000475It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000476
Guido van Rossum74f31432003-01-07 20:01:29 +0000477
Michael W. Hudson796df152003-01-30 10:12:51 +0000478static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000479get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000480{
481 if (completer == NULL) {
Christian Heimes1bc4af42007-11-12 18:58:08 +0000482 Py_RETURN_NONE;
Michael W. Hudson796df152003-01-30 10:12:51 +0000483 }
484 Py_INCREF(completer);
485 return completer;
486}
487
488PyDoc_STRVAR(doc_get_completer,
489"get_completer() -> function\n\
490\n\
491Returns current completer function.");
492
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000493/* Exported function to get any element of history */
494
495static PyObject *
496get_history_item(PyObject *self, PyObject *args)
497{
498 int idx = 0;
499 HIST_ENTRY *hist_ent;
500
501 if (!PyArg_ParseTuple(args, "i:index", &idx))
502 return NULL;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000503#ifdef __APPLE__
504 if (using_libedit_emulation) {
505 /* Libedit emulation uses 0-based indexes,
506 * the real one uses 1-based indexes,
507 * adjust the index to ensure that Python
508 * code doesn't have to worry about the
509 * difference.
510 */
511 HISTORY_STATE *hist_st;
512 hist_st = history_get_history_state();
513
514 idx --;
515
516 /*
517 * Apple's readline emulation crashes when
518 * the index is out of range, therefore
519 * test for that and fail gracefully.
520 */
521 if (idx < 0 || idx >= hist_st->length) {
522 Py_RETURN_NONE;
523 }
524 }
525#endif /* __APPLE__ */
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000526 if ((hist_ent = history_get(idx)))
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000527 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000528 else {
Christian Heimes1bc4af42007-11-12 18:58:08 +0000529 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000530 }
531}
532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000533PyDoc_STRVAR(doc_get_history_item,
534"get_history_item() -> string\n\
535return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000536
Guido van Rossum74f31432003-01-07 20:01:29 +0000537
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000538/* Exported function to get current length of history */
539
540static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000541get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000542{
543 HISTORY_STATE *hist_st;
544
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000545 hist_st = history_get_history_state();
546 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
547}
548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000549PyDoc_STRVAR(doc_get_current_history_length,
550"get_current_history_length() -> integer\n\
551return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000552
Guido van Rossum74f31432003-01-07 20:01:29 +0000553
Guido van Rossum79378ff1997-10-07 14:53:21 +0000554/* Exported function to read the current line buffer */
555
556static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000557get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000558{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000559 return PyString_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000560}
561
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000562PyDoc_STRVAR(doc_get_line_buffer,
563"get_line_buffer() -> string\n\
564return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000565
Guido van Rossum74f31432003-01-07 20:01:29 +0000566
Martin v. Löwise7a97962003-09-20 16:08:33 +0000567#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
568
569/* Exported function to clear the current history */
570
571static PyObject *
572py_clear_history(PyObject *self, PyObject *noarg)
573{
574 clear_history();
Christian Heimes1bc4af42007-11-12 18:58:08 +0000575 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000576}
577
578PyDoc_STRVAR(doc_clear_history,
579"clear_history() -> None\n\
580Clear the current readline history.");
581#endif
582
583
Guido van Rossum79378ff1997-10-07 14:53:21 +0000584/* Exported function to insert text into the line buffer */
585
586static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000587insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000588{
589 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000590 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000591 return NULL;
592 rl_insert_text(s);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000593 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000594}
595
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000596PyDoc_STRVAR(doc_insert_text,
597"insert_text(string) -> None\n\
598Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000599
Guido van Rossum74f31432003-01-07 20:01:29 +0000600
601/* Redisplay the line buffer */
602
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000603static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000604redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000605{
606 rl_redisplay();
Christian Heimes1bc4af42007-11-12 18:58:08 +0000607 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000608}
609
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000610PyDoc_STRVAR(doc_redisplay,
611"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000612Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000613contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000614
Guido van Rossum74f31432003-01-07 20:01:29 +0000615
Guido van Rossum290900a1997-09-26 21:51:21 +0000616/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000617
618static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000619{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000620 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000621 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000622 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000623 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000624 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000625 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000626 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000627 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000628 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000629 {"get_history_item", get_history_item,
630 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000631 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000632 METH_NOARGS, doc_get_current_history_length},
Christian Heimes1bc4af42007-11-12 18:58:08 +0000633 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000634 METH_VARARGS, set_history_length_doc},
Christian Heimes1bc4af42007-11-12 18:58:08 +0000635 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000636 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000637 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000638 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000639 {"get_completion_type", get_completion_type,
640 METH_NOARGS, doc_get_completion_type},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000641 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
642 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000643
Guido van Rossum74f31432003-01-07 20:01:29 +0000644 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000645 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000646 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Christian Heimes1bc4af42007-11-12 18:58:08 +0000647 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
648 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000649 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000650 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000651
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000652 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
653 METH_VARARGS, doc_set_completion_display_matches_hook},
Guido van Rossum74f31432003-01-07 20:01:29 +0000654 {"set_startup_hook", set_startup_hook,
655 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000656#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000657 {"set_pre_input_hook", set_pre_input_hook,
658 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000659#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000660#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
661 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
662#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000663 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000664};
665
Guido van Rossum05ac4492003-01-07 20:04:12 +0000666
Martin v. Löwis0daad592001-09-30 21:09:59 +0000667/* C function to call the Python hooks. */
668
669static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000670on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000671{
672 int result = 0;
673 if (func != NULL) {
674 PyObject *r;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000675#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000676 PyGILState_STATE gilstate = PyGILState_Ensure();
677#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000678 r = PyObject_CallFunction(func, NULL);
679 if (r == NULL)
680 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000681 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000682 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000683 else {
Martin v. Löwis0daad592001-09-30 21:09:59 +0000684 result = PyInt_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000685 if (result == -1 && PyErr_Occurred())
686 goto error;
687 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000688 Py_DECREF(r);
689 goto done;
690 error:
691 PyErr_Clear();
692 Py_XDECREF(r);
693 done:
Christian Heimes1bc4af42007-11-12 18:58:08 +0000694#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000695 PyGILState_Release(gilstate);
696#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000697 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000698 }
699 return result;
700}
701
702static int
703on_startup_hook(void)
704{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000705 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000706}
707
708#ifdef HAVE_RL_PRE_INPUT_HOOK
709static int
710on_pre_input_hook(void)
711{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000712 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000713}
714#endif
715
Guido van Rossum05ac4492003-01-07 20:04:12 +0000716
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000717/* C function to call the Python completion_display_matches */
718
719static void
720on_completion_display_matches_hook(char **matches,
721 int num_matches, int max_length)
722{
Martin v. Löwisf3548942007-11-12 04:53:02 +0000723 int i;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000724 PyObject *m=NULL, *s=NULL, *r=NULL;
725#ifdef WITH_THREAD
Martin v. Löwisf3548942007-11-12 04:53:02 +0000726 PyGILState_STATE gilstate = PyGILState_Ensure();
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000727#endif
Martin v. Löwisf3548942007-11-12 04:53:02 +0000728 m = PyList_New(num_matches);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000729 if (m == NULL)
730 goto error;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000731 for (i = 0; i < num_matches; i++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000732 s = PyString_FromString(matches[i+1]);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000733 if (s == NULL)
734 goto error;
735 if (PyList_SetItem(m, i, s) == -1)
736 goto error;
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000737 }
Martin v. Löwisf3548942007-11-12 04:53:02 +0000738
739 r = PyObject_CallFunction(completion_display_matches_hook,
740 "sOi", matches[0], m, max_length);
741
Matthias Klose91a3b9e2009-04-05 21:19:13 +0000742 Py_DECREF(m); m=NULL;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000743
744 if (r == NULL ||
745 (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
746 goto error;
747 }
Matthias Klose91a3b9e2009-04-05 21:19:13 +0000748 Py_XDECREF(r); r=NULL;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000749
Christian Heimes1bc4af42007-11-12 18:58:08 +0000750 if (0) {
751 error:
752 PyErr_Clear();
753 Py_XDECREF(m);
754 Py_XDECREF(r);
755 }
756#ifdef WITH_THREAD
Martin v. Löwisf3548942007-11-12 04:53:02 +0000757 PyGILState_Release(gilstate);
758#endif
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000759}
760
761
Guido van Rossum290900a1997-09-26 21:51:21 +0000762/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000763
Guido van Rossum290900a1997-09-26 21:51:21 +0000764static char *
Neal Norwitzbd538702007-04-19 05:52:37 +0000765on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000766{
Guido van Rossum290900a1997-09-26 21:51:21 +0000767 char *result = NULL;
768 if (completer != NULL) {
769 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000770#ifdef WITH_THREAD
771 PyGILState_STATE gilstate = PyGILState_Ensure();
772#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000773 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000774 r = PyObject_CallFunction(completer, "si", text, state);
775 if (r == NULL)
776 goto error;
777 if (r == Py_None) {
778 result = NULL;
779 }
780 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000781 char *s = PyString_AsString(r);
Guido van Rossum290900a1997-09-26 21:51:21 +0000782 if (s == NULL)
783 goto error;
784 result = strdup(s);
785 }
786 Py_DECREF(r);
787 goto done;
788 error:
789 PyErr_Clear();
790 Py_XDECREF(r);
791 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000792#ifdef WITH_THREAD
793 PyGILState_Release(gilstate);
794#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000795 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000796 }
797 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000798}
799
Guido van Rossum290900a1997-09-26 21:51:21 +0000800
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000801/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000802 * before calling the normal completer */
803
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000804static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000805flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000806{
Antoine Pitrou119cdef2009-10-19 18:17:18 +0000807#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
808 rl_completion_append_character ='\0';
Antoine Pitroud9ff74e2009-10-26 19:16:46 +0000809#endif
810#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrou119cdef2009-10-19 18:17:18 +0000811 rl_completion_suppress_append = 0;
812#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000813 Py_XDECREF(begidx);
814 Py_XDECREF(endidx);
815 begidx = PyInt_FromLong((long) start);
816 endidx = PyInt_FromLong((long) end);
817 return completion_matches(text, *on_completion);
818}
819
Guido van Rossum05ac4492003-01-07 20:04:12 +0000820
Guido van Rossum290900a1997-09-26 21:51:21 +0000821/* Helper to initialize GNU readline properly. */
822
823static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000824setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000825{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000826#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000827 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000828 if (!saved_locale)
829 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000830#endif
831
Skip Montanaroa0392742002-06-11 14:32:46 +0000832 using_history();
833
Guido van Rossum290900a1997-09-26 21:51:21 +0000834 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000835#if defined(PYOS_OS2) && defined(PYCC_GCC)
836 /* Allow $if term= in .inputrc to work */
837 rl_terminal_name = getenv("TERM");
838#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000839 /* Force rebind of TAB to insert-tab */
840 rl_bind_key('\t', rl_insert);
841 /* Bind both ESC-TAB and ESC-ESC to the completion function */
842 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
843 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000844 /* Set our hook functions */
845 rl_startup_hook = (Function *)on_startup_hook;
846#ifdef HAVE_RL_PRE_INPUT_HOOK
847 rl_pre_input_hook = (Function *)on_pre_input_hook;
848#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000849 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000850 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000851 /* Set Python word break characters */
852 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000853 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000854 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000855
856 begidx = PyInt_FromLong(0L);
857 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000858 /* Initialize (allows .inputrc to override)
859 *
860 * XXX: A bug in the readline-2.2 library causes a memory leak
861 * inside this function. Nothing we can do about it.
862 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000863 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000864
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000865 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000866}
867
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000868/* Wrapper around GNU readline that handles signals differently. */
869
870
871#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
872
873static char *completed_input_string;
874static void
875rlhandler(char *text)
876{
877 completed_input_string = text;
878 rl_callback_handler_remove();
879}
880
881extern PyThreadState* _PyOS_ReadlineTState;
882
883static char *
884readline_until_enter_or_signal(char *prompt, int *signal)
885{
886 char * not_done_reading = "";
887 fd_set selectset;
888
889 *signal = 0;
890#ifdef HAVE_RL_CATCH_SIGNAL
891 rl_catch_signals = 0;
892#endif
893
894 rl_callback_handler_install (prompt, rlhandler);
895 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000896
897 completed_input_string = not_done_reading;
898
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000899 while (completed_input_string == not_done_reading) {
900 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000901
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000902 while (!has_input)
903 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000904
905 /* [Bug #1552726] Only limit the pause if an input hook has been
906 defined. */
907 struct timeval *timeoutp = NULL;
908 if (PyOS_InputHook)
909 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000910 FD_SET(fileno(rl_instream), &selectset);
911 /* select resets selectset if no input was available */
912 has_input = select(fileno(rl_instream) + 1, &selectset,
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000913 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000914 if(PyOS_InputHook) PyOS_InputHook();
915 }
916
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000917 if(has_input > 0) {
918 rl_callback_read_char();
919 }
920 else if (errno == EINTR) {
921 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000922#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000923 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000924#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000925 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000926#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000927 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000928#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000929 if (s < 0) {
930 rl_free_line_state();
931 rl_cleanup_after_signal();
932 rl_callback_handler_remove();
933 *signal = 1;
934 completed_input_string = NULL;
935 }
936 }
937 }
938
939 return completed_input_string;
940}
941
942
943#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000944
945/* Interrupt handler */
946
947static jmp_buf jbuf;
948
Guido van Rossum0969d361997-08-05 21:27:50 +0000949/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000950static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000951onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000952{
Guido van Rossum290900a1997-09-26 21:51:21 +0000953 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000954}
955
Guido van Rossum290900a1997-09-26 21:51:21 +0000956
Guido van Rossum0969d361997-08-05 21:27:50 +0000957static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000958readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000959{
Guido van Rossum174efc92000-09-16 16:37:53 +0000960 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000961 char *p;
962
963 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000964
Guido van Rossum174efc92000-09-16 16:37:53 +0000965 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000966 if (setjmp(jbuf)) {
967#ifdef HAVE_SIGRELSE
968 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
969 sigrelse(SIGINT);
970#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000971 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000972 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000973 return NULL;
974 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000975 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000976 p = readline(prompt);
977 PyOS_setsig(SIGINT, old_inthandler);
978
979 return p;
980}
981#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
982
983
984static char *
985call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
986{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000987 size_t n;
988 char *p, *q;
989 int signal;
990
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000991#ifdef SAVE_LOCALE
992 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000993 if (!saved_locale)
994 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000995 setlocale(LC_CTYPE, "");
996#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000997
Guido van Rossum74f31432003-01-07 20:01:29 +0000998 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
999 rl_instream = sys_stdin;
1000 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001001#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +00001002 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001003#endif
Guido van Rossum74f31432003-01-07 20:01:29 +00001004 }
1005
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001006 p = readline_until_enter_or_signal(prompt, &signal);
1007
1008 /* we got an interrupt signal */
Neal Norwitz5eaf7722006-07-16 02:15:27 +00001009 if (signal) {
1010 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001011 return NULL;
1012 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001013
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001014 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +00001015 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001016 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001017 if (p != NULL)
1018 *p = '\0';
Neal Norwitz5eaf7722006-07-16 02:15:27 +00001019 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +00001020 return p;
1021 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001022
1023 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +00001024 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +00001025 if (n > 0) {
1026 char *line;
1027 HISTORY_STATE *state = history_get_history_state();
1028 if (state->length > 0)
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001029#ifdef __APPLE__
1030 if (using_libedit_emulation) {
1031 /*
1032 * Libedit's emulation uses 0-based indexes,
1033 * the real readline uses 1-based indexes.
1034 */
1035 line = history_get(state->length - 1)->line;
1036 } else
1037#endif /* __APPLE__ */
Skip Montanaroa0392742002-06-11 14:32:46 +00001038 line = history_get(state->length)->line;
1039 else
1040 line = "";
1041 if (strcmp(p, line))
1042 add_history(p);
1043 /* the history docs don't say so, but the address of state
1044 changes each time history_get_history_state is called
1045 which makes me think it's freshly malloc'd memory...
1046 on the other hand, the address of the last line stays the
1047 same as long as history isn't extended, so it appears to
1048 be malloc'd but managed by the history package... */
1049 free(state);
1050 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001051 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1052 release the original. */
1053 q = p;
1054 p = PyMem_Malloc(n+2);
1055 if (p != NULL) {
1056 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +00001057 p[n] = '\n';
1058 p[n+1] = '\0';
1059 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001060 free(q);
Neal Norwitz5eaf7722006-07-16 02:15:27 +00001061 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +00001062 return p;
1063}
1064
Guido van Rossum290900a1997-09-26 21:51:21 +00001065
1066/* Initialize the module */
1067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001068PyDoc_STRVAR(doc_module,
1069"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001070
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001071#ifdef __APPLE__
1072PyDoc_STRVAR(doc_module_le,
1073"Importing this module enables command line editing using libedit readline.");
1074#endif /* __APPLE__ */
1075
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001076PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001077initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001078{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +00001079 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001080
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001081#ifdef __APPLE__
1082 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1083 using_libedit_emulation = 1;
1084 }
1085
1086 if (using_libedit_emulation)
1087 m = Py_InitModule4("readline", readline_methods, doc_module_le,
1088 (PyObject *)NULL, PYTHON_API_VERSION);
1089 else
1090
1091#endif /* __APPLE__ */
1092
Guido van Rossum290900a1997-09-26 21:51:21 +00001093 m = Py_InitModule4("readline", readline_methods, doc_module,
1094 (PyObject *)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001095 if (m == NULL)
1096 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001097
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001098
1099
Guido van Rossum74f31432003-01-07 20:01:29 +00001100 PyOS_ReadlineFunctionPointer = call_readline;
1101 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +00001102}