Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 1 | \section{\module{readline} --- |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 2 | GNU readline interface} |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 3 | |
| 4 | \declaremodule{builtin}{readline} |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 5 | \platform{Unix} |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 6 | \sectionauthor{Skip Montanaro}{skip@mojam.com} |
Fred Drake | f8ca7d8 | 2000-10-10 17:03:45 +0000 | [diff] [blame] | 7 | \modulesynopsis{GNU readline support for Python.} |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 8 | |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 9 | |
| 10 | The \module{readline} module defines a number of functions used either |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 11 | directly or from the \refmodule{rlcompleter} module to facilitate |
| 12 | completion and history file read and write from the Python |
| 13 | interpreter. |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 14 | |
| 15 | The \module{readline} module defines the following functions: |
| 16 | |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 17 | |
Skip Montanaro | 0dc2310 | 2004-05-23 17:46:50 +0000 | [diff] [blame] | 18 | \begin{funcdesc}{readline}{\optional{prompt}} |
| 19 | Get a single line of input from the user. |
| 20 | \end{funcdesc} |
| 21 | |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 22 | \begin{funcdesc}{parse_and_bind}{string} |
| 23 | Parse and execute single line of a readline init file. |
| 24 | \end{funcdesc} |
| 25 | |
| 26 | \begin{funcdesc}{get_line_buffer}{} |
| 27 | Return the current contents of the line buffer. |
| 28 | \end{funcdesc} |
| 29 | |
| 30 | \begin{funcdesc}{insert_text}{string} |
| 31 | Insert text into the command line. |
| 32 | \end{funcdesc} |
| 33 | |
| 34 | \begin{funcdesc}{read_init_file}{\optional{filename}} |
| 35 | Parse a readline initialization file. |
| 36 | The default filename is the last filename used. |
| 37 | \end{funcdesc} |
| 38 | |
| 39 | \begin{funcdesc}{read_history_file}{\optional{filename}} |
| 40 | Load a readline history file. |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 41 | The default filename is \file{\~{}/.history}. |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 42 | \end{funcdesc} |
| 43 | |
| 44 | \begin{funcdesc}{write_history_file}{\optional{filename}} |
| 45 | Save a readline history file. |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 46 | The default filename is \file{\~{}/.history}. |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 47 | \end{funcdesc} |
| 48 | |
Martin v. Löwis | e7a9796 | 2003-09-20 16:08:33 +0000 | [diff] [blame] | 49 | \begin{funcdesc}{clear_history}{} |
| 50 | Clear the current history. (Note: this function is not available if |
| 51 | the installed version of GNU readline doesn't support it.) |
| 52 | \versionadded{2.4} |
| 53 | \end{funcdesc} |
| 54 | |
Skip Montanaro | 7cb1524 | 2000-07-19 16:56:26 +0000 | [diff] [blame] | 55 | \begin{funcdesc}{get_history_length}{} |
| 56 | Return the desired length of the history file. Negative values imply |
| 57 | unlimited history file size. |
| 58 | \end{funcdesc} |
| 59 | |
| 60 | \begin{funcdesc}{set_history_length}{length} |
| 61 | Set the number of lines to save in the history file. |
Fred Drake | 3fe9a98 | 2000-08-09 14:37:05 +0000 | [diff] [blame] | 62 | \function{write_history_file()} uses this value to truncate the |
| 63 | history file when saving. Negative values imply unlimited history |
| 64 | file size. |
Skip Montanaro | 7cb1524 | 2000-07-19 16:56:26 +0000 | [diff] [blame] | 65 | \end{funcdesc} |
| 66 | |
Phillip J. Eby | 5068c87 | 2004-05-04 19:20:22 +0000 | [diff] [blame] | 67 | \begin{funcdesc}{get_current_history_length}{} |
| 68 | Return the number of lines currently in the history. (This is different |
| 69 | from \function{get_history_length()}, which returns the maximum number of |
| 70 | lines that will be written to a history file.) \versionadded{2.3} |
| 71 | \end{funcdesc} |
| 72 | |
| 73 | \begin{funcdesc}{get_history_item}{index} |
| 74 | Return the current contents of history item at \var{index}. |
| 75 | \versionadded{2.3} |
| 76 | \end{funcdesc} |
| 77 | |
| 78 | \begin{funcdesc}{redisplay}{} |
| 79 | Change what's displayed on the screen to reflect the current contents |
| 80 | of the line buffer. \versionadded{2.3} |
| 81 | \end{funcdesc} |
| 82 | |
Martin v. Löwis | 0daad59 | 2001-09-30 21:09:59 +0000 | [diff] [blame] | 83 | \begin{funcdesc}{set_startup_hook}{\optional{function}} |
| 84 | Set or remove the startup_hook function. If \var{function} is specified, |
| 85 | it will be used as the new startup_hook function; if omitted or |
| 86 | \code{None}, any hook function already installed is removed. The |
| 87 | startup_hook function is called with no arguments just |
| 88 | before readline prints the first prompt. |
| 89 | \end{funcdesc} |
| 90 | |
| 91 | \begin{funcdesc}{set_pre_input_hook}{\optional{function}} |
| 92 | Set or remove the pre_input_hook function. If \var{function} is specified, |
| 93 | it will be used as the new pre_input_hook function; if omitted or |
| 94 | \code{None}, any hook function already installed is removed. The |
| 95 | pre_input_hook function is called with no arguments after the first prompt |
| 96 | has been printed and just before readline starts reading input characters. |
| 97 | \end{funcdesc} |
| 98 | |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 99 | \begin{funcdesc}{set_completer}{\optional{function}} |
Fred Drake | 905dc55 | 2001-08-01 21:42:45 +0000 | [diff] [blame] | 100 | Set or remove the completer function. If \var{function} is specified, |
| 101 | it will be used as the new completer function; if omitted or |
| 102 | \code{None}, any completer function already installed is removed. The |
| 103 | completer function is called as \code{\var{function}(\var{text}, |
| 104 | \var{state})}, for \var{state} in \code{0}, \code{1}, \code{2}, ..., |
| 105 | until it returns a non-string value. It should return the next |
| 106 | possible completion starting with \var{text}. |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 107 | \end{funcdesc} |
| 108 | |
Neal Norwitz | b7d1d3c | 2003-02-21 18:57:05 +0000 | [diff] [blame] | 109 | \begin{funcdesc}{get_completer}{} |
Michael W. Hudson | f5dd753 | 2003-02-21 20:11:09 +0000 | [diff] [blame] | 110 | Get the completer function, or \code{None} if no completer function |
| 111 | has been set. \versionadded{2.3} |
Neal Norwitz | b7d1d3c | 2003-02-21 18:57:05 +0000 | [diff] [blame] | 112 | \end{funcdesc} |
| 113 | |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 114 | \begin{funcdesc}{get_begidx}{} |
| 115 | Get the beginning index of the readline tab-completion scope. |
| 116 | \end{funcdesc} |
| 117 | |
| 118 | \begin{funcdesc}{get_endidx}{} |
| 119 | Get the ending index of the readline tab-completion scope. |
| 120 | \end{funcdesc} |
| 121 | |
| 122 | \begin{funcdesc}{set_completer_delims}{string} |
| 123 | Set the readline word delimiters for tab-completion. |
| 124 | \end{funcdesc} |
| 125 | |
| 126 | \begin{funcdesc}{get_completer_delims}{} |
| 127 | Get the readline word delimiters for tab-completion. |
| 128 | \end{funcdesc} |
| 129 | |
Guido van Rossum | b6c1d52 | 2001-10-19 01:18:43 +0000 | [diff] [blame] | 130 | \begin{funcdesc}{add_history}{line} |
| 131 | Append a line to the history buffer, as if it was the last line typed. |
| 132 | \end{funcdesc} |
| 133 | |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 134 | |
| 135 | \begin{seealso} |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 136 | \seemodule{rlcompleter}{Completion of Python identifiers at the |
| 137 | interactive prompt.} |
Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame] | 138 | \end{seealso} |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 139 | |
| 140 | |
| 141 | \subsection{Example \label{readline-example}} |
| 142 | |
| 143 | The following example demonstrates how to use the |
| 144 | \module{readline} module's history reading and writing functions to |
| 145 | automatically load and save a history file named \file{.pyhist} from |
| 146 | the user's home directory. The code below would normally be executed |
| 147 | automatically during interactive sessions from the user's |
| 148 | \envvar{PYTHONSTARTUP} file. |
| 149 | |
| 150 | \begin{verbatim} |
| 151 | import os |
| 152 | histfile = os.path.join(os.environ["HOME"], ".pyhist") |
| 153 | try: |
| 154 | readline.read_history_file(histfile) |
| 155 | except IOError: |
| 156 | pass |
| 157 | import atexit |
| 158 | atexit.register(readline.write_history_file, histfile) |
| 159 | del os, histfile |
| 160 | \end{verbatim} |
Skip Montanaro | 0dc2310 | 2004-05-23 17:46:50 +0000 | [diff] [blame] | 161 | |
Skip Montanaro | b98a8ba | 2004-05-23 19:06:41 +0000 | [diff] [blame^] | 162 | The following example extends the \class{code.InteractiveConsole} class to |
| 163 | support command line editing and history save/restore. |
| 164 | |
| 165 | \begin{verbatim} |
| 166 | import code |
| 167 | import readline |
| 168 | import atexit |
| 169 | import os |
| 170 | |
| 171 | class HistoryConsole(code.InteractiveConsole): |
| 172 | def __init__(self, locals=None, filename="<console>", |
| 173 | histfile=os.path.expanduser("~/.console-history")): |
| 174 | code.InteractiveConsole.__init__(self) |
| 175 | self.init_history(histfile) |
| 176 | |
| 177 | def init_history(self, histfile): |
| 178 | readline.parse_and_bind("tab: complete") |
| 179 | if hasattr(readline, "read_history_file"): |
| 180 | try: |
| 181 | readline.read_history_file(histfile) |
| 182 | except IOError: |
| 183 | pass |
| 184 | atexit.register(self.save_history, histfile) |
| 185 | |
| 186 | def raw_input(self, prompt=""): |
| 187 | line = readline.readline(prompt) |
| 188 | if line: |
| 189 | readline.add_history(line) |
| 190 | return line |
| 191 | |
| 192 | def save_history(self, histfile): |
| 193 | readline.write_history_file(histfile) |
| 194 | \end{verbatim} |