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