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