"""Define interfaces."""import jsonimport os.pathimport timeimport vim # noqaclass VimPymodeEnviroment(object):"""Vim User interface."""prefix = '[Pymode]'def __init__(self):"""Init VIM environment."""self.current = vim.currentself.options = dict(encoding=vim.eval('&enc'))self.options['debug'] = self.var('g:pymode_debug', True)@propertydef curdir(self):"""Return current working directory."""return self.var('getcwd()')@propertydef curbuf(self):"""Return current buffer."""return self.current.buffer@propertydef cursor(self):"""Return current window position.:return tuple: (row, col)"""return self.current.window.cursor@propertydef source(self):"""Return source of current buffer."""return "\n".join(self.lines)@propertydef lines(self):"""Iterate by lines in current file.:return list:"""return self.curbuf@staticmethoddef var(name, to_bool=False, silence=False, default=None):"""Get vim variable.:return vimobj:"""try:value = vim.eval(name)except vim.error:if silence:return defaultraiseif to_bool:try:value = bool(int(value))except ValueError:value = valuereturn value@staticmethoddef message(msg, history=False):"""Show message to user.:return: :None"""if history:return vim.command('echom "%s"' % str(msg))return vim.command('call pymode#wide_message("%s")' % str(msg))def user_input(self, msg='', default=''):"""Return user input or default.:return str:"""prompt = []prompt.append(str(self.prefix.strip()))prompt.append(str(msg).strip())if default != '':prompt.append('[%s]' % default)prompt.append('> ')prompt = ' '.join([s for s in prompt if s])vim.command('echohl Debug')try:input_str = vim.eval('input(%r)' % (prompt,))except KeyboardInterrupt:input_str = ''vim.command('echohl none')return input_str or defaultdef user_confirm(self, msg, yes=False):"""Get user confirmation.:return bool:"""default = 'yes' if yes else 'no'action = self.user_input(msg, default)return action and 'yes'.startswith(action)def user_input_choices(self, msg, *options):"""Get one of many options.:return str: A choosen option"""choices = ['%s %s' % (self.prefix, msg)]choices += ["%s. %s" % (num, opt) for num, opt in enumerate(options, 1)]try:input_str = int(vim.eval('inputlist(%s)' % self.prepare_value(choices)))except (KeyboardInterrupt, ValueError):input_str = 0if not input_str:self.message('Cancelled!')return Falsetry:return options[input_str - 1]except (IndexError, ValueError):self.error('Invalid option: %s' % input_str)return self.user_input_choices(msg, *options)@staticmethoddef error(msg):"""Show error to user."""vim.command('call pymode#error("%s")' % str(msg))def debug(self, msg, *args):"""Print debug information."""if self.options.get('debug'):print("%s %s [%s]" % (int(time.time()), msg, ', '.join([str(a) for a in args])))def stop(self, value=None):"""Break Vim function."""cmd = 'return'if value is not None:cmd += ' ' + self.prepare_value(value)vim.command(cmd)def catch_exceptions(self, func):"""Decorator. Make execution more silence.:return func:"""def _wrapper(*args, **kwargs):try:return func(*args, **kwargs)except (Exception, vim.error) as e: # noqaif self.options.get('debug'):raiseself.error(e)return Nonereturn _wrapperdef run(self, name, *args):"""Run vim function."""vim.command('call %s(%s)' % (name, ", ".join([self.prepare_value(a) for a in args])))def let(self, name, value):"""Set variable."""cmd = 'let %s = %s' % (name, self.prepare_value(value))self.debug(cmd)vim.command(cmd)def prepare_value(self, value, dumps=True):"""Decode bstr to vim encoding.:return unicode string:"""if dumps:value = json.dumps(value)return valuedef get_offset_params(self, cursor=None, base=""):"""Calculate current offset.:return tuple: (source, offset)"""row, col = cursor or env.cursorsource = ""offset = 0for i, line in enumerate(self.lines, 1):if i == row:source += line[:col] + baseoffset = len(source)source += line[col:]else:source += linesource += '\n'env.debug('Get offset', base or None, row, col, offset)return source, offset@staticmethoddef goto_line(line):"""Go to line."""vim.command('normal %sggzz' % line)def goto_file(self, path, cmd='e', force=False):"""Open file by path."""if force or os.path.abspath(path) != self.curbuf.name:self.debug('read', path)if ' ' in path and os.name == 'posix':path = path.replace(' ', '\\ ')vim.command("%s %s" % (cmd, path))@staticmethoddef goto_buffer(bufnr):"""Open buffer."""if str(bufnr) != '-1':vim.command('buffer %s' % bufnr)env = VimPymodeEnviroment()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。