Beispiel #1
0
    def _add_session(self, view: View, defx: Defx, context: Context) -> None:
        path = context.args[0] if context.args else defx._cwd
        if path[-1] == '/':
            # Remove the last slash
            path = path[:-1]

        opened_candidates = [] if context.args else list(
            defx._opened_candidates)
        opened_candidates.sort()

        session: Session
        if path in view._sessions:
            old_session = view._sessions[path]
            session = Session(name=old_session.name,
                              path=old_session.path,
                              opened_candidates=opened_candidates)
        else:
            name = Path(path).name
            session = Session(name=name,
                              path=path,
                              opened_candidates=opened_candidates)
            view.print_msg(f'session "{name}" is created')

        view._sessions[session.path] = session

        self._save_session(view, defx, context)
Beispiel #2
0
def _paste(view: View, defx: Defx, context: Context) -> None:
    action = view._clipboard.action
    for index, candidate in enumerate(view._clipboard.candidates):
        path = candidate['action__path']
        dest = Path(defx._cwd).joinpath(path.name)
        if dest.exists():
            overwrite = check_overwrite(view, dest, path)
            if overwrite == Path(''):
                continue
            dest = Path(defx._cwd).joinpath(overwrite.name)

        if 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:
            shutil.move(str(path), defx._cwd)
        view._vim.command('redraw')
    view._vim.command('echo')
Beispiel #3
0
def _yank_path(view: View, defx: Defx, context: Context) -> None:
    yank = '\n'.join([str(x['action__path']) for x in context.targets])
    view._vim.call('setreg', '"', yank)
    if (view._vim.call('has', 'clipboard')
            or view._vim.call('has', 'xterm_clipboard')):
        view._vim.call('setreg', '+', yank)
    view.print_msg('Yanked:\n' + yank)
Beispiel #4
0
def _execute_command(view: View, defx: Defx, context: Context) -> None:
    """
    Execute the command.
    """

    command = context.args[0] if context.args else view._vim.call(
        'defx#util#input', 'Command: ', '', 'shellcmd')
    if not command:
        return

    view._vim.command('redraw')

    def parse_argument(arg: str) -> str:
        if not arg.startswith('%'):
            return arg
        arg = arg[1:]
        m = re.match(r'((:.)*)(.*)', arg)
        target_path = str(target['action__path'])
        if not m:
            return target_path
        return fnamemodify(view._vim, target_path, m.group(2)) + m.group(3)

    for target in context.targets:
        args = [parse_argument(x) for x in shlex.split(command)]
        output = subprocess.check_output(args, cwd=defx._cwd)
        if output:
            view.print_msg(output)
Beispiel #5
0
def _paste(view: View, defx: Defx, context: Context) -> None:
    candidate = view.get_cursor_candidate(context.cursor)
    if not candidate:
        return
    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 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:
            shutil.move(str(path), cwd)
        view._vim.command('redraw')
    view._vim.command('echo')

    view.redraw(True)
    if dest:
        view.search_file(dest, defx._index)
Beispiel #6
0
def _move(view: View, defx: Defx, context: Context) -> None:
    message = 'Move to the clipboard: {}'.format(
        str(context.targets[0]['action__path']) if len(context.targets) ==
        1 else str(len(context.targets)) + ' files')
    view.print_msg(message)

    view._clipboard.action = ClipboardAction.MOVE
    view._clipboard.candidates = context.targets
Beispiel #7
0
def _copy(view: View, defx: Defx, context: Context) -> None:
    if not context.targets:
        return

    message = 'Copy to the clipboard: {}'.format(
        str(context.targets[0]['action__path']) if len(context.targets) ==
        1 else str(len(context.targets)) + ' files')
    view.print_msg(message)

    view._clipboard.action = ClipboardAction.COPY
    view._clipboard.candidates = context.targets
Beispiel #8
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 #9
0
 def _yank_path(self, view: View, defx: Defx, context: Context) -> None:
     mods = context.args[0] if context.args else ''
     paths = [str(x['action__path']) for x in context.targets]
     if mods:
         paths = [view._vim.call('fnamemodify', x, mods) for x in paths]
     yank = '\n'.join(paths)
     view._vim.call('setreg', '"', yank)
     if (view._vim.call('has', 'clipboard')
             or view._vim.call('has', 'xterm_clipboard')):
         view._vim.call('setreg', '+', yank)
     view.print_msg('Yanked:\n' + yank)
Beispiel #10
0
    def _move(self, view: View, defx: Defx, context: Context) -> None:
        if not context.targets:
            return

        message = 'Move to the clipboard: {}'.format(
            str(context.targets[0]['action__path']) if len(context.targets) ==
            1 else str(len(context.targets)) + ' files')
        view.print_msg(message)

        view._clipboard.action = ClipboardAction.MOVE
        view._clipboard.candidates = context.targets
        view._clipboard.source_name = defx._source.name
Beispiel #11
0
def _link(view: View, defx: Defx, context: Context) -> None:
    if not context.targets:
        return

    message = 'Link to the clipboard: {}'.format(
        str(context.targets[0]['action__path'])
        if len(context.targets) == 1
        else str(len(context.targets)) + ' files')
    view.print_msg(message)

    view._clipboard.action = ClipboardAction.LINK
    view._clipboard.candidates = context.targets
    view._clipboard.source_name = 'file'
Beispiel #12
0
def check_overwrite(view: View, dest: Path, src: Path) -> Path:
    s_stat = src.stat()
    s_mtime = s_stat.st_mtime
    view.print_msg(f' src: {src} {s_stat.st_size} bytes')
    view.print_msg(f'      {time.strftime("%c", time.localtime(s_mtime))}')
    d_stat = dest.stat()
    d_mtime = d_stat.st_mtime
    view.print_msg(f'dest: {dest} {d_stat.st_size} bytes')
    view.print_msg(f'      {time.strftime("%c", time.localtime(d_mtime))}')

    choice: int = view._vim.call('confirm',
                                 f'{dest} already exists.  Overwrite?',
                                 '&Force\n&No\n&Rename\n&Time\n&Underbar', 0)
    ret: Path = Path('')
    if choice == 1:
        ret = src
    elif choice == 2:
        ret = Path('')
    elif choice == 3:
        ret = Path(
            view._vim.call('input', f'{src} -> ', str(src),
                           ('dir' if src.is_dir() else 'file')))
    elif choice == 4 and d_mtime < s_mtime:
        ret = src
    elif choice == 5:
        ret = Path(str(src) + '_')
    return ret
Beispiel #13
0
    def check_overwrite(self, view: View, dest: PathLike,
                        src: PathLike) -> PathLike:
        if not src.exists() or not dest.exists():
            return self.path_maker('')

        s_stat = src.stat()
        s_mtime = s_stat.st_mtime
        view.print_msg(f' src: {src} {s_stat.st_size} bytes')
        view.print_msg(f'      {time.strftime("%c", time.localtime(s_mtime))}')
        d_stat = dest.stat()
        d_mtime = d_stat.st_mtime
        view.print_msg(f'dest: {dest} {d_stat.st_size} bytes')
        view.print_msg(f'      {time.strftime("%c", time.localtime(d_mtime))}')

        choice: int = view._vim.call('defx#util#confirm',
                                     f'{dest} already exists.  Overwrite?',
                                     '&Force\n&No\n&Rename\n&Time\n&Underbar',
                                     0)

        ret: PathLike = self.path_maker('')
        if choice == 1:
            ret = dest
        elif choice == 2:
            ret = self.path_maker('')
        elif choice == 3:
            ret = self.path_maker(
                view._vim.call('defx#util#input', f'{src} -> ', str(dest),
                               ('dir' if src.is_dir() else 'file')))
        elif choice == 4 and d_mtime < s_mtime:
            ret = src
        elif choice == 5:
            ret = self.path_maker(str(dest) + '_')
        return ret
Beispiel #14
0
def _execute_command(view: View, defx: Defx, context: Context) -> None:
    """
    Execute the command.
    """
    save_cwd = view._vim.call('getcwd')
    cd(view._vim, defx._cwd)

    command = context.args[0] if context.args else view._vim.call(
        'input', 'Command: ', '', 'shellcmd')

    output = view._vim.call('system', command)
    if output:
        view.print_msg(output)

    cd(view._vim, save_cwd)
Beispiel #15
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 #16
0
def _print(view: View, defx: Defx, context: Context) -> None:
    for target in context.targets:
        view.print_msg(str(target['action__path']))
Beispiel #17
0
 def _remove_trash(self, view: View, defx: Defx, context: Context) -> None:
     view.print_msg('remove_trash is not supported')
Beispiel #18
0
def check_output(view: View, cwd: str, args: typing.List[str]) -> None:
    output = subprocess.check_output(args, cwd=cwd)
    if output:
        view.print_msg(output)