blob: 841fe0219a4ba4b311351974cec7023cc7590878 [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 Rossum73bacfc1998-01-19 22:05:22 +000015#ifdef HAVE_UNISTD_H
16#include <unistd.h> /* For isatty() */
17#endif
18
Guido van Rossum290900a1997-09-26 21:51:21 +000019/* GNU readline definitions */
Guido van Rossumb0e51b22001-04-13 18:14:27 +000020#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
Guido van Rossumbcc20741998-08-04 22:53:56 +000021#include <readline/readline.h>
22#include <readline/history.h>
Guido van Rossum730806d1998-04-10 22:27:42 +000023
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
49static char doc_parse_and_bind[] = "\
50parse_and_bind(string) -> None\n\
51Parse and execute single line of a readline init file.\
52";
53
54
55/* Exported function to parse a readline init file */
56
57static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000058read_init_file(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +000059{
60 char *s = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +000061 if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
Guido van Rossum290900a1997-09-26 21:51:21 +000062 return NULL;
63 errno = rl_read_init_file(s);
64 if (errno)
65 return PyErr_SetFromErrno(PyExc_IOError);
66 Py_INCREF(Py_None);
67 return Py_None;
68}
69
70static char doc_read_init_file[] = "\
71read_init_file([filename]) -> None\n\
72Parse a readline initialization file.\n\
73The default filename is the last filename used.\
74";
75
76
Skip Montanaro28067822000-07-06 18:55:12 +000077/* Exported function to load a readline history file */
78
79static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000080read_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +000081{
82 char *s = NULL;
83 if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
84 return NULL;
85 errno = read_history(s);
86 if (errno)
87 return PyErr_SetFromErrno(PyExc_IOError);
88 Py_INCREF(Py_None);
89 return Py_None;
90}
91
Skip Montanaro49bd24d2000-07-19 16:54:53 +000092static int history_length = -1; /* do not truncate history by default */
Skip Montanaro28067822000-07-06 18:55:12 +000093static char doc_read_history_file[] = "\
94read_history_file([filename]) -> None\n\
95Load a readline history file.\n\
96The default filename is ~/.history.\
97";
98
99
100/* Exported function to save a readline history file */
101
102static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000103write_history_file(PyObject *self, PyObject *args)
Skip Montanaro28067822000-07-06 18:55:12 +0000104{
105 char *s = NULL;
106 if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
107 return NULL;
108 errno = write_history(s);
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000109 if (!errno && history_length >= 0)
110 history_truncate_file(s, history_length);
Skip Montanaro28067822000-07-06 18:55:12 +0000111 if (errno)
112 return PyErr_SetFromErrno(PyExc_IOError);
113 Py_INCREF(Py_None);
114 return Py_None;
115}
116
117static char doc_write_history_file[] = "\
118write_history_file([filename]) -> None\n\
119Save a readline history file.\n\
120The default filename is ~/.history.\
121";
122
123
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000124static char set_history_length_doc[] = "\
125set_history_length(length) -> None\n\
126set the maximal number of items which will be written to\n\
127the history file. A negative length is used to inhibit\n\
128history truncation.\n\
129";
130
131static PyObject*
132set_history_length(PyObject *self, PyObject *args)
133{
134 int length = history_length;
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000135 if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
136 return NULL;
137 history_length = length;
138 Py_INCREF(Py_None);
139 return Py_None;
140}
141
142
143
144static char get_history_length_doc[] = "\
145get_history_length() -> int\n\
146return the current history length value.\n\
147";
148
149static PyObject*
150get_history_length(PyObject *self, PyObject *args)
151{
Skip Montanaro49bd24d2000-07-19 16:54:53 +0000152 if (!PyArg_ParseTuple(args, ":get_history_length"))
153 return NULL;
154 return Py_BuildValue("i", history_length);
155}
156
157
158
Guido van Rossum290900a1997-09-26 21:51:21 +0000159/* Exported function to specify a word completer in Python */
160
161static PyObject *completer = NULL;
162static PyThreadState *tstate = NULL;
163
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000164static PyObject *begidx = NULL;
165static PyObject *endidx = NULL;
166
167/* get the beginning index for the scope of the tab-completion */
168static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000169get_begidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000170{
171 if(!PyArg_NoArgs(args)) {
172 return NULL;
173 }
174 Py_INCREF(begidx);
175 return begidx;
176}
177
178static char doc_get_begidx[] = "\
179get_begidx() -> int\n\
180get the beginning index of the readline tab-completion scope";
181
182/* get the ending index for the scope of the tab-completion */
183static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000184get_endidx(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000185{
186 if(!PyArg_NoArgs(args)) {
187 return NULL;
188 }
189 Py_INCREF(endidx);
190 return endidx;
191}
192
193static char doc_get_endidx[] = "\
194get_endidx() -> int\n\
195get the ending index of the readline tab-completion scope";
196
197
198/* set the tab-completion word-delimiters that readline uses */
199
200static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000201set_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000202{
203 char *break_chars;
204
Guido van Rossum43713e52000-02-29 13:59:29 +0000205 if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000206 return NULL;
207 }
208 free(rl_completer_word_break_characters);
209 rl_completer_word_break_characters = strdup(break_chars);
210 Py_INCREF(Py_None);
211 return Py_None;
212}
213
214static char doc_set_completer_delims[] = "\
215set_completer_delims(string) -> None\n\
216set the readline word delimiters for tab-completion";
217
218
219/* get the tab-completion word-delimiters that readline uses */
220
221static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000222get_completer_delims(PyObject *self, PyObject *args)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000223{
224 if(!PyArg_NoArgs(args)) {
225 return NULL;
226 }
227 return PyString_FromString(rl_completer_word_break_characters);
228}
229
230static char doc_get_completer_delims[] = "\
231get_completer_delims() -> string\n\
232get the readline word delimiters for tab-completion";
233
Guido van Rossum290900a1997-09-26 21:51:21 +0000234static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000235set_completer(PyObject *self, PyObject *args)
Guido van Rossum290900a1997-09-26 21:51:21 +0000236{
237 PyObject *function = Py_None;
Guido van Rossum43713e52000-02-29 13:59:29 +0000238 if (!PyArg_ParseTuple(args, "|O:set_completer", &function))
Guido van Rossum290900a1997-09-26 21:51:21 +0000239 return NULL;
240 if (function == Py_None) {
241 Py_XDECREF(completer);
242 completer = NULL;
243 tstate = NULL;
244 }
245 else if (PyCallable_Check(function)) {
246 PyObject *tmp = completer;
247 Py_INCREF(function);
248 completer = function;
249 Py_XDECREF(tmp);
250 tstate = PyThreadState_Get();
251 }
252 else {
253 PyErr_SetString(PyExc_TypeError,
254 "set_completer(func): argument not callable");
255 return NULL;
256 }
257 Py_INCREF(Py_None);
258 return Py_None;
259}
260
261static char doc_set_completer[] = "\
262set_completer([function]) -> None\n\
263Set or remove the completer function.\n\
264The function is called as function(text, state),\n\
265for i in [0, 1, 2, ...] until it returns a non-string.\n\
266It should return the next possible completion starting with 'text'.\
267";
268
Guido van Rossum79378ff1997-10-07 14:53:21 +0000269/* Exported function to read the current line buffer */
270
271static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000272get_line_buffer(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000273{
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000274 if (!PyArg_NoArgs(args))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000275 return NULL;
276 return PyString_FromString(rl_line_buffer);
277}
278
279static char doc_get_line_buffer[] = "\
Guido van Rossuma88c5f31998-05-20 15:50:56 +0000280get_line_buffer() -> string\n\
Guido van Rossum79378ff1997-10-07 14:53:21 +0000281return the current contents of the line buffer.\
282";
283
284/* Exported function to insert text into the line buffer */
285
286static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000287insert_text(PyObject *self, PyObject *args)
Guido van Rossum79378ff1997-10-07 14:53:21 +0000288{
289 char *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000290 if (!PyArg_ParseTuple(args, "s:insert_text", &s))
Guido van Rossum79378ff1997-10-07 14:53:21 +0000291 return NULL;
292 rl_insert_text(s);
293 Py_INCREF(Py_None);
294 return Py_None;
295}
296
297
298static char doc_insert_text[] = "\
299insert_text(string) -> None\n\
300Insert text into the command line.\
301";
302
Guido van Rossum290900a1997-09-26 21:51:21 +0000303
304/* Table of functions exported by the module */
Guido van Rossum0969d361997-08-05 21:27:50 +0000305
306static struct PyMethodDef readline_methods[] =
Guido van Rossum290900a1997-09-26 21:51:21 +0000307{
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000308 {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000309 {"get_line_buffer", get_line_buffer,
310 METH_OLDARGS, doc_get_line_buffer},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000311 {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
312 {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
313 {"read_history_file", read_history_file,
314 METH_VARARGS, doc_read_history_file},
315 {"write_history_file", write_history_file,
316 METH_VARARGS, doc_write_history_file},
317 {"set_history_length", set_history_length,
318 METH_VARARGS, set_history_length_doc},
319 {"get_history_length", get_history_length,
320 METH_VARARGS, get_history_length_doc},
321 {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000322 {"get_begidx", get_begidx, METH_OLDARGS, doc_get_begidx},
323 {"get_endidx", get_endidx, METH_OLDARGS, doc_get_endidx},
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000324
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000325 {"set_completer_delims", set_completer_delims,
326 METH_VARARGS, doc_set_completer_delims},
327 {"get_completer_delims", get_completer_delims,
328 METH_OLDARGS, doc_get_completer_delims},
Guido van Rossum290900a1997-09-26 21:51:21 +0000329 {0, 0}
Guido van Rossum0969d361997-08-05 21:27:50 +0000330};
331
Guido van Rossum290900a1997-09-26 21:51:21 +0000332/* C function to call the Python completer. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000333
Guido van Rossum290900a1997-09-26 21:51:21 +0000334static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000335on_completion(char *text, int state)
Guido van Rossum0969d361997-08-05 21:27:50 +0000336{
Guido van Rossum290900a1997-09-26 21:51:21 +0000337 char *result = NULL;
338 if (completer != NULL) {
339 PyObject *r;
Guido van Rossuma59406a1997-10-10 17:39:19 +0000340 PyThreadState *save_tstate;
Guido van Rossum290900a1997-09-26 21:51:21 +0000341 /* Note that readline is called with the interpreter
342 lock released! */
Guido van Rossuma59406a1997-10-10 17:39:19 +0000343 save_tstate = PyThreadState_Swap(NULL);
Guido van Rossum290900a1997-09-26 21:51:21 +0000344 PyEval_RestoreThread(tstate);
345 r = PyObject_CallFunction(completer, "si", text, state);
346 if (r == NULL)
347 goto error;
348 if (r == Py_None) {
349 result = NULL;
350 }
351 else {
352 char *s = PyString_AsString(r);
353 if (s == NULL)
354 goto error;
355 result = strdup(s);
356 }
357 Py_DECREF(r);
358 goto done;
359 error:
360 PyErr_Clear();
361 Py_XDECREF(r);
362 done:
363 PyEval_SaveThread();
Guido van Rossuma59406a1997-10-10 17:39:19 +0000364 PyThreadState_Swap(save_tstate);
Guido van Rossum290900a1997-09-26 21:51:21 +0000365 }
366 return result;
Guido van Rossum0969d361997-08-05 21:27:50 +0000367}
368
Guido van Rossum290900a1997-09-26 21:51:21 +0000369
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000370/* a more flexible constructor that saves the "begidx" and "endidx"
371 * before calling the normal completer */
372
373char **
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000374flex_complete(char *text, int start, int end)
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000375{
376 Py_XDECREF(begidx);
377 Py_XDECREF(endidx);
378 begidx = PyInt_FromLong((long) start);
379 endidx = PyInt_FromLong((long) end);
380 return completion_matches(text, *on_completion);
381}
382
Guido van Rossum290900a1997-09-26 21:51:21 +0000383/* Helper to initialize GNU readline properly. */
384
385static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000386setup_readline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000387{
388 rl_readline_name = "python";
389 /* Force rebind of TAB to insert-tab */
390 rl_bind_key('\t', rl_insert);
391 /* Bind both ESC-TAB and ESC-ESC to the completion function */
392 rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
393 rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
394 /* Set our completion function */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000395 rl_attempted_completion_function = (CPPFunction *)flex_complete;
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000396 /* Set Python word break characters */
397 rl_completer_word_break_characters =
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000398 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
Guido van Rossumb6c935a1997-09-26 23:00:37 +0000399 /* All nonalphanums except '.' */
Guido van Rossumb960e7a1999-11-18 17:51:02 +0000400
401 begidx = PyInt_FromLong(0L);
402 endidx = PyInt_FromLong(0L);
Barry Warsawf7612871999-01-29 21:55:03 +0000403 /* Initialize (allows .inputrc to override)
404 *
405 * XXX: A bug in the readline-2.2 library causes a memory leak
406 * inside this function. Nothing we can do about it.
407 */
Guido van Rossum290900a1997-09-26 21:51:21 +0000408 rl_initialize();
409}
410
411
412/* Interrupt handler */
413
414static jmp_buf jbuf;
415
Guido van Rossum0969d361997-08-05 21:27:50 +0000416/* ARGSUSED */
Tim Peters4f1b2082000-07-23 21:18:09 +0000417static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000418onintr(int sig)
Guido van Rossum0969d361997-08-05 21:27:50 +0000419{
Guido van Rossum290900a1997-09-26 21:51:21 +0000420 longjmp(jbuf, 1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000421}
422
Guido van Rossum290900a1997-09-26 21:51:21 +0000423
424/* Wrapper around GNU readline that handles signals differently. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000425
426static char *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000427call_readline(char *prompt)
Guido van Rossum0969d361997-08-05 21:27:50 +0000428{
Guido van Rossum26418a92000-06-28 21:30:31 +0000429 size_t n;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000430 char *p, *q;
Guido van Rossum174efc92000-09-16 16:37:53 +0000431 PyOS_sighandler_t old_inthandler;
432
433 old_inthandler = PyOS_setsig(SIGINT, onintr);
Guido van Rossum0969d361997-08-05 21:27:50 +0000434 if (setjmp(jbuf)) {
435#ifdef HAVE_SIGRELSE
436 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
437 sigrelse(SIGINT);
438#endif
Guido van Rossum174efc92000-09-16 16:37:53 +0000439 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossum0969d361997-08-05 21:27:50 +0000440 return NULL;
441 }
Guido van Rossum44620641997-08-11 18:57:29 +0000442 rl_event_hook = PyOS_InputHook;
Guido van Rossum0969d361997-08-05 21:27:50 +0000443 p = readline(prompt);
Guido van Rossum174efc92000-09-16 16:37:53 +0000444 PyOS_setsig(SIGINT, old_inthandler);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000445
446 /* We must return a buffer allocated with PyMem_Malloc. */
Guido van Rossum0969d361997-08-05 21:27:50 +0000447 if (p == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000448 p = PyMem_Malloc(1);
Guido van Rossum0969d361997-08-05 21:27:50 +0000449 if (p != NULL)
450 *p = '\0';
451 return p;
452 }
453 n = strlen(p);
454 if (n > 0)
455 add_history(p);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000456 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
457 release the original. */
458 q = p;
459 p = PyMem_Malloc(n+2);
460 if (p != NULL) {
461 strncpy(p, q, n);
Guido van Rossum0969d361997-08-05 21:27:50 +0000462 p[n] = '\n';
463 p[n+1] = '\0';
464 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000465 free(q);
Guido van Rossum0969d361997-08-05 21:27:50 +0000466 return p;
467}
468
Guido van Rossum290900a1997-09-26 21:51:21 +0000469
470/* Initialize the module */
471
472static char doc_module[] =
473"Importing this module enables command line editing using GNU readline.";
474
Guido van Rossum3886bb61998-12-04 18:50:17 +0000475DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000476initreadline(void)
Guido van Rossum290900a1997-09-26 21:51:21 +0000477{
Guido van Rossum1ea64ea2000-10-02 15:53:08 +0000478 PyObject *m;
Guido van Rossum290900a1997-09-26 21:51:21 +0000479
480 m = Py_InitModule4("readline", readline_methods, doc_module,
481 (PyObject *)NULL, PYTHON_API_VERSION);
482 if (isatty(fileno(stdin))) {
483 PyOS_ReadlineFunctionPointer = call_readline;
484 setup_readline();
485 }
Guido van Rossum0969d361997-08-05 21:27:50 +0000486}