標籤:python version 提示資訊
Vim 外掛程式是一個 .vim 的指令檔,定義了函數、映射、文法規則和命令,可用於操作視窗、緩衝以及行。一般一個外掛程式包含了命令定義和事件鉤子。當使用 Python 編寫 vim 外掛程式時,函數外面是使用 VimL 編寫,儘管 VimL 學起來很快,但 Python 更加靈活,例如可以用 urllib/httplib/simplejson 來訪問某些 Web 服務,這也是為什麼很多需要訪問 Web 服務的外掛程式都是使用 VimL + Python 編寫的原因。
在開始編寫外掛程式之前,你需要確認 Vim 支援 Python,通過以下命令來判別:
vim --version | grep +python
接下來我們通過一個簡單的例子來學慣用 Python 編寫 Vim 外掛程式,該外掛程式用來擷取 Reddit 首頁資訊並顯示在當前緩衝區上。
首先在 Vim 建立 vimmit.vim 檔案,我們首先需要判斷是否支援 Python,如果不支援給出提示資訊:
1234 |
if !has( ‘python‘ ) echo "Error: Required vim compiled with +python" finish endif |
上面這段代碼就是用 VimL 編寫的,它將檢查 Vim 是否支援 Python。
下面是用 Python 編寫的 Reddit() 主函數:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
" Vim comments start with a double quote. " Function definition is VimL. We can mix VimL and Python in " function definition. function! Reddit() " We start the python code like the next line. python << EOF # the vim module contains everything we need to interface with vim from # python. We need urllib2 for the web service consumer. import vim, urllib2 # we need json for parsing the response import json # we define a timeout that we‘ll use in the API call. We don‘t want # users to wait much. TIMEOUT = 20 URL = "codego.net" try : # Get the posts and parse the json response response = urllib2.urlopen(URL, None , TIMEOUT).read() json_response = json.loads(response) posts = json_response.get( "data" , " ").get(" children ", " ") # vim.current.buffer is the current buffer. It‘s list-like object. # each line is an item in the list. We can loop through them delete # them, alter them etc. # Here we delete all lines in the current buffer del vim.current. buffer [:] # Here we append some lines above. Aesthetics. vim.current. buffer [ 0 ] = 80 * "-" for post in posts: # In the next few lines, we get the post details post_data = post.get( "data" , {}) up = post_data.get( "ups" , 0 ) down = post_data.get( "downs" , 0 ) title = post_data.get( "title" , "NO TITLE" ).encode( "utf-8" ) score = post_data.get( "score" , 0 ) permalink = post_data.get( "permalink" ).encode( "utf-8" ) url = post_data.get( "url" ).encode( "utf-8" ) comments = post_data.get( "num_comments" ) # And here we append line by line to the buffer. # First the upvotes vim.current. buffer .append( "↑ %s" % up) # Then the title and the url vim.current. buffer .append( " %s [%s]" % (title, url,)) # Then the downvotes and number of comments vim.current. buffer .append( "↓ %s | comments: %s [%s]" % (down, comments, permalink,)) # And last we append some "-" for visual appeal. vim.current. buffer .append( 80 * "-" ) except Exception, e: print e EOF " Here the python code is closed. We can continue writing VimL or python again. endfunction |
使用如下命令儲存檔案
:source vimmit.vim
然後調用該外掛程式:
:call Reddit()
這個命令用起來不那麼方便,因此我們再定義一個命令:
command! -nargs=0 Reddit call Reddit()
我們定義了命令:Reddit來調用這個函數。-nargs 參數聲明命令列中有多少個參數。
關於函數參數的問題:
問:如何訪問函數中的參數?
1234567891011121314 |
function! SomeName(arg1, arg2, arg3) " Get the first argument by name in VimL let firstarg = a:arg1 " Get the second argument by position in Viml let secondarg = a: 1 " Get the arguments in python python << EOF import vim first_argument = vim. eval ( "a:arg1" ) #or vim.eval("a:0") second_argument = vim. eval ( "a:arg2" ) #or vim.eval("a:1") |
你可以使用 ... 來處理可變個數參數來替換特定的參數名,可通過位置或者具名引數來訪問,如:(arg1, arg2, ...)
問:如何在 Python 中調用 Vim 命令?
vim.command("[vim-command-here]")
問:如何定義全域變數,並在 VimL 和 Python 中訪問?
全域變數使用形如 g:. 的首碼,定義全域變數前應該檢查該變數是否已定義:
123 |
if !exists( "g:reddit_apicall_timeout" ) let g:reddit_apicall_timeout = 40 endif |
然後你通過下面代碼在 Python 中訪問這個變數:
1 |
TIMEOUT = vim. eval ( "g:reddit_apicall_timeout" ) |
可通過下面的方法來對全域變數進行重新賦值:
1 |
let g:reddit_apicall_timeout = 60 |
更多關於使用 Python 編寫 Vim 外掛程式的說明請看官方文檔。
codego.net代碼節選
解釋Python編寫vim外掛程式