Beispiel #1
0
def _open(view: View, defx: Defx, context: Context) -> None:
    """
    Open the file.
    """
    cwd = view._vim.call('getcwd', -1)
    command = context.args[0] if context.args else 'edit'
    previewed_buffers = view._vim.vars['defx#_previewed_buffers']
    for target in context.targets:
        path = target['action__path']

        if path.is_dir():
            view.cd(defx, defx._source.name, str(path), context.cursor)
            continue

        if not view._vim.call('haslocaldir'):
            try:
                path = path.relative_to(cwd)
            except ValueError:
                pass

        view._vim.call('defx#util#execute_path', command, str(path))

        bufnr = str(view._vim.call('bufnr', str(path)))
        if bufnr in previewed_buffers:
            previewed_buffers.pop(bufnr)
            view._vim.vars['defx#_previewed_buffers'] = previewed_buffers

        view.restore_previous_buffer()
    view.close_preview()
Beispiel #2
0
def _drop(view: View, defx: Defx, context: Context) -> None:
    """
    Open like :drop.
    """
    cwd = view._vim.call('getcwd')
    command = context.args[0] if context.args else 'edit'

    for target in context.targets:
        path = target['action__path']

        if path.is_dir():
            view.cd(defx, str(path), context.cursor)
            continue

        bufnr = view._vim.call('bufnr', f'^{path}$')
        winids = view._vim.call('win_findbuf', bufnr)

        if winids:
            view._vim.call('win_gotoid', winids[0])
        else:
            if context.prev_winid != view._winid and view._vim.call(
                    'win_id2win', context.prev_winid):
                view._vim.call('win_gotoid', context.prev_winid)
            else:
                view._vim.command('wincmd w')
            try:
                path = path.relative_to(cwd)
            except ValueError:
                pass
            view._vim.call('defx#util#execute_path', command, str(path))
Beispiel #3
0
    def create_open(self, view: View, defx: Defx, context: Context, path: Path,
                    command: str, isdir: bool, isopen: bool) -> None:
        if isdir:
            path.mkdir(parents=True)
        else:
            path.parent.mkdir(parents=True, exist_ok=True)
            path.touch()

        if not isopen:
            view.redraw(True)
            view.search_recursive(path, defx._index)
            return

        # Note: Must be redraw before actions
        view.redraw(True)
        view.search_recursive(path, defx._index)

        if isdir:
            if command == 'open_tree':
                view.open_tree(path, defx._index, False, 0)
            else:
                view.cd(defx, defx._source.name, str(path), context.cursor)
        else:
            if command == 'drop':
                self._drop(
                    view, defx,
                    context._replace(args=[], targets=[{
                        'action__path': path
                    }]))
            else:
                view._vim.call('defx#util#execute_path', command,
                               self.get_buffer_name(str(path)))
Beispiel #4
0
def _drop(view: View, defx: Defx, context: Context) -> None:
    """
    Open like :drop.
    """
    command = context.args[0] if context.args else 'edit'

    for target in context.targets:
        path = target['action__path']

        if path.is_dir():
            view.cd(defx, defx._source.name, str(path), context.cursor)
            continue

        bufnr = view._vim.call('bufnr', f'^{path}$')
        winids = view._vim.call('win_findbuf', bufnr)

        if winids:
            view._vim.call('win_gotoid', winids[0])
        else:
            if context.prev_winid != view._winid and view._vim.call(
                    'win_id2win', context.prev_winid):
                view._vim.call('win_gotoid', context.prev_winid)
            else:
                view._vim.command('wincmd w')
            view._vim.call('defx#util#execute_path', command, str(path))

        view.restore_previous_buffer()
Beispiel #5
0
def _cd(view: View, defx: Defx, context: Context) -> None:
    """
    Change the current directory.
    """
    source_name = defx._source.name
    is_parent = context.args and context.args[0] == '..'
    prev_cwd = Path(defx._cwd)

    if is_parent:
        path = prev_cwd.parent
    else:
        if context.args:
            if len(context.args) > 1:
                source_name = context.args[0]
                path = Path(context.args[1])
            else:
                path = Path(context.args[0])
        else:
            path = Path.home()
        path = prev_cwd.joinpath(path)
        if not readable(path):
            error(view._vim, f'{path} is invalid.')
        path = path.resolve()
        if source_name == 'file' and not path.is_dir():
            error(view._vim, f'{path} is invalid.')
            return

    view.cd(defx, source_name, str(path), context.cursor)
    if is_parent:
        view.search_file(prev_cwd, defx._index)
Beispiel #6
0
def _open_directory(view: View, defx: Defx, context: Context) -> None:
    """
    Open the directory.
    """
    for target in context.targets:
        path = target['action__path']

        if path.is_dir():
            view.cd(defx, str(path), context.cursor)
Beispiel #7
0
def _cd(view: View, defx: Defx, context: Context) -> None:
    """
    Change the current directory.
    """
    path = context.args[0] if context.args else expand('~')
    path = os.path.normpath(os.path.join(defx._cwd, path))
    if not os.path.isdir(path):
        error(view._vim, '{} is not directory'.format(path))
        return

    view.cd(defx, path, context.cursor)
    view._selected_candidates = []
Beispiel #8
0
def _open_directory(view: View, defx: Defx, context: Context) -> None:
    """
    Open the directory.
    """
    if context.args:
        path = Path(context.args[0])
    else:
        for target in context.targets:
            path = target['action__path']

    if path.is_dir():
        view.cd(defx, 'file', str(path), context.cursor)
Beispiel #9
0
def _cd(view: View, defx: Defx, context: Context) -> None:
    """
    Change the current directory.
    """
    path = Path(context.args[0]) if context.args else Path.home()
    path = Path(defx._cwd).joinpath(path).resolve()
    if not readable(path) or not path.is_dir():
        error(view._vim, f'{path} is not readable directory')
        return

    prev_cwd = defx._cwd
    view.cd(defx, str(path), context.cursor)
    if context.args and context.args[0] == '..':
        view.search_file(Path(prev_cwd), defx._index)
Beispiel #10
0
def _open(view: View, defx: Defx, context: Context) -> None:
    """
    Open the file.
    """
    cwd = view._vim.call('getcwd')
    command = context.args[0] if context.args else 'edit'
    for target in context.targets:
        path = target['action__path']

        if os.path.isdir(path):
            view.cd(defx, path, context.cursor)
        else:
            if path.startswith(cwd):
                path = os.path.relpath(path, cwd)
            view._vim.call('defx#util#execute_path', command, path)
Beispiel #11
0
def _open(view: View, defx: Defx, context: Context) -> None:
    """
    Open the file.
    """
    cwd = view._vim.call('getcwd')
    command = context.args[0] if context.args else 'edit'
    for target in context.targets:
        path = target['action__path']

        if path.is_dir():
            view.cd(defx, str(path), context.cursor)
        else:
            if path.match(cwd):
                path = path.relative_to(cwd)
            view._vim.call('defx#util#execute_path', command, str(path))
Beispiel #12
0
    def _search_recursive(self, view: View, defx: Defx,
                          context: Context) -> None:
        if not context.args or not context.args[0]:
            return

        search_path = Path(context.args[0])
        if not search_path.is_absolute():
            # Use relative directory instead.
            search_path = Path(Path(defx._cwd).joinpath(context.args[0]))

        if not view.search_recursive(search_path, defx._index):
            # Not found in current path.
            # Change defx directory to "search_path".
            view.cd(defx, defx._source.name,
                    str(search_path.parent), context.cursor)
            view.search_recursive(search_path, defx._index)
Beispiel #13
0
def _open(view: View, defx: Defx, context: Context) -> None:
    """
    Open the file.
    """
    cwd = view._vim.call('getcwd')
    command = context.args[0] if context.args else 'edit'
    for target in context.targets:
        path = target['action__path']

        if path.is_dir():
            view.cd(defx, defx._source.name, str(path), context.cursor)
            continue

        try:
            path = path.relative_to(cwd)
        except ValueError:
            pass
        view._vim.call('defx#util#execute_path', command, str(path))
Beispiel #14
0
    def _drop(self, view: View, defx: Defx, context: Context) -> None:
        """
        Open like :drop.
        """
        cwd = view._vim.call('getcwd', -1)
        command = context.args[0] if context.args else 'edit'

        for target in context.targets:
            path = target['action__path']

            if path.is_dir():
                view.cd(defx, defx._source.name, str(path), context.cursor)
                continue

            # bufnr check
            winids = []
            try:
                bufnr = view._vim.call('bufnr', f'^{path}$')
                winids = view._vim.call('win_findbuf', bufnr)
            except NvimError:
                pass

            if winids:
                view._vim.call('win_gotoid', winids[0])
            else:
                # Jump to the last accessed window
                view._vim.command('wincmd p')

                if view._vim.call('win_getid') == view._winid:
                    view._vim.command('wincmd w')

                if (not view._vim.call('haslocaldir')
                        and not bool(view._vim.options['autochdir'])):
                    # Note: autochdir is dangerous option
                    try:
                        path = path.relative_to(cwd)
                    except ValueError:
                        pass

                view._vim.call('defx#util#execute_path', command,
                               self.get_buffer_name(str(path)))

            view.restore_previous_buffer(view._prev_bufnr)
        view.close_preview()
Beispiel #15
0
    def _cd(self, view: View, defx: Defx, context: Context) -> None:
        """
        Change the current directory.
        """
        source_name = defx._source.name
        is_parent = context.args and context.args[0] == '..'
        open = len(context.args) > 1 and context.args[1] == 'open'
        save_history = len(context.args) < 3 or context.args[2] != 'nohist'
        prev_cwd = self.path_maker(defx._cwd)

        if open:
            # Don't close current directory
            defx._opened_candidates.add(defx._cwd)

        if is_parent:
            path = prev_cwd.parent
        else:
            if context.args:
                if len(context.args) > 1:
                    source_name = context.args[0]
                    path = self.path_maker(context.args[1])
                else:
                    path = self.path_maker(context.args[0])
            else:
                path = self.get_home()
            path = prev_cwd.joinpath(path)
            if not self.is_readable(path):
                error(view._vim, f'{path} is invalid.')
            path = path.resolve()
            if source_name == 'file' and not path.is_dir():
                error(view._vim, f'{path} is invalid.')
                return

        view.cd(defx,
                source_name,
                str(path),
                context.cursor,
                save_history=save_history)
        if is_parent:
            view.search_file(prev_cwd, defx._index)
Beispiel #16
0
    def _open(self, view: View, defx: Defx, context: Context) -> None:
        """
        Open the file.
        """
        cwd = view._vim.call('getcwd', -1)
        command = context.args[0] if context.args else 'edit'
        choose = command == 'choose' and (
            view._vim.call('exists', 'g:loaded_choosewin')
            or view._vim.call('hasmapto', '<Plug>(choosewin)', 'n'))
        previewed_buffers = view._vim.vars['defx#_previewed_buffers']
        for target in context.targets:
            path = target['action__path']

            if path.is_dir():
                view.cd(defx, defx._source.name, str(path), context.cursor)
                continue

            if (not view._vim.call('haslocaldir')
                    and not bool(view._vim.options['autochdir'])):
                # Note: autochdir is dangerous option
                try:
                    path = path.relative_to(cwd)
                except ValueError:
                    pass

            if choose:
                self.switch(view)

            view._vim.call('defx#util#execute_path',
                           'edit' if choose else command,
                           self.get_buffer_name(str(path)))

            bufnr = str(view._vim.call('bufnr', str(path)))
            if bufnr in previewed_buffers:
                previewed_buffers.pop(bufnr)
                view._vim.vars['defx#_previewed_buffers'] = previewed_buffers

            view.restore_previous_buffer(view._prev_bufnr)
        view.close_preview()
Beispiel #17
0
def _drop(view: View, defx: Defx, context: Context) -> None:
    """
    Open like :drop.
    """
    cwd = view._vim.call('getcwd')
    command = context.args[0] if context.args else 'edit'
    for target in context.targets:
        path = target['action__path']

        if path.is_dir():
            view.cd(defx, str(path), context.cursor)
            continue

        bufnr = view._vim.call('bufnr', str(path))
        winids = view._vim.call('win_findbuf', bufnr)

        if winids:
            view._vim.call('win_gotoid', winids[0])
        else:
            view._vim.command('wincmd p')
            if path.match(cwd):
                path = path.relative_to(cwd)
            view._vim.call('defx#util#execute_path', command, str(path))