Beispiel #1
0
def _new_file(view: View, defx: Defx, context: Context) -> None:
    """
    Create a new file and it's parent directories.
    """
    candidate = view.get_cursor_candidate(context.cursor)
    if not candidate:
        return

    if candidate['is_opened_tree'] or candidate['is_root']:
        cwd = str(candidate['action__path'])
    else:
        cwd = str(Path(candidate['action__path']).parent)

    new_filename = cwd_input(
        view._vim, cwd,
        'Please input a new filename: ', '', 'file')
    if not new_filename:
        return
    isdir = new_filename[-1] == '/'
    filename = Path(cwd).joinpath(new_filename)

    if not filename:
        return
    if filename.exists():
        error(view._vim, f'{filename} already exists')
        return

    if isdir:
        filename.mkdir(parents=True)
    else:
        filename.parent.mkdir(parents=True, exist_ok=True)
        filename.touch()

    view.redraw(True)
    view.search_recursive(filename, defx._index)
Beispiel #2
0
    def _new_directory(self, view: View, defx: Defx, context: Context) -> None:
        """
        Create a new directory.
        """
        candidate = view.get_cursor_candidate(context.cursor)
        if not candidate:
            return

        if candidate['is_opened_tree'] or candidate['is_root']:
            cwd = str(candidate['action__path'])
        else:
            cwd = str(Path(candidate['action__path']).parent)

        new_filename = self.input(view, defx, cwd,
                                  'Please input a new directory name: ', '',
                                  'file')
        if not new_filename:
            return
        filename = self.path_maker(cwd).joinpath(new_filename)

        if not filename:
            return
        if filename.exists():
            error(view._vim, f'{filename} already exists')
            return

        filename.mkdir(parents=True)
        view.redraw(True)
        view.search_recursive(filename, defx._index)
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 _paste(view: View, defx: Defx, context: Context) -> None:
    candidate = view.get_cursor_candidate(context.cursor)
    if not candidate:
        return

    if candidate['is_opened_tree'] or candidate['is_root']:
        cwd = str(candidate['action__path'])
    else:
        cwd = str(Path(candidate['action__path']).parent)

    action = view._clipboard.action
    dest = None
    for index, candidate in enumerate(view._clipboard.candidates):
        path = candidate['action__path']
        dest = Path(cwd).joinpath(path.name)
        if dest.exists():
            overwrite = check_overwrite(view, dest, path)
            if overwrite == Path(''):
                continue
            dest = overwrite

        if not path.exists() or path == dest:
            continue

        view.print_msg(
            f'[{index + 1}/{len(view._clipboard.candidates)}] {path}')
        if action == ClipboardAction.COPY:
            if path.is_dir():
                shutil.copytree(str(path), dest)
            else:
                shutil.copy2(str(path), dest)
        elif action == ClipboardAction.MOVE:
            if dest.exists():
                # Must remove dest before
                if dest.is_dir():
                    shutil.rmtree(str(dest))
                else:
                    dest.unlink()
            shutil.move(str(path), cwd)

            # Check rename
            if not path.is_dir():
                view._vim.call('defx#util#buffer_rename',
                               view._vim.call('bufnr', str(path)), str(dest))

        view._vim.command('redraw')
    if action == ClipboardAction.MOVE:
        # Clear clipboard after move
        view._clipboard.candidates = []
    view._vim.command('echo')

    view.redraw(True)
    if dest:
        view.search_recursive(dest, defx._index)
Beispiel #5
0
    def _rename(self, view: View, defx: Defx, context: Context) -> None:
        """
        Rename the file or directory.
        """

        if len(context.targets) > 1:
            # ex rename
            view._vim.call('defx#exrename#create_buffer',
                           [{
                               'action__path': str(x['action__path'])
                           }
                            for x in context.targets], {'buffer_name': 'defx'})
            return

        mode = '' if not context.args else context.args[0]
        for target in context.targets:
            old = target['action__path']
            default = str(old)
            if mode == 'insert':
                view._vim.call('defx#util#back_cursor_input', len(old.name))
            elif mode == 'append':
                view._vim.call('defx#util#back_cursor_input', len(old.suffix))
            elif mode == 'new':
                default = default[:-len(old.name)]
            new_filename = self.input(view, defx, defx._cwd,
                                      f'Old name: {old}\nNew name: ', default,
                                      'file')
            view._vim.command('redraw')
            if not new_filename:
                return
            new = self.path_maker(defx._cwd).joinpath(new_filename)
            if not new or new == old:
                continue
            if str(new).lower() != str(old).lower() and new.exists():
                error(view._vim, f'{new} already exists')
                continue

            if not new.parent.exists():
                new.parent.mkdir(parents=True)
            old.rename(new)

            # Check rename
            # The old is directory, the path may be matched opened file
            if not new.is_dir() and view._vim.call('bufexists', str(old)):
                view._vim.call('defx#util#buffer_rename',
                               view._vim.call('bufnr', str(old)), str(new))

            view.redraw(True)
            view.search_recursive(new, defx._index)
Beispiel #6
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 #7
0
    def _paste(self, view: View, defx: Defx, context: Context) -> None:
        candidate = view.get_cursor_candidate(context.cursor)
        action = view._clipboard.action
        if not candidate or action == ClipboardAction.NONE:
            return

        if candidate['is_opened_tree'] or candidate['is_root']:
            cwd = str(candidate['action__path'])
        else:
            cwd = str(Path(candidate['action__path']).parent)

        dest = None
        for index, candidate in enumerate(view._clipboard.candidates):
            path = candidate['action__path']
            dest = self.path_maker(cwd).joinpath(path.name)
            if dest.exists():
                overwrite = self.check_overwrite(view, dest, path)
                if overwrite == self.path_maker(''):
                    continue
                dest = overwrite

            if not path.exists() or path == dest:
                continue

            view.print_msg(
                f'[{index + 1}/{len(view._clipboard.candidates)}] {path}')

            if dest.exists() and action != ClipboardAction.MOVE:
                # Must remove dest before
                if not dest.is_symlink() and dest.is_dir():
                    self.rmtree(dest)
                else:
                    dest.unlink()

            self.paste(view, path, dest, cwd)
            view._vim.command('redraw')

        if action == ClipboardAction.MOVE:
            # Clear clipboard after action
            view._clipboard.action = ClipboardAction.NONE
            view._clipboard.candidates = []

        view._vim.command('echo')

        view.redraw(True)
        if dest:
            view.search_recursive(dest, defx._index)
Beispiel #8
0
def _new_multiple_files(view: View, defx: Defx, context: Context) -> None:
    """
    Create multiple files.
    """
    candidate = view.get_cursor_candidate(context.cursor)
    if not candidate:
        return

    if candidate['is_opened_tree'] or candidate['is_root']:
        cwd = str(candidate['action__path'])
    else:
        cwd = str(Path(candidate['action__path']).parent)

    save_cwd = view._vim.call('getcwd')
    cd(view._vim, cwd)

    str_filenames: str = view._vim.call('input',
                                        'Please input new filenames: ', '',
                                        'file')
    cd(view._vim, save_cwd)

    if not str_filenames:
        return None

    for name in str_filenames.split():
        is_dir = name[-1] == '/'

        filename = Path(cwd).joinpath(name)
        if filename.exists():
            error(view._vim, f'{filename} already exists')
            continue

        if is_dir:
            filename.mkdir(parents=True)
        else:
            if not filename.parent.exists():
                filename.parent.mkdir(parents=True)
            filename.touch()

    view.redraw(True)
    view.search_recursive(filename, defx._index)
Beispiel #9
0
def _rename(view: View, defx: Defx, context: Context) -> None:
    """
    Rename the file or directory.
    """

    if len(context.targets) > 1:
        # ex rename
        view._vim.call('defx#exrename#create_buffer',
                       [{
                           'action__path': str(x['action__path'])
                       } for x in context.targets], {'buffer_name': 'defx'})
        return

    for target in context.targets:
        old = target['action__path']
        new_filename = cwd_input(view._vim,
                                 defx._cwd, f'Old name: {old}\nNew name: ',
                                 str(old), 'file')
        view._vim.command('redraw')
        if not new_filename:
            return
        new = Path(defx._cwd).joinpath(new_filename)
        if not new or new == old:
            continue
        if str(new).lower() != str(old).lower() and new.exists():
            error(view._vim, f'{new} already exists')
            continue

        if not new.parent.exists():
            new.parent.mkdir(parents=True)
        old.rename(new)

        # Check rename
        # The old is directory, the path may be matched opened file
        if not new.is_dir():
            view._vim.call('defx#util#buffer_rename',
                           view._vim.call('bufnr', str(old)), str(new))

        view.redraw(True)
        view.search_recursive(new, defx._index)
Beispiel #10
0
    def _search(self, view: View, defx: Defx, context: Context) -> None:
        if not context.args or not context.args[0]:
            return

        search_path = context.args[0]
        view.search_recursive(Path(search_path), defx._index)