import functools


def with_window(f):
    """
    Decorator that runs function only if buffer is in selected window.

    This decorator expects the first parameter of the wrapped function
    to be a buffer. Note that this modifies the argument list of f,
    inserting window as second positional argument.
    """
    @functools.wraps(f)
    def _with_window(*args, **kwargs):
        self = args[0]
        win = self.window()
        if win:
            f(args[0], win, *args[1:], **kwargs)
    return _with_window