From 183661d9b98b20b64c247d457565404374cf6cab Mon Sep 17 00:00:00 2001 From: "R. Bernstein" Date: Sun, 10 Nov 2024 08:54:47 -0500 Subject: [PATCH] Add trepan3k --edit-mode option (#67) --- trepan/inout/input.py | 23 ++++++++++++++++------- trepan/options.py | 22 +++++++++++++++++++++- trepan/processor/command/skip.py | 10 +++++----- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/trepan/inout/input.py b/trepan/inout/input.py index ae586f46..c36ae688 100644 --- a/trepan/inout/input.py +++ b/trepan/inout/input.py @@ -23,25 +23,34 @@ from trepan.inout import base as Mbase try: - from prompt_toolkit import PromptSession, HTML - from prompt_toolkit.styles import Style + from prompt_toolkit import HTML, PromptSession + from prompt_toolkit.enums import EditingMode from prompt_toolkit.history import FileHistory + from prompt_toolkit.styles import Style except: PromptSession = lambda history: None FileHistory = lambda history: None HTML = lambda string: string + class DebuggerUserInput(Mbase.DebuggerInputBase): """Debugger input connected to what we think of as a end-user input as opposed to a relay mechanism to another process. Input could be interactive terminal, but it might be file input.""" - def __init__(self, inp=None, opts=None): - - if opts and opts.get("readline") == "prompt_toolkit": - self.session = PromptSession(history=FileHistory(opts.get("histfile"))) + def __init__(self, inp=None, opts=dict()): + + self.edit_mode = opts.get("edit_mode", "emacs") + if opts.get("readline") == "prompt_toolkit": + prompt_editing_mode = ( + EditingMode.EMACS if self.edit_mode == "emacs" else EditingMode.VI + ) + self.session = PromptSession( + editing_mode=prompt_editing_mode, + enable_history_search=True, + history=FileHistory(opts.get("histfile")), + ) self.input = self.session.input - self.session.enable_history_search = True self.line_edit = True self.closed = False self.use_raw = False diff --git a/trepan/options.py b/trepan/options.py index 210f9f01..7959ff29 100644 --- a/trepan/options.py +++ b/trepan/options.py @@ -173,6 +173,14 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None): # action="store", type='string', # help="Write debugger's error output " # + "(stderr) to FILE") + optparser.add_option( + "--edit-mode", + default="emacs", + dest="edit_mode", + type="string", + help='input edit mode. This should be either "emacs" or "vi"', + ) + optparser.add_option( "-e", "--exec", @@ -347,7 +355,7 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None): dest="use_prompt_toolkit", action="store_true", default=True, - help="Try using prompt_toolkit", + help="Try using prompt_toolkit. This take precedence over the --gnu-readline option", ) optparser.add_option( "--no-prompt-toolkit", @@ -367,7 +375,18 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None): optparser.disable_interspersed_args() sys.argv = list(sys_argv) + + # Here is where we *parse* arguments (opts, sys.argv) = optparser.parse_args(sys_argv[1:]) + + if opts.edit_mode not in ("vi", "emacs"): + sys.stderr.write( + 'Option --editmode should be either "emacs" or "vi"; assuming "emacs".\n' + f'Got: "{opts.edit_mode}".\n' + ) + opts.edit_mode = "emacs" + + if hasattr(opts, "use_prompt_toolkit") and opts.use_prompt_toolkit: readline = "prompt_toolkit" elif hasattr(opts, "use_gnu_readline") and opts.use_gnu_readline: @@ -380,6 +399,7 @@ def process_options(pkg_version: str, sys_argv: str, option_list=None): "interface_opts": { "readline": readline, "debugger_name": "trepan3k", + "edit_mode": opts.edit_mode, } } diff --git a/trepan/processor/command/skip.py b/trepan/processor/command/skip.py index 1565775b..f8432fec 100644 --- a/trepan/processor/command/skip.py +++ b/trepan/processor/command/skip.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2009, 2013, 2015, 2020 Rocky Bernstein +# Copyright (C) 2009, 2013, 2015, 2020, 2024 Rocky Bernstein # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -17,7 +17,7 @@ # Our local modules from trepan.processor.command.base_cmd import DebuggerCommand from trepan.processor.cmdproc import print_location -from trepan.lib import bytecode as Mbytecode +from trepan.lib.bytecode import next_linestart class SkipCommand(DebuggerCommand): @@ -53,14 +53,14 @@ def run(self, args): if len(args) == 1: count = 1 else: - msg = "skip: expecting a number, got %s." % args[1] + msg = f"skip: expecting a number, got {args[1]}." count = self.proc.get_an_int(args[1], msg) pass co = self.proc.curframe.f_code offset = self.proc.curframe.f_lasti if count is None: return False - lineno = Mbytecode.next_linestart(co, offset, count) + lineno = next_linestart(co, offset, count) if lineno < 0: self.errmsg("No next line found") @@ -76,7 +76,7 @@ def run(self, args): ) print_location(self.proc) except ValueError as e: - self.errmsg("skip failed: %s" % e) + self.errmsg(f"skip failed: {e}") return False pass