blob: a2efd47f626b32bd8c6351f439789d2f749d2488 [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
3 * Center. The completer interface was inspired by Lele Gaifax.
4 *
5 * More recently, it was largely rewritten by Guido van Rossum who is
6 * now maintaining it.
Guido van Rossum0969d361997-08-05 21:27:50 +00007 */
8
Guido van Rossum290900a1997-09-26 21:51:21 +00009/* Standard definitions */
Guido van Rossum0969d361997-08-05 21:27:50 +000010#include "Python.h"
11#include <setjmp.h>
12#include <signal.h>
Guido van Rossum290900a1997-09-26 21:51:21 +000013#include <errno.h>
Guido van Rossum0969d361997-08-05 21:27:50 +000014
Guido van Rossum290900a1997-09-26 21:51:21 +000015/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000016#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000017#include <readline/readline.h>
18#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000019
Guido van Rossum353ae582001-07-10 16:45:32 +000020#ifdef HAVE_RL_COMPLETION_MATCHES
21#define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
22#endif
23
Guido van Rossum290900a1997-09-26 21:51:21 +000024/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum5a530192001-01-10 21:03:32 +000025extern DL_IMPORT(int) (*PyOS_InputHook)(void);
26extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(char *);
Guido van Rossum0969d361997-08-05 21:27:50 +000027
Guido van Rossum0969d361997-08-05 21:27:50 +000028
Guido van Rossum290900a1997-09-26 21:51:21 +000029/* Exported function to send one line to readline's init file parser */
30
31static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000032parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000033{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000034 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000035 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000036 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000037 /* Make a copy -- rl_parse_and_bind() modifies its argument */
38 /* Bernard Herzog */
39 copy = malloc(1 + strlen(s));
40 if (copy == NULL)
41 return PyErr_NoMemory();
42 strcpy(copy, s);
43 rl_parse_and_bind(copy);
44 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000045 Py_INCREF(Py_None);
46 return Py_None;
47}
48
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000049PyDoc_STRVAR(doc_parse_and_bind,
50"parse_and_bind(string) -> None\n\
51Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000052
53
54/* Exported function to parse a readline init file */
55
56static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000057read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000058{
59 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000060 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000061 return NULL;
62 errno = rl_read_init_file(s);
63 if (errno)
64 return PyErr_SetFromErrno(PyExc_IOError);
65 Py_INCREF(Py_None);
66 return Py_None;
67}
68
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000069PyDoc_STRVAR(doc_read_init_file,
70"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000071Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000072The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000073
74
Skip Montanaro28067822000-07-06 18:55:12 +000075/* Exported function to load a readline history file */
76
77static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000078read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000079{
80 char *s = NULL;
81 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
82 return NULL;
83 errno = read_history(s);
84 if (errno)
85 return PyErr_SetFromErrno(PyExc_IOError);
86 Py_INCREF(Py_None);
87 return Py_None;
88}
89
Skip Montanaro49bd24d2000-07-19 16:54:53 +000090static int history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000091PyDoc_STRVAR(doc_read_history_file,
92"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +000093Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000094The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +000095
96
97/* Exported function to save a readline history file */
98
99static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000100write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000101{
102 char *s = NULL;
103 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
104 return NULL;
105 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000106 if (!errno && history_length >= 0)
107 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000108 if (errno)
109 return PyErr_SetFromErrno(PyExc_IOError);
110 Py_INCREF(Py_None);
111 return Py_None;
112}
113
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000114PyDoc_STRVAR(doc_write_history_file,
115"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000116Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000118
119
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000120PyDoc_STRVAR(set_history_length_doc,
121"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000122set the maximal number of items which will be written to\n\
123the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000124history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000125
126static PyObject*
127set_history_length(PyObject *self, PyObject *args)
128{
129 int length = history_length;
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000130 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
131 return NULL;
132 history_length = length;
133 Py_INCREF(Py_None);
134 return Py_None;
135}
136
137
138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000139PyDoc_STRVAR(get_history_length_doc,
140"get_history_length() -> int\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000141return the maximum number of items that will be written to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000142the history file.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000143
144static PyObject*
145get_history_length(PyObject *self, PyObject *args)
146{
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000147 if (!PyArg_ParseTuple(args, ":get_history_length"))
148 return NULL;
149 return Py_BuildValue("i", history_length);
150}
151
Martin v. Löwis0daad592001-09-30 21:09:59 +0000152/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000153
Martin v. Löwis0daad592001-09-30 21:09:59 +0000154static PyObject *
155set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args)
156{
157 PyObject *function = Py_None;
158 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000159 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000160 if (!PyArg_ParseTuple(args, buf, &function))
161 return NULL;
162 if (function == Py_None) {
163 Py_XDECREF(*hook_var);
164 *hook_var = NULL;
165 *tstate = NULL;
166 }
167 else if (PyCallable_Check(function)) {
168 PyObject *tmp = *hook_var;
169 Py_INCREF(function);
170 *hook_var = function;
171 Py_XDECREF(tmp);
172 *tstate = PyThreadState_Get();
173 }
174 else {
Tim Peters885d4572001-11-28 20:27:42 +0000175 PyOS_snprintf(buf, sizeof(buf),
176 "set_%.50s(func): argument not callable",
177 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000178 PyErr_SetString(PyExc_TypeError, buf);
179 return NULL;
180 }
181 Py_INCREF(Py_None);
182 return Py_None;
183}
184
185/* Exported functions to specify hook functions in Python */
186
187static PyObject *startup_hook = NULL;
188static PyThreadState *startup_hook_tstate = NULL;
189
190#ifdef HAVE_RL_PRE_INPUT_HOOK
191static PyObject *pre_input_hook = NULL;
192static PyThreadState *pre_input_hook_tstate = NULL;
193#endif
194
195static PyObject *
196set_startup_hook(PyObject *self, PyObject *args)
197{
198 return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args);
199}
200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000201PyDoc_STRVAR(doc_set_startup_hook,
202"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000203Set or remove the startup_hook function.\n\
204The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000206
207#ifdef HAVE_RL_PRE_INPUT_HOOK
208static PyObject *
209set_pre_input_hook(PyObject *self, PyObject *args)
210{
211 return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args);
212}
213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000214PyDoc_STRVAR(doc_set_pre_input_hook,
215"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000216Set or remove the pre_input_hook function.\n\
217The function is called with no arguments after the first prompt\n\
218has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000219characters.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000220#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000221
Guido van Rossum290900a1997-09-26 21:51:21 +0000222/* Exported function to specify a word completer in Python */
223
224static PyObject *completer = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000225static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000226
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000227static PyObject *begidx = NULL;
228static PyObject *endidx = NULL;
229
230/* get the beginning index for the scope of the tab-completion */
231static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000232get_begidx(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000233{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000234 Py_INCREF(begidx);
235 return begidx;
236}
237
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000238PyDoc_STRVAR(doc_get_begidx,
239"get_begidx() -> int\n\
240get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000241
242/* get the ending index for the scope of the tab-completion */
243static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000244get_endidx(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000245{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000246 Py_INCREF(endidx);
247 return endidx;
248}
249
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000250PyDoc_STRVAR(doc_get_endidx,
251"get_endidx() -> int\n\
252get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000253
254
255/* set the tab-completion word-delimiters that readline uses */
256
257static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000258set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000259{
260 char *break_chars;
261
Guido van Rossum43713e52000-02-29 13:59:29 +0000262 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000263 return NULL;
264 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000265 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000266 rl_completer_word_break_characters = strdup(break_chars);
267 Py_INCREF(Py_None);
268 return Py_None;
269}
270
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271PyDoc_STRVAR(doc_set_completer_delims,
272"set_completer_delims(string) -> None\n\
273set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000274
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000275static PyObject *
276py_add_history(PyObject *self, PyObject *args)
277{
278 char *line;
279
280 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
281 return NULL;
282 }
283 add_history(line);
284 Py_INCREF(Py_None);
285 return Py_None;
286}
287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000288PyDoc_STRVAR(doc_add_history,
289"add_history(string) -> None\n\
290add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000291
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000292
293/* get the tab-completion word-delimiters that readline uses */
294
295static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000296get_completer_delims(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000297{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000298 return PyString_FromString(rl_completer_word_break_characters);
299}
300
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000301PyDoc_STRVAR(doc_get_completer_delims,
302"get_completer_delims() -> string\n\
303get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000304
Guido van Rossum290900a1997-09-26 21:51:21 +0000305static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000306set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000307{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000308 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000309}
310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000311PyDoc_STRVAR(doc_set_completer,
312"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000313Set or remove the completer function.\n\
314The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000315for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000316It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000317
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000318/* Exported function to get any element of history */
319
320static PyObject *
321get_history_item(PyObject *self, PyObject *args)
322{
323 int idx = 0;
324 HIST_ENTRY *hist_ent;
325
326 if (!PyArg_ParseTuple(args, "i:index", &idx))
327 return NULL;
328 if ((hist_ent = history_get(idx)))
329 return PyString_FromString(hist_ent->line);
330 else {
331 Py_INCREF(Py_None);
332 return Py_None;
333 }
334}
335
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000336PyDoc_STRVAR(doc_get_history_item,
337"get_history_item() -> string\n\
338return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000339
340/* Exported function to get current length of history */
341
342static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000343get_current_history_length(PyObject *self)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000344{
345 HISTORY_STATE *hist_st;
346
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000347 hist_st = history_get_history_state();
348 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
349}
350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000351PyDoc_STRVAR(doc_get_current_history_length,
352"get_current_history_length() -> integer\n\
353return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000354
Guido van Rossum79378ff1997-10-07 14:53:21 +0000355/* Exported function to read the current line buffer */
356
357static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000358get_line_buffer(PyObject *self)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000359{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000360 return PyString_FromString(rl_line_buffer);
361}
362
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000363PyDoc_STRVAR(doc_get_line_buffer,
364"get_line_buffer() -> string\n\
365return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000366
367/* Exported function to insert text into the line buffer */
368
369static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000370insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000371{
372 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000373 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000374 return NULL;
375 rl_insert_text(s);
376 Py_INCREF(Py_None);
377 return Py_None;
378}
379
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000380PyDoc_STRVAR(doc_insert_text,
381"insert_text(string) -> None\n\
382Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000383
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000384static PyObject *
385redisplay(PyObject *self)
386{
387 rl_redisplay();
388 Py_INCREF(Py_None);
389 return Py_None;
390}
391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000392PyDoc_STRVAR(doc_redisplay,
393"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000394Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000395contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000396
397/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000398
399static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000400{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000401 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Neal Norwitz767f8352002-03-31 16:13:39 +0000402 {"get_line_buffer", (PyCFunction)get_line_buffer,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000403 METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000404 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000405 {"redisplay", (PyCFunction)redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000406 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
407 {"read_history_file", read_history_file,
408 METH_VARARGS, doc_read_history_file},
409 {"write_history_file", write_history_file,
410 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000411 {"get_history_item", get_history_item,
412 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000413 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000414 METH_NOARGS, doc_get_current_history_length},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000415 {"set_history_length", set_history_length,
416 METH_VARARGS, set_history_length_doc},
417 {"get_history_length", get_history_length,
418 METH_VARARGS, get_history_length_doc},
419 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Neal Norwitz767f8352002-03-31 16:13:39 +0000420 {"get_begidx", (PyCFunction)get_begidx, METH_NOARGS, doc_get_begidx},
421 {"get_endidx", (PyCFunction)get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000422
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000423 {"set_completer_delims", set_completer_delims,
424 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000425 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Neal Norwitz767f8352002-03-31 16:13:39 +0000426 {"get_completer_delims", (PyCFunction)get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000427 METH_NOARGS, doc_get_completer_delims},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000428
429 {"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook},
430#ifdef HAVE_RL_PRE_INPUT_HOOK
431 {"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook},
432#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000433 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000434};
435
Martin v. Löwis0daad592001-09-30 21:09:59 +0000436/* C function to call the Python hooks. */
437
438static int
439on_hook(PyObject *func, PyThreadState *tstate)
440{
441 int result = 0;
442 if (func != NULL) {
443 PyObject *r;
444 PyThreadState *save_tstate;
445 /* Note that readline is called with the interpreter
446 lock released! */
447 save_tstate = PyThreadState_Swap(NULL);
448 PyEval_RestoreThread(tstate);
449 r = PyObject_CallFunction(func, NULL);
450 if (r == NULL)
451 goto error;
452 if (r == Py_None)
453 result = 0;
454 else
455 result = PyInt_AsLong(r);
456 Py_DECREF(r);
457 goto done;
458 error:
459 PyErr_Clear();
460 Py_XDECREF(r);
461 done:
462 PyEval_SaveThread();
463 PyThreadState_Swap(save_tstate);
464 }
465 return result;
466}
467
468static int
469on_startup_hook(void)
470{
471 return on_hook(startup_hook, startup_hook_tstate);
472}
473
474#ifdef HAVE_RL_PRE_INPUT_HOOK
475static int
476on_pre_input_hook(void)
477{
478 return on_hook(pre_input_hook, pre_input_hook_tstate);
479}
480#endif
481
Guido van Rossum290900a1997-09-26 21:51:21 +0000482/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000483
Guido van Rossum290900a1997-09-26 21:51:21 +0000484static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000485on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000486{
Guido van Rossum290900a1997-09-26 21:51:21 +0000487 char *result = NULL;
488 if (completer != NULL) {
489 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000490 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000491 /* Note that readline is called with the interpreter
492 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000493 save_tstate = PyThreadState_Swap(NULL);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000494 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000495 /* Don't use the default filename completion if we
496 * have a custom completion function... */
497 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000498 r = PyObject_CallFunction(completer, "si", text, state);
499 if (r == NULL)
500 goto error;
501 if (r == Py_None) {
502 result = NULL;
503 }
504 else {
505 char *s = PyString_AsString(r);
506 if (s == NULL)
507 goto error;
508 result = strdup(s);
509 }
510 Py_DECREF(r);
511 goto done;
512 error:
513 PyErr_Clear();
514 Py_XDECREF(r);
515 done:
516 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000517 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000518 }
519 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000520}
521
Guido van Rossum290900a1997-09-26 21:51:21 +0000522
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000523/* a more flexible constructor that saves the "begidx" and "endidx"
524 * before calling the normal completer */
525
526char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000527flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000528{
529 Py_XDECREF(begidx);
530 Py_XDECREF(endidx);
531 begidx = PyInt_FromLong((long) start);
532 endidx = PyInt_FromLong((long) end);
533 return completion_matches(text, *on_completion);
534}
535
Guido van Rossum290900a1997-09-26 21:51:21 +0000536/* Helper to initialize GNU readline properly. */
537
538static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000539setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000540{
Skip Montanaroa0392742002-06-11 14:32:46 +0000541 using_history();
542
Guido van Rossum290900a1997-09-26 21:51:21 +0000543 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000544#if defined(PYOS_OS2) && defined(PYCC_GCC)
545 /* Allow $if term= in .inputrc to work */
546 rl_terminal_name = getenv("TERM");
547#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000548 /* Force rebind of TAB to insert-tab */
549 rl_bind_key('\t', rl_insert);
550 /* Bind both ESC-TAB and ESC-ESC to the completion function */
551 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
552 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000553 /* Set our hook functions */
554 rl_startup_hook = (Function *)on_startup_hook;
555#ifdef HAVE_RL_PRE_INPUT_HOOK
556 rl_pre_input_hook = (Function *)on_pre_input_hook;
557#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000558 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000559 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000560 /* Set Python word break characters */
561 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000562 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000563 /* All nonalphanums except '.' */
Guido van Rossum84271bb2002-05-30 15:41:56 +0000564 rl_completion_append_character ='\0';
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000565
566 begidx = PyInt_FromLong(0L);
567 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000568 /* Initialize (allows .inputrc to override)
569 *
570 * XXX: A bug in the readline-2.2 library causes a memory leak
571 * inside this function. Nothing we can do about it.
572 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000573 rl_initialize();
574}
575
576
577/* Interrupt handler */
578
579static jmp_buf jbuf;
580
Guido van Rossum0969d361997-08-05 21:27:50 +0000581/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000582static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000583onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000584{
Guido van Rossum290900a1997-09-26 21:51:21 +0000585 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000586}
587
Guido van Rossum290900a1997-09-26 21:51:21 +0000588
589/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000590
591static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000592call_readline(char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000593{
Guido van Rossum26418a92000-06-28 21:30:31 +0000594 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000595 char *p, *q;
Guido van Rossum174efc92000-09-16 16:37:53 +0000596 PyOS_sighandler_t old_inthandler;
597
598 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000599 if (setjmp(jbuf)) {
600#ifdef HAVE_SIGRELSE
601 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
602 sigrelse(SIGINT);
603#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000604 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossum0969d361997-08-05 21:27:50 +0000605 return NULL;
606 }
Guido van Rossum44620641997-08-11 18:57:29 +0000607 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000608 p = readline(prompt);
Guido van Rossum174efc92000-09-16 16:37:53 +0000609 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000610
611 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000612 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000613 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000614 if (p != NULL)
615 *p = '\0';
616 return p;
617 }
618 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000619 if (n > 0) {
620 char *line;
621 HISTORY_STATE *state = history_get_history_state();
622 if (state->length > 0)
623 line = history_get(state->length)->line;
624 else
625 line = "";
626 if (strcmp(p, line))
627 add_history(p);
628 /* the history docs don't say so, but the address of state
629 changes each time history_get_history_state is called
630 which makes me think it's freshly malloc'd memory...
631 on the other hand, the address of the last line stays the
632 same as long as history isn't extended, so it appears to
633 be malloc'd but managed by the history package... */
634 free(state);
635 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000636 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
637 release the original. */
638 q = p;
639 p = PyMem_Malloc(n+2);
640 if (p != NULL) {
641 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000642 p[n] = '\n';
643 p[n+1] = '\0';
644 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000645 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000646 return p;
647}
648
Guido van Rossum290900a1997-09-26 21:51:21 +0000649
650/* Initialize the module */
651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000652PyDoc_STRVAR(doc_module,
653"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000654
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000655PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000656initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000657{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000658 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000659
660 m = Py_InitModule4("readline", readline_methods, doc_module,
661 (PyObject *)NULL, PYTHON_API_VERSION);
662 if (isatty(fileno(stdin))) {
663 PyOS_ReadlineFunctionPointer = call_readline;
664 setup_readline();
665 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000666}