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 |
Michael W. Hudson | 9a8c314 | 2005-03-30 10:09:12 +0000 | [diff] [blame] | 3 | * Center. The completer interface was inspired by Lele Gaifax. More |
| 4 | * recently, it was largely rewritten by Guido van Rossum. |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 7 | /* Standard definitions */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 8 | #include "Python.h" |
| 9 | #include <setjmp.h> |
| 10 | #include <signal.h> |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 11 | #include <errno.h> |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 12 | #include <sys/time.h> |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 13 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 14 | #if defined(HAVE_SETLOCALE) |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 15 | /* GNU readline() mistakenly sets the LC_CTYPE locale. |
| 16 | * This is evil. Only the user or the app's main() should do this! |
| 17 | * We must save and restore the locale around the rl_initialize() call. |
| 18 | */ |
| 19 | #define SAVE_LOCALE |
| 20 | #include <locale.h> |
| 21 | #endif |
| 22 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 23 | #ifdef SAVE_LOCALE |
| 24 | # define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); } |
| 25 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | # define RESTORE_LOCALE(sl) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 29 | /* GNU readline definitions */ |
Guido van Rossum | b0e51b2 | 2001-04-13 18:14:27 +0000 | [diff] [blame] | 30 | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 31 | #include <readline/readline.h> |
| 32 | #include <readline/history.h> |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 34 | #ifdef HAVE_RL_COMPLETION_MATCHES |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 35 | #define completion_matches(x, y) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | rl_completion_matches((x), ((rl_compentry_func_t *)(y))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 37 | #else |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 38 | #if defined(_RL_FUNCTION_TYPEDEF) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 39 | extern char **completion_matches(char *, rl_compentry_func_t *); |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 40 | #else |
Ronald Oussoren | 25696bb | 2010-02-11 13:15:00 +0000 | [diff] [blame] | 41 | |
| 42 | #if !defined(__APPLE__) |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 43 | extern char **completion_matches(char *, CPFunction *); |
| 44 | #endif |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 45 | #endif |
Ronald Oussoren | 25696bb | 2010-02-11 13:15:00 +0000 | [diff] [blame] | 46 | #endif |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 47 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 48 | #ifdef __APPLE__ |
| 49 | /* |
| 50 | * It is possible to link the readline module to the readline |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | * emulation library of editline/libedit. |
| 52 | * |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 53 | * On OSX this emulation library is not 100% API compatible |
| 54 | * with the "real" readline and cannot be detected at compile-time, |
| 55 | * hence we use a runtime check to detect if we're using libedit |
| 56 | * |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | * Currently there is one know API incompatibility: |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 58 | * - 'get_history' has a 1-based index with GNU readline, and a 0-based |
| 59 | * index with libedit's emulation. |
| 60 | * - Note that replace_history and remove_history use a 0-based index |
| 61 | * with both implementation. |
| 62 | */ |
| 63 | static int using_libedit_emulation = 0; |
| 64 | static const char libedit_version_tag[] = "EditLine wrapper"; |
| 65 | #endif /* __APPLE__ */ |
| 66 | |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 67 | static void |
| 68 | on_completion_display_matches_hook(char **matches, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | int num_matches, int max_length); |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 72 | /* Exported function to send one line to readline's init file parser */ |
| 73 | |
| 74 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 75 | parse_and_bind(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 76 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | char *s, *copy; |
| 78 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) |
| 79 | return NULL; |
| 80 | /* Make a copy -- rl_parse_and_bind() modifies its argument */ |
| 81 | /* Bernard Herzog */ |
| 82 | copy = malloc(1 + strlen(s)); |
| 83 | if (copy == NULL) |
| 84 | return PyErr_NoMemory(); |
| 85 | strcpy(copy, s); |
| 86 | rl_parse_and_bind(copy); |
| 87 | free(copy); /* Free the copy */ |
| 88 | Py_RETURN_NONE; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 91 | PyDoc_STRVAR(doc_parse_and_bind, |
| 92 | "parse_and_bind(string) -> None\n\ |
| 93 | Parse and execute single line of a readline init file."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 94 | |
| 95 | |
| 96 | /* Exported function to parse a readline init file */ |
| 97 | |
| 98 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 99 | read_init_file(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 100 | { |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 101 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 102 | if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | return NULL; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 104 | if (filename_obj != Py_None) { |
| 105 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 106 | return NULL; |
| 107 | errno = rl_read_init_file(PyBytes_AsString(filename_bytes)); |
| 108 | Py_DECREF(filename_bytes); |
| 109 | } else |
| 110 | errno = rl_read_init_file(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | if (errno) |
| 112 | return PyErr_SetFromErrno(PyExc_IOError); |
| 113 | Py_RETURN_NONE; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 116 | PyDoc_STRVAR(doc_read_init_file, |
| 117 | "read_init_file([filename]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 118 | Parse a readline initialization file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 119 | The default filename is the last filename used."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 120 | |
| 121 | |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 122 | /* Exported function to load a readline history file */ |
| 123 | |
| 124 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 125 | read_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 126 | { |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 127 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 128 | if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | return NULL; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 130 | if (filename_obj != Py_None) { |
| 131 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 132 | return NULL; |
| 133 | errno = read_history(PyBytes_AsString(filename_bytes)); |
| 134 | Py_DECREF(filename_bytes); |
| 135 | } else |
| 136 | errno = read_history(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | if (errno) |
| 138 | return PyErr_SetFromErrno(PyExc_IOError); |
| 139 | Py_RETURN_NONE; |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Hye-Shik Chang | 7a8173a | 2004-11-25 04:04:20 +0000 | [diff] [blame] | 142 | 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] | 143 | PyDoc_STRVAR(doc_read_history_file, |
| 144 | "read_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 145 | Load a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 146 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | /* Exported function to save a readline history file */ |
| 150 | |
| 151 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 152 | write_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 153 | { |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 154 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 155 | char *filename; |
| 156 | if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | return NULL; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 158 | if (filename_obj != Py_None) { |
| 159 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 160 | return NULL; |
| 161 | filename = PyBytes_AsString(filename_bytes); |
| 162 | } else { |
| 163 | filename_bytes = NULL; |
| 164 | filename = NULL; |
| 165 | } |
| 166 | errno = write_history(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | if (!errno && _history_length >= 0) |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 168 | history_truncate_file(filename, _history_length); |
| 169 | Py_XDECREF(filename_bytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | if (errno) |
| 171 | return PyErr_SetFromErrno(PyExc_IOError); |
| 172 | Py_RETURN_NONE; |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 175 | PyDoc_STRVAR(doc_write_history_file, |
| 176 | "write_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 177 | Save a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 178 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 179 | |
| 180 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 181 | /* Set history length */ |
| 182 | |
| 183 | static PyObject* |
| 184 | set_history_length(PyObject *self, PyObject *args) |
| 185 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | int length = _history_length; |
| 187 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) |
| 188 | return NULL; |
| 189 | _history_length = length; |
| 190 | Py_RETURN_NONE; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 193 | PyDoc_STRVAR(set_history_length_doc, |
| 194 | "set_history_length(length) -> None\n\ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 195 | set the maximal number of items which will be written to\n\ |
| 196 | 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] | 197 | history truncation."); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 198 | |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 200 | /* Get history length */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 201 | |
| 202 | static PyObject* |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 203 | get_history_length(PyObject *self, PyObject *noarg) |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | return PyLong_FromLong(_history_length); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 208 | PyDoc_STRVAR(get_history_length_doc, |
| 209 | "get_history_length() -> int\n\ |
| 210 | return the maximum number of items that will be written to\n\ |
| 211 | the history file."); |
| 212 | |
| 213 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 214 | /* Generic hook function setter */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 215 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 216 | static PyObject * |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 217 | set_hook(const char *funcname, PyObject **hook_var, PyObject *args) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | PyObject *function = Py_None; |
| 220 | char buf[80]; |
| 221 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); |
| 222 | if (!PyArg_ParseTuple(args, buf, &function)) |
| 223 | return NULL; |
| 224 | if (function == Py_None) { |
| 225 | Py_XDECREF(*hook_var); |
| 226 | *hook_var = NULL; |
| 227 | } |
| 228 | else if (PyCallable_Check(function)) { |
| 229 | PyObject *tmp = *hook_var; |
| 230 | Py_INCREF(function); |
| 231 | *hook_var = function; |
| 232 | Py_XDECREF(tmp); |
| 233 | } |
| 234 | else { |
| 235 | PyOS_snprintf(buf, sizeof(buf), |
| 236 | "set_%.50s(func): argument not callable", |
| 237 | funcname); |
| 238 | PyErr_SetString(PyExc_TypeError, buf); |
| 239 | return NULL; |
| 240 | } |
| 241 | Py_RETURN_NONE; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 244 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 245 | /* Exported functions to specify hook functions in Python */ |
| 246 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 247 | static PyObject *completion_display_matches_hook = NULL; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 248 | static PyObject *startup_hook = NULL; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 249 | |
| 250 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 251 | static PyObject *pre_input_hook = NULL; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 252 | #endif |
| 253 | |
| 254 | static PyObject * |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 255 | set_completion_display_matches_hook(PyObject *self, PyObject *args) |
| 256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | PyObject *result = set_hook("completion_display_matches_hook", |
| 258 | &completion_display_matches_hook, args); |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 259 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | /* We cannot set this hook globally, since it replaces the |
| 261 | default completion display. */ |
| 262 | rl_completion_display_matches_hook = |
| 263 | completion_display_matches_hook ? |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 264 | #if defined(_RL_FUNCTION_TYPEDEF) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | (rl_compdisp_func_t *)on_completion_display_matches_hook : 0; |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 266 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | (VFunction *)on_completion_display_matches_hook : 0; |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 268 | #endif |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 269 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | return result; |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 271 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | PyDoc_STRVAR(doc_set_completion_display_matches_hook, |
| 275 | "set_completion_display_matches_hook([function]) -> None\n\ |
| 276 | Set or remove the completion display function.\n\ |
| 277 | The function is called as\n\ |
| 278 | function(substitution, [matches], longest_match_length)\n\ |
| 279 | once each time matches need to be displayed."); |
| 280 | |
| 281 | static PyObject * |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 282 | set_startup_hook(PyObject *self, PyObject *args) |
| 283 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | return set_hook("startup_hook", &startup_hook, args); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 287 | PyDoc_STRVAR(doc_set_startup_hook, |
| 288 | "set_startup_hook([function]) -> None\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 289 | Set or remove the startup_hook function.\n\ |
| 290 | The function is called with no arguments just\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 291 | before readline prints the first prompt."); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 292 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 293 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 294 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 295 | |
| 296 | /* Set pre-input hook */ |
| 297 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 298 | static PyObject * |
| 299 | set_pre_input_hook(PyObject *self, PyObject *args) |
| 300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | return set_hook("pre_input_hook", &pre_input_hook, args); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 304 | PyDoc_STRVAR(doc_set_pre_input_hook, |
| 305 | "set_pre_input_hook([function]) -> None\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 306 | Set or remove the pre_input_hook function.\n\ |
| 307 | The function is called with no arguments after the first prompt\n\ |
| 308 | 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] | 309 | characters."); |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 310 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 311 | #endif |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 314 | /* Exported function to specify a word completer in Python */ |
| 315 | |
| 316 | static PyObject *completer = NULL; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 317 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 318 | static PyObject *begidx = NULL; |
| 319 | static PyObject *endidx = NULL; |
| 320 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 321 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 322 | /* Get the completion type for the scope of the tab-completion */ |
| 323 | static PyObject * |
| 324 | get_completion_type(PyObject *self, PyObject *noarg) |
| 325 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 326 | return PyLong_FromLong(rl_completion_type); |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | PyDoc_STRVAR(doc_get_completion_type, |
| 330 | "get_completion_type() -> int\n\ |
| 331 | Get the type of completion being attempted."); |
| 332 | |
| 333 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 334 | /* Get the beginning index for the scope of the tab-completion */ |
| 335 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 336 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 337 | get_begidx(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | Py_INCREF(begidx); |
| 340 | return begidx; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 343 | PyDoc_STRVAR(doc_get_begidx, |
| 344 | "get_begidx() -> int\n\ |
| 345 | get the beginning index of the readline tab-completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 347 | |
| 348 | /* Get the ending index for the scope of the tab-completion */ |
| 349 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 350 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 351 | get_endidx(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | Py_INCREF(endidx); |
| 354 | return endidx; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 357 | PyDoc_STRVAR(doc_get_endidx, |
| 358 | "get_endidx() -> int\n\ |
| 359 | get the ending index of the readline tab-completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 360 | |
| 361 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 362 | /* Set the tab-completion word-delimiters that readline uses */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 363 | |
| 364 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 365 | set_completer_delims(PyObject *self, PyObject *args) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 366 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | char *break_chars; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { |
| 370 | return NULL; |
| 371 | } |
| 372 | free((void*)rl_completer_word_break_characters); |
| 373 | rl_completer_word_break_characters = strdup(break_chars); |
| 374 | Py_RETURN_NONE; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 377 | PyDoc_STRVAR(doc_set_completer_delims, |
| 378 | "set_completer_delims(string) -> None\n\ |
| 379 | set the readline word delimiters for tab-completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 380 | |
Mark Dickinson | 29b238e | 2010-08-03 16:08:16 +0000 | [diff] [blame] | 381 | /* _py_free_history_entry: Utility function to free a history entry. */ |
| 382 | |
| 383 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500 |
| 384 | |
| 385 | /* Readline version >= 5.0 introduced a timestamp field into the history entry |
| 386 | structure; this needs to be freed to avoid a memory leak. This version of |
| 387 | readline also introduced the handy 'free_history_entry' function, which |
| 388 | takes care of the timestamp. */ |
| 389 | |
| 390 | static void |
| 391 | _py_free_history_entry(HIST_ENTRY *entry) |
| 392 | { |
| 393 | histdata_t data = free_history_entry(entry); |
| 394 | free(data); |
| 395 | } |
| 396 | |
| 397 | #else |
| 398 | |
| 399 | /* No free_history_entry function; free everything manually. */ |
| 400 | |
| 401 | static void |
| 402 | _py_free_history_entry(HIST_ENTRY *entry) |
| 403 | { |
| 404 | if (entry->line) |
| 405 | free((void *)entry->line); |
| 406 | if (entry->data) |
| 407 | free(entry->data); |
| 408 | free(entry); |
| 409 | } |
| 410 | |
| 411 | #endif |
| 412 | |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 413 | static PyObject * |
| 414 | py_remove_history(PyObject *self, PyObject *args) |
| 415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | int entry_number; |
| 417 | HIST_ENTRY *entry; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number)) |
| 420 | return NULL; |
| 421 | if (entry_number < 0) { |
| 422 | PyErr_SetString(PyExc_ValueError, |
| 423 | "History index cannot be negative"); |
| 424 | return NULL; |
| 425 | } |
| 426 | entry = remove_history(entry_number); |
| 427 | if (!entry) { |
| 428 | PyErr_Format(PyExc_ValueError, |
| 429 | "No history item at position %d", |
| 430 | entry_number); |
| 431 | return NULL; |
| 432 | } |
| 433 | /* free memory allocated for the history entry */ |
Mark Dickinson | 29b238e | 2010-08-03 16:08:16 +0000 | [diff] [blame] | 434 | _py_free_history_entry(entry); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | Py_RETURN_NONE; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | PyDoc_STRVAR(doc_remove_history, |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 439 | "remove_history_item(pos) -> None\n\ |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 440 | remove history item given by its position"); |
| 441 | |
| 442 | static PyObject * |
| 443 | py_replace_history(PyObject *self, PyObject *args) |
| 444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | int entry_number; |
| 446 | char *line; |
| 447 | HIST_ENTRY *old_entry; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, |
| 450 | &line)) { |
| 451 | return NULL; |
| 452 | } |
| 453 | if (entry_number < 0) { |
| 454 | PyErr_SetString(PyExc_ValueError, |
| 455 | "History index cannot be negative"); |
| 456 | return NULL; |
| 457 | } |
| 458 | old_entry = replace_history_entry(entry_number, line, (void *)NULL); |
| 459 | if (!old_entry) { |
| 460 | PyErr_Format(PyExc_ValueError, |
| 461 | "No history item at position %d", |
| 462 | entry_number); |
| 463 | return NULL; |
| 464 | } |
| 465 | /* free memory allocated for the old history entry */ |
Mark Dickinson | 29b238e | 2010-08-03 16:08:16 +0000 | [diff] [blame] | 466 | _py_free_history_entry(old_entry); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | Py_RETURN_NONE; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | PyDoc_STRVAR(doc_replace_history, |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 471 | "replace_history_item(pos, line) -> None\n\ |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 472 | replaces history item given by its position with contents of line"); |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 473 | |
| 474 | /* Add a line to the history buffer */ |
| 475 | |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 476 | static PyObject * |
| 477 | py_add_history(PyObject *self, PyObject *args) |
| 478 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | char *line; |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | if(!PyArg_ParseTuple(args, "s:add_history", &line)) { |
| 482 | return NULL; |
| 483 | } |
| 484 | add_history(line); |
| 485 | Py_RETURN_NONE; |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 488 | PyDoc_STRVAR(doc_add_history, |
| 489 | "add_history(string) -> None\n\ |
| 490 | add a line to the history buffer"); |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 491 | |
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 | /* Get the tab-completion word-delimiters that readline uses */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 494 | |
| 495 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 496 | get_completer_delims(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | return PyUnicode_FromString(rl_completer_word_break_characters); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 499 | } |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 500 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 501 | PyDoc_STRVAR(doc_get_completer_delims, |
| 502 | "get_completer_delims() -> string\n\ |
| 503 | get the readline word delimiters for tab-completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 504 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 505 | |
| 506 | /* Set the completer function */ |
| 507 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 508 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 509 | set_completer(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 510 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | return set_hook("completer", &completer, args); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 514 | PyDoc_STRVAR(doc_set_completer, |
| 515 | "set_completer([function]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 516 | Set or remove the completer function.\n\ |
| 517 | The function is called as function(text, state),\n\ |
Fred Drake | 52d55a3 | 2001-08-01 21:44:14 +0000 | [diff] [blame] | 518 | 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] | 519 | It should return the next possible completion starting with 'text'."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 520 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 521 | |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 522 | static PyObject * |
Neal Norwitz | d9efdc5 | 2003-03-01 15:19:41 +0000 | [diff] [blame] | 523 | get_completer(PyObject *self, PyObject *noargs) |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 524 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | if (completer == NULL) { |
| 526 | Py_RETURN_NONE; |
| 527 | } |
| 528 | Py_INCREF(completer); |
| 529 | return completer; |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | PyDoc_STRVAR(doc_get_completer, |
| 533 | "get_completer() -> function\n\ |
| 534 | \n\ |
| 535 | Returns current completer function."); |
| 536 | |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 537 | /* Private function to get current length of history. XXX It may be |
| 538 | * possible to replace this with a direct use of history_length instead, |
| 539 | * but it's not clear whether BSD's libedit keeps history_length up to date. |
| 540 | * See issue #8065.*/ |
| 541 | |
| 542 | static int |
| 543 | _py_get_history_length(void) |
| 544 | { |
| 545 | HISTORY_STATE *hist_st = history_get_history_state(); |
| 546 | int length = hist_st->length; |
| 547 | /* the history docs don't say so, but the address of hist_st changes each |
| 548 | time history_get_history_state is called which makes me think it's |
| 549 | freshly malloc'd memory... on the other hand, the address of the last |
| 550 | line stays the same as long as history isn't extended, so it appears to |
| 551 | be malloc'd but managed by the history package... */ |
| 552 | free(hist_st); |
| 553 | return length; |
| 554 | } |
| 555 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 556 | /* Exported function to get any element of history */ |
| 557 | |
| 558 | static PyObject * |
| 559 | get_history_item(PyObject *self, PyObject *args) |
| 560 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | int idx = 0; |
| 562 | HIST_ENTRY *hist_ent; |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | if (!PyArg_ParseTuple(args, "i:index", &idx)) |
| 565 | return NULL; |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 566 | #ifdef __APPLE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | if (using_libedit_emulation) { |
| 568 | /* Libedit emulation uses 0-based indexes, |
| 569 | * the real one uses 1-based indexes, |
| 570 | * adjust the index to ensure that Python |
| 571 | * code doesn't have to worry about the |
| 572 | * difference. |
| 573 | */ |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 574 | int length = _py_get_history_length(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | idx --; |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | /* |
| 578 | * Apple's readline emulation crashes when |
| 579 | * the index is out of range, therefore |
| 580 | * test for that and fail gracefully. |
| 581 | */ |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 582 | if (idx < 0 || idx >= length) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | Py_RETURN_NONE; |
| 584 | } |
| 585 | } |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 586 | #endif /* __APPLE__ */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | if ((hist_ent = history_get(idx))) |
| 588 | return PyUnicode_FromString(hist_ent->line); |
| 589 | else { |
| 590 | Py_RETURN_NONE; |
| 591 | } |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 594 | PyDoc_STRVAR(doc_get_history_item, |
| 595 | "get_history_item() -> string\n\ |
| 596 | return the current contents of history item at index."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 597 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 598 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 599 | /* Exported function to get current length of history */ |
| 600 | |
| 601 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 602 | get_current_history_length(PyObject *self, PyObject *noarg) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 603 | { |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 604 | return PyLong_FromLong((long)_py_get_history_length()); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 607 | PyDoc_STRVAR(doc_get_current_history_length, |
| 608 | "get_current_history_length() -> integer\n\ |
| 609 | return the current (not the maximum) length of history."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 610 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 611 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 612 | /* Exported function to read the current line buffer */ |
| 613 | |
| 614 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 615 | get_line_buffer(PyObject *self, PyObject *noarg) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 616 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | return PyUnicode_FromString(rl_line_buffer); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 620 | PyDoc_STRVAR(doc_get_line_buffer, |
| 621 | "get_line_buffer() -> string\n\ |
| 622 | return the current contents of the line buffer."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 623 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 624 | |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 625 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
| 626 | |
| 627 | /* Exported function to clear the current history */ |
| 628 | |
| 629 | static PyObject * |
| 630 | py_clear_history(PyObject *self, PyObject *noarg) |
| 631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | clear_history(); |
| 633 | Py_RETURN_NONE; |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | PyDoc_STRVAR(doc_clear_history, |
| 637 | "clear_history() -> None\n\ |
| 638 | Clear the current readline history."); |
| 639 | #endif |
| 640 | |
| 641 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 642 | /* Exported function to insert text into the line buffer */ |
| 643 | |
| 644 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 645 | insert_text(PyObject *self, PyObject *args) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | char *s; |
| 648 | if (!PyArg_ParseTuple(args, "s:insert_text", &s)) |
| 649 | return NULL; |
| 650 | rl_insert_text(s); |
| 651 | Py_RETURN_NONE; |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 654 | PyDoc_STRVAR(doc_insert_text, |
| 655 | "insert_text(string) -> None\n\ |
| 656 | Insert text into the command line."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 657 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 658 | |
| 659 | /* Redisplay the line buffer */ |
| 660 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 661 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 662 | redisplay(PyObject *self, PyObject *noarg) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 663 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | rl_redisplay(); |
| 665 | Py_RETURN_NONE; |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 668 | PyDoc_STRVAR(doc_redisplay, |
| 669 | "redisplay() -> None\n\ |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 670 | 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] | 671 | contents of the line buffer."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 672 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 673 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 674 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 675 | |
| 676 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 677 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind}, |
| 679 | {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer}, |
| 680 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text}, |
| 681 | {"redisplay", redisplay, METH_NOARGS, doc_redisplay}, |
| 682 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, |
| 683 | {"read_history_file", read_history_file, |
| 684 | METH_VARARGS, doc_read_history_file}, |
| 685 | {"write_history_file", write_history_file, |
| 686 | METH_VARARGS, doc_write_history_file}, |
| 687 | {"get_history_item", get_history_item, |
| 688 | METH_VARARGS, doc_get_history_item}, |
| 689 | {"get_current_history_length", (PyCFunction)get_current_history_length, |
| 690 | METH_NOARGS, doc_get_current_history_length}, |
| 691 | {"set_history_length", set_history_length, |
| 692 | METH_VARARGS, set_history_length_doc}, |
| 693 | {"get_history_length", get_history_length, |
| 694 | METH_NOARGS, get_history_length_doc}, |
| 695 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer}, |
| 696 | {"get_completer", get_completer, METH_NOARGS, doc_get_completer}, |
| 697 | {"get_completion_type", get_completion_type, |
| 698 | METH_NOARGS, doc_get_completion_type}, |
| 699 | {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx}, |
| 700 | {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx}, |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | {"set_completer_delims", set_completer_delims, |
| 703 | METH_VARARGS, doc_set_completer_delims}, |
| 704 | {"add_history", py_add_history, METH_VARARGS, doc_add_history}, |
| 705 | {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history}, |
| 706 | {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history}, |
| 707 | {"get_completer_delims", get_completer_delims, |
| 708 | METH_NOARGS, doc_get_completer_delims}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | {"set_completion_display_matches_hook", set_completion_display_matches_hook, |
| 711 | METH_VARARGS, doc_set_completion_display_matches_hook}, |
| 712 | {"set_startup_hook", set_startup_hook, |
| 713 | METH_VARARGS, doc_set_startup_hook}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 714 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | {"set_pre_input_hook", set_pre_input_hook, |
| 716 | METH_VARARGS, doc_set_pre_input_hook}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 717 | #endif |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 718 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history}, |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 720 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 722 | }; |
| 723 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 724 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 725 | /* C function to call the Python hooks. */ |
| 726 | |
| 727 | static int |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 728 | on_hook(PyObject *func) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | int result = 0; |
| 731 | if (func != NULL) { |
| 732 | PyObject *r; |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 733 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 735 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | r = PyObject_CallFunction(func, NULL); |
| 737 | if (r == NULL) |
| 738 | goto error; |
| 739 | if (r == Py_None) |
| 740 | result = 0; |
| 741 | else { |
| 742 | result = PyLong_AsLong(r); |
| 743 | if (result == -1 && PyErr_Occurred()) |
| 744 | goto error; |
| 745 | } |
| 746 | Py_DECREF(r); |
| 747 | goto done; |
| 748 | error: |
| 749 | PyErr_Clear(); |
| 750 | Py_XDECREF(r); |
| 751 | done: |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 752 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | PyGILState_Release(gilstate); |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 754 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | return result; |
| 756 | } |
| 757 | return result; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | static int |
| 761 | on_startup_hook(void) |
| 762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | return on_hook(startup_hook); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 767 | static int |
| 768 | on_pre_input_hook(void) |
| 769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | return on_hook(pre_input_hook); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 771 | } |
| 772 | #endif |
| 773 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 774 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 775 | /* C function to call the Python completion_display_matches */ |
| 776 | |
| 777 | static void |
| 778 | on_completion_display_matches_hook(char **matches, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | int num_matches, int max_length) |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 780 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | int i; |
| 782 | PyObject *m=NULL, *s=NULL, *r=NULL; |
Christian Heimes | aec75c3 | 2007-11-11 22:42:36 +0000 | [diff] [blame] | 783 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 785 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | m = PyList_New(num_matches); |
| 787 | if (m == NULL) |
| 788 | goto error; |
| 789 | for (i = 0; i < num_matches; i++) { |
| 790 | s = PyUnicode_FromString(matches[i+1]); |
| 791 | if (s == NULL) |
| 792 | goto error; |
| 793 | if (PyList_SetItem(m, i, s) == -1) |
| 794 | goto error; |
| 795 | } |
| 796 | r = PyObject_CallFunction(completion_display_matches_hook, |
| 797 | "sOi", matches[0], m, max_length); |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | Py_DECREF(m); m=NULL; |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | if (r == NULL || |
| 802 | (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) { |
| 803 | goto error; |
| 804 | } |
| 805 | Py_XDECREF(r); r=NULL; |
| 806 | |
| 807 | if (0) { |
| 808 | error: |
| 809 | PyErr_Clear(); |
| 810 | Py_XDECREF(m); |
| 811 | Py_XDECREF(r); |
| 812 | } |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 813 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | PyGILState_Release(gilstate); |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 815 | #endif |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 819 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 820 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 821 | static char * |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 822 | on_completion(const char *text, int state) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 823 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | char *result = NULL; |
| 825 | if (completer != NULL) { |
| 826 | PyObject *r; |
Christian Heimes | aec75c3 | 2007-11-11 22:42:36 +0000 | [diff] [blame] | 827 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 829 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | rl_attempted_completion_over = 1; |
| 831 | r = PyObject_CallFunction(completer, "si", text, state); |
| 832 | if (r == NULL) |
| 833 | goto error; |
| 834 | if (r == Py_None) { |
| 835 | result = NULL; |
| 836 | } |
| 837 | else { |
| 838 | char *s = _PyUnicode_AsString(r); |
| 839 | if (s == NULL) |
| 840 | goto error; |
| 841 | result = strdup(s); |
| 842 | } |
| 843 | Py_DECREF(r); |
| 844 | goto done; |
| 845 | error: |
| 846 | PyErr_Clear(); |
| 847 | Py_XDECREF(r); |
| 848 | done: |
Christian Heimes | aec75c3 | 2007-11-11 22:42:36 +0000 | [diff] [blame] | 849 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | PyGILState_Release(gilstate); |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 851 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 852 | return result; |
| 853 | } |
| 854 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 857 | |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 858 | /* A more flexible constructor that saves the "begidx" and "endidx" |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 859 | * before calling the normal completer */ |
| 860 | |
Neal Norwitz | c355f0c | 2003-02-21 00:30:18 +0000 | [diff] [blame] | 861 | static char ** |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 862 | flex_complete(char *text, int start, int end) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 863 | { |
Antoine Pitrou | dc0900b | 2009-10-19 18:22:37 +0000 | [diff] [blame] | 864 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | rl_completion_append_character ='\0'; |
Antoine Pitrou | d513177 | 2009-10-26 19:22:14 +0000 | [diff] [blame] | 866 | #endif |
| 867 | #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | rl_completion_suppress_append = 0; |
Antoine Pitrou | dc0900b | 2009-10-19 18:22:37 +0000 | [diff] [blame] | 869 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | Py_XDECREF(begidx); |
| 871 | Py_XDECREF(endidx); |
| 872 | begidx = PyLong_FromLong((long) start); |
| 873 | endidx = PyLong_FromLong((long) end); |
| 874 | return completion_matches(text, *on_completion); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 877 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 878 | /* Helper to initialize GNU readline properly. */ |
| 879 | |
| 880 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 881 | setup_readline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 882 | { |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 883 | #ifdef SAVE_LOCALE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
| 885 | if (!saved_locale) |
| 886 | Py_FatalError("not enough memory to save locale"); |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 887 | #endif |
| 888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | using_history(); |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | rl_readline_name = "python"; |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 892 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | /* Allow $if term= in .inputrc to work */ |
| 894 | rl_terminal_name = getenv("TERM"); |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 895 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | /* Force rebind of TAB to insert-tab */ |
| 897 | rl_bind_key('\t', rl_insert); |
| 898 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 899 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 900 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap); |
| 901 | /* Set our hook functions */ |
| 902 | rl_startup_hook = (Function *)on_startup_hook; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 903 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | rl_pre_input_hook = (Function *)on_pre_input_hook; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 905 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | /* Set our completion function */ |
| 907 | rl_attempted_completion_function = (CPPFunction *)flex_complete; |
| 908 | /* Set Python word break characters */ |
| 909 | rl_completer_word_break_characters = |
| 910 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); |
| 911 | /* All nonalphanums except '.' */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | begidx = PyLong_FromLong(0L); |
| 914 | endidx = PyLong_FromLong(0L); |
| 915 | /* Initialize (allows .inputrc to override) |
| 916 | * |
| 917 | * XXX: A bug in the readline-2.2 library causes a memory leak |
| 918 | * inside this function. Nothing we can do about it. |
| 919 | */ |
| 920 | rl_initialize(); |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | RESTORE_LOCALE(saved_locale) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 925 | /* Wrapper around GNU readline that handles signals differently. */ |
| 926 | |
| 927 | |
| 928 | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) |
| 929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | static char *completed_input_string; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 931 | static void |
| 932 | rlhandler(char *text) |
| 933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | completed_input_string = text; |
| 935 | rl_callback_handler_remove(); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | extern PyThreadState* _PyOS_ReadlineTState; |
| 939 | |
| 940 | static char * |
| 941 | readline_until_enter_or_signal(char *prompt, int *signal) |
| 942 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | char * not_done_reading = ""; |
| 944 | fd_set selectset; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | *signal = 0; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 947 | #ifdef HAVE_RL_CATCH_SIGNAL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | rl_catch_signals = 0; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 949 | #endif |
| 950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 951 | rl_callback_handler_install (prompt, rlhandler); |
| 952 | FD_ZERO(&selectset); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | completed_input_string = not_done_reading; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | while (completed_input_string == not_done_reading) { |
| 957 | int has_input = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | while (!has_input) |
| 960 | { struct timeval timeout = {0, 100000}; /* 0.1 seconds */ |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | /* [Bug #1552726] Only limit the pause if an input hook has been |
| 963 | defined. */ |
| 964 | struct timeval *timeoutp = NULL; |
| 965 | if (PyOS_InputHook) |
| 966 | timeoutp = &timeout; |
| 967 | FD_SET(fileno(rl_instream), &selectset); |
| 968 | /* select resets selectset if no input was available */ |
| 969 | has_input = select(fileno(rl_instream) + 1, &selectset, |
| 970 | NULL, NULL, timeoutp); |
| 971 | if(PyOS_InputHook) PyOS_InputHook(); |
| 972 | } |
| 973 | |
| 974 | if(has_input > 0) { |
| 975 | rl_callback_read_char(); |
| 976 | } |
| 977 | else if (errno == EINTR) { |
| 978 | int s; |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 979 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 981 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | s = PyErr_CheckSignals(); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 983 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | PyEval_SaveThread(); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 985 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | if (s < 0) { |
| 987 | rl_free_line_state(); |
| 988 | rl_cleanup_after_signal(); |
| 989 | rl_callback_handler_remove(); |
| 990 | *signal = 1; |
| 991 | completed_input_string = NULL; |
| 992 | } |
| 993 | } |
| 994 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | return completed_input_string; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | |
| 1000 | #else |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1001 | |
| 1002 | /* Interrupt handler */ |
| 1003 | |
| 1004 | static jmp_buf jbuf; |
| 1005 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1006 | /* ARGSUSED */ |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 1007 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 1008 | onintr(int sig) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1009 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | longjmp(jbuf, 1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1013 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1014 | static char * |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1015 | readline_until_enter_or_signal(char *prompt, int *signal) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1016 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | PyOS_sighandler_t old_inthandler; |
| 1018 | char *p; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | *signal = 0; |
| 1021 | |
| 1022 | old_inthandler = PyOS_setsig(SIGINT, onintr); |
| 1023 | if (setjmp(jbuf)) { |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1024 | #ifdef HAVE_SIGRELSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 1026 | sigrelse(SIGINT); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1027 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | PyOS_setsig(SIGINT, old_inthandler); |
| 1029 | *signal = 1; |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | rl_event_hook = PyOS_InputHook; |
| 1033 | p = readline(prompt); |
| 1034 | PyOS_setsig(SIGINT, old_inthandler); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1035 | |
| 1036 | return p; |
| 1037 | } |
| 1038 | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */ |
| 1039 | |
| 1040 | |
| 1041 | static char * |
| 1042 | call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
| 1043 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | size_t n; |
| 1045 | char *p, *q; |
| 1046 | int signal; |
Neal Norwitz | 1fa040b | 2004-08-25 01:20:18 +0000 | [diff] [blame] | 1047 | |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 1048 | #ifdef SAVE_LOCALE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
| 1050 | if (!saved_locale) |
| 1051 | Py_FatalError("not enough memory to save locale"); |
| 1052 | setlocale(LC_CTYPE, ""); |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 1053 | #endif |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { |
| 1056 | rl_instream = sys_stdin; |
| 1057 | rl_outstream = sys_stdout; |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 1058 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | rl_prep_terminal (1); |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 1060 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | } |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 1062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | p = readline_until_enter_or_signal(prompt, &signal); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | /* we got an interrupt signal */ |
| 1066 | if (signal) { |
| 1067 | RESTORE_LOCALE(saved_locale) |
| 1068 | return NULL; |
| 1069 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | /* We got an EOF, return a empty string. */ |
| 1072 | if (p == NULL) { |
| 1073 | p = PyMem_Malloc(1); |
| 1074 | if (p != NULL) |
| 1075 | *p = '\0'; |
| 1076 | RESTORE_LOCALE(saved_locale) |
| 1077 | return p; |
| 1078 | } |
| 1079 | |
| 1080 | /* we have a valid line */ |
| 1081 | n = strlen(p); |
| 1082 | if (n > 0) { |
| 1083 | char *line; |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 1084 | int length = _py_get_history_length(); |
| 1085 | if (length > 0) |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1086 | #ifdef __APPLE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | if (using_libedit_emulation) { |
| 1088 | /* |
| 1089 | * Libedit's emulation uses 0-based indexes, |
| 1090 | * the real readline uses 1-based indexes. |
| 1091 | */ |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 1092 | line = history_get(length - 1)->line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | } else |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1094 | #endif /* __APPLE__ */ |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 1095 | line = history_get(length)->line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | else |
| 1097 | line = ""; |
| 1098 | if (strcmp(p, line)) |
| 1099 | add_history(p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | } |
| 1101 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and |
| 1102 | release the original. */ |
| 1103 | q = p; |
| 1104 | p = PyMem_Malloc(n+2); |
| 1105 | if (p != NULL) { |
| 1106 | strncpy(p, q, n); |
| 1107 | p[n] = '\n'; |
| 1108 | p[n+1] = '\0'; |
| 1109 | } |
| 1110 | free(q); |
| 1111 | RESTORE_LOCALE(saved_locale) |
| 1112 | return p; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1115 | |
| 1116 | /* Initialize the module */ |
| 1117 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1118 | PyDoc_STRVAR(doc_module, |
| 1119 | "Importing this module enables command line editing using GNU readline."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1120 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1121 | #ifdef __APPLE__ |
| 1122 | PyDoc_STRVAR(doc_module_le, |
| 1123 | "Importing this module enables command line editing using libedit readline."); |
| 1124 | #endif /* __APPLE__ */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1125 | |
| 1126 | static struct PyModuleDef readlinemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | PyModuleDef_HEAD_INIT, |
| 1128 | "readline", |
| 1129 | doc_module, |
| 1130 | -1, |
| 1131 | readline_methods, |
| 1132 | NULL, |
| 1133 | NULL, |
| 1134 | NULL, |
| 1135 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1136 | }; |
| 1137 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1138 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1139 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1140 | PyInit_readline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | PyObject *m; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1143 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1144 | #ifdef __APPLE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) { |
| 1146 | using_libedit_emulation = 1; |
| 1147 | } |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | if (using_libedit_emulation) |
| 1150 | readlinemodule.m_doc = doc_module_le; |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1151 | |
| 1152 | #endif /* __APPLE__ */ |
| 1153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | m = PyModule_Create(&readlinemodule); |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | if (m == NULL) |
| 1157 | return NULL; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 1158 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1159 | |
| 1160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | PyOS_ReadlineFunctionPointer = call_readline; |
| 1162 | setup_readline(); |
| 1163 | return m; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1164 | } |