blob: 8925d8da004c42ccfebc33f13121b0195eef0c99 [file] [log] [blame]
Guido van Rossum290900a1997-09-26 21:51:21 +00001/* This module makes GNU readline available to Python. It has ideas
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
Michael W. Hudson9a8c3142005-03-30 10:09:12 +00003 * Center. The completer interface was inspired by Lele Gaifax. More
4 * recently, it was largely rewritten by Guido van Rossum.
Guido van Rossum0969d361997-08-05 21:27:50 +00005 */
6
Guido van Rossum290900a1997-09-26 21:51:21 +00007/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +00008#include "Python.h"
9#include <setjmp.h>
10#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000011#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000012#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000013
Skip Montanaro7befb992004-02-10 16:50:21 +000014#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000015/* GNU readline() mistakenly sets the LC_CTYPE locale.
16 * This is evil. Only the user or the app's main() should do this!
17 * We must save and restore the locale around the rl_initialize() call.
18 */
19#define SAVE_LOCALE
20#include <locale.h>
21#endif
22
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
26# define RESTORE_LOCALE(sl)
27#endif
28
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
36 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossumd8faa362007-04-27 19:54:29 +000037#else
Martin v. Löwisb37509b2008-11-04 20:45:29 +000038#if defined(_RL_FUNCTION_TYPEDEF)
Guido van Rossumd8faa362007-04-27 19:54:29 +000039extern char **completion_matches(char *, rl_compentry_func_t *);
Martin v. Löwisb37509b2008-11-04 20:45:29 +000040#else
41extern char **completion_matches(char *, CPFunction *);
42#endif
Guido van Rossum353ae582001-07-10 16:45:32 +000043#endif
44
Christian Heimes32fbe592007-11-12 15:01:33 +000045static void
46on_completion_display_matches_hook(char **matches,
47 int num_matches, int max_length);
48
Guido van Rossum0969d361997-08-05 21:27:50 +000049
Guido van Rossum290900a1997-09-26 21:51:21 +000050/* Exported function to send one line to readline's init file parser */
51
52static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000053parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000054{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000055 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000056 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000057 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000058 /* Make a copy -- rl_parse_and_bind() modifies its argument */
59 /* Bernard Herzog */
60 copy = malloc(1 + strlen(s));
61 if (copy == NULL)
62 return PyErr_NoMemory();
63 strcpy(copy, s);
64 rl_parse_and_bind(copy);
65 free(copy); /* Free the copy */
Guido van Rossum3d392eb2007-11-16 00:35:22 +000066 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000067}
68
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000069PyDoc_STRVAR(doc_parse_and_bind,
70"parse_and_bind(string) -> None\n\
71Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000072
73
74/* Exported function to parse a readline init file */
75
76static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000077read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000078{
79 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000080 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000081 return NULL;
82 errno = rl_read_init_file(s);
83 if (errno)
84 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3d392eb2007-11-16 00:35:22 +000085 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000086}
87
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000088PyDoc_STRVAR(doc_read_init_file,
89"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000090Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000091The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000092
93
Skip Montanaro28067822000-07-06 18:55:12 +000094/* Exported function to load a readline history file */
95
96static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000097read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000098{
99 char *s = NULL;
100 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
101 return NULL;
102 errno = read_history(s);
103 if (errno)
104 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000105 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000106}
107
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000108static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000109PyDoc_STRVAR(doc_read_history_file,
110"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000111Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000112The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000113
114
115/* Exported function to save a readline history file */
116
117static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000118write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000119{
120 char *s = NULL;
121 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
122 return NULL;
123 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000124 if (!errno && _history_length >= 0)
125 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000126 if (errno)
127 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000128 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000129}
130
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000131PyDoc_STRVAR(doc_write_history_file,
132"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000133Save 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
Guido van Rossum74f31432003-01-07 20:01:29 +0000137/* Set history length */
138
139static PyObject*
140set_history_length(PyObject *self, PyObject *args)
141{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000142 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000143 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
144 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000145 _history_length = length;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000146 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000147}
148
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000149PyDoc_STRVAR(set_history_length_doc,
150"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151set the maximal number of items which will be written to\n\
152the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000154
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000155
Guido van Rossum74f31432003-01-07 20:01:29 +0000156/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000157
158static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000159get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000160{
Christian Heimes217cfd12007-12-02 14:31:20 +0000161 return PyLong_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000162}
163
Guido van Rossum74f31432003-01-07 20:01:29 +0000164PyDoc_STRVAR(get_history_length_doc,
165"get_history_length() -> int\n\
166return the maximum number of items that will be written to\n\
167the history file.");
168
169
Martin v. Löwis0daad592001-09-30 21:09:59 +0000170/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000171
Martin v. Löwis0daad592001-09-30 21:09:59 +0000172static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000173set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000174{
175 PyObject *function = Py_None;
176 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000177 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000178 if (!PyArg_ParseTuple(args, buf, &function))
179 return NULL;
180 if (function == Py_None) {
181 Py_XDECREF(*hook_var);
182 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000183 }
184 else if (PyCallable_Check(function)) {
185 PyObject *tmp = *hook_var;
186 Py_INCREF(function);
187 *hook_var = function;
188 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000189 }
190 else {
Tim Peters885d4572001-11-28 20:27:42 +0000191 PyOS_snprintf(buf, sizeof(buf),
192 "set_%.50s(func): argument not callable",
193 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000194 PyErr_SetString(PyExc_TypeError, buf);
195 return NULL;
196 }
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000197 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000198}
199
Guido van Rossum74f31432003-01-07 20:01:29 +0000200
Martin v. Löwis0daad592001-09-30 21:09:59 +0000201/* Exported functions to specify hook functions in Python */
202
Thomas Wouters89d996e2007-09-08 17:39:28 +0000203static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000204static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000205
206#ifdef HAVE_RL_PRE_INPUT_HOOK
207static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000208#endif
209
210static PyObject *
Thomas Wouters89d996e2007-09-08 17:39:28 +0000211set_completion_display_matches_hook(PyObject *self, PyObject *args)
212{
Christian Heimes32fbe592007-11-12 15:01:33 +0000213 PyObject *result = set_hook("completion_display_matches_hook",
Thomas Wouters89d996e2007-09-08 17:39:28 +0000214 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000215#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
216 /* We cannot set this hook globally, since it replaces the
217 default completion display. */
218 rl_completion_display_matches_hook =
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000219 completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000220#if defined(_RL_FUNCTION_TYPEDEF)
Christian Heimes32fbe592007-11-12 15:01:33 +0000221 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000222#else
223 (VFunction *)on_completion_display_matches_hook : 0;
224#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000225#endif
226 return result;
227
Thomas Wouters89d996e2007-09-08 17:39:28 +0000228}
229
230PyDoc_STRVAR(doc_set_completion_display_matches_hook,
231"set_completion_display_matches_hook([function]) -> None\n\
232Set or remove the completion display function.\n\
233The function is called as\n\
234 function(substitution, [matches], longest_match_length)\n\
235once each time matches need to be displayed.");
236
237static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000238set_startup_hook(PyObject *self, PyObject *args)
239{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000240 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000241}
242
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000243PyDoc_STRVAR(doc_set_startup_hook,
244"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000245Set or remove the startup_hook function.\n\
246The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000247before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000248
Guido van Rossum74f31432003-01-07 20:01:29 +0000249
Martin v. Löwis0daad592001-09-30 21:09:59 +0000250#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000251
252/* Set pre-input hook */
253
Martin v. Löwis0daad592001-09-30 21:09:59 +0000254static PyObject *
255set_pre_input_hook(PyObject *self, PyObject *args)
256{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000257 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000258}
259
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000260PyDoc_STRVAR(doc_set_pre_input_hook,
261"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000262Set or remove the pre_input_hook function.\n\
263The function is called with no arguments after the first prompt\n\
264has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000265characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000266
Martin v. Löwis0daad592001-09-30 21:09:59 +0000267#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000268
Guido van Rossum74f31432003-01-07 20:01:29 +0000269
Guido van Rossum290900a1997-09-26 21:51:21 +0000270/* Exported function to specify a word completer in Python */
271
272static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000273
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000274static PyObject *begidx = NULL;
275static PyObject *endidx = NULL;
276
Guido van Rossum74f31432003-01-07 20:01:29 +0000277
Thomas Wouters89d996e2007-09-08 17:39:28 +0000278/* Get the completion type for the scope of the tab-completion */
279static PyObject *
280get_completion_type(PyObject *self, PyObject *noarg)
281{
Christian Heimes217cfd12007-12-02 14:31:20 +0000282 return PyLong_FromLong(rl_completion_type);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000283}
284
285PyDoc_STRVAR(doc_get_completion_type,
286"get_completion_type() -> int\n\
287Get the type of completion being attempted.");
288
289
Guido van Rossum74f31432003-01-07 20:01:29 +0000290/* Get the beginning index for the scope of the tab-completion */
291
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000292static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000293get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000294{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000295 Py_INCREF(begidx);
296 return begidx;
297}
298
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000299PyDoc_STRVAR(doc_get_begidx,
300"get_begidx() -> int\n\
301get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000302
Guido van Rossum74f31432003-01-07 20:01:29 +0000303
304/* Get the ending index for the scope of the tab-completion */
305
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000306static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000307get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000308{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000309 Py_INCREF(endidx);
310 return endidx;
311}
312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000313PyDoc_STRVAR(doc_get_endidx,
314"get_endidx() -> int\n\
315get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000316
317
Guido van Rossum74f31432003-01-07 20:01:29 +0000318/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000319
320static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000321set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000322{
323 char *break_chars;
324
Guido van Rossum43713e52000-02-29 13:59:29 +0000325 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000326 return NULL;
327 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000328 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000329 rl_completer_word_break_characters = strdup(break_chars);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000330 Py_RETURN_NONE;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000331}
332
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000333PyDoc_STRVAR(doc_set_completer_delims,
334"set_completer_delims(string) -> None\n\
335set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000336
Skip Montanaroe5069012004-08-15 14:32:06 +0000337static PyObject *
338py_remove_history(PyObject *self, PyObject *args)
339{
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000340 int entry_number;
341 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000342
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000343 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
344 return NULL;
345 if (entry_number < 0) {
346 PyErr_SetString(PyExc_ValueError,
347 "History index cannot be negative");
348 return NULL;
349 }
350 entry = remove_history(entry_number);
351 if (!entry) {
352 PyErr_Format(PyExc_ValueError,
353 "No history item at position %d",
354 entry_number);
355 return NULL;
356 }
357 /* free memory allocated for the history entry */
358 if (entry->line)
359 free(entry->line);
360 if (entry->data)
361 free(entry->data);
362 free(entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000363
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000364 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000365}
366
367PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000368"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000369remove history item given by its position");
370
371static PyObject *
372py_replace_history(PyObject *self, PyObject *args)
373{
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000374 int entry_number;
375 char *line;
376 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000377
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000378 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
379 &line)) {
380 return NULL;
381 }
382 if (entry_number < 0) {
383 PyErr_SetString(PyExc_ValueError,
384 "History index cannot be negative");
385 return NULL;
386 }
387 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
388 if (!old_entry) {
389 PyErr_Format(PyExc_ValueError,
390 "No history item at position %d",
391 entry_number);
392 return NULL;
393 }
394 /* free memory allocated for the old history entry */
395 if (old_entry->line)
396 free(old_entry->line);
397 if (old_entry->data)
398 free(old_entry->data);
399 free(old_entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000400
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000401 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000402}
403
404PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000405"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000406replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000407
408/* Add a line to the history buffer */
409
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000410static PyObject *
411py_add_history(PyObject *self, PyObject *args)
412{
413 char *line;
414
415 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
416 return NULL;
417 }
418 add_history(line);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000419 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000420}
421
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000422PyDoc_STRVAR(doc_add_history,
423"add_history(string) -> None\n\
424add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000425
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000426
Guido van Rossum74f31432003-01-07 20:01:29 +0000427/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000428
429static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000430get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000431{
Christian Heimesaec75c32007-11-11 22:42:36 +0000432 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000433}
Guido van Rossum74f31432003-01-07 20:01:29 +0000434
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000435PyDoc_STRVAR(doc_get_completer_delims,
436"get_completer_delims() -> string\n\
437get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000438
Guido van Rossum74f31432003-01-07 20:01:29 +0000439
440/* Set the completer function */
441
Guido van Rossum290900a1997-09-26 21:51:21 +0000442static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000443set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000444{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000445 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000446}
447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000448PyDoc_STRVAR(doc_set_completer,
449"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000450Set or remove the completer function.\n\
451The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000452for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000453It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000454
Guido van Rossum74f31432003-01-07 20:01:29 +0000455
Michael W. Hudson796df152003-01-30 10:12:51 +0000456static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000457get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000458{
459 if (completer == NULL) {
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000460 Py_RETURN_NONE;
Michael W. Hudson796df152003-01-30 10:12:51 +0000461 }
462 Py_INCREF(completer);
463 return completer;
464}
465
466PyDoc_STRVAR(doc_get_completer,
467"get_completer() -> function\n\
468\n\
469Returns current completer function.");
470
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000471/* Exported function to get any element of history */
472
473static PyObject *
474get_history_item(PyObject *self, PyObject *args)
475{
476 int idx = 0;
477 HIST_ENTRY *hist_ent;
478
479 if (!PyArg_ParseTuple(args, "i:index", &idx))
480 return NULL;
481 if ((hist_ent = history_get(idx)))
Christian Heimesaec75c32007-11-11 22:42:36 +0000482 return PyUnicode_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000483 else {
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000484 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000485 }
486}
487
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000488PyDoc_STRVAR(doc_get_history_item,
489"get_history_item() -> string\n\
490return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000491
Guido van Rossum74f31432003-01-07 20:01:29 +0000492
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000493/* Exported function to get current length of history */
494
495static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000496get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000497{
498 HISTORY_STATE *hist_st;
499
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000500 hist_st = history_get_history_state();
Christian Heimes217cfd12007-12-02 14:31:20 +0000501 return PyLong_FromLong(hist_st ? (long) hist_st->length : (long) 0);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000502}
503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000504PyDoc_STRVAR(doc_get_current_history_length,
505"get_current_history_length() -> integer\n\
506return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000507
Guido van Rossum74f31432003-01-07 20:01:29 +0000508
Guido van Rossum79378ff1997-10-07 14:53:21 +0000509/* Exported function to read the current line buffer */
510
511static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000512get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000513{
Christian Heimesaec75c32007-11-11 22:42:36 +0000514 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000515}
516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517PyDoc_STRVAR(doc_get_line_buffer,
518"get_line_buffer() -> string\n\
519return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000520
Guido van Rossum74f31432003-01-07 20:01:29 +0000521
Martin v. Löwise7a97962003-09-20 16:08:33 +0000522#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
523
524/* Exported function to clear the current history */
525
526static PyObject *
527py_clear_history(PyObject *self, PyObject *noarg)
528{
529 clear_history();
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000530 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000531}
532
533PyDoc_STRVAR(doc_clear_history,
534"clear_history() -> None\n\
535Clear the current readline history.");
536#endif
537
538
Guido van Rossum79378ff1997-10-07 14:53:21 +0000539/* Exported function to insert text into the line buffer */
540
541static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000542insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000543{
544 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000545 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000546 return NULL;
547 rl_insert_text(s);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000548 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000549}
550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000551PyDoc_STRVAR(doc_insert_text,
552"insert_text(string) -> None\n\
553Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000554
Guido van Rossum74f31432003-01-07 20:01:29 +0000555
556/* Redisplay the line buffer */
557
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000558static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000559redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000560{
561 rl_redisplay();
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000562 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000563}
564
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000565PyDoc_STRVAR(doc_redisplay,
566"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000567Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000568contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000569
Guido van Rossum74f31432003-01-07 20:01:29 +0000570
Guido van Rossum290900a1997-09-26 21:51:21 +0000571/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000572
573static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000574{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000575 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000576 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000577 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000578 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000579 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000580 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000581 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000582 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000583 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000584 {"get_history_item", get_history_item,
585 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000586 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000587 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000588 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000589 METH_VARARGS, set_history_length_doc},
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000590 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000591 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000592 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000593 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Thomas Wouters89d996e2007-09-08 17:39:28 +0000594 {"get_completion_type", get_completion_type,
595 METH_NOARGS, doc_get_completion_type},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000596 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
597 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000598
Guido van Rossum74f31432003-01-07 20:01:29 +0000599 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000600 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000601 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000602 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
603 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000604 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000605 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000606
Thomas Wouters89d996e2007-09-08 17:39:28 +0000607 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
608 METH_VARARGS, doc_set_completion_display_matches_hook},
Guido van Rossum74f31432003-01-07 20:01:29 +0000609 {"set_startup_hook", set_startup_hook,
610 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000611#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000612 {"set_pre_input_hook", set_pre_input_hook,
613 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000614#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000615#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
616 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
617#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000618 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000619};
620
Guido van Rossum05ac4492003-01-07 20:04:12 +0000621
Martin v. Löwis0daad592001-09-30 21:09:59 +0000622/* C function to call the Python hooks. */
623
624static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000625on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000626{
627 int result = 0;
628 if (func != NULL) {
629 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000630#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000631 PyGILState_STATE gilstate = PyGILState_Ensure();
632#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000633 r = PyObject_CallFunction(func, NULL);
634 if (r == NULL)
635 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000636 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000637 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000638 else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000639 result = PyLong_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000640 if (result == -1 && PyErr_Occurred())
641 goto error;
642 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000643 Py_DECREF(r);
644 goto done;
645 error:
646 PyErr_Clear();
647 Py_XDECREF(r);
648 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000649#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000650 PyGILState_Release(gilstate);
651#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000652 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000653 }
654 return result;
655}
656
657static int
658on_startup_hook(void)
659{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000660 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000661}
662
663#ifdef HAVE_RL_PRE_INPUT_HOOK
664static int
665on_pre_input_hook(void)
666{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000667 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000668}
669#endif
670
Guido van Rossum05ac4492003-01-07 20:04:12 +0000671
Thomas Wouters89d996e2007-09-08 17:39:28 +0000672/* C function to call the Python completion_display_matches */
673
674static void
675on_completion_display_matches_hook(char **matches,
676 int num_matches, int max_length)
677{
Christian Heimes32fbe592007-11-12 15:01:33 +0000678 int i;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000679 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000680#ifdef WITH_THREAD
Christian Heimes32fbe592007-11-12 15:01:33 +0000681 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000682#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000683 m = PyList_New(num_matches);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000684 if (m == NULL)
685 goto error;
Christian Heimes32fbe592007-11-12 15:01:33 +0000686 for (i = 0; i < num_matches; i++) {
687 s = PyUnicode_FromString(matches[i+1]);
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000688 if (s == NULL)
Christian Heimes32fbe592007-11-12 15:01:33 +0000689 goto error;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000690 if (PyList_SetItem(m, i, s) == -1)
691 goto error;
Thomas Wouters89d996e2007-09-08 17:39:28 +0000692 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000693 r = PyObject_CallFunction(completion_display_matches_hook,
694 "sOi", matches[0], m, max_length);
695
Matthias Klose091c7b12009-04-07 13:24:27 +0000696 Py_DECREF(m); m=NULL;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000697
Christian Heimes32fbe592007-11-12 15:01:33 +0000698 if (r == NULL ||
Christian Heimes217cfd12007-12-02 14:31:20 +0000699 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
Christian Heimes32fbe592007-11-12 15:01:33 +0000700 goto error;
701 }
Matthias Klose091c7b12009-04-07 13:24:27 +0000702 Py_XDECREF(r); r=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000703
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000704 if (0) {
705 error:
706 PyErr_Clear();
707 Py_XDECREF(m);
708 Py_XDECREF(r);
709 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000710#ifdef WITH_THREAD
711 PyGILState_Release(gilstate);
712#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000713}
714
715
Guido van Rossum290900a1997-09-26 21:51:21 +0000716/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000717
Guido van Rossum290900a1997-09-26 21:51:21 +0000718static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000719on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000720{
Guido van Rossum290900a1997-09-26 21:51:21 +0000721 char *result = NULL;
722 if (completer != NULL) {
723 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000724#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000725 PyGILState_STATE gilstate = PyGILState_Ensure();
726#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000727 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000728 r = PyObject_CallFunction(completer, "si", text, state);
729 if (r == NULL)
730 goto error;
731 if (r == Py_None) {
732 result = NULL;
733 }
734 else {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000735 char *s = _PyUnicode_AsString(r);
Guido van Rossum290900a1997-09-26 21:51:21 +0000736 if (s == NULL)
737 goto error;
738 result = strdup(s);
739 }
740 Py_DECREF(r);
741 goto done;
742 error:
743 PyErr_Clear();
744 Py_XDECREF(r);
745 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000746#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000747 PyGILState_Release(gilstate);
748#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000749 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000750 }
751 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000752}
753
Guido van Rossum290900a1997-09-26 21:51:21 +0000754
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000755/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000756 * before calling the normal completer */
757
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000758static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000759flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000760{
761 Py_XDECREF(begidx);
762 Py_XDECREF(endidx);
Christian Heimes217cfd12007-12-02 14:31:20 +0000763 begidx = PyLong_FromLong((long) start);
764 endidx = PyLong_FromLong((long) end);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000765 return completion_matches(text, *on_completion);
766}
767
Guido van Rossum05ac4492003-01-07 20:04:12 +0000768
Guido van Rossum290900a1997-09-26 21:51:21 +0000769/* Helper to initialize GNU readline properly. */
770
771static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000772setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000773{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000774#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000775 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000776 if (!saved_locale)
777 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000778#endif
779
Skip Montanaroa0392742002-06-11 14:32:46 +0000780 using_history();
781
Guido van Rossum290900a1997-09-26 21:51:21 +0000782 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000783#if defined(PYOS_OS2) && defined(PYCC_GCC)
784 /* Allow $if term= in .inputrc to work */
785 rl_terminal_name = getenv("TERM");
786#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000787 /* Force rebind of TAB to insert-tab */
788 rl_bind_key('\t', rl_insert);
789 /* Bind both ESC-TAB and ESC-ESC to the completion function */
790 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
791 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000792 /* Set our hook functions */
793 rl_startup_hook = (Function *)on_startup_hook;
794#ifdef HAVE_RL_PRE_INPUT_HOOK
795 rl_pre_input_hook = (Function *)on_pre_input_hook;
796#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000797 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000798 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000799 /* Set Python word break characters */
800 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000801 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000802 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000803#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000804 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000805#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000806
Christian Heimes217cfd12007-12-02 14:31:20 +0000807 begidx = PyLong_FromLong(0L);
808 endidx = PyLong_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000809 /* Initialize (allows .inputrc to override)
810 *
811 * XXX: A bug in the readline-2.2 library causes a memory leak
812 * inside this function. Nothing we can do about it.
813 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000814 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000815
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000816 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000817}
818
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000819/* Wrapper around GNU readline that handles signals differently. */
820
821
822#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
823
824static char *completed_input_string;
825static void
826rlhandler(char *text)
827{
828 completed_input_string = text;
829 rl_callback_handler_remove();
830}
831
832extern PyThreadState* _PyOS_ReadlineTState;
833
834static char *
835readline_until_enter_or_signal(char *prompt, int *signal)
836{
837 char * not_done_reading = "";
838 fd_set selectset;
839
840 *signal = 0;
841#ifdef HAVE_RL_CATCH_SIGNAL
842 rl_catch_signals = 0;
843#endif
844
845 rl_callback_handler_install (prompt, rlhandler);
846 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000847
848 completed_input_string = not_done_reading;
849
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000850 while (completed_input_string == not_done_reading) {
851 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000852
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000853 while (!has_input)
854 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000855
856 /* [Bug #1552726] Only limit the pause if an input hook has been
857 defined. */
858 struct timeval *timeoutp = NULL;
859 if (PyOS_InputHook)
860 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000861 FD_SET(fileno(rl_instream), &selectset);
862 /* select resets selectset if no input was available */
863 has_input = select(fileno(rl_instream) + 1, &selectset,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000864 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000865 if(PyOS_InputHook) PyOS_InputHook();
866 }
867
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000868 if(has_input > 0) {
869 rl_callback_read_char();
870 }
871 else if (errno == EINTR) {
872 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000873#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000874 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000875#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000876 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000877#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000878 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000879#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000880 if (s < 0) {
881 rl_free_line_state();
882 rl_cleanup_after_signal();
883 rl_callback_handler_remove();
884 *signal = 1;
885 completed_input_string = NULL;
886 }
887 }
888 }
889
890 return completed_input_string;
891}
892
893
894#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000895
896/* Interrupt handler */
897
898static jmp_buf jbuf;
899
Guido van Rossum0969d361997-08-05 21:27:50 +0000900/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000901static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000902onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000903{
Guido van Rossum290900a1997-09-26 21:51:21 +0000904 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000905}
906
Guido van Rossum290900a1997-09-26 21:51:21 +0000907
Guido van Rossum0969d361997-08-05 21:27:50 +0000908static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000909readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000910{
Guido van Rossum174efc92000-09-16 16:37:53 +0000911 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000912 char *p;
913
914 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000915
Guido van Rossum174efc92000-09-16 16:37:53 +0000916 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000917 if (setjmp(jbuf)) {
918#ifdef HAVE_SIGRELSE
919 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
920 sigrelse(SIGINT);
921#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000922 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000923 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000924 return NULL;
925 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000926 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000927 p = readline(prompt);
928 PyOS_setsig(SIGINT, old_inthandler);
929
930 return p;
931}
932#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
933
934
935static char *
936call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
937{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000938 size_t n;
939 char *p, *q;
940 int signal;
941
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000942#ifdef SAVE_LOCALE
943 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000944 if (!saved_locale)
945 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000946 setlocale(LC_CTYPE, "");
947#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000948
Guido van Rossum74f31432003-01-07 20:01:29 +0000949 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
950 rl_instream = sys_stdin;
951 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000952#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000953 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000954#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000955 }
956
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000957 p = readline_until_enter_or_signal(prompt, &signal);
958
959 /* we got an interrupt signal */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000960 if (signal) {
961 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000962 return NULL;
963 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000964
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000965 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000966 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000967 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000968 if (p != NULL)
969 *p = '\0';
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000970 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000971 return p;
972 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000973
974 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000975 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000976 if (n > 0) {
977 char *line;
978 HISTORY_STATE *state = history_get_history_state();
979 if (state->length > 0)
980 line = history_get(state->length)->line;
981 else
982 line = "";
983 if (strcmp(p, line))
984 add_history(p);
985 /* the history docs don't say so, but the address of state
986 changes each time history_get_history_state is called
987 which makes me think it's freshly malloc'd memory...
988 on the other hand, the address of the last line stays the
989 same as long as history isn't extended, so it appears to
990 be malloc'd but managed by the history package... */
991 free(state);
992 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000993 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
994 release the original. */
995 q = p;
996 p = PyMem_Malloc(n+2);
997 if (p != NULL) {
998 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000999 p[n] = '\n';
1000 p[n+1] = '\0';
1001 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001002 free(q);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001003 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +00001004 return p;
1005}
1006
Guido van Rossum290900a1997-09-26 21:51:21 +00001007
1008/* Initialize the module */
1009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001010PyDoc_STRVAR(doc_module,
1011"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001012
Martin v. Löwis1a214512008-06-11 05:26:20 +00001013
1014static struct PyModuleDef readlinemodule = {
1015 PyModuleDef_HEAD_INIT,
1016 "readline",
1017 doc_module,
1018 -1,
1019 readline_methods,
1020 NULL,
1021 NULL,
1022 NULL,
1023 NULL
1024};
1025
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001026PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001027PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001028{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +00001029 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001030
Martin v. Löwis1a214512008-06-11 05:26:20 +00001031 m = PyModule_Create(&readlinemodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001032 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001033 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001034
Guido van Rossum74f31432003-01-07 20:01:29 +00001035 PyOS_ReadlineFunctionPointer = call_readline;
1036 setup_readline();
Martin v. Löwis1a214512008-06-11 05:26:20 +00001037 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001038}