Module vpe.mapping

Python support for key sequence mapping.

This module provides support for mapping key sequences to Python function calls.

KeyHandler

class mapping.KeyHandler

Mix-in to support mapping key sequences to methods.

This can be used as a simple base class, but also as a mix-in. For example:

class MyScratchBuffer(ScratchBuffer, KeyHandler):
    def __init__(*args, **kwargs):
        super().__init__(*args, **kwargs)
        self.auto_map_keys()
        ...

The mapped static method is used to decorate the methods that you wish to handle mappings. Some examples:

@KeyHandler.mapped('<C-F1>')
def show_general_help(self) -> None:
    ...

@KeyHandler.mapped('<Leader>h')
def show_help_for_word_under_cursor(self) -> None:
    ...

# This mapping is forced to be global by the ``buffer=False`` argument.
@KeyHandler.mapped('<F12>', buffer=False)
def list_buffers(self) -> None:
    ...

The auto_map_keys method must be called to set up the mappings, typically within the class __init__ method. If the main class is a subclass of Buffer (such as MyScratchBuffer above) then the mappings are set up just for that buffer, unless buffer argument was specified for the mapped method. Otherwise the mappings default to global.

Methods

auto_map_keys(pass_info: bool = False) None

Set up mappings for methods.

Parameters

pass_info: bool

If set then each key handler method will be invoked with a MappingInfo object. This is useful if you want a single method to handle several mappings, but behave differently depening on which mapping was triggered.

Static methods

static mapped(...)
mapped(
        mode: Union[str, Iterable[str]],
        keyseq: Union[str, Iterable[str]],
        **kwargs

Decorator to make a keyboard mapping invoke a method.

This decorator supports the ‘<Leader>’ prefix in key sequences, in much the same way as describled in mapleader. For example if g:mapleader is set to ‘,’ then the key sequence ‘<Leader>q’ is equivalent to ‘,q’. If g:mapleader is unset or blank then ‘' is used.

The interpretation of <Leader> occurs at the time of decoration, so changing g:mapleader after plugin loading will typicallhave no effect.

Parameters

mode: Union

The mode in which the mapping applies, one of normal, op-pending, visual or insert. Or an iterable sequence of modes.

keyseq: Union

A key sequence string or sequence thereof, as used by map.

kwargs

See map for the supported values.

MapCallback

class mapping.MapCallback(*args, **kwargs)

Wrapper for a function to be invoked by a key mapping.

This extends the core Callback to provide a MappingInfo as the first positional argument.

Parameters

pass_info

If True, provide a MappingInfo object as the first argument to the callback function.

Methods

get_call_args(_vpe_args: dict[str, Any])

Get the Python positional and keyword arguments.

This makes the first positional argument a MappingInfo instance, unless self.pass_info has been cleared.

MappingInfo

class mapping.MappingInfo(mode: str, keys: str)

Information passed to a key mapping callback handler.

The initialisation parameters are made available as attributes.

Attributes

end_cursor

When mode==”visual”, a tuple (line, column) of the selection end. Both values are 1-based. Will be (-1, -1) when not applicable.

keys

The sequence of keys that triggered the mapping.

lidx

The index of the current line.

mode

The mode in which the mapping was triggered (normal, visual, op-pending or insert).

start_cursor

When mode==”visual”, a tuple (line, column) of the selection start. Both values are 1-based. Will be (-1, -1) when not applicable.

vmode

The visual mode (‘character’, ‘line’ or ‘block’). Will be None when not applicable.

Properties

property effective_line_range tuple[int, int] | None

The effective line range.

If the mod is ‘visual’ then this is the same as line_range otherwise it is lidx, lidx + 1.

property line_range tuple[int, int] | None

The line range, if visual mode was active.

This is a Python style range.

imap

mapping.imap(...)
imap(
        keys: Union[str, Iterable[str]],
        func: Union[Callable, str],
        buffer: bool = True,
        silent: bool = True,
        unique: bool = False,
        pass_info=True,
        nowait: bool = False,
        command: bool = False,
        args=(),
        kwargs: Optional[dict] = None,

Set up an insert mapping that invokes a Python function.

See map for argument details.

map

mapping.map(...)
map(
        mode: str,
        keys: Union[str, Iterable[str]],
        func: Union[Callable, str],
        buffer: bool = True,
        silent: bool = True,
        unique: bool = False,
        nowait: bool = False,
        command: bool = False,
        pass_info=True,
        args=(),
        kwargs: Optional[dict] = None,
        vim_exprs: tuple[str, ...] = ()

Set up a key mapping that invokes a Python function.

By default, the effective map command has the form:

{m}noremap <buffer> <silent> keys …

Where {m} is one of n, x, o, i.

The noremap form is always used.

By default the first argument passed to the mapped function is a MappingInfo object. The pass_info argument can be used to prevent this. Additional arguments can be specified using args and kwargs.

For convenience, mode specific versions are provided (nmap, xmap, omap and imap). See those for details of what he mapped function can do. It is recommended that these mode specific versions are use in preference to this function.

The func argument may also be a string, in which case it is interpreted as the literal RHS of the key mapping.

Parameters

mode: str

A string defining the mode in which the mapping occurs. This should be one of: normal, visual, op-pending, insert, command, select. The command and select mode are not supported when func is not a string.

keys: Union

The key sequence to be mapped. This may be an iterable set of key sequences that should all be mapped to the same action.

func: Union

The Python function to invoke for the mapping or a string to use as the right hand side of the mapping.

buffer: bool

Use the <buffer> special argument. Defaults to True.

silent: bool

Use the <silent> special argument. Defaults to True.

unique: bool

Use the <unique> special argument. Defaults to False.

nowait: bool

Use the <nowait> special argument. Defaults to False.

command: bool

Only applicable to insert mode. If true then the function is invoked from the command prompt and the return value is not used. Otherwise (the default) the function should return the text to be inserted.

pass_info

If set then the first argument passed to func is a MappingInfo object. Defaults to True.

args

Additional arguments to pass to the mapped function.

kwargs: Optional

Additional keyword arguments to pass to the mapped function.

vim_exprs: tuple

Vim expressions to be evaluated and passed to the callback function, when the mapping is triggered.

nmap

mapping.nmap(...)
nmap(
        keys: Union[str, Iterable[str]],
        func: Union[Callable, str],
        buffer: bool = True,
        silent: bool = True,
        unique: bool = False,
        pass_info=True,
        nowait: bool = False,
        args=(),
        kwargs: Optional[dict] = None,

Set up a normal mode mapping that invokes a Python function.

See map for argument details.

omap

mapping.omap(...)
omap(
        keys: Union[str, Iterable[str]],
        func: Union[Callable, str],
        buffer: bool = True,
        silent: bool = True,
        unique: bool = False,
        pass_info=True,
        nowait: bool = False,
        args=(),
        kwargs: Optional[dict] = None,

Set up an operator-pending mode mapping that invokes a Python function.

See map for argument details.

xmap

mapping.xmap(...)
xmap(
        keys: Union[str, Iterable[str]],
        func: Union[Callable, str],
        buffer: bool = True,
        silent: bool = True,
        unique: bool = False,
        pass_info=True,
        nowait: bool = False,
        args=(),
        kwargs: Optional[dict] = None,

Set up a visual mode mapping that invokes a Python function.

See map for argument details.