1 /* 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.
5 * More recently, it was largely rewritten by Guido van Rossum who is
9 /* Standard definitions */
16 #include <unistd.h> /* For isatty() */
19 /* GNU readline definitions */
20 #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
21 #include <readline/readline.h>
22 #include <readline/history.h>
24 #ifdef HAVE_RL_COMPLETION_MATCHES
25 #define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
28 /* Pointers needed from outside (but not declared in a header file). */
29 extern DL_IMPORT(int) (*PyOS_InputHook
)(void);
30 extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer
)(char *);
33 /* Exported function to send one line to readline's init file parser */
36 parse_and_bind(PyObject
*self
, PyObject
*args
)
39 if (!PyArg_ParseTuple(args
, "s:parse_and_bind", &s
))
41 /* Make a copy -- rl_parse_and_bind() modifies its argument */
43 copy
= malloc(1 + strlen(s
));
45 return PyErr_NoMemory();
47 rl_parse_and_bind(copy
);
48 free(copy
); /* Free the copy */
53 static char doc_parse_and_bind
[] = "\
54 parse_and_bind(string) -> None\n\
55 Parse and execute single line of a readline init file.\
59 /* Exported function to parse a readline init file */
62 read_init_file(PyObject
*self
, PyObject
*args
)
65 if (!PyArg_ParseTuple(args
, "|z:read_init_file", &s
))
67 errno
= rl_read_init_file(s
);
69 return PyErr_SetFromErrno(PyExc_IOError
);
74 static char doc_read_init_file
[] = "\
75 read_init_file([filename]) -> None\n\
76 Parse a readline initialization file.\n\
77 The default filename is the last filename used.\
81 /* Exported function to load a readline history file */
84 read_history_file(PyObject
*self
, PyObject
*args
)
87 if (!PyArg_ParseTuple(args
, "|z:read_history_file", &s
))
89 errno
= read_history(s
);
91 return PyErr_SetFromErrno(PyExc_IOError
);
96 static int history_length
= -1; /* do not truncate history by default */
97 static char doc_read_history_file
[] = "\
98 read_history_file([filename]) -> None\n\
99 Load a readline history file.\n\
100 The default filename is ~/.history.\
104 /* Exported function to save a readline history file */
107 write_history_file(PyObject
*self
, PyObject
*args
)
110 if (!PyArg_ParseTuple(args
, "|z:write_history_file", &s
))
112 errno
= write_history(s
);
113 if (!errno
&& history_length
>= 0)
114 history_truncate_file(s
, history_length
);
116 return PyErr_SetFromErrno(PyExc_IOError
);
121 static char doc_write_history_file
[] = "\
122 write_history_file([filename]) -> None\n\
123 Save a readline history file.\n\
124 The default filename is ~/.history.\
128 static char set_history_length_doc
[] = "\
129 set_history_length(length) -> None\n\
130 set the maximal number of items which will be written to\n\
131 the history file. A negative length is used to inhibit\n\
132 history truncation.\n\
136 set_history_length(PyObject
*self
, PyObject
*args
)
138 int length
= history_length
;
139 if (!PyArg_ParseTuple(args
, "i:set_history_length", &length
))
141 history_length
= length
;
148 static char get_history_length_doc
[] = "\
149 get_history_length() -> int\n\
150 return the current history length value.\n\
154 get_history_length(PyObject
*self
, PyObject
*args
)
156 if (!PyArg_ParseTuple(args
, ":get_history_length"))
158 return Py_BuildValue("i", history_length
);
163 /* Exported function to specify a word completer in Python */
165 static PyObject
*completer
= NULL
;
166 static PyThreadState
*tstate
= NULL
;
168 static PyObject
*begidx
= NULL
;
169 static PyObject
*endidx
= NULL
;
171 /* get the beginning index for the scope of the tab-completion */
173 get_begidx(PyObject
*self
, PyObject
*args
)
175 if(!PyArg_NoArgs(args
)) {
182 static char doc_get_begidx
[] = "\
183 get_begidx() -> int\n\
184 get the beginning index of the readline tab-completion scope";
186 /* get the ending index for the scope of the tab-completion */
188 get_endidx(PyObject
*self
, PyObject
*args
)
190 if(!PyArg_NoArgs(args
)) {
197 static char doc_get_endidx
[] = "\
198 get_endidx() -> int\n\
199 get the ending index of the readline tab-completion scope";
202 /* set the tab-completion word-delimiters that readline uses */
205 set_completer_delims(PyObject
*self
, PyObject
*args
)
209 if(!PyArg_ParseTuple(args
, "s:set_completer_delims", &break_chars
)) {
212 free(rl_completer_word_break_characters
);
213 rl_completer_word_break_characters
= strdup(break_chars
);
218 static char doc_set_completer_delims
[] = "\
219 set_completer_delims(string) -> None\n\
220 set the readline word delimiters for tab-completion";
223 /* get the tab-completion word-delimiters that readline uses */
226 get_completer_delims(PyObject
*self
, PyObject
*args
)
228 if(!PyArg_NoArgs(args
)) {
231 return PyString_FromString(rl_completer_word_break_characters
);
234 static char doc_get_completer_delims
[] = "\
235 get_completer_delims() -> string\n\
236 get the readline word delimiters for tab-completion";
239 set_completer(PyObject
*self
, PyObject
*args
)
241 PyObject
*function
= Py_None
;
242 if (!PyArg_ParseTuple(args
, "|O:set_completer", &function
))
244 if (function
== Py_None
) {
245 Py_XDECREF(completer
);
249 else if (PyCallable_Check(function
)) {
250 PyObject
*tmp
= completer
;
252 completer
= function
;
254 tstate
= PyThreadState_Get();
257 PyErr_SetString(PyExc_TypeError
,
258 "set_completer(func): argument not callable");
265 static char doc_set_completer
[] = "\
266 set_completer([function]) -> None\n\
267 Set or remove the completer function.\n\
268 The function is called as function(text, state),\n\
269 for i in [0, 1, 2, ...] until it returns a non-string.\n\
270 It should return the next possible completion starting with 'text'.\
273 /* Exported function to read the current line buffer */
276 get_line_buffer(PyObject
*self
, PyObject
*args
)
278 if (!PyArg_NoArgs(args
))
280 return PyString_FromString(rl_line_buffer
);
283 static char doc_get_line_buffer
[] = "\
284 get_line_buffer() -> string\n\
285 return the current contents of the line buffer.\
288 /* Exported function to insert text into the line buffer */
291 insert_text(PyObject
*self
, PyObject
*args
)
294 if (!PyArg_ParseTuple(args
, "s:insert_text", &s
))
302 static char doc_insert_text
[] = "\
303 insert_text(string) -> None\n\
304 Insert text into the command line.\
308 /* Table of functions exported by the module */
310 static struct PyMethodDef readline_methods
[] =
312 {"parse_and_bind", parse_and_bind
, METH_VARARGS
, doc_parse_and_bind
},
313 {"get_line_buffer", get_line_buffer
,
314 METH_OLDARGS
, doc_get_line_buffer
},
315 {"insert_text", insert_text
, METH_VARARGS
, doc_insert_text
},
316 {"read_init_file", read_init_file
, METH_VARARGS
, doc_read_init_file
},
317 {"read_history_file", read_history_file
,
318 METH_VARARGS
, doc_read_history_file
},
319 {"write_history_file", write_history_file
,
320 METH_VARARGS
, doc_write_history_file
},
321 {"set_history_length", set_history_length
,
322 METH_VARARGS
, set_history_length_doc
},
323 {"get_history_length", get_history_length
,
324 METH_VARARGS
, get_history_length_doc
},
325 {"set_completer", set_completer
, METH_VARARGS
, doc_set_completer
},
326 {"get_begidx", get_begidx
, METH_OLDARGS
, doc_get_begidx
},
327 {"get_endidx", get_endidx
, METH_OLDARGS
, doc_get_endidx
},
329 {"set_completer_delims", set_completer_delims
,
330 METH_VARARGS
, doc_set_completer_delims
},
331 {"get_completer_delims", get_completer_delims
,
332 METH_OLDARGS
, doc_get_completer_delims
},
336 /* C function to call the Python completer. */
339 on_completion(char *text
, int state
)
342 if (completer
!= NULL
) {
344 PyThreadState
*save_tstate
;
345 /* Note that readline is called with the interpreter
347 save_tstate
= PyThreadState_Swap(NULL
);
348 PyEval_RestoreThread(tstate
);
349 r
= PyObject_CallFunction(completer
, "si", text
, state
);
356 char *s
= PyString_AsString(r
);
368 PyThreadState_Swap(save_tstate
);
374 /* a more flexible constructor that saves the "begidx" and "endidx"
375 * before calling the normal completer */
378 flex_complete(char *text
, int start
, int end
)
382 begidx
= PyInt_FromLong((long) start
);
383 endidx
= PyInt_FromLong((long) end
);
384 return completion_matches(text
, *on_completion
);
387 /* Helper to initialize GNU readline properly. */
392 rl_readline_name
= "python";
393 /* Force rebind of TAB to insert-tab */
394 rl_bind_key('\t', rl_insert
);
395 /* Bind both ESC-TAB and ESC-ESC to the completion function */
396 rl_bind_key_in_map ('\t', rl_complete
, emacs_meta_keymap
);
397 rl_bind_key_in_map ('\033', rl_complete
, emacs_meta_keymap
);
398 /* Set our completion function */
399 rl_attempted_completion_function
= (CPPFunction
*)flex_complete
;
400 /* Set Python word break characters */
401 rl_completer_word_break_characters
=
402 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
403 /* All nonalphanums except '.' */
405 begidx
= PyInt_FromLong(0L);
406 endidx
= PyInt_FromLong(0L);
407 /* Initialize (allows .inputrc to override)
409 * XXX: A bug in the readline-2.2 library causes a memory leak
410 * inside this function. Nothing we can do about it.
416 /* Interrupt handler */
428 /* Wrapper around GNU readline that handles signals differently. */
431 call_readline(char *prompt
)
435 PyOS_sighandler_t old_inthandler
;
437 old_inthandler
= PyOS_setsig(SIGINT
, onintr
);
440 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
443 PyOS_setsig(SIGINT
, old_inthandler
);
446 rl_event_hook
= PyOS_InputHook
;
447 p
= readline(prompt
);
448 PyOS_setsig(SIGINT
, old_inthandler
);
450 /* We must return a buffer allocated with PyMem_Malloc. */
460 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
461 release the original. */
463 p
= PyMem_Malloc(n
+2);
474 /* Initialize the module */
476 static char doc_module
[] =
477 "Importing this module enables command line editing using GNU readline.";
484 m
= Py_InitModule4("readline", readline_methods
, doc_module
,
485 (PyObject
*)NULL
, PYTHON_API_VERSION
);
486 if (isatty(fileno(stdin
))) {
487 PyOS_ReadlineFunctionPointer
= call_readline
;