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