def main():
    # We start with a `Registry` of default key bindings.
    bindings = KeyBindings()

    # Create the decorators to be used for registering text objects and
    # operators in this registry.
    operator = create_operator_decorator(bindings)
    text_object = create_text_object_decorator(bindings)

    # Create a custom operator.

    @operator("R")
    def _(event, text_object):
        "Custom operator that reverses text."
        buff = event.current_buffer

        # Get relative start/end coordinates.
        start, end = text_object.operator_range(buff.document)
        start += buff.cursor_position
        end += buff.cursor_position

        text = buff.text[start:end]
        text = "".join(reversed(text))

        event.app.current_buffer.text = buff.text[:start] + text + buff.text[
            end:]

    # Create a text object.

    @text_object("A")
    def _(event):
        "A custom text object that involves everything."
        # Note that a `TextObject` has coordinates, relative to the cursor position.
        buff = event.current_buffer
        return TextObject(
            -buff.document.cursor_position,  # The start.
            len(buff.text) - buff.document.cursor_position,
        )  # The end.

    # Read input.
    print('There is a custom text object "A" that applies to everything')
    print('and a custom operator "r" that reverses the text object.\n')

    print("Things that are possible:")
    print("-  Riw    - reverse inner word.")
    print("-  yA     - yank everything.")
    print("-  RA     - reverse everything.")

    text = prompt("> ",
                  default="hello world",
                  key_bindings=bindings,
                  editing_mode=EditingMode.VI)
    print("You said: %s" % text)
def main():
    # We start with a `Registry` of default key bindings.
    bindings = KeyBindings()

    # Create the decorators to be used for registering text objects and
    # operators in this registry.
    operator = create_operator_decorator(bindings)
    text_object = create_text_object_decorator(bindings)

    # Create a custom operator.

    @operator('R')
    def _(event, text_object):
        " Custom operator that reverses text. "
        buff = event.current_buffer

        # Get relative start/end coordinates.
        start, end = text_object.operator_range(buff.document)
        start += buff.cursor_position
        end += buff.cursor_position

        text = buff.text[start:end]
        text = ''.join(reversed(text))

        event.app.current_buffer.text = buff.text[:start] + text + buff.text[end:]

    # Create a text object.

    @text_object('A')
    def _(event):
        " A custom text object that involves everything. "
        # Note that a `TextObject` has coordinates, relative to the cursor position.
        buff = event.current_buffer
        return TextObject(
                -buff.document.cursor_position,  # The start.
                len(buff.text) - buff.document.cursor_position)  # The end.

    # Read input.
    print('There is a custom text object "A" that applies to everything')
    print('and a custom operator "r" that reverses the text object.\n')

    print('Things that are possible:')
    print('-  Riw    - reverse inner word.')
    print('-  yA     - yank everything.')
    print('-  RA     - reverse everything.')

    text = prompt('> ', default='hello world', key_bindings=bindings,
                  editing_mode=EditingMode.VI)
    print('You said: %s' % text)
Example #3
0
from prompt_toolkit.filters.cli import ViNavigationMode, ViSelectionMode, ViMode
from prompt_toolkit.filters import Always, IsReadOnly
from IPython import get_ipython
from prompt_toolkit.key_binding.bindings.vi import create_operator_decorator, create_text_object_decorator, TextObject
from prompt_toolkit.document import Document
from prompt_toolkit.buffer import ClipboardData
from prompt_toolkit.selection import SelectionType

ip = get_ipython()
registry = ip.pt_cli.application.key_bindings_registry

handle = registry.add_binding
text_object = create_text_object_decorator(registry)
operator = create_operator_decorator(registry)

navigation_mode = ViNavigationMode() & ViMode() & Always()
selection_mode = ViSelectionMode() & ViMode() & Always()


@handle('t', filter=selection_mode, eager=True)
def _____(event):
    event.current_buffer.cursor_up(count=event.arg)


@handle('h', filter=selection_mode, eager=True)
def _____(event):
    event.current_buffer.cursor_down(count=event.arg)


@text_object('d', eager=True)
def _____(event):
import klembord
from prompt_toolkit.clipboard import Clipboard
from prompt_toolkit.filters.app import vi_navigation_mode, vi_selection_mode
from prompt_toolkit.key_binding.bindings.vi import create_operator_decorator, create_text_object_decorator

klembord.init()

ip = get_ipython()

if getattr(ip, 'pt_app', None):

    bindings = ip.pt_app.key_bindings
    operator = create_operator_decorator(bindings)
    text_object = create_text_object_decorator(bindings)


    @bindings.add('p', filter=vi_navigation_mode)
    def _(event):
        buf = event.current_buffer
        cp = None
        while cp is None:
            cp = klembord.get_text()
        buf.insert_text(cp)

    @bindings.add("x", filter=vi_selection_mode)
    def _cut(event):
        """
        Cut selection.
        ('x' is not an operator.)
        """
        clipboard_data = event.current_buffer.cut_selection()
Example #5
0
def LoadViBindings(registry):
    """Adds gcloud interactive specific vi key bindings."""

    if six.PY2:
        ascii_lowercase = string.ascii_lowercase.decode('ascii')
    else:
        ascii_lowercase = string.ascii_lowercase
    vi_register_names = ascii_lowercase + '0123456789'
    # handle = registry.add_binding  # for non-operator binding
    operator = create_operator_decorator(registry)

    def CreateChangeOperators(with_register=False):
        """Creates and registers change operators.

    Args:
      with_register: Copy the changed text to this named register instead of
        the clipboard.
    """
        if with_register:
            handler_keys = ('"', Keys.Any, 'c')
        else:
            handler_keys = 'c'

        @operator(*handler_keys, filter=~IsReadOnly())
        def ChangeOperator(event, text_object):  # pylint: disable=unused-variable
            """A change operator."""
            clipboard_data = None
            buf = event.current_buffer

            if text_object:
                # <google-cloud-sdk-patch>
                # 'cw' shouldn't eat trailing whitespace -- get it back.
                if text_object.start < text_object.end:
                    while (text_object.end > text_object.start
                           and buf.text[buf.cursor_position + text_object.end -
                                        1].isspace()):
                        text_object.end -= 1
                else:
                    while (text_object.start > text_object.end
                           and buf.text[buf.cursor_position +
                                        text_object.start - 1].isspace()):
                        text_object.start -= 1
                # </google-cloud-sdk-patch>
                new_document, clipboard_data = text_object.cut(buf)
                buf.document = new_document

            # Set changed text to clipboard or named register.
            if clipboard_data and clipboard_data.text:
                if with_register:
                    reg_name = event.key_sequence[1].data
                    if reg_name in vi_register_names:
                        event.cli.vi_state.named_registers[
                            reg_name] = clipboard_data
                else:
                    event.cli.clipboard.set_data(clipboard_data)

            # Go back to insert mode.
            event.cli.vi_state.input_mode = InputMode.INSERT

    CreateChangeOperators(False)
    CreateChangeOperators(True)