blob: bc8562ea651d2843b00d9fd6d44fa3161f69d2cf [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
Guido van Rossum74f31432003-01-07 20:01:29 +000030#define completion_matches(x, y) \
31 rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
Guido van Rossum353ae582001-07-10 16:45:32 +000032#endif
33
Guido van Rossum290900a1997-09-26 21:51:21 +000034/* Pointers needed from outside (but not declared in a header file). */
Guido van Rossum5a530192001-01-10 21:03:32 +000035extern DL_IMPORT(int) (*PyOS_InputHook)(void);
Martin v. Löwis566f6af2002-10-26 14:39:10 +000036extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *,char *);
Guido van Rossum0969d361997-08-05 21:27:50 +000037
Guido van Rossum0969d361997-08-05 21:27:50 +000038
Guido van Rossum290900a1997-09-26 21:51:21 +000039/* Exported function to send one line to readline's init file parser */
40
41static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000042parse_and_bind(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000043{
Guido van Rossum3b5330e1998-12-04 15:34:39 +000044 char *s, *copy;
Guido van Rossum43713e52000-02-29 13:59:29 +000045 if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000046 return NULL;
Guido van Rossum3b5330e1998-12-04 15:34:39 +000047 /* Make a copy -- rl_parse_and_bind() modifies its argument */
48 /* Bernard Herzog */
49 copy = malloc(1 + strlen(s));
50 if (copy == NULL)
51 return PyErr_NoMemory();
52 strcpy(copy, s);
53 rl_parse_and_bind(copy);
54 free(copy); /* Free the copy */
Guido van Rossum290900a1997-09-26 21:51:21 +000055 Py_INCREF(Py_None);
56 return Py_None;
57}
58
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000059PyDoc_STRVAR(doc_parse_and_bind,
60"parse_and_bind(string) -> None\n\
61Parse and execute single line of a readline init file.");
Guido van Rossum290900a1997-09-26 21:51:21 +000062
63
64/* Exported function to parse a readline init file */
65
66static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000067read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000068{
69 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000070 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000071 return NULL;
72 errno = rl_read_init_file(s);
73 if (errno)
74 return PyErr_SetFromErrno(PyExc_IOError);
75 Py_INCREF(Py_None);
76 return Py_None;
77}
78
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000079PyDoc_STRVAR(doc_read_init_file,
80"read_init_file([filename]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +000081Parse a readline initialization file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000082The default filename is the last filename used.");
Guido van Rossum290900a1997-09-26 21:51:21 +000083
84
Skip Montanaro28067822000-07-06 18:55:12 +000085/* Exported function to load a readline history file */
86
87static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000088read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000089{
90 char *s = NULL;
91 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
92 return NULL;
93 errno = read_history(s);
94 if (errno)
95 return PyErr_SetFromErrno(PyExc_IOError);
96 Py_INCREF(Py_None);
97 return Py_None;
98}
99
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000100static int history_length = -1; /* do not truncate history by default */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000101PyDoc_STRVAR(doc_read_history_file,
102"read_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000103Load a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000105
106
107/* Exported function to save a readline history file */
108
109static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000110write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000111{
112 char *s = NULL;
113 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
114 return NULL;
115 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000116 if (!errno && history_length >= 0)
117 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000118 if (errno)
119 return PyErr_SetFromErrno(PyExc_IOError);
120 Py_INCREF(Py_None);
121 return Py_None;
122}
123
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000124PyDoc_STRVAR(doc_write_history_file,
125"write_history_file([filename]) -> None\n\
Skip Montanaro28067822000-07-06 18:55:12 +0000126Save a readline history file.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127The default filename is ~/.history.");
Skip Montanaro28067822000-07-06 18:55:12 +0000128
129
Guido van Rossum74f31432003-01-07 20:01:29 +0000130/* Set history length */
131
132static PyObject*
133set_history_length(PyObject *self, PyObject *args)
134{
135 int length = history_length;
136 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
137 return NULL;
138 history_length = length;
139 Py_INCREF(Py_None);
140 return Py_None;
141}
142
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000143PyDoc_STRVAR(set_history_length_doc,
144"set_history_length(length) -> None\n\
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000145set the maximal number of items which will be written to\n\
146the history file. A negative length is used to inhibit\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000147history truncation.");
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000148
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000149
Guido van Rossum74f31432003-01-07 20:01:29 +0000150/* Get history length */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000151
152static PyObject*
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000153get_history_length(PyObject *self, PyObject *noarg)
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000154{
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000155 return PyInt_FromLong(history_length);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000156}
157
Guido van Rossum74f31432003-01-07 20:01:29 +0000158PyDoc_STRVAR(get_history_length_doc,
159"get_history_length() -> int\n\
160return the maximum number of items that will be written to\n\
161the history file.");
162
163
Martin v. Löwis0daad592001-09-30 21:09:59 +0000164/* Generic hook function setter */
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000165
Martin v. Löwis0daad592001-09-30 21:09:59 +0000166static PyObject *
Guido van Rossum74f31432003-01-07 20:01:29 +0000167set_hook(const char *funcname, PyObject **hook_var,
168 PyThreadState **tstate, PyObject *args)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000169{
170 PyObject *function = Py_None;
171 char buf[80];
Tim Peters885d4572001-11-28 20:27:42 +0000172 PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000173 if (!PyArg_ParseTuple(args, buf, &function))
174 return NULL;
175 if (function == Py_None) {
176 Py_XDECREF(*hook_var);
177 *hook_var = NULL;
178 *tstate = NULL;
179 }
180 else if (PyCallable_Check(function)) {
181 PyObject *tmp = *hook_var;
182 Py_INCREF(function);
183 *hook_var = function;
184 Py_XDECREF(tmp);
185 *tstate = PyThreadState_Get();
186 }
187 else {
Tim Peters885d4572001-11-28 20:27:42 +0000188 PyOS_snprintf(buf, sizeof(buf),
189 "set_%.50s(func): argument not callable",
190 funcname);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000191 PyErr_SetString(PyExc_TypeError, buf);
192 return NULL;
193 }
194 Py_INCREF(Py_None);
195 return Py_None;
196}
197
Guido van Rossum74f31432003-01-07 20:01:29 +0000198
Martin v. Löwis0daad592001-09-30 21:09:59 +0000199/* Exported functions to specify hook functions in Python */
200
201static PyObject *startup_hook = NULL;
202static PyThreadState *startup_hook_tstate = NULL;
203
204#ifdef HAVE_RL_PRE_INPUT_HOOK
205static PyObject *pre_input_hook = NULL;
206static PyThreadState *pre_input_hook_tstate = NULL;
207#endif
208
209static PyObject *
210set_startup_hook(PyObject *self, PyObject *args)
211{
Guido van Rossum74f31432003-01-07 20:01:29 +0000212 return set_hook("startup_hook", &startup_hook,
213 &startup_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000214}
215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000216PyDoc_STRVAR(doc_set_startup_hook,
217"set_startup_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000218Set or remove the startup_hook function.\n\
219The function is called with no arguments just\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000220before readline prints the first prompt.");
Martin v. Löwis0daad592001-09-30 21:09:59 +0000221
Guido van Rossum74f31432003-01-07 20:01:29 +0000222
Martin v. Löwis0daad592001-09-30 21:09:59 +0000223#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000224
225/* Set pre-input hook */
226
Martin v. Löwis0daad592001-09-30 21:09:59 +0000227static PyObject *
228set_pre_input_hook(PyObject *self, PyObject *args)
229{
Guido van Rossum74f31432003-01-07 20:01:29 +0000230 return set_hook("pre_input_hook", &pre_input_hook,
231 &pre_input_hook_tstate, args);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000232}
233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000234PyDoc_STRVAR(doc_set_pre_input_hook,
235"set_pre_input_hook([function]) -> None\n\
Martin v. Löwis0daad592001-09-30 21:09:59 +0000236Set or remove the pre_input_hook function.\n\
237The function is called with no arguments after the first prompt\n\
238has been printed and just before readline starts reading input\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239characters.");
Guido van Rossum74f31432003-01-07 20:01:29 +0000240
Martin v. Löwis0daad592001-09-30 21:09:59 +0000241#endif
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000242
Guido van Rossum74f31432003-01-07 20:01:29 +0000243
Guido van Rossum290900a1997-09-26 21:51:21 +0000244/* Exported function to specify a word completer in Python */
245
246static PyObject *completer = NULL;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000247static PyThreadState *completer_tstate = NULL;
Guido van Rossum290900a1997-09-26 21:51:21 +0000248
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000249static PyObject *begidx = NULL;
250static PyObject *endidx = NULL;
251
Guido van Rossum74f31432003-01-07 20:01:29 +0000252
253/* Get the beginning index for the scope of the tab-completion */
254
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000255static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000256get_begidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000257{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000258 Py_INCREF(begidx);
259 return begidx;
260}
261
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000262PyDoc_STRVAR(doc_get_begidx,
263"get_begidx() -> int\n\
264get the beginning index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000265
Guido van Rossum74f31432003-01-07 20:01:29 +0000266
267/* Get the ending index for the scope of the tab-completion */
268
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000269static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000270get_endidx(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000271{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000272 Py_INCREF(endidx);
273 return endidx;
274}
275
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000276PyDoc_STRVAR(doc_get_endidx,
277"get_endidx() -> int\n\
278get the ending index of the readline tab-completion scope");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000279
280
Guido van Rossum74f31432003-01-07 20:01:29 +0000281/* Set the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000282
283static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000284set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000285{
286 char *break_chars;
287
Guido van Rossum43713e52000-02-29 13:59:29 +0000288 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000289 return NULL;
290 }
Neal Norwitz0e0ee592002-04-21 15:03:18 +0000291 free((void*)rl_completer_word_break_characters);
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000292 rl_completer_word_break_characters = strdup(break_chars);
293 Py_INCREF(Py_None);
294 return Py_None;
295}
296
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000297PyDoc_STRVAR(doc_set_completer_delims,
298"set_completer_delims(string) -> None\n\
299set the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000300
Guido van Rossum74f31432003-01-07 20:01:29 +0000301
302/* Add a line to the history buffer */
303
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000304static PyObject *
305py_add_history(PyObject *self, PyObject *args)
306{
307 char *line;
308
309 if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
310 return NULL;
311 }
312 add_history(line);
313 Py_INCREF(Py_None);
314 return Py_None;
315}
316
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000317PyDoc_STRVAR(doc_add_history,
318"add_history(string) -> None\n\
319add a line to the history buffer");
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000320
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000321
Guido van Rossum74f31432003-01-07 20:01:29 +0000322/* Get the tab-completion word-delimiters that readline uses */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000323
324static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000325get_completer_delims(PyObject *self, PyObject *noarg)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000326{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000327 return PyString_FromString(rl_completer_word_break_characters);
328}
Guido van Rossum74f31432003-01-07 20:01:29 +0000329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000330PyDoc_STRVAR(doc_get_completer_delims,
331"get_completer_delims() -> string\n\
332get the readline word delimiters for tab-completion");
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000333
Guido van Rossum74f31432003-01-07 20:01:29 +0000334
335/* Set the completer function */
336
Guido van Rossum290900a1997-09-26 21:51:21 +0000337static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000338set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000339{
Martin v. Löwis0daad592001-09-30 21:09:59 +0000340 return set_hook("completer", &completer, &completer_tstate, args);
Guido van Rossum290900a1997-09-26 21:51:21 +0000341}
342
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000343PyDoc_STRVAR(doc_set_completer,
344"set_completer([function]) -> None\n\
Guido van Rossum290900a1997-09-26 21:51:21 +0000345Set or remove the completer function.\n\
346The function is called as function(text, state),\n\
Fred Drake52d55a32001-08-01 21:44:14 +0000347for state in 0, 1, 2, ..., until it returns a non-string.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000348It should return the next possible completion starting with 'text'.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000349
Guido van Rossum74f31432003-01-07 20:01:29 +0000350
Michael W. Hudson796df152003-01-30 10:12:51 +0000351static PyObject *
Neal Norwitzd9efdc52003-03-01 15:19:41 +0000352get_completer(PyObject *self, PyObject *noargs)
Michael W. Hudson796df152003-01-30 10:12:51 +0000353{
354 if (completer == NULL) {
355 Py_INCREF(Py_None);
356 return Py_None;
357 }
358 Py_INCREF(completer);
359 return completer;
360}
361
362PyDoc_STRVAR(doc_get_completer,
363"get_completer() -> function\n\
364\n\
365Returns current completer function.");
366
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000367/* Exported function to get any element of history */
368
369static PyObject *
370get_history_item(PyObject *self, PyObject *args)
371{
372 int idx = 0;
373 HIST_ENTRY *hist_ent;
374
375 if (!PyArg_ParseTuple(args, "i:index", &idx))
376 return NULL;
377 if ((hist_ent = history_get(idx)))
Guido van Rossum05ac4492003-01-07 20:04:12 +0000378 return PyString_FromString(hist_ent->line);
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000379 else {
380 Py_INCREF(Py_None);
381 return Py_None;
382 }
383}
384
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000385PyDoc_STRVAR(doc_get_history_item,
386"get_history_item() -> string\n\
387return the current contents of history item at index.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000388
Guido van Rossum74f31432003-01-07 20:01:29 +0000389
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000390/* Exported function to get current length of history */
391
392static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000393get_current_history_length(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000394{
395 HISTORY_STATE *hist_st;
396
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000397 hist_st = history_get_history_state();
398 return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
399}
400
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000401PyDoc_STRVAR(doc_get_current_history_length,
402"get_current_history_length() -> integer\n\
403return the current (not the maximum) length of history.");
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000404
Guido van Rossum74f31432003-01-07 20:01:29 +0000405
Guido van Rossum79378ff1997-10-07 14:53:21 +0000406/* Exported function to read the current line buffer */
407
408static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000409get_line_buffer(PyObject *self, PyObject *noarg)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000410{
Guido van Rossum79378ff1997-10-07 14:53:21 +0000411 return PyString_FromString(rl_line_buffer);
412}
413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000414PyDoc_STRVAR(doc_get_line_buffer,
415"get_line_buffer() -> string\n\
416return the current contents of the line buffer.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000417
Guido van Rossum74f31432003-01-07 20:01:29 +0000418
Guido van Rossum79378ff1997-10-07 14:53:21 +0000419/* Exported function to insert text into the line buffer */
420
421static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000422insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000423{
424 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000425 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000426 return NULL;
427 rl_insert_text(s);
428 Py_INCREF(Py_None);
429 return Py_None;
430}
431
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000432PyDoc_STRVAR(doc_insert_text,
433"insert_text(string) -> None\n\
434Insert text into the command line.");
Guido van Rossum79378ff1997-10-07 14:53:21 +0000435
Guido van Rossum74f31432003-01-07 20:01:29 +0000436
437/* Redisplay the line buffer */
438
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000439static PyObject *
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000440redisplay(PyObject *self, PyObject *noarg)
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000441{
442 rl_redisplay();
443 Py_INCREF(Py_None);
444 return Py_None;
445}
446
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000447PyDoc_STRVAR(doc_redisplay,
448"redisplay() -> None\n\
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000449Change what's displayed on the screen to reflect the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000450contents of the line buffer.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000451
Guido van Rossum74f31432003-01-07 20:01:29 +0000452
Guido van Rossum290900a1997-09-26 21:51:21 +0000453/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000454
455static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000456{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000457 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000458 {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000459 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000460 {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000461 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000462 {"read_history_file", read_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000463 METH_VARARGS, doc_read_history_file},
Guido van Rossum74f31432003-01-07 20:01:29 +0000464 {"write_history_file", write_history_file,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000465 METH_VARARGS, doc_write_history_file},
Neil Schemenauer0f75e0d2002-03-24 01:09:04 +0000466 {"get_history_item", get_history_item,
467 METH_VARARGS, doc_get_history_item},
Neal Norwitz767f8352002-03-31 16:13:39 +0000468 {"get_current_history_length", (PyCFunction)get_current_history_length,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000469 METH_NOARGS, doc_get_current_history_length},
Guido van Rossum74f31432003-01-07 20:01:29 +0000470 {"set_history_length", set_history_length,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000471 METH_VARARGS, set_history_length_doc},
Guido van Rossum74f31432003-01-07 20:01:29 +0000472 {"get_history_length", get_history_length,
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000473 METH_NOARGS, get_history_length_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000474 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Michael W. Hudson796df152003-01-30 10:12:51 +0000475 {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000476 {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
477 {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000478
Guido van Rossum74f31432003-01-07 20:01:29 +0000479 {"set_completer_delims", set_completer_delims,
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000480 METH_VARARGS, doc_set_completer_delims},
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000481 {"add_history", py_add_history, METH_VARARGS, doc_add_history},
Michael W. Hudson0e986a32003-01-30 14:17:16 +0000482 {"get_completer_delims", get_completer_delims,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000483 METH_NOARGS, doc_get_completer_delims},
Guido van Rossum74f31432003-01-07 20:01:29 +0000484
485 {"set_startup_hook", set_startup_hook,
486 METH_VARARGS, doc_set_startup_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000487#ifdef HAVE_RL_PRE_INPUT_HOOK
Guido van Rossum74f31432003-01-07 20:01:29 +0000488 {"set_pre_input_hook", set_pre_input_hook,
489 METH_VARARGS, doc_set_pre_input_hook},
Martin v. Löwis0daad592001-09-30 21:09:59 +0000490#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000491 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000492};
493
Guido van Rossum05ac4492003-01-07 20:04:12 +0000494
Martin v. Löwis0daad592001-09-30 21:09:59 +0000495/* C function to call the Python hooks. */
496
497static int
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000498on_hook(PyObject *func, PyThreadState **tstate)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000499{
500 int result = 0;
501 if (func != NULL) {
502 PyObject *r;
Martin v. Löwis0daad592001-09-30 21:09:59 +0000503 /* Note that readline is called with the interpreter
504 lock released! */
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000505 PyEval_RestoreThread(*tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000506 r = PyObject_CallFunction(func, NULL);
507 if (r == NULL)
508 goto error;
Guido van Rossum74f31432003-01-07 20:01:29 +0000509 if (r == Py_None)
Martin v. Löwis0daad592001-09-30 21:09:59 +0000510 result = 0;
Guido van Rossum74f31432003-01-07 20:01:29 +0000511 else
Martin v. Löwis0daad592001-09-30 21:09:59 +0000512 result = PyInt_AsLong(r);
513 Py_DECREF(r);
514 goto done;
515 error:
516 PyErr_Clear();
517 Py_XDECREF(r);
518 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000519 *tstate = PyEval_SaveThread();
Martin v. Löwis0daad592001-09-30 21:09:59 +0000520 }
521 return result;
522}
523
524static int
525on_startup_hook(void)
526{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000527 return on_hook(startup_hook, &startup_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000528}
529
530#ifdef HAVE_RL_PRE_INPUT_HOOK
531static int
532on_pre_input_hook(void)
533{
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000534 return on_hook(pre_input_hook, &pre_input_hook_tstate);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000535}
536#endif
537
Guido van Rossum05ac4492003-01-07 20:04:12 +0000538
Guido van Rossum290900a1997-09-26 21:51:21 +0000539/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000540
Guido van Rossum290900a1997-09-26 21:51:21 +0000541static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000542on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000543{
Guido van Rossum290900a1997-09-26 21:51:21 +0000544 char *result = NULL;
545 if (completer != NULL) {
546 PyObject *r;
547 /* Note that readline is called with the interpreter
548 lock released! */
Martin v. Löwis0daad592001-09-30 21:09:59 +0000549 PyEval_RestoreThread(completer_tstate);
Michael W. Hudson0c1ceaf2002-02-13 11:58:25 +0000550 /* Don't use the default filename completion if we
551 * have a custom completion function... */
552 rl_attempted_completion_over = 1;
Guido van Rossum290900a1997-09-26 21:51:21 +0000553 r = PyObject_CallFunction(completer, "si", text, state);
554 if (r == NULL)
555 goto error;
556 if (r == Py_None) {
557 result = NULL;
558 }
559 else {
560 char *s = PyString_AsString(r);
561 if (s == NULL)
562 goto error;
563 result = strdup(s);
564 }
565 Py_DECREF(r);
566 goto done;
567 error:
568 PyErr_Clear();
569 Py_XDECREF(r);
570 done:
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000571 completer_tstate = PyEval_SaveThread();
Guido van Rossum290900a1997-09-26 21:51:21 +0000572 }
573 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000574}
575
Guido van Rossum290900a1997-09-26 21:51:21 +0000576
Guido van Rossum6d0d3652003-01-07 20:34:19 +0000577/* A more flexible constructor that saves the "begidx" and "endidx"
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000578 * before calling the normal completer */
579
Neal Norwitzc355f0c2003-02-21 00:30:18 +0000580static char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000581flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000582{
583 Py_XDECREF(begidx);
584 Py_XDECREF(endidx);
585 begidx = PyInt_FromLong((long) start);
586 endidx = PyInt_FromLong((long) end);
587 return completion_matches(text, *on_completion);
588}
589
Guido van Rossum05ac4492003-01-07 20:04:12 +0000590
Guido van Rossum290900a1997-09-26 21:51:21 +0000591/* Helper to initialize GNU readline properly. */
592
593static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000594setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000595{
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000596#ifdef SAVE_LOCALE
597 char *saved_locale = setlocale(LC_CTYPE, NULL);
598#endif
599
Skip Montanaroa0392742002-06-11 14:32:46 +0000600 using_history();
601
Guido van Rossum290900a1997-09-26 21:51:21 +0000602 rl_readline_name = "python";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000603#if defined(PYOS_OS2) && defined(PYCC_GCC)
604 /* Allow $if term= in .inputrc to work */
605 rl_terminal_name = getenv("TERM");
606#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000607 /* Force rebind of TAB to insert-tab */
608 rl_bind_key('\t', rl_insert);
609 /* Bind both ESC-TAB and ESC-ESC to the completion function */
610 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
611 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
Martin v. Löwis0daad592001-09-30 21:09:59 +0000612 /* Set our hook functions */
613 rl_startup_hook = (Function *)on_startup_hook;
614#ifdef HAVE_RL_PRE_INPUT_HOOK
615 rl_pre_input_hook = (Function *)on_pre_input_hook;
616#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000617 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000618 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000619 /* Set Python word break characters */
620 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000621 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000622 /* All nonalphanums except '.' */
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000623#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum84271bb2002-05-30 15:41:56 +0000624 rl_completion_append_character ='\0';
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000625#endif
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000626
627 begidx = PyInt_FromLong(0L);
628 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000629 /* Initialize (allows .inputrc to override)
630 *
631 * XXX: A bug in the readline-2.2 library causes a memory leak
632 * inside this function. Nothing we can do about it.
633 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000634 rl_initialize();
Guido van Rossum60c8a3a2002-10-09 21:27:33 +0000635
636#ifdef SAVE_LOCALE
637 setlocale(LC_CTYPE, saved_locale); /* Restore locale */
638#endif
Guido van Rossum290900a1997-09-26 21:51:21 +0000639}
640
641
642/* Interrupt handler */
643
644static jmp_buf jbuf;
645
Guido van Rossum0969d361997-08-05 21:27:50 +0000646/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000647static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000648onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000649{
Guido van Rossum290900a1997-09-26 21:51:21 +0000650 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000651}
652
Guido van Rossum290900a1997-09-26 21:51:21 +0000653
654/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000655
656static char *
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000657call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000658{
Guido van Rossum26418a92000-06-28 21:30:31 +0000659 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000660 char *p, *q;
Guido van Rossum174efc92000-09-16 16:37:53 +0000661 PyOS_sighandler_t old_inthandler;
Guido van Rossum74f31432003-01-07 20:01:29 +0000662
Guido van Rossum174efc92000-09-16 16:37:53 +0000663 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000664 if (setjmp(jbuf)) {
665#ifdef HAVE_SIGRELSE
666 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
667 sigrelse(SIGINT);
668#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000669 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossum0969d361997-08-05 21:27:50 +0000670 return NULL;
671 }
Guido van Rossum44620641997-08-11 18:57:29 +0000672 rl_event_hook = PyOS_InputHook;
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000673
Guido van Rossum74f31432003-01-07 20:01:29 +0000674 if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
675 rl_instream = sys_stdin;
676 rl_outstream = sys_stdout;
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000677#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
Guido van Rossum74f31432003-01-07 20:01:29 +0000678 rl_prep_terminal (1);
Guido van Rossumfaf5e4d2002-12-30 16:25:41 +0000679#endif
Guido van Rossum74f31432003-01-07 20:01:29 +0000680 }
681
Guido van Rossum0969d361997-08-05 21:27:50 +0000682 p = readline(prompt);
Guido van Rossum174efc92000-09-16 16:37:53 +0000683 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000684
685 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000686 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000687 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000688 if (p != NULL)
689 *p = '\0';
690 return p;
691 }
692 n = strlen(p);
Skip Montanaroa0392742002-06-11 14:32:46 +0000693 if (n > 0) {
694 char *line;
695 HISTORY_STATE *state = history_get_history_state();
696 if (state->length > 0)
697 line = history_get(state->length)->line;
698 else
699 line = "";
700 if (strcmp(p, line))
701 add_history(p);
702 /* the history docs don't say so, but the address of state
703 changes each time history_get_history_state is called
704 which makes me think it's freshly malloc'd memory...
705 on the other hand, the address of the last line stays the
706 same as long as history isn't extended, so it appears to
707 be malloc'd but managed by the history package... */
708 free(state);
709 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000710 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
711 release the original. */
712 q = p;
713 p = PyMem_Malloc(n+2);
714 if (p != NULL) {
715 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000716 p[n] = '\n';
717 p[n+1] = '\0';
718 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000719 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000720 return p;
721}
722
Guido van Rossum290900a1997-09-26 21:51:21 +0000723
724/* Initialize the module */
725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000726PyDoc_STRVAR(doc_module,
727"Importing this module enables command line editing using GNU readline.");
Guido van Rossum290900a1997-09-26 21:51:21 +0000728
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000729PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000730initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000731{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000732 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000733
734 m = Py_InitModule4("readline", readline_methods, doc_module,
735 (PyObject *)NULL, PYTHON_API_VERSION);
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000736
Guido van Rossum74f31432003-01-07 20:01:29 +0000737 PyOS_ReadlineFunctionPointer = call_readline;
738 setup_readline();
Guido van Rossum0969d361997-08-05 21:27:50 +0000739}