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: soil-staging
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
3 changes: 3 additions & 0 deletions .windsurfrules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1. The Python parts of this project are written in Python 2.7. This includes the type annotations. Never use syntax or features that are specific to Python 3.
2. Prefer explicit boolean tests in the Python code. E.g. `if foo is not None` is better than `if foo`.
3. Assume MyPy for type checking.
34 changes: 26 additions & 8 deletions builtin/readline_osh.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
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,12 +49,16 @@ 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
readline = self.readline
Expand Down Expand Up @@ -79,16 +86,16 @@ 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: -" +
", -".join(self.exclusive_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 +149,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 @@ -173,6 +179,18 @@ def Run(self, cmd_val):

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):
"""Show interactive command history."""
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
5 changes: 2 additions & 3 deletions devtools/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ yapf-known() {

yapf-changed() {
branch="${1:-master}"
start="${2:-HEAD}"

#git diff --name-only .."$branch" '*.py'

git diff --name-only .."$branch" '*.py' \
git diff --name-only "$start".."$branch" '*.py' \
| xargs --no-run-if-empty -- $0 yapf-files
}

Expand Down
31 changes: 31 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 @@ -142,6 +143,36 @@ 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():
# type: () -> Optional[Readline]
Expand Down
Loading
Loading