diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Freadline.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Freadline.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Freadline.html | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Freadline.html b/devdocs/python~3.12/library%2Freadline.html new file mode 100644 index 00000000..149d3d7c --- /dev/null +++ b/devdocs/python~3.12/library%2Freadline.html @@ -0,0 +1,136 @@ + <span id="readline-gnu-readline-interface"></span><h1>readline — GNU readline interface</h1> <p>The <a class="reference internal" href="#module-readline" title="readline: GNU readline support for Python. (Unix)"><code>readline</code></a> module defines a number of functions to facilitate completion and reading/writing of history files from the Python interpreter. This module can be used directly, or via the <a class="reference internal" href="rlcompleter#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><code>rlcompleter</code></a> module, which supports completion of Python identifiers at the interactive prompt. Settings made using this module affect the behaviour of both the interpreter’s interactive prompt and the prompts offered by the built-in <a class="reference internal" href="functions#input" title="input"><code>input()</code></a> function.</p> <p>Readline keybindings may be configured via an initialization file, typically <code>.inputrc</code> in your home directory. See <a class="reference external" href="https://tiswww.cwru.edu/php/chet/readline/rluserman.html#Readline-Init-File">Readline Init File</a> in the GNU Readline manual for information about the format and allowable constructs of that file, and the capabilities of the Readline library in general.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The underlying Readline library API may be implemented by the <code>libedit</code> library instead of GNU readline. On macOS the <a class="reference internal" href="#module-readline" title="readline: GNU readline support for Python. (Unix)"><code>readline</code></a> module detects which library is being used at run time.</p> <p>The configuration file for <code>libedit</code> is different from that of GNU readline. If you programmatically load configuration strings you can check for the text “libedit” in <code>readline.__doc__</code> to differentiate between GNU readline and libedit.</p> <p>If you use <em>editline</em>/<code>libedit</code> readline emulation on macOS, the initialization file located in your home directory is named <code>.editrc</code>. For example, the following content in <code>~/.editrc</code> will turn ON <em>vi</em> keybindings and TAB completion:</p> <pre data-language="python">python:bind -v +python:bind ^I rl_complete +</pre> </div> <section id="init-file"> <h2>Init file</h2> <p>The following functions relate to the init file and user configuration:</p> <dl class="py function"> <dt class="sig sig-object py" id="readline.parse_and_bind"> +<code>readline.parse_and_bind(string)</code> </dt> <dd> +<p>Execute the init line provided in the <em>string</em> argument. This calls <code>rl_parse_and_bind()</code> in the underlying library.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.read_init_file"> +<code>readline.read_init_file([filename])</code> </dt> <dd> +<p>Execute a readline initialization file. The default filename is the last filename used. This calls <code>rl_read_init_file()</code> in the underlying library.</p> </dd> +</dl> </section> <section id="line-buffer"> <h2>Line buffer</h2> <p>The following functions operate on the line buffer:</p> <dl class="py function"> <dt class="sig sig-object py" id="readline.get_line_buffer"> +<code>readline.get_line_buffer()</code> </dt> <dd> +<p>Return the current contents of the line buffer (<code>rl_line_buffer</code> in the underlying library).</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.insert_text"> +<code>readline.insert_text(string)</code> </dt> <dd> +<p>Insert text into the line buffer at the cursor position. This calls <code>rl_insert_text()</code> in the underlying library, but ignores the return value.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.redisplay"> +<code>readline.redisplay()</code> </dt> <dd> +<p>Change what’s displayed on the screen to reflect the current contents of the line buffer. This calls <code>rl_redisplay()</code> in the underlying library.</p> </dd> +</dl> </section> <section id="history-file"> <h2>History file</h2> <p>The following functions operate on a history file:</p> <dl class="py function"> <dt class="sig sig-object py" id="readline.read_history_file"> +<code>readline.read_history_file([filename])</code> </dt> <dd> +<p>Load a readline history file, and append it to the history list. The default filename is <code>~/.history</code>. This calls <code>read_history()</code> in the underlying library.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.write_history_file"> +<code>readline.write_history_file([filename])</code> </dt> <dd> +<p>Save the history list to a readline history file, overwriting any existing file. The default filename is <code>~/.history</code>. This calls <code>write_history()</code> in the underlying library.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.append_history_file"> +<code>readline.append_history_file(nelements[, filename])</code> </dt> <dd> +<p>Append the last <em>nelements</em> items of history to a file. The default filename is <code>~/.history</code>. The file must already exist. This calls <code>append_history()</code> in the underlying library. This function only exists if Python was compiled for a version of the library that supports it.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.get_history_length"> +<code>readline.get_history_length()</code> </dt> <dt class="sig sig-object py" id="readline.set_history_length"> +<code>readline.set_history_length(length)</code> </dt> <dd> +<p>Set or return the desired number of lines to save in the history file. The <a class="reference internal" href="#readline.write_history_file" title="readline.write_history_file"><code>write_history_file()</code></a> function uses this value to truncate the history file, by calling <code>history_truncate_file()</code> in the underlying library. Negative values imply unlimited history file size.</p> </dd> +</dl> </section> <section id="history-list"> <h2>History list</h2> <p>The following functions operate on a global history list:</p> <dl class="py function"> <dt class="sig sig-object py" id="readline.clear_history"> +<code>readline.clear_history()</code> </dt> <dd> +<p>Clear the current history. This calls <code>clear_history()</code> in the underlying library. The Python function only exists if Python was compiled for a version of the library that supports it.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.get_current_history_length"> +<code>readline.get_current_history_length()</code> </dt> <dd> +<p>Return the number of items currently in the history. (This is different from <a class="reference internal" href="#readline.get_history_length" title="readline.get_history_length"><code>get_history_length()</code></a>, which returns the maximum number of lines that will be written to a history file.)</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.get_history_item"> +<code>readline.get_history_item(index)</code> </dt> <dd> +<p>Return the current contents of history item at <em>index</em>. The item index is one-based. This calls <code>history_get()</code> in the underlying library.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.remove_history_item"> +<code>readline.remove_history_item(pos)</code> </dt> <dd> +<p>Remove history item specified by its position from the history. The position is zero-based. This calls <code>remove_history()</code> in the underlying library.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.replace_history_item"> +<code>readline.replace_history_item(pos, line)</code> </dt> <dd> +<p>Replace history item specified by its position with <em>line</em>. The position is zero-based. This calls <code>replace_history_entry()</code> in the underlying library.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.add_history"> +<code>readline.add_history(line)</code> </dt> <dd> +<p>Append <em>line</em> to the history buffer, as if it was the last line typed. This calls <code>add_history()</code> in the underlying library.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.set_auto_history"> +<code>readline.set_auto_history(enabled)</code> </dt> <dd> +<p>Enable or disable automatic calls to <code>add_history()</code> when reading input via readline. The <em>enabled</em> argument should be a Boolean value that when true, enables auto history, and that when false, disables auto history.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> Auto history is enabled by default, and changes to this do not persist across multiple sessions.</p> </div> </dd> +</dl> </section> <section id="startup-hooks"> <h2>Startup hooks</h2> <dl class="py function"> <dt class="sig sig-object py" id="readline.set_startup_hook"> +<code>readline.set_startup_hook([function])</code> </dt> <dd> +<p>Set or remove the function invoked by the <code>rl_startup_hook</code> callback of the underlying library. If <em>function</em> is specified, it will be used as the new hook function; if omitted or <code>None</code>, any function already installed is removed. The hook is called with no arguments just before readline prints the first prompt.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.set_pre_input_hook"> +<code>readline.set_pre_input_hook([function])</code> </dt> <dd> +<p>Set or remove the function invoked by the <code>rl_pre_input_hook</code> callback of the underlying library. If <em>function</em> is specified, it will be used as the new hook function; if omitted or <code>None</code>, any function already installed is removed. The hook is called with no arguments after the first prompt has been printed and just before readline starts reading input characters. This function only exists if Python was compiled for a version of the library that supports it.</p> </dd> +</dl> </section> <section id="completion"> <span id="readline-completion"></span><h2>Completion</h2> <p>The following functions relate to implementing a custom word completion function. This is typically operated by the Tab key, and can suggest and automatically complete a word being typed. By default, Readline is set up to be used by <a class="reference internal" href="rlcompleter#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><code>rlcompleter</code></a> to complete Python identifiers for the interactive interpreter. If the <a class="reference internal" href="#module-readline" title="readline: GNU readline support for Python. (Unix)"><code>readline</code></a> module is to be used with a custom completer, a different set of word delimiters should be set.</p> <dl class="py function"> <dt class="sig sig-object py" id="readline.set_completer"> +<code>readline.set_completer([function])</code> </dt> <dd> +<p>Set or remove the completer function. If <em>function</em> is specified, it will be used as the new completer function; if omitted or <code>None</code>, any completer function already installed is removed. The completer function is called as <code>function(text, state)</code>, for <em>state</em> in <code>0</code>, <code>1</code>, <code>2</code>, …, until it returns a non-string value. It should return the next possible completion starting with <em>text</em>.</p> <p>The installed completer function is invoked by the <em>entry_func</em> callback passed to <code>rl_completion_matches()</code> in the underlying library. The <em>text</em> string comes from the first parameter to the <code>rl_attempted_completion_function</code> callback of the underlying library.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.get_completer"> +<code>readline.get_completer()</code> </dt> <dd> +<p>Get the completer function, or <code>None</code> if no completer function has been set.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.get_completion_type"> +<code>readline.get_completion_type()</code> </dt> <dd> +<p>Get the type of completion being attempted. This returns the <code>rl_completion_type</code> variable in the underlying library as an integer.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.get_begidx"> +<code>readline.get_begidx()</code> </dt> <dt class="sig sig-object py" id="readline.get_endidx"> +<code>readline.get_endidx()</code> </dt> <dd> +<p>Get the beginning or ending index of the completion scope. These indexes are the <em>start</em> and <em>end</em> arguments passed to the <code>rl_attempted_completion_function</code> callback of the underlying library. The values may be different in the same input editing scenario based on the underlying C readline implementation. Ex: libedit is known to behave differently than libreadline.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.set_completer_delims"> +<code>readline.set_completer_delims(string)</code> </dt> <dt class="sig sig-object py" id="readline.get_completer_delims"> +<code>readline.get_completer_delims()</code> </dt> <dd> +<p>Set or get the word delimiters for completion. These determine the start of the word to be considered for completion (the completion scope). These functions access the <code>rl_completer_word_break_characters</code> variable in the underlying library.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="readline.set_completion_display_matches_hook"> +<code>readline.set_completion_display_matches_hook([function])</code> </dt> <dd> +<p>Set or remove the completion display function. If <em>function</em> is specified, it will be used as the new completion display function; if omitted or <code>None</code>, any completion display function already installed is removed. This sets or clears the <code>rl_completion_display_matches_hook</code> callback in the underlying library. The completion display function is called as <code>function(substitution, [matches], longest_match_length)</code> once each time matches need to be displayed.</p> </dd> +</dl> </section> <section id="example"> <span id="readline-example"></span><h2>Example</h2> <p>The following example demonstrates how to use the <a class="reference internal" href="#module-readline" title="readline: GNU readline support for Python. (Unix)"><code>readline</code></a> module’s history reading and writing functions to automatically load and save a history file named <code>.python_history</code> from the user’s home directory. The code below would normally be executed automatically during interactive sessions from the user’s <span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONSTARTUP"><code>PYTHONSTARTUP</code></a> file.</p> <pre data-language="python">import atexit +import os +import readline + +histfile = os.path.join(os.path.expanduser("~"), ".python_history") +try: + readline.read_history_file(histfile) + # default history len is -1 (infinite), which may grow unruly + readline.set_history_length(1000) +except FileNotFoundError: + pass + +atexit.register(readline.write_history_file, histfile) +</pre> <p>This code is actually automatically run when Python is run in <a class="reference internal" href="../tutorial/interpreter#tut-interactive"><span class="std std-ref">interactive mode</span></a> (see <a class="reference internal" href="site#rlcompleter-config"><span class="std std-ref">Readline configuration</span></a>).</p> <p>The following example achieves the same goal but supports concurrent interactive sessions, by only appending the new history.</p> <pre data-language="python">import atexit +import os +import readline +histfile = os.path.join(os.path.expanduser("~"), ".python_history") + +try: + readline.read_history_file(histfile) + h_len = readline.get_current_history_length() +except FileNotFoundError: + open(histfile, 'wb').close() + h_len = 0 + +def save(prev_h_len, histfile): + new_h_len = readline.get_current_history_length() + readline.set_history_length(1000) + readline.append_history_file(new_h_len - prev_h_len, histfile) +atexit.register(save, h_len, histfile) +</pre> <p>The following example extends the <a class="reference internal" href="code#code.InteractiveConsole" title="code.InteractiveConsole"><code>code.InteractiveConsole</code></a> class to support history save/restore.</p> <pre data-language="python">import atexit +import code +import os +import readline + +class HistoryConsole(code.InteractiveConsole): + def __init__(self, locals=None, filename="<console>", + histfile=os.path.expanduser("~/.console-history")): + code.InteractiveConsole.__init__(self, locals, filename) + self.init_history(histfile) + + def init_history(self, histfile): + readline.parse_and_bind("tab: complete") + if hasattr(readline, "read_history_file"): + try: + readline.read_history_file(histfile) + except FileNotFoundError: + pass + atexit.register(self.save_history, histfile) + + def save_history(self, histfile): + readline.set_history_length(1000) + readline.write_history_file(histfile) +</pre> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/readline.html" class="_attribution-link">https://docs.python.org/3.12/library/readline.html</a> + </p> +</div> |
