Kaydet (Commit) 74f3143d authored tarafından Guido van Rossum's avatar Guido van Rossum

Various cleanups:

- Whitespace normalization.

- Cleaned up some comments.

- Broke long lines.
üst 8b7b3453
...@@ -27,7 +27,8 @@ ...@@ -27,7 +27,8 @@
#include <readline/history.h> #include <readline/history.h>
#ifdef HAVE_RL_COMPLETION_MATCHES #ifdef HAVE_RL_COMPLETION_MATCHES
#define completion_matches(x, y) rl_completion_matches((x), ((rl_compentry_func_t *)(y))) #define completion_matches(x, y) \
rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
#endif #endif
/* Pointers needed from outside (but not declared in a header file). */ /* Pointers needed from outside (but not declared in a header file). */
...@@ -126,29 +127,27 @@ Save a readline history file.\n\ ...@@ -126,29 +127,27 @@ Save a readline history file.\n\
The default filename is ~/.history."); The default filename is ~/.history.");
PyDoc_STRVAR(set_history_length_doc, /* Set history length */
"set_history_length(length) -> None\n\
set the maximal number of items which will be written to\n\
the history file. A negative length is used to inhibit\n\
history truncation.");
static PyObject* static PyObject*
set_history_length(PyObject *self, PyObject *args) set_history_length(PyObject *self, PyObject *args)
{ {
int length = history_length; int length = history_length;
if (!PyArg_ParseTuple(args, "i:set_history_length", &length)) if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
return NULL; return NULL;
history_length = length; history_length = length;
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
PyDoc_STRVAR(set_history_length_doc,
"set_history_length(length) -> None\n\
set the maximal number of items which will be written to\n\
the history file. A negative length is used to inhibit\n\
history truncation.");
PyDoc_STRVAR(get_history_length_doc, /* Get history length */
"get_history_length() -> int\n\
return the maximum number of items that will be written to\n\
the history file.");
static PyObject* static PyObject*
get_history_length(PyObject *self, PyObject *args) get_history_length(PyObject *self, PyObject *args)
...@@ -158,10 +157,17 @@ get_history_length(PyObject *self, PyObject *args) ...@@ -158,10 +157,17 @@ get_history_length(PyObject *self, PyObject *args)
return Py_BuildValue("i", history_length); return Py_BuildValue("i", history_length);
} }
PyDoc_STRVAR(get_history_length_doc,
"get_history_length() -> int\n\
return the maximum number of items that will be written to\n\
the history file.");
/* Generic hook function setter */ /* Generic hook function setter */
static PyObject * static PyObject *
set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyObject *args) set_hook(const char *funcname, PyObject **hook_var,
PyThreadState **tstate, PyObject *args)
{ {
PyObject *function = Py_None; PyObject *function = Py_None;
char buf[80]; char buf[80];
...@@ -191,6 +197,7 @@ set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyO ...@@ -191,6 +197,7 @@ set_hook(const char * funcname, PyObject **hook_var, PyThreadState **tstate, PyO
return Py_None; return Py_None;
} }
/* Exported functions to specify hook functions in Python */ /* Exported functions to specify hook functions in Python */
static PyObject *startup_hook = NULL; static PyObject *startup_hook = NULL;
...@@ -204,7 +211,8 @@ static PyThreadState *pre_input_hook_tstate = NULL; ...@@ -204,7 +211,8 @@ static PyThreadState *pre_input_hook_tstate = NULL;
static PyObject * static PyObject *
set_startup_hook(PyObject *self, PyObject *args) set_startup_hook(PyObject *self, PyObject *args)
{ {
return set_hook("startup_hook", &startup_hook, &startup_hook_tstate, args); return set_hook("startup_hook", &startup_hook,
&startup_hook_tstate, args);
} }
PyDoc_STRVAR(doc_set_startup_hook, PyDoc_STRVAR(doc_set_startup_hook,
...@@ -213,11 +221,16 @@ Set or remove the startup_hook function.\n\ ...@@ -213,11 +221,16 @@ Set or remove the startup_hook function.\n\
The function is called with no arguments just\n\ The function is called with no arguments just\n\
before readline prints the first prompt."); before readline prints the first prompt.");
#ifdef HAVE_RL_PRE_INPUT_HOOK #ifdef HAVE_RL_PRE_INPUT_HOOK
/* Set pre-input hook */
static PyObject * static PyObject *
set_pre_input_hook(PyObject *self, PyObject *args) set_pre_input_hook(PyObject *self, PyObject *args)
{ {
return set_hook("pre_input_hook", &pre_input_hook, &pre_input_hook_tstate, args); return set_hook("pre_input_hook", &pre_input_hook,
&pre_input_hook_tstate, args);
} }
PyDoc_STRVAR(doc_set_pre_input_hook, PyDoc_STRVAR(doc_set_pre_input_hook,
...@@ -226,8 +239,10 @@ Set or remove the pre_input_hook function.\n\ ...@@ -226,8 +239,10 @@ Set or remove the pre_input_hook function.\n\
The function is called with no arguments after the first prompt\n\ The function is called with no arguments after the first prompt\n\
has been printed and just before readline starts reading input\n\ has been printed and just before readline starts reading input\n\
characters."); characters.");
#endif #endif
/* Exported function to specify a word completer in Python */ /* Exported function to specify a word completer in Python */
static PyObject *completer = NULL; static PyObject *completer = NULL;
...@@ -236,7 +251,9 @@ static PyThreadState *completer_tstate = NULL; ...@@ -236,7 +251,9 @@ static PyThreadState *completer_tstate = NULL;
static PyObject *begidx = NULL; static PyObject *begidx = NULL;
static PyObject *endidx = NULL; static PyObject *endidx = NULL;
/* get the beginning index for the scope of the tab-completion */
/* Get the beginning index for the scope of the tab-completion */
static PyObject * static PyObject *
get_begidx(PyObject *self) get_begidx(PyObject *self)
{ {
...@@ -248,7 +265,9 @@ PyDoc_STRVAR(doc_get_begidx, ...@@ -248,7 +265,9 @@ PyDoc_STRVAR(doc_get_begidx,
"get_begidx() -> int\n\ "get_begidx() -> int\n\
get the beginning index of the readline tab-completion scope"); get the beginning index of the readline tab-completion scope");
/* get the ending index for the scope of the tab-completion */
/* Get the ending index for the scope of the tab-completion */
static PyObject * static PyObject *
get_endidx(PyObject *self) get_endidx(PyObject *self)
{ {
...@@ -261,7 +280,7 @@ PyDoc_STRVAR(doc_get_endidx, ...@@ -261,7 +280,7 @@ PyDoc_STRVAR(doc_get_endidx,
get the ending index of the readline tab-completion scope"); get the ending index of the readline tab-completion scope");
/* set the tab-completion word-delimiters that readline uses */ /* Set the tab-completion word-delimiters that readline uses */
static PyObject * static PyObject *
set_completer_delims(PyObject *self, PyObject *args) set_completer_delims(PyObject *self, PyObject *args)
...@@ -281,6 +300,9 @@ PyDoc_STRVAR(doc_set_completer_delims, ...@@ -281,6 +300,9 @@ PyDoc_STRVAR(doc_set_completer_delims,
"set_completer_delims(string) -> None\n\ "set_completer_delims(string) -> None\n\
set the readline word delimiters for tab-completion"); set the readline word delimiters for tab-completion");
/* Add a line to the history buffer */
static PyObject * static PyObject *
py_add_history(PyObject *self, PyObject *args) py_add_history(PyObject *self, PyObject *args)
{ {
...@@ -299,18 +321,21 @@ PyDoc_STRVAR(doc_add_history, ...@@ -299,18 +321,21 @@ PyDoc_STRVAR(doc_add_history,
add a line to the history buffer"); add a line to the history buffer");
/* get the tab-completion word-delimiters that readline uses */ /* Get the tab-completion word-delimiters that readline uses */
static PyObject * static PyObject *
get_completer_delims(PyObject *self) get_completer_delims(PyObject *self)
{ {
return PyString_FromString(rl_completer_word_break_characters); return PyString_FromString(rl_completer_word_break_characters);
} }
PyDoc_STRVAR(doc_get_completer_delims, PyDoc_STRVAR(doc_get_completer_delims,
"get_completer_delims() -> string\n\ "get_completer_delims() -> string\n\
get the readline word delimiters for tab-completion"); get the readline word delimiters for tab-completion");
/* Set the completer function */
static PyObject * static PyObject *
set_completer(PyObject *self, PyObject *args) set_completer(PyObject *self, PyObject *args)
{ {
...@@ -324,6 +349,7 @@ The function is called as function(text, state),\n\ ...@@ -324,6 +349,7 @@ The function is called as function(text, state),\n\
for state in 0, 1, 2, ..., until it returns a non-string.\n\ for state in 0, 1, 2, ..., until it returns a non-string.\n\
It should return the next possible completion starting with 'text'."); It should return the next possible completion starting with 'text'.");
/* Exported function to get any element of history */ /* Exported function to get any element of history */
static PyObject * static PyObject *
...@@ -346,6 +372,7 @@ PyDoc_STRVAR(doc_get_history_item, ...@@ -346,6 +372,7 @@ PyDoc_STRVAR(doc_get_history_item,
"get_history_item() -> string\n\ "get_history_item() -> string\n\
return the current contents of history item at index."); return the current contents of history item at index.");
/* Exported function to get current length of history */ /* Exported function to get current length of history */
static PyObject * static PyObject *
...@@ -361,6 +388,7 @@ PyDoc_STRVAR(doc_get_current_history_length, ...@@ -361,6 +388,7 @@ PyDoc_STRVAR(doc_get_current_history_length,
"get_current_history_length() -> integer\n\ "get_current_history_length() -> integer\n\
return the current (not the maximum) length of history."); return the current (not the maximum) length of history.");
/* Exported function to read the current line buffer */ /* Exported function to read the current line buffer */
static PyObject * static PyObject *
...@@ -373,6 +401,7 @@ PyDoc_STRVAR(doc_get_line_buffer, ...@@ -373,6 +401,7 @@ PyDoc_STRVAR(doc_get_line_buffer,
"get_line_buffer() -> string\n\ "get_line_buffer() -> string\n\
return the current contents of the line buffer."); return the current contents of the line buffer.");
/* Exported function to insert text into the line buffer */ /* Exported function to insert text into the line buffer */
static PyObject * static PyObject *
...@@ -390,6 +419,9 @@ PyDoc_STRVAR(doc_insert_text, ...@@ -390,6 +419,9 @@ PyDoc_STRVAR(doc_insert_text,
"insert_text(string) -> None\n\ "insert_text(string) -> None\n\
Insert text into the command line."); Insert text into the command line.");
/* Redisplay the line buffer */
static PyObject * static PyObject *
redisplay(PyObject *self) redisplay(PyObject *self)
{ {
...@@ -403,41 +435,44 @@ PyDoc_STRVAR(doc_redisplay, ...@@ -403,41 +435,44 @@ PyDoc_STRVAR(doc_redisplay,
Change what's displayed on the screen to reflect the current\n\ Change what's displayed on the screen to reflect the current\n\
contents of the line buffer."); contents of the line buffer.");
/* Table of functions exported by the module */ /* Table of functions exported by the module */
static struct PyMethodDef readline_methods[] = static struct PyMethodDef readline_methods[] =
{ {
{"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind}, {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
{"get_line_buffer", (PyCFunction)get_line_buffer, {"get_line_buffer", (PyCFunction)get_line_buffer,
METH_NOARGS, doc_get_line_buffer}, METH_NOARGS, doc_get_line_buffer},
{"insert_text", insert_text, METH_VARARGS, doc_insert_text}, {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
{"redisplay", (PyCFunction)redisplay, METH_NOARGS, doc_redisplay}, {"redisplay", (PyCFunction)redisplay, METH_NOARGS, doc_redisplay},
{"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file}, {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
{"read_history_file", read_history_file, {"read_history_file", read_history_file,
METH_VARARGS, doc_read_history_file}, METH_VARARGS, doc_read_history_file},
{"write_history_file", write_history_file, {"write_history_file", write_history_file,
METH_VARARGS, doc_write_history_file}, METH_VARARGS, doc_write_history_file},
{"get_history_item", get_history_item, {"get_history_item", get_history_item,
METH_VARARGS, doc_get_history_item}, METH_VARARGS, doc_get_history_item},
{"get_current_history_length", (PyCFunction)get_current_history_length, {"get_current_history_length", (PyCFunction)get_current_history_length,
METH_NOARGS, doc_get_current_history_length}, METH_NOARGS, doc_get_current_history_length},
{"set_history_length", set_history_length, {"set_history_length", set_history_length,
METH_VARARGS, set_history_length_doc}, METH_VARARGS, set_history_length_doc},
{"get_history_length", get_history_length, {"get_history_length", get_history_length,
METH_VARARGS, get_history_length_doc}, METH_VARARGS, get_history_length_doc},
{"set_completer", set_completer, METH_VARARGS, doc_set_completer}, {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
{"get_begidx", (PyCFunction)get_begidx, METH_NOARGS, doc_get_begidx}, {"get_begidx", (PyCFunction)get_begidx, METH_NOARGS, doc_get_begidx},
{"get_endidx", (PyCFunction)get_endidx, METH_NOARGS, doc_get_endidx}, {"get_endidx", (PyCFunction)get_endidx, METH_NOARGS, doc_get_endidx},
{"set_completer_delims", set_completer_delims, {"set_completer_delims", set_completer_delims,
METH_VARARGS, doc_set_completer_delims}, METH_VARARGS, doc_set_completer_delims},
{"add_history", py_add_history, METH_VARARGS, doc_add_history}, {"add_history", py_add_history, METH_VARARGS, doc_add_history},
{"get_completer_delims", (PyCFunction)get_completer_delims, {"get_completer_delims", (PyCFunction)get_completer_delims,
METH_NOARGS, doc_get_completer_delims}, METH_NOARGS, doc_get_completer_delims},
{"set_startup_hook", set_startup_hook, METH_VARARGS, doc_set_startup_hook}, {"set_startup_hook", set_startup_hook,
METH_VARARGS, doc_set_startup_hook},
#ifdef HAVE_RL_PRE_INPUT_HOOK #ifdef HAVE_RL_PRE_INPUT_HOOK
{"set_pre_input_hook", set_pre_input_hook, METH_VARARGS, doc_set_pre_input_hook}, {"set_pre_input_hook", set_pre_input_hook,
METH_VARARGS, doc_set_pre_input_hook},
#endif #endif
{0, 0} {0, 0}
}; };
...@@ -458,9 +493,9 @@ on_hook(PyObject *func, PyThreadState *tstate) ...@@ -458,9 +493,9 @@ on_hook(PyObject *func, PyThreadState *tstate)
r = PyObject_CallFunction(func, NULL); r = PyObject_CallFunction(func, NULL);
if (r == NULL) if (r == NULL)
goto error; goto error;
if (r == Py_None) if (r == Py_None)
result = 0; result = 0;
else else
result = PyInt_AsLong(r); result = PyInt_AsLong(r);
Py_DECREF(r); Py_DECREF(r);
goto done; goto done;
...@@ -613,7 +648,7 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) ...@@ -613,7 +648,7 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
size_t n; size_t n;
char *p, *q; char *p, *q;
PyOS_sighandler_t old_inthandler; PyOS_sighandler_t old_inthandler;
old_inthandler = PyOS_setsig(SIGINT, onintr); old_inthandler = PyOS_setsig(SIGINT, onintr);
if (setjmp(jbuf)) { if (setjmp(jbuf)) {
#ifdef HAVE_SIGRELSE #ifdef HAVE_SIGRELSE
...@@ -625,14 +660,14 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) ...@@ -625,14 +660,14 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
} }
rl_event_hook = PyOS_InputHook; rl_event_hook = PyOS_InputHook;
if (sys_stdin != rl_instream || sys_stdout != rl_outstream) { if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
rl_instream = sys_stdin; rl_instream = sys_stdin;
rl_outstream = sys_stdout; rl_outstream = sys_stdout;
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
rl_prep_terminal (1); rl_prep_terminal (1);
#endif #endif
} }
p = readline(prompt); p = readline(prompt);
PyOS_setsig(SIGINT, old_inthandler); PyOS_setsig(SIGINT, old_inthandler);
...@@ -688,6 +723,6 @@ initreadline(void) ...@@ -688,6 +723,6 @@ initreadline(void)
m = Py_InitModule4("readline", readline_methods, doc_module, m = Py_InitModule4("readline", readline_methods, doc_module,
(PyObject *)NULL, PYTHON_API_VERSION); (PyObject *)NULL, PYTHON_API_VERSION);
PyOS_ReadlineFunctionPointer = call_readline; PyOS_ReadlineFunctionPointer = call_readline;
setup_readline(); setup_readline();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment