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