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