Home Explore Blog CI



neovim

4th chunk of `runtime/doc/if_pyth.txt`
1e40fddbbe2d341fbb9f18c607b7c04f71e1434cb0b2f2230000000100000fa0
 command specifies a range, this range of lines becomes the
	"current range".  A range is a bit like a buffer, but with all access
	restricted to a subset of lines.  See |python-range| for more details.

	Note: When assigning to vim.current.{buffer,window,tabpage} it expects
	valid |python-buffer|, |python-window| or |python-tabpage| objects
	respectively. Assigning triggers normal (with |autocommand|s)
	switching to given buffer, window or tab page. It is the only way to
	switch UI objects in python: you can't assign to
	|python-tabpage|.window attribute. To switch without triggering
	autocommands use >vim
	    py << EOF
	    saved_eventignore = vim.options['eventignore']
	    vim.options['eventignore'] = 'all'
	    try:
	        vim.current.buffer = vim.buffers[2] # Switch to buffer 2
	    finally:
	        vim.options['eventignore'] = saved_eventignore
	    EOF
<
vim.vars						*python-vars*
vim.vvars						*python-vvars*
	Dictionary-like objects holding dictionaries with global (|g:|) and
	vim (|v:|) variables respectively.

vim.options						*python-options*
	Object partly supporting mapping protocol (supports setting and
	getting items) providing a read-write access to global options.
	Note: unlike |:set| this provides access only to global options. You
	cannot use this object to obtain or set local options' values or
	access local-only options in any fashion. Raises KeyError if no global
	option with such name exists (i.e. does not raise KeyError for
	|global-local| options and global only options, but does for window-
	and buffer-local ones).  Use |python-buffer| objects to access to
	buffer-local options and |python-window| objects to access to
	window-local options.

	Type of this object is available via "Options" attribute of vim
	module.

Output from Python					*python-output*
	Vim displays all Python code output in the Vim message area.  Normal
	output appears as information messages, and error output appears as
	error messages.

	In implementation terms, this means that all output to sys.stdout
	(including the output from print statements) appears as information
	messages, and all output to sys.stderr (including error tracebacks)
	appears as error messages.

							*python-input*
	Input (via sys.stdin, including input() and raw_input()) is not
	supported, and may cause the program to crash.  This should probably be
	fixed.

				  *python3-directory* *pythonx-directory*
Python 'runtimepath' handling				*python-special-path*

In python vim.VIM_SPECIAL_PATH special directory is used as a replacement for
the list of paths found in 'runtimepath': with this directory in sys.path and
vim.path_hooks in sys.path_hooks python will try to load module from
{rtp}/python3 and {rtp}/pythonx for each {rtp} found in 'runtimepath'.

Implementation is similar to the following, but written in C: >python

    from imp import find_module, load_module
    import vim
    import sys

    class VimModuleLoader(object):
        def __init__(self, module):
            self.module = module

        def load_module(self, fullname, path=None):
            return self.module

    def _find_module(fullname, oldtail, path):
        idx = oldtail.find('.')
        if idx > 0:
            name = oldtail[:idx]
            tail = oldtail[idx+1:]
            fmr = find_module(name, path)
            module = load_module(fullname[:-len(oldtail)] + name, *fmr)
            return _find_module(fullname, tail, module.__path__)
        else:
            fmr = find_module(fullname, path)
            return load_module(fullname, *fmr)

    # It uses vim module itself in place of VimPathFinder class: it does not
    # matter for python which object has find_module function attached to as
    # an attribute.
    class VimPathFinder(object):
        @classmethod
        def find_module(cls, fullname, path=None):
            try:
                return VimModuleLoader(_find_module(fullname, fullname, path or vim._get_paths()))
            except ImportError:
       

Title: NVim Python Interface: `vim.vars`, `vim.vvars`, `vim.options`, Output and Input, and Runtimepath Handling
Summary
This section elaborates on `vim.vars` and `vim.vvars`, which provide dictionary-like access to global and vim variables. It explains `vim.options` for read-write access to global options, noting its limitations compared to `:set`. It covers how Python output is displayed in Vim (stdout as information, stderr as errors) and the lack of support for input. Finally, it describes how Python handles 'runtimepath' using `vim.VIM_SPECIAL_PATH` for loading modules from `python3` and `pythonx` directories within 'runtimepath', providing a Python-like implementation for context.