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