Skip to content

Commit

Permalink
Add bind -x skeleton code
Browse files Browse the repository at this point in the history
  • Loading branch information
KingMob committed Dec 29, 2024
1 parent a3e3607 commit fca2f8a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 1 addition & 3 deletions builtin/readline_osh.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ def Run(self, cmd_val):
readline.unbind_keyseq(arg.r)

if arg.x is not None:
self.errfmt.Print_("warning: bind -x isn't implemented",
blame_loc=cmd_val.arg_locs[0])
return 1
readline.bind_shell_command(arg.x)

if arg.X:
readline.print_shell_cmd_map()
Expand Down
18 changes: 18 additions & 0 deletions frontend/py_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ def print_shell_cmd_map(self):
def unbind_keyseq(self, keyseq):
# type: (str) -> None
line_input.unbind_keyseq(keyseq)

def bind_shell_command(self, cmdseq):
# type: (str) -> None
cmdseq_split = cmdseq.strip().split(":", 1)
if len(cmdseq_split) != 2:
raise ValueError("%s: missing colon separator" % cmdseq)

# Below checks prevent need to do so in C, but also ensure rl_generic_bind
# will not try to incorrectly xfree `cmd`/`data`, which doesn't belong to it
keyseq = cmdseq_split[0].rstrip()
if len(keyseq) <= 2:
raise ValueError("%s: empty binding key sequence" % keyseq)
if keyseq[0] != '"' or keyseq[-1] != '"':
raise ValueError("%s: missing double-quotes around the binding" % keyseq)
keyseq = keyseq[1:-1]

cmd = cmdseq_split[1]
line_input.bind_shell_command(keyseq, cmd)


def MaybeGetReadline():
Expand Down
1 change: 1 addition & 0 deletions pyext/line_input.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ def print_shell_cmd_map() -> None: ...

def unbind_keyseq(keyseq: str) -> None: ...

def bind_shell_command(keyseq: str, cmd: str) -> None: ...

0 comments on commit fca2f8a

Please sign in to comment.