import termios from gi.repository import Gtk from gi.repository import GObject # Control keys are intercept directly (see DESIGN.md) CTRL = { "c": termios.VINTR, "z": termios.VSUSP, "d": termios.VEOF, } class Bindings(): stylesheet = b""" @binding-set saneterm-key-bindings { bind "u" { "kill-after-output" () }; bind "a" { "move-input-start" () }; bind "e" { "move-input-end" () }; bind "j" { "insert-at-cursor" ("\\n") }; bind "l" { "clear-view" () }; bind "f" { "toggle-search" () }; bind "w" { "delete-from-cursor" (word-ends, -1) }; bind "h" { "backspace" () }; bind "a" { "toggle-config" ("autoscroll") }; bind "w" { "toggle-config" ("wordwrap") }; bind "Up" { "history-entry" (1) }; bind "Down" { "history-entry" (-1) }; bind "Tab" { "tab-completion" () }; bind "Insert" { "paste-primary" () }; /* Since c is used for VINTR, unbind v */ unbind "v"; } * { -gtk-key-bindings: saneterm-key-bindings; } """ def __init__(self, widget): self.provider = Gtk.CssProvider() self.provider.load_from_data(self.stylesheet) style_ctx = widget.get_style_context() style_ctx.add_provider(self.provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) def add_bind(self, key, signal, arg): bindings = self.__binding_set() # Using Gtk.BindingEntry.add_signall() would be preferable # https://gitlab.gnome.org/GNOME/pygobject/-/issues/474 Gtk.BindingEntry().add_signal_from_string(bindings, F'bind "{key}" {{ "{signal}" ({arg}) }};') def __binding_set(self): return Gtk.BindingSet.find("saneterm-key-bindings")