def inst(nvim: Nvim) -> Atomic: pkgs = { path: spec for path, spec in ((p_name(spec.git.uri), spec) for spec in pkg_specs) if path.exists() } atomic1 = Atomic() keymap = Keymap() for spec in pkgs.values(): for key in spec.keys: for lhs, rhs in key.maps.items(): attrgetter(key.modes)(keymap)(lhs, **asdict(key.opts)) << rhs for lhs, rhs in spec.vals.items(): atomic1.set_var(lhs, rhs) atomic2 = rtp_packages(nvim, plugins=pkgs) for spec in pkgs.values(): if spec.lua: lua = indent(spec.lua, " " * 2) lua_block = f"(function(){linesep}{lua}{linesep}end)()" atomic2.call_function("luaeval", (lua_block, ())) if spec.viml: atomic2.command(spec.viml) return atomic1 + keymap.drain(buf=None) + atomic2
def inst(nvim: Nvim) -> Atomic: pkgs = { path: spec for path, spec in ((p_name(spec.git.uri), spec) for spec in pkg_specs) if path.exists() } atomic1 = Atomic() keymap = Keymap() for spec in pkgs.values(): for key in spec.keys: for lhs, rhs in key.maps.items(): attrgetter(key.modes)(keymap)(lhs, **asdict(key.opts)) << rhs for lhs, rhs in spec.vals.items(): atomic1.set_var(lhs, rhs) atomic2 = Atomic() atomic2.command("packloadall") for spec in pkgs.values(): if spec.lua: body = indent(spec.lua, " " * 2) lua = f""" (function() local _, err = pcall(function() {body} end) if err then vim.api.nvim_err_writeln(err) end end)() """ atomic2.exec_lua(dedent(lua), ()) if spec.viml: lua = f""" (function(viml) local _, err = pcall(vim.cmd, viml) if err then vim.api.nvim_err_writeln(err) end end)(...) """ atomic2.exec_lua(dedent(lua), (spec.viml, )) return atomic1 + keymap.drain(buf=None) + atomic2
def new_fm_buffer(nvim: Nvim, settings: Settings) -> Buffer: buf = create_buf(nvim, listed=False, scratch=True, wipe=False, nofile=True) buf_set_option(nvim, buf=buf, key="modifiable", val=False) buf_set_option(nvim, buf=buf, key="filetype", val=FM_FILETYPE) km = Keymap() km.n("{") << f"{settings.page_increment}<up>" km.n("}") << f"{settings.page_increment}<down>" for function, mappings in settings.keymap.items(): for mapping in mappings: km.n(mapping, noremap=True, silent=True, nowait=True) << f"<cmd>lua {function}(false)<cr>" km.v(mapping, noremap=True, silent=True, nowait=True) << f"<esc><cmd>lua {function}(true)<cr>" km.drain(buf=buf).commit(nvim) return buf
from pynvim import Nvim from pynvim_pp.atomic import Atomic from pynvim_pp.autocmd import AutoCMD from pynvim_pp.keymap import Keymap from pynvim_pp.rpc import RPC, RpcSpec from pynvim_pp.settings import Settings from .components.localization import load from .components.rtp import inst from .consts import PATH, PYTHONPATH LANG = load(code=None) atomic = Atomic() autocmd = AutoCMD() keymap = Keymap() rpc = RPC() settings = Settings() def drain(nvim: Nvim) -> Tuple[Atomic, Sequence[RpcSpec]]: _atomic = Atomic() _atomic.call_function("setenv", ("PATH", PATH)) _atomic.call_function("setenv", ("PYTHONPATH", PYTHONPATH)) _atomic.set_var("mapleader", " ") _atomic.set_var("maplocalleader", " ") a0 = inst(nvim) a1 = settings.drain() a2, s0 = rpc.drain(nvim.channel_id) a3 = keymap.drain(buf=None)
def set_options(nvim: Nvim, mapping: KeyMapping, fast_close: bool) -> None: settings = Settings() keymap = Keymap() settings["completefunc"] = f"v:lua.{NAMESPACE}.{omnifunc.name}" if mapping.eval_snips: (keymap.n(mapping.eval_snips) << f"<cmd>lua {NAMESPACE}.{eval_snips.name}(false)<cr>") (keymap.v(mapping.eval_snips) << rf"<c-\><c-n><cmd>lua {NAMESPACE}.{eval_snips.name}(true)<cr>") if mapping.bigger_preview: (keymap.i(mapping.bigger_preview, expr=True) << f"(pumvisible() && complete_info(['mode']).mode ==# 'eval') ? {preview_preview.name}() : '{mapping.bigger_preview}'" ) if mapping.jump_to_mark: keymap.n(mapping.jump_to_mark ) << f"<cmd>lua {NAMESPACE}.{nav_mark.name}()<cr>" (keymap.iv(mapping.jump_to_mark) << rf"<c-\><c-n><cmd>lua {NAMESPACE}.{nav_mark.name}()<cr>") if mapping.repeat: keymap.n(mapping.repeat) << f"<cmd>lua {NAMESPACE}.{repeat.name}()<cr>" if mapping.manual_complete: (keymap.i(mapping.manual_complete, expr=True) << "pumvisible() ? '<c-e><c-x><c-u>' : '<c-x><c-u>'") keymap.nv(mapping.manual_complete) << r"<c-\><c-n>i<c-x><c-u>" settings["completeopt"] += ( "noinsert", "menuone", *(() if mapping.pre_select else ("noselect", )), ) if mapping.recommended: keymap.i("<esc>", expr=True) << "pumvisible() ? '<c-e><esc>' : '<esc>'" keymap.i("<c-c>", expr=True) << "pumvisible() ? '<c-e><c-c>' : '<c-c>'" keymap.i("<bs>", expr=True) << "pumvisible() ? '<c-e><bs>' : '<bs>'" keymap.i("<c-w>", expr=True) << "pumvisible() ? '<c-e><c-w>' : '<c-w>'" keymap.i("<c-u>", expr=True) << "pumvisible() ? '<c-e><c-u>' : '<c-u>'" (keymap.i("<cr>", expr=True) << "pumvisible() ? (complete_info(['selected']).selected == -1 ? '<c-e><cr>' : '<c-y>') : '<cr>'" ) keymap.i("<tab>", expr=True) << "pumvisible() ? '<c-n>' : '<tab>'" keymap.i("<s-tab>", expr=True) << "pumvisible() ? '<c-p>' : '<bs>'" if fast_close: settings["shortmess"] += "c" (settings.drain() + keymap.drain(buf=None)).commit(nvim)