亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

使用Python編寫vim插件的簡單示例

系統(tǒng) 1836 0

?Vim 插件是一個 .vim 的腳本文件,定義了函數(shù)、映射、語法規(guī)則和命令,可用于操作窗口、緩沖以及行。一般一個插件包含了命令定義和事件鉤子。當(dāng)使用 Python 編寫 vim 插件時,函數(shù)外面是使用 VimL 編寫,盡管 VimL 學(xué)起來很快,但 Python 更加靈活,例如可以用 urllib/httplib/simplejson 來訪問某些 Web 服務(wù),這也是為什么很多需要訪問 Web 服務(wù)的插件都是使用 VimL + Python 編寫的原因。


在開始編寫插件之前,你需要確認(rèn) Vim 支持 Python,通過以下命令來判別:
?

復(fù)制代碼 代碼如下:
vim --version | grep +python


接下來我們通過一個簡單的例子來學(xué)習(xí)用 Python 編寫 Vim 插件,該插件用來獲取 Reddit 首頁信息并顯示在當(dāng)前緩沖區(qū)上。

首先在 Vim 新建 vimmit.vim 文件,我們首先需要判斷是否支持 Python,如果不支持給出提示信息:
?

            
if !has('python')
  echo "Error: Required vim compiled with +python"
  finish
endif

          

上面這段代碼就是用 VimL 編寫的,它將檢查 Vim 是否支持 Python。


下面是用 Python 編寫的 Reddit() 主函數(shù):

?

            
" 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 = "http://reddit.com/.json"
 
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

          

使用如下命令保存文件
?

復(fù)制代碼 代碼如下:
:source vimmit.vim

然后調(diào)用該插件:
?

復(fù)制代碼 代碼如下:
:call Reddit()

這個命令用起來不那么方便,因此我們再定義一個命令:

復(fù)制代碼 代碼如下:
command! -nargs=0 Reddit call Reddit()

我們定義了命令:Reddit來調(diào)用這個函數(shù)。-nargs 參數(shù)聲明命令行中有多少個參數(shù)。


關(guān)于函數(shù)參數(shù)的問題:

問:如何訪問函數(shù)中的參數(shù)?
?

            
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")

          

你可以使用 ... 來處理可變個數(shù)參數(shù)來替換特定的參數(shù)名,可通過位置或者命名參數(shù)來訪問,如:(arg1, arg2, ...)

問:如何在 Python 中調(diào)用 Vim 命令?
?

復(fù)制代碼 代碼如下:
vim.command("[vim-command-here]")

問:如何定義全局變量,并在 VimL 和 Python 中訪問?

全局變量使用形如 g:. 的前綴,定義全局變量前應(yīng)該檢查該變量是否已定義:
?

            
if !exists("g:reddit_apicall_timeout")
  let g:reddit_apicall_timeout=40
endif

          

然后你通過下面代碼在 Python 中訪問這個變量:
?

            
TIMEOUT = vim.eval("g:reddit_apicall_timeout")

          

可通過下面的方法來對全局變量進(jìn)行重新賦值:
?

            
let g:reddit_apicall_timeout=60

          

更多關(guān)于使用 Python 編寫 Vim 插件的說明請看官方文檔。


備注:

一旦你用過VimL,就會發(fā)現(xiàn)它挺簡單的,你用python寫的代碼也可以用它來實現(xiàn)。詳細(xì)請參考vim python模塊文檔,這是一份重要的參考資料。

除了上述文檔,你也可以在IBM developerWorks網(wǎng)站找到一些有用的資料。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日本免费人成黄页网观看视频 | 日韩中文字幕网站 | 国产高清视频青青青在线 | 最新国产网站 | 日本一级毛片片在线播放 | 一区二区三区在线免费看 | 亚洲国产综合自在线另类 | 免费一级大毛片a一观看不卡 | 日本久久久久中文字幕 | 青青久久精品国产免费看 | 日本一级毛一级毛片短视频 | 久久综合亚洲伊人色 | 国产亚洲欧美在线 | 久久爱综合久久爱com | 99热久久久久久久免费观看 | 日本亚洲一区二区三区 | 手机看片欧美 | 2020年国产高中毛片在线视频 | 欧美成人久久一级c片免费 欧美成人剧情中文字幕 | 中文在线1区二区六区 | 久久天堂网 | 国产一区二区三区免费视频 | 国产高清一区二区三区视频 | 好好的日com欧美 | a级毛片免费完整视频 | 欧美日韩国产另类一区二区三区 | 狠狠色噜噜狠狠狠米奇9999 | 一级特黄aaaaaa大片 | 欧美性色黄大片一级毛片视频 | 俄罗斯色视频 | 九九国产在线视频 | 国产一区二区三区日韩 | 欧美色精品 | 国产精品成人第一区 | 黄色成人在线网站 | 97在线观看成人免费视频 | 一区二区日韩欧美 | 亚洲精品一区二区不卡 | 亚洲精品欧美精品一区二区 | 99久久香蕉国产线看观香 | 色视频网站人成免费 |