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