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