--I want to make a plugin for vim
――But I don't understand vimscript! !!
--I want to write in Python! !! !! !!
--If you enter MyNameIs <name>
, you will see Hello <name>
--Implementation escapes to python, vimscript is used like a python wrapper
I often use python
and pyfile
Python and pyfile are essentially the same, the difference between whether the source is written directly or separated into files. For example, the following two codes are the same
python:
python print('Hello')
pyfile
pyfile hello.py
hello.py
print('Hello')
Note that if you write python, that line is already in the Python world. I wrote the following and was very worried.
function! hello#hello(name)
python hello_hello(a:name)
endfuncion
It is easy to understand that this is a mistake by rewriting it as follows
function! hello#hello(name)
python << endpython
hello(a:name)
endpython
endfuncion
There is also a pydo
one. If you write pydo <body>
,
def _vim_pydo(line, linenr):
<body>
Is created, and the line contents are passed to line and the line number is passed to linenr for each line in the selected range. If you return a value, the line will be replaced with that value, but since it is a function, you must write return as a matter of course. So it's a little long and crap. It seems that ruby can write this kind of thing more beautifully.
For example, if you execute the code below, the line number will be inserted at the beginning of the line in the selection range.
pydo return str(linenr) + line
plugin/
hello.vim
autoload/
hello.vim
src/
hello.py
hello.py
Create a function without worrying about vim.
1/25 postscript: @Thnka pointed out the possibility of name conflicts. It pollutes the global space and must be prefixed or included in the class.
src/hello.py
def hello_hello(name):
print('Hello {0}'.format(name))
autoload/hello.vim
This is the liver
There are three things to do.
First, load the created python script. Note how to get the script path.
autoload/hello.vim
pyfile <sfile>:h:h/src/hello.py
Then import vim. If you do not do this, you will not be able to exchange values.
autoload/hello.vim
python import vim
Finally, create a function called from the plugin side and hit the python function inside the function. Since python and the missing line are already in the python world, pass arguments through the vim module.
autoload/hello.vim
function! hello#hello(name)
python hello(vim.eval('a:name'))
endfunction
plugin/hello.vim
This is no different from a normal plug-in. Bridging the command and the function described in autoload
plugin/hello.vim
command! -nargs=1 MyNameIs call hello#hello(<f-args>)
src/hello.py
def hello_hello(name):
print('Hello {0}'.format(name))
autoload/hello.vim
let s:save_cpo = &cpo
set cpo&vim
pyfile <sfile>:h:h/src/hello.py
python import vim
function! hello#hello(name)
python hello_hello(vim.eval('a:name'))
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo
plugin/hello.vim
if exists("g:loaded_hello")
finish
endif
let g:loaded_hello = 1
let s:save_cpo = &cpo
set cpo&vim
command! -nargs=1 MyNameIs call hello#hello(<f-args>)
let &cpo = s:save_cpo
unlet s:save_cpo
-Until the Vim plugin is made-Bocchi study session
I actually made a plugin called httpstatus.vim. You can see what the HTTP status code meant from Vim. The text uses Python's BaseHTTPServer.BaseHTTPRequestHandler.responses
.
I made it enthusiastically, but apparently mattn had already made it. In addition, it seems to be more sophisticated than the one I made this time (you can check not only the status code but also the message), but anyway I can only check from the status code, so it was enough.
Recommended Posts