blob: f988d5001af76c4b1e893f7ce0d16a2adb0b9f30 [file] [log] [blame]
Guido van Rossum290900a1997-09-26 21:51:21 +00001/* This module makes GNU readline available to Python. It has ideas
2 * contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
Michael W. Hudson9a8c3142005-03-30 10:09:12 +00003 * Center. The completer interface was inspired by Lele Gaifax. More
4 * recently, it was largely rewritten by Guido van Rossum.
Guido van Rossum0969d361997-08-05 21:27:50 +00005 */
6
Guido van Rossum290900a1997-09-26 21:51:21 +00007/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +00008#include "Python.h"
9#include <setjmp.h>
10#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000011#include <errno.h>
Michael W. Hudson8da2b012004-10-07 13:46:33 +000012#include <sys/time.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000013
Skip Montanaro7befb992004-02-10 16:50:21 +000014#if defined(HAVE_SETLOCALE)
Guido van Rossum60c8a3a2002-10-09 21:27:33 +000015/* GNU readline() mistakenly sets the LC_CTYPE locale.
16 * This is evil. Only the user or the app's main() should do this!
17 * We must save and restore the locale around the rl_initialize() call.
18 */
19#define SAVE_LOCALE
20#include <locale.h>
21#endif
22
Neal Norwitz5eaf7722006-07-16 02:15:27 +000023#ifdef SAVE_LOCALE
24# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
25#else
26# define RESTORE_LOCALE(sl)
27#endif
28
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000030#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000031#include <readline/readline.h>
32#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000033
Guido van Rossum353ae582001-07-10 16:45:32 +000034#ifdef HAVE_RL_COMPLETION_MATCHES
Guido van Rossum74f31432003-01-07 20:01:29 +000035#define completion_matches(x, y) \
36 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Neal Norwitzbd538702007-04-19 05:52:37 +000037#else
38extern char **completion_matches(char *, rl_compentry_func_t *);
Guido van Rossum353ae582001-07-10 16:45:32 +000039#endif
40
Martin v. Löwisf3548942007-11-12 04:53:02 +000041static void
42on_completion_display_matches_hook(char **matches,
43 int num_matches, int max_length);
44
Guido van Rossum0969d361997-08-05 21:27:50 +000045
Guido van Rossum290900a1997-09-26 21:51:21 +000046/* Exported function to send one line to readline's init file parser */
47
48static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000049parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000050{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000051 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000052 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000053 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000054 /* Make a copy -- rl_parse_and_bind() modifies its argument */
55 /* Bernard Herzog */
56 copy = malloc(1 + strlen(s));
57 if (copy == NULL)
58 return PyErr_NoMemory();
59 strcpy(copy, s);
60 rl_parse_and_bind(copy);
61 free(copy); /* Free the copy */
Christian Heimes1bc4af42007-11-12 18:58:08 +000062 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000063}
64
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000065PyDoc_STRVAR(doc_parse_and_bind,
66"parse_and_bind(string) -> None\n\
67Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000068
69
70/* Exported function to parse a readline init file */
71
72static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000073read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000074{
75 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000076 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000077 return NULL;
78 errno = rl_read_init_file(s);
79 if (errno)
80 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes1bc4af42007-11-12 18:58:08 +000081 Py_RETURN_NONE;
Guido van Rossum290900a1997-09-26 21:51:21 +000082}
83
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084PyDoc_STRVAR(doc_read_init_file,
85"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000086Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000087The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000088
89
Skip Montanaro28067822000-07-06 18:55:12 +000090/* Exported function to load a readline history file */
91
92static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000093read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000094{
95 char *s = NULL;
96 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
97 return NULL;
98 errno = read_history(s);
99 if (errno)
100 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000101 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000102}
103
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000104static int _history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000105PyDoc_STRVAR(doc_read_history_file,
106"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000107Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000108The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000109
110
111/* Exported function to save a readline history file */
112
113static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000114write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000115{
116 char *s = NULL;
117 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
118 return NULL;
119 errno = write_history(s);
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000120 if (!errno && _history_length >= 0)
121 history_truncate_file(s, _history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000122 if (errno)
123 return PyErr_SetFromErrno(PyExc_IOError);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000124 Py_RETURN_NONE;
Skip Montanaro28067822000-07-06 18:55:12 +0000125}
126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127PyDoc_STRVAR(doc_write_history_file,
128"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000129Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000130The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000131
132
Guido van Rossum74f31432003-01-07 20:01:29 +0000133/* Set history length */
134
135static PyObject*
136set_history_length(PyObject *self, PyObject *args)
137{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000138 int length = _history_length;
Guido van Rossum74f31432003-01-07 20:01:29 +0000139 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
140 return NULL;
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000141 _history_length = length;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000142 Py_RETURN_NONE;
Guido van Rossum74f31432003-01-07 20:01:29 +0000143}
144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000145PyDoc_STRVAR(set_history_length_doc,
146"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000147set the maximal number of items which will be written to\n\
148the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000149history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000150
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151
Guido van Rossum74f31432003-01-07 20:01:29 +0000152/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000153
154static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000155get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000156{
Hye-Shik Chang7a8173a2004-11-25 04:04:20 +0000157 return PyInt_FromLong(_history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000158}
159
Guido van Rossum74f31432003-01-07 20:01:29 +0000160PyDoc_STRVAR(get_history_length_doc,
161"get_history_length() -> int\n\
162return the maximum number of items that will be written to\n\
163the history file.");
164
165
Martin v. Löwis0daad592001-09-30 21:09:59 +0000166/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000167
Martin v. Löwis0daad592001-09-30 21:09:59 +0000168static PyObject *
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000169set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000170{
171 PyObject *function = Py_None;
172 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000173 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000174 if (!PyArg_ParseTuple(args, buf, &function))
175 return NULL;
176 if (function == Py_None) {
177 Py_XDECREF(*hook_var);
178 *hook_var = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000179 }
180 else if (PyCallable_Check(function)) {
181 PyObject *tmp = *hook_var;
182 Py_INCREF(function);
183 *hook_var = function;
184 Py_XDECREF(tmp);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000185 }
186 else {
Tim Peters885d4572001-11-28 20:27:42 +0000187 PyOS_snprintf(buf, sizeof(buf),
188 "set_%.50s(func): argument not callable",
189 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000190 PyErr_SetString(PyExc_TypeError, buf);
191 return NULL;
192 }
Christian Heimes1bc4af42007-11-12 18:58:08 +0000193 Py_RETURN_NONE;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000194}
195
Guido van Rossum74f31432003-01-07 20:01:29 +0000196
Martin v. Löwis0daad592001-09-30 21:09:59 +0000197/* Exported functions to specify hook functions in Python */
198
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000199static PyObject *completion_display_matches_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000200static PyObject *startup_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000201
202#ifdef HAVE_RL_PRE_INPUT_HOOK
203static PyObject *pre_input_hook = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000204#endif
205
206static PyObject *
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000207set_completion_display_matches_hook(PyObject *self, PyObject *args)
208{
Martin v. Löwisf3548942007-11-12 04:53:02 +0000209 PyObject *result = set_hook("completion_display_matches_hook",
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000210 &completion_display_matches_hook, args);
Martin v. Löwisf3548942007-11-12 04:53:02 +0000211#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
212 /* We cannot set this hook globally, since it replaces the
213 default completion display. */
214 rl_completion_display_matches_hook =
Christian Heimes1bc4af42007-11-12 18:58:08 +0000215 completion_display_matches_hook ?
Martin v. Löwisf3548942007-11-12 04:53:02 +0000216 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
217#endif
218 return result;
219
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000220}
221
222PyDoc_STRVAR(doc_set_completion_display_matches_hook,
223"set_completion_display_matches_hook([function]) -> None\n\
224Set or remove the completion display function.\n\
225The function is called as\n\
226 function(substitution, [matches], longest_match_length)\n\
227once each time matches need to be displayed.");
228
229static PyObject *
Martin v. Löwis0daad592001-09-30 21:09:59 +0000230set_startup_hook(PyObject *self, PyObject *args)
231{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000232 return set_hook("startup_hook", &startup_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000233}
234
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235PyDoc_STRVAR(doc_set_startup_hook,
236"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000237Set or remove the startup_hook function.\n\
238The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000240
Guido van Rossum74f31432003-01-07 20:01:29 +0000241
Martin v. Löwis0daad592001-09-30 21:09:59 +0000242#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000243
244/* Set pre-input hook */
245
Martin v. Löwis0daad592001-09-30 21:09:59 +0000246static PyObject *
247set_pre_input_hook(PyObject *self, PyObject *args)
248{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000249 return set_hook("pre_input_hook", &pre_input_hook, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000250}
251
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000252PyDoc_STRVAR(doc_set_pre_input_hook,
253"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000254Set or remove the pre_input_hook function.\n\
255The function is called with no arguments after the first prompt\n\
256has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000258
Martin v. Löwis0daad592001-09-30 21:09:59 +0000259#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000260
Guido van Rossum74f31432003-01-07 20:01:29 +0000261
Guido van Rossum290900a1997-09-26 21:51:21 +0000262/* Exported function to specify a word completer in Python */
263
264static PyObject *completer = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000265
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000266static PyObject *begidx = NULL;
267static PyObject *endidx = NULL;
268
Guido van Rossum74f31432003-01-07 20:01:29 +0000269
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000270/* Get the completion type for the scope of the tab-completion */
271static PyObject *
272get_completion_type(PyObject *self, PyObject *noarg)
273{
274 return PyInt_FromLong(rl_completion_type);
275}
276
277PyDoc_STRVAR(doc_get_completion_type,
278"get_completion_type() -> int\n\
279Get the type of completion being attempted.");
280
281
Guido van Rossum74f31432003-01-07 20:01:29 +0000282/* Get the beginning index for the scope of the tab-completion */
283
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000284static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000285get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000286{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000287 Py_INCREF(begidx);
288 return begidx;
289}
290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291PyDoc_STRVAR(doc_get_begidx,
292"get_begidx() -> int\n\
293get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000294
Guido van Rossum74f31432003-01-07 20:01:29 +0000295
296/* Get the ending index for the scope of the tab-completion */
297
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000298static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000299get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000300{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000301 Py_INCREF(endidx);
302 return endidx;
303}
304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000305PyDoc_STRVAR(doc_get_endidx,
306"get_endidx() -> int\n\
307get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000308
309
Guido van Rossum74f31432003-01-07 20:01:29 +0000310/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000311
312static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000313set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000314{
315 char *break_chars;
316
Guido van Rossum43713e52000-02-29 13:59:29 +0000317 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000318 return NULL;
319 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000320 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000321 rl_completer_word_break_characters = strdup(break_chars);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000322 Py_RETURN_NONE;
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000323}
324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000325PyDoc_STRVAR(doc_set_completer_delims,
326"set_completer_delims(string) -> None\n\
327set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000328
Skip Montanaroe5069012004-08-15 14:32:06 +0000329static PyObject *
330py_remove_history(PyObject *self, PyObject *args)
331{
Christian Heimes1bc4af42007-11-12 18:58:08 +0000332 int entry_number;
333 HIST_ENTRY *entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000334
Christian Heimes1bc4af42007-11-12 18:58:08 +0000335 if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
336 return NULL;
337 if (entry_number < 0) {
338 PyErr_SetString(PyExc_ValueError,
339 "History index cannot be negative");
340 return NULL;
341 }
342 entry = remove_history(entry_number);
343 if (!entry) {
344 PyErr_Format(PyExc_ValueError,
345 "No history item at position %d",
346 entry_number);
347 return NULL;
348 }
349 /* free memory allocated for the history entry */
350 if (entry->line)
351 free(entry->line);
352 if (entry->data)
353 free(entry->data);
354 free(entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000355
Christian Heimes1bc4af42007-11-12 18:58:08 +0000356 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000357}
358
359PyDoc_STRVAR(doc_remove_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000360"remove_history_item(pos) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000361remove history item given by its position");
362
363static PyObject *
364py_replace_history(PyObject *self, PyObject *args)
365{
Christian Heimes1bc4af42007-11-12 18:58:08 +0000366 int entry_number;
367 char *line;
368 HIST_ENTRY *old_entry;
Skip Montanaroe5069012004-08-15 14:32:06 +0000369
Christian Heimes1bc4af42007-11-12 18:58:08 +0000370 if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
371 &line)) {
372 return NULL;
373 }
374 if (entry_number < 0) {
375 PyErr_SetString(PyExc_ValueError,
376 "History index cannot be negative");
377 return NULL;
378 }
379 old_entry = replace_history_entry(entry_number, line, (void *)NULL);
380 if (!old_entry) {
381 PyErr_Format(PyExc_ValueError,
382 "No history item at position %d",
383 entry_number);
384 return NULL;
385 }
386 /* free memory allocated for the old history entry */
387 if (old_entry->line)
388 free(old_entry->line);
389 if (old_entry->data)
390 free(old_entry->data);
391 free(old_entry);
Skip Montanaroe5069012004-08-15 14:32:06 +0000392
Christian Heimes1bc4af42007-11-12 18:58:08 +0000393 Py_RETURN_NONE;
Skip Montanaroe5069012004-08-15 14:32:06 +0000394}
395
396PyDoc_STRVAR(doc_replace_history,
Skip Montanaro6c06cd52004-08-16 16:15:13 +0000397"replace_history_item(pos, line) -> None\n\
Skip Montanaroe5069012004-08-15 14:32:06 +0000398replaces history item given by its position with contents of line");
Guido van Rossum74f31432003-01-07 20:01:29 +0000399
400/* Add a line to the history buffer */
401
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000402static PyObject *
403py_add_history(PyObject *self, PyObject *args)
404{
405 char *line;
406
407 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
408 return NULL;
409 }
410 add_history(line);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000411 Py_RETURN_NONE;
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000412}
413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000414PyDoc_STRVAR(doc_add_history,
415"add_history(string) -> None\n\
416add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000417
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000418
Guido van Rossum74f31432003-01-07 20:01:29 +0000419/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000420
421static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000422get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000423{
Christian Heimes593daf52008-05-26 12:51:38 +0000424 return PyBytes_FromString(rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000425}
Guido van Rossum74f31432003-01-07 20:01:29 +0000426
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000427PyDoc_STRVAR(doc_get_completer_delims,
428"get_completer_delims() -> string\n\
429get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000430
Guido van Rossum74f31432003-01-07 20:01:29 +0000431
432/* Set the completer function */
433
Guido van Rossum290900a1997-09-26 21:51:21 +0000434static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000435set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000436{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000437 return set_hook("completer", &completer, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000438}
439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000440PyDoc_STRVAR(doc_set_completer,
441"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000442Set or remove the completer function.\n\
443The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000444for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000445It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000446
Guido van Rossum74f31432003-01-07 20:01:29 +0000447
Michael W. Hudson796df152003-01-30 10:12:51 +0000448static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000449get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000450{
451 if (completer == NULL) {
Christian Heimes1bc4af42007-11-12 18:58:08 +0000452 Py_RETURN_NONE;
Michael W. Hudson796df152003-01-30 10:12:51 +0000453 }
454 Py_INCREF(completer);
455 return completer;
456}
457
458PyDoc_STRVAR(doc_get_completer,
459"get_completer() -> function\n\
460\n\
461Returns current completer function.");
462
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000463/* Exported function to get any element of history */
464
465static PyObject *
466get_history_item(PyObject *self, PyObject *args)
467{
468 int idx = 0;
469 HIST_ENTRY *hist_ent;
470
471 if (!PyArg_ParseTuple(args, "i:index", &idx))
472 return NULL;
473 if ((hist_ent = history_get(idx)))
Christian Heimes593daf52008-05-26 12:51:38 +0000474 return PyBytes_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000475 else {
Christian Heimes1bc4af42007-11-12 18:58:08 +0000476 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000477 }
478}
479
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000480PyDoc_STRVAR(doc_get_history_item,
481"get_history_item() -> string\n\
482return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000483
Guido van Rossum74f31432003-01-07 20:01:29 +0000484
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000485/* Exported function to get current length of history */
486
487static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000488get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000489{
490 HISTORY_STATE *hist_st;
491
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000492 hist_st = history_get_history_state();
493 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
494}
495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000496PyDoc_STRVAR(doc_get_current_history_length,
497"get_current_history_length() -> integer\n\
498return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000499
Guido van Rossum74f31432003-01-07 20:01:29 +0000500
Guido van Rossum79378ff1997-10-07 14:53:21 +0000501/* Exported function to read the current line buffer */
502
503static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000504get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000505{
Christian Heimes593daf52008-05-26 12:51:38 +0000506 return PyBytes_FromString(rl_line_buffer);
Guido van Rossum79378ff1997-10-07 14:53:21 +0000507}
508
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000509PyDoc_STRVAR(doc_get_line_buffer,
510"get_line_buffer() -> string\n\
511return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000512
Guido van Rossum74f31432003-01-07 20:01:29 +0000513
Martin v. Löwise7a97962003-09-20 16:08:33 +0000514#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
515
516/* Exported function to clear the current history */
517
518static PyObject *
519py_clear_history(PyObject *self, PyObject *noarg)
520{
521 clear_history();
Christian Heimes1bc4af42007-11-12 18:58:08 +0000522 Py_RETURN_NONE;
Martin v. Löwise7a97962003-09-20 16:08:33 +0000523}
524
525PyDoc_STRVAR(doc_clear_history,
526"clear_history() -> None\n\
527Clear the current readline history.");
528#endif
529
530
Guido van Rossum79378ff1997-10-07 14:53:21 +0000531/* Exported function to insert text into the line buffer */
532
533static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000534insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000535{
536 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000537 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000538 return NULL;
539 rl_insert_text(s);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000540 Py_RETURN_NONE;
Guido van Rossum79378ff1997-10-07 14:53:21 +0000541}
542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000543PyDoc_STRVAR(doc_insert_text,
544"insert_text(string) -> None\n\
545Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000546
Guido van Rossum74f31432003-01-07 20:01:29 +0000547
548/* Redisplay the line buffer */
549
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000550static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000551redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000552{
553 rl_redisplay();
Christian Heimes1bc4af42007-11-12 18:58:08 +0000554 Py_RETURN_NONE;
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000555}
556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000557PyDoc_STRVAR(doc_redisplay,
558"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000559Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000560contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000561
Guido van Rossum74f31432003-01-07 20:01:29 +0000562
Guido van Rossum290900a1997-09-26 21:51:21 +0000563/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000564
565static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000566{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000567 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000568 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000569 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000570 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000571 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000572 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000573 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000574 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000575 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000576 {"get_history_item", get_history_item,
577 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000578 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000579 METH_NOARGS, doc_get_current_history_length},
Christian Heimes1bc4af42007-11-12 18:58:08 +0000580 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000581 METH_VARARGS, set_history_length_doc},
Christian Heimes1bc4af42007-11-12 18:58:08 +0000582 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000583 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000584 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000585 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000586 {"get_completion_type", get_completion_type,
587 METH_NOARGS, doc_get_completion_type},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000588 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
589 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000590
Guido van Rossum74f31432003-01-07 20:01:29 +0000591 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000592 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000593 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Christian Heimes1bc4af42007-11-12 18:58:08 +0000594 {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
595 {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000596 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000597 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000598
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000599 {"set_completion_display_matches_hook", set_completion_display_matches_hook,
600 METH_VARARGS, doc_set_completion_display_matches_hook},
Guido van Rossum74f31432003-01-07 20:01:29 +0000601 {"set_startup_hook", set_startup_hook,
602 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000603#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000604 {"set_pre_input_hook", set_pre_input_hook,
605 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000606#endif
Martin v. Löwise7a97962003-09-20 16:08:33 +0000607#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
608 {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
609#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000610 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000611};
612
Guido van Rossum05ac4492003-01-07 20:04:12 +0000613
Martin v. Löwis0daad592001-09-30 21:09:59 +0000614/* C function to call the Python hooks. */
615
616static int
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000617on_hook(PyObject *func)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000618{
619 int result = 0;
620 if (func != NULL) {
621 PyObject *r;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000622#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000623 PyGILState_STATE gilstate = PyGILState_Ensure();
624#endif
Martin v. Löwis0daad592001-09-30 21:09:59 +0000625 r = PyObject_CallFunction(func, NULL);
626 if (r == NULL)
627 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000628 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000629 result = 0;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000630 else {
Martin v. Löwis0daad592001-09-30 21:09:59 +0000631 result = PyInt_AsLong(r);
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000632 if (result == -1 && PyErr_Occurred())
633 goto error;
634 }
Martin v. Löwis0daad592001-09-30 21:09:59 +0000635 Py_DECREF(r);
636 goto done;
637 error:
638 PyErr_Clear();
639 Py_XDECREF(r);
640 done:
Christian Heimes1bc4af42007-11-12 18:58:08 +0000641#ifdef WITH_THREAD
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000642 PyGILState_Release(gilstate);
643#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000644 return result;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000645 }
646 return result;
647}
648
649static int
650on_startup_hook(void)
651{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000652 return on_hook(startup_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000653}
654
655#ifdef HAVE_RL_PRE_INPUT_HOOK
656static int
657on_pre_input_hook(void)
658{
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000659 return on_hook(pre_input_hook);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000660}
661#endif
662
Guido van Rossum05ac4492003-01-07 20:04:12 +0000663
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000664/* C function to call the Python completion_display_matches */
665
666static void
667on_completion_display_matches_hook(char **matches,
668 int num_matches, int max_length)
669{
Martin v. Löwisf3548942007-11-12 04:53:02 +0000670 int i;
Christian Heimes1bc4af42007-11-12 18:58:08 +0000671 PyObject *m=NULL, *s=NULL, *r=NULL;
672#ifdef WITH_THREAD
Martin v. Löwisf3548942007-11-12 04:53:02 +0000673 PyGILState_STATE gilstate = PyGILState_Ensure();
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000674#endif
Martin v. Löwisf3548942007-11-12 04:53:02 +0000675 m = PyList_New(num_matches);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000676 if (m == NULL)
677 goto error;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000678 for (i = 0; i < num_matches; i++) {
Christian Heimes593daf52008-05-26 12:51:38 +0000679 s = PyBytes_FromString(matches[i+1]);
Christian Heimes1bc4af42007-11-12 18:58:08 +0000680 if (s == NULL)
681 goto error;
682 if (PyList_SetItem(m, i, s) == -1)
683 goto error;
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000684 }
Martin v. Löwisf3548942007-11-12 04:53:02 +0000685
686 r = PyObject_CallFunction(completion_display_matches_hook,
687 "sOi", matches[0], m, max_length);
688
Christian Heimes1bc4af42007-11-12 18:58:08 +0000689 Py_DECREF(m), m=NULL;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000690
691 if (r == NULL ||
692 (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
693 goto error;
694 }
Christian Heimes1bc4af42007-11-12 18:58:08 +0000695 Py_XDECREF(r), r=NULL;
Martin v. Löwisf3548942007-11-12 04:53:02 +0000696
Christian Heimes1bc4af42007-11-12 18:58:08 +0000697 if (0) {
698 error:
699 PyErr_Clear();
700 Py_XDECREF(m);
701 Py_XDECREF(r);
702 }
703#ifdef WITH_THREAD
Martin v. Löwisf3548942007-11-12 04:53:02 +0000704 PyGILState_Release(gilstate);
705#endif
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000706}
707
708
Guido van Rossum290900a1997-09-26 21:51:21 +0000709/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000710
Guido van Rossum290900a1997-09-26 21:51:21 +0000711static char *
Neal Norwitzbd538702007-04-19 05:52:37 +0000712on_completion(const char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000713{
Guido van Rossum290900a1997-09-26 21:51:21 +0000714 char *result = NULL;
715 if (completer != NULL) {
716 PyObject *r;
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000717#ifdef WITH_THREAD
718 PyGILState_STATE gilstate = PyGILState_Ensure();
719#endif
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000720 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000721 r = PyObject_CallFunction(completer, "si", text, state);
722 if (r == NULL)
723 goto error;
724 if (r == Py_None) {
725 result = NULL;
726 }
727 else {
Christian Heimes593daf52008-05-26 12:51:38 +0000728 char *s = PyBytes_AsString(r);
Guido van Rossum290900a1997-09-26 21:51:21 +0000729 if (s == NULL)
730 goto error;
731 result = strdup(s);
732 }
733 Py_DECREF(r);
734 goto done;
735 error:
736 PyErr_Clear();
737 Py_XDECREF(r);
738 done:
Michael W. Hudsonda6242c2005-03-30 11:21:53 +0000739#ifdef WITH_THREAD
740 PyGILState_Release(gilstate);
741#endif
Georg Brandle677adc2005-09-29 13:40:49 +0000742 return result;
Guido van Rossum290900a1997-09-26 21:51:21 +0000743 }
744 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000745}
746
Guido van Rossum290900a1997-09-26 21:51:21 +0000747
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000748/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000749 * before calling the normal completer */
750
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000751static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000752flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000753{
754 Py_XDECREF(begidx);
755 Py_XDECREF(endidx);
756 begidx = PyInt_FromLong((long) start);
757 endidx = PyInt_FromLong((long) end);
758 return completion_matches(text, *on_completion);
759}
760
Guido van Rossum05ac4492003-01-07 20:04:12 +0000761
Guido van Rossum290900a1997-09-26 21:51:21 +0000762/* Helper to initialize GNU readline properly. */
763
764static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000765setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000766{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000767#ifdef SAVE_LOCALE
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000768 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000769 if (!saved_locale)
770 Py_FatalError("not enough memory to save locale");
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000771#endif
772
Skip Montanaroa0392742002-06-11 14:32:46 +0000773 using_history();
774
Guido van Rossum290900a1997-09-26 21:51:21 +0000775 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000776#if defined(PYOS_OS2) && defined(PYCC_GCC)
777 /* Allow $if term= in .inputrc to work */
778 rl_terminal_name = getenv("TERM");
779#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000780 /* Force rebind of TAB to insert-tab */
781 rl_bind_key('\t', rl_insert);
782 /* Bind both ESC-TAB and ESC-ESC to the completion function */
783 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
784 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000785 /* Set our hook functions */
786 rl_startup_hook = (Function *)on_startup_hook;
787#ifdef HAVE_RL_PRE_INPUT_HOOK
788 rl_pre_input_hook = (Function *)on_pre_input_hook;
789#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000790 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000791 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000792 /* Set Python word break characters */
793 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000794 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000795 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000796#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000797 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000798#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000799
800 begidx = PyInt_FromLong(0L);
801 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000802 /* Initialize (allows .inputrc to override)
803 *
804 * XXX: A bug in the readline-2.2 library causes a memory leak
805 * inside this function. Nothing we can do about it.
806 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000807 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000808
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000809 RESTORE_LOCALE(saved_locale)
Guido van Rossum290900a1997-09-26 21:51:21 +0000810}
811
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000812/* Wrapper around GNU readline that handles signals differently. */
813
814
815#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
816
817static char *completed_input_string;
818static void
819rlhandler(char *text)
820{
821 completed_input_string = text;
822 rl_callback_handler_remove();
823}
824
825extern PyThreadState* _PyOS_ReadlineTState;
826
827static char *
828readline_until_enter_or_signal(char *prompt, int *signal)
829{
830 char * not_done_reading = "";
831 fd_set selectset;
832
833 *signal = 0;
834#ifdef HAVE_RL_CATCH_SIGNAL
835 rl_catch_signals = 0;
836#endif
837
838 rl_callback_handler_install (prompt, rlhandler);
839 FD_ZERO(&selectset);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000840
841 completed_input_string = not_done_reading;
842
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000843 while (completed_input_string == not_done_reading) {
844 int has_input = 0;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000845
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000846 while (!has_input)
847 { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000848
849 /* [Bug #1552726] Only limit the pause if an input hook has been
850 defined. */
851 struct timeval *timeoutp = NULL;
852 if (PyOS_InputHook)
853 timeoutp = &timeout;
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000854 FD_SET(fileno(rl_instream), &selectset);
855 /* select resets selectset if no input was available */
856 has_input = select(fileno(rl_instream) + 1, &selectset,
Andrew M. Kuchling62e475b2006-09-07 13:59:38 +0000857 NULL, NULL, timeoutp);
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000858 if(PyOS_InputHook) PyOS_InputHook();
859 }
860
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000861 if(has_input > 0) {
862 rl_callback_read_char();
863 }
864 else if (errno == EINTR) {
865 int s;
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000866#ifdef WITH_THREAD
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000867 PyEval_RestoreThread(_PyOS_ReadlineTState);
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000868#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000869 s = PyErr_CheckSignals();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000870#ifdef WITH_THREAD
Michael W. Hudson23849902004-07-08 15:28:26 +0000871 PyEval_SaveThread();
Michael W. Hudsone3afc592005-04-07 10:11:19 +0000872#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000873 if (s < 0) {
874 rl_free_line_state();
875 rl_cleanup_after_signal();
876 rl_callback_handler_remove();
877 *signal = 1;
878 completed_input_string = NULL;
879 }
880 }
881 }
882
883 return completed_input_string;
884}
885
886
887#else
Guido van Rossum290900a1997-09-26 21:51:21 +0000888
889/* Interrupt handler */
890
891static jmp_buf jbuf;
892
Guido van Rossum0969d361997-08-05 21:27:50 +0000893/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000894static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000895onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000896{
Guido van Rossum290900a1997-09-26 21:51:21 +0000897 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000898}
899
Guido van Rossum290900a1997-09-26 21:51:21 +0000900
Guido van Rossum0969d361997-08-05 21:27:50 +0000901static char *
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000902readline_until_enter_or_signal(char *prompt, int *signal)
Guido van Rossum0969d361997-08-05 21:27:50 +0000903{
Guido van Rossum174efc92000-09-16 16:37:53 +0000904 PyOS_sighandler_t old_inthandler;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000905 char *p;
906
907 *signal = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000908
Guido van Rossum174efc92000-09-16 16:37:53 +0000909 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000910 if (setjmp(jbuf)) {
911#ifdef HAVE_SIGRELSE
912 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
913 sigrelse(SIGINT);
914#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000915 PyOS_setsig(SIGINT, old_inthandler);
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000916 *signal = 1;
Guido van Rossum0969d361997-08-05 21:27:50 +0000917 return NULL;
918 }
Michael W. Hudson8da2b012004-10-07 13:46:33 +0000919 rl_event_hook = PyOS_InputHook;
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000920 p = readline(prompt);
921 PyOS_setsig(SIGINT, old_inthandler);
922
923 return p;
924}
925#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
926
927
928static char *
929call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
930{
Neal Norwitz1fa040b2004-08-25 01:20:18 +0000931 size_t n;
932 char *p, *q;
933 int signal;
934
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000935#ifdef SAVE_LOCALE
936 char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwis701abe72004-08-20 06:26:59 +0000937 if (!saved_locale)
938 Py_FatalError("not enough memory to save locale");
Martin v. Löwis78a8acc2004-08-18 13:34:00 +0000939 setlocale(LC_CTYPE, "");
940#endif
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000941
Guido van Rossum74f31432003-01-07 20:01:29 +0000942 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
943 rl_instream = sys_stdin;
944 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000945#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000946 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000947#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000948 }
949
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000950 p = readline_until_enter_or_signal(prompt, &signal);
951
952 /* we got an interrupt signal */
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000953 if (signal) {
954 RESTORE_LOCALE(saved_locale)
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000955 return NULL;
956 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000957
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000958 /* We got an EOF, return a empty string. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000959 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000960 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000961 if (p != NULL)
962 *p = '\0';
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000963 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000964 return p;
965 }
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000966
967 /* we have a valid line */
Guido van Rossum0969d361997-08-05 21:27:50 +0000968 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000969 if (n > 0) {
970 char *line;
971 HISTORY_STATE *state = history_get_history_state();
972 if (state->length > 0)
973 line = history_get(state->length)->line;
974 else
975 line = "";
976 if (strcmp(p, line))
977 add_history(p);
978 /* the history docs don't say so, but the address of state
979 changes each time history_get_history_state is called
980 which makes me think it's freshly malloc'd memory...
981 on the other hand, the address of the last line stays the
982 same as long as history isn't extended, so it appears to
983 be malloc'd but managed by the history package... */
984 free(state);
985 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000986 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
987 release the original. */
988 q = p;
989 p = PyMem_Malloc(n+2);
990 if (p != NULL) {
991 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000992 p[n] = '\n';
993 p[n+1] = '\0';
994 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000995 free(q);
Neal Norwitz5eaf7722006-07-16 02:15:27 +0000996 RESTORE_LOCALE(saved_locale)
Guido van Rossum0969d361997-08-05 21:27:50 +0000997 return p;
998}
999
Guido van Rossum290900a1997-09-26 21:51:21 +00001000
1001/* Initialize the module */
1002
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001003PyDoc_STRVAR(doc_module,
1004"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +00001005
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001006PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001007initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +00001008{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +00001009 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +00001010
1011 m = Py_InitModule4("readline", readline_methods, doc_module,
1012 (PyObject *)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001013 if (m == NULL)
1014 return;
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001015
Guido van Rossum74f31432003-01-07 20:01:29 +00001016 PyOS_ReadlineFunctionPointer = call_readline;
1017 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +00001018}