blob: 462d52f9b88a5332b3bb75cad25fba931b044a60 [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 Rossum60c8a3a2002-10-09 21:27:33 +000015#if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
16/* GNU readline() mistakenly sets the LC_CTYPE locale.
17 * This is evil. Only the user or the app's main() should do this!
18 * We must save and restore the locale around the rl_initialize() call.
19 */
20#define SAVE_LOCALE
21#include <locale.h>
22#endif
23
Guido van Rossum290900a1997-09-26 21:51:21 +000024/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000025#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000026#include <readline/readline.h>
27#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000028
Guido van Rossum353ae582001-07-10 16:45:32 +000029#ifdef HAVE_RL_COMPLETION_MATCHES
30#define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
31#endif
32
Guido van Rossum290900a1997-09-26 21:51:21 +000033/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum5a530192001-01-10 21:03:32 +000034extern DL_IMPORT(int) (*PyOS_InputHook)(void);
Martin v. Löwis566f6af2002-10-26 14:39:10 +000035extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *,char *);
Guido van Rossum0969d361997-08-05 21:27:50 +000036
Guido van Rossum0969d361997-08-05 21:27:50 +000037
Guido van Rossum290900a1997-09-26 21:51:21 +000038/* Exported function to send one line to readline's init file parser */
39
40static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000041parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000042{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000043 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000044 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000045 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000046 /* Make a copy -- rl_parse_and_bind() modifies its argument */
47 /* Bernard Herzog */
48 copy = malloc(1 + strlen(s));
49 if (copy == NULL)
50 return PyErr_NoMemory();
51 strcpy(copy, s);
52 rl_parse_and_bind(copy);
53 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000054 Py_INCREF(Py_None);
55 return Py_None;
56}
57
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000058PyDoc_STRVAR(doc_parse_and_bind,
59"parse_and_bind(string) -> None\n\
60Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000061
62
63/* Exported function to parse a readline init file */
64
65static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000066read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000067{
68 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000069 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000070 return NULL;
71 errno = rl_read_init_file(s);
72 if (errno)
73 return PyErr_SetFromErrno(PyExc_IOError);
74 Py_INCREF(Py_None);
75 return Py_None;
76}
77
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000078PyDoc_STRVAR(doc_read_init_file,
79"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000080Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000082
83
Skip Montanaro28067822000-07-06 18:55:12 +000084/* Exported function to load a readline history file */
85
86static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000087read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000088{
89 char *s = NULL;
90 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
91 return NULL;
92 errno = read_history(s);
93 if (errno)
94 return PyErr_SetFromErrno(PyExc_IOError);
95 Py_INCREF(Py_None);
96 return Py_None;
97}
98
Skip Montanaro49bd24d2000-07-19 16:54:53 +000099static int history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000100PyDoc_STRVAR(doc_read_history_file,
101"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000102Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000103The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000104
105
106/* Exported function to save a readline history file */
107
108static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000109write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000110{
111 char *s = NULL;
112 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
113 return NULL;
114 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000115 if (!errno && history_length >= 0)
116 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000117 if (errno)
118 return PyErr_SetFromErrno(PyExc_IOError);
119 Py_INCREF(Py_None);
120 return Py_None;
121}
122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000123PyDoc_STRVAR(doc_write_history_file,
124"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000125Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000126The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000127
128
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000129PyDoc_STRVAR(set_history_length_doc,
130"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000131set the maximal number of items which will be written to\n\
132the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000133history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000134
135static PyObject*
136set_history_length(PyObject *self, PyObject *args)
137{
138 int length = history_length;
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000139 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
140 return NULL;
141 history_length = length;
142 Py_INCREF(Py_None);
143 return Py_None;
144}
145
146
147
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000148PyDoc_STRVAR(get_history_length_doc,
149"get_history_length() -> int\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000150return the maximum number of items that will be written to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000151the history file.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000152
153static PyObject*
154get_history_length(PyObject *self, PyObject *args)
155{
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000156 if (!PyArg_ParseTuple(args, ":get_history_length"))
157 return NULL;
158 return Py_BuildValue("i", history_length);
159}
160
Martin v. Löwis0daad592001-09-30 21:09:59 +0000161/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000162
Martin v. Löwis0daad592001-09-30 21:09:59 +0000163static PyObject *
164set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args)
165{
166 PyObject *function = Py_None;
167 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000168 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000169 if (!PyArg_ParseTuple(args, buf, &function))
170 return NULL;
171 if (function == Py_None) {
172 Py_XDECREF(*hook_var);
173 *hook_var = NULL;
174 *tstate = NULL;
175 }
176 else if (PyCallable_Check(function)) {
177 PyObject *tmp = *hook_var;
178 Py_INCREF(function);
179 *hook_var = function;
180 Py_XDECREF(tmp);
181 *tstate = PyThreadState_Get();
182 }
183 else {
Tim Peters885d4572001-11-28 20:27:42 +0000184 PyOS_snprintf(buf, sizeof(buf),
185 "set_%.50s(func): argument not callable",
186 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000187 PyErr_SetString(PyExc_TypeError, buf);
188 return NULL;
189 }
190 Py_INCREF(Py_None);
191 return Py_None;
192}
193
194/* Exported functions to specify hook functions in Python */
195
196static PyObject *startup_hook = NULL;
197static PyThreadState *startup_hook_tstate = NULL;
198
199#ifdef HAVE_RL_PRE_INPUT_HOOK
200static PyObject *pre_input_hook = NULL;
201static PyThreadState *pre_input_hook_tstate = NULL;
202#endif
203
204static PyObject *
205set_startup_hook(PyObject *self, PyObject *args)
206{
207 return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args);
208}
209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000210PyDoc_STRVAR(doc_set_startup_hook,
211"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000212Set or remove the startup_hook function.\n\
213The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000214before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000215
216#ifdef HAVE_RL_PRE_INPUT_HOOK
217static PyObject *
218set_pre_input_hook(PyObject *self, PyObject *args)
219{
220 return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args);
221}
222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000223PyDoc_STRVAR(doc_set_pre_input_hook,
224"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000225Set or remove the pre_input_hook function.\n\
226The function is called with no arguments after the first prompt\n\
227has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000228characters.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000229#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000230
Guido van Rossum290900a1997-09-26 21:51:21 +0000231/* Exported function to specify a word completer in Python */
232
233static PyObject *completer = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000234static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000235
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000236static PyObject *begidx = NULL;
237static PyObject *endidx = NULL;
238
239/* get the beginning index for the scope of the tab-completion */
240static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000241get_begidx(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000242{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000243 Py_INCREF(begidx);
244 return begidx;
245}
246
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000247PyDoc_STRVAR(doc_get_begidx,
248"get_begidx() -> int\n\
249get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000250
251/* get the ending index for the scope of the tab-completion */
252static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000253get_endidx(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000254{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000255 Py_INCREF(endidx);
256 return endidx;
257}
258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000259PyDoc_STRVAR(doc_get_endidx,
260"get_endidx() -> int\n\
261get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000262
263
264/* set the tab-completion word-delimiters that readline uses */
265
266static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000267set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000268{
269 char *break_chars;
270
Guido van Rossum43713e52000-02-29 13:59:29 +0000271 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000272 return NULL;
273 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000274 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000275 rl_completer_word_break_characters = strdup(break_chars);
276 Py_INCREF(Py_None);
277 return Py_None;
278}
279
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000280PyDoc_STRVAR(doc_set_completer_delims,
281"set_completer_delims(string) -> None\n\
282set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000283
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000284static PyObject *
285py_add_history(PyObject *self, PyObject *args)
286{
287 char *line;
288
289 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
290 return NULL;
291 }
292 add_history(line);
293 Py_INCREF(Py_None);
294 return Py_None;
295}
296
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000297PyDoc_STRVAR(doc_add_history,
298"add_history(string) -> None\n\
299add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000300
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000301
302/* get the tab-completion word-delimiters that readline uses */
303
304static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000305get_completer_delims(PyObject *self)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000306{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000307 return PyString_FromString(rl_completer_word_break_characters);
308}
309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000310PyDoc_STRVAR(doc_get_completer_delims,
311"get_completer_delims() -> string\n\
312get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000313
Guido van Rossum290900a1997-09-26 21:51:21 +0000314static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000315set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000316{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000317 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000318}
319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000320PyDoc_STRVAR(doc_set_completer,
321"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000322Set or remove the completer function.\n\
323The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000324for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000325It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000326
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000327/* Exported function to get any element of history */
328
329static PyObject *
330get_history_item(PyObject *self, PyObject *args)
331{
332 int idx = 0;
333 HIST_ENTRY *hist_ent;
334
335 if (!PyArg_ParseTuple(args, "i:index", &idx))
336 return NULL;
337 if ((hist_ent = history_get(idx)))
338 return PyString_FromString(hist_ent->line);
339 else {
340 Py_INCREF(Py_None);
341 return Py_None;
342 }
343}
344
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000345PyDoc_STRVAR(doc_get_history_item,
346"get_history_item() -> string\n\
347return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000348
349/* Exported function to get current length of history */
350
351static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000352get_current_history_length(PyObject *self)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000353{
354 HISTORY_STATE *hist_st;
355
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000356 hist_st = history_get_history_state();
357 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
358}
359
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000360PyDoc_STRVAR(doc_get_current_history_length,
361"get_current_history_length() -> integer\n\
362return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000363
Guido van Rossum79378ff1997-10-07 14:53:21 +0000364/* Exported function to read the current line buffer */
365
366static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000367get_line_buffer(PyObject *self)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000368{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000369 return PyString_FromString(rl_line_buffer);
370}
371
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000372PyDoc_STRVAR(doc_get_line_buffer,
373"get_line_buffer() -> string\n\
374return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000375
376/* Exported function to insert text into the line buffer */
377
378static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000379insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000380{
381 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000382 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000383 return NULL;
384 rl_insert_text(s);
385 Py_INCREF(Py_None);
386 return Py_None;
387}
388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000389PyDoc_STRVAR(doc_insert_text,
390"insert_text(string) -> None\n\
391Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000392
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000393static PyObject *
394redisplay(PyObject *self)
395{
396 rl_redisplay();
397 Py_INCREF(Py_None);
398 return Py_None;
399}
400
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000401PyDoc_STRVAR(doc_redisplay,
402"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000403Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000404contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000405
406/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000407
408static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000409{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000410 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Neal Norwitz767f8352002-03-31 16:13:39 +0000411 {"get_line_buffer", (PyCFunction)get_line_buffer,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000412 METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000413 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000414 {"redisplay", (PyCFunction)redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000415 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
416 {"read_history_file", read_history_file,
417 METH_VARARGS, doc_read_history_file},
418 {"write_history_file", write_history_file,
419 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000420 {"get_history_item", get_history_item,
421 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000422 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000423 METH_NOARGS, doc_get_current_history_length},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000424 {"set_history_length", set_history_length,
425 METH_VARARGS, set_history_length_doc},
426 {"get_history_length", get_history_length,
427 METH_VARARGS, get_history_length_doc},
428 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Neal Norwitz767f8352002-03-31 16:13:39 +0000429 {"get_begidx", (PyCFunction)get_begidx, METH_NOARGS, doc_get_begidx},
430 {"get_endidx", (PyCFunction)get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000431
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000432 {"set_completer_delims", set_completer_delims,
433 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000434 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Neal Norwitz767f8352002-03-31 16:13:39 +0000435 {"get_completer_delims", (PyCFunction)get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000436 METH_NOARGS, doc_get_completer_delims},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000437
438 {"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook},
439#ifdef HAVE_RL_PRE_INPUT_HOOK
440 {"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook},
441#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000442 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000443};
444
Martin v. Löwis0daad592001-09-30 21:09:59 +0000445/* C function to call the Python hooks. */
446
447static int
448on_hook(PyObject *func, PyThreadState *tstate)
449{
450 int result = 0;
451 if (func != NULL) {
452 PyObject *r;
453 PyThreadState *save_tstate;
454 /* Note that readline is called with the interpreter
455 lock released! */
456 save_tstate = PyThreadState_Swap(NULL);
457 PyEval_RestoreThread(tstate);
458 r = PyObject_CallFunction(func, NULL);
459 if (r == NULL)
460 goto error;
461 if (r == Py_None)
462 result = 0;
463 else
464 result = PyInt_AsLong(r);
465 Py_DECREF(r);
466 goto done;
467 error:
468 PyErr_Clear();
469 Py_XDECREF(r);
470 done:
471 PyEval_SaveThread();
472 PyThreadState_Swap(save_tstate);
473 }
474 return result;
475}
476
477static int
478on_startup_hook(void)
479{
480 return on_hook(startup_hook, startup_hook_tstate);
481}
482
483#ifdef HAVE_RL_PRE_INPUT_HOOK
484static int
485on_pre_input_hook(void)
486{
487 return on_hook(pre_input_hook, pre_input_hook_tstate);
488}
489#endif
490
Guido van Rossum290900a1997-09-26 21:51:21 +0000491/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000492
Guido van Rossum290900a1997-09-26 21:51:21 +0000493static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000494on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000495{
Guido van Rossum290900a1997-09-26 21:51:21 +0000496 char *result = NULL;
497 if (completer != NULL) {
498 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000499 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000500 /* Note that readline is called with the interpreter
501 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000502 save_tstate = PyThreadState_Swap(NULL);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000503 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000504 /* Don't use the default filename completion if we
505 * have a custom completion function... */
506 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000507 r = PyObject_CallFunction(completer, "si", text, state);
508 if (r == NULL)
509 goto error;
510 if (r == Py_None) {
511 result = NULL;
512 }
513 else {
514 char *s = PyString_AsString(r);
515 if (s == NULL)
516 goto error;
517 result = strdup(s);
518 }
519 Py_DECREF(r);
520 goto done;
521 error:
522 PyErr_Clear();
523 Py_XDECREF(r);
524 done:
525 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000526 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000527 }
528 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000529}
530
Guido van Rossum290900a1997-09-26 21:51:21 +0000531
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000532/* a more flexible constructor that saves the "begidx" and "endidx"
533 * before calling the normal completer */
534
535char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000536flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000537{
538 Py_XDECREF(begidx);
539 Py_XDECREF(endidx);
540 begidx = PyInt_FromLong((long) start);
541 endidx = PyInt_FromLong((long) end);
542 return completion_matches(text, *on_completion);
543}
544
Guido van Rossum290900a1997-09-26 21:51:21 +0000545/* Helper to initialize GNU readline properly. */
546
547static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000548setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000549{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000550#ifdef SAVE_LOCALE
551 char *saved_locale = setlocale(LC_CTYPE, NULL);
552#endif
553
Skip Montanaroa0392742002-06-11 14:32:46 +0000554 using_history();
555
Guido van Rossum290900a1997-09-26 21:51:21 +0000556 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000557#if defined(PYOS_OS2) && defined(PYCC_GCC)
558 /* Allow $if term= in .inputrc to work */
559 rl_terminal_name = getenv("TERM");
560#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000561 /* Force rebind of TAB to insert-tab */
562 rl_bind_key('\t', rl_insert);
563 /* Bind both ESC-TAB and ESC-ESC to the completion function */
564 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
565 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000566 /* Set our hook functions */
567 rl_startup_hook = (Function *)on_startup_hook;
568#ifdef HAVE_RL_PRE_INPUT_HOOK
569 rl_pre_input_hook = (Function *)on_pre_input_hook;
570#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000571 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000572 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000573 /* Set Python word break characters */
574 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000575 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000576 /* All nonalphanums except '.' */
Guido van Rossum84271bb2002-05-30 15:41:56 +0000577 rl_completion_append_character ='\0';
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000578
579 begidx = PyInt_FromLong(0L);
580 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000581 /* Initialize (allows .inputrc to override)
582 *
583 * XXX: A bug in the readline-2.2 library causes a memory leak
584 * inside this function. Nothing we can do about it.
585 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000586 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000587
588#ifdef SAVE_LOCALE
589 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
590#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000591}
592
593
594/* Interrupt handler */
595
596static jmp_buf jbuf;
597
Guido van Rossum0969d361997-08-05 21:27:50 +0000598/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000599static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000600onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000601{
Guido van Rossum290900a1997-09-26 21:51:21 +0000602 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000603}
604
Guido van Rossum290900a1997-09-26 21:51:21 +0000605
606/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000607
608static char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000609call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000610{
Guido van Rossum26418a92000-06-28 21:30:31 +0000611 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000612 char *p, *q;
Guido van Rossum174efc92000-09-16 16:37:53 +0000613 PyOS_sighandler_t old_inthandler;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000614
Guido van Rossum174efc92000-09-16 16:37:53 +0000615 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000616 if (setjmp(jbuf)) {
617#ifdef HAVE_SIGRELSE
618 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
619 sigrelse(SIGINT);
620#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000621 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossum0969d361997-08-05 21:27:50 +0000622 return NULL;
623 }
Guido van Rossum44620641997-08-11 18:57:29 +0000624 rl_event_hook = PyOS_InputHook;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000625
626 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
627 rl_instream = sys_stdin;
628 rl_outstream = sys_stdout;
629 rl_prep_terminal (1);
630 }
631
Guido van Rossum0969d361997-08-05 21:27:50 +0000632 p = readline(prompt);
Guido van Rossum174efc92000-09-16 16:37:53 +0000633 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000634
635 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000636 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000637 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000638 if (p != NULL)
639 *p = '\0';
640 return p;
641 }
642 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000643 if (n > 0) {
644 char *line;
645 HISTORY_STATE *state = history_get_history_state();
646 if (state->length > 0)
647 line = history_get(state->length)->line;
648 else
649 line = "";
650 if (strcmp(p, line))
651 add_history(p);
652 /* the history docs don't say so, but the address of state
653 changes each time history_get_history_state is called
654 which makes me think it's freshly malloc'd memory...
655 on the other hand, the address of the last line stays the
656 same as long as history isn't extended, so it appears to
657 be malloc'd but managed by the history package... */
658 free(state);
659 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000660 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
661 release the original. */
662 q = p;
663 p = PyMem_Malloc(n+2);
664 if (p != NULL) {
665 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000666 p[n] = '\n';
667 p[n+1] = '\0';
668 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000669 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000670 return p;
671}
672
Guido van Rossum290900a1997-09-26 21:51:21 +0000673
674/* Initialize the module */
675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000676PyDoc_STRVAR(doc_module,
677"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000678
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000679PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000680initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000681{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000682 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000683
684 m = Py_InitModule4("readline", readline_methods, doc_module,
685 (PyObject *)NULL, PYTHON_API_VERSION);
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000686
687 PyOS_ReadlineFunctionPointer = call_readline;
688 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000689}