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> |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 14 | #include <sys/time.h> |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 15 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 16 | #if defined(HAVE_SETLOCALE) |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 17 | /* GNU readline() mistakenly sets the LC_CTYPE locale. |
| 18 | * This is evil. Only the user or the app's main() should do this! |
| 19 | * We must save and restore the locale around the rl_initialize() call. |
| 20 | */ |
| 21 | #define SAVE_LOCALE |
| 22 | #include <locale.h> |
| 23 | #endif |
| 24 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 25 | /* GNU readline definitions */ |
Guido van Rossum | b0e51b2 | 2001-04-13 18:14:27 +0000 | [diff] [blame] | 26 | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 27 | #include <readline/readline.h> |
| 28 | #include <readline/history.h> |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 30 | #ifdef HAVE_RL_COMPLETION_MATCHES |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 31 | #define completion_matches(x, y) \ |
| 32 | rl_completion_matches((x), ((rl_compentry_func_t *)(y))) |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 36 | /* Exported function to send one line to readline's init file parser */ |
| 37 | |
| 38 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 39 | parse_and_bind(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 40 | { |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 41 | char *s, *copy; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 42 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 43 | return NULL; |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 44 | /* Make a copy -- rl_parse_and_bind() modifies its argument */ |
| 45 | /* Bernard Herzog */ |
| 46 | copy = malloc(1 + strlen(s)); |
| 47 | if (copy == NULL) |
| 48 | return PyErr_NoMemory(); |
| 49 | strcpy(copy, s); |
| 50 | rl_parse_and_bind(copy); |
| 51 | free(copy); /* Free the copy */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 52 | Py_INCREF(Py_None); |
| 53 | return Py_None; |
| 54 | } |
| 55 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 56 | PyDoc_STRVAR(doc_parse_and_bind, |
| 57 | "parse_and_bind(string) -> None\n\ |
| 58 | Parse and execute single line of a readline init file."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | /* Exported function to parse a readline init file */ |
| 62 | |
| 63 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 64 | read_init_file(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 65 | { |
| 66 | char *s = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 67 | if (!PyArg_ParseTuple(args, "|z:read_init_file", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 68 | return NULL; |
| 69 | errno = rl_read_init_file(s); |
| 70 | if (errno) |
| 71 | return PyErr_SetFromErrno(PyExc_IOError); |
| 72 | Py_INCREF(Py_None); |
| 73 | return Py_None; |
| 74 | } |
| 75 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 76 | PyDoc_STRVAR(doc_read_init_file, |
| 77 | "read_init_file([filename]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 78 | Parse a readline initialization file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 79 | The default filename is the last filename used."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 80 | |
| 81 | |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 82 | /* Exported function to load a readline history file */ |
| 83 | |
| 84 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 85 | read_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 86 | { |
| 87 | char *s = NULL; |
| 88 | if (!PyArg_ParseTuple(args, "|z:read_history_file", &s)) |
| 89 | return NULL; |
| 90 | errno = read_history(s); |
| 91 | if (errno) |
| 92 | return PyErr_SetFromErrno(PyExc_IOError); |
| 93 | Py_INCREF(Py_None); |
| 94 | return Py_None; |
| 95 | } |
| 96 | |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 97 | static int history_length = -1; /* do not truncate history by default */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 98 | PyDoc_STRVAR(doc_read_history_file, |
| 99 | "read_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 100 | Load a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 101 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 121 | PyDoc_STRVAR(doc_write_history_file, |
| 122 | "write_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 123 | Save a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 124 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 125 | |
| 126 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 127 | /* Set history length */ |
| 128 | |
| 129 | static PyObject* |
| 130 | set_history_length(PyObject *self, PyObject *args) |
| 131 | { |
| 132 | int length = history_length; |
| 133 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) |
| 134 | return NULL; |
| 135 | history_length = length; |
| 136 | Py_INCREF(Py_None); |
| 137 | return Py_None; |
| 138 | } |
| 139 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 140 | PyDoc_STRVAR(set_history_length_doc, |
| 141 | "set_history_length(length) -> None\n\ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 142 | set the maximal number of items which will be written to\n\ |
| 143 | the history file. A negative length is used to inhibit\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 144 | history truncation."); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 145 | |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 147 | /* Get history length */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 148 | |
| 149 | static PyObject* |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 150 | get_history_length(PyObject *self, PyObject *noarg) |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 151 | { |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 152 | return PyInt_FromLong(history_length); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 155 | PyDoc_STRVAR(get_history_length_doc, |
| 156 | "get_history_length() -> int\n\ |
| 157 | return the maximum number of items that will be written to\n\ |
| 158 | the history file."); |
| 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 * |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 164 | set_hook(const char *funcname, PyObject **hook_var, |
| 165 | PyThreadState **tstate, PyObject *args) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 166 | { |
| 167 | PyObject *function = Py_None; |
| 168 | char buf[80]; |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 169 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 170 | if (!PyArg_ParseTuple(args, buf, &function)) |
| 171 | return NULL; |
| 172 | if (function == Py_None) { |
| 173 | Py_XDECREF(*hook_var); |
| 174 | *hook_var = NULL; |
| 175 | *tstate = NULL; |
| 176 | } |
| 177 | else if (PyCallable_Check(function)) { |
| 178 | PyObject *tmp = *hook_var; |
| 179 | Py_INCREF(function); |
| 180 | *hook_var = function; |
| 181 | Py_XDECREF(tmp); |
Nicholas Bastin | 2786d90 | 2004-03-25 02:16:23 +0000 | [diff] [blame] | 182 | *tstate = PyThreadState_GET(); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 183 | } |
| 184 | else { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 185 | PyOS_snprintf(buf, sizeof(buf), |
| 186 | "set_%.50s(func): argument not callable", |
| 187 | funcname); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 188 | PyErr_SetString(PyExc_TypeError, buf); |
| 189 | return NULL; |
| 190 | } |
| 191 | Py_INCREF(Py_None); |
| 192 | return Py_None; |
| 193 | } |
| 194 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 195 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 196 | /* Exported functions to specify hook functions in Python */ |
| 197 | |
| 198 | static PyObject *startup_hook = NULL; |
| 199 | static PyThreadState *startup_hook_tstate = NULL; |
| 200 | |
| 201 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 202 | static PyObject *pre_input_hook = NULL; |
| 203 | static PyThreadState *pre_input_hook_tstate = NULL; |
| 204 | #endif |
| 205 | |
| 206 | static PyObject * |
| 207 | set_startup_hook(PyObject *self, PyObject *args) |
| 208 | { |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 209 | return set_hook("startup_hook", &startup_hook, |
| 210 | &startup_hook_tstate, args); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 213 | PyDoc_STRVAR(doc_set_startup_hook, |
| 214 | "set_startup_hook([function]) -> None\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 215 | Set or remove the startup_hook function.\n\ |
| 216 | The function is called with no arguments just\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 217 | before readline prints the first prompt."); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 218 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 219 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 220 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 221 | |
| 222 | /* Set pre-input hook */ |
| 223 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 224 | static PyObject * |
| 225 | set_pre_input_hook(PyObject *self, PyObject *args) |
| 226 | { |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 227 | return set_hook("pre_input_hook", &pre_input_hook, |
| 228 | &pre_input_hook_tstate, args); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 231 | PyDoc_STRVAR(doc_set_pre_input_hook, |
| 232 | "set_pre_input_hook([function]) -> None\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 233 | Set or remove the pre_input_hook function.\n\ |
| 234 | The function is called with no arguments after the first prompt\n\ |
| 235 | has been printed and just before readline starts reading input\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 236 | characters."); |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 237 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 238 | #endif |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 241 | /* Exported function to specify a word completer in Python */ |
| 242 | |
| 243 | static PyObject *completer = NULL; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 244 | static PyThreadState *completer_tstate = NULL; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 246 | static PyObject *begidx = NULL; |
| 247 | static PyObject *endidx = NULL; |
| 248 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 249 | |
| 250 | /* Get the beginning index for the scope of the tab-completion */ |
| 251 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 252 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 253 | get_begidx(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 254 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 255 | Py_INCREF(begidx); |
| 256 | return begidx; |
| 257 | } |
| 258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 259 | PyDoc_STRVAR(doc_get_begidx, |
| 260 | "get_begidx() -> int\n\ |
| 261 | get the beginning index of the readline tab-completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 263 | |
| 264 | /* Get the ending index for the scope of the tab-completion */ |
| 265 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 266 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 267 | get_endidx(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 268 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 269 | Py_INCREF(endidx); |
| 270 | return endidx; |
| 271 | } |
| 272 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 273 | PyDoc_STRVAR(doc_get_endidx, |
| 274 | "get_endidx() -> int\n\ |
| 275 | get the ending index of the readline tab-completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 276 | |
| 277 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 278 | /* Set the tab-completion word-delimiters that readline uses */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 279 | |
| 280 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 281 | set_completer_delims(PyObject *self, PyObject *args) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 282 | { |
| 283 | char *break_chars; |
| 284 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 285 | if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 286 | return NULL; |
| 287 | } |
Neal Norwitz | 0e0ee59 | 2002-04-21 15:03:18 +0000 | [diff] [blame] | 288 | free((void*)rl_completer_word_break_characters); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 289 | rl_completer_word_break_characters = strdup(break_chars); |
| 290 | Py_INCREF(Py_None); |
| 291 | return Py_None; |
| 292 | } |
| 293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 294 | PyDoc_STRVAR(doc_set_completer_delims, |
| 295 | "set_completer_delims(string) -> None\n\ |
| 296 | set the readline word delimiters for tab-completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 297 | |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 298 | static PyObject * |
| 299 | py_remove_history(PyObject *self, PyObject *args) |
| 300 | { |
| 301 | int entry_number; |
| 302 | HIST_ENTRY *entry; |
| 303 | |
| 304 | if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number)) |
| 305 | return NULL; |
| 306 | entry = remove_history(entry_number); |
| 307 | if (!entry) { |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 308 | PyErr_Format(PyExc_ValueError, |
| 309 | "No history item at position %d", |
| 310 | entry_number); |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 311 | return NULL; |
| 312 | } |
| 313 | /* free memory allocated for the history entry */ |
| 314 | if (entry->line) |
| 315 | free(entry->line); |
| 316 | if (entry->data) |
| 317 | free(entry->data); |
| 318 | free(entry); |
| 319 | |
| 320 | Py_INCREF(Py_None); |
| 321 | return Py_None; |
| 322 | } |
| 323 | |
| 324 | PyDoc_STRVAR(doc_remove_history, |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 325 | "remove_history_item(pos) -> None\n\ |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 326 | remove history item given by its position"); |
| 327 | |
| 328 | static PyObject * |
| 329 | py_replace_history(PyObject *self, PyObject *args) |
| 330 | { |
| 331 | int entry_number; |
| 332 | char *line; |
| 333 | HIST_ENTRY *old_entry; |
| 334 | |
| 335 | if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) { |
| 336 | return NULL; |
| 337 | } |
| 338 | old_entry = replace_history_entry(entry_number, line, (void *)NULL); |
| 339 | if (!old_entry) { |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 340 | PyErr_Format(PyExc_ValueError, |
| 341 | "No history item at position %d", |
| 342 | entry_number); |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
| 345 | /* free memory allocated for the old history entry */ |
| 346 | if (old_entry->line) |
| 347 | free(old_entry->line); |
| 348 | if (old_entry->data) |
| 349 | free(old_entry->data); |
| 350 | free(old_entry); |
| 351 | |
| 352 | Py_INCREF(Py_None); |
| 353 | return Py_None; |
| 354 | } |
| 355 | |
| 356 | PyDoc_STRVAR(doc_replace_history, |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 357 | "replace_history_item(pos, line) -> None\n\ |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 358 | replaces history item given by its position with contents of line"); |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 359 | |
| 360 | /* Add a line to the history buffer */ |
| 361 | |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 362 | static PyObject * |
| 363 | py_add_history(PyObject *self, PyObject *args) |
| 364 | { |
| 365 | char *line; |
| 366 | |
| 367 | if(!PyArg_ParseTuple(args, "s:add_history", &line)) { |
| 368 | return NULL; |
| 369 | } |
| 370 | add_history(line); |
| 371 | Py_INCREF(Py_None); |
| 372 | return Py_None; |
| 373 | } |
| 374 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 375 | PyDoc_STRVAR(doc_add_history, |
| 376 | "add_history(string) -> None\n\ |
| 377 | add a line to the history buffer"); |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 378 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 380 | /* Get the tab-completion word-delimiters that readline uses */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 381 | |
| 382 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 383 | get_completer_delims(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 384 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 385 | return PyString_FromString(rl_completer_word_break_characters); |
| 386 | } |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 387 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 388 | PyDoc_STRVAR(doc_get_completer_delims, |
| 389 | "get_completer_delims() -> string\n\ |
| 390 | get the readline word delimiters for tab-completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 392 | |
| 393 | /* Set the completer function */ |
| 394 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 395 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 396 | set_completer(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 397 | { |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 398 | return set_hook("completer", &completer, &completer_tstate, args); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 401 | PyDoc_STRVAR(doc_set_completer, |
| 402 | "set_completer([function]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 403 | Set or remove the completer function.\n\ |
| 404 | The function is called as function(text, state),\n\ |
Fred Drake | 52d55a3 | 2001-08-01 21:44:14 +0000 | [diff] [blame] | 405 | for state in 0, 1, 2, ..., until it returns a non-string.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 406 | It should return the next possible completion starting with 'text'."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 408 | |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 409 | static PyObject * |
Neal Norwitz | d9efdc5 | 2003-03-01 15:19:41 +0000 | [diff] [blame] | 410 | get_completer(PyObject *self, PyObject *noargs) |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 411 | { |
| 412 | if (completer == NULL) { |
| 413 | Py_INCREF(Py_None); |
| 414 | return Py_None; |
| 415 | } |
| 416 | Py_INCREF(completer); |
| 417 | return completer; |
| 418 | } |
| 419 | |
| 420 | PyDoc_STRVAR(doc_get_completer, |
| 421 | "get_completer() -> function\n\ |
| 422 | \n\ |
| 423 | Returns current completer function."); |
| 424 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 425 | /* Exported function to get any element of history */ |
| 426 | |
| 427 | static PyObject * |
| 428 | get_history_item(PyObject *self, PyObject *args) |
| 429 | { |
| 430 | int idx = 0; |
| 431 | HIST_ENTRY *hist_ent; |
| 432 | |
| 433 | if (!PyArg_ParseTuple(args, "i:index", &idx)) |
| 434 | return NULL; |
| 435 | if ((hist_ent = history_get(idx))) |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 436 | return PyString_FromString(hist_ent->line); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 437 | else { |
| 438 | Py_INCREF(Py_None); |
| 439 | return Py_None; |
| 440 | } |
| 441 | } |
| 442 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 443 | PyDoc_STRVAR(doc_get_history_item, |
| 444 | "get_history_item() -> string\n\ |
| 445 | return the current contents of history item at index."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 446 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 447 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 448 | /* Exported function to get current length of history */ |
| 449 | |
| 450 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 451 | get_current_history_length(PyObject *self, PyObject *noarg) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 452 | { |
| 453 | HISTORY_STATE *hist_st; |
| 454 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 455 | hist_st = history_get_history_state(); |
| 456 | return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0); |
| 457 | } |
| 458 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 459 | PyDoc_STRVAR(doc_get_current_history_length, |
| 460 | "get_current_history_length() -> integer\n\ |
| 461 | return the current (not the maximum) length of history."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 462 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 463 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 464 | /* Exported function to read the current line buffer */ |
| 465 | |
| 466 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 467 | get_line_buffer(PyObject *self, PyObject *noarg) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 468 | { |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 469 | return PyString_FromString(rl_line_buffer); |
| 470 | } |
| 471 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 472 | PyDoc_STRVAR(doc_get_line_buffer, |
| 473 | "get_line_buffer() -> string\n\ |
| 474 | return the current contents of the line buffer."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 475 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 476 | |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 477 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
| 478 | |
| 479 | /* Exported function to clear the current history */ |
| 480 | |
| 481 | static PyObject * |
| 482 | py_clear_history(PyObject *self, PyObject *noarg) |
| 483 | { |
| 484 | clear_history(); |
| 485 | Py_INCREF(Py_None); |
| 486 | return Py_None; |
| 487 | } |
| 488 | |
| 489 | PyDoc_STRVAR(doc_clear_history, |
| 490 | "clear_history() -> None\n\ |
| 491 | Clear the current readline history."); |
| 492 | #endif |
| 493 | |
| 494 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 495 | /* Exported function to insert text into the line buffer */ |
| 496 | |
| 497 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 498 | insert_text(PyObject *self, PyObject *args) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 499 | { |
| 500 | char *s; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 501 | if (!PyArg_ParseTuple(args, "s:insert_text", &s)) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 502 | return NULL; |
| 503 | rl_insert_text(s); |
| 504 | Py_INCREF(Py_None); |
| 505 | return Py_None; |
| 506 | } |
| 507 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 508 | PyDoc_STRVAR(doc_insert_text, |
| 509 | "insert_text(string) -> None\n\ |
| 510 | Insert text into the command line."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 511 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 512 | |
| 513 | /* Redisplay the line buffer */ |
| 514 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 515 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 516 | redisplay(PyObject *self, PyObject *noarg) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 517 | { |
| 518 | rl_redisplay(); |
| 519 | Py_INCREF(Py_None); |
| 520 | return Py_None; |
| 521 | } |
| 522 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 523 | PyDoc_STRVAR(doc_redisplay, |
| 524 | "redisplay() -> None\n\ |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 525 | Change what's displayed on the screen to reflect the current\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 526 | contents of the line buffer."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 527 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 528 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 529 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 530 | |
| 531 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 532 | { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 533 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind}, |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 534 | {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 535 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text}, |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 536 | {"redisplay", redisplay, METH_NOARGS, doc_redisplay}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 537 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 538 | {"read_history_file", read_history_file, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 539 | METH_VARARGS, doc_read_history_file}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 540 | {"write_history_file", write_history_file, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 541 | METH_VARARGS, doc_write_history_file}, |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 542 | {"get_history_item", get_history_item, |
| 543 | METH_VARARGS, doc_get_history_item}, |
Neal Norwitz | 767f835 | 2002-03-31 16:13:39 +0000 | [diff] [blame] | 544 | {"get_current_history_length", (PyCFunction)get_current_history_length, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 545 | METH_NOARGS, doc_get_current_history_length}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 546 | {"set_history_length", set_history_length, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 547 | METH_VARARGS, set_history_length_doc}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 548 | {"get_history_length", get_history_length, |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 549 | METH_NOARGS, get_history_length_doc}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 550 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer}, |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 551 | {"get_completer", get_completer, METH_NOARGS, doc_get_completer}, |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 552 | {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx}, |
| 553 | {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx}, |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 554 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 555 | {"set_completer_delims", set_completer_delims, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 556 | METH_VARARGS, doc_set_completer_delims}, |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 557 | {"add_history", py_add_history, METH_VARARGS, doc_add_history}, |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 558 | {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history}, |
| 559 | {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history}, |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 560 | {"get_completer_delims", get_completer_delims, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 561 | METH_NOARGS, doc_get_completer_delims}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 562 | |
| 563 | {"set_startup_hook", set_startup_hook, |
| 564 | METH_VARARGS, doc_set_startup_hook}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 565 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 566 | {"set_pre_input_hook", set_pre_input_hook, |
| 567 | METH_VARARGS, doc_set_pre_input_hook}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 568 | #endif |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 569 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
| 570 | {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history}, |
| 571 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 572 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 573 | }; |
| 574 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 575 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 576 | /* C function to call the Python hooks. */ |
| 577 | |
| 578 | static int |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 579 | on_hook(PyObject *func, PyThreadState **tstate) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 580 | { |
| 581 | int result = 0; |
| 582 | if (func != NULL) { |
| 583 | PyObject *r; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 584 | /* Note that readline is called with the interpreter |
| 585 | lock released! */ |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 586 | PyEval_RestoreThread(*tstate); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 587 | r = PyObject_CallFunction(func, NULL); |
| 588 | if (r == NULL) |
| 589 | goto error; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 590 | if (r == Py_None) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 591 | result = 0; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 592 | else |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 593 | result = PyInt_AsLong(r); |
| 594 | Py_DECREF(r); |
| 595 | goto done; |
| 596 | error: |
| 597 | PyErr_Clear(); |
| 598 | Py_XDECREF(r); |
| 599 | done: |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 600 | *tstate = PyEval_SaveThread(); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 601 | } |
| 602 | return result; |
| 603 | } |
| 604 | |
| 605 | static int |
| 606 | on_startup_hook(void) |
| 607 | { |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 608 | return on_hook(startup_hook, &startup_hook_tstate); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 612 | static int |
| 613 | on_pre_input_hook(void) |
| 614 | { |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 615 | return on_hook(pre_input_hook, &pre_input_hook_tstate); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 616 | } |
| 617 | #endif |
| 618 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 619 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 620 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 621 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 622 | static char * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 623 | on_completion(char *text, int state) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 624 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 625 | char *result = NULL; |
| 626 | if (completer != NULL) { |
| 627 | PyObject *r; |
| 628 | /* Note that readline is called with the interpreter |
| 629 | lock released! */ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 630 | PyEval_RestoreThread(completer_tstate); |
Michael W. Hudson | 0c1ceaf | 2002-02-13 11:58:25 +0000 | [diff] [blame] | 631 | /* Don't use the default filename completion if we |
| 632 | * have a custom completion function... */ |
| 633 | rl_attempted_completion_over = 1; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 634 | r = PyObject_CallFunction(completer, "si", text, state); |
| 635 | if (r == NULL) |
| 636 | goto error; |
| 637 | if (r == Py_None) { |
| 638 | result = NULL; |
| 639 | } |
| 640 | else { |
| 641 | char *s = PyString_AsString(r); |
| 642 | if (s == NULL) |
| 643 | goto error; |
| 644 | result = strdup(s); |
| 645 | } |
| 646 | Py_DECREF(r); |
| 647 | goto done; |
| 648 | error: |
| 649 | PyErr_Clear(); |
| 650 | Py_XDECREF(r); |
| 651 | done: |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 652 | completer_tstate = PyEval_SaveThread(); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 653 | } |
| 654 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 657 | |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 658 | /* A more flexible constructor that saves the "begidx" and "endidx" |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 659 | * before calling the normal completer */ |
| 660 | |
Neal Norwitz | c355f0c | 2003-02-21 00:30:18 +0000 | [diff] [blame] | 661 | static char ** |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 662 | flex_complete(char *text, int start, int end) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 663 | { |
| 664 | Py_XDECREF(begidx); |
| 665 | Py_XDECREF(endidx); |
| 666 | begidx = PyInt_FromLong((long) start); |
| 667 | endidx = PyInt_FromLong((long) end); |
| 668 | return completion_matches(text, *on_completion); |
| 669 | } |
| 670 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 671 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 672 | /* Helper to initialize GNU readline properly. */ |
| 673 | |
| 674 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 675 | setup_readline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 676 | { |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 677 | #ifdef SAVE_LOCALE |
Martin v. Löwis | f56d015 | 2003-11-13 07:43:21 +0000 | [diff] [blame] | 678 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
Martin v. Löwis | 701abe7 | 2004-08-20 06:26:59 +0000 | [diff] [blame] | 679 | if (!saved_locale) |
| 680 | Py_FatalError("not enough memory to save locale"); |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 681 | #endif |
| 682 | |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 683 | using_history(); |
| 684 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 685 | rl_readline_name = "python"; |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 686 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 687 | /* Allow $if term= in .inputrc to work */ |
| 688 | rl_terminal_name = getenv("TERM"); |
| 689 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 690 | /* Force rebind of TAB to insert-tab */ |
| 691 | rl_bind_key('\t', rl_insert); |
| 692 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 693 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 694 | 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] | 695 | /* Set our hook functions */ |
| 696 | rl_startup_hook = (Function *)on_startup_hook; |
| 697 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 698 | rl_pre_input_hook = (Function *)on_pre_input_hook; |
| 699 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 700 | /* Set our completion function */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 701 | rl_attempted_completion_function = (CPPFunction *)flex_complete; |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 702 | /* Set Python word break characters */ |
| 703 | rl_completer_word_break_characters = |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 704 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 705 | /* All nonalphanums except '.' */ |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 706 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Guido van Rossum | 84271bb | 2002-05-30 15:41:56 +0000 | [diff] [blame] | 707 | rl_completion_append_character ='\0'; |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 708 | #endif |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 709 | |
| 710 | begidx = PyInt_FromLong(0L); |
| 711 | endidx = PyInt_FromLong(0L); |
Barry Warsaw | f761287 | 1999-01-29 21:55:03 +0000 | [diff] [blame] | 712 | /* Initialize (allows .inputrc to override) |
| 713 | * |
| 714 | * XXX: A bug in the readline-2.2 library causes a memory leak |
| 715 | * inside this function. Nothing we can do about it. |
| 716 | */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 717 | rl_initialize(); |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 718 | |
| 719 | #ifdef SAVE_LOCALE |
| 720 | setlocale(LC_CTYPE, saved_locale); /* Restore locale */ |
Martin v. Löwis | f56d015 | 2003-11-13 07:43:21 +0000 | [diff] [blame] | 721 | free(saved_locale); |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 722 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 725 | /* Wrapper around GNU readline that handles signals differently. */ |
| 726 | |
| 727 | |
| 728 | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) |
| 729 | |
| 730 | static char *completed_input_string; |
| 731 | static void |
| 732 | rlhandler(char *text) |
| 733 | { |
| 734 | completed_input_string = text; |
| 735 | rl_callback_handler_remove(); |
| 736 | } |
| 737 | |
| 738 | extern PyThreadState* _PyOS_ReadlineTState; |
| 739 | |
| 740 | static char * |
| 741 | readline_until_enter_or_signal(char *prompt, int *signal) |
| 742 | { |
| 743 | char * not_done_reading = ""; |
| 744 | fd_set selectset; |
| 745 | |
| 746 | *signal = 0; |
| 747 | #ifdef HAVE_RL_CATCH_SIGNAL |
| 748 | rl_catch_signals = 0; |
| 749 | #endif |
| 750 | |
| 751 | rl_callback_handler_install (prompt, rlhandler); |
| 752 | FD_ZERO(&selectset); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 753 | |
| 754 | completed_input_string = not_done_reading; |
| 755 | |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 756 | while (completed_input_string == not_done_reading) { |
| 757 | int has_input = 0; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 758 | |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 759 | while (!has_input) |
| 760 | { struct timeval timeout = {0, 100000}; /* 0.1 seconds */ |
| 761 | FD_SET(fileno(rl_instream), &selectset); |
| 762 | /* select resets selectset if no input was available */ |
| 763 | has_input = select(fileno(rl_instream) + 1, &selectset, |
| 764 | NULL, NULL, &timeout); |
| 765 | if(PyOS_InputHook) PyOS_InputHook(); |
| 766 | } |
| 767 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 768 | if(has_input > 0) { |
| 769 | rl_callback_read_char(); |
| 770 | } |
| 771 | else if (errno == EINTR) { |
| 772 | int s; |
| 773 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
| 774 | s = PyErr_CheckSignals(); |
Michael W. Hudson | 2384990 | 2004-07-08 15:28:26 +0000 | [diff] [blame] | 775 | PyEval_SaveThread(); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 776 | if (s < 0) { |
| 777 | rl_free_line_state(); |
| 778 | rl_cleanup_after_signal(); |
| 779 | rl_callback_handler_remove(); |
| 780 | *signal = 1; |
| 781 | completed_input_string = NULL; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | return completed_input_string; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | #else |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 791 | |
| 792 | /* Interrupt handler */ |
| 793 | |
| 794 | static jmp_buf jbuf; |
| 795 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 796 | /* ARGSUSED */ |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 797 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 798 | onintr(int sig) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 799 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 800 | longjmp(jbuf, 1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 803 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 804 | static char * |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 805 | readline_until_enter_or_signal(char *prompt, int *signal) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 806 | { |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 807 | PyOS_sighandler_t old_inthandler; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 808 | char *p; |
| 809 | |
| 810 | *signal = 0; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 811 | |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 812 | old_inthandler = PyOS_setsig(SIGINT, onintr); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 813 | if (setjmp(jbuf)) { |
| 814 | #ifdef HAVE_SIGRELSE |
| 815 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 816 | sigrelse(SIGINT); |
| 817 | #endif |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 818 | PyOS_setsig(SIGINT, old_inthandler); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 819 | *signal = 1; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 820 | return NULL; |
| 821 | } |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 822 | rl_event_hook = PyOS_InputHook; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 823 | p = readline(prompt); |
| 824 | PyOS_setsig(SIGINT, old_inthandler); |
| 825 | |
| 826 | return p; |
| 827 | } |
| 828 | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */ |
| 829 | |
| 830 | |
| 831 | static char * |
| 832 | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
| 833 | { |
Neal Norwitz | 1fa040b | 2004-08-25 01:20:18 +0000 | [diff] [blame] | 834 | size_t n; |
| 835 | char *p, *q; |
| 836 | int signal; |
| 837 | |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 838 | #ifdef SAVE_LOCALE |
| 839 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
Martin v. Löwis | 701abe7 | 2004-08-20 06:26:59 +0000 | [diff] [blame] | 840 | if (!saved_locale) |
| 841 | Py_FatalError("not enough memory to save locale"); |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 842 | setlocale(LC_CTYPE, ""); |
| 843 | #endif |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 844 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 845 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { |
| 846 | rl_instream = sys_stdin; |
| 847 | rl_outstream = sys_stdout; |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 848 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 849 | rl_prep_terminal (1); |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 850 | #endif |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 853 | p = readline_until_enter_or_signal(prompt, &signal); |
| 854 | |
| 855 | /* we got an interrupt signal */ |
| 856 | if(signal) { |
| 857 | return NULL; |
| 858 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 859 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 860 | /* We got an EOF, return a empty string. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 861 | if (p == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 862 | p = PyMem_Malloc(1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 863 | if (p != NULL) |
| 864 | *p = '\0'; |
| 865 | return p; |
| 866 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 867 | |
| 868 | /* we have a valid line */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 869 | n = strlen(p); |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 870 | if (n > 0) { |
| 871 | char *line; |
| 872 | HISTORY_STATE *state = history_get_history_state(); |
| 873 | if (state->length > 0) |
| 874 | line = history_get(state->length)->line; |
| 875 | else |
| 876 | line = ""; |
| 877 | if (strcmp(p, line)) |
| 878 | add_history(p); |
| 879 | /* the history docs don't say so, but the address of state |
| 880 | changes each time history_get_history_state is called |
| 881 | which makes me think it's freshly malloc'd memory... |
| 882 | on the other hand, the address of the last line stays the |
| 883 | same as long as history isn't extended, so it appears to |
| 884 | be malloc'd but managed by the history package... */ |
| 885 | free(state); |
| 886 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 887 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and |
| 888 | release the original. */ |
| 889 | q = p; |
| 890 | p = PyMem_Malloc(n+2); |
| 891 | if (p != NULL) { |
| 892 | strncpy(p, q, n); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 893 | p[n] = '\n'; |
| 894 | p[n+1] = '\0'; |
| 895 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 896 | free(q); |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 897 | #ifdef SAVE_LOCALE |
| 898 | setlocale(LC_CTYPE, saved_locale); /* Restore locale */ |
| 899 | free(saved_locale); |
| 900 | #endif |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 901 | return p; |
| 902 | } |
| 903 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 904 | |
| 905 | /* Initialize the module */ |
| 906 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 907 | PyDoc_STRVAR(doc_module, |
| 908 | "Importing this module enables command line editing using GNU readline."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 909 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 910 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 911 | initreadline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 912 | { |
Guido van Rossum | 1ea64ea | 2000-10-02 15:53:08 +0000 | [diff] [blame] | 913 | PyObject *m; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 914 | |
| 915 | m = Py_InitModule4("readline", readline_methods, doc_module, |
| 916 | (PyObject *)NULL, PYTHON_API_VERSION); |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 917 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 918 | PyOS_ReadlineFunctionPointer = call_readline; |
| 919 | setup_readline(); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 920 | } |