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 */
15 /* GNU readline definitions */
16 #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
17 #include <readline/readline.h>
18 #include <readline/history.h>
20 #ifdef HAVE_RL_COMPLETION_MATCHES
21 #define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
24 /* Pointers needed from outside (but not declared in a header file). */
25 extern DL_IMPORT(int) (*PyOS_InputHook
)(void);
26 extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer
)(char *);
29 /* Exported function to send one line to readline's init file parser */
32 parse_and_bind(PyObject
*self
, PyObject
*args
)
35 if (!PyArg_ParseTuple(args
, "s:parse_and_bind", &s
))
37 /* Make a copy -- rl_parse_and_bind() modifies its argument */
39 copy
= malloc(1 + strlen(s
));
41 return PyErr_NoMemory();
43 rl_parse_and_bind(copy
);
44 free(copy
); /* Free the copy */
49 static char doc_parse_and_bind
[] = "\
50 parse_and_bind(string) -> None\n\
51 Parse and execute single line of a readline init file.\
55 /* Exported function to parse a readline init file */
58 read_init_file(PyObject
*self
, PyObject
*args
)
61 if (!PyArg_ParseTuple(args
, "|z:read_init_file", &s
))
63 errno
= rl_read_init_file(s
);
65 return PyErr_SetFromErrno(PyExc_IOError
);
70 static char doc_read_init_file
[] = "\
71 read_init_file([filename]) -> None\n\
72 Parse a readline initialization file.\n\
73 The default filename is the last filename used.\
77 /* Exported function to load a readline history file */
80 read_history_file(PyObject
*self
, PyObject
*args
)
83 if (!PyArg_ParseTuple(args
, "|z:read_history_file", &s
))
85 errno
= read_history(s
);
87 return PyErr_SetFromErrno(PyExc_IOError
);
92 static int history_length
= -1; /* do not truncate history by default */
93 static char doc_read_history_file
[] = "\
94 read_history_file([filename]) -> None\n\
95 Load a readline history file.\n\
96 The default filename is ~/.history.\
100 /* Exported function to save a readline history file */
103 write_history_file(PyObject
*self
, PyObject
*args
)
106 if (!PyArg_ParseTuple(args
, "|z:write_history_file", &s
))
108 errno
= write_history(s
);
109 if (!errno
&& history_length
>= 0)
110 history_truncate_file(s
, history_length
);
112 return PyErr_SetFromErrno(PyExc_IOError
);
117 static char doc_write_history_file
[] = "\
118 write_history_file([filename]) -> None\n\
119 Save a readline history file.\n\
120 The default filename is ~/.history.\
124 static char set_history_length_doc
[] = "\
125 set_history_length(length) -> None\n\
126 set the maximal number of items which will be written to\n\
127 the history file. A negative length is used to inhibit\n\
128 history truncation.\n\
132 set_history_length(PyObject
*self
, PyObject
*args
)
134 int length
= history_length
;
135 if (!PyArg_ParseTuple(args
, "i:set_history_length", &length
))
137 history_length
= length
;
144 static char get_history_length_doc
[] = "\
145 get_history_length() -> int\n\
146 return the current history length value.\n\
150 get_history_length(PyObject
*self
, PyObject
*args
)
152 if (!PyArg_ParseTuple(args
, ":get_history_length"))
154 return Py_BuildValue("i", history_length
);
157 /* Generic hook function setter */
160 set_hook(const char * funcname
, PyObject
**hook_var
, PyThreadState
**tstate
, PyObject
*args
)
162 PyObject
*function
= Py_None
;
164 PyOS_snprintf(buf
, sizeof(buf
), "|O:set_%.50s", funcname
);
165 if (!PyArg_ParseTuple(args
, buf
, &function
))
167 if (function
== Py_None
) {
168 Py_XDECREF(*hook_var
);
172 else if (PyCallable_Check(function
)) {
173 PyObject
*tmp
= *hook_var
;
175 *hook_var
= function
;
177 *tstate
= PyThreadState_Get();
180 PyOS_snprintf(buf
, sizeof(buf
),
181 "set_%.50s(func): argument not callable",
183 PyErr_SetString(PyExc_TypeError
, buf
);
190 /* Exported functions to specify hook functions in Python */
192 static PyObject
*startup_hook
= NULL
;
193 static PyThreadState
*startup_hook_tstate
= NULL
;
195 #ifdef HAVE_RL_PRE_INPUT_HOOK
196 static PyObject
*pre_input_hook
= NULL
;
197 static PyThreadState
*pre_input_hook_tstate
= NULL
;
201 set_startup_hook(PyObject
*self
, PyObject
*args
)
203 return set_hook("startup_hook", &startup_hook
, &startup_hook_tstate
, args
);
206 static char doc_set_startup_hook
[] = "\
207 set_startup_hook([function]) -> None\n\
208 Set or remove the startup_hook function.\n\
209 The function is called with no arguments just\n\
210 before readline prints the first prompt.\n\
213 #ifdef HAVE_RL_PRE_INPUT_HOOK
215 set_pre_input_hook(PyObject
*self
, PyObject
*args
)
217 return set_hook("pre_input_hook", &pre_input_hook
, &pre_input_hook_tstate
, args
);
220 static char doc_set_pre_input_hook
[] = "\
221 set_pre_input_hook([function]) -> None\n\
222 Set or remove the pre_input_hook function.\n\
223 The function is called with no arguments after the first prompt\n\
224 has been printed and just before readline starts reading input\n\
229 /* Exported function to specify a word completer in Python */
231 static PyObject
*completer
= NULL
;
232 static PyThreadState
*completer_tstate
= NULL
;
234 static PyObject
*begidx
= NULL
;
235 static PyObject
*endidx
= NULL
;
237 /* get the beginning index for the scope of the tab-completion */
239 get_begidx(PyObject
*self
, PyObject
*args
)
241 if(!PyArg_NoArgs(args
)) {
248 static char doc_get_begidx
[] = "\
249 get_begidx() -> int\n\
250 get the beginning index of the readline tab-completion scope";
252 /* get the ending index for the scope of the tab-completion */
254 get_endidx(PyObject
*self
, PyObject
*args
)
256 if(!PyArg_NoArgs(args
)) {
263 static char doc_get_endidx
[] = "\
264 get_endidx() -> int\n\
265 get the ending index of the readline tab-completion scope";
268 /* set the tab-completion word-delimiters that readline uses */
271 set_completer_delims(PyObject
*self
, PyObject
*args
)
275 if(!PyArg_ParseTuple(args
, "s:set_completer_delims", &break_chars
)) {
278 free(rl_completer_word_break_characters
);
279 rl_completer_word_break_characters
= strdup(break_chars
);
284 static char doc_set_completer_delims
[] = "\
285 set_completer_delims(string) -> None\n\
286 set the readline word delimiters for tab-completion";
289 py_add_history(PyObject
*self
, PyObject
*args
)
293 if(!PyArg_ParseTuple(args
, "s:add_history", &line
)) {
301 static char doc_add_history
[] = "\
302 add_history(string) -> None\n\
303 add a line to the history buffer";
306 /* get the tab-completion word-delimiters that readline uses */
309 get_completer_delims(PyObject
*self
, PyObject
*args
)
311 if(!PyArg_NoArgs(args
)) {
314 return PyString_FromString(rl_completer_word_break_characters
);
317 static char doc_get_completer_delims
[] = "\
318 get_completer_delims() -> string\n\
319 get the readline word delimiters for tab-completion";
322 set_completer(PyObject
*self
, PyObject
*args
)
324 return set_hook("completer", &completer
, &completer_tstate
, args
);
327 static char doc_set_completer
[] = "\
328 set_completer([function]) -> None\n\
329 Set or remove the completer function.\n\
330 The function is called as function(text, state),\n\
331 for state in 0, 1, 2, ..., until it returns a non-string.\n\
332 It should return the next possible completion starting with 'text'.\
335 /* Exported function to read the current line buffer */
338 get_line_buffer(PyObject
*self
, PyObject
*args
)
340 if (!PyArg_NoArgs(args
))
342 return PyString_FromString(rl_line_buffer
);
345 static char doc_get_line_buffer
[] = "\
346 get_line_buffer() -> string\n\
347 return the current contents of the line buffer.\
350 /* Exported function to insert text into the line buffer */
353 insert_text(PyObject
*self
, PyObject
*args
)
356 if (!PyArg_ParseTuple(args
, "s:insert_text", &s
))
364 static char doc_insert_text
[] = "\
365 insert_text(string) -> None\n\
366 Insert text into the command line.\
370 /* Table of functions exported by the module */
372 static struct PyMethodDef readline_methods
[] =
374 {"parse_and_bind", parse_and_bind
, METH_VARARGS
, doc_parse_and_bind
},
375 {"get_line_buffer", get_line_buffer
,
376 METH_OLDARGS
, doc_get_line_buffer
},
377 {"insert_text", insert_text
, METH_VARARGS
, doc_insert_text
},
378 {"read_init_file", read_init_file
, METH_VARARGS
, doc_read_init_file
},
379 {"read_history_file", read_history_file
,
380 METH_VARARGS
, doc_read_history_file
},
381 {"write_history_file", write_history_file
,
382 METH_VARARGS
, doc_write_history_file
},
383 {"set_history_length", set_history_length
,
384 METH_VARARGS
, set_history_length_doc
},
385 {"get_history_length", get_history_length
,
386 METH_VARARGS
, get_history_length_doc
},
387 {"set_completer", set_completer
, METH_VARARGS
, doc_set_completer
},
388 {"get_begidx", get_begidx
, METH_OLDARGS
, doc_get_begidx
},
389 {"get_endidx", get_endidx
, METH_OLDARGS
, doc_get_endidx
},
391 {"set_completer_delims", set_completer_delims
,
392 METH_VARARGS
, doc_set_completer_delims
},
393 {"add_history", py_add_history
, METH_VARARGS
, doc_add_history
},
394 {"get_completer_delims", get_completer_delims
,
395 METH_OLDARGS
, doc_get_completer_delims
},
397 {"set_startup_hook", set_startup_hook
, METH_VARARGS
, doc_set_startup_hook
},
398 #ifdef HAVE_RL_PRE_INPUT_HOOK
399 {"set_pre_input_hook", set_pre_input_hook
, METH_VARARGS
, doc_set_pre_input_hook
},
404 /* C function to call the Python hooks. */
407 on_hook(PyObject
*func
, PyThreadState
*tstate
)
412 PyThreadState
*save_tstate
;
413 /* Note that readline is called with the interpreter
415 save_tstate
= PyThreadState_Swap(NULL
);
416 PyEval_RestoreThread(tstate
);
417 r
= PyObject_CallFunction(func
, NULL
);
423 result
= PyInt_AsLong(r
);
431 PyThreadState_Swap(save_tstate
);
437 on_startup_hook(void)
439 return on_hook(startup_hook
, startup_hook_tstate
);
442 #ifdef HAVE_RL_PRE_INPUT_HOOK
444 on_pre_input_hook(void)
446 return on_hook(pre_input_hook
, pre_input_hook_tstate
);
450 /* C function to call the Python completer. */
453 on_completion(char *text
, int state
)
456 if (completer
!= NULL
) {
458 PyThreadState
*save_tstate
;
459 /* Note that readline is called with the interpreter
461 save_tstate
= PyThreadState_Swap(NULL
);
462 PyEval_RestoreThread(completer_tstate
);
463 r
= PyObject_CallFunction(completer
, "si", text
, state
);
470 char *s
= PyString_AsString(r
);
482 PyThreadState_Swap(save_tstate
);
488 /* a more flexible constructor that saves the "begidx" and "endidx"
489 * before calling the normal completer */
492 flex_complete(char *text
, int start
, int end
)
496 begidx
= PyInt_FromLong((long) start
);
497 endidx
= PyInt_FromLong((long) end
);
498 return completion_matches(text
, *on_completion
);
501 /* Helper to initialize GNU readline properly. */
506 rl_readline_name
= "python";
507 /* Force rebind of TAB to insert-tab */
508 rl_bind_key('\t', rl_insert
);
509 /* Bind both ESC-TAB and ESC-ESC to the completion function */
510 rl_bind_key_in_map ('\t', rl_complete
, emacs_meta_keymap
);
511 rl_bind_key_in_map ('\033', rl_complete
, emacs_meta_keymap
);
512 /* Set our hook functions */
513 rl_startup_hook
= (Function
*)on_startup_hook
;
514 #ifdef HAVE_RL_PRE_INPUT_HOOK
515 rl_pre_input_hook
= (Function
*)on_pre_input_hook
;
517 /* Set our completion function */
518 rl_attempted_completion_function
= (CPPFunction
*)flex_complete
;
519 /* Set Python word break characters */
520 rl_completer_word_break_characters
=
521 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
522 /* All nonalphanums except '.' */
524 begidx
= PyInt_FromLong(0L);
525 endidx
= PyInt_FromLong(0L);
526 /* Initialize (allows .inputrc to override)
528 * XXX: A bug in the readline-2.2 library causes a memory leak
529 * inside this function. Nothing we can do about it.
535 /* Interrupt handler */
547 /* Wrapper around GNU readline that handles signals differently. */
550 call_readline(char *prompt
)
554 PyOS_sighandler_t old_inthandler
;
556 old_inthandler
= PyOS_setsig(SIGINT
, onintr
);
559 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
562 PyOS_setsig(SIGINT
, old_inthandler
);
565 rl_event_hook
= PyOS_InputHook
;
566 p
= readline(prompt
);
567 PyOS_setsig(SIGINT
, old_inthandler
);
569 /* We must return a buffer allocated with PyMem_Malloc. */
579 /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
580 release the original. */
582 p
= PyMem_Malloc(n
+2);
593 /* Initialize the module */
595 static char doc_module
[] =
596 "Importing this module enables command line editing using GNU readline.";
603 m
= Py_InitModule4("readline", readline_methods
, doc_module
,
604 (PyObject
*)NULL
, PYTHON_API_VERSION
);
605 if (isatty(fileno(stdin
))) {
606 PyOS_ReadlineFunctionPointer
= call_readline
;