Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f75e0d

Browse files
committed
Add get_history_item, get_current_history_length, and redisplay functions.
Clarify the docstring for get_history_length. Closes SF patch 494066.
1 parent e085017 commit 0f75e0d

1 file changed

Lines changed: 61 additions & 2 deletions

File tree

Modules/readline.c

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ set_history_length(PyObject *self, PyObject *args)
143143

144144
static char get_history_length_doc[] = "\
145145
get_history_length() -> int\n\
146-
return the current history length value.\n\
146+
return the maximum number of items that will be written to\n\
147+
the history file.\n\
147148
";
148149

149150
static PyObject*
@@ -332,6 +333,47 @@ for state in 0, 1, 2, ..., until it returns a non-string.\n\
332333
It should return the next possible completion starting with 'text'.\
333334
";
334335

336+
/* Exported function to get any element of history */
337+
338+
static PyObject *
339+
get_history_item(PyObject *self, PyObject *args)
340+
{
341+
int idx = 0;
342+
HIST_ENTRY *hist_ent;
343+
344+
if (!PyArg_ParseTuple(args, "i:index", &idx))
345+
return NULL;
346+
if ((hist_ent = history_get(idx)))
347+
return PyString_FromString(hist_ent->line);
348+
else {
349+
Py_INCREF(Py_None);
350+
return Py_None;
351+
}
352+
}
353+
354+
static char doc_get_history_item[] = "\
355+
get_history_item() -> string\n\
356+
return the current contents of history item at index.\
357+
";
358+
359+
/* Exported function to get current length of history */
360+
361+
static PyObject *
362+
get_current_history_length(PyObject *self, PyObject *args)
363+
{
364+
HISTORY_STATE *hist_st;
365+
366+
if (!PyArg_NoArgs(args))
367+
return NULL;
368+
hist_st = history_get_history_state();
369+
return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
370+
}
371+
372+
static char doc_get_current_history_length[] = "\
373+
get_current_history_length() -> integer\n\
374+
return the current (not the maximum) length of history.\
375+
";
376+
335377
/* Exported function to read the current line buffer */
336378

337379
static PyObject *
@@ -360,12 +402,24 @@ insert_text(PyObject *self, PyObject *args)
360402
return Py_None;
361403
}
362404

363-
364405
static char doc_insert_text[] = "\
365406
insert_text(string) -> None\n\
366407
Insert text into the command line.\
367408
";
368409

410+
static PyObject *
411+
redisplay(PyObject *self)
412+
{
413+
rl_redisplay();
414+
Py_INCREF(Py_None);
415+
return Py_None;
416+
}
417+
418+
static char doc_redisplay[] = "\
419+
redisplay() -> None\n\
420+
Change what's displayed on the screen to reflect the current\n\
421+
contents of the line buffer.\
422+
";
369423

370424
/* Table of functions exported by the module */
371425

@@ -375,11 +429,16 @@ static struct PyMethodDef readline_methods[] =
375429
{"get_line_buffer", get_line_buffer,
376430
METH_OLDARGS, doc_get_line_buffer},
377431
{"insert_text", insert_text, METH_VARARGS, doc_insert_text},
432+
{"redisplay", (PyCFunction)redisplay, METH_NOARGS, doc_redisplay},
378433
{"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
379434
{"read_history_file", read_history_file,
380435
METH_VARARGS, doc_read_history_file},
381436
{"write_history_file", write_history_file,
382437
METH_VARARGS, doc_write_history_file},
438+
{"get_history_item", get_history_item,
439+
METH_VARARGS, doc_get_history_item},
440+
{"get_current_history_length", get_current_history_length,
441+
METH_OLDARGS, doc_get_current_history_length},
383442
{"set_history_length", set_history_length,
384443
METH_VARARGS, set_history_length_doc},
385444
{"get_history_length", get_history_length,

0 commit comments

Comments
 (0)