Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 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. |
| 4 | * |
| 5 | * More recently, it was largely rewritten by Guido van Rossum who is |
| 6 | * now maintaining it. |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 9 | /* Standard definitions */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 10 | #include "Python.h" |
| 11 | #include <setjmp.h> |
| 12 | #include <signal.h> |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 13 | #include <errno.h> |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 73bacfc | 1998-01-19 22:05:22 +0000 | [diff] [blame] | 15 | #ifdef HAVE_UNISTD_H |
| 16 | #include <unistd.h> /* For isatty() */ |
| 17 | #endif |
| 18 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 19 | /* GNU readline definitions */ |
Guido van Rossum | b0e51b2 | 2001-04-13 18:14:27 +0000 | [diff] [blame] | 20 | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 21 | #include <readline/readline.h> |
| 22 | #include <readline/history.h> |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 24 | #ifdef HAVE_RL_COMPLETION_MATCHES |
| 25 | #define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y))) |
| 26 | #endif |
| 27 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 28 | /* Pointers needed from outside (but not declared in a header file). */ |
Guido van Rossum | 5a53019 | 2001-01-10 21:03:32 +0000 | [diff] [blame] | 29 | extern DL_IMPORT(int) (*PyOS_InputHook)(void); |
| 30 | extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(char *); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 33 | /* Exported function to send one line to readline's init file parser */ |
| 34 | |
| 35 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 36 | parse_and_bind(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 37 | { |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 38 | char *s, *copy; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 39 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 40 | return NULL; |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 41 | /* Make a copy -- rl_parse_and_bind() modifies its argument */ |
| 42 | /* Bernard Herzog */ |
| 43 | copy = malloc(1 + strlen(s)); |
| 44 | if (copy == NULL) |
| 45 | return PyErr_NoMemory(); |
| 46 | strcpy(copy, s); |
| 47 | rl_parse_and_bind(copy); |
| 48 | free(copy); /* Free the copy */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 49 | Py_INCREF(Py_None); |
| 50 | return Py_None; |
| 51 | } |
| 52 | |
| 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.\ |
| 56 | "; |
| 57 | |
| 58 | |
| 59 | /* Exported function to parse a readline init file */ |
| 60 | |
| 61 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 62 | read_init_file(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 63 | { |
| 64 | char *s = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 65 | if (!PyArg_ParseTuple(args, "|z:read_init_file", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 66 | return NULL; |
| 67 | errno = rl_read_init_file(s); |
| 68 | if (errno) |
| 69 | return PyErr_SetFromErrno(PyExc_IOError); |
| 70 | Py_INCREF(Py_None); |
| 71 | return Py_None; |
| 72 | } |
| 73 | |
| 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.\ |
| 78 | "; |
| 79 | |
| 80 | |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 81 | /* Exported function to load a readline history file */ |
| 82 | |
| 83 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 84 | read_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 85 | { |
| 86 | char *s = NULL; |
| 87 | if (!PyArg_ParseTuple(args, "|z:read_history_file", &s)) |
| 88 | return NULL; |
| 89 | errno = read_history(s); |
| 90 | if (errno) |
| 91 | return PyErr_SetFromErrno(PyExc_IOError); |
| 92 | Py_INCREF(Py_None); |
| 93 | return Py_None; |
| 94 | } |
| 95 | |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 96 | static int history_length = -1; /* do not truncate history by default */ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 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.\ |
| 101 | "; |
| 102 | |
| 103 | |
| 104 | /* Exported function to save a readline history file */ |
| 105 | |
| 106 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 107 | write_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 108 | { |
| 109 | char *s = NULL; |
| 110 | if (!PyArg_ParseTuple(args, "|z:write_history_file", &s)) |
| 111 | return NULL; |
| 112 | errno = write_history(s); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 113 | if (!errno && history_length >= 0) |
| 114 | history_truncate_file(s, history_length); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 115 | if (errno) |
| 116 | return PyErr_SetFromErrno(PyExc_IOError); |
| 117 | Py_INCREF(Py_None); |
| 118 | return Py_None; |
| 119 | } |
| 120 | |
| 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.\ |
| 125 | "; |
| 126 | |
| 127 | |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 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\ |
| 133 | "; |
| 134 | |
| 135 | static PyObject* |
| 136 | set_history_length(PyObject *self, PyObject *args) |
| 137 | { |
| 138 | int length = history_length; |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 139 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) |
| 140 | return NULL; |
| 141 | history_length = length; |
| 142 | Py_INCREF(Py_None); |
| 143 | return Py_None; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | static char get_history_length_doc[] = "\ |
| 149 | get_history_length() -> int\n\ |
| 150 | return the current history length value.\n\ |
| 151 | "; |
| 152 | |
| 153 | static PyObject* |
| 154 | get_history_length(PyObject *self, PyObject *args) |
| 155 | { |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 156 | if (!PyArg_ParseTuple(args, ":get_history_length")) |
| 157 | return NULL; |
| 158 | return Py_BuildValue("i", history_length); |
| 159 | } |
| 160 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 161 | /* Generic hook function setter */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 162 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 163 | static PyObject * |
| 164 | set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args) |
| 165 | { |
| 166 | PyObject *function = Py_None; |
| 167 | char buf[80]; |
| 168 | sprintf(buf, "|O:set_%s", funcname); |
| 169 | if (!PyArg_ParseTuple(args, buf, &function)) |
| 170 | return NULL; |
| 171 | if (function == Py_None) { |
| 172 | Py_XDECREF(*hook_var); |
| 173 | *hook_var = NULL; |
| 174 | *tstate = NULL; |
| 175 | } |
| 176 | else if (PyCallable_Check(function)) { |
| 177 | PyObject *tmp = *hook_var; |
| 178 | Py_INCREF(function); |
| 179 | *hook_var = function; |
| 180 | Py_XDECREF(tmp); |
| 181 | *tstate = PyThreadState_Get(); |
| 182 | } |
| 183 | else { |
| 184 | sprintf(buf, "set_%s(func): argument not callable", funcname); |
| 185 | PyErr_SetString(PyExc_TypeError, buf); |
| 186 | return NULL; |
| 187 | } |
| 188 | Py_INCREF(Py_None); |
| 189 | return Py_None; |
| 190 | } |
| 191 | |
| 192 | /* Exported functions to specify hook functions in Python */ |
| 193 | |
| 194 | static PyObject *startup_hook = NULL; |
| 195 | static PyThreadState *startup_hook_tstate = NULL; |
| 196 | |
| 197 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 198 | static PyObject *pre_input_hook = NULL; |
| 199 | static PyThreadState *pre_input_hook_tstate = NULL; |
| 200 | #endif |
| 201 | |
| 202 | static PyObject * |
| 203 | set_startup_hook(PyObject *self, PyObject *args) |
| 204 | { |
| 205 | return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args); |
| 206 | } |
| 207 | |
| 208 | static char doc_set_startup_hook[] = "\ |
| 209 | set_startup_hook([function]) -> None\n\ |
| 210 | Set or remove the startup_hook function.\n\ |
| 211 | The function is called with no arguments just\n\ |
| 212 | before readline prints the first prompt.\n\ |
| 213 | "; |
| 214 | |
| 215 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 216 | static PyObject * |
| 217 | set_pre_input_hook(PyObject *self, PyObject *args) |
| 218 | { |
| 219 | return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args); |
| 220 | } |
| 221 | |
| 222 | static char doc_set_pre_input_hook[] = "\ |
| 223 | set_pre_input_hook([function]) -> None\n\ |
| 224 | Set or remove the pre_input_hook function.\n\ |
| 225 | The function is called with no arguments after the first prompt\n\ |
| 226 | has been printed and just before readline starts reading input\n\ |
| 227 | characters.\n\ |
| 228 | "; |
| 229 | #endif |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 231 | /* Exported function to specify a word completer in Python */ |
| 232 | |
| 233 | static PyObject *completer = NULL; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 234 | static PyThreadState *completer_tstate = NULL; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 236 | static PyObject *begidx = NULL; |
| 237 | static PyObject *endidx = NULL; |
| 238 | |
| 239 | /* get the beginning index for the scope of the tab-completion */ |
| 240 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 241 | get_begidx(PyObject *self, PyObject *args) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 242 | { |
| 243 | if(!PyArg_NoArgs(args)) { |
| 244 | return NULL; |
| 245 | } |
| 246 | Py_INCREF(begidx); |
| 247 | return begidx; |
| 248 | } |
| 249 | |
| 250 | static char doc_get_begidx[] = "\ |
| 251 | get_begidx() -> int\n\ |
| 252 | get the beginning index of the readline tab-completion scope"; |
| 253 | |
| 254 | /* get the ending index for the scope of the tab-completion */ |
| 255 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 256 | get_endidx(PyObject *self, PyObject *args) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 257 | { |
| 258 | if(!PyArg_NoArgs(args)) { |
| 259 | return NULL; |
| 260 | } |
| 261 | Py_INCREF(endidx); |
| 262 | return endidx; |
| 263 | } |
| 264 | |
| 265 | static char doc_get_endidx[] = "\ |
| 266 | get_endidx() -> int\n\ |
| 267 | get the ending index of the readline tab-completion scope"; |
| 268 | |
| 269 | |
| 270 | /* set the tab-completion word-delimiters that readline uses */ |
| 271 | |
| 272 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 273 | set_completer_delims(PyObject *self, PyObject *args) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 274 | { |
| 275 | char *break_chars; |
| 276 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 277 | if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 278 | return NULL; |
| 279 | } |
| 280 | free(rl_completer_word_break_characters); |
| 281 | rl_completer_word_break_characters = strdup(break_chars); |
| 282 | Py_INCREF(Py_None); |
| 283 | return Py_None; |
| 284 | } |
| 285 | |
| 286 | static char doc_set_completer_delims[] = "\ |
| 287 | set_completer_delims(string) -> None\n\ |
| 288 | set the readline word delimiters for tab-completion"; |
| 289 | |
| 290 | |
| 291 | /* get the tab-completion word-delimiters that readline uses */ |
| 292 | |
| 293 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 294 | get_completer_delims(PyObject *self, PyObject *args) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 295 | { |
| 296 | if(!PyArg_NoArgs(args)) { |
| 297 | return NULL; |
| 298 | } |
| 299 | return PyString_FromString(rl_completer_word_break_characters); |
| 300 | } |
| 301 | |
| 302 | static char doc_get_completer_delims[] = "\ |
| 303 | get_completer_delims() -> string\n\ |
| 304 | get the readline word delimiters for tab-completion"; |
| 305 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 306 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 307 | set_completer(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 308 | { |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 309 | return set_hook("completer", &completer, &completer_tstate, args); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static char doc_set_completer[] = "\ |
| 313 | set_completer([function]) -> None\n\ |
| 314 | Set or remove the completer function.\n\ |
| 315 | The function is called as function(text, state),\n\ |
Fred Drake | 52d55a3 | 2001-08-01 21:44:14 +0000 | [diff] [blame] | 316 | for state in 0, 1, 2, ..., until it returns a non-string.\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 317 | It should return the next possible completion starting with 'text'.\ |
| 318 | "; |
| 319 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 320 | /* Exported function to read the current line buffer */ |
| 321 | |
| 322 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 323 | get_line_buffer(PyObject *self, PyObject *args) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 324 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 325 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 326 | return NULL; |
| 327 | return PyString_FromString(rl_line_buffer); |
| 328 | } |
| 329 | |
| 330 | static char doc_get_line_buffer[] = "\ |
Guido van Rossum | a88c5f3 | 1998-05-20 15:50:56 +0000 | [diff] [blame] | 331 | get_line_buffer() -> string\n\ |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 332 | return the current contents of the line buffer.\ |
| 333 | "; |
| 334 | |
| 335 | /* Exported function to insert text into the line buffer */ |
| 336 | |
| 337 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 338 | insert_text(PyObject *self, PyObject *args) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 339 | { |
| 340 | char *s; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 341 | if (!PyArg_ParseTuple(args, "s:insert_text", &s)) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 342 | return NULL; |
| 343 | rl_insert_text(s); |
| 344 | Py_INCREF(Py_None); |
| 345 | return Py_None; |
| 346 | } |
| 347 | |
| 348 | |
| 349 | static char doc_insert_text[] = "\ |
| 350 | insert_text(string) -> None\n\ |
| 351 | Insert text into the command line.\ |
| 352 | "; |
| 353 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 354 | |
| 355 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 356 | |
| 357 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 358 | { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 359 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 360 | {"get_line_buffer", get_line_buffer, |
| 361 | METH_OLDARGS, doc_get_line_buffer}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 362 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text}, |
| 363 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, |
| 364 | {"read_history_file", read_history_file, |
| 365 | METH_VARARGS, doc_read_history_file}, |
| 366 | {"write_history_file", write_history_file, |
| 367 | METH_VARARGS, doc_write_history_file}, |
| 368 | {"set_history_length", set_history_length, |
| 369 | METH_VARARGS, set_history_length_doc}, |
| 370 | {"get_history_length", get_history_length, |
| 371 | METH_VARARGS, get_history_length_doc}, |
| 372 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 373 | {"get_begidx", get_begidx, METH_OLDARGS, doc_get_begidx}, |
| 374 | {"get_endidx", get_endidx, METH_OLDARGS, doc_get_endidx}, |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 375 | |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 376 | {"set_completer_delims", set_completer_delims, |
| 377 | METH_VARARGS, doc_set_completer_delims}, |
| 378 | {"get_completer_delims", get_completer_delims, |
| 379 | METH_OLDARGS, doc_get_completer_delims}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 380 | |
| 381 | {"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook}, |
| 382 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 383 | {"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook}, |
| 384 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 385 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 386 | }; |
| 387 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 388 | /* C function to call the Python hooks. */ |
| 389 | |
| 390 | static int |
| 391 | on_hook(PyObject *func, PyThreadState *tstate) |
| 392 | { |
| 393 | int result = 0; |
| 394 | if (func != NULL) { |
| 395 | PyObject *r; |
| 396 | PyThreadState *save_tstate; |
| 397 | /* Note that readline is called with the interpreter |
| 398 | lock released! */ |
| 399 | save_tstate = PyThreadState_Swap(NULL); |
| 400 | PyEval_RestoreThread(tstate); |
| 401 | r = PyObject_CallFunction(func, NULL); |
| 402 | if (r == NULL) |
| 403 | goto error; |
| 404 | if (r == Py_None) |
| 405 | result = 0; |
| 406 | else |
| 407 | result = PyInt_AsLong(r); |
| 408 | Py_DECREF(r); |
| 409 | goto done; |
| 410 | error: |
| 411 | PyErr_Clear(); |
| 412 | Py_XDECREF(r); |
| 413 | done: |
| 414 | PyEval_SaveThread(); |
| 415 | PyThreadState_Swap(save_tstate); |
| 416 | } |
| 417 | return result; |
| 418 | } |
| 419 | |
| 420 | static int |
| 421 | on_startup_hook(void) |
| 422 | { |
| 423 | return on_hook(startup_hook, startup_hook_tstate); |
| 424 | } |
| 425 | |
| 426 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 427 | static int |
| 428 | on_pre_input_hook(void) |
| 429 | { |
| 430 | return on_hook(pre_input_hook, pre_input_hook_tstate); |
| 431 | } |
| 432 | #endif |
| 433 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 434 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 436 | static char * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 437 | on_completion(char *text, int state) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 438 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 439 | char *result = NULL; |
| 440 | if (completer != NULL) { |
| 441 | PyObject *r; |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 442 | PyThreadState *save_tstate; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 443 | /* Note that readline is called with the interpreter |
| 444 | lock released! */ |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 445 | save_tstate = PyThreadState_Swap(NULL); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 446 | PyEval_RestoreThread(completer_tstate); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 447 | r = PyObject_CallFunction(completer, "si", text, state); |
| 448 | if (r == NULL) |
| 449 | goto error; |
| 450 | if (r == Py_None) { |
| 451 | result = NULL; |
| 452 | } |
| 453 | else { |
| 454 | char *s = PyString_AsString(r); |
| 455 | if (s == NULL) |
| 456 | goto error; |
| 457 | result = strdup(s); |
| 458 | } |
| 459 | Py_DECREF(r); |
| 460 | goto done; |
| 461 | error: |
| 462 | PyErr_Clear(); |
| 463 | Py_XDECREF(r); |
| 464 | done: |
| 465 | PyEval_SaveThread(); |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 466 | PyThreadState_Swap(save_tstate); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 467 | } |
| 468 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 472 | /* a more flexible constructor that saves the "begidx" and "endidx" |
| 473 | * before calling the normal completer */ |
| 474 | |
| 475 | char ** |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 476 | flex_complete(char *text, int start, int end) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 477 | { |
| 478 | Py_XDECREF(begidx); |
| 479 | Py_XDECREF(endidx); |
| 480 | begidx = PyInt_FromLong((long) start); |
| 481 | endidx = PyInt_FromLong((long) end); |
| 482 | return completion_matches(text, *on_completion); |
| 483 | } |
| 484 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 485 | /* Helper to initialize GNU readline properly. */ |
| 486 | |
| 487 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 488 | setup_readline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 489 | { |
| 490 | rl_readline_name = "python"; |
| 491 | /* Force rebind of TAB to insert-tab */ |
| 492 | rl_bind_key('\t', rl_insert); |
| 493 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 494 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 495 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 496 | /* Set our hook functions */ |
| 497 | rl_startup_hook = (Function *)on_startup_hook; |
| 498 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 499 | rl_pre_input_hook = (Function *)on_pre_input_hook; |
| 500 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 501 | /* Set our completion function */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 502 | rl_attempted_completion_function = (CPPFunction *)flex_complete; |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 503 | /* Set Python word break characters */ |
| 504 | rl_completer_word_break_characters = |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 505 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 506 | /* All nonalphanums except '.' */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 507 | |
| 508 | begidx = PyInt_FromLong(0L); |
| 509 | endidx = PyInt_FromLong(0L); |
Barry Warsaw | f761287 | 1999-01-29 21:55:03 +0000 | [diff] [blame] | 510 | /* Initialize (allows .inputrc to override) |
| 511 | * |
| 512 | * XXX: A bug in the readline-2.2 library causes a memory leak |
| 513 | * inside this function. Nothing we can do about it. |
| 514 | */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 515 | rl_initialize(); |
| 516 | } |
| 517 | |
| 518 | |
| 519 | /* Interrupt handler */ |
| 520 | |
| 521 | static jmp_buf jbuf; |
| 522 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 523 | /* ARGSUSED */ |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 524 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 525 | onintr(int sig) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 526 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 527 | longjmp(jbuf, 1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 530 | |
| 531 | /* Wrapper around GNU readline that handles signals differently. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 532 | |
| 533 | static char * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 534 | call_readline(char *prompt) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 535 | { |
Guido van Rossum | 26418a9 | 2000-06-28 21:30:31 +0000 | [diff] [blame] | 536 | size_t n; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 537 | char *p, *q; |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 538 | PyOS_sighandler_t old_inthandler; |
| 539 | |
| 540 | old_inthandler = PyOS_setsig(SIGINT, onintr); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 541 | if (setjmp(jbuf)) { |
| 542 | #ifdef HAVE_SIGRELSE |
| 543 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 544 | sigrelse(SIGINT); |
| 545 | #endif |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 546 | PyOS_setsig(SIGINT, old_inthandler); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 547 | return NULL; |
| 548 | } |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 549 | rl_event_hook = PyOS_InputHook; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 550 | p = readline(prompt); |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 551 | PyOS_setsig(SIGINT, old_inthandler); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 552 | |
| 553 | /* We must return a buffer allocated with PyMem_Malloc. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 554 | if (p == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 555 | p = PyMem_Malloc(1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 556 | if (p != NULL) |
| 557 | *p = '\0'; |
| 558 | return p; |
| 559 | } |
| 560 | n = strlen(p); |
| 561 | if (n > 0) |
| 562 | add_history(p); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 563 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and |
| 564 | release the original. */ |
| 565 | q = p; |
| 566 | p = PyMem_Malloc(n+2); |
| 567 | if (p != NULL) { |
| 568 | strncpy(p, q, n); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 569 | p[n] = '\n'; |
| 570 | p[n+1] = '\0'; |
| 571 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 572 | free(q); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 573 | return p; |
| 574 | } |
| 575 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 576 | |
| 577 | /* Initialize the module */ |
| 578 | |
| 579 | static char doc_module[] = |
| 580 | "Importing this module enables command line editing using GNU readline."; |
| 581 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 582 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 583 | initreadline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 584 | { |
Guido van Rossum | 1ea64ea | 2000-10-02 15:53:08 +0000 | [diff] [blame] | 585 | PyObject *m; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 586 | |
| 587 | m = Py_InitModule4("readline", readline_methods, doc_module, |
| 588 | (PyObject *)NULL, PYTHON_API_VERSION); |
| 589 | if (isatty(fileno(stdin))) { |
| 590 | PyOS_ReadlineFunctionPointer = call_readline; |
| 591 | setup_readline(); |
| 592 | } |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 593 | } |