Skip Montanaro | 510ca1d | 2000-07-06 03:25:26 +0000 | [diff] [blame^] | 1 | \section{\module{readline} --- |
| 2 | Expose GNU readline functionality to Python} |
| 3 | |
| 4 | \declaremodule{builtin}{readline} |
| 5 | \sectionauthor{Skip Montanaro}{skip@mojam.com} |
| 6 | |
| 7 | \modulesynopsis{GNU Readline in Python.} |
| 8 | |
| 9 | \platform{UNIX} |
| 10 | |
| 11 | The \module{readline} module defines a number of functions used either |
| 12 | directly or from the \module{rlcompleter} module to facilitate completion |
| 13 | and history file read and write from the Python interpreter. |
| 14 | |
| 15 | The \module{readline} module defines the following functions: |
| 16 | |
| 17 | \begin{funcdesc}{parse_and_bind}{string} |
| 18 | Parse and execute single line of a readline init file. |
| 19 | \end{funcdesc} |
| 20 | |
| 21 | \begin{funcdesc}{get_line_buffer}{} |
| 22 | Return the current contents of the line buffer. |
| 23 | \end{funcdesc} |
| 24 | |
| 25 | \begin{funcdesc}{insert_text}{string} |
| 26 | Insert text into the command line. |
| 27 | \end{funcdesc} |
| 28 | |
| 29 | \begin{funcdesc}{read_init_file}{\optional{filename}} |
| 30 | Parse a readline initialization file. |
| 31 | The default filename is the last filename used. |
| 32 | \end{funcdesc} |
| 33 | |
| 34 | \begin{funcdesc}{read_history_file}{\optional{filename}} |
| 35 | Load a readline history file. |
| 36 | The default filename is ~/.history. |
| 37 | \end{funcdesc} |
| 38 | |
| 39 | \begin{funcdesc}{write_history_file}{\optional{filename}} |
| 40 | Save a readline history file. |
| 41 | The default filename is ~/.history. |
| 42 | \end{funcdesc} |
| 43 | |
| 44 | \begin{funcdesc}{set_completer}{\optional{function}} |
| 45 | Set or remove the completer function. |
| 46 | The function is called as function(text, state), |
| 47 | \code{for i in [0, 1, 2, ...]} until it returns a non-string. |
| 48 | It should return the next possible completion starting with 'text'. |
| 49 | \end{funcdesc} |
| 50 | |
| 51 | \begin{funcdesc}{get_begidx}{} |
| 52 | Get the beginning index of the readline tab-completion scope. |
| 53 | \end{funcdesc} |
| 54 | |
| 55 | \begin{funcdesc}{get_endidx}{} |
| 56 | Get the ending index of the readline tab-completion scope. |
| 57 | \end{funcdesc} |
| 58 | |
| 59 | \begin{funcdesc}{set_completer_delims}{string} |
| 60 | Set the readline word delimiters for tab-completion. |
| 61 | \end{funcdesc} |
| 62 | |
| 63 | \begin{funcdesc}{get_completer_delims}{} |
| 64 | Get the readline word delimiters for tab-completion. |
| 65 | \end{funcdesc} |
| 66 | |
| 67 | \subsection{Example} |
| 68 | \nodename{Readline Example} |
| 69 | |
| 70 | The following example demonstrates how to use the \module{readline} module's |
| 71 | history reading and writing functions to automatically load and save a |
| 72 | history file named \code{.pyhist} from the user's home directory. The code |
| 73 | below would normally be executed automatically during interactive sessions |
| 74 | from the user's PYTHONSTARTUP file. |
| 75 | |
| 76 | \begin{verbatim} |
| 77 | >>> import os |
| 78 | >>> histfile = os.path.join(os.environ["HOME"], ".pyhist") |
| 79 | >>> try: |
| 80 | >>> readline.read_history_file(histfile) |
| 81 | >>> except IOError: |
| 82 | >>> pass |
| 83 | >>> import atexit |
| 84 | >>> atexit.register(readline.write_history_file, histfile) |
| 85 | >>> del os, histfile |
| 86 | \end{verbatim} |
| 87 | |
| 88 | \begin{seealso} |
| 89 | \seemodule{rlcompleter}{completion of Python identifiers} |
| 90 | \end{seealso} |