--- Python-2.3.4/Modules/readline.c 2003-11-13 02:42:13.000000000 -0500 +++ Python-2.3.4-patched/Modules/readline.c 2007-03-28 07:45:31.000000000 -0400 @@ -197,6 +197,9 @@ static PyObject *startup_hook = NULL; static PyThreadState *startup_hook_tstate = NULL; +static PyObject *custom_hook = NULL; +static PyThreadState *custom_hook_tstate = NULL; + #ifdef HAVE_RL_PRE_INPUT_HOOK static PyObject *pre_input_hook = NULL; static PyThreadState *pre_input_hook_tstate = NULL; @@ -230,12 +233,27 @@ PyDoc_STRVAR(doc_set_pre_input_hook, "set_pre_input_hook([function]) -> None\n\ Set or remove the pre_input_hook function.\n\ +This custom function can be bound to a key or keyseq using parse_and_bind().\n\ +"); + +#endif + +PyDoc_STRVAR(doc_set_custom_hook, +"set_custom_hook([function]) -> None\n\ +Set or remove the custom_hook function.\n\ The function is called with no arguments after the first prompt\n\ has been printed and just before readline starts reading input\n\ characters."); -#endif +/* Set custom hook */ + +static PyObject * +set_custom_hook(PyObject *self, PyObject *args) +{ + return set_hook("custom_hook", &custom_hook, + &custom_hook_tstate, args); +} /* Exported function to specify a word completer in Python */ @@ -484,6 +502,8 @@ {"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook}, #endif + {"set_custom_hook", set_custom_hook, + METH_VARARGS, doc_set_custom_hook}, {0, 0} }; @@ -531,6 +551,11 @@ } #endif +static int +on_custom_hook(int count, int key) +{ + return on_hook(custom_hook, &custom_hook_tstate); +} /* C function to call the Python completer. */ @@ -620,6 +645,9 @@ rl_completion_append_character ='\0'; #endif + /* register custom function */ + rl_add_defun("custom", (rl_command_func_t *)on_custom_hook, -1); + begidx = PyInt_FromLong(0L); endidx = PyInt_FromLong(0L); /* Initialize (allows .inputrc to override) @@ -717,7 +745,6 @@ return p; } - /* Initialize the module */ PyDoc_STRVAR(doc_module,