def reset_default_colors(self): for name, color in VimVar('NETRColors').items(): if name not in default.color: VimErrorMsg('netranger: {} is not a valid NETRColors key!') continue if type(color) is int and (color < 0 or color > 255): VimErrorMsg('netranger: Color value should be within 0~255') continue elif type(color) is str and color not in colortbl: VimErrorMsg('netranger: {} is not a valid color name!') continue default.color[name] = color
def NETRemotePush(self): """ Sync remote so that the remote content of the current directory will be the same as the local content. """ try: curBuf = self.curBuf except KeyError: VimErrorMsg('Not a netranger buffer') return if not self.isRemotePath(curBuf.wd): VimErrorMsg('Not a remote directory') else: self.rclone.sync(curBuf.wd, Rclone.SyncDirection.UP)
def NETRemotePull(self): """ Sync local so that the local content of the current directory will be the same as the remote content. """ try: curBuf = self.curBuf except KeyError: VimErrorMsg('Not a netranger buffer') return if not self.isRemotePath(curBuf.wd): VimErrorMsg('Not a remote directory') else: self.rclone.sync(curBuf.wd, Rclone.SyncDirection.DOWN) curBuf.refresh_nodes(force_refreh=True, cheap_remote_ls=True)
def NETRemoteList(self): if self.rclone is None: Rclone.valid_or_install(self.vim) self.rclone = Rclone(VimVar('NETRemoteCacheDir'), VimVar('NETRemoteRoots')) if self.rclone.has_remote: self.vim.command('tabe ' + VimVar('NETRemoteCacheDir')) else: VimErrorMsg( "There's no remote now. Run 'rclone config' in a terminal to setup remotes" )
def NETROpen(self, open_cmd=None, rifle_cmd=None, use_rifle=True): """ The real work for opening directories is handled in on_bufenter. For openning files, we check if there's rifle rule to open the file. Otherwise, open it in vim. """ curNode = self.curNode if curNode.isINFO: return if open_cmd is None: if curNode.isDir: open_cmd = 'edit' else: open_cmd = VimVar('NETROpenCmd') fullpath = curNode.fullpath if curNode.isDir: if curNode.size == '?' or curNode.size == '': VimErrorMsg('Permission Denied: {}'.format(curNode.name)) return self.vim.command('silent {} {}'.format(open_cmd, fullpath)) # manually call on_bufenter as vim might not trigger BufEnter with the above command self.on_bufenter(self.vim.eval("winnr()")) else: if self.rclone is not None and self.isRemotePath(fullpath): self.rclone.ensure_downloaded(fullpath) if rifle_cmd is None: rifle_cmd = self.rifle.decide_open_cmd(fullpath) if use_rifle and rifle_cmd is not None: Shell.run_async(rifle_cmd.format(fullpath)) else: try: self.vim.command('{} {}'.format(open_cmd, fullpath)) except Exception as e: err_msg = str(e) if 'E325' not in err_msg: VimErrorMsg(err_msg)
def Save(self): """ Rename the files according to current buffer content. Retur false if called but is_editing is false. Otherwise return true. """ if not self.is_editing: return False self.is_editing = False vimBuf = self.vim.current.buffer if len(self.nodes) != len(vimBuf): VimErrorMsg('Edit mode can not add/delete files!') self.render() return True oriNode = self.curNode change = {} i = 0 for i in range(len(vimBuf)): line = vimBuf[i].strip() if not self.nodes[i].isINFO and line != self.nodes[i].name: # change name of the i'th node oripath = self.nodes[i].rename(line) change[oripath] = self.nodes[i].fullpath # change dirname of subnodes endInd = self.next_lesseq_level_ind(begInd=i) for j in range(i + 1, endInd): self.nodes[j].change_dirname(oripath, self.nodes[i].fullpath) # apply the changes for oripath, fullpath in change.items(): self.fs.rename(oripath, fullpath) self.nodes = [self.header_node] + self.sortNodes( self.nodes) + [self.footer_node] self.render() self.setClineNoByNode(oriNode) self.vim.command('setlocal nowrap') self.vim.command('setlocal nomodifiable') return True
def Save(self): """ Rename the files according to current buffer content. """ vimBuf = self.vim.current.buffer if len(self.nodes) != len(vimBuf): VimErrorMsg('Edit mode can not add/delete files!') self.render() return oriNode = self.curNode change = {} i = 0 for i in range(len(vimBuf)): line = vimBuf[i].strip() if not self.nodes[i].isHeader and line != self.nodes[i].name: # if self.nodes[i].name in self.mtime: # self.mtime[line] = self.mtime[self.nodes[i].name] # del self.mtime[self.nodes[i].name] # change name of the i'th node oripath = self.nodes[i].rename(line) change[oripath] = self.nodes[i].fullpath # change dirname of subnodes endInd = self.next_lesseq_level_ind(begInd=i) for j in range(i + 1, endInd): self.nodes[j].change_dirname(oripath, self.nodes[i].fullpath) # apply the changes for oripath, fullpath in change.items(): self.fs.rename(oripath, fullpath) self.nodes = self.sortNodes(self.nodes) self.render() self.setClineNoByNode(oriNode)
def register_keymap(self, keys_fns): mapped_keys = [] for keys in self.keymaps.values(): mapped_keys += keys mapped_keys = set(mapped_keys) for keys, fn in keys_fns: if type(keys) is str: keys = [keys] real_keys = [] name = fn.__name__ for key in keys: if key in mapped_keys: VimErrorMsg( "netranger: Fail to bind key {} to {} because it has been mapped to other function." .format(key, name)) continue real_keys.append(key) self.keymaps[name] = real_keys assert not hasattr( self, name ), "Plugin of vim-netranger should not register a keymap with a function name already defined by vim-netranger." setattr(self, name, fn)
def __init__(self, vim, path): self.vim = vim self.rules = [] if not os.path.isfile(path): Shell.cp(os.path.join(config_dir, 'rifle.conf'), path) with open(path, 'r') as f: for i, line in enumerate(f): try: # remove things after the first # (including) line = line[:line.index('#')] except ValueError: pass line = line.strip() if len(line) == 0: continue sp = line.split('=') if len(sp) != 2: VimErrorMsg( 'invalid rule: rifle.conf line {}. There should be one and only one "=" for each line' .format(i + 1)) continue tests = [] for test in sp[0].strip().split(','): testSp = [e for e in test.split(' ') if e != ''] tests.append(globals()[testSp[0]](testSp[1])) command = sp[1].strip() # simple case, used specify only the command # For sophisicated command like bash -c "command {}" # user should add '{}' themselves if '{}' not in command: command = command + ' {}' self.rules.append((tests, command))
def print_error(job_id, err_msg): # Truncate unnecessary message if from fs_server.py ind = err_msg.rfind('FSServerException: ') if ind > 0: err_msg = err_msg[ind + 19:] VimErrorMsg(err_msg)
def run(cls, cmd): try: return subprocess.check_output( cmd, shell=True, stderr=subprocess.STDOUT).decode('utf-8') except subprocess.CalledProcessError as e: VimErrorMsg(e)