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" |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 9 | #include <stddef.h> |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 10 | #include <setjmp.h> |
| 11 | #include <signal.h> |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 13 | #include <sys/time.h> |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 14 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 15 | #if defined(HAVE_SETLOCALE) |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 16 | /* GNU readline() mistakenly sets the LC_CTYPE locale. |
| 17 | * This is evil. Only the user or the app's main() should do this! |
| 18 | * We must save and restore the locale around the rl_initialize() call. |
| 19 | */ |
| 20 | #define SAVE_LOCALE |
| 21 | #include <locale.h> |
| 22 | #endif |
| 23 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 24 | #ifdef SAVE_LOCALE |
| 25 | # define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); } |
| 26 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | # define RESTORE_LOCALE(sl) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 30 | /* GNU readline definitions */ |
Guido van Rossum | b0e51b2 | 2001-04-13 18:14:27 +0000 | [diff] [blame] | 31 | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 32 | #include <readline/readline.h> |
| 33 | #include <readline/history.h> |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 35 | #ifdef HAVE_RL_COMPLETION_MATCHES |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 36 | #define completion_matches(x, y) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | rl_completion_matches((x), ((rl_compentry_func_t *)(y))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 38 | #else |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 39 | #if defined(_RL_FUNCTION_TYPEDEF) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 40 | extern char **completion_matches(char *, rl_compentry_func_t *); |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 41 | #else |
Ronald Oussoren | 25696bb | 2010-02-11 13:15:00 +0000 | [diff] [blame] | 42 | |
| 43 | #if !defined(__APPLE__) |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 44 | extern char **completion_matches(char *, CPFunction *); |
| 45 | #endif |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 46 | #endif |
Ronald Oussoren | 25696bb | 2010-02-11 13:15:00 +0000 | [diff] [blame] | 47 | #endif |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 48 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 49 | #ifdef __APPLE__ |
| 50 | /* |
| 51 | * It is possible to link the readline module to the readline |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | * emulation library of editline/libedit. |
| 53 | * |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 54 | * On OSX this emulation library is not 100% API compatible |
| 55 | * with the "real" readline and cannot be detected at compile-time, |
| 56 | * hence we use a runtime check to detect if we're using libedit |
| 57 | * |
Ned Deily | 5d4121a | 2013-10-12 15:47:58 -0700 | [diff] [blame] | 58 | * Currently there is one known API incompatibility: |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 59 | * - 'get_history' has a 1-based index with GNU readline, and a 0-based |
Ned Deily | 5d4121a | 2013-10-12 15:47:58 -0700 | [diff] [blame] | 60 | * index with older versions of libedit's emulation. |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 61 | * - Note that replace_history and remove_history use a 0-based index |
Ned Deily | 5d4121a | 2013-10-12 15:47:58 -0700 | [diff] [blame] | 62 | * with both implementations. |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 63 | */ |
| 64 | static int using_libedit_emulation = 0; |
| 65 | static const char libedit_version_tag[] = "EditLine wrapper"; |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 66 | |
| 67 | static int libedit_history_start = 0; |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 68 | #endif /* __APPLE__ */ |
| 69 | |
Georg Brandl | 646fdd6 | 2010-10-18 07:27:55 +0000 | [diff] [blame] | 70 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 71 | static void |
| 72 | on_completion_display_matches_hook(char **matches, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | int num_matches, int max_length); |
Georg Brandl | 646fdd6 | 2010-10-18 07:27:55 +0000 | [diff] [blame] | 74 | #endif |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 75 | |
Antoine Pitrou | a7f7deb | 2013-05-06 21:51:03 +0200 | [diff] [blame] | 76 | /* Memory allocated for rl_completer_word_break_characters |
| 77 | (see issue #17289 for the motivation). */ |
| 78 | static char *completer_word_break_characters; |
| 79 | |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 80 | typedef struct { |
Martin Panter | f6e9f47 | 2016-03-22 02:19:29 +0000 | [diff] [blame] | 81 | /* Specify hook functions in Python */ |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 82 | PyObject *completion_display_matches_hook; |
| 83 | PyObject *startup_hook; |
| 84 | PyObject *pre_input_hook; |
Martin Panter | f6e9f47 | 2016-03-22 02:19:29 +0000 | [diff] [blame] | 85 | |
| 86 | PyObject *completer; /* Specify a word completer in Python */ |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 87 | PyObject *begidx; |
| 88 | PyObject *endidx; |
| 89 | } readlinestate; |
| 90 | |
| 91 | |
| 92 | #define readline_state(o) ((readlinestate *)PyModule_GetState(o)) |
| 93 | |
| 94 | static int |
| 95 | readline_clear(PyObject *m) |
| 96 | { |
| 97 | readlinestate *state = readline_state(m); |
| 98 | Py_CLEAR(state->completion_display_matches_hook); |
| 99 | Py_CLEAR(state->startup_hook); |
| 100 | Py_CLEAR(state->pre_input_hook); |
| 101 | Py_CLEAR(state->completer); |
| 102 | Py_CLEAR(state->begidx); |
| 103 | Py_CLEAR(state->endidx); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | readline_traverse(PyObject *m, visitproc visit, void *arg) |
| 109 | { |
| 110 | readlinestate *state = readline_state(m); |
| 111 | Py_VISIT(state->completion_display_matches_hook); |
| 112 | Py_VISIT(state->startup_hook); |
| 113 | Py_VISIT(state->pre_input_hook); |
| 114 | Py_VISIT(state->completer); |
| 115 | Py_VISIT(state->begidx); |
| 116 | Py_VISIT(state->endidx); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | readline_free(void *m) |
| 122 | { |
| 123 | readline_clear((PyObject *)m); |
| 124 | } |
| 125 | |
| 126 | static PyModuleDef readlinemodule; |
| 127 | |
| 128 | #define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule))) |
| 129 | |
| 130 | |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 131 | /* Convert to/from multibyte C strings */ |
| 132 | |
| 133 | static PyObject * |
| 134 | encode(PyObject *b) |
| 135 | { |
| 136 | return PyUnicode_EncodeLocale(b, "surrogateescape"); |
| 137 | } |
| 138 | |
| 139 | static PyObject * |
| 140 | decode(const char *s) |
| 141 | { |
| 142 | return PyUnicode_DecodeLocale(s, "surrogateescape"); |
| 143 | } |
| 144 | |
| 145 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 146 | /* Exported function to send one line to readline's init file parser */ |
| 147 | |
| 148 | static PyObject * |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 149 | parse_and_bind(PyObject *self, PyObject *string) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 150 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 151 | char *copy; |
| 152 | PyObject *encoded = encode(string); |
| 153 | if (encoded == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | return NULL; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 155 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | /* Make a copy -- rl_parse_and_bind() modifies its argument */ |
| 157 | /* Bernard Herzog */ |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 158 | copy = PyMem_Malloc(1 + PyBytes_GET_SIZE(encoded)); |
| 159 | if (copy == NULL) { |
| 160 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | return PyErr_NoMemory(); |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 162 | } |
| 163 | strcpy(copy, PyBytes_AS_STRING(encoded)); |
| 164 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | rl_parse_and_bind(copy); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 166 | PyMem_Free(copy); /* Free the copy */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | Py_RETURN_NONE; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 170 | PyDoc_STRVAR(doc_parse_and_bind, |
| 171 | "parse_and_bind(string) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 172 | Execute the init line provided in the string argument."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 173 | |
| 174 | |
| 175 | /* Exported function to parse a readline init file */ |
| 176 | |
| 177 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 178 | read_init_file(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 179 | { |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 180 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 181 | if (!PyArg_ParseTuple(args, "|O:read_init_file", &filename_obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | return NULL; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 183 | if (filename_obj != Py_None) { |
| 184 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 185 | return NULL; |
| 186 | errno = rl_read_init_file(PyBytes_AsString(filename_bytes)); |
| 187 | Py_DECREF(filename_bytes); |
| 188 | } else |
| 189 | errno = rl_read_init_file(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | if (errno) |
| 191 | return PyErr_SetFromErrno(PyExc_IOError); |
| 192 | Py_RETURN_NONE; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 195 | PyDoc_STRVAR(doc_read_init_file, |
| 196 | "read_init_file([filename]) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 197 | Execute a readline initialization file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 198 | The default filename is the last filename used."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 199 | |
| 200 | |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 201 | /* Exported function to load a readline history file */ |
| 202 | |
| 203 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 204 | read_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 205 | { |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 206 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 207 | if (!PyArg_ParseTuple(args, "|O:read_history_file", &filename_obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | return NULL; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 209 | if (filename_obj != Py_None) { |
| 210 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 211 | return NULL; |
| 212 | errno = read_history(PyBytes_AsString(filename_bytes)); |
| 213 | Py_DECREF(filename_bytes); |
| 214 | } else |
| 215 | errno = read_history(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | if (errno) |
| 217 | return PyErr_SetFromErrno(PyExc_IOError); |
| 218 | Py_RETURN_NONE; |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Hye-Shik Chang | 7a8173a | 2004-11-25 04:04:20 +0000 | [diff] [blame] | 221 | 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] | 222 | PyDoc_STRVAR(doc_read_history_file, |
| 223 | "read_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 224 | Load a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 225 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 226 | |
| 227 | |
| 228 | /* Exported function to save a readline history file */ |
| 229 | |
| 230 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 231 | write_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 232 | { |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 233 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 234 | char *filename; |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 235 | int err; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 236 | if (!PyArg_ParseTuple(args, "|O:write_history_file", &filename_obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | return NULL; |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 238 | if (filename_obj != Py_None) { |
| 239 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 240 | return NULL; |
| 241 | filename = PyBytes_AsString(filename_bytes); |
| 242 | } else { |
| 243 | filename_bytes = NULL; |
| 244 | filename = NULL; |
| 245 | } |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 246 | errno = err = write_history(filename); |
| 247 | if (!err && _history_length >= 0) |
Victor Stinner | 19e65a3 | 2010-06-11 22:27:14 +0000 | [diff] [blame] | 248 | history_truncate_file(filename, _history_length); |
| 249 | Py_XDECREF(filename_bytes); |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 250 | errno = err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | if (errno) |
| 252 | return PyErr_SetFromErrno(PyExc_IOError); |
| 253 | Py_RETURN_NONE; |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 256 | PyDoc_STRVAR(doc_write_history_file, |
| 257 | "write_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 258 | Save a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 259 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 260 | |
| 261 | |
Benjamin Peterson | d1e22ba | 2014-11-26 14:35:12 -0600 | [diff] [blame] | 262 | #ifdef HAVE_RL_APPEND_HISTORY |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 263 | /* Exported function to save part of a readline history file */ |
| 264 | |
| 265 | static PyObject * |
| 266 | append_history_file(PyObject *self, PyObject *args) |
| 267 | { |
| 268 | int nelements; |
| 269 | PyObject *filename_obj = Py_None, *filename_bytes; |
| 270 | char *filename; |
| 271 | int err; |
| 272 | if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj)) |
| 273 | return NULL; |
| 274 | if (filename_obj != Py_None) { |
| 275 | if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) |
| 276 | return NULL; |
| 277 | filename = PyBytes_AsString(filename_bytes); |
| 278 | } else { |
| 279 | filename_bytes = NULL; |
| 280 | filename = NULL; |
| 281 | } |
| 282 | errno = err = append_history(nelements, filename); |
| 283 | if (!err && _history_length >= 0) |
| 284 | history_truncate_file(filename, _history_length); |
| 285 | Py_XDECREF(filename_bytes); |
| 286 | errno = err; |
| 287 | if (errno) |
| 288 | return PyErr_SetFromErrno(PyExc_IOError); |
| 289 | Py_RETURN_NONE; |
| 290 | } |
| 291 | |
| 292 | PyDoc_STRVAR(doc_append_history_file, |
| 293 | "append_history_file(nelements[, filename]) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 294 | Append the last nelements items of the history list to file.\n\ |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 295 | The default filename is ~/.history."); |
Benjamin Peterson | d1e22ba | 2014-11-26 14:35:12 -0600 | [diff] [blame] | 296 | #endif |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 297 | |
| 298 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 299 | /* Set history length */ |
| 300 | |
| 301 | static PyObject* |
| 302 | set_history_length(PyObject *self, PyObject *args) |
| 303 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | int length = _history_length; |
| 305 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) |
| 306 | return NULL; |
| 307 | _history_length = length; |
| 308 | Py_RETURN_NONE; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 311 | PyDoc_STRVAR(set_history_length_doc, |
| 312 | "set_history_length(length) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 313 | set the maximal number of lines which will be written to\n\ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 314 | 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] | 315 | history truncation."); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 316 | |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 317 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 318 | /* Get history length */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 319 | |
| 320 | static PyObject* |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 321 | get_history_length(PyObject *self, PyObject *noarg) |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | return PyLong_FromLong(_history_length); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 326 | PyDoc_STRVAR(get_history_length_doc, |
| 327 | "get_history_length() -> int\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 328 | return the maximum number of lines that will be written to\n\ |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 329 | the history file."); |
| 330 | |
| 331 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 332 | /* Generic hook function setter */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 333 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 334 | static PyObject * |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 335 | set_hook(const char *funcname, PyObject **hook_var, PyObject *args) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | PyObject *function = Py_None; |
| 338 | char buf[80]; |
| 339 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); |
| 340 | if (!PyArg_ParseTuple(args, buf, &function)) |
| 341 | return NULL; |
| 342 | if (function == Py_None) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 343 | Py_CLEAR(*hook_var); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | } |
| 345 | else if (PyCallable_Check(function)) { |
| 346 | PyObject *tmp = *hook_var; |
| 347 | Py_INCREF(function); |
| 348 | *hook_var = function; |
| 349 | Py_XDECREF(tmp); |
| 350 | } |
| 351 | else { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 352 | PyErr_Format(PyExc_TypeError, |
| 353 | "set_%.50s(func): argument not callable", |
| 354 | funcname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | return NULL; |
| 356 | } |
| 357 | Py_RETURN_NONE; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 360 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 361 | static PyObject * |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 362 | set_completion_display_matches_hook(PyObject *self, PyObject *args) |
| 363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | PyObject *result = set_hook("completion_display_matches_hook", |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 365 | &readlinestate_global->completion_display_matches_hook, args); |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 366 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | /* We cannot set this hook globally, since it replaces the |
| 368 | default completion display. */ |
| 369 | rl_completion_display_matches_hook = |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 370 | readlinestate_global->completion_display_matches_hook ? |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 371 | #if defined(_RL_FUNCTION_TYPEDEF) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | (rl_compdisp_func_t *)on_completion_display_matches_hook : 0; |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 373 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | (VFunction *)on_completion_display_matches_hook : 0; |
Martin v. Löwis | b37509b | 2008-11-04 20:45:29 +0000 | [diff] [blame] | 375 | #endif |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 376 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | return result; |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 378 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | PyDoc_STRVAR(doc_set_completion_display_matches_hook, |
| 382 | "set_completion_display_matches_hook([function]) -> None\n\ |
| 383 | Set or remove the completion display function.\n\ |
| 384 | The function is called as\n\ |
| 385 | function(substitution, [matches], longest_match_length)\n\ |
| 386 | once each time matches need to be displayed."); |
| 387 | |
| 388 | static PyObject * |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 389 | set_startup_hook(PyObject *self, PyObject *args) |
| 390 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 391 | return set_hook("startup_hook", &readlinestate_global->startup_hook, args); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 394 | PyDoc_STRVAR(doc_set_startup_hook, |
| 395 | "set_startup_hook([function]) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 396 | Set or remove the function invoked by the rl_startup_hook callback.\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 397 | The function is called with no arguments just\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 398 | before readline prints the first prompt."); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 399 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 400 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 401 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 402 | |
| 403 | /* Set pre-input hook */ |
| 404 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 405 | static PyObject * |
| 406 | set_pre_input_hook(PyObject *self, PyObject *args) |
| 407 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 408 | return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 411 | PyDoc_STRVAR(doc_set_pre_input_hook, |
| 412 | "set_pre_input_hook([function]) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 413 | Set or remove the function invoked by the rl_pre_input_hook callback.\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 414 | The function is called with no arguments after the first prompt\n\ |
| 415 | 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] | 416 | characters."); |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 417 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 418 | #endif |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 420 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 421 | /* Get the completion type for the scope of the tab-completion */ |
| 422 | static PyObject * |
| 423 | get_completion_type(PyObject *self, PyObject *noarg) |
| 424 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 425 | return PyLong_FromLong(rl_completion_type); |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | PyDoc_STRVAR(doc_get_completion_type, |
| 429 | "get_completion_type() -> int\n\ |
| 430 | Get the type of completion being attempted."); |
| 431 | |
| 432 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 433 | /* Get the beginning index for the scope of the tab-completion */ |
| 434 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 435 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 436 | get_begidx(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 437 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 438 | Py_INCREF(readlinestate_global->begidx); |
| 439 | return readlinestate_global->begidx; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 442 | PyDoc_STRVAR(doc_get_begidx, |
| 443 | "get_begidx() -> int\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 444 | get the beginning index of the completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 445 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 446 | |
| 447 | /* Get the ending index for the scope of the tab-completion */ |
| 448 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 449 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 450 | get_endidx(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 451 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 452 | Py_INCREF(readlinestate_global->endidx); |
| 453 | return readlinestate_global->endidx; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 456 | PyDoc_STRVAR(doc_get_endidx, |
| 457 | "get_endidx() -> int\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 458 | get the ending index of the completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 459 | |
| 460 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 461 | /* Set the tab-completion word-delimiters that readline uses */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 462 | |
| 463 | static PyObject * |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 464 | set_completer_delims(PyObject *self, PyObject *string) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | char *break_chars; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 467 | PyObject *encoded = encode(string); |
| 468 | if (encoded == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | return NULL; |
| 470 | } |
Antoine Pitrou | a7f7deb | 2013-05-06 21:51:03 +0200 | [diff] [blame] | 471 | /* Keep a reference to the allocated memory in the module state in case |
| 472 | some other module modifies rl_completer_word_break_characters |
| 473 | (see issue #17289). */ |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 474 | break_chars = strdup(PyBytes_AS_STRING(encoded)); |
| 475 | Py_DECREF(encoded); |
Serhiy Storchaka | 1138439 | 2015-09-27 22:34:59 +0300 | [diff] [blame] | 476 | if (break_chars) { |
| 477 | free(completer_word_break_characters); |
| 478 | completer_word_break_characters = break_chars; |
| 479 | rl_completer_word_break_characters = break_chars; |
Antoine Pitrou | a7f7deb | 2013-05-06 21:51:03 +0200 | [diff] [blame] | 480 | Py_RETURN_NONE; |
| 481 | } |
| 482 | else |
| 483 | return PyErr_NoMemory(); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 486 | PyDoc_STRVAR(doc_set_completer_delims, |
| 487 | "set_completer_delims(string) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 488 | set the word delimiters for completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 489 | |
Mark Dickinson | 29b238e | 2010-08-03 16:08:16 +0000 | [diff] [blame] | 490 | /* _py_free_history_entry: Utility function to free a history entry. */ |
| 491 | |
| 492 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500 |
| 493 | |
| 494 | /* Readline version >= 5.0 introduced a timestamp field into the history entry |
| 495 | structure; this needs to be freed to avoid a memory leak. This version of |
| 496 | readline also introduced the handy 'free_history_entry' function, which |
| 497 | takes care of the timestamp. */ |
| 498 | |
| 499 | static void |
| 500 | _py_free_history_entry(HIST_ENTRY *entry) |
| 501 | { |
| 502 | histdata_t data = free_history_entry(entry); |
| 503 | free(data); |
| 504 | } |
| 505 | |
| 506 | #else |
| 507 | |
| 508 | /* No free_history_entry function; free everything manually. */ |
| 509 | |
| 510 | static void |
| 511 | _py_free_history_entry(HIST_ENTRY *entry) |
| 512 | { |
| 513 | if (entry->line) |
| 514 | free((void *)entry->line); |
| 515 | if (entry->data) |
| 516 | free(entry->data); |
| 517 | free(entry); |
| 518 | } |
| 519 | |
| 520 | #endif |
| 521 | |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 522 | static PyObject * |
| 523 | py_remove_history(PyObject *self, PyObject *args) |
| 524 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | int entry_number; |
| 526 | HIST_ENTRY *entry; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 527 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 528 | if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | return NULL; |
| 530 | if (entry_number < 0) { |
| 531 | PyErr_SetString(PyExc_ValueError, |
| 532 | "History index cannot be negative"); |
| 533 | return NULL; |
| 534 | } |
| 535 | entry = remove_history(entry_number); |
| 536 | if (!entry) { |
| 537 | PyErr_Format(PyExc_ValueError, |
| 538 | "No history item at position %d", |
| 539 | entry_number); |
| 540 | return NULL; |
| 541 | } |
| 542 | /* free memory allocated for the history entry */ |
Mark Dickinson | 29b238e | 2010-08-03 16:08:16 +0000 | [diff] [blame] | 543 | _py_free_history_entry(entry); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | Py_RETURN_NONE; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | PyDoc_STRVAR(doc_remove_history, |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 548 | "remove_history_item(pos) -> None\n\ |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 549 | remove history item given by its position"); |
| 550 | |
| 551 | static PyObject * |
| 552 | py_replace_history(PyObject *self, PyObject *args) |
| 553 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | int entry_number; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 555 | PyObject *line; |
| 556 | PyObject *encoded; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | HIST_ENTRY *old_entry; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 558 | |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 559 | if (!PyArg_ParseTuple(args, "iU:replace_history_item", &entry_number, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | &line)) { |
| 561 | return NULL; |
| 562 | } |
| 563 | if (entry_number < 0) { |
| 564 | PyErr_SetString(PyExc_ValueError, |
| 565 | "History index cannot be negative"); |
| 566 | return NULL; |
| 567 | } |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 568 | encoded = encode(line); |
| 569 | if (encoded == NULL) { |
| 570 | return NULL; |
| 571 | } |
| 572 | old_entry = replace_history_entry(entry_number, PyBytes_AS_STRING(encoded), (void *)NULL); |
| 573 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | if (!old_entry) { |
| 575 | PyErr_Format(PyExc_ValueError, |
| 576 | "No history item at position %d", |
| 577 | entry_number); |
| 578 | return NULL; |
| 579 | } |
| 580 | /* free memory allocated for the old history entry */ |
Mark Dickinson | 29b238e | 2010-08-03 16:08:16 +0000 | [diff] [blame] | 581 | _py_free_history_entry(old_entry); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | Py_RETURN_NONE; |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | PyDoc_STRVAR(doc_replace_history, |
Skip Montanaro | 6c06cd5 | 2004-08-16 16:15:13 +0000 | [diff] [blame] | 586 | "replace_history_item(pos, line) -> None\n\ |
Skip Montanaro | e506901 | 2004-08-15 14:32:06 +0000 | [diff] [blame] | 587 | replaces history item given by its position with contents of line"); |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 588 | |
| 589 | /* Add a line to the history buffer */ |
| 590 | |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 591 | static PyObject * |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 592 | py_add_history(PyObject *self, PyObject *string) |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 593 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 594 | PyObject *encoded = encode(string); |
| 595 | if (encoded == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | return NULL; |
| 597 | } |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 598 | add_history(PyBytes_AS_STRING(encoded)); |
| 599 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | Py_RETURN_NONE; |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 603 | PyDoc_STRVAR(doc_add_history, |
| 604 | "add_history(string) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 605 | add an item to the history buffer"); |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 606 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 607 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 608 | /* Get the tab-completion word-delimiters that readline uses */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 609 | |
| 610 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 611 | get_completer_delims(PyObject *self, PyObject *noarg) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 612 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 613 | return decode(rl_completer_word_break_characters); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 614 | } |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 616 | PyDoc_STRVAR(doc_get_completer_delims, |
| 617 | "get_completer_delims() -> string\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 618 | get the word delimiters for completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 619 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 620 | |
| 621 | /* Set the completer function */ |
| 622 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 623 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 624 | set_completer(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 625 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 626 | return set_hook("completer", &readlinestate_global->completer, args); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 629 | PyDoc_STRVAR(doc_set_completer, |
| 630 | "set_completer([function]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 631 | Set or remove the completer function.\n\ |
| 632 | The function is called as function(text, state),\n\ |
Fred Drake | 52d55a3 | 2001-08-01 21:44:14 +0000 | [diff] [blame] | 633 | 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] | 634 | It should return the next possible completion starting with 'text'."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 635 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 636 | |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 637 | static PyObject * |
Neal Norwitz | d9efdc5 | 2003-03-01 15:19:41 +0000 | [diff] [blame] | 638 | get_completer(PyObject *self, PyObject *noargs) |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 639 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 640 | if (readlinestate_global->completer == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | Py_RETURN_NONE; |
| 642 | } |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 643 | Py_INCREF(readlinestate_global->completer); |
| 644 | return readlinestate_global->completer; |
Michael W. Hudson | 796df15 | 2003-01-30 10:12:51 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | PyDoc_STRVAR(doc_get_completer, |
| 648 | "get_completer() -> function\n\ |
| 649 | \n\ |
| 650 | Returns current completer function."); |
| 651 | |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 652 | /* Private function to get current length of history. XXX It may be |
| 653 | * possible to replace this with a direct use of history_length instead, |
| 654 | * but it's not clear whether BSD's libedit keeps history_length up to date. |
| 655 | * See issue #8065.*/ |
| 656 | |
| 657 | static int |
| 658 | _py_get_history_length(void) |
| 659 | { |
| 660 | HISTORY_STATE *hist_st = history_get_history_state(); |
| 661 | int length = hist_st->length; |
| 662 | /* the history docs don't say so, but the address of hist_st changes each |
| 663 | time history_get_history_state is called which makes me think it's |
| 664 | freshly malloc'd memory... on the other hand, the address of the last |
| 665 | line stays the same as long as history isn't extended, so it appears to |
| 666 | be malloc'd but managed by the history package... */ |
| 667 | free(hist_st); |
| 668 | return length; |
| 669 | } |
| 670 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 671 | /* Exported function to get any element of history */ |
| 672 | |
| 673 | static PyObject * |
| 674 | get_history_item(PyObject *self, PyObject *args) |
| 675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | int idx = 0; |
| 677 | HIST_ENTRY *hist_ent; |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 678 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 679 | if (!PyArg_ParseTuple(args, "i:get_history_item", &idx)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | return NULL; |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 681 | #ifdef __APPLE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | if (using_libedit_emulation) { |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 683 | /* Older versions of libedit's readline emulation |
| 684 | * use 0-based indexes, while readline and newer |
| 685 | * versions of libedit use 1-based indexes. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | */ |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 687 | int length = _py_get_history_length(); |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 688 | |
| 689 | idx = idx - 1 + libedit_history_start; |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | /* |
| 692 | * Apple's readline emulation crashes when |
| 693 | * the index is out of range, therefore |
| 694 | * test for that and fail gracefully. |
| 695 | */ |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 696 | if (idx < (0 + libedit_history_start) |
| 697 | || idx >= (length + libedit_history_start)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | Py_RETURN_NONE; |
| 699 | } |
| 700 | } |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 701 | #endif /* __APPLE__ */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | if ((hist_ent = history_get(idx))) |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 703 | return decode(hist_ent->line); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | else { |
| 705 | Py_RETURN_NONE; |
| 706 | } |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 709 | PyDoc_STRVAR(doc_get_history_item, |
| 710 | "get_history_item() -> string\n\ |
| 711 | return the current contents of history item at index."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 712 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 713 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 714 | /* Exported function to get current length of history */ |
| 715 | |
| 716 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 717 | get_current_history_length(PyObject *self, PyObject *noarg) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 718 | { |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 719 | return PyLong_FromLong((long)_py_get_history_length()); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 722 | PyDoc_STRVAR(doc_get_current_history_length, |
| 723 | "get_current_history_length() -> integer\n\ |
| 724 | return the current (not the maximum) length of history."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 725 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 726 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 727 | /* Exported function to read the current line buffer */ |
| 728 | |
| 729 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 730 | get_line_buffer(PyObject *self, PyObject *noarg) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 731 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 732 | return decode(rl_line_buffer); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 735 | PyDoc_STRVAR(doc_get_line_buffer, |
| 736 | "get_line_buffer() -> string\n\ |
| 737 | return the current contents of the line buffer."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 738 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 739 | |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 740 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
| 741 | |
| 742 | /* Exported function to clear the current history */ |
| 743 | |
| 744 | static PyObject * |
| 745 | py_clear_history(PyObject *self, PyObject *noarg) |
| 746 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | clear_history(); |
| 748 | Py_RETURN_NONE; |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | PyDoc_STRVAR(doc_clear_history, |
| 752 | "clear_history() -> None\n\ |
| 753 | Clear the current readline history."); |
| 754 | #endif |
| 755 | |
| 756 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 757 | /* Exported function to insert text into the line buffer */ |
| 758 | |
| 759 | static PyObject * |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 760 | insert_text(PyObject *self, PyObject *string) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 761 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 762 | PyObject *encoded = encode(string); |
| 763 | if (encoded == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | return NULL; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 765 | } |
| 766 | rl_insert_text(PyBytes_AS_STRING(encoded)); |
| 767 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | Py_RETURN_NONE; |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 771 | PyDoc_STRVAR(doc_insert_text, |
| 772 | "insert_text(string) -> None\n\ |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 773 | Insert text into the line buffer at the cursor position."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 774 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 775 | |
| 776 | /* Redisplay the line buffer */ |
| 777 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 778 | static PyObject * |
Michael W. Hudson | 0e986a3 | 2003-01-30 14:17:16 +0000 | [diff] [blame] | 779 | redisplay(PyObject *self, PyObject *noarg) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 780 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | rl_redisplay(); |
| 782 | Py_RETURN_NONE; |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 785 | PyDoc_STRVAR(doc_redisplay, |
| 786 | "redisplay() -> None\n\ |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 787 | 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] | 788 | contents of the line buffer."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 789 | |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 790 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 791 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 792 | |
| 793 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 794 | { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 795 | {"parse_and_bind", parse_and_bind, METH_O, doc_parse_and_bind}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer}, |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 797 | {"insert_text", insert_text, METH_O, doc_insert_text}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | {"redisplay", redisplay, METH_NOARGS, doc_redisplay}, |
| 799 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, |
| 800 | {"read_history_file", read_history_file, |
| 801 | METH_VARARGS, doc_read_history_file}, |
| 802 | {"write_history_file", write_history_file, |
| 803 | METH_VARARGS, doc_write_history_file}, |
Benjamin Peterson | d1e22ba | 2014-11-26 14:35:12 -0600 | [diff] [blame] | 804 | #ifdef HAVE_RL_APPEND_HISTORY |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 805 | {"append_history_file", append_history_file, |
| 806 | METH_VARARGS, doc_append_history_file}, |
Ned Deily | 8007cbc | 2014-11-26 13:02:33 -0800 | [diff] [blame] | 807 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | {"get_history_item", get_history_item, |
| 809 | METH_VARARGS, doc_get_history_item}, |
| 810 | {"get_current_history_length", (PyCFunction)get_current_history_length, |
| 811 | METH_NOARGS, doc_get_current_history_length}, |
| 812 | {"set_history_length", set_history_length, |
| 813 | METH_VARARGS, set_history_length_doc}, |
| 814 | {"get_history_length", get_history_length, |
| 815 | METH_NOARGS, get_history_length_doc}, |
| 816 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer}, |
| 817 | {"get_completer", get_completer, METH_NOARGS, doc_get_completer}, |
| 818 | {"get_completion_type", get_completion_type, |
| 819 | METH_NOARGS, doc_get_completion_type}, |
| 820 | {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx}, |
| 821 | {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx}, |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | {"set_completer_delims", set_completer_delims, |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 824 | METH_O, doc_set_completer_delims}, |
| 825 | {"add_history", py_add_history, METH_O, doc_add_history}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history}, |
| 827 | {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history}, |
| 828 | {"get_completer_delims", get_completer_delims, |
| 829 | METH_NOARGS, doc_get_completer_delims}, |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 830 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 831 | {"set_completion_display_matches_hook", set_completion_display_matches_hook, |
| 832 | METH_VARARGS, doc_set_completion_display_matches_hook}, |
| 833 | {"set_startup_hook", set_startup_hook, |
| 834 | METH_VARARGS, doc_set_startup_hook}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 835 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | {"set_pre_input_hook", set_pre_input_hook, |
| 837 | METH_VARARGS, doc_set_pre_input_hook}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 838 | #endif |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 839 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history}, |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 841 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 843 | }; |
| 844 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 845 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 846 | /* C function to call the Python hooks. */ |
| 847 | |
| 848 | static int |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 849 | on_hook(PyObject *func) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 850 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | int result = 0; |
| 852 | if (func != NULL) { |
| 853 | PyObject *r; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | r = PyObject_CallFunction(func, NULL); |
| 855 | if (r == NULL) |
| 856 | goto error; |
| 857 | if (r == Py_None) |
| 858 | result = 0; |
| 859 | else { |
| 860 | result = PyLong_AsLong(r); |
| 861 | if (result == -1 && PyErr_Occurred()) |
| 862 | goto error; |
| 863 | } |
| 864 | Py_DECREF(r); |
| 865 | goto done; |
| 866 | error: |
| 867 | PyErr_Clear(); |
| 868 | Py_XDECREF(r); |
| 869 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | return result; |
| 871 | } |
| 872 | return result; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static int |
Ned Deily | 7b9ddea | 2014-02-05 16:53:10 -0800 | [diff] [blame] | 876 | #if defined(_RL_FUNCTION_TYPEDEF) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 877 | on_startup_hook(void) |
Ned Deily | 7b9ddea | 2014-02-05 16:53:10 -0800 | [diff] [blame] | 878 | #else |
| 879 | on_startup_hook() |
| 880 | #endif |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 881 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 882 | int r; |
| 883 | #ifdef WITH_THREAD |
| 884 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
| 885 | #endif |
| 886 | r = on_hook(readlinestate_global->startup_hook); |
| 887 | #ifdef WITH_THREAD |
| 888 | PyGILState_Release(gilstate); |
| 889 | #endif |
| 890 | return r; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 894 | static int |
Ned Deily | 7b9ddea | 2014-02-05 16:53:10 -0800 | [diff] [blame] | 895 | #if defined(_RL_FUNCTION_TYPEDEF) |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 896 | on_pre_input_hook(void) |
Ned Deily | 7b9ddea | 2014-02-05 16:53:10 -0800 | [diff] [blame] | 897 | #else |
| 898 | on_pre_input_hook() |
| 899 | #endif |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 900 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 901 | int r; |
| 902 | #ifdef WITH_THREAD |
| 903 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
| 904 | #endif |
| 905 | r = on_hook(readlinestate_global->pre_input_hook); |
| 906 | #ifdef WITH_THREAD |
| 907 | PyGILState_Release(gilstate); |
| 908 | #endif |
| 909 | return r; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 910 | } |
| 911 | #endif |
| 912 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 913 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 914 | /* C function to call the Python completion_display_matches */ |
| 915 | |
Georg Brandl | 646fdd6 | 2010-10-18 07:27:55 +0000 | [diff] [blame] | 916 | #ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 917 | static void |
| 918 | on_completion_display_matches_hook(char **matches, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | int num_matches, int max_length) |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 920 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | int i; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 922 | PyObject *sub, *m=NULL, *s=NULL, *r=NULL; |
Christian Heimes | aec75c3 | 2007-11-11 22:42:36 +0000 | [diff] [blame] | 923 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 925 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | m = PyList_New(num_matches); |
| 927 | if (m == NULL) |
| 928 | goto error; |
| 929 | for (i = 0; i < num_matches; i++) { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 930 | s = decode(matches[i+1]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | if (s == NULL) |
| 932 | goto error; |
| 933 | if (PyList_SetItem(m, i, s) == -1) |
| 934 | goto error; |
| 935 | } |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 936 | sub = decode(matches[0]); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 937 | r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook, |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 938 | "NNi", sub, m, max_length); |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 939 | |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 940 | m=NULL; |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | if (r == NULL || |
| 943 | (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) { |
| 944 | goto error; |
| 945 | } |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 946 | Py_CLEAR(r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | |
| 948 | if (0) { |
| 949 | error: |
| 950 | PyErr_Clear(); |
| 951 | Py_XDECREF(m); |
| 952 | Py_XDECREF(r); |
| 953 | } |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 954 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | PyGILState_Release(gilstate); |
Christian Heimes | 32fbe59 | 2007-11-12 15:01:33 +0000 | [diff] [blame] | 956 | #endif |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Senthil Kumaran | 95c0700 | 2010-11-04 03:51:05 +0000 | [diff] [blame] | 959 | #endif |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 960 | |
Martin Panter | 5dbbf1a | 2016-04-03 02:54:58 +0000 | [diff] [blame] | 961 | #ifdef HAVE_RL_RESIZE_TERMINAL |
| 962 | static volatile sig_atomic_t sigwinch_received; |
Martin Panter | 28f35b2 | 2016-04-03 08:00:49 +0000 | [diff] [blame] | 963 | static PyOS_sighandler_t sigwinch_ohandler; |
Martin Panter | 5dbbf1a | 2016-04-03 02:54:58 +0000 | [diff] [blame] | 964 | |
| 965 | static void |
| 966 | readline_sigwinch_handler(int signum) |
| 967 | { |
| 968 | sigwinch_received = 1; |
| 969 | if (sigwinch_ohandler && |
| 970 | sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL) |
| 971 | sigwinch_ohandler(signum); |
| 972 | |
| 973 | #ifndef HAVE_SIGACTION |
| 974 | /* If the handler was installed with signal() rather than sigaction(), |
| 975 | we need to reinstall it. */ |
| 976 | PyOS_setsig(SIGWINCH, readline_sigwinch_handler); |
| 977 | #endif |
| 978 | } |
| 979 | #endif |
| 980 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 981 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 982 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 983 | static char * |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 984 | on_completion(const char *text, int state) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 985 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | char *result = NULL; |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 987 | if (readlinestate_global->completer != NULL) { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 988 | PyObject *r = NULL, *t; |
Christian Heimes | aec75c3 | 2007-11-11 22:42:36 +0000 | [diff] [blame] | 989 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 991 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | rl_attempted_completion_over = 1; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 993 | t = decode(text); |
| 994 | r = PyObject_CallFunction(readlinestate_global->completer, "Ni", t, state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | if (r == NULL) |
| 996 | goto error; |
| 997 | if (r == Py_None) { |
| 998 | result = NULL; |
| 999 | } |
| 1000 | else { |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 1001 | PyObject *encoded = encode(r); |
| 1002 | if (encoded == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | goto error; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 1004 | result = strdup(PyBytes_AS_STRING(encoded)); |
| 1005 | Py_DECREF(encoded); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | } |
| 1007 | Py_DECREF(r); |
| 1008 | goto done; |
| 1009 | error: |
| 1010 | PyErr_Clear(); |
| 1011 | Py_XDECREF(r); |
| 1012 | done: |
Christian Heimes | aec75c3 | 2007-11-11 22:42:36 +0000 | [diff] [blame] | 1013 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | PyGILState_Release(gilstate); |
Michael W. Hudson | da6242c | 2005-03-30 11:21:53 +0000 | [diff] [blame] | 1015 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | return result; |
| 1017 | } |
| 1018 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1021 | |
Guido van Rossum | 6d0d365 | 2003-01-07 20:34:19 +0000 | [diff] [blame] | 1022 | /* A more flexible constructor that saves the "begidx" and "endidx" |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 1023 | * before calling the normal completer */ |
| 1024 | |
Neal Norwitz | c355f0c | 2003-02-21 00:30:18 +0000 | [diff] [blame] | 1025 | static char ** |
Benjamin Peterson | f0b463a | 2014-01-24 11:44:16 -0500 | [diff] [blame] | 1026 | flex_complete(const char *text, int start, int end) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 1027 | { |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1028 | char **result; |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 1029 | char saved; |
| 1030 | size_t start_size, end_size; |
| 1031 | wchar_t *s; |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1032 | #ifdef WITH_THREAD |
| 1033 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
| 1034 | #endif |
Antoine Pitrou | dc0900b | 2009-10-19 18:22:37 +0000 | [diff] [blame] | 1035 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | rl_completion_append_character ='\0'; |
Antoine Pitrou | d513177 | 2009-10-26 19:22:14 +0000 | [diff] [blame] | 1037 | #endif |
| 1038 | #ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | rl_completion_suppress_append = 0; |
Antoine Pitrou | dc0900b | 2009-10-19 18:22:37 +0000 | [diff] [blame] | 1040 | #endif |
Martin Panter | f00c49d | 2016-06-14 01:16:16 +0000 | [diff] [blame] | 1041 | |
| 1042 | saved = rl_line_buffer[start]; |
| 1043 | rl_line_buffer[start] = 0; |
| 1044 | s = Py_DecodeLocale(rl_line_buffer, &start_size); |
| 1045 | rl_line_buffer[start] = saved; |
| 1046 | if (s == NULL) { |
| 1047 | goto done; |
| 1048 | } |
| 1049 | PyMem_RawFree(s); |
| 1050 | saved = rl_line_buffer[end]; |
| 1051 | rl_line_buffer[end] = 0; |
| 1052 | s = Py_DecodeLocale(rl_line_buffer + start, &end_size); |
| 1053 | rl_line_buffer[end] = saved; |
| 1054 | if (s == NULL) { |
| 1055 | goto done; |
| 1056 | } |
| 1057 | PyMem_RawFree(s); |
| 1058 | start = (int)start_size; |
| 1059 | end = start + (int)end_size; |
| 1060 | |
| 1061 | done: |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1062 | Py_XDECREF(readlinestate_global->begidx); |
| 1063 | Py_XDECREF(readlinestate_global->endidx); |
| 1064 | readlinestate_global->begidx = PyLong_FromLong((long) start); |
| 1065 | readlinestate_global->endidx = PyLong_FromLong((long) end); |
Benjamin Peterson | 1bb0c0b | 2016-09-05 18:26:19 -0700 | [diff] [blame] | 1066 | result = completion_matches((char *)text, *on_completion); |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1067 | #ifdef WITH_THREAD |
| 1068 | PyGILState_Release(gilstate); |
| 1069 | #endif |
| 1070 | return result; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Guido van Rossum | 05ac449 | 2003-01-07 20:04:12 +0000 | [diff] [blame] | 1073 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1074 | /* Helper to initialize GNU readline properly. */ |
| 1075 | |
| 1076 | static void |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1077 | setup_readline(readlinestate *mod_state) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1078 | { |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +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"); |
Guido van Rossum | 60c8a3a | 2002-10-09 21:27:33 +0000 | [diff] [blame] | 1083 | #endif |
| 1084 | |
R. David Murray | 52d1b4e | 2010-12-18 03:48:32 +0000 | [diff] [blame] | 1085 | #ifdef __APPLE__ |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1086 | /* the libedit readline emulation resets key bindings etc |
R. David Murray | 52d1b4e | 2010-12-18 03:48:32 +0000 | [diff] [blame] | 1087 | * when calling rl_initialize. So call it upfront |
| 1088 | */ |
| 1089 | if (using_libedit_emulation) |
| 1090 | rl_initialize(); |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 1091 | |
| 1092 | /* Detect if libedit's readline emulation uses 0-based |
| 1093 | * indexing or 1-based indexing. |
| 1094 | */ |
| 1095 | add_history("1"); |
| 1096 | if (history_get(1) == NULL) { |
| 1097 | libedit_history_start = 0; |
| 1098 | } else { |
| 1099 | libedit_history_start = 1; |
| 1100 | } |
| 1101 | clear_history(); |
R. David Murray | 52d1b4e | 2010-12-18 03:48:32 +0000 | [diff] [blame] | 1102 | #endif /* __APPLE__ */ |
| 1103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | using_history(); |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 1105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | rl_readline_name = "python"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1107 | /* Force rebind of TAB to insert-tab */ |
| 1108 | rl_bind_key('\t', rl_insert); |
| 1109 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 1110 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 1111 | rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap); |
Martin Panter | 5dbbf1a | 2016-04-03 02:54:58 +0000 | [diff] [blame] | 1112 | #ifdef HAVE_RL_RESIZE_TERMINAL |
| 1113 | /* Set up signal handler for window resize */ |
| 1114 | sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler); |
| 1115 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | /* Set our hook functions */ |
Benjamin Peterson | f0b463a | 2014-01-24 11:44:16 -0500 | [diff] [blame] | 1117 | rl_startup_hook = on_startup_hook; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 1118 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
Benjamin Peterson | f0b463a | 2014-01-24 11:44:16 -0500 | [diff] [blame] | 1119 | rl_pre_input_hook = on_pre_input_hook; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 1120 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | /* Set our completion function */ |
Benjamin Peterson | f0b463a | 2014-01-24 11:44:16 -0500 | [diff] [blame] | 1122 | rl_attempted_completion_function = flex_complete; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | /* Set Python word break characters */ |
Antoine Pitrou | a7f7deb | 2013-05-06 21:51:03 +0200 | [diff] [blame] | 1124 | completer_word_break_characters = |
| 1125 | rl_completer_word_break_characters = |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); |
| 1127 | /* All nonalphanums except '.' */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 1128 | |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1129 | mod_state->begidx = PyLong_FromLong(0L); |
| 1130 | mod_state->endidx = PyLong_FromLong(0L); |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 1131 | |
Martin Panter | c427b8d | 2016-08-27 03:23:11 +0000 | [diff] [blame] | 1132 | #ifdef __APPLE__ |
| 1133 | if (!using_libedit_emulation) |
Victor Stinner | 92639cc | 2014-07-24 22:11:38 +0200 | [diff] [blame] | 1134 | #endif |
Martin Panter | c427b8d | 2016-08-27 03:23:11 +0000 | [diff] [blame] | 1135 | { |
| 1136 | if (!isatty(STDOUT_FILENO)) { |
| 1137 | /* Issue #19884: stdout is not a terminal. Disable meta modifier |
| 1138 | keys to not write the ANSI sequence "\033[1034h" into stdout. On |
| 1139 | terminals supporting 8 bit characters like TERM=xterm-256color |
| 1140 | (which is now the default Fedora since Fedora 18), the meta key is |
| 1141 | used to enable support of 8 bit characters (ANSI sequence |
| 1142 | "\033[1034h"). |
| 1143 | |
| 1144 | With libedit, this call makes readline() crash. */ |
| 1145 | rl_variable_bind ("enable-meta-key", "off"); |
| 1146 | } |
| 1147 | } |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 1148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | /* Initialize (allows .inputrc to override) |
| 1150 | * |
| 1151 | * XXX: A bug in the readline-2.2 library causes a memory leak |
| 1152 | * inside this function. Nothing we can do about it. |
| 1153 | */ |
R. David Murray | 52d1b4e | 2010-12-18 03:48:32 +0000 | [diff] [blame] | 1154 | #ifdef __APPLE__ |
| 1155 | if (using_libedit_emulation) |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1156 | rl_read_init_file(NULL); |
R. David Murray | 52d1b4e | 2010-12-18 03:48:32 +0000 | [diff] [blame] | 1157 | else |
| 1158 | #endif /* __APPLE__ */ |
| 1159 | rl_initialize(); |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | RESTORE_LOCALE(saved_locale) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1164 | /* Wrapper around GNU readline that handles signals differently. */ |
| 1165 | |
| 1166 | |
| 1167 | #if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) |
| 1168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | static char *completed_input_string; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1170 | static void |
| 1171 | rlhandler(char *text) |
| 1172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | completed_input_string = text; |
| 1174 | rl_callback_handler_remove(); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1177 | static char * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 1178 | readline_until_enter_or_signal(const char *prompt, int *signal) |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1179 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | char * not_done_reading = ""; |
| 1181 | fd_set selectset; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | *signal = 0; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1184 | #ifdef HAVE_RL_CATCH_SIGNAL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | rl_catch_signals = 0; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1186 | #endif |
| 1187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | rl_callback_handler_install (prompt, rlhandler); |
| 1189 | FD_ZERO(&selectset); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | completed_input_string = not_done_reading; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | while (completed_input_string == not_done_reading) { |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 1194 | int has_input = 0, err = 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | while (!has_input) |
| 1197 | { struct timeval timeout = {0, 100000}; /* 0.1 seconds */ |
Michael W. Hudson | 8da2b01 | 2004-10-07 13:46:33 +0000 | [diff] [blame] | 1198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | /* [Bug #1552726] Only limit the pause if an input hook has been |
| 1200 | defined. */ |
| 1201 | struct timeval *timeoutp = NULL; |
| 1202 | if (PyOS_InputHook) |
| 1203 | timeoutp = &timeout; |
Martin Panter | 5dbbf1a | 2016-04-03 02:54:58 +0000 | [diff] [blame] | 1204 | #ifdef HAVE_RL_RESIZE_TERMINAL |
| 1205 | /* Update readline's view of the window size after SIGWINCH */ |
| 1206 | if (sigwinch_received) { |
| 1207 | sigwinch_received = 0; |
| 1208 | rl_resize_terminal(); |
| 1209 | } |
| 1210 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | FD_SET(fileno(rl_instream), &selectset); |
| 1212 | /* select resets selectset if no input was available */ |
| 1213 | has_input = select(fileno(rl_instream) + 1, &selectset, |
| 1214 | NULL, NULL, timeoutp); |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 1215 | err = errno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | if(PyOS_InputHook) PyOS_InputHook(); |
| 1217 | } |
| 1218 | |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 1219 | if (has_input > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | rl_callback_read_char(); |
| 1221 | } |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 1222 | else if (err == EINTR) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | int s; |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 1224 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 1226 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | s = PyErr_CheckSignals(); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 1228 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | PyEval_SaveThread(); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 1230 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1231 | if (s < 0) { |
| 1232 | rl_free_line_state(); |
Martin Panter | d6990d2 | 2016-03-22 07:24:05 +0000 | [diff] [blame] | 1233 | #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0700 |
| 1234 | rl_callback_sigcleanup(); |
| 1235 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | rl_cleanup_after_signal(); |
| 1237 | rl_callback_handler_remove(); |
| 1238 | *signal = 1; |
| 1239 | completed_input_string = NULL; |
| 1240 | } |
| 1241 | } |
| 1242 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | return completed_input_string; |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | #else |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1249 | |
| 1250 | /* Interrupt handler */ |
| 1251 | |
| 1252 | static jmp_buf jbuf; |
| 1253 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1254 | /* ARGSUSED */ |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 1255 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 1256 | onintr(int sig) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | longjmp(jbuf, 1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1261 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1262 | static char * |
Christian Heimes | a3da7c5 | 2013-12-04 09:31:47 +0100 | [diff] [blame] | 1263 | readline_until_enter_or_signal(const char *prompt, int *signal) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | PyOS_sighandler_t old_inthandler; |
| 1266 | char *p; |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 1267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | *signal = 0; |
| 1269 | |
| 1270 | old_inthandler = PyOS_setsig(SIGINT, onintr); |
| 1271 | if (setjmp(jbuf)) { |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1272 | #ifdef HAVE_SIGRELSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 1274 | sigrelse(SIGINT); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1275 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | PyOS_setsig(SIGINT, old_inthandler); |
| 1277 | *signal = 1; |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | rl_event_hook = PyOS_InputHook; |
| 1281 | p = readline(prompt); |
| 1282 | PyOS_setsig(SIGINT, old_inthandler); |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1283 | |
| 1284 | return p; |
| 1285 | } |
| 1286 | #endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */ |
| 1287 | |
| 1288 | |
| 1289 | static char * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 1290 | call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1291 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | size_t n; |
| 1293 | char *p, *q; |
| 1294 | int signal; |
Neal Norwitz | 1fa040b | 2004-08-25 01:20:18 +0000 | [diff] [blame] | 1295 | |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 1296 | #ifdef SAVE_LOCALE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | char *saved_locale = strdup(setlocale(LC_CTYPE, NULL)); |
| 1298 | if (!saved_locale) |
| 1299 | Py_FatalError("not enough memory to save locale"); |
Nadeem Vawda | 6375257 | 2013-02-02 20:05:11 +0100 | [diff] [blame] | 1300 | setlocale(LC_CTYPE, ""); |
Martin v. Löwis | 78a8acc | 2004-08-18 13:34:00 +0000 | [diff] [blame] | 1301 | #endif |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1303 | if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { |
| 1304 | rl_instream = sys_stdin; |
| 1305 | rl_outstream = sys_stdout; |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 1306 | #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | rl_prep_terminal (1); |
Guido van Rossum | faf5e4d | 2002-12-30 16:25:41 +0000 | [diff] [blame] | 1308 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | } |
Guido van Rossum | 74f3143 | 2003-01-07 20:01:29 +0000 | [diff] [blame] | 1310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | p = readline_until_enter_or_signal(prompt, &signal); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | /* we got an interrupt signal */ |
| 1314 | if (signal) { |
| 1315 | RESTORE_LOCALE(saved_locale) |
| 1316 | return NULL; |
| 1317 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1318 | |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 1319 | /* We got an EOF, return an empty string. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | if (p == NULL) { |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 1321 | p = PyMem_RawMalloc(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | if (p != NULL) |
| 1323 | *p = '\0'; |
| 1324 | RESTORE_LOCALE(saved_locale) |
| 1325 | return p; |
| 1326 | } |
| 1327 | |
| 1328 | /* we have a valid line */ |
| 1329 | n = strlen(p); |
| 1330 | if (n > 0) { |
Brett Cannon | 2525dc8 | 2010-08-22 20:36:25 +0000 | [diff] [blame] | 1331 | const char *line; |
Mark Dickinson | 6b54e1f | 2010-08-03 16:49:49 +0000 | [diff] [blame] | 1332 | int length = _py_get_history_length(); |
| 1333 | if (length > 0) |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1334 | #ifdef __APPLE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | if (using_libedit_emulation) { |
Ned Deily | f70f4a6 | 2013-09-06 15:16:19 -0700 | [diff] [blame] | 1336 | /* handle older 0-based or newer 1-based indexing */ |
| 1337 | line = (const char *)history_get(length + libedit_history_start - 1)->line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1338 | } else |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1339 | #endif /* __APPLE__ */ |
Brett Cannon | 2525dc8 | 2010-08-22 20:36:25 +0000 | [diff] [blame] | 1340 | line = (const char *)history_get(length)->line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | else |
| 1342 | line = ""; |
| 1343 | if (strcmp(p, line)) |
| 1344 | add_history(p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | } |
| 1346 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and |
| 1347 | release the original. */ |
| 1348 | q = p; |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 1349 | p = PyMem_RawMalloc(n+2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | if (p != NULL) { |
| 1351 | strncpy(p, q, n); |
| 1352 | p[n] = '\n'; |
| 1353 | p[n+1] = '\0'; |
| 1354 | } |
| 1355 | free(q); |
| 1356 | RESTORE_LOCALE(saved_locale) |
| 1357 | return p; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1360 | |
| 1361 | /* Initialize the module */ |
| 1362 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1363 | PyDoc_STRVAR(doc_module, |
| 1364 | "Importing this module enables command line editing using GNU readline."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1365 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1366 | #ifdef __APPLE__ |
| 1367 | PyDoc_STRVAR(doc_module_le, |
| 1368 | "Importing this module enables command line editing using libedit readline."); |
| 1369 | #endif /* __APPLE__ */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1370 | |
| 1371 | static struct PyModuleDef readlinemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | PyModuleDef_HEAD_INIT, |
| 1373 | "readline", |
| 1374 | doc_module, |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1375 | sizeof(readlinestate), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | readline_methods, |
| 1377 | NULL, |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1378 | readline_traverse, |
| 1379 | readline_clear, |
| 1380 | readline_free |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1381 | }; |
| 1382 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1383 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1384 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1385 | PyInit_readline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | PyObject *m; |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1388 | readlinestate *mod_state; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 1389 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1390 | #ifdef __APPLE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) { |
| 1392 | using_libedit_emulation = 1; |
| 1393 | } |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | if (using_libedit_emulation) |
| 1396 | readlinemodule.m_doc = doc_module_le; |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1397 | |
| 1398 | #endif /* __APPLE__ */ |
| 1399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | m = PyModule_Create(&readlinemodule); |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | if (m == NULL) |
| 1403 | return NULL; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1405 | mod_state = (readlinestate *) PyModule_GetState(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1406 | PyOS_ReadlineFunctionPointer = call_readline; |
Antoine Pitrou | 5c30a75 | 2013-07-31 21:52:53 +0200 | [diff] [blame] | 1407 | setup_readline(mod_state); |
Antoine Pitrou | 7e8b867 | 2014-11-04 14:52:10 +0100 | [diff] [blame] | 1408 | |
| 1409 | PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION); |
| 1410 | PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version); |
| 1411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | return m; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 1413 | } |