def _set(self, mark): """ The callback for the BookMarkUI/set. """ if mark == '': return if mark not in self.valid_mark: Vim.command('echo "Only a-zA-Z are valid mark!!"') return set_buf = self.bufs['set'] set_buf.options['modifiable'] = True if mark in self.mark_dict: for i, line in enumerate(set_buf): if len(line) > 0 and line[0] == mark: set_buf[i] = f'{mark}:{self.path_to_mark}' break elif self.path_to_mark in self.mark_dict.values(): for i, line in enumerate(set_buf): if len(line) > 0 and line[2:] == self.path_to_mark: set_buf[i] = f'{mark}:{self.path_to_mark}' break else: set_buf.append(f'{mark}:{self.path_to_mark}') set_buf.options['modifiable'] = False self.mark_dict[mark] = self.path_to_mark self.del_buf('go') with open(Vim.Var('NETRBookmarkFile'), 'w') as f: for k, p in self.mark_dict.items(): f.write(f'{k}:{p}\n')
def view_mime(self): Vim.command('setlocal buftype=nofile') Vim.command('setlocal noswapfile') Vim.command('setlocal nobuflisted') Vim.command('setlocal bufhidden=wipe') Vim.current.buffer[:] = [self.path, self.mime_type] Vim.command('setlocal nomodifiable')
def map_key_reg(self, key, regval): """ Register a key mapping in a UI buffer. The mapped action is: 1. Store regval into g:NETRRegister, which serves as the argument for netranger buffer's call back after UI buffer quit. 2. Quit the UI buffer. See Netranger.pend_onuiquit for details. """ Vim.command(f'nnoremap <nowait> <silent> <buffer> {key} ' f':let g:NETRRegister=["{regval}"] <cr> :quit <cr>')
def list_remotes_in_vim_buffer(self): if not Shell.isinPATH('rclone'): self.install_rclone() remotes = set(self.cmd_listremotes()) local_remotes = set(super(Rclone, self).ls(self.root_dir)) for remote in remotes.difference(local_remotes): Shell.mkdir(os.path.join(self.root_dir, remote)) for remote in local_remotes.difference(remotes): Shell.rm(os.path.join(self.root_dir, remote)) if len(remotes) > 0: Vim.command(f'NETRTabdrop {self.root_dir}') else: Vim.ErrorMsg( "There's no remote now. Run 'rclone config' in a terminal to " "setup remotes and restart vim again.")
def view_image(self): try: import ueberzug except ModuleNotFoundError: Vim.ErrorMsg('Please install ueberzug for image preview') self.view_mime() return path = self.path if self.path.endswith('gif') and Shell.isinPATH('convert'): if self.path not in self.tempfile_cache: dir = tempfile.TemporaryDirectory().name Shell.mkdir(dir) self.tempfile_cache[self.path] = dir dir = self.tempfile_cache[self.path] path = dir Shell.run(f'convert -deconstruct "{self.path}" {dir}/a.png') Vim.AsyncRun(f'{util.GenNetRangerScriptCmd("image_preview")}\ "{path}" {self.total_width} {self.preview_width}', term=True, termopencmd='') Vim.command('setlocal nocursorline')
def edit(self): """ Show the buffer for editing the bookmark. """ Vim.command(f'belowright split {Vim.Var("NETRBookmarkFile")}') Vim.command('wincmd J') Vim.command('setlocal bufhidden=wipe') self.del_buf('set') self.del_buf('go') self.netranger.pend_onuiquit(self.load_bookmarks)
def create_buf(self, content, mappings=None, name='default', map_cr=False): """ Create the UI buffer. """ Vim.command(f'{self.position} new') self.set_buf_common_option() new_buf = Vim.current.buffer self.bufs[name] = new_buf if mappings is not None: for k, v in mappings: self.map_key_reg(k, v) if map_cr: assert mappings is not None ui_internal_vim_dict_name = f'g:_{type(self).__name__}Map' Vim.command(f'let {ui_internal_vim_dict_name}={dict(mappings)}') Vim.command( "nnoremap <nowait> <silent> <buffer> <Cr> " ":let g:NETRRegister=[{}[getline('.')[0]]] <cr> :quit <cr>". format(ui_internal_vim_dict_name)) new_buf.options['modifiable'] = True new_buf[:] = content new_buf.options['modifiable'] = False Vim.command('quit')
def set_buf_common_option(self, modifiable=False): """ Set common option for a UI buffer. """ Vim.command('setlocal noswapfile') Vim.command('setlocal foldmethod=manual') Vim.command('setlocal foldcolumn=0') Vim.command('setlocal nofoldenable') Vim.command('setlocal nobuflisted') Vim.command('setlocal nospell') Vim.command('setlocal buftype=nofile') Vim.command('setlocal bufhidden=hide') Vim.command('setlocal nomodifiable')
def show(self, name='default'): """ Show the UI buffer. """ Vim.command(f'{self.position} {self.bufs[name].number}sb') Vim.command('wincmd J')
def view_plaintext(self): bak_shortmess = Vim.options['shortmess'] Vim.options['shortmess'] = 'A' Vim.command(f'silent edit {self.path}') Vim.options['shortmess'] = bak_shortmess Vim.current.window.options['foldenable'] = False