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