blob: 712b8573ece93c84ceaf9ef767f1c812a3a43f2e [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öwis7f08c1f2008-11-04 20:43:31 +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öwis7f08c1f2008-11-04 20:43:31 +000040#else
Ronald Oussoren8ed66ed2010-02-07 20:04:45 +000041#if !defined(__APPLE__)
Martin v. Löwis7f08c1f2008-11-04 20:43:31 +000042extern char **completion_matches(char *, CPFunction *);
43#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000044#endif
Ronald Oussoren8ed66ed2010-02-07 20:04:45 +000045#endif
46
47#ifdef __APPLE__
48/*
49 * It is possible to link the readline module to the readline
50 * emulation library of editline/libedit.
51 *
52 * On OSX this emulation library is not 100% API compatible
53 * with the "real" readline and cannot be detected at compile-time,
54 * hence we use a runtime check to detect if we're using libedit
55 *
56 * Currently there is one know API incompatibility:
57 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
58 * index with libedit's emulation.
59 * - Note that replace_history and remove_history use a 0-based index
60 * with both implementation.
61 */
62static int using_libedit_emulation = 0;
63static const char libedit_version_tag[] = "EditLine wrapper";
64#endif /* __APPLE__ */
Guido van Rossum353ae582001-07-10 16:45:32 +000065
Martin v. Löwisf3548942007-11-12 04:53:02 +000066static void
67on_completion_display_matches_hook(char **matches,
68 int num_matches, int max_length);
69
Guido van Rossum0969d361997-08-05 21:27:50 +000070
Guido van Rossum290900a1997-09-26 21:51:21 +000071/* Exported function to send one line to readline's init file parser */
72
73static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000074parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000075{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000076 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000077 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000078 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000079 /* Make a copy -- rl_parse_and_bind() modifies its argument */
80 /* Bernard Herzog */
81 copy = malloc(1 + strlen(s));
82 if (copy == NULL)
83 return PyErr_NoMemory();
84 strcpy(copy, s);
85 rl_parse_and_bind(copy);
86 free(copy); /* Free the copy */
Christian Heimes1bc4af42007-11-12 18:58:08 +000087 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000088}
89
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000090PyDoc_STRVAR(doc_parse_and_bind,
91"parse_and_bind(string) -> None\n\
92Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000093
94
95/* Exported function to parse a readline init file */
96
97static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000098read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000099{
100 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +0000101 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +0000102 return NULL;
103 errno = rl_read_init_file(s);
104 if (errno)
105 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000106 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +0000107}
108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000109PyDoc_STRVAR(doc_read_init_file,
110"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000111Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000112The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000113
114
Skip Montanaro28067822000-07-06 18:55:12 +0000115/* Exported function to load a readline history file */
116
117static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000118read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000119{
120 char *s = NULL;
121 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
122 return NULL;
123 errno = read_history(s);
124 if (errno)
125 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000126 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000127}
128
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000129static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000130PyDoc_STRVAR(doc_read_history_file,
131"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000132Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000133The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000134
135
136/* Exported function to save a readline history file */
137
138static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000139write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000140{
141 char *s = NULL;
142 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
143 return NULL;
144 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000145 if (!errno && _history_length >= 0)
146 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000147 if (errno)
148 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000149 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000150}
151
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000152PyDoc_STRVAR(doc_write_history_file,
153"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000154Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000155The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000156
157
Guido van Rossum74f31432003-01-07 20:01:29 +0000158/* Set history length */
159
160static PyObject*
161set_history_length(PyObject *self, PyObject *args)
162{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000163 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000164 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
165 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000166 _history_length = length;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000167 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000168}
169
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000170PyDoc_STRVAR(set_history_length_doc,
171"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000172set the maximal number of items which will be written to\n\
173the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000174history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000175
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000176
Guido van Rossum74f31432003-01-07 20:01:29 +0000177/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000178
179static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000180get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000181{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000182 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000183}
184
Guido van Rossum74f31432003-01-07 20:01:29 +0000185PyDoc_STRVAR(get_history_length_doc,
186"get_history_length() -> int\n\
187return the maximum number of items that will be written to\n\
188the history file.");
189
190
Martin v. Löwis0daad592001-09-30 21:09:59 +0000191/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000192
Martin v. Löwis0daad592001-09-30 21:09:59 +0000193static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000194set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000195{
196 PyObject *function = Py_None;
197 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000198 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000199 if (!PyArg_ParseTuple(args, buf, &function))
200 return NULL;
201 if (function == Py_None) {
202 Py_XDECREF(*hook_var);
203 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000204 }
205 else if (PyCallable_Check(function)) {
206 PyObject *tmp = *hook_var;
207 Py_INCREF(function);
208 *hook_var = function;
209 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000210 }
211 else {
Tim Peters885d4572001-11-28 20:27:42 +0000212 PyOS_snprintf(buf, sizeof(buf),
213 "set_%.50s(func): argument not callable",
214 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000215 PyErr_SetString(PyExc_TypeError, buf);
216 return NULL;
217 }
Christian Heimes1bc4af42007-11-12 18:58:08 +0000218 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000219}
220
Guido van Rossum74f31432003-01-07 20:01:29 +0000221
Martin v. Löwis0daad592001-09-30 21:09:59 +0000222/* Exported functions to specify hook functions in Python */
223
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000224static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000225static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000226
227#ifdef HAVE_RL_PRE_INPUT_HOOK
228static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000229#endif
230
231static PyObject *
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000232set_completion_display_matches_hook(PyObject *self, PyObject *args)
233{
Martin v. Löwisf3548942007-11-12 04:53:02 +0000234 PyObject *result = set_hook("completion_display_matches_hook",
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000235 &completion_display_matches_hook, args);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000236#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
237 /* We cannot set this hook globally, since it replaces the
238 default completion display. */
239 rl_completion_display_matches_hook =
Christian Heimes1bc4af42007-11-12 18:58:08 +0000240 completion_display_matches_hook ?
Martin v. Löwis7f08c1f2008-11-04 20:43:31 +0000241#if defined(_RL_FUNCTION_TYPEDEF)
Martin v. Löwisf3548942007-11-12 04:53:02 +0000242 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwis7f08c1f2008-11-04 20:43:31 +0000243#else
244 (VFunction *)on_completion_display_matches_hook : 0;
245#endif
Martin v. Löwisf3548942007-11-12 04:53:02 +0000246#endif
247 return result;
248
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000249}
250
251PyDoc_STRVAR(doc_set_completion_display_matches_hook,
252"set_completion_display_matches_hook([function]) -> None\n\
253Set or remove the completion display function.\n\
254The function is called as\n\
255 function(substitution, [matches], longest_match_length)\n\
256once each time matches need to be displayed.");
257
258static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000259set_startup_hook(PyObject *self, PyObject *args)
260{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000261 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000262}
263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000264PyDoc_STRVAR(doc_set_startup_hook,
265"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000266Set or remove the startup_hook function.\n\
267The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000268before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000269
Guido van Rossum74f31432003-01-07 20:01:29 +0000270
Martin v. Löwis0daad592001-09-30 21:09:59 +0000271#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000272
273/* Set pre-input hook */
274
Martin v. Löwis0daad592001-09-30 21:09:59 +0000275static PyObject *
276set_pre_input_hook(PyObject *self, PyObject *args)
277{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000278 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000279}
280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000281PyDoc_STRVAR(doc_set_pre_input_hook,
282"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000283Set or remove the pre_input_hook function.\n\
284The function is called with no arguments after the first prompt\n\
285has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000286characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000287
Martin v. Löwis0daad592001-09-30 21:09:59 +0000288#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000289
Guido van Rossum74f31432003-01-07 20:01:29 +0000290
Guido van Rossum290900a1997-09-26 21:51:21 +0000291/* Exported function to specify a word completer in Python */
292
293static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000294
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000295static PyObject *begidx = NULL;
296static PyObject *endidx = NULL;
297
Guido van Rossum74f31432003-01-07 20:01:29 +0000298
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000299/* Get the completion type for the scope of the tab-completion */
300static PyObject *
301get_completion_type(PyObject *self, PyObject *noarg)
302{
303 return PyInt_FromLong(rl_completion_type);
304}
305
306PyDoc_STRVAR(doc_get_completion_type,
307"get_completion_type() -> int\n\
308Get the type of completion being attempted.");
309
310
Guido van Rossum74f31432003-01-07 20:01:29 +0000311/* Get the beginning index for the scope of the tab-completion */
312
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000313static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000314get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000315{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000316 Py_INCREF(begidx);
317 return begidx;
318}
319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000320PyDoc_STRVAR(doc_get_begidx,
321"get_begidx() -> int\n\
322get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000323
Guido van Rossum74f31432003-01-07 20:01:29 +0000324
325/* Get the ending index for the scope of the tab-completion */
326
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000327static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000328get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000329{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000330 Py_INCREF(endidx);
331 return endidx;
332}
333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000334PyDoc_STRVAR(doc_get_endidx,
335"get_endidx() -> int\n\
336get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000337
338
Guido van Rossum74f31432003-01-07 20:01:29 +0000339/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000340
341static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000342set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000343{
344 char *break_chars;
345
Guido van Rossum43713e52000-02-29 13:59:29 +0000346 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000347 return NULL;
348 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000349 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000350 rl_completer_word_break_characters = strdup(break_chars);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000351 Py_RETURN_NONE;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000352}
353
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000354PyDoc_STRVAR(doc_set_completer_delims,
355"set_completer_delims(string) -> None\n\
356set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000357
Skip Montanaroe5069012004-08-15 14:32:06 +0000358static PyObject *
359py_remove_history(PyObject *self, PyObject *args)
360{
Christian Heimes1bc4af42007-11-12 18:58:08 +0000361 int entry_number;
362 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000363
Christian Heimes1bc4af42007-11-12 18:58:08 +0000364 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
365 return NULL;
366 if (entry_number < 0) {
367 PyErr_SetString(PyExc_ValueError,
368 "History index cannot be negative");
369 return NULL;
370 }
371 entry = remove_history(entry_number);
372 if (!entry) {
373 PyErr_Format(PyExc_ValueError,
374 "No history item at position %d",
375 entry_number);
376 return NULL;
377 }
378 /* free memory allocated for the history entry */
379 if (entry->line)
380 free(entry->line);
381 if (entry->data)
382 free(entry->data);
383 free(entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000384
Christian Heimes1bc4af42007-11-12 18:58:08 +0000385 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000386}
387
388PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000389"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000390remove history item given by its position");
391
392static PyObject *
393py_replace_history(PyObject *self, PyObject *args)
394{
Christian Heimes1bc4af42007-11-12 18:58:08 +0000395 int entry_number;
396 char *line;
397 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000398
Christian Heimes1bc4af42007-11-12 18:58:08 +0000399 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
400 &line)) {
401 return NULL;
402 }
403 if (entry_number < 0) {
404 PyErr_SetString(PyExc_ValueError,
405 "History index cannot be negative");
406 return NULL;
407 }
408 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
409 if (!old_entry) {
410 PyErr_Format(PyExc_ValueError,
411 "No history item at position %d",
412 entry_number);
413 return NULL;
414 }
415 /* free memory allocated for the old history entry */
416 if (old_entry->line)
417 free(old_entry->line);
418 if (old_entry->data)
419 free(old_entry->data);
420 free(old_entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000421
Christian Heimes1bc4af42007-11-12 18:58:08 +0000422 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000423}
424
425PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000426"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000427replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000428
429/* Add a line to the history buffer */
430
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000431static PyObject *
432py_add_history(PyObject *self, PyObject *args)
433{
434 char *line;
435
436 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
437 return NULL;
438 }
439 add_history(line);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000440 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000441}
442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000443PyDoc_STRVAR(doc_add_history,
444"add_history(string) -> None\n\
445add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000446
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000447
Guido van Rossum74f31432003-01-07 20:01:29 +0000448/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000449
450static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000451get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000452{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000453 return PyString_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000454}
Guido van Rossum74f31432003-01-07 20:01:29 +0000455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000456PyDoc_STRVAR(doc_get_completer_delims,
457"get_completer_delims() -> string\n\
458get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000459
Guido van Rossum74f31432003-01-07 20:01:29 +0000460
461/* Set the completer function */
462
Guido van Rossum290900a1997-09-26 21:51:21 +0000463static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000464set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000465{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000466 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000467}
468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000469PyDoc_STRVAR(doc_set_completer,
470"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000471Set or remove the completer function.\n\
472The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000473for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000474It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000475
Guido van Rossum74f31432003-01-07 20:01:29 +0000476
Michael W. Hudson796df152003-01-30 10:12:51 +0000477static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000478get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000479{
480 if (completer == NULL) {
Christian Heimes1bc4af42007-11-12 18:58:08 +0000481 Py_RETURN_NONE;
Michael W. Hudson796df152003-01-30 10:12:51 +0000482 }
483 Py_INCREF(completer);
484 return completer;
485}
486
487PyDoc_STRVAR(doc_get_completer,
488"get_completer() -> function\n\
489\n\
490Returns current completer function.");
491
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000492/* Exported function to get any element of history */
493
494static PyObject *
495get_history_item(PyObject *self, PyObject *args)
496{
497 int idx = 0;
498 HIST_ENTRY *hist_ent;
499
500 if (!PyArg_ParseTuple(args, "i:index", &idx))
501 return NULL;
Ronald Oussoren8ed66ed2010-02-07 20:04:45 +0000502#ifdef __APPLE__
503 if (using_libedit_emulation) {
504 /* Libedit emulation uses 0-based indexes,
505 * the real one uses 1-based indexes,
506 * adjust the index to ensure that Python
507 * code doesn't have to worry about the
508 * difference.
509 */
510 HISTORY_STATE *hist_st;
511 hist_st = history_get_history_state();
512
513 idx --;
514
515 /*
516 * Apple's readline emulation crashes when
517 * the index is out of range, therefore
518 * test for that and fail gracefully.
519 */
520 if (idx < 0 || idx >= hist_st->length) {
521 Py_RETURN_NONE;
522 }
523 }
524#endif /* __APPLE__ */
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000525 if ((hist_ent = history_get(idx)))
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000526 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000527 else {
Christian Heimes1bc4af42007-11-12 18:58:08 +0000528 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000529 }
530}
531
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000532PyDoc_STRVAR(doc_get_history_item,
533"get_history_item() -> string\n\
534return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000535
Guido van Rossum74f31432003-01-07 20:01:29 +0000536
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000537/* Exported function to get current length of history */
538
539static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000540get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000541{
542 HISTORY_STATE *hist_st;
543
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000544 hist_st = history_get_history_state();
545 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
546}
547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000548PyDoc_STRVAR(doc_get_current_history_length,
549"get_current_history_length() -> integer\n\
550return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000551
Guido van Rossum74f31432003-01-07 20:01:29 +0000552
Guido van Rossum79378ff1997-10-07 14:53:21 +0000553/* Exported function to read the current line buffer */
554
555static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000556get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000557{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000558 return PyString_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000559}
560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000561PyDoc_STRVAR(doc_get_line_buffer,
562"get_line_buffer() -> string\n\
563return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000564
Guido van Rossum74f31432003-01-07 20:01:29 +0000565
Martin v. Löwise7a97962003-09-20 16:08:33 +0000566#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
567
568/* Exported function to clear the current history */
569
570static PyObject *
571py_clear_history(PyObject *self, PyObject *noarg)
572{
573 clear_history();
Christian Heimes1bc4af42007-11-12 18:58:08 +0000574 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000575}
576
577PyDoc_STRVAR(doc_clear_history,
578"clear_history() -> None\n\
579Clear the current readline history.");
580#endif
581
582
Guido van Rossum79378ff1997-10-07 14:53:21 +0000583/* Exported function to insert text into the line buffer */
584
585static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000586insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000587{
588 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000589 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000590 return NULL;
591 rl_insert_text(s);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000592 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000593}
594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000595PyDoc_STRVAR(doc_insert_text,
596"insert_text(string) -> None\n\
597Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000598
Guido van Rossum74f31432003-01-07 20:01:29 +0000599
600/* Redisplay the line buffer */
601
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000602static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000603redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000604{
605 rl_redisplay();
Christian Heimes1bc4af42007-11-12 18:58:08 +0000606 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000607}
608
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000609PyDoc_STRVAR(doc_redisplay,
610"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000611Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000612contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000613
Guido van Rossum74f31432003-01-07 20:01:29 +0000614
Guido van Rossum290900a1997-09-26 21:51:21 +0000615/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000616
617static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000618{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000619 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000620 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000621 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000622 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000623 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000624 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000625 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000626 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000627 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000628 {"get_history_item", get_history_item,
629 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000630 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000631 METH_NOARGS, doc_get_current_history_length},
Christian Heimes1bc4af42007-11-12 18:58:08 +0000632 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000633 METH_VARARGS, set_history_length_doc},
Christian Heimes1bc4af42007-11-12 18:58:08 +0000634 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000635 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000636 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000637 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000638 {"get_completion_type", get_completion_type,
639 METH_NOARGS, doc_get_completion_type},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000640 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
641 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000642
Guido van Rossum74f31432003-01-07 20:01:29 +0000643 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000644 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000645 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Christian Heimes1bc4af42007-11-12 18:58:08 +0000646 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
647 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000648 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000649 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000650
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000651 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
652 METH_VARARGS, doc_set_completion_display_matches_hook},
Guido van Rossum74f31432003-01-07 20:01:29 +0000653 {"set_startup_hook", set_startup_hook,
654 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000655#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000656 {"set_pre_input_hook", set_pre_input_hook,
657 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000658#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000659#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
660 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
661#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000662 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000663};
664
Guido van Rossum05ac4492003-01-07 20:04:12 +0000665
Martin v. Löwis0daad592001-09-30 21:09:59 +0000666/* C function to call the Python hooks. */
667
668static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000669on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000670{
671 int result = 0;
672 if (func != NULL) {
673 PyObject *r;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000674#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000675 PyGILState_STATE gilstate = PyGILState_Ensure();
676#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000677 r = PyObject_CallFunction(func, NULL);
678 if (r == NULL)
679 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000680 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000681 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000682 else {
Martin v. Löwis0daad592001-09-30 21:09:59 +0000683 result = PyInt_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000684 if (result == -1 && PyErr_Occurred())
685 goto error;
686 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000687 Py_DECREF(r);
688 goto done;
689 error:
690 PyErr_Clear();
691 Py_XDECREF(r);
692 done:
Christian Heimes1bc4af42007-11-12 18:58:08 +0000693#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000694 PyGILState_Release(gilstate);
695#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000696 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000697 }
698 return result;
699}
700
701static int
702on_startup_hook(void)
703{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000704 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000705}
706
707#ifdef HAVE_RL_PRE_INPUT_HOOK
708static int
709on_pre_input_hook(void)
710{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000711 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000712}
713#endif
714
Guido van Rossum05ac4492003-01-07 20:04:12 +0000715
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000716/* C function to call the Python completion_display_matches */
717
718static void
719on_completion_display_matches_hook(char **matches,
720 int num_matches, int max_length)
721{
Martin v. Löwisf3548942007-11-12 04:53:02 +0000722 int i;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000723 PyObject *m=NULL, *s=NULL, *r=NULL;
724#ifdef WITH_THREAD
Martin v. Löwisf3548942007-11-12 04:53:02 +0000725 PyGILState_STATE gilstate = PyGILState_Ensure();
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000726#endif
Martin v. Löwisf3548942007-11-12 04:53:02 +0000727 m = PyList_New(num_matches);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000728 if (m == NULL)
729 goto error;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000730 for (i = 0; i < num_matches; i++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000731 s = PyString_FromString(matches[i+1]);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000732 if (s == NULL)
733 goto error;
734 if (PyList_SetItem(m, i, s) == -1)
735 goto error;
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000736 }
Martin v. Löwisf3548942007-11-12 04:53:02 +0000737
738 r = PyObject_CallFunction(completion_display_matches_hook,
739 "sOi", matches[0], m, max_length);
740
Christian Heimes1bc4af42007-11-12 18:58:08 +0000741 Py_DECREF(m), m=NULL;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000742
743 if (r == NULL ||
744 (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
745 goto error;
746 }
Christian Heimes1bc4af42007-11-12 18:58:08 +0000747 Py_XDECREF(r), r=NULL;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000748
Christian Heimes1bc4af42007-11-12 18:58:08 +0000749 if (0) {
750 error:
751 PyErr_Clear();
752 Py_XDECREF(m);
753 Py_XDECREF(r);
754 }
755#ifdef WITH_THREAD
Martin v. Löwisf3548942007-11-12 04:53:02 +0000756 PyGILState_Release(gilstate);
757#endif
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000758}
759
760
Guido van Rossum290900a1997-09-26 21:51:21 +0000761/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000762
Guido van Rossum290900a1997-09-26 21:51:21 +0000763static char *
Neal Norwitzbd538702007-04-19 05:52:37 +0000764on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000765{
Guido van Rossum290900a1997-09-26 21:51:21 +0000766 char *result = NULL;
767 if (completer != NULL) {
768 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000769#ifdef WITH_THREAD
770 PyGILState_STATE gilstate = PyGILState_Ensure();
771#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000772 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000773 r = PyObject_CallFunction(completer, "si", text, state);
774 if (r == NULL)
775 goto error;
776 if (r == Py_None) {
777 result = NULL;
778 }
779 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000780 char *s = PyString_AsString(r);
Guido van Rossum290900a1997-09-26 21:51:21 +0000781 if (s == NULL)
782 goto error;
783 result = strdup(s);
784 }
785 Py_DECREF(r);
786 goto done;
787 error:
788 PyErr_Clear();
789 Py_XDECREF(r);
790 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000791#ifdef WITH_THREAD
792 PyGILState_Release(gilstate);
793#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000794 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000795 }
796 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000797}
798
Guido van Rossum290900a1997-09-26 21:51:21 +0000799
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000800/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000801 * before calling the normal completer */
802
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000803static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000804flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000805{
Antoine Pitrou632e93f2009-10-27 12:30:12 +0000806#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
807 rl_completion_append_character ='\0';
808#endif
809#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
810 rl_completion_suppress_append = 0;
811#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000812 Py_XDECREF(begidx);
813 Py_XDECREF(endidx);
814 begidx = PyInt_FromLong((long) start);
815 endidx = PyInt_FromLong((long) end);
816 return completion_matches(text, *on_completion);
817}
818
Guido van Rossum05ac4492003-01-07 20:04:12 +0000819
Guido van Rossum290900a1997-09-26 21:51:21 +0000820/* Helper to initialize GNU readline properly. */
821
822static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000823setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000824{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000825#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000826 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000827 if (!saved_locale)
828 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000829#endif
830
Skip Montanaroa0392742002-06-11 14:32:46 +0000831 using_history();
832
Guido van Rossum290900a1997-09-26 21:51:21 +0000833 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000834#if defined(PYOS_OS2) && defined(PYCC_GCC)
835 /* Allow $if term= in .inputrc to work */
836 rl_terminal_name = getenv("TERM");
837#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000838 /* Force rebind of TAB to insert-tab */
839 rl_bind_key('\t', rl_insert);
840 /* Bind both ESC-TAB and ESC-ESC to the completion function */
841 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
842 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000843 /* Set our hook functions */
844 rl_startup_hook = (Function *)on_startup_hook;
845#ifdef HAVE_RL_PRE_INPUT_HOOK
846 rl_pre_input_hook = (Function *)on_pre_input_hook;
847#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000848 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000849 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000850 /* Set Python word break characters */
851 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000852 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000853 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000854
855 begidx = PyInt_FromLong(0L);
856 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000857 /* Initialize (allows .inputrc to override)
858 *
859 * XXX: A bug in the readline-2.2 library causes a memory leak
860 * inside this function. Nothing we can do about it.
861 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000862 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000863
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000864 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000865}
866
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000867/* Wrapper around GNU readline that handles signals differently. */
868
869
870#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
871
872static char *completed_input_string;
873static void
874rlhandler(char *text)
875{
876 completed_input_string = text;
877 rl_callback_handler_remove();
878}
879
880extern PyThreadState* _PyOS_ReadlineTState;
881
882static char *
883readline_until_enter_or_signal(char *prompt, int *signal)
884{
885 char * not_done_reading = "";
886 fd_set selectset;
887
888 *signal = 0;
889#ifdef HAVE_RL_CATCH_SIGNAL
890 rl_catch_signals = 0;
891#endif
892
893 rl_callback_handler_install (prompt, rlhandler);
894 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000895
896 completed_input_string = not_done_reading;
897
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000898 while (completed_input_string == not_done_reading) {
899 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000900
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000901 while (!has_input)
902 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000903
904 /* [Bug #1552726] Only limit the pause if an input hook has been
905 defined. */
906 struct timeval *timeoutp = NULL;
907 if (PyOS_InputHook)
908 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000909 FD_SET(fileno(rl_instream), &selectset);
910 /* select resets selectset if no input was available */
911 has_input = select(fileno(rl_instream) + 1, &selectset,
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000912 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000913 if(PyOS_InputHook) PyOS_InputHook();
914 }
915
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000916 if(has_input > 0) {
917 rl_callback_read_char();
918 }
919 else if (errno == EINTR) {
920 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000921#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000922 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000923#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000924 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000925#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000926 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000927#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000928 if (s < 0) {
929 rl_free_line_state();
930 rl_cleanup_after_signal();
931 rl_callback_handler_remove();
932 *signal = 1;
933 completed_input_string = NULL;
934 }
935 }
936 }
937
938 return completed_input_string;
939}
940
941
942#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000943
944/* Interrupt handler */
945
946static jmp_buf jbuf;
947
Guido van Rossum0969d361997-08-05 21:27:50 +0000948/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000949static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000950onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000951{
Guido van Rossum290900a1997-09-26 21:51:21 +0000952 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000953}
954
Guido van Rossum290900a1997-09-26 21:51:21 +0000955
Guido van Rossum0969d361997-08-05 21:27:50 +0000956static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000957readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000958{
Guido van Rossum174efc92000-09-16 16:37:53 +0000959 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000960 char *p;
961
962 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000963
Guido van Rossum174efc92000-09-16 16:37:53 +0000964 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000965 if (setjmp(jbuf)) {
966#ifdef HAVE_SIGRELSE
967 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
968 sigrelse(SIGINT);
969#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000970 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000971 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000972 return NULL;
973 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000974 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000975 p = readline(prompt);
976 PyOS_setsig(SIGINT, old_inthandler);
977
978 return p;
979}
980#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
981
982
983static char *
984call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
985{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000986 size_t n;
987 char *p, *q;
988 int signal;
989
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000990#ifdef SAVE_LOCALE
991 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000992 if (!saved_locale)
993 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000994 setlocale(LC_CTYPE, "");
995#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000996
Guido van Rossum74f31432003-01-07 20:01:29 +0000997 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
998 rl_instream = sys_stdin;
999 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001000#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +00001001 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +00001002#endif
Guido van Rossum74f31432003-01-07 20:01:29 +00001003 }
1004
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001005 p = readline_until_enter_or_signal(prompt, &signal);
1006
1007 /* we got an interrupt signal */
Neal Norwitz5eaf7722006-07-16 02:15:27 +00001008 if (signal) {
1009 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001010 return NULL;
1011 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001012
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001013 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +00001014 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001015 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +00001016 if (p != NULL)
1017 *p = '\0';
Neal Norwitz5eaf7722006-07-16 02:15:27 +00001018 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +00001019 return p;
1020 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001021
1022 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +00001023 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +00001024 if (n > 0) {
1025 char *line;
1026 HISTORY_STATE *state = history_get_history_state();
1027 if (state->length > 0)
Ronald Oussoren8ed66ed2010-02-07 20:04:45 +00001028#ifdef __APPLE__
1029 if (using_libedit_emulation) {
1030 /*
1031 * Libedit's emulation uses 0-based indexes,
1032 * the real readline uses 1-based indexes.
1033 */
1034 line = history_get(state->length - 1)->line;
1035 } else
1036#endif /* __APPLE__ */
Skip Montanaroa0392742002-06-11 14:32:46 +00001037 line = history_get(state->length)->line;
1038 else
1039 line = "";
1040 if (strcmp(p, line))
1041 add_history(p);
1042 /* the history docs don't say so, but the address of state
1043 changes each time history_get_history_state is called
1044 which makes me think it's freshly malloc'd memory...
1045 on the other hand, the address of the last line stays the
1046 same as long as history isn't extended, so it appears to
1047 be malloc'd but managed by the history package... */
1048 free(state);
1049 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001050 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1051 release the original. */
1052 q = p;
1053 p = PyMem_Malloc(n+2);
1054 if (p != NULL) {
1055 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +00001056 p[n] = '\n';
1057 p[n+1] = '\0';
1058 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001059 free(q);
Neal Norwitz5eaf7722006-07-16 02:15:27 +00001060 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +00001061 return p;
1062}
1063
Guido van Rossum290900a1997-09-26 21:51:21 +00001064
1065/* Initialize the module */
1066
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001067PyDoc_STRVAR(doc_module,
1068"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001069
Ronald Oussoren8ed66ed2010-02-07 20:04:45 +00001070#ifdef __APPLE__
1071PyDoc_STRVAR(doc_module_le,
1072"Importing this module enables command line editing using libedit readline.");
1073#endif /* __APPLE__ */
1074
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001075PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001076initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001077{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +00001078 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001079
Ronald Oussoren8ed66ed2010-02-07 20:04:45 +00001080#ifdef __APPLE__
1081 if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
1082 using_libedit_emulation = 1;
1083 }
1084
1085 if (using_libedit_emulation)
1086 m = Py_InitModule4("readline", readline_methods, doc_module_le,
1087 (PyObject *)NULL, PYTHON_API_VERSION);
1088 else
1089
1090#endif /* __APPLE__ */
1091
Guido van Rossum290900a1997-09-26 21:51:21 +00001092 m = Py_InitModule4("readline", readline_methods, doc_module,
1093 (PyObject *)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001094 if (m == NULL)
1095 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001096
Ronald Oussoren8ed66ed2010-02-07 20:04:45 +00001097
1098
Guido van Rossum74f31432003-01-07 20:01:29 +00001099 PyOS_ReadlineFunctionPointer = call_readline;
1100 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +00001101}