Example #1
0
def _ex_route_print(state):
    command = TokenCommand('print')
    command.addressable = True
    command.cooperates_with_global = True

    # TODO [review] count param looks unused.
    params = {'count': '', 'flags': []}

    while True:
        c = state.consume()

        state.skip(' ')
        state.ignore()

        if c == state.EOF:
            command.params = params

            return None, [command, TokenEof()]

        if c.isdigit():
            state.match(r'\d*')
            params['count'] = state.emit()
            continue

        m = state.expect_match(r'[l#p]+')
        params['flags'] = list(m.group(0))
        state.ignore()
        state.expect_eof()
        break

    command.params = params

    return None, [command, TokenEof()]
Example #2
0
def _ex_route_wqall(state):
    command = TokenCommand('wqall')
    command.addressable = True
    params = {'++': ''}

    state.skip(' ')
    state.ignore()

    c = state.consume()
    if c == '+':
        state.expect('+')
        state.ignore()
        # TODO: expect_match should work with emit()
        # https://vimhelp.appspot.com/editing.txt.html#[++opt]
        m = state.expect_match(
            r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
            lambda: Exception("E474: Invalid argument"))

        name = m.group(0)

        plus_plus_translations = {
            'ff': 'fileformat',
            'bin': 'binary',
            'enc': 'fileencoding',
            'nobin': 'nobinary'
        }

        params['++'] = plus_plus_translations.get(name, name)
        state.ignore()

    state.expect_eof()

    command.params = params

    return None, [command, TokenEof()]
Example #3
0
def _ex_route_delete(state):
    command = TokenCommand('delete')
    command.addressable = True

    params = {'register': '"', 'count': None}

    state.skip(' ')
    state.ignore()

    c = state.consume()
    if c == state.EOF:
        command.params = params

        return None, [command, TokenEof()]

    state.backup()
    state.skip(' ')
    state.ignore()

    m = state.expect_match(
        r'(?P<register>[a-zA-Z0-9"])(?:\s+(?P<count>\d+))?\s*$')

    params.update(m.groupdict())
    if params['count']:
        raise NotImplementedError('parameter not implemented')

    command.params = params

    return None, [command, TokenEof()]
Example #4
0
def _ex_route_print(state):
    command = TokenCommand('print')
    command.addressable = True
    command.cooperates_with_global = True

    while True:
        c = state.consume()

        state.skip(' ')
        state.ignore()

        if c == state.EOF:
            break

        c = state.consume()
        if c.isdigit():
            state.match(r'\d*')
            command.params['count'] = state.emit()
            continue

        state.backup()

        m = state.expect_match(r'[l#p]+')
        command.params['flags'] = list(m.group(0))
        state.ignore()
        state.expect_eof()
        break

    return command
Example #5
0
def _ex_route_shell_out(state):
    command = TokenCommand('!', target='shell_out')
    command.addressable = True
    params = {'cmd': None}

    m = state.expect_match(r'(?P<cmd>.+)$')
    params.update(m.groupdict())

    command.params = params

    return None, [command, TokenEof()]
Example #6
0
def _ex_route_copy(state):
    command = TokenCommand('copy')
    command.addressable = True

    params = {'address': None}
    m = state.expect_match(r'\s*(?P<address>.+?)\s*$')
    params.update(m.groupdict())

    command.params = params

    return None, [command, TokenEof()]
Example #7
0
def _ex_route_move(state):
    command = TokenCommand('move')
    command.addressable = True

    state.skip(' ')
    state.ignore()

    m = state.match(r'(?P<address>.*$)')
    if m:
        command.params['address'] = m.group(0).strip() or '.'

    return command
Example #8
0
def _ex_route_move(state):
    command = TokenCommand('move')
    command.addressable = True
    params = {'address': None}

    state.skip(' ')
    state.ignore()

    m = state.match(r'(?P<address>.*$)')
    if m:
        address_command_line = m.group(0).strip() or '.'
        params['address'] = address_command_line

    command.params = params

    return None, [command, TokenEof()]
Example #9
0
def _ex_route_double_ampersand(state):
    command = TokenCommand('&&', target='double_ampersand')
    command.addressable = True

    params = {'flags': [], 'count': ''}

    m = state.match(r'\s*([cgr])*\s*(\d*)\s*$')

    params['flags'] = list(m.group(1)) if m.group(1) else []
    params['count'] = m.group(2) or ''

    state.expect_eof()

    command.params = params

    return None, [command, TokenEof()]
Example #10
0
def _ex_route_global(state):
    command = TokenCommand('global')
    command.addressable = True
    params = {'pattern': None, 'cmd': None}

    c = state.consume()

    bang = c == '!'
    sep = c if not bang else state.consume()

    # TODO: we're probably missing legal separators.
    # TODO [refactor] and remove assertion
    assert c in '!:?/\\&$', 'bad separator'

    state.ignore()

    while True:
        c = state.consume()

        if c == state.EOF:
            raise ValueError('unexpected EOF in: ' + state.source)

        if c == sep:
            state.backup()

            params['pattern'] = state.emit()

            state.consume()
            state.ignore()
            break

    cmd = state.match(r'.*$').group(0).strip()
    if cmd:
        params['cmd'] = cmd

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
Example #11
0
def _ex_route_substitute(state):
    command = TokenCommand('substitute')
    command.addressable = True

    delim = state.consume()

    if delim == state.EOF:
        return None, [command, TokenEof()]

    state.backup()
    delim = state.consume()
    params = {
        "pattern": None,
        "replacement": None,
        "count": 1,
        "flags": [],
    }

    while True:
        c = state.consume()

        if c == delim:
            state.start += 1
            state.backup()
            params['pattern'] = state.emit()
            state.consume()
            break

        if c == state.EOF:
            raise ValueError("bad command: {0}".format(state.source))

    while True:
        c = state.consume()
        if c == delim:
            state.start += 1
            state.backup()
            params['replacement'] = state.emit()
            state.consume()
            state.ignore()
            break

        if c == state.EOF:
            state.start += 1
            params['replacement'] = state.emit()
            state.consume()
            state.ignore()
            break

    if state.match(r'\s*[&cegiInp#lr]+'):
        params['flags'] = list(state.emit().strip())
        if '&' in params['flags'] and params['flags'][0] != '&':
            raise ValueError("bad command: {}".format(state.source))

    if state.peek(' '):
        state.skip(' ')
        state.ignore()
        if state.match(r'\d+'):
            params['count'] = int(state.emit())

    state.skip(' ')
    state.ignore()
    state.expect_eof()

    command.params = params

    return None, [command, TokenEof()]
Example #12
0
def _ex_route_exit(state):
    command = TokenCommand('exit')
    command.addressable = True

    # TODO [review] file_name param looks unused by the ex_exit
    params = {'file_name': ''}

    bang = state.consume()

    if bang == state.EOF:
        command.params = params

        return None, [command, TokenEof()]

    bang = bang == '!'
    if not bang:
        state.backup()

    state.skip(' ')
    state.ignore()

    plus_plus_translations = {
        'ff': 'fileformat',
        'bin': 'binary',
        'enc': 'fileencoding',
        'nobin': 'nobinary'
    }

    while True:
        c = state.consume()

        if c == state.EOF:
            command.params = params
            command.forced = bang

            return None, [command, TokenEof()]

        if c == '+':
            state.expect('+')
            state.ignore()

            # TODO: expect_match should work with emit()
            # https://vimhelp.appspot.com/editing.txt.html#[++opt]
            m = state.expect_match(
                r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
                lambda: Exception("E474: Invalid argument"))

            name = m.group(0)
            params['++'] = plus_plus_translations.get(name, name)

            state.ignore()
            continue

        if c != ' ':
            state.match(r'.*')
            params['file_name'] = state.emit().strip()
            state.skip(' ')
            state.ignore()

    state.expect_eof()

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
Example #13
0
def _ex_route_write(state):
    command = TokenCommand('write')
    command.addressable = True

    # TODO [refactor] params should used keys compatible with ex command function keyword arguments. Review other routes too.  # noqa: E501

    params = {
        '++': '',
        'file_name': '',
        '>>': False,
        'cmd': '',
    }

    bang = state.consume()
    if bang == state.EOF:
        command.params = params

        return None, [command, TokenEof()]

    if bang != '!':
        bang = False
        state.backup()

    state.skip(' ')
    state.ignore()

    plus_plus_translations = {
        'ff': 'fileformat',
        'bin': 'binary',
        'enc': 'fileencoding',
        'nobin': 'nobinary'
    }

    while True:
        c = state.consume()
        if c == state.EOF:
            # TODO: forced?
            command.params = params
            command.forced = bang

            return None, [command, TokenEof()]

        if c == '+':
            state.expect('+')
            state.ignore()
            # TODO: expect_match should work with emit()
            # https://vimhelp.appspot.com/editing.txt.html#[++opt]
            m = state.expect_match(
                r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
                lambda: Exception("E474: Invalid argument"))

            name = m.group(0)
            params['++'] = plus_plus_translations.get(name, name)
            state.ignore()
            continue

        if c == '>':
            state.expect('>')
            state.ignore()
            params['>>'] = True
            state.match(r'.*$')
            params['file_name'] = state.emit().strip()
            continue

        if c == '!':
            state.ignore()
            state.match(r'.*$')
            params['cmd'] = state.emit()
            continue

        if c != ' ':
            state.match(r'.*')
            params['file_name'] = state.emit().strip()
            state.skip(' ')
            state.ignore()

    state.expect_eof()

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
Example #14
0
def _ex_route_copy(state):
    command = TokenCommand('copy')
    command.addressable = True
    command.params = state.expect_match(r'\s*(?P<address>.+?)\s*$').groupdict()

    return command