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