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 |
| 3 | * Center. The completer interface was inspired by Lele Gaifax. |
| 4 | * |
| 5 | * More recently, it was largely rewritten by Guido van Rossum who is |
| 6 | * now maintaining it. |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 9 | /* Standard definitions */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 10 | #include "Python.h" |
| 11 | #include <setjmp.h> |
| 12 | #include <signal.h> |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 13 | #include <errno.h> |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 15 | /* GNU readline definitions */ |
Guido van Rossum | b0e51b2 | 2001-04-13 18:14:27 +0000 | [diff] [blame] | 16 | #undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 17 | #include <readline/readline.h> |
| 18 | #include <readline/history.h> |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 353ae58 | 2001-07-10 16:45:32 +0000 | [diff] [blame] | 20 | #ifdef HAVE_RL_COMPLETION_MATCHES |
| 21 | #define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y))) |
| 22 | #endif |
| 23 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 24 | /* Pointers needed from outside (but not declared in a header file). */ |
Guido van Rossum | 5a53019 | 2001-01-10 21:03:32 +0000 | [diff] [blame] | 25 | extern DL_IMPORT(int) (*PyOS_InputHook)(void); |
| 26 | extern DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(char *); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 29 | /* Exported function to send one line to readline's init file parser */ |
| 30 | |
| 31 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 32 | parse_and_bind(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 33 | { |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 34 | char *s, *copy; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 35 | if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 36 | return NULL; |
Guido van Rossum | 3b5330e | 1998-12-04 15:34:39 +0000 | [diff] [blame] | 37 | /* Make a copy -- rl_parse_and_bind() modifies its argument */ |
| 38 | /* Bernard Herzog */ |
| 39 | copy = malloc(1 + strlen(s)); |
| 40 | if (copy == NULL) |
| 41 | return PyErr_NoMemory(); |
| 42 | strcpy(copy, s); |
| 43 | rl_parse_and_bind(copy); |
| 44 | free(copy); /* Free the copy */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 45 | Py_INCREF(Py_None); |
| 46 | return Py_None; |
| 47 | } |
| 48 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 49 | PyDoc_STRVAR(doc_parse_and_bind, |
| 50 | "parse_and_bind(string) -> None\n\ |
| 51 | Parse and execute single line of a readline init file."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | /* Exported function to parse a readline init file */ |
| 55 | |
| 56 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 57 | read_init_file(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 58 | { |
| 59 | char *s = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 60 | if (!PyArg_ParseTuple(args, "|z:read_init_file", &s)) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 61 | return NULL; |
| 62 | errno = rl_read_init_file(s); |
| 63 | if (errno) |
| 64 | return PyErr_SetFromErrno(PyExc_IOError); |
| 65 | Py_INCREF(Py_None); |
| 66 | return Py_None; |
| 67 | } |
| 68 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 69 | PyDoc_STRVAR(doc_read_init_file, |
| 70 | "read_init_file([filename]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 71 | Parse a readline initialization file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 72 | The default filename is the last filename used."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 73 | |
| 74 | |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 75 | /* Exported function to load a readline history file */ |
| 76 | |
| 77 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 78 | read_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 79 | { |
| 80 | char *s = NULL; |
| 81 | if (!PyArg_ParseTuple(args, "|z:read_history_file", &s)) |
| 82 | return NULL; |
| 83 | errno = read_history(s); |
| 84 | if (errno) |
| 85 | return PyErr_SetFromErrno(PyExc_IOError); |
| 86 | Py_INCREF(Py_None); |
| 87 | return Py_None; |
| 88 | } |
| 89 | |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 90 | 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] | 91 | PyDoc_STRVAR(doc_read_history_file, |
| 92 | "read_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 93 | Load a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 94 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | /* Exported function to save a readline history file */ |
| 98 | |
| 99 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 100 | write_history_file(PyObject *self, PyObject *args) |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 101 | { |
| 102 | char *s = NULL; |
| 103 | if (!PyArg_ParseTuple(args, "|z:write_history_file", &s)) |
| 104 | return NULL; |
| 105 | errno = write_history(s); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 106 | if (!errno && history_length >= 0) |
| 107 | history_truncate_file(s, history_length); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 108 | if (errno) |
| 109 | return PyErr_SetFromErrno(PyExc_IOError); |
| 110 | Py_INCREF(Py_None); |
| 111 | return Py_None; |
| 112 | } |
| 113 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 114 | PyDoc_STRVAR(doc_write_history_file, |
| 115 | "write_history_file([filename]) -> None\n\ |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 116 | Save a readline history file.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 117 | The default filename is ~/.history."); |
Skip Montanaro | 2806782 | 2000-07-06 18:55:12 +0000 | [diff] [blame] | 118 | |
| 119 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 120 | PyDoc_STRVAR(set_history_length_doc, |
| 121 | "set_history_length(length) -> None\n\ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 122 | set the maximal number of items which will be written to\n\ |
| 123 | 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] | 124 | history truncation."); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 125 | |
| 126 | static PyObject* |
| 127 | set_history_length(PyObject *self, PyObject *args) |
| 128 | { |
| 129 | int length = history_length; |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 130 | if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) |
| 131 | return NULL; |
| 132 | history_length = length; |
| 133 | Py_INCREF(Py_None); |
| 134 | return Py_None; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 139 | PyDoc_STRVAR(get_history_length_doc, |
| 140 | "get_history_length() -> int\n\ |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 141 | return the maximum number of items that will be written to\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 142 | the history file."); |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 143 | |
| 144 | static PyObject* |
| 145 | get_history_length(PyObject *self, PyObject *args) |
| 146 | { |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 147 | if (!PyArg_ParseTuple(args, ":get_history_length")) |
| 148 | return NULL; |
| 149 | return Py_BuildValue("i", history_length); |
| 150 | } |
| 151 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 152 | /* Generic hook function setter */ |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 153 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 154 | static PyObject * |
| 155 | set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args) |
| 156 | { |
| 157 | PyObject *function = Py_None; |
| 158 | char buf[80]; |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 159 | PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 160 | if (!PyArg_ParseTuple(args, buf, &function)) |
| 161 | return NULL; |
| 162 | if (function == Py_None) { |
| 163 | Py_XDECREF(*hook_var); |
| 164 | *hook_var = NULL; |
| 165 | *tstate = NULL; |
| 166 | } |
| 167 | else if (PyCallable_Check(function)) { |
| 168 | PyObject *tmp = *hook_var; |
| 169 | Py_INCREF(function); |
| 170 | *hook_var = function; |
| 171 | Py_XDECREF(tmp); |
| 172 | *tstate = PyThreadState_Get(); |
| 173 | } |
| 174 | else { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 175 | PyOS_snprintf(buf, sizeof(buf), |
| 176 | "set_%.50s(func): argument not callable", |
| 177 | funcname); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 178 | PyErr_SetString(PyExc_TypeError, buf); |
| 179 | return NULL; |
| 180 | } |
| 181 | Py_INCREF(Py_None); |
| 182 | return Py_None; |
| 183 | } |
| 184 | |
| 185 | /* Exported functions to specify hook functions in Python */ |
| 186 | |
| 187 | static PyObject *startup_hook = NULL; |
| 188 | static PyThreadState *startup_hook_tstate = NULL; |
| 189 | |
| 190 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 191 | static PyObject *pre_input_hook = NULL; |
| 192 | static PyThreadState *pre_input_hook_tstate = NULL; |
| 193 | #endif |
| 194 | |
| 195 | static PyObject * |
| 196 | set_startup_hook(PyObject *self, PyObject *args) |
| 197 | { |
| 198 | return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args); |
| 199 | } |
| 200 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 201 | PyDoc_STRVAR(doc_set_startup_hook, |
| 202 | "set_startup_hook([function]) -> None\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 203 | Set or remove the startup_hook function.\n\ |
| 204 | The function is called with no arguments just\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 205 | before readline prints the first prompt."); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 206 | |
| 207 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 208 | static PyObject * |
| 209 | set_pre_input_hook(PyObject *self, PyObject *args) |
| 210 | { |
| 211 | return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args); |
| 212 | } |
| 213 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 214 | PyDoc_STRVAR(doc_set_pre_input_hook, |
| 215 | "set_pre_input_hook([function]) -> None\n\ |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 216 | Set or remove the pre_input_hook function.\n\ |
| 217 | The function is called with no arguments after the first prompt\n\ |
| 218 | 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] | 219 | characters."); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 220 | #endif |
Skip Montanaro | 49bd24d | 2000-07-19 16:54:53 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 222 | /* Exported function to specify a word completer in Python */ |
| 223 | |
| 224 | static PyObject *completer = NULL; |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 225 | static PyThreadState *completer_tstate = NULL; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 227 | static PyObject *begidx = NULL; |
| 228 | static PyObject *endidx = NULL; |
| 229 | |
| 230 | /* get the beginning index for the scope of the tab-completion */ |
| 231 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 232 | get_begidx(PyObject *self) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 233 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 234 | Py_INCREF(begidx); |
| 235 | return begidx; |
| 236 | } |
| 237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 238 | PyDoc_STRVAR(doc_get_begidx, |
| 239 | "get_begidx() -> int\n\ |
| 240 | get the beginning index of the readline tab-completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 241 | |
| 242 | /* get the ending index for the scope of the tab-completion */ |
| 243 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 244 | get_endidx(PyObject *self) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 245 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 246 | Py_INCREF(endidx); |
| 247 | return endidx; |
| 248 | } |
| 249 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 250 | PyDoc_STRVAR(doc_get_endidx, |
| 251 | "get_endidx() -> int\n\ |
| 252 | get the ending index of the readline tab-completion scope"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 253 | |
| 254 | |
| 255 | /* set the tab-completion word-delimiters that readline uses */ |
| 256 | |
| 257 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 258 | set_completer_delims(PyObject *self, PyObject *args) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 259 | { |
| 260 | char *break_chars; |
| 261 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 262 | if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 263 | return NULL; |
| 264 | } |
Neal Norwitz | 0e0ee59 | 2002-04-21 15:03:18 +0000 | [diff] [blame] | 265 | free((void*)rl_completer_word_break_characters); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 266 | rl_completer_word_break_characters = strdup(break_chars); |
| 267 | Py_INCREF(Py_None); |
| 268 | return Py_None; |
| 269 | } |
| 270 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 271 | PyDoc_STRVAR(doc_set_completer_delims, |
| 272 | "set_completer_delims(string) -> None\n\ |
| 273 | set the readline word delimiters for tab-completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 275 | static PyObject * |
| 276 | py_add_history(PyObject *self, PyObject *args) |
| 277 | { |
| 278 | char *line; |
| 279 | |
| 280 | if(!PyArg_ParseTuple(args, "s:add_history", &line)) { |
| 281 | return NULL; |
| 282 | } |
| 283 | add_history(line); |
| 284 | Py_INCREF(Py_None); |
| 285 | return Py_None; |
| 286 | } |
| 287 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 288 | PyDoc_STRVAR(doc_add_history, |
| 289 | "add_history(string) -> None\n\ |
| 290 | add a line to the history buffer"); |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 291 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 292 | |
| 293 | /* get the tab-completion word-delimiters that readline uses */ |
| 294 | |
| 295 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 296 | get_completer_delims(PyObject *self) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 297 | { |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 298 | return PyString_FromString(rl_completer_word_break_characters); |
| 299 | } |
| 300 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 301 | PyDoc_STRVAR(doc_get_completer_delims, |
| 302 | "get_completer_delims() -> string\n\ |
| 303 | get the readline word delimiters for tab-completion"); |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 305 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 306 | set_completer(PyObject *self, PyObject *args) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 307 | { |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 308 | return set_hook("completer", &completer, &completer_tstate, args); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 311 | PyDoc_STRVAR(doc_set_completer, |
| 312 | "set_completer([function]) -> None\n\ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 313 | Set or remove the completer function.\n\ |
| 314 | The function is called as function(text, state),\n\ |
Fred Drake | 52d55a3 | 2001-08-01 21:44:14 +0000 | [diff] [blame] | 315 | 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] | 316 | It should return the next possible completion starting with 'text'."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 317 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 318 | /* Exported function to get any element of history */ |
| 319 | |
| 320 | static PyObject * |
| 321 | get_history_item(PyObject *self, PyObject *args) |
| 322 | { |
| 323 | int idx = 0; |
| 324 | HIST_ENTRY *hist_ent; |
| 325 | |
| 326 | if (!PyArg_ParseTuple(args, "i:index", &idx)) |
| 327 | return NULL; |
| 328 | if ((hist_ent = history_get(idx))) |
| 329 | return PyString_FromString(hist_ent->line); |
| 330 | else { |
| 331 | Py_INCREF(Py_None); |
| 332 | return Py_None; |
| 333 | } |
| 334 | } |
| 335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 336 | PyDoc_STRVAR(doc_get_history_item, |
| 337 | "get_history_item() -> string\n\ |
| 338 | return the current contents of history item at index."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 339 | |
| 340 | /* Exported function to get current length of history */ |
| 341 | |
| 342 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 343 | get_current_history_length(PyObject *self) |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 344 | { |
| 345 | HISTORY_STATE *hist_st; |
| 346 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 347 | hist_st = history_get_history_state(); |
| 348 | return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0); |
| 349 | } |
| 350 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 351 | PyDoc_STRVAR(doc_get_current_history_length, |
| 352 | "get_current_history_length() -> integer\n\ |
| 353 | return the current (not the maximum) length of history."); |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 354 | |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 355 | /* Exported function to read the current line buffer */ |
| 356 | |
| 357 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 358 | get_line_buffer(PyObject *self) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 359 | { |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 360 | return PyString_FromString(rl_line_buffer); |
| 361 | } |
| 362 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 363 | PyDoc_STRVAR(doc_get_line_buffer, |
| 364 | "get_line_buffer() -> string\n\ |
| 365 | return the current contents of the line buffer."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 366 | |
| 367 | /* Exported function to insert text into the line buffer */ |
| 368 | |
| 369 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 370 | insert_text(PyObject *self, PyObject *args) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 371 | { |
| 372 | char *s; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 373 | if (!PyArg_ParseTuple(args, "s:insert_text", &s)) |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 374 | return NULL; |
| 375 | rl_insert_text(s); |
| 376 | Py_INCREF(Py_None); |
| 377 | return Py_None; |
| 378 | } |
| 379 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 380 | PyDoc_STRVAR(doc_insert_text, |
| 381 | "insert_text(string) -> None\n\ |
| 382 | Insert text into the command line."); |
Guido van Rossum | 79378ff | 1997-10-07 14:53:21 +0000 | [diff] [blame] | 383 | |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 384 | static PyObject * |
| 385 | redisplay(PyObject *self) |
| 386 | { |
| 387 | rl_redisplay(); |
| 388 | Py_INCREF(Py_None); |
| 389 | return Py_None; |
| 390 | } |
| 391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 392 | PyDoc_STRVAR(doc_redisplay, |
| 393 | "redisplay() -> None\n\ |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 394 | 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] | 395 | contents of the line buffer."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 396 | |
| 397 | /* Table of functions exported by the module */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 398 | |
| 399 | static struct PyMethodDef readline_methods[] = |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 400 | { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 401 | {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind}, |
Neal Norwitz | 767f835 | 2002-03-31 16:13:39 +0000 | [diff] [blame] | 402 | {"get_line_buffer", (PyCFunction)get_line_buffer, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 403 | METH_NOARGS, doc_get_line_buffer}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 404 | {"insert_text", insert_text, METH_VARARGS, doc_insert_text}, |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 405 | {"redisplay", (PyCFunction)redisplay, METH_NOARGS, doc_redisplay}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 406 | {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, |
| 407 | {"read_history_file", read_history_file, |
| 408 | METH_VARARGS, doc_read_history_file}, |
| 409 | {"write_history_file", write_history_file, |
| 410 | METH_VARARGS, doc_write_history_file}, |
Neil Schemenauer | 0f75e0d | 2002-03-24 01:09:04 +0000 | [diff] [blame] | 411 | {"get_history_item", get_history_item, |
| 412 | METH_VARARGS, doc_get_history_item}, |
Neal Norwitz | 767f835 | 2002-03-31 16:13:39 +0000 | [diff] [blame] | 413 | {"get_current_history_length", (PyCFunction)get_current_history_length, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 414 | METH_NOARGS, doc_get_current_history_length}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 415 | {"set_history_length", set_history_length, |
| 416 | METH_VARARGS, set_history_length_doc}, |
| 417 | {"get_history_length", get_history_length, |
| 418 | METH_VARARGS, get_history_length_doc}, |
| 419 | {"set_completer", set_completer, METH_VARARGS, doc_set_completer}, |
Neal Norwitz | 767f835 | 2002-03-31 16:13:39 +0000 | [diff] [blame] | 420 | {"get_begidx", (PyCFunction)get_begidx, METH_NOARGS, doc_get_begidx}, |
| 421 | {"get_endidx", (PyCFunction)get_endidx, METH_NOARGS, doc_get_endidx}, |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 422 | |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 423 | {"set_completer_delims", set_completer_delims, |
| 424 | METH_VARARGS, doc_set_completer_delims}, |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 425 | {"add_history", py_add_history, METH_VARARGS, doc_add_history}, |
Neal Norwitz | 767f835 | 2002-03-31 16:13:39 +0000 | [diff] [blame] | 426 | {"get_completer_delims", (PyCFunction)get_completer_delims, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 427 | METH_NOARGS, doc_get_completer_delims}, |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 428 | |
| 429 | {"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook}, |
| 430 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 431 | {"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook}, |
| 432 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 433 | {0, 0} |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 434 | }; |
| 435 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 436 | /* C function to call the Python hooks. */ |
| 437 | |
| 438 | static int |
| 439 | on_hook(PyObject *func, PyThreadState *tstate) |
| 440 | { |
| 441 | int result = 0; |
| 442 | if (func != NULL) { |
| 443 | PyObject *r; |
| 444 | PyThreadState *save_tstate; |
| 445 | /* Note that readline is called with the interpreter |
| 446 | lock released! */ |
| 447 | save_tstate = PyThreadState_Swap(NULL); |
| 448 | PyEval_RestoreThread(tstate); |
| 449 | r = PyObject_CallFunction(func, NULL); |
| 450 | if (r == NULL) |
| 451 | goto error; |
| 452 | if (r == Py_None) |
| 453 | result = 0; |
| 454 | else |
| 455 | result = PyInt_AsLong(r); |
| 456 | Py_DECREF(r); |
| 457 | goto done; |
| 458 | error: |
| 459 | PyErr_Clear(); |
| 460 | Py_XDECREF(r); |
| 461 | done: |
| 462 | PyEval_SaveThread(); |
| 463 | PyThreadState_Swap(save_tstate); |
| 464 | } |
| 465 | return result; |
| 466 | } |
| 467 | |
| 468 | static int |
| 469 | on_startup_hook(void) |
| 470 | { |
| 471 | return on_hook(startup_hook, startup_hook_tstate); |
| 472 | } |
| 473 | |
| 474 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 475 | static int |
| 476 | on_pre_input_hook(void) |
| 477 | { |
| 478 | return on_hook(pre_input_hook, pre_input_hook_tstate); |
| 479 | } |
| 480 | #endif |
| 481 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 482 | /* C function to call the Python completer. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 484 | static char * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 485 | on_completion(char *text, int state) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 486 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 487 | char *result = NULL; |
| 488 | if (completer != NULL) { |
| 489 | PyObject *r; |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 490 | PyThreadState *save_tstate; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 491 | /* Note that readline is called with the interpreter |
| 492 | lock released! */ |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 493 | save_tstate = PyThreadState_Swap(NULL); |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 494 | PyEval_RestoreThread(completer_tstate); |
Michael W. Hudson | 0c1ceaf | 2002-02-13 11:58:25 +0000 | [diff] [blame] | 495 | /* Don't use the default filename completion if we |
| 496 | * have a custom completion function... */ |
| 497 | rl_attempted_completion_over = 1; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 498 | r = PyObject_CallFunction(completer, "si", text, state); |
| 499 | if (r == NULL) |
| 500 | goto error; |
| 501 | if (r == Py_None) { |
| 502 | result = NULL; |
| 503 | } |
| 504 | else { |
| 505 | char *s = PyString_AsString(r); |
| 506 | if (s == NULL) |
| 507 | goto error; |
| 508 | result = strdup(s); |
| 509 | } |
| 510 | Py_DECREF(r); |
| 511 | goto done; |
| 512 | error: |
| 513 | PyErr_Clear(); |
| 514 | Py_XDECREF(r); |
| 515 | done: |
| 516 | PyEval_SaveThread(); |
Guido van Rossum | a59406a | 1997-10-10 17:39:19 +0000 | [diff] [blame] | 517 | PyThreadState_Swap(save_tstate); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 518 | } |
| 519 | return result; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 522 | |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 523 | /* a more flexible constructor that saves the "begidx" and "endidx" |
| 524 | * before calling the normal completer */ |
| 525 | |
| 526 | char ** |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 527 | flex_complete(char *text, int start, int end) |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 528 | { |
| 529 | Py_XDECREF(begidx); |
| 530 | Py_XDECREF(endidx); |
| 531 | begidx = PyInt_FromLong((long) start); |
| 532 | endidx = PyInt_FromLong((long) end); |
| 533 | return completion_matches(text, *on_completion); |
| 534 | } |
| 535 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 536 | /* Helper to initialize GNU readline properly. */ |
| 537 | |
| 538 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 539 | setup_readline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 540 | { |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 541 | using_history(); |
| 542 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 543 | rl_readline_name = "python"; |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 544 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 545 | /* Allow $if term= in .inputrc to work */ |
| 546 | rl_terminal_name = getenv("TERM"); |
| 547 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 548 | /* Force rebind of TAB to insert-tab */ |
| 549 | rl_bind_key('\t', rl_insert); |
| 550 | /* Bind both ESC-TAB and ESC-ESC to the completion function */ |
| 551 | rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap); |
| 552 | 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] | 553 | /* Set our hook functions */ |
| 554 | rl_startup_hook = (Function *)on_startup_hook; |
| 555 | #ifdef HAVE_RL_PRE_INPUT_HOOK |
| 556 | rl_pre_input_hook = (Function *)on_pre_input_hook; |
| 557 | #endif |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 558 | /* Set our completion function */ |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 559 | rl_attempted_completion_function = (CPPFunction *)flex_complete; |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 560 | /* Set Python word break characters */ |
| 561 | rl_completer_word_break_characters = |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 562 | strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?"); |
Guido van Rossum | b6c935a | 1997-09-26 23:00:37 +0000 | [diff] [blame] | 563 | /* All nonalphanums except '.' */ |
Guido van Rossum | 84271bb | 2002-05-30 15:41:56 +0000 | [diff] [blame] | 564 | rl_completion_append_character ='\0'; |
Guido van Rossum | b960e7a | 1999-11-18 17:51:02 +0000 | [diff] [blame] | 565 | |
| 566 | begidx = PyInt_FromLong(0L); |
| 567 | endidx = PyInt_FromLong(0L); |
Barry Warsaw | f761287 | 1999-01-29 21:55:03 +0000 | [diff] [blame] | 568 | /* Initialize (allows .inputrc to override) |
| 569 | * |
| 570 | * XXX: A bug in the readline-2.2 library causes a memory leak |
| 571 | * inside this function. Nothing we can do about it. |
| 572 | */ |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 573 | rl_initialize(); |
| 574 | } |
| 575 | |
| 576 | |
| 577 | /* Interrupt handler */ |
| 578 | |
| 579 | static jmp_buf jbuf; |
| 580 | |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 581 | /* ARGSUSED */ |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 582 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 583 | onintr(int sig) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 584 | { |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 585 | longjmp(jbuf, 1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 588 | |
| 589 | /* Wrapper around GNU readline that handles signals differently. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 590 | |
| 591 | static char * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 592 | call_readline(char *prompt) |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 593 | { |
Guido van Rossum | 26418a9 | 2000-06-28 21:30:31 +0000 | [diff] [blame] | 594 | size_t n; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 595 | char *p, *q; |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 596 | PyOS_sighandler_t old_inthandler; |
| 597 | |
| 598 | old_inthandler = PyOS_setsig(SIGINT, onintr); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 599 | if (setjmp(jbuf)) { |
| 600 | #ifdef HAVE_SIGRELSE |
| 601 | /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */ |
| 602 | sigrelse(SIGINT); |
| 603 | #endif |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 604 | PyOS_setsig(SIGINT, old_inthandler); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 605 | return NULL; |
| 606 | } |
Guido van Rossum | 4462064 | 1997-08-11 18:57:29 +0000 | [diff] [blame] | 607 | rl_event_hook = PyOS_InputHook; |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 608 | p = readline(prompt); |
Guido van Rossum | 174efc9 | 2000-09-16 16:37:53 +0000 | [diff] [blame] | 609 | PyOS_setsig(SIGINT, old_inthandler); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 610 | |
| 611 | /* We must return a buffer allocated with PyMem_Malloc. */ |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 612 | if (p == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 613 | p = PyMem_Malloc(1); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 614 | if (p != NULL) |
| 615 | *p = '\0'; |
| 616 | return p; |
| 617 | } |
| 618 | n = strlen(p); |
Skip Montanaro | a039274 | 2002-06-11 14:32:46 +0000 | [diff] [blame] | 619 | if (n > 0) { |
| 620 | char *line; |
| 621 | HISTORY_STATE *state = history_get_history_state(); |
| 622 | if (state->length > 0) |
| 623 | line = history_get(state->length)->line; |
| 624 | else |
| 625 | line = ""; |
| 626 | if (strcmp(p, line)) |
| 627 | add_history(p); |
| 628 | /* the history docs don't say so, but the address of state |
| 629 | changes each time history_get_history_state is called |
| 630 | which makes me think it's freshly malloc'd memory... |
| 631 | on the other hand, the address of the last line stays the |
| 632 | same as long as history isn't extended, so it appears to |
| 633 | be malloc'd but managed by the history package... */ |
| 634 | free(state); |
| 635 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 636 | /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and |
| 637 | release the original. */ |
| 638 | q = p; |
| 639 | p = PyMem_Malloc(n+2); |
| 640 | if (p != NULL) { |
| 641 | strncpy(p, q, n); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 642 | p[n] = '\n'; |
| 643 | p[n+1] = '\0'; |
| 644 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 645 | free(q); |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 646 | return p; |
| 647 | } |
| 648 | |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 649 | |
| 650 | /* Initialize the module */ |
| 651 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 652 | PyDoc_STRVAR(doc_module, |
| 653 | "Importing this module enables command line editing using GNU readline."); |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 654 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 655 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 656 | initreadline(void) |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 657 | { |
Guido van Rossum | 1ea64ea | 2000-10-02 15:53:08 +0000 | [diff] [blame] | 658 | PyObject *m; |
Guido van Rossum | 290900a | 1997-09-26 21:51:21 +0000 | [diff] [blame] | 659 | |
| 660 | m = Py_InitModule4("readline", readline_methods, doc_module, |
| 661 | (PyObject *)NULL, PYTHON_API_VERSION); |
| 662 | if (isatty(fileno(stdin))) { |
| 663 | PyOS_ReadlineFunctionPointer = call_readline; |
| 664 | setup_readline(); |
| 665 | } |
Guido van Rossum | 0969d36 | 1997-08-05 21:27:50 +0000 | [diff] [blame] | 666 | } |