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