blob: fb6d3f9857e1f322d4d46d193d9d6000194aa277 [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
Brett Cannon23b581a2010-05-04 00:52:41 +000026# define RESTORE_LOCALE(sl)
Neal Norwitz5eaf7722006-07-16 02:15:27 +000027#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) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +000036 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
Brett Cannon23b581a2010-05-04 00:52:41 +000051 * emulation library of editline/libedit.
52 *
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000053 * 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 *
Brett Cannon23b581a2010-05-04 00:52:41 +000057 * Currently there is one know API incompatibility:
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000058 * - '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,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000069 int num_matches, int max_length);
Martin v. Löwisf3548942007-11-12 04:53:02 +000070
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000077 char *s, *copy;
78 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
79 return NULL;
80 /* 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 */
88 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000101 char *s = NULL;
102 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
103 return NULL;
104 errno = rl_read_init_file(s);
105 if (errno)
106 return PyErr_SetFromErrno(PyExc_IOError);
107 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 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);
127 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000142 char *s = NULL;
143 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
144 return NULL;
145 errno = write_history(s);
146 if (!errno && _history_length >= 0)
147 history_truncate_file(s, _history_length);
148 if (errno)
149 return PyErr_SetFromErrno(PyExc_IOError);
150 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000164 int length = _history_length;
165 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
166 return NULL;
167 _history_length = length;
168 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 PyObject *function = Py_None;
198 char buf[80];
199 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
200 if (!PyArg_ParseTuple(args, buf, &function))
201 return NULL;
202 if (function == Py_None) {
203 Py_XDECREF(*hook_var);
204 *hook_var = NULL;
205 }
206 else if (PyCallable_Check(function)) {
207 PyObject *tmp = *hook_var;
208 Py_INCREF(function);
209 *hook_var = function;
210 Py_XDECREF(tmp);
211 }
212 else {
213 PyOS_snprintf(buf, sizeof(buf),
214 "set_%.50s(func): argument not callable",
215 funcname);
216 PyErr_SetString(PyExc_TypeError, buf);
217 return NULL;
218 }
219 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000235 PyObject *result = set_hook("completion_display_matches_hook",
236 &completion_display_matches_hook, args);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000237#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000238 /* We cannot set this hook globally, since it replaces the
239 default completion display. */
240 rl_completion_display_matches_hook =
241 completion_display_matches_hook ?
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000242#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000244#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000245 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisbb86d832008-11-04 20:40:09 +0000246#endif
Martin v. Löwisf3548942007-11-12 04:53:02 +0000247#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000248 return result;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000249
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000317 Py_INCREF(begidx);
318 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000319}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331 Py_INCREF(endidx);
332 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000333}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000345 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000346
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000347 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
348 return NULL;
349 }
350 free((void*)rl_completer_word_break_characters);
351 rl_completer_word_break_characters = strdup(break_chars);
352 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
Mark Dickinson4ee98532010-08-03 16:18:39 +0000359/* _py_free_history_entry: Utility function to free a history entry. */
360
361#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
362
363/* Readline version >= 5.0 introduced a timestamp field into the history entry
364 structure; this needs to be freed to avoid a memory leak. This version of
365 readline also introduced the handy 'free_history_entry' function, which
366 takes care of the timestamp. */
367
368static void
369_py_free_history_entry(HIST_ENTRY *entry)
370{
371 histdata_t data = free_history_entry(entry);
372 free(data);
373}
374
375#else
376
377/* No free_history_entry function; free everything manually. */
378
379static void
380_py_free_history_entry(HIST_ENTRY *entry)
381{
382 if (entry->line)
383 free((void *)entry->line);
384 if (entry->data)
385 free(entry->data);
386 free(entry);
387}
388
389#endif
390
Skip Montanaroe5069012004-08-15 14:32:06 +0000391static PyObject *
392py_remove_history(PyObject *self, PyObject *args)
393{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000394 int entry_number;
395 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000396
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000397 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
398 return NULL;
399 if (entry_number < 0) {
400 PyErr_SetString(PyExc_ValueError,
401 "History index cannot be negative");
402 return NULL;
403 }
404 entry = remove_history(entry_number);
405 if (!entry) {
406 PyErr_Format(PyExc_ValueError,
407 "No history item at position %d",
408 entry_number);
409 return NULL;
410 }
411 /* free memory allocated for the history entry */
Mark Dickinson4ee98532010-08-03 16:18:39 +0000412 _py_free_history_entry(entry);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000413 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000414}
415
416PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000417"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000418remove history item given by its position");
419
420static PyObject *
421py_replace_history(PyObject *self, PyObject *args)
422{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000423 int entry_number;
424 char *line;
425 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000426
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000427 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
428 &line)) {
429 return NULL;
430 }
431 if (entry_number < 0) {
432 PyErr_SetString(PyExc_ValueError,
433 "History index cannot be negative");
434 return NULL;
435 }
436 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
437 if (!old_entry) {
438 PyErr_Format(PyExc_ValueError,
439 "No history item at position %d",
440 entry_number);
441 return NULL;
442 }
443 /* free memory allocated for the old history entry */
Mark Dickinson4ee98532010-08-03 16:18:39 +0000444 _py_free_history_entry(old_entry);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000445 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000446}
447
448PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000449"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000450replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000451
452/* Add a line to the history buffer */
453
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000454static PyObject *
455py_add_history(PyObject *self, PyObject *args)
456{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000457 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000458
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000459 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
460 return NULL;
461 }
462 add_history(line);
463 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000464}
465
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000466PyDoc_STRVAR(doc_add_history,
467"add_history(string) -> None\n\
468add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000469
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000470
Guido van Rossum74f31432003-01-07 20:01:29 +0000471/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000472
473static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000474get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000475{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000476 return PyString_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000477}
Guido van Rossum74f31432003-01-07 20:01:29 +0000478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000479PyDoc_STRVAR(doc_get_completer_delims,
480"get_completer_delims() -> string\n\
481get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000482
Guido van Rossum74f31432003-01-07 20:01:29 +0000483
484/* Set the completer function */
485
Guido van Rossum290900a1997-09-26 21:51:21 +0000486static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000487set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000488{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000489 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000490}
491
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000492PyDoc_STRVAR(doc_set_completer,
493"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000494Set or remove the completer function.\n\
495The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000496for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000497It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000498
Guido van Rossum74f31432003-01-07 20:01:29 +0000499
Michael W. Hudson796df152003-01-30 10:12:51 +0000500static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000501get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000502{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000503 if (completer == NULL) {
504 Py_RETURN_NONE;
505 }
506 Py_INCREF(completer);
507 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000508}
509
510PyDoc_STRVAR(doc_get_completer,
511"get_completer() -> function\n\
512\n\
513Returns current completer function.");
514
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000515/* Exported function to get any element of history */
516
517static PyObject *
518get_history_item(PyObject *self, PyObject *args)
519{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000520 int idx = 0;
521 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000522
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000523 if (!PyArg_ParseTuple(args, "i:index", &idx))
524 return NULL;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000525#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000526 if (using_libedit_emulation) {
527 /* Libedit emulation uses 0-based indexes,
528 * the real one uses 1-based indexes,
529 * adjust the index to ensure that Python
530 * code doesn't have to worry about the
531 * difference.
532 */
533 HISTORY_STATE *hist_st;
534 hist_st = history_get_history_state();
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000535
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000536 idx --;
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000537
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000538 /*
539 * Apple's readline emulation crashes when
540 * the index is out of range, therefore
541 * test for that and fail gracefully.
542 */
543 if (idx < 0 || idx >= hist_st->length) {
544 Py_RETURN_NONE;
545 }
546 }
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +0000547#endif /* __APPLE__ */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000548 if ((hist_ent = history_get(idx)))
549 return PyString_FromString(hist_ent->line);
550 else {
551 Py_RETURN_NONE;
552 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000553}
554
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000555PyDoc_STRVAR(doc_get_history_item,
556"get_history_item() -> string\n\
557return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000558
Guido van Rossum74f31432003-01-07 20:01:29 +0000559
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000560/* Exported function to get current length of history */
561
562static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000563get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000564{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000565 HISTORY_STATE *hist_st;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000566
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000567 hist_st = history_get_history_state();
568 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000569}
570
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000571PyDoc_STRVAR(doc_get_current_history_length,
572"get_current_history_length() -> integer\n\
573return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000574
Guido van Rossum74f31432003-01-07 20:01:29 +0000575
Guido van Rossum79378ff1997-10-07 14:53:21 +0000576/* Exported function to read the current line buffer */
577
578static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000579get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000580{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000581 return PyString_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000582}
583
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000584PyDoc_STRVAR(doc_get_line_buffer,
585"get_line_buffer() -> string\n\
586return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000587
Guido van Rossum74f31432003-01-07 20:01:29 +0000588
Martin v. Löwise7a97962003-09-20 16:08:33 +0000589#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
590
591/* Exported function to clear the current history */
592
593static PyObject *
594py_clear_history(PyObject *self, PyObject *noarg)
595{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000596 clear_history();
597 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000598}
599
600PyDoc_STRVAR(doc_clear_history,
601"clear_history() -> None\n\
602Clear the current readline history.");
603#endif
604
605
Guido van Rossum79378ff1997-10-07 14:53:21 +0000606/* Exported function to insert text into the line buffer */
607
608static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000609insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000610{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000611 char *s;
612 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
613 return NULL;
614 rl_insert_text(s);
615 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000616}
617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000618PyDoc_STRVAR(doc_insert_text,
619"insert_text(string) -> None\n\
620Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000621
Guido van Rossum74f31432003-01-07 20:01:29 +0000622
623/* Redisplay the line buffer */
624
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000625static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000626redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000627{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000628 rl_redisplay();
629 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000630}
631
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000632PyDoc_STRVAR(doc_redisplay,
633"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000634Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000635contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000636
Guido van Rossum74f31432003-01-07 20:01:29 +0000637
Guido van Rossum290900a1997-09-26 21:51:21 +0000638/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000639
640static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000641{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000642 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
643 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
644 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
645 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
646 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
647 {"read_history_file", read_history_file,
648 METH_VARARGS, doc_read_history_file},
649 {"write_history_file", write_history_file,
650 METH_VARARGS, doc_write_history_file},
651 {"get_history_item", get_history_item,
652 METH_VARARGS, doc_get_history_item},
653 {"get_current_history_length", (PyCFunction)get_current_history_length,
654 METH_NOARGS, doc_get_current_history_length},
655 {"set_history_length", set_history_length,
656 METH_VARARGS, set_history_length_doc},
657 {"get_history_length", get_history_length,
658 METH_NOARGS, get_history_length_doc},
659 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
660 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
661 {"get_completion_type", get_completion_type,
662 METH_NOARGS, doc_get_completion_type},
663 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
664 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000665
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000666 {"set_completer_delims", set_completer_delims,
667 METH_VARARGS, doc_set_completer_delims},
668 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
669 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
670 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
671 {"get_completer_delims", get_completer_delims,
672 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000673
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000674 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
675 METH_VARARGS, doc_set_completion_display_matches_hook},
676 {"set_startup_hook", set_startup_hook,
677 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000678#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000679 {"set_pre_input_hook", set_pre_input_hook,
680 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000681#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000682#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000683 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000684#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000685 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000686};
687
Guido van Rossum05ac4492003-01-07 20:04:12 +0000688
Martin v. Löwis0daad592001-09-30 21:09:59 +0000689/* C function to call the Python hooks. */
690
691static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000692on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000693{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000694 int result = 0;
695 if (func != NULL) {
696 PyObject *r;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000697#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000698 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000699#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000700 r = PyObject_CallFunction(func, NULL);
701 if (r == NULL)
702 goto error;
703 if (r == Py_None)
704 result = 0;
705 else {
706 result = PyInt_AsLong(r);
707 if (result == -1 && PyErr_Occurred())
708 goto error;
709 }
710 Py_DECREF(r);
711 goto done;
712 error:
713 PyErr_Clear();
714 Py_XDECREF(r);
715 done:
Christian Heimes1bc4af42007-11-12 18:58:08 +0000716#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000717 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000718#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 return result;
720 }
721 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000722}
723
724static int
725on_startup_hook(void)
726{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000727 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000728}
729
730#ifdef HAVE_RL_PRE_INPUT_HOOK
731static int
732on_pre_input_hook(void)
733{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000734 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000735}
736#endif
737
Guido van Rossum05ac4492003-01-07 20:04:12 +0000738
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000739/* C function to call the Python completion_display_matches */
740
741static void
742on_completion_display_matches_hook(char **matches,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000743 int num_matches, int max_length)
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000744{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000745 int i;
746 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000747#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000748 PyGILState_STATE gilstate = PyGILState_Ensure();
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000749#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000750 m = PyList_New(num_matches);
751 if (m == NULL)
752 goto error;
753 for (i = 0; i < num_matches; i++) {
754 s = PyString_FromString(matches[i+1]);
755 if (s == NULL)
756 goto error;
757 if (PyList_SetItem(m, i, s) == -1)
758 goto error;
759 }
Martin v. Löwisf3548942007-11-12 04:53:02 +0000760
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000761 r = PyObject_CallFunction(completion_display_matches_hook,
762 "sOi", matches[0], m, max_length);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000763
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000764 Py_DECREF(m); m=NULL;
Brett Cannon23b581a2010-05-04 00:52:41 +0000765
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000766 if (r == NULL ||
767 (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
768 goto error;
769 }
770 Py_XDECREF(r); r=NULL;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000771
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000772 if (0) {
773 error:
774 PyErr_Clear();
775 Py_XDECREF(m);
776 Py_XDECREF(r);
777 }
Christian Heimes1bc4af42007-11-12 18:58:08 +0000778#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000779 PyGILState_Release(gilstate);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000780#endif
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000781}
782
783
Guido van Rossum290900a1997-09-26 21:51:21 +0000784/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000785
Guido van Rossum290900a1997-09-26 21:51:21 +0000786static char *
Neal Norwitzbd538702007-04-19 05:52:37 +0000787on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000788{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000789 char *result = NULL;
790 if (completer != NULL) {
791 PyObject *r;
Brett Cannon23b581a2010-05-04 00:52:41 +0000792#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000793 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000794#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000795 rl_attempted_completion_over = 1;
796 r = PyObject_CallFunction(completer, "si", text, state);
797 if (r == NULL)
798 goto error;
799 if (r == Py_None) {
800 result = NULL;
801 }
802 else {
803 char *s = PyString_AsString(r);
804 if (s == NULL)
805 goto error;
806 result = strdup(s);
807 }
808 Py_DECREF(r);
809 goto done;
810 error:
811 PyErr_Clear();
812 Py_XDECREF(r);
813 done:
Brett Cannon23b581a2010-05-04 00:52:41 +0000814#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000815 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000816#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000817 return result;
818 }
819 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000820}
821
Guido van Rossum290900a1997-09-26 21:51:21 +0000822
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000823/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000824 * before calling the normal completer */
825
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000826static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000827flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000828{
Antoine Pitrou119cdef2009-10-19 18:17:18 +0000829#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000830 rl_completion_append_character ='\0';
Antoine Pitroud9ff74e2009-10-26 19:16:46 +0000831#endif
832#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000833 rl_completion_suppress_append = 0;
Antoine Pitrou119cdef2009-10-19 18:17:18 +0000834#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000835 Py_XDECREF(begidx);
836 Py_XDECREF(endidx);
837 begidx = PyInt_FromLong((long) start);
838 endidx = PyInt_FromLong((long) end);
839 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000840}
841
Guido van Rossum05ac4492003-01-07 20:04:12 +0000842
Guido van Rossum290900a1997-09-26 21:51:21 +0000843/* Helper to initialize GNU readline properly. */
844
845static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000846setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000847{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000848#ifdef SAVE_LOCALE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000849 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
850 if (!saved_locale)
851 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000852#endif
853
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000854 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000855
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000856 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000857#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000858 /* Allow $if term= in .inputrc to work */
859 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000860#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000861 /* Force rebind of TAB to insert-tab */
862 rl_bind_key('\t', rl_insert);
863 /* Bind both ESC-TAB and ESC-ESC to the completion function */
864 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
865 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
866 /* Set our hook functions */
867 rl_startup_hook = (Function *)on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000868#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000869 rl_pre_input_hook = (Function *)on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000870#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000871 /* Set our completion function */
872 rl_attempted_completion_function = (CPPFunction *)flex_complete;
873 /* Set Python word break characters */
874 rl_completer_word_break_characters =
875 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
876 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000877
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000878 begidx = PyInt_FromLong(0L);
879 endidx = PyInt_FromLong(0L);
880 /* Initialize (allows .inputrc to override)
881 *
882 * XXX: A bug in the readline-2.2 library causes a memory leak
883 * inside this function. Nothing we can do about it.
884 */
885 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000886
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000887 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000888}
889
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000890/* Wrapper around GNU readline that handles signals differently. */
891
892
893#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
894
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000895static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000896static void
897rlhandler(char *text)
898{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000899 completed_input_string = text;
900 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000901}
902
903extern PyThreadState* _PyOS_ReadlineTState;
904
905static char *
906readline_until_enter_or_signal(char *prompt, int *signal)
907{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000908 char * not_done_reading = "";
909 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000910
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000911 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000912#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000914#endif
915
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000916 rl_callback_handler_install (prompt, rlhandler);
917 FD_ZERO(&selectset);
Brett Cannon23b581a2010-05-04 00:52:41 +0000918
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000919 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000920
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000921 while (completed_input_string == not_done_reading) {
922 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000923
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000924 while (!has_input)
925 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000926
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 /* [Bug #1552726] Only limit the pause if an input hook has been
928 defined. */
929 struct timeval *timeoutp = NULL;
930 if (PyOS_InputHook)
931 timeoutp = &timeout;
932 FD_SET(fileno(rl_instream), &selectset);
933 /* select resets selectset if no input was available */
934 has_input = select(fileno(rl_instream) + 1, &selectset,
935 NULL, NULL, timeoutp);
936 if(PyOS_InputHook) PyOS_InputHook();
937 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000938
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000939 if(has_input > 0) {
940 rl_callback_read_char();
941 }
942 else if (errno == EINTR) {
943 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000944#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000945 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000946#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000947 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000948#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000949 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000950#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000951 if (s < 0) {
952 rl_free_line_state();
953 rl_cleanup_after_signal();
954 rl_callback_handler_remove();
955 *signal = 1;
956 completed_input_string = NULL;
957 }
958 }
959 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000960
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000961 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000962}
963
964
965#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000966
967/* Interrupt handler */
968
969static jmp_buf jbuf;
970
Guido van Rossum0969d361997-08-05 21:27:50 +0000971/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000972static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000973onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000974{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000975 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000976}
977
Guido van Rossum290900a1997-09-26 21:51:21 +0000978
Guido van Rossum0969d361997-08-05 21:27:50 +0000979static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000980readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000981{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000982 PyOS_sighandler_t old_inthandler;
983 char *p;
Brett Cannon23b581a2010-05-04 00:52:41 +0000984
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000985 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000986
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000987 old_inthandler = PyOS_setsig(SIGINT, onintr);
988 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +0000989#ifdef HAVE_SIGRELSE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000990 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
991 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +0000992#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000993 PyOS_setsig(SIGINT, old_inthandler);
994 *signal = 1;
995 return NULL;
996 }
997 rl_event_hook = PyOS_InputHook;
998 p = readline(prompt);
999 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001000
1001 return p;
1002}
1003#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
1004
1005
1006static char *
1007call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
1008{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001009 size_t n;
1010 char *p, *q;
1011 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +00001012
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001013#ifdef SAVE_LOCALE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001014 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
1015 if (!saved_locale)
1016 Py_FatalError("not enough memory to save locale");
1017 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +00001018#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001020 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
1021 rl_instream = sys_stdin;
1022 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001023#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001024 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001025#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001026 }
Guido van Rossum74f31432003-01-07 20:01:29 +00001027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001028 p = readline_until_enter_or_signal(prompt, &signal);
Brett Cannon23b581a2010-05-04 00:52:41 +00001029
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001030 /* we got an interrupt signal */
1031 if (signal) {
1032 RESTORE_LOCALE(saved_locale)
1033 return NULL;
1034 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001035
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001036 /* We got an EOF, return a empty string. */
1037 if (p == NULL) {
1038 p = PyMem_Malloc(1);
1039 if (p != NULL)
1040 *p = '\0';
1041 RESTORE_LOCALE(saved_locale)
1042 return p;
1043 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001044
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001045 /* we have a valid line */
1046 n = strlen(p);
1047 if (n > 0) {
1048 const char *line;
1049 HISTORY_STATE *state = history_get_history_state();
1050 if (state->length > 0)
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001051#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001052 if (using_libedit_emulation) {
1053 /*
1054 * Libedit's emulation uses 0-based indexes,
1055 * the real readline uses 1-based indexes.
1056 */
1057 line = history_get(state->length - 1)->line;
1058 } else
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001059#endif /* __APPLE__ */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001060 line = history_get(state->length)->line;
1061 else
1062 line = "";
1063 if (strcmp(p, line))
1064 add_history(p);
1065 /* the history docs don't say so, but the address of state
1066 changes each time history_get_history_state is called
1067 which makes me think it's freshly malloc'd memory...
1068 on the other hand, the address of the last line stays the
1069 same as long as history isn't extended, so it appears to
1070 be malloc'd but managed by the history package... */
1071 free(state);
1072 }
1073 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1074 release the original. */
1075 q = p;
1076 p = PyMem_Malloc(n+2);
1077 if (p != NULL) {
1078 strncpy(p, q, n);
1079 p[n] = '\n';
1080 p[n+1] = '\0';
1081 }
1082 free(q);
1083 RESTORE_LOCALE(saved_locale)
1084 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001085}
1086
Guido van Rossum290900a1997-09-26 21:51:21 +00001087
1088/* Initialize the module */
1089
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001090PyDoc_STRVAR(doc_module,
1091"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001092
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001093#ifdef __APPLE__
1094PyDoc_STRVAR(doc_module_le,
1095"Importing this module enables command line editing using libedit readline.");
1096#endif /* __APPLE__ */
1097
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001098PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001099initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001100{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001101 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001102
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001103#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001104 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1105 using_libedit_emulation = 1;
1106 }
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001107
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001108 if (using_libedit_emulation)
1109 m = Py_InitModule4("readline", readline_methods, doc_module_le,
1110 (PyObject *)NULL, PYTHON_API_VERSION);
1111 else
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001112
1113#endif /* __APPLE__ */
1114
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001115 m = Py_InitModule4("readline", readline_methods, doc_module,
1116 (PyObject *)NULL, PYTHON_API_VERSION);
1117 if (m == NULL)
1118 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001119
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001120
1121
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001122 PyOS_ReadlineFunctionPointer = call_readline;
1123 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +00001124}