blob: 53c6de2c5c746463eb45fa3b0b985f3b36287324 [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
Mark Dickinson29c72b72010-08-03 16:52:23 +0000493/* Private function to get current length of history. XXX It may be
494 * possible to replace this with a direct use of history_length instead,
495 * but it's not clear whether BSD's libedit keeps history_length up to date.
496 * See issue #8065.*/
497
498static int
499_py_get_history_length(void)
500{
501 HISTORY_STATE *hist_st = history_get_history_state();
502 int length = hist_st->length;
503 /* the history docs don't say so, but the address of hist_st changes each
504 time history_get_history_state is called which makes me think it's
505 freshly malloc'd memory... on the other hand, the address of the last
506 line stays the same as long as history isn't extended, so it appears to
507 be malloc'd but managed by the history package... */
508 free(hist_st);
509 return length;
510}
511
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000512/* Exported function to get any element of history */
513
514static PyObject *
515get_history_item(PyObject *self, PyObject *args)
516{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000517 int idx = 0;
518 HIST_ENTRY *hist_ent;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000519
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000520 if (!PyArg_ParseTuple(args, "i:index", &idx))
521 return NULL;
522 if ((hist_ent = history_get(idx)))
523 return PyUnicode_FromString(hist_ent->line);
524 else {
525 Py_RETURN_NONE;
526 }
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000527}
528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000529PyDoc_STRVAR(doc_get_history_item,
530"get_history_item() -> string\n\
531return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000532
Guido van Rossum74f31432003-01-07 20:01:29 +0000533
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000534/* Exported function to get current length of history */
535
536static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000537get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000538{
Mark Dickinson29c72b72010-08-03 16:52:23 +0000539 return PyLong_FromLong((long)_py_get_history_length());
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000540}
541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000542PyDoc_STRVAR(doc_get_current_history_length,
543"get_current_history_length() -> integer\n\
544return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000545
Guido van Rossum74f31432003-01-07 20:01:29 +0000546
Guido van Rossum79378ff1997-10-07 14:53:21 +0000547/* Exported function to read the current line buffer */
548
549static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000550get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000551{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000552 return PyUnicode_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000553}
554
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000555PyDoc_STRVAR(doc_get_line_buffer,
556"get_line_buffer() -> string\n\
557return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000558
Guido van Rossum74f31432003-01-07 20:01:29 +0000559
Martin v. Löwise7a97962003-09-20 16:08:33 +0000560#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
561
562/* Exported function to clear the current history */
563
564static PyObject *
565py_clear_history(PyObject *self, PyObject *noarg)
566{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000567 clear_history();
568 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000569}
570
571PyDoc_STRVAR(doc_clear_history,
572"clear_history() -> None\n\
573Clear the current readline history.");
574#endif
575
576
Guido van Rossum79378ff1997-10-07 14:53:21 +0000577/* Exported function to insert text into the line buffer */
578
579static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000580insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000581{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000582 char *s;
583 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
584 return NULL;
585 rl_insert_text(s);
586 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000587}
588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000589PyDoc_STRVAR(doc_insert_text,
590"insert_text(string) -> None\n\
591Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000592
Guido van Rossum74f31432003-01-07 20:01:29 +0000593
594/* Redisplay the line buffer */
595
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000596static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000597redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000598{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000599 rl_redisplay();
600 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000601}
602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000603PyDoc_STRVAR(doc_redisplay,
604"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000605Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000606contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000607
Guido van Rossum74f31432003-01-07 20:01:29 +0000608
Guido van Rossum290900a1997-09-26 21:51:21 +0000609/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000610
611static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000612{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000613 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
614 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
615 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
616 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
617 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
618 {"read_history_file", read_history_file,
619 METH_VARARGS, doc_read_history_file},
620 {"write_history_file", write_history_file,
621 METH_VARARGS, doc_write_history_file},
622 {"get_history_item", get_history_item,
623 METH_VARARGS, doc_get_history_item},
624 {"get_current_history_length", (PyCFunction)get_current_history_length,
625 METH_NOARGS, doc_get_current_history_length},
626 {"set_history_length", set_history_length,
627 METH_VARARGS, set_history_length_doc},
628 {"get_history_length", get_history_length,
629 METH_NOARGS, get_history_length_doc},
630 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
631 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
632 {"get_completion_type", get_completion_type,
633 METH_NOARGS, doc_get_completion_type},
634 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
635 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000636
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000637 {"set_completer_delims", set_completer_delims,
638 METH_VARARGS, doc_set_completer_delims},
639 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
640 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
641 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
642 {"get_completer_delims", get_completer_delims,
643 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000644
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000645 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
646 METH_VARARGS, doc_set_completion_display_matches_hook},
647 {"set_startup_hook", set_startup_hook,
648 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000649#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 {"set_pre_input_hook", set_pre_input_hook,
651 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000652#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000653#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000654 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
Martin v. Löwise7a97962003-09-20 16:08:33 +0000655#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000656 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000657};
658
Guido van Rossum05ac4492003-01-07 20:04:12 +0000659
Martin v. Löwis0daad592001-09-30 21:09:59 +0000660/* C function to call the Python hooks. */
661
662static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000663on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000664{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000665 int result = 0;
666 if (func != NULL) {
667 PyObject *r;
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000668#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000669 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000670#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000671 r = PyObject_CallFunction(func, NULL);
672 if (r == NULL)
673 goto error;
674 if (r == Py_None)
675 result = 0;
676 else {
677 result = PyLong_AsLong(r);
678 if (result == -1 && PyErr_Occurred())
679 goto error;
680 }
681 Py_DECREF(r);
682 goto done;
683 error:
684 PyErr_Clear();
685 Py_XDECREF(r);
686 done:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000687#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000688 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000689#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000690 return result;
691 }
692 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000693}
694
695static int
696on_startup_hook(void)
697{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000698 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000699}
700
701#ifdef HAVE_RL_PRE_INPUT_HOOK
702static int
703on_pre_input_hook(void)
704{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000705 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000706}
707#endif
708
Guido van Rossum05ac4492003-01-07 20:04:12 +0000709
Thomas Wouters89d996e2007-09-08 17:39:28 +0000710/* C function to call the Python completion_display_matches */
711
712static void
713on_completion_display_matches_hook(char **matches,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000714 int num_matches, int max_length)
Thomas Wouters89d996e2007-09-08 17:39:28 +0000715{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000716 int i;
717 PyObject *m=NULL, *s=NULL, *r=NULL;
Christian Heimesaec75c32007-11-11 22:42:36 +0000718#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000719 PyGILState_STATE gilstate = PyGILState_Ensure();
Thomas Wouters89d996e2007-09-08 17:39:28 +0000720#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000721 m = PyList_New(num_matches);
722 if (m == NULL)
723 goto error;
724 for (i = 0; i < num_matches; i++) {
725 s = PyUnicode_FromString(matches[i+1]);
726 if (s == NULL)
727 goto error;
728 if (PyList_SetItem(m, i, s) == -1)
729 goto error;
730 }
731 r = PyObject_CallFunction(completion_display_matches_hook,
732 "sOi", matches[0], m, max_length);
Christian Heimes32fbe592007-11-12 15:01:33 +0000733
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000734 Py_DECREF(m); m=NULL;
Christian Heimes32fbe592007-11-12 15:01:33 +0000735
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000736 if (r == NULL ||
737 (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
738 goto error;
739 }
740 Py_XDECREF(r); r=NULL;
741
742 if (0) {
743 error:
744 PyErr_Clear();
745 Py_XDECREF(m);
746 Py_XDECREF(r);
747 }
Christian Heimes32fbe592007-11-12 15:01:33 +0000748#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000749 PyGILState_Release(gilstate);
Christian Heimes32fbe592007-11-12 15:01:33 +0000750#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000751}
752
753
Guido van Rossum290900a1997-09-26 21:51:21 +0000754/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000755
Guido van Rossum290900a1997-09-26 21:51:21 +0000756static char *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000757on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000758{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000759 char *result = NULL;
760 if (completer != NULL) {
761 PyObject *r;
Christian Heimesaec75c32007-11-11 22:42:36 +0000762#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000763 PyGILState_STATE gilstate = PyGILState_Ensure();
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000764#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000765 rl_attempted_completion_over = 1;
766 r = PyObject_CallFunction(completer, "si", text, state);
767 if (r == NULL)
768 goto error;
769 if (r == Py_None) {
770 result = NULL;
771 }
772 else {
773 char *s = _PyUnicode_AsString(r);
774 if (s == NULL)
775 goto error;
776 result = strdup(s);
777 }
778 Py_DECREF(r);
779 goto done;
780 error:
781 PyErr_Clear();
782 Py_XDECREF(r);
783 done:
Christian Heimesaec75c32007-11-11 22:42:36 +0000784#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000785 PyGILState_Release(gilstate);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000786#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000787 return result;
788 }
789 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000790}
791
Guido van Rossum290900a1997-09-26 21:51:21 +0000792
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000793/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000794 * before calling the normal completer */
795
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000796static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000797flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000798{
Antoine Pitroue566bda2009-10-19 18:24:35 +0000799#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000800 rl_completion_append_character ='\0';
Antoine Pitrou37276002009-10-26 19:32:51 +0000801#endif
802#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000803 rl_completion_suppress_append = 0;
Antoine Pitroue566bda2009-10-19 18:24:35 +0000804#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000805 Py_XDECREF(begidx);
806 Py_XDECREF(endidx);
807 begidx = PyLong_FromLong((long) start);
808 endidx = PyLong_FromLong((long) end);
809 return completion_matches(text, *on_completion);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000810}
811
Guido van Rossum05ac4492003-01-07 20:04:12 +0000812
Guido van Rossum290900a1997-09-26 21:51:21 +0000813/* Helper to initialize GNU readline properly. */
814
815static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000816setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000817{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000818#ifdef SAVE_LOCALE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000819 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
820 if (!saved_locale)
821 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000822#endif
823
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000824 using_history();
Skip Montanaroa0392742002-06-11 14:32:46 +0000825
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000826 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000827#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000828 /* Allow $if term= in .inputrc to work */
829 rl_terminal_name = getenv("TERM");
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000830#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000831 /* Force rebind of TAB to insert-tab */
832 rl_bind_key('\t', rl_insert);
833 /* Bind both ESC-TAB and ESC-ESC to the completion function */
834 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
835 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
836 /* Set our hook functions */
837 rl_startup_hook = (Function *)on_startup_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000838#ifdef HAVE_RL_PRE_INPUT_HOOK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000839 rl_pre_input_hook = (Function *)on_pre_input_hook;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000840#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000841 /* Set our completion function */
842 rl_attempted_completion_function = (CPPFunction *)flex_complete;
843 /* Set Python word break characters */
844 rl_completer_word_break_characters =
845 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
846 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000847
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000848 begidx = PyLong_FromLong(0L);
849 endidx = PyLong_FromLong(0L);
850 /* Initialize (allows .inputrc to override)
851 *
852 * XXX: A bug in the readline-2.2 library causes a memory leak
853 * inside this function. Nothing we can do about it.
854 */
855 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000856
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000857 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000858}
859
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000860/* Wrapper around GNU readline that handles signals differently. */
861
862
863#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
864
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000865static char *completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000866static void
867rlhandler(char *text)
868{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000869 completed_input_string = text;
870 rl_callback_handler_remove();
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000871}
872
873extern PyThreadState* _PyOS_ReadlineTState;
874
875static char *
876readline_until_enter_or_signal(char *prompt, int *signal)
877{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 char * not_done_reading = "";
879 fd_set selectset;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000880
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 *signal = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000882#ifdef HAVE_RL_CATCH_SIGNAL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000883 rl_catch_signals = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000884#endif
885
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000886 rl_callback_handler_install (prompt, rlhandler);
887 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000888
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000889 completed_input_string = not_done_reading;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000890
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000891 while (completed_input_string == not_done_reading) {
892 int has_input = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000893
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000894 while (!has_input)
895 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000896
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 /* [Bug #1552726] Only limit the pause if an input hook has been
898 defined. */
899 struct timeval *timeoutp = NULL;
900 if (PyOS_InputHook)
901 timeoutp = &timeout;
902 FD_SET(fileno(rl_instream), &selectset);
903 /* select resets selectset if no input was available */
904 has_input = select(fileno(rl_instream) + 1, &selectset,
905 NULL, NULL, timeoutp);
906 if(PyOS_InputHook) PyOS_InputHook();
907 }
908
909 if(has_input > 0) {
910 rl_callback_read_char();
911 }
912 else if (errno == EINTR) {
913 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000914#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000915 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000916#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000917 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000918#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000919 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000920#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000921 if (s < 0) {
922 rl_free_line_state();
923 rl_cleanup_after_signal();
924 rl_callback_handler_remove();
925 *signal = 1;
926 completed_input_string = NULL;
927 }
928 }
929 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000930
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000931 return completed_input_string;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000932}
933
934
935#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000936
937/* Interrupt handler */
938
939static jmp_buf jbuf;
940
Guido van Rossum0969d361997-08-05 21:27:50 +0000941/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000942static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000943onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000944{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000945 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000946}
947
Guido van Rossum290900a1997-09-26 21:51:21 +0000948
Guido van Rossum0969d361997-08-05 21:27:50 +0000949static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000950readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000951{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000952 PyOS_sighandler_t old_inthandler;
953 char *p;
Guido van Rossum74f31432003-01-07 20:01:29 +0000954
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000955 *signal = 0;
956
957 old_inthandler = PyOS_setsig(SIGINT, onintr);
958 if (setjmp(jbuf)) {
Guido van Rossum0969d361997-08-05 21:27:50 +0000959#ifdef HAVE_SIGRELSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000960 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
961 sigrelse(SIGINT);
Guido van Rossum0969d361997-08-05 21:27:50 +0000962#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000963 PyOS_setsig(SIGINT, old_inthandler);
964 *signal = 1;
965 return NULL;
966 }
967 rl_event_hook = PyOS_InputHook;
968 p = readline(prompt);
969 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000970
971 return p;
972}
973#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
974
975
976static char *
977call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
978{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000979 size_t n;
980 char *p, *q;
981 int signal;
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000982
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000983#ifdef SAVE_LOCALE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000984 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
985 if (!saved_locale)
986 Py_FatalError("not enough memory to save locale");
987 setlocale(LC_CTYPE, "");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000988#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000989
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000990 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
991 rl_instream = sys_stdin;
992 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000993#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000994 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000995#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000996 }
Guido van Rossum74f31432003-01-07 20:01:29 +0000997
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000998 p = readline_until_enter_or_signal(prompt, &signal);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000999
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001000 /* we got an interrupt signal */
1001 if (signal) {
1002 RESTORE_LOCALE(saved_locale)
1003 return NULL;
1004 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001005
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001006 /* We got an EOF, return a empty string. */
1007 if (p == NULL) {
1008 p = PyMem_Malloc(1);
1009 if (p != NULL)
1010 *p = '\0';
1011 RESTORE_LOCALE(saved_locale)
1012 return p;
1013 }
1014
1015 /* we have a valid line */
1016 n = strlen(p);
1017 if (n > 0) {
1018 char *line;
Mark Dickinson29c72b72010-08-03 16:52:23 +00001019 int length = _py_get_history_length();
1020 if (length > 0)
1021 line = history_get(length)->line;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001022 else
1023 line = "";
1024 if (strcmp(p, line))
1025 add_history(p);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001026 }
1027 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
1028 release the original. */
1029 q = p;
1030 p = PyMem_Malloc(n+2);
1031 if (p != NULL) {
1032 strncpy(p, q, n);
1033 p[n] = '\n';
1034 p[n+1] = '\0';
1035 }
1036 free(q);
1037 RESTORE_LOCALE(saved_locale)
1038 return p;
Guido van Rossum0969d361997-08-05 21:27:50 +00001039}
1040
Guido van Rossum290900a1997-09-26 21:51:21 +00001041
1042/* Initialize the module */
1043
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001044PyDoc_STRVAR(doc_module,
1045"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001046
Martin v. Löwis1a214512008-06-11 05:26:20 +00001047
1048static struct PyModuleDef readlinemodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001049 PyModuleDef_HEAD_INIT,
1050 "readline",
1051 doc_module,
1052 -1,
1053 readline_methods,
1054 NULL,
1055 NULL,
1056 NULL,
1057 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001058};
1059
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001060PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001061PyInit_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001062{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001063 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001064
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001065 m = PyModule_Create(&readlinemodule);
1066 if (m == NULL)
1067 return NULL;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001068
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001069 PyOS_ReadlineFunctionPointer = call_readline;
1070 setup_readline();
1071 return m;
Guido van Rossum0969d361997-08-05 21:27:50 +00001072}