blob: e187129dfa34b25244964dc343742c909ad8144c [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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000026# define RESTORE_LOCALE(sl)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000027#endif
28
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000036 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,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000047 int num_matches, int max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +000048
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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000055 char *s, *copy;
56 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
57 return NULL;
58 /* 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 */
66 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079 char *s = NULL;
80 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
81 return NULL;
82 errno = rl_read_init_file(s);
83 if (errno)
84 return PyErr_SetFromErrno(PyExc_IOError);
85 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000099 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);
105 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000120 char *s = NULL;
121 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
122 return NULL;
123 errno = write_history(s);
124 if (!errno && _history_length >= 0)
125 history_truncate_file(s, _history_length);
126 if (errno)
127 return PyErr_SetFromErrno(PyExc_IOError);
128 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000142 int length = _history_length;
143 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
144 return NULL;
145 _history_length = length;
146 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000175 PyObject *function = Py_None;
176 char buf[80];
177 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
178 if (!PyArg_ParseTuple(args, buf, &function))
179 return NULL;
180 if (function == Py_None) {
181 Py_XDECREF(*hook_var);
182 *hook_var = NULL;
183 }
184 else if (PyCallable_Check(function)) {
185 PyObject *tmp = *hook_var;
186 Py_INCREF(function);
187 *hook_var = function;
188 Py_XDECREF(tmp);
189 }
190 else {
191 PyOS_snprintf(buf, sizeof(buf),
192 "set_%.50s(func): argument not callable",
193 funcname);
194 PyErr_SetString(PyExc_TypeError, buf);
195 return NULL;
196 }
197 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000213 PyObject *result = set_hook("completion_display_matches_hook",
214 &completion_display_matches_hook, args);
Christian Heimes32fbe592007-11-12 15:01:33 +0000215#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216 /* We cannot set this hook globally, since it replaces the
217 default completion display. */
218 rl_completion_display_matches_hook =
219 completion_display_matches_hook ?
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000220#if defined(_RL_FUNCTION_TYPEDEF)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000221 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000222#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000223 (VFunction *)on_completion_display_matches_hook : 0;
Martin v. Löwisb37509b2008-11-04 20:45:29 +0000224#endif
Christian Heimes32fbe592007-11-12 15:01:33 +0000225#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000226 return result;
Christian Heimes32fbe592007-11-12 15:01:33 +0000227
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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000295 Py_INCREF(begidx);
296 return begidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000297}
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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000309 Py_INCREF(endidx);
310 return endidx;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000311}
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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 char *break_chars;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000324
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000325 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
326 return NULL;
327 }
328 free((void*)rl_completer_word_break_characters);
329 rl_completer_word_break_characters = strdup(break_chars);
330 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
Mark Dickinson856e44d2010-08-03 16:14:09 +0000337/* _py_free_history_entry: Utility function to free a history entry. */
338
339#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
340
341/* Readline version >= 5.0 introduced a timestamp field into the history entry
342 structure; this needs to be freed to avoid a memory leak. This version of
343 readline also introduced the handy 'free_history_entry' function, which
344 takes care of the timestamp. */
345
346static void
347_py_free_history_entry(HIST_ENTRY *entry)
348{
349 histdata_t data = free_history_entry(entry);
350 free(data);
351}
352
353#else
354
355/* No free_history_entry function; free everything manually. */
356
357static void
358_py_free_history_entry(HIST_ENTRY *entry)
359{
360 if (entry->line)
361 free((void *)entry->line);
362 if (entry->data)
363 free(entry->data);
364 free(entry);
365}
366
367#endif
368
Skip Montanaroe5069012004-08-15 14:32:06 +0000369static PyObject *
370py_remove_history(PyObject *self, PyObject *args)
371{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000372 int entry_number;
373 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000374
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000375 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
376 return NULL;
377 if (entry_number < 0) {
378 PyErr_SetString(PyExc_ValueError,
379 "History index cannot be negative");
380 return NULL;
381 }
382 entry = remove_history(entry_number);
383 if (!entry) {
384 PyErr_Format(PyExc_ValueError,
385 "No history item at position %d",
386 entry_number);
387 return NULL;
388 }
389 /* free memory allocated for the history entry */
Mark Dickinson856e44d2010-08-03 16:14:09 +0000390 _py_free_history_entry(entry);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000391 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000392}
393
394PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000395"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000396remove history item given by its position");
397
398static PyObject *
399py_replace_history(PyObject *self, PyObject *args)
400{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000401 int entry_number;
402 char *line;
403 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000404
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000405 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
406 &line)) {
407 return NULL;
408 }
409 if (entry_number < 0) {
410 PyErr_SetString(PyExc_ValueError,
411 "History index cannot be negative");
412 return NULL;
413 }
414 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
415 if (!old_entry) {
416 PyErr_Format(PyExc_ValueError,
417 "No history item at position %d",
418 entry_number);
419 return NULL;
420 }
421 /* free memory allocated for the old history entry */
Mark Dickinson856e44d2010-08-03 16:14:09 +0000422 _py_free_history_entry(old_entry);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000423 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000424}
425
426PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000427"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000428replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000429
430/* Add a line to the history buffer */
431
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000432static PyObject *
433py_add_history(PyObject *self, PyObject *args)
434{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000435 char *line;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000436
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000437 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
438 return NULL;
439 }
440 add_history(line);
441 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000442}
443
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000444PyDoc_STRVAR(doc_add_history,
445"add_history(string) -> None\n\
446add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000447
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000448
Guido van Rossum74f31432003-01-07 20:01:29 +0000449/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000450
451static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000452get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000453{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000454 return PyUnicode_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000455}
Guido van Rossum74f31432003-01-07 20:01:29 +0000456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000457PyDoc_STRVAR(doc_get_completer_delims,
458"get_completer_delims() -> string\n\
459get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000460
Guido van Rossum74f31432003-01-07 20:01:29 +0000461
462/* Set the completer function */
463
Guido van Rossum290900a1997-09-26 21:51:21 +0000464static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000465set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000466{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000467 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000468}
469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000470PyDoc_STRVAR(doc_set_completer,
471"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000472Set or remove the completer function.\n\
473The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000474for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000475It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000476
Guido van Rossum74f31432003-01-07 20:01:29 +0000477
Michael W. Hudson796df152003-01-30 10:12:51 +0000478static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000479get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000480{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000481 if (completer == NULL) {
482 Py_RETURN_NONE;
483 }
484 Py_INCREF(completer);
485 return completer;
Michael W. Hudson796df152003-01-30 10:12:51 +0000486}
487
488PyDoc_STRVAR(doc_get_completer,
489"get_completer() -> function\n\
490\n\
491Returns current completer function.");
492
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000493/* Exported function to get any element of history */
494
495static PyObject *
496get_history_item(PyObject *self, PyObject *args)
497{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000498 int idx = 0;
499 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000500
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000501 if (!PyArg_ParseTuple(args, "i:index", &idx))
502 return NULL;
503 if ((hist_ent = history_get(idx)))
504 return PyUnicode_FromString(hist_ent->line);
505 else {
506 Py_RETURN_NONE;
507 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000508}
509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000510PyDoc_STRVAR(doc_get_history_item,
511"get_history_item() -> string\n\
512return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000513
Guido van Rossum74f31432003-01-07 20:01:29 +0000514
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000515/* Exported function to get current length of history */
516
517static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000518get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000519{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000520 HISTORY_STATE *hist_st;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000521
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000522 hist_st = history_get_history_state();
523 return PyLong_FromLong(hist_st ? (long) hist_st->length : (long) 0);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000524}
525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000526PyDoc_STRVAR(doc_get_current_history_length,
527"get_current_history_length() -> integer\n\
528return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000529
Guido van Rossum74f31432003-01-07 20:01:29 +0000530
Guido van Rossum79378ff1997-10-07 14:53:21 +0000531/* Exported function to read the current line buffer */
532
533static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000534get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000535{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000536 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000537}
538
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000539PyDoc_STRVAR(doc_get_line_buffer,
540"get_line_buffer() -> string\n\
541return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000542
Guido van Rossum74f31432003-01-07 20:01:29 +0000543
Martin v. Löwise7a97962003-09-20 16:08:33 +0000544#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
545
546/* Exported function to clear the current history */
547
548static PyObject *
549py_clear_history(PyObject *self, PyObject *noarg)
550{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000551 clear_history();
552 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000553}
554
555PyDoc_STRVAR(doc_clear_history,
556"clear_history() -> None\n\
557Clear the current readline history.");
558#endif
559
560
Guido van Rossum79378ff1997-10-07 14:53:21 +0000561/* Exported function to insert text into the line buffer */
562
563static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000564insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000565{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000566 char *s;
567 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
568 return NULL;
569 rl_insert_text(s);
570 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000571}
572
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000573PyDoc_STRVAR(doc_insert_text,
574"insert_text(string) -> None\n\
575Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000576
Guido van Rossum74f31432003-01-07 20:01:29 +0000577
578/* Redisplay the line buffer */
579
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000580static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000581redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000582{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000583 rl_redisplay();
584 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000585}
586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000587PyDoc_STRVAR(doc_redisplay,
588"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000589Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000590contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000591
Guido van Rossum74f31432003-01-07 20:01:29 +0000592
Guido van Rossum290900a1997-09-26 21:51:21 +0000593/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000594
595static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000596{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000597 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
598 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
599 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
600 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
601 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
602 {"read_history_file", read_history_file,
603 METH_VARARGS, doc_read_history_file},
604 {"write_history_file", write_history_file,
605 METH_VARARGS, doc_write_history_file},
606 {"get_history_item", get_history_item,
607 METH_VARARGS, doc_get_history_item},
608 {"get_current_history_length", (PyCFunction)get_current_history_length,
609 METH_NOARGS, doc_get_current_history_length},
610 {"set_history_length", set_history_length,
611 METH_VARARGS, set_history_length_doc},
612 {"get_history_length", get_history_length,
613 METH_NOARGS, get_history_length_doc},
614 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
615 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
616 {"get_completion_type", get_completion_type,
617 METH_NOARGS, doc_get_completion_type},
618 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
619 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000620
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000621 {"set_completer_delims", set_completer_delims,
622 METH_VARARGS, doc_set_completer_delims},
623 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
624 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
625 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
626 {"get_completer_delims", get_completer_delims,
627 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000628
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000629 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
630 METH_VARARGS, doc_set_completion_display_matches_hook},
631 {"set_startup_hook", set_startup_hook,
632 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000633#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000634 {"set_pre_input_hook", set_pre_input_hook,
635 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000636#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000637#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000638 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000639#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000640 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000641};
642
Guido van Rossum05ac4492003-01-07 20:04:12 +0000643
Martin v. Löwis0daad592001-09-30 21:09:59 +0000644/* C function to call the Python hooks. */
645
646static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000647on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000648{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000649 int result = 0;
650 if (func != NULL) {
651 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000652#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000653 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000654#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000655 r = PyObject_CallFunction(func, NULL);
656 if (r == NULL)
657 goto error;
658 if (r == Py_None)
659 result = 0;
660 else {
661 result = PyLong_AsLong(r);
662 if (result == -1 && PyErr_Occurred())
663 goto error;
664 }
665 Py_DECREF(r);
666 goto done;
667 error:
668 PyErr_Clear();
669 Py_XDECREF(r);
670 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000671#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000672 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000673#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000674 return result;
675 }
676 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000677}
678
679static int
680on_startup_hook(void)
681{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000682 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000683}
684
685#ifdef HAVE_RL_PRE_INPUT_HOOK
686static int
687on_pre_input_hook(void)
688{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000689 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000690}
691#endif
692
Guido van Rossum05ac4492003-01-07 20:04:12 +0000693
Thomas Wouters89d996e2007-09-08 17:39:28 +0000694/* C function to call the Python completion_display_matches */
695
696static void
697on_completion_display_matches_hook(char **matches,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000698 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000699{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000700 int i;
701 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000702#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000703 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000704#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000705 m = PyList_New(num_matches);
706 if (m == NULL)
707 goto error;
708 for (i = 0; i < num_matches; i++) {
709 s = PyUnicode_FromString(matches[i+1]);
710 if (s == NULL)
711 goto error;
712 if (PyList_SetItem(m, i, s) == -1)
713 goto error;
714 }
715 r = PyObject_CallFunction(completion_display_matches_hook,
716 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000717
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000718 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000719
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000720 if (r == NULL ||
721 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
722 goto error;
723 }
724 Py_XDECREF(r); r=NULL;
725
726 if (0) {
727 error:
728 PyErr_Clear();
729 Py_XDECREF(m);
730 Py_XDECREF(r);
731 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000732#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000733 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000734#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000735}
736
737
Guido van Rossum290900a1997-09-26 21:51:21 +0000738/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000739
Guido van Rossum290900a1997-09-26 21:51:21 +0000740static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000741on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000742{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000743 char *result = NULL;
744 if (completer != NULL) {
745 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000746#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000747 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000748#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000749 rl_attempted_completion_over = 1;
750 r = PyObject_CallFunction(completer, "si", text, state);
751 if (r == NULL)
752 goto error;
753 if (r == Py_None) {
754 result = NULL;
755 }
756 else {
757 char *s = _PyUnicode_AsString(r);
758 if (s == NULL)
759 goto error;
760 result = strdup(s);
761 }
762 Py_DECREF(r);
763 goto done;
764 error:
765 PyErr_Clear();
766 Py_XDECREF(r);
767 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000768#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000769 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000770#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000771 return result;
772 }
773 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000774}
775
Guido van Rossum290900a1997-09-26 21:51:21 +0000776
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000777/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000778 * before calling the normal completer */
779
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000780static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000781flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000782{
Antoine Pitroue566bda2009-10-19 18:24:35 +0000783#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000784 rl_completion_append_character ='\0';
Antoine Pitrou37276002009-10-26 19:32:51 +0000785#endif
786#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000787 rl_completion_suppress_append = 0;
Antoine Pitroue566bda2009-10-19 18:24:35 +0000788#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000789 Py_XDECREF(begidx);
790 Py_XDECREF(endidx);
791 begidx = PyLong_FromLong((long) start);
792 endidx = PyLong_FromLong((long) end);
793 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000794}
795
Guido van Rossum05ac4492003-01-07 20:04:12 +0000796
Guido van Rossum290900a1997-09-26 21:51:21 +0000797/* Helper to initialize GNU readline properly. */
798
799static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000800setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000801{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000802#ifdef SAVE_LOCALE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000803 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
804 if (!saved_locale)
805 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000806#endif
807
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000808 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000809
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000810 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000811#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000812 /* Allow $if term= in .inputrc to work */
813 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000814#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000815 /* Force rebind of TAB to insert-tab */
816 rl_bind_key('\t', rl_insert);
817 /* Bind both ESC-TAB and ESC-ESC to the completion function */
818 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
819 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
820 /* Set our hook functions */
821 rl_startup_hook = (Function *)on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000822#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000823 rl_pre_input_hook = (Function *)on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000824#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000825 /* Set our completion function */
826 rl_attempted_completion_function = (CPPFunction *)flex_complete;
827 /* Set Python word break characters */
828 rl_completer_word_break_characters =
829 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
830 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000831
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000832 begidx = PyLong_FromLong(0L);
833 endidx = PyLong_FromLong(0L);
834 /* Initialize (allows .inputrc to override)
835 *
836 * XXX: A bug in the readline-2.2 library causes a memory leak
837 * inside this function. Nothing we can do about it.
838 */
839 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000840
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000841 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000842}
843
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000844/* Wrapper around GNU readline that handles signals differently. */
845
846
847#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
848
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000849static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000850static void
851rlhandler(char *text)
852{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000853 completed_input_string = text;
854 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000855}
856
857extern PyThreadState* _PyOS_ReadlineTState;
858
859static char *
860readline_until_enter_or_signal(char *prompt, int *signal)
861{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000862 char * not_done_reading = "";
863 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000864
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000865 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000866#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000867 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000868#endif
869
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000870 rl_callback_handler_install (prompt, rlhandler);
871 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000872
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000873 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000874
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000875 while (completed_input_string == not_done_reading) {
876 int has_input = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000877
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 while (!has_input)
879 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000880
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 /* [Bug #1552726] Only limit the pause if an input hook has been
882 defined. */
883 struct timeval *timeoutp = NULL;
884 if (PyOS_InputHook)
885 timeoutp = &timeout;
886 FD_SET(fileno(rl_instream), &selectset);
887 /* select resets selectset if no input was available */
888 has_input = select(fileno(rl_instream) + 1, &selectset,
889 NULL, NULL, timeoutp);
890 if(PyOS_InputHook) PyOS_InputHook();
891 }
892
893 if(has_input > 0) {
894 rl_callback_read_char();
895 }
896 else if (errno == EINTR) {
897 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000898#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000899 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000900#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000901 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000902#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000903 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000904#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000905 if (s < 0) {
906 rl_free_line_state();
907 rl_cleanup_after_signal();
908 rl_callback_handler_remove();
909 *signal = 1;
910 completed_input_string = NULL;
911 }
912 }
913 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000914
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000915 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000916}
917
918
919#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000920
921/* Interrupt handler */
922
923static jmp_buf jbuf;
924
Guido van Rossum0969d361997-08-05 21:27:50 +0000925/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000926static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000927onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000928{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000929 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000930}
931
Guido van Rossum290900a1997-09-26 21:51:21 +0000932
Guido van Rossum0969d361997-08-05 21:27:50 +0000933static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000934readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000935{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000936 PyOS_sighandler_t old_inthandler;
937 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +0000938
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000939 *signal = 0;
940
941 old_inthandler = PyOS_setsig(SIGINT, onintr);
942 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +0000943#ifdef HAVE_SIGRELSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000944 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
945 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +0000946#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000947 PyOS_setsig(SIGINT, old_inthandler);
948 *signal = 1;
949 return NULL;
950 }
951 rl_event_hook = PyOS_InputHook;
952 p = readline(prompt);
953 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000954
955 return p;
956}
957#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
958
959
960static char *
961call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
962{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000963 size_t n;
964 char *p, *q;
965 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000966
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000967#ifdef SAVE_LOCALE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000968 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
969 if (!saved_locale)
970 Py_FatalError("not enough memory to save locale");
971 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000972#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000973
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000974 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
975 rl_instream = sys_stdin;
976 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000977#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000978 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000979#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000980 }
Guido van Rossum74f31432003-01-07 20:01:29 +0000981
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000982 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000983
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000984 /* we got an interrupt signal */
985 if (signal) {
986 RESTORE_LOCALE(saved_locale)
987 return NULL;
988 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000989
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000990 /* We got an EOF, return a empty string. */
991 if (p == NULL) {
992 p = PyMem_Malloc(1);
993 if (p != NULL)
994 *p = '\0';
995 RESTORE_LOCALE(saved_locale)
996 return p;
997 }
998
999 /* we have a valid line */
1000 n = strlen(p);
1001 if (n > 0) {
1002 char *line;
1003 HISTORY_STATE *state = history_get_history_state();
1004 if (state->length > 0)
1005 line = history_get(state->length)->line;
1006 else
1007 line = "";
1008 if (strcmp(p, line))
1009 add_history(p);
1010 /* the history docs don't say so, but the address of state
1011 changes each time history_get_history_state is called
1012 which makes me think it's freshly malloc'd memory...
1013 on the other hand, the address of the last line stays the
1014 same as long as history isn't extended, so it appears to
1015 be malloc'd but managed by the history package... */
1016 free(state);
1017 }
1018 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1019 release the original. */
1020 q = p;
1021 p = PyMem_Malloc(n+2);
1022 if (p != NULL) {
1023 strncpy(p, q, n);
1024 p[n] = '\n';
1025 p[n+1] = '\0';
1026 }
1027 free(q);
1028 RESTORE_LOCALE(saved_locale)
1029 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001030}
1031
Guido van Rossum290900a1997-09-26 21:51:21 +00001032
1033/* Initialize the module */
1034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001035PyDoc_STRVAR(doc_module,
1036"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001037
Martin v. Löwis1a214512008-06-11 05:26:20 +00001038
1039static struct PyModuleDef readlinemodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001040 PyModuleDef_HEAD_INIT,
1041 "readline",
1042 doc_module,
1043 -1,
1044 readline_methods,
1045 NULL,
1046 NULL,
1047 NULL,
1048 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001049};
1050
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001051PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001052PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001053{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001054 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001055
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001056 m = PyModule_Create(&readlinemodule);
1057 if (m == NULL)
1058 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001059
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001060 PyOS_ReadlineFunctionPointer = call_readline;
1061 setup_readline();
1062 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001063}