📄 readline.c
字号:
static PyObject *
get_completer_delims(PyObject *self, PyObject *args)
{
if(!PyArg_NoArgs(args)) {
return NULL;
}
return PyString_FromString(rl_completer_word_break_characters);
}
static char doc_get_completer_delims[] = "\
get_completer_delims() -> string\n\
get the readline word delimiters for tab-completion";
static PyObject *
set_completer(PyObject *self, PyObject *args)
{
return set_hook("completer", &completer, &completer_tstate, args);
}
static char doc_set_completer[] = "\
set_completer([function]) -> None\n\
Set or remove the completer function.\n\
The function is called as function(text, state),\n\
for state in 0, 1, 2, ..., until it returns a non-string.\n\
It should return the next possible completion starting with 'text'.\
";
/* Exported function to read the current line buffer */
static PyObject *
get_line_buffer(PyObject *self, PyObject *args)
{
if (!PyArg_NoArgs(args))
return NULL;
return PyString_FromString(rl_line_buffer);
}
static char doc_get_line_buffer[] = "\
get_line_buffer() -> string\n\
return the current contents of the line buffer.\
";
/* Exported function to insert text into the line buffer */
static PyObject *
insert_text(PyObject *self, PyObject *args)
{
char *s;
if (!PyArg_ParseTuple(args, "s:insert_text", &s))
return NULL;
rl_insert_text(s);
Py_INCREF(Py_None);
return Py_None;
}
static char doc_insert_text[] = "\
insert_text(string) -> None\n\
Insert text into the command line.\
";
/* Table of functions exported by the module */
static struct PyMethodDef readline_methods[] =
{
{"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
{"get_line_buffer", get_line_buffer,
METH_OLDARGS, doc_get_line_buffer},
{"insert_text", insert_text, METH_VARARGS, doc_insert_text},
{"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
{"read_history_file", read_history_file,
METH_VARARGS, doc_read_history_file},
{"write_history_file", write_history_file,
METH_VARARGS, doc_write_history_file},
{"set_history_length", set_history_length,
METH_VARARGS, set_history_length_doc},
{"get_history_length", get_history_length,
METH_VARARGS, get_history_length_doc},
{"set_completer", set_completer, METH_VARARGS, doc_set_completer},
{"get_begidx", get_begidx, METH_OLDARGS, doc_get_begidx},
{"get_endidx", get_endidx, METH_OLDARGS, doc_get_endidx},
{"set_completer_delims", set_completer_delims,
METH_VARARGS, doc_set_completer_delims},
{"add_history", py_add_history, METH_VARARGS, doc_add_history},
{"get_completer_delims", get_completer_delims,
METH_OLDARGS, doc_get_completer_delims},
{"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook},
#ifdef HAVE_RL_PRE_INPUT_HOOK
{"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook},
#endif
{0, 0}
};
/* C function to call the Python hooks. */
static int
on_hook(PyObject *func, PyThreadState *tstate)
{
int result = 0;
if (func != NULL) {
PyObject *r;
PyThreadState *save_tstate;
/* Note that readline is called with the interpreter
lock released! */
save_tstate = PyThreadState_Swap(NULL);
PyEval_RestoreThread(tstate);
r = PyObject_CallFunction(func, NULL);
if (r == NULL)
goto error;
if (r == Py_None)
result = 0;
else
result = PyInt_AsLong(r);
Py_DECREF(r);
goto done;
error:
PyErr_Clear();
Py_XDECREF(r);
done:
PyEval_SaveThread();
PyThreadState_Swap(save_tstate);
}
return result;
}
static int
on_startup_hook(void)
{
return on_hook(startup_hook, startup_hook_tstate);
}
#ifdef HAVE_RL_PRE_INPUT_HOOK
static int
on_pre_input_hook(void)
{
return on_hook(pre_input_hook, pre_input_hook_tstate);
}
#endif
/* C function to call the Python completer. */
static char *
on_completion(char *text, int state)
{
char *result = NULL;
if (completer != NULL) {
PyObject *r;
PyThreadState *save_tstate;
/* Note that readline is called with the interpreter
lock released! */
save_tstate = PyThreadState_Swap(NULL);
PyEval_RestoreThread(completer_tstate);
r = PyObject_CallFunction(completer, "si", text, state);
if (r == NULL)
goto error;
if (r == Py_None) {
result = NULL;
}
else {
char *s = PyString_AsString(r);
if (s == NULL)
goto error;
result = strdup(s);
}
Py_DECREF(r);
goto done;
error:
PyErr_Clear();
Py_XDECREF(r);
done:
PyEval_SaveThread();
PyThreadState_Swap(save_tstate);
}
return result;
}
/* a more flexible constructor that saves the "begidx" and "endidx"
* before calling the normal completer */
char **
flex_complete(char *text, int start, int end)
{
Py_XDECREF(begidx);
Py_XDECREF(endidx);
begidx = PyInt_FromLong((long) start);
endidx = PyInt_FromLong((long) end);
return completion_matches(text, *on_completion);
}
/* Helper to initialize GNU readline properly. */
static void
setup_readline(void)
{
rl_readline_name = "python";
/* Force rebind of TAB to insert-tab */
rl_bind_key('\t', rl_insert);
/* Bind both ESC-TAB and ESC-ESC to the completion function */
rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
/* Set our hook functions */
rl_startup_hook = (Function *)on_startup_hook;
#ifdef HAVE_RL_PRE_INPUT_HOOK
rl_pre_input_hook = (Function *)on_pre_input_hook;
#endif
/* Set our completion function */
rl_attempted_completion_function = (CPPFunction *)flex_complete;
/* Set Python word break characters */
rl_completer_word_break_characters =
strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
/* All nonalphanums except '.' */
rl_completion_append_character ='\0';
begidx = PyInt_FromLong(0L);
endidx = PyInt_FromLong(0L);
/* Initialize (allows .inputrc to override)
*
* XXX: A bug in the readline-2.2 library causes a memory leak
* inside this function. Nothing we can do about it.
*/
rl_initialize();
}
/* Interrupt handler */
static jmp_buf jbuf;
/* ARGSUSED */
static void
onintr(int sig)
{
longjmp(jbuf, 1);
}
/* Wrapper around GNU readline that handles signals differently. */
static char *
call_readline(char *prompt)
{
size_t n;
char *p, *q;
PyOS_sighandler_t old_inthandler;
old_inthandler = PyOS_setsig(SIGINT, onintr);
if (setjmp(jbuf)) {
#ifdef HAVE_SIGRELSE
/* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
sigrelse(SIGINT);
#endif
PyOS_setsig(SIGINT, old_inthandler);
return NULL;
}
rl_event_hook = PyOS_InputHook;
p = readline(prompt);
PyOS_setsig(SIGINT, old_inthandler);
/* We must return a buffer allocated with PyMem_Malloc. */
if (p == NULL) {
p = PyMem_Malloc(1);
if (p != NULL)
*p = '\0';
return p;
}
n = strlen(p);
if (n > 0)
add_history(p);
/* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
release the original. */
q = p;
p = PyMem_Malloc(n+2);
if (p != NULL) {
strncpy(p, q, n);
p[n] = '\n';
p[n+1] = '\0';
}
free(q);
return p;
}
/* Initialize the module */
static char doc_module[] =
"Importing this module enables command line editing using GNU readline.";
DL_EXPORT(void)
initreadline(void)
{
PyObject *m;
m = Py_InitModule4("readline", readline_methods, doc_module,
(PyObject *)NULL, PYTHON_API_VERSION);
if (isatty(fileno(stdin))) {
PyOS_ReadlineFunctionPointer = call_readline;
setup_readline();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -