<commit_before>"""
Key bindings which are also known by GNU readline by the given names.

See: http://www.delorie.com/gnu/docs/readline/rlman_13.html
"""
from __future__ import unicode_literals
from prompt_toolkit.enums import IncrementalSearchDirection, SEARCH_BUFFER
from prompt_toolkit.selection import PasteMode
from six.moves import range
import six

from .completion import generate_completions, display_completions_like_readline
from prompt_toolkit.document import Document
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.key_binding.input_processor import KeyPress
from prompt_toolkit.keys import Keys

__all__ = (
    'get_by_name',
)


# Registry that maps the Readline command names to their handlers.
_readline_commands = {}

def register(name):
    """
    Store handler in the `_readline_commands` dictionary.
    """
    assert isinstance(name, six.text_type)
    def decorator(handler):
        assert callable(handler)

        _readline_commands[name] = handler
        return handler
    return decorator


def get_by_name(name):
    """
    Return the handler for the (Readline) command with the given name.
    """
    try:
        return _readline_commands[name]
    except KeyError:
        raise KeyError('Unknown readline command: %r' % name)

#
# Commands for moving
# See: http://www.delorie.com/gnu/docs/readline/rlman_14.html
#

@register('beginning-of-line')
def beginning_of_line(event):
    " Move to the start of the current line. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_start_of_line_position(after_whitespace=False)


@register('end-of-line')
def end_of_line(event):
    " Move to the end of the line. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_end_of_line_position()


@register('forward-char')
def forward_char(event):
    " Move forward a character. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_cursor_right_position(count=event.arg)


@register('backward-char')
def backward_char(event):
    " Move back a character. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_cursor_left_position(count=event.arg)


@register('forward-word')
def forward_word(event):
    """
    Move forward to the end of the next word. Words are composed of letters and
    digits.
    """
    buff = event.current_buffer
    pos = buff.document.find_next_word_ending(count=event.arg)

    if pos:
        buff.cursor_position += pos


@register('backward-word')
def backward_word(event):
    """
    Move back to the start of the current or previous word. Words are composed
    of letters and digits.
    """
    buff = event.current_buffer
    pos = buff.document.find_previous_word_beginning(count=event.arg)

    if pos:
        buff.cursor_position += pos


@register('clear-screen')
def clear_screen(event):
    """
    Clear the screen and redraw everything at the top of the screen.
    """
    event.cli.renderer.clear()


@register('redraw-current-line')
def redraw_current_line(event):
    """
    Refresh the current line.
    (Readline defines this command, but prompt-toolkit doesn't have it.)
    """
    pass

#
# Commands for manipulating the history.
# See: http://www.delorie.com/gnu/docs/readline/rlman_15.html
#

@register('accept-line')
def accept_line(event):
    " Accept the line regardless of where the cursor is. "
    b = event.current_buffer
    b.accept_action.validate_and_handle(event.cli, b)


@register('previous-history')
def previous_history(event):
    " Move `back' through the history list, fetching the previous command.  "
    event.current_buffer.history_backward(count=event.arg)


@register('next-history')
def next_history(event):
    " Move `forward' through the history list, fetching the next command. "
    event.current_buffer.history_forward(count=event.arg)


@register('beginning-of-history')
def beginning_of_history(event):
    " Move to the first line in the history. "
    event.current_buffer.go_to_history(0)


@register('end-of-history')
def end_of_history(event):
    """
    Move to the end of the input history, i.e., the line currently being entered.
    """
    event.current_buffer.history_forward(count=10**100)
    buff = event.current_buffer
    buff.go_to_history(len(buff._working_lines) - 1)


@register('reverse-search-history')
def reverse_search_history(event):
    """
    Search backward starting at the current line and moving `up' through
    the history as necessary. This is an incremental search.<commit_msg>use matching backticks<commit_after>"""
Key bindings which are also known by GNU readline by the given names.

See: http://www.delorie.com/gnu/docs/readline/rlman_13.html
"""
from __future__ import unicode_literals
from prompt_toolkit.enums import IncrementalSearchDirection, SEARCH_BUFFER
from prompt_toolkit.selection import PasteMode
from six.moves import range
import six

from .completion import generate_completions, display_completions_like_readline
from prompt_toolkit.document import Document
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.key_binding.input_processor import KeyPress
from prompt_toolkit.keys import Keys

__all__ = (
    'get_by_name',
)


# Registry that maps the Readline command names to their handlers.
_readline_commands = {}

def register(name):
    """
    Store handler in the `_readline_commands` dictionary.
    """
    assert isinstance(name, six.text_type)
    def decorator(handler):
        assert callable(handler)

        _readline_commands[name] = handler
        return handler
    return decorator


def get_by_name(name):
    """
    Return the handler for the (Readline) command with the given name.
    """
    try:
        return _readline_commands[name]
    except KeyError:
        raise KeyError('Unknown readline command: %r' % name)

#
# Commands for moving
# See: http://www.delorie.com/gnu/docs/readline/rlman_14.html
#

@register('beginning-of-line')
def beginning_of_line(event):
    " Move to the start of the current line. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_start_of_line_position(after_whitespace=False)


@register('end-of-line')
def end_of_line(event):
    " Move to the end of the line. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_end_of_line_position()


@register('forward-char')
def forward_char(event):
    " Move forward a character. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_cursor_right_position(count=event.arg)


@register('backward-char')
def backward_char(event):
    " Move back a character. "
    buff = event.current_buffer
    buff.cursor_position += buff.document.get_cursor_left_position(count=event.arg)


@register('forward-word')
def forward_word(event):
    """
    Move forward to the end of the next word. Words are composed of letters and
    digits.
    """
    buff = event.current_buffer
    pos = buff.document.find_next_word_ending(count=event.arg)

    if pos:
        buff.cursor_position += pos


@register('backward-word')
def backward_word(event):
    """
    Move back to the start of the current or previous word. Words are composed
    of letters and digits.
    """
    buff = event.current_buffer
    pos = buff.document.find_previous_word_beginning(count=event.arg)

    if pos:
        buff.cursor_position += pos


@register('clear-screen')
def clear_screen(event):
    """
    Clear the screen and redraw everything at the top of the screen.
    """
    event.cli.renderer.clear()


@register('redraw-current-line')
def redraw_current_line(event):
    """
    Refresh the current line.
    (Readline defines this command, but prompt-toolkit doesn't have it.)
    """
    pass

#
# Commands for manipulating the history.
# See: http://www.delorie.com/gnu/docs/readline/rlman_15.html
#

@register('accept-line')
def accept_line(event):
    " Accept the line regardless of where the cursor is. "
    b = event.current_buffer
    b.accept_action.validate_and_handle(event.cli, b)


@register('previous-history')
def previous_history(event):
    " Move `back` through the history list, fetching the previous command.  "
    event.current_buffer.history_backward(count=event.arg)


@register('next-history')
def next_history(event):
    " Move `forward` through the history list, fetching the next command. "
    event.current_buffer.history_forward(count=event.arg)


@register('beginning-of-history')
def beginning_of_history(event):
    " Move to the first line in the history. "
    event.current_buffer.go_to_history(0)


@register('end-of-history')
def end_of_history(event):
    """
    Move to the end of the input history, i.e., the line currently being entered.
    """
    event.current_buffer.history_forward(count=10**100)
    buff = event.current_buffer
    buff.go_to_history(len(buff._working_lines) - 1)


@register('reverse-search-history')
def reverse_search_history(event):
    """
    Search backward starting at the current line and moving `up` through
    the history as necessary. This is an incremental search.
{'fraction_non_alphanumeric': 0.09826020352336141, 'fraction_numerical': 0.002844950213371266, 'mean_word_length': 3.964693101575231, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3950827', 'n_tokens_mistral': 2814, 'n_tokens_neox': 2735, 'n_words': 735}