Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bind -x support #2197

Draft
wants to merge 2 commits into
base: bind-x-tests
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions builtin/readline_osh.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
from frontend.py_readline import Readline
from core import sh_init
from display import ui
from state import Mem

_ = log

import sys # REMOVE ME

class ctx_Keymap(object):

Expand All @@ -46,11 +48,14 @@ def __exit__(self, type, value, traceback):
class Bind(vm._Builtin):
"""Interactive interface to readline bindings"""

def __init__(self, readline, errfmt):
# type: (Optional[Readline], ui.ErrorFormatter) -> None
def __init__(self, readline, errfmt, mem):
# type: (Optional[Readline], ui.ErrorFormatter, Mem) -> None
self.readline = readline
self.errfmt = errfmt
self.mem = mem
self.exclusive_flags = ["q", "u", "r", "x", "f"]

readline.set_bind_shell_command_hook(lambda *args: self.bind_shell_command_hook(*args))

def Run(self, cmd_val):
# type: (cmd_value.Argv) -> int
Expand Down Expand Up @@ -79,15 +84,15 @@ def Run(self, cmd_val):
# print("\tFound flag: {0} with tag: {1}".format(flag, attrs.attrs[flag].tag()))
if found:
self.errfmt.Print_(
"error: can only use one of the following flags at a time: -"
"error: Can only use one of the following flags at a time: -"
+ ", -".join(self.exclusive_flags),
blame_loc=cmd_val.arg_locs[0])
return 1
else:
found = True
if found and not arg_r.AtEnd():
self.errfmt.Print_(
"error: cannot mix bind commands with the following flags: -" +
"error: Too many arguments. Check your quoting. Also, you cannot mix normal bindings with the following flags: -" +
", -".join(self.exclusive_flags),
blame_loc=cmd_val.arg_locs[0])
return 1
Expand Down Expand Up @@ -142,9 +147,8 @@ 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
# print("arg.x: %s" % arg.x)
readline.bind_shell_command(arg.x)

if arg.X:
readline.print_shell_cmd_map()
Expand Down Expand Up @@ -172,6 +176,18 @@ def Run(self, cmd_val):
return 1

return 0

def bind_shell_command_hook(self, cmd, line_buffer, point):
# type: (str, str, int) -> (int, str, str)
print("Executing cmd: %s" % cmd)
print("Setting READLINE_LINE to: %s" % line_buffer)
print("Setting READLINE_POINT to: %s" % point)
sys.stdout.flush()

self.mem

temp_return_code = 0
return (temp_return_code, line_buffer, str(point))


class History(vm._Builtin):
Expand Down
2 changes: 1 addition & 1 deletion core/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ def Main(
b[builtin_i.forkwait] = process_osh.ForkWait(shell_ex)

# Interactive builtins depend on readline
b[builtin_i.bind] = readline_osh.Bind(readline, errfmt)
b[builtin_i.bind] = readline_osh.Bind(readline, errfmt, mem)
b[builtin_i.history] = readline_osh.History(readline, sh_files, errfmt,
mylib.Stdout())

Expand Down
30 changes: 30 additions & 0 deletions frontend/py_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
if TYPE_CHECKING:
from core.completion import ReadlineCallback
from core.comp_ui import _IDisplay
from core.state import Mem


class Readline(object):
Expand Down Expand Up @@ -141,6 +142,35 @@ def print_shell_cmd_map(self):
def unbind_keyseq(self, keyseq):
# type: (str) -> None
line_input.unbind_keyseq(keyseq)

def bind_shell_command(self, bindseq):
# type: (str) -> None
import sys
print("default encoding: %s" % sys.getdefaultencoding())
cmdseq_split = bindseq.strip().split(":", 1)
if len(cmdseq_split) != 2:
raise ValueError("%s: missing colon separator" % bindseq)

# 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/invalid key sequence" % keyseq)
if keyseq[0] != '"' or keyseq[-1] != '"':
raise ValueError("%s: missing double-quotes around the key sequence" % keyseq)
keyseq = keyseq[1:-1]

cmd = cmdseq_split[1]
print("type of cmd string: %s" % type(cmd)) # REMOVE ME
line_input.bind_shell_command(keyseq, cmd)

def set_bind_shell_command_hook(self, hook):
# type: (Callable[[str, str, int], (int, str, str)]) -> None

if hook is None:
raise ValueError("missing bind shell command hook function")

line_input.set_bind_shell_command_hook(hook)


def MaybeGetReadline():
Expand Down
Loading
Loading