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 /* If you have string.h, you might need to add yourself to this #if... [cjh] */
23 /* At max warnings, we need protos for everything. [cjh] */
24 #include <readline/readline.h>
25 #include <readline/history.h>
28 #include <readline/readline.h> /* You may need to add an -I option to Setup */
30 extern int rl_parse_and_bind();
31 extern int rl_read_init_file();
32 extern int rl_insert_text();
33 extern int rl_bind_key();
34 extern int rl_bind_key_in_map();
35 extern int rl_initialize();
36 extern int add_history();
39 /* Pointers needed from outside (but not declared in a header file). */
40 extern int (*PyOS_InputHook
)();
41 extern char *(*PyOS_ReadlineFunctionPointer
) Py_PROTO((char *));
44 /* Exported function to send one line to readline's init file parser */
47 parse_and_bind(self
, args
)
52 if (!PyArg_ParseTuple(args
, "s", &s
))
59 static char doc_parse_and_bind
[] = "\
60 parse_and_bind(string) -> None\n\
61 Parse and execute single line of a readline init file.\
65 /* Exported function to parse a readline init file */
68 read_init_file(self
, args
)
73 if (!PyArg_ParseTuple(args
, "|z", &s
))
75 errno
= rl_read_init_file(s
);
77 return PyErr_SetFromErrno(PyExc_IOError
);
82 static char doc_read_init_file
[] = "\
83 read_init_file([filename]) -> None\n\
84 Parse a readline initialization file.\n\
85 The default filename is the last filename used.\
89 /* Exported function to specify a word completer in Python */
91 static PyObject
*completer
= NULL
;
92 static PyThreadState
*tstate
= NULL
;
95 set_completer(self
, args
)
99 PyObject
*function
= Py_None
;
100 if (!PyArg_ParseTuple(args
, "|O", &function
))
102 if (function
== Py_None
) {
103 Py_XDECREF(completer
);
107 else if (PyCallable_Check(function
)) {
108 PyObject
*tmp
= completer
;
110 completer
= function
;
112 tstate
= PyThreadState_Get();
115 PyErr_SetString(PyExc_TypeError
,
116 "set_completer(func): argument not callable");
123 static char doc_set_completer
[] = "\
124 set_completer([function]) -> None\n\
125 Set or remove the completer function.\n\
126 The function is called as function(text, state),\n\
127 for i in [0, 1, 2, ...] until it returns a non-string.\n\
128 It should return the next possible completion starting with 'text'.\
131 /* Exported function to read the current line buffer */
134 get_line_buffer(self
, args
)
138 if (PyArg_NoArgs(args
))
140 return PyString_FromString(rl_line_buffer
);
143 static char doc_get_line_buffer
[] = "\
144 get_line_buffer() -> string\n\
145 return the current contents of the line buffer.\
148 /* Exported function to insert text into the line buffer */
151 insert_text(self
, args
)
156 if (!PyArg_ParseTuple(args
, "s", &s
))
164 static char doc_insert_text
[] = "\
165 insert_text(string) -> None\n\
166 Insert text into the command line.\
170 /* Table of functions exported by the module */
172 static struct PyMethodDef readline_methods
[] =
174 {"parse_and_bind", parse_and_bind
, 1, doc_parse_and_bind
},
175 {"get_line_buffer", get_line_buffer
, 1, doc_get_line_buffer
},
176 {"insert_text", insert_text
, 1, doc_insert_text
},
177 {"read_init_file", read_init_file
, 1, doc_read_init_file
},
178 {"set_completer", set_completer
, 1, doc_set_completer
},
182 /* C function to call the Python completer. */
185 on_completion(text
, state
)
190 if (completer
!= NULL
) {
192 PyThreadState
*save_tstate
;
193 /* Note that readline is called with the interpreter
195 save_tstate
= PyThreadState_Swap(NULL
);
196 PyEval_RestoreThread(tstate
);
197 r
= PyObject_CallFunction(completer
, "si", text
, state
);
204 char *s
= PyString_AsString(r
);
216 PyThreadState_Swap(save_tstate
);
222 /* Helper to initialize GNU readline properly. */
227 rl_readline_name
= "python";
228 /* Force rebind of TAB to insert-tab */
229 rl_bind_key('\t', rl_insert
);
230 /* Bind both ESC-TAB and ESC-ESC to the completion function */
231 rl_bind_key_in_map ('\t', rl_complete
, emacs_meta_keymap
);
232 rl_bind_key_in_map ('\033', rl_complete
, emacs_meta_keymap
);
233 /* Set our completion function */
234 rl_completion_entry_function
= (Function
*) on_completion
;
235 /* Set Python word break characters */
236 rl_completer_word_break_characters
=
237 " \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?";
238 /* All nonalphanums except '.' */
239 /* Initialize (allows .inputrc to override) */
244 /* Interrupt handler */
257 /* Wrapper around GNU readline that handles signals differently. */
260 call_readline(prompt
)
265 RETSIGTYPE (*old_inthandler
)();
266 old_inthandler
= signal(SIGINT
, onintr
);
269 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
272 signal(SIGINT
, old_inthandler
);
275 rl_event_hook
= PyOS_InputHook
;
276 p
= readline(prompt
);
277 signal(SIGINT
, old_inthandler
);
287 if ((p
= realloc(p
, n
+2)) != NULL
) {
295 /* Initialize the module */
297 static char doc_module
[] =
298 "Importing this module enables command line editing using GNU readline.";
305 m
= Py_InitModule4("readline", readline_methods
, doc_module
,
306 (PyObject
*)NULL
, PYTHON_API_VERSION
);
307 if (isatty(fileno(stdin
))) {
308 PyOS_ReadlineFunctionPointer
= call_readline
;