Skip to content

Commit 1374212

Browse files
pythongh-102130: Support a tab completion for Libedit in cmd, site, and pdb.
1 parent 16c9415 commit 1374212

File tree

8 files changed

+47
-12
lines changed

8 files changed

+47
-12
lines changed

Doc/library/cmd.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ command interpreters. These are often useful for test harnesses, administrative
1515
tools, and prototypes that will later be wrapped in a more sophisticated
1616
interface.
1717

18-
.. class:: Cmd(completekey='tab', stdin=None, stdout=None)
18+
.. class:: Cmd(completekey=r'"\t"', stdin=None, stdout=None)
1919

2020
A :class:`Cmd` instance or subclass instance is a line-oriented interpreter
2121
framework. There is no good reason to instantiate :class:`Cmd` itself; rather,
2222
it's useful as a superclass of an interpreter class you define yourself in order
2323
to inherit :class:`Cmd`'s methods and encapsulate action methods.
2424

2525
The optional argument *completekey* is the :mod:`readline` name of a completion
26-
key; it defaults to :kbd:`Tab`. If *completekey* is not :const:`None` and
26+
key; it defaults to "\\t"(:kbd:`tab`). If *completekey* is not :const:`None` and
2727
:mod:`readline` is available, command completion is done automatically.
2828

2929
The optional arguments *stdin* and *stdout* specify the input and output file

Doc/library/pdb.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ The ``run*`` functions and :func:`set_trace` are aliases for instantiating the
183183
:class:`Pdb` class and calling the method of the same name. If you want to
184184
access further features, you have to do this yourself:
185185

186-
.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \
186+
.. class:: Pdb(completekey=r'"\t"', stdin=None, stdout=None, skip=None, \
187187
nosigint=False, readrc=True)
188188

189189
:class:`Pdb` is the debugger class.

Doc/library/readline.rst

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ made using this module affect the behaviour of both the interpreter's
1717
interactive prompt and the prompts offered by the built-in :func:`input`
1818
function.
1919

20+
Compatible keybindings
21+
----------------------
22+
23+
To support compatible keybindings between GNU readline and Libedit, use the
24+
following backslashed escape sequences or octal escape sequences in quotes. ::
25+
26+
\a bell
27+
\b backspace
28+
\f form feed
29+
\n newline
30+
\r carriage return
31+
\t horizontal tab
32+
\v vertical tab
33+
\nnn octal escape sequences
34+
2035
Readline keybindings may be configured via an initialization file, typically
2136
``.inputrc`` in your home directory. See `Readline Init File
2237
<https://tiswww.cwru.edu/php/chet/readline/rluserman.html#Readline-Init-File>`_
@@ -39,10 +54,10 @@ Readline library in general.
3954
If you use *editline*/``libedit`` readline emulation on macOS, the
4055
initialization file located in your home directory is named
4156
``.editrc``. For example, the following content in ``~/.editrc`` will
42-
turn ON *vi* keybindings and TAB completion::
57+
turn ON *vi* keybindings and "\\t"(:kbd:`tab`) completion::
4358

4459
python:bind -v
45-
python:bind ^I rl_complete
60+
python:bind "\t" rl_complete
4661

4762

4863
Init file
@@ -56,6 +71,17 @@ The following functions relate to the init file and user configuration:
5671
Execute the init line provided in the *string* argument. This calls
5772
:c:func:`rl_parse_and_bind` in the underlying library.
5873

74+
.. note::
75+
The syntax and command of *string* argument may be different depending
76+
on the readline library.
77+
78+
For GNU readline::
79+
80+
readline.parse_and_bind(r'"\t": complete')
81+
82+
For Libedit::
83+
84+
readline.parse_and_bind(r'bind "\t" rl_complete')
5985

6086
.. function:: read_init_file([filename])
6187

@@ -348,7 +374,11 @@ support history save/restore. ::
348374
self.init_history(histfile)
349375

350376
def init_history(self, histfile):
351-
readline.parse_and_bind("tab: complete")
377+
readline_doc = getattr(readline, '__doc__', '')
378+
if readline_doc is not None and 'libedit' in readline_doc:
379+
readline.parse_and_bind(r'bind "\t" rl_complete')
380+
else:
381+
readline.parse_and_bind(r'"\t": complete')
352382
if hasattr(readline, "read_history_file"):
353383
try:
354384
readline.read_history_file(histfile)

Doc/library/rlcompleter.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Example::
2121

2222
>>> import rlcompleter
2323
>>> import readline
24-
>>> readline.parse_and_bind("tab: complete")
24+
>>> readline.parse_and_bind(r'"\t": complete')
2525
>>> readline. <TAB PRESSED>
2626
readline.__doc__ readline.get_line_buffer( readline.read_init_file(
2727
readline.__file__ readline.insert_text( readline.set_completer(

Lib/cmd.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Cmd:
7373
nohelp = "*** No help on %s"
7474
use_rawinput = 1
7575

76-
def __init__(self, completekey='tab', stdin=None, stdout=None):
76+
def __init__(self, completekey=r'"\t"', stdin=None, stdout=None):
7777
"""Instantiate a line-oriented interpreter framework.
7878
7979
The optional argument 'completekey' is the readline name of a
@@ -108,7 +108,11 @@ def cmdloop(self, intro=None):
108108
import readline
109109
self.old_completer = readline.get_completer()
110110
readline.set_completer(self.complete)
111-
readline.parse_and_bind(self.completekey+": complete")
111+
readline_doc = getattr(readline, '__doc__', '')
112+
if readline_doc is not None and 'libedit' in readline_doc:
113+
readline.parse_and_bind("bind "+self.completekey+" rl_complete")
114+
else:
115+
readline.parse_and_bind(self.completekey+": complete")
112116
except ImportError:
113117
pass
114118
try:

Lib/pdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
209209

210210
_previous_sigint_handler = None
211211

212-
def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
212+
def __init__(self, completekey=r'"\t"', stdin=None, stdout=None, skip=None,
213213
nosigint=False, readrc=True):
214214
bdb.Bdb.__init__(self, skip=skip)
215215
cmd.Cmd.__init__(self, completekey, stdin, stdout)

Lib/site.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,9 @@ def register_readline():
446446
# completion key, so we set one first and then read the file.
447447
readline_doc = getattr(readline, '__doc__', '')
448448
if readline_doc is not None and 'libedit' in readline_doc:
449-
readline.parse_and_bind('bind ^I rl_complete')
449+
readline.parse_and_bind(r'bind "\t" rl_complete')
450450
else:
451-
readline.parse_and_bind('tab: complete')
451+
readline.parse_and_bind(r'"\t": complete')
452452

453453
try:
454454
readline.read_init_file()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ Thomas Holmes
784784
Craig Holmquist
785785
Philip Homburg
786786
Naofumi Honda
787+
Constantin Hong
787788
Weipeng Hong
788789
Jeffrey Honig
789790
Rob Hooft

0 commit comments

Comments
 (0)