LoginSignup
11
4

More than 3 years have passed since last update.

簡単なVimプラグインを作って基礎を学ぶ

Last updated at Posted at 2020-05-03

普段はVimで開発しており、様々なプラグインを使っていますが、自作プラグインというものに興味があったので簡単かつミニマムなプラグインを作ってみてVimScriptの基礎を学んでみます。

環境

  • macOS High Sierra
  • Vim8.2

内容

今回は天気予報APIを使ってその日の天気を教えてくれるプラグインを作ってみます。

ディレクトリ構成

まずvimプラグインのディレクトリは、

  • autoload
  • plugin

この2つのディレクトリを作ります。

autoload

autoload配下にファイルを作成すると、Vim起動時ではなくコマンド実行時にのみファイルを読むことが可能です。

plugin

plugin配下には実際に呼び出す関数を定義したファイルを作成します。

それぞれのディレクトリに、
show_weather.vim
というファイルを作成します。

.
├── autoload
│   └── show_weather.vim
└── plugin
    └── show_weather.vim

関数定義

実際に処理を行う関数を作成します。

autoload/show_weather.vim
function! show_weather#ShowWeather() abort
  " ここに処理を書いていく
endfunction

Vimではエラーが発生してもその後の処理も実行されますが、abortをつけておくとエラー発生時に即関数を抜けることが可能となります。

APIリクエスト

次に天気予報APIにリクエストを送る処理を書いていきます。
APIには無料のopenweathermapを利用します。
APIの利用には会員登録が必要となります。
https://openweathermap.org/

リクエストにはVimから外部コマンドcurlを叩いてその結果を得ます。
今回は東京の天気を取得します。
APIリクエストにAPIKeyが必要となりますので、vimrcなりに書いて呼び出すようにしておきましょう。

autoload/show_weather.vim
...
  let cmd = "curl -s 'http://api.openweathermap.org/data/2.5/weather?units=metric&q=tokyo,jp&APPID=".g:open_weather_appid."'"
.vimrc
let g:open_weather_appid = 'xxxxxxxxx'

g:変数名でグローバルな変数を定義出来ます。

分岐処理

実行前にcurlコマンドが使えるかの分岐処理を入れておきます。
他の言語同様にif文が使えるのでexist()の結果で分岐してみます。
外部コマンドはsystem()を使って実行が可能です。

autoload/show_weather.vim
...
  if executable('curl')
    let cmd = "curl -s 'http://api.openweathermap.org/data/2.5/weather?units=metric&q=tokyo,jp&APPID=".g:open_weather_appid."'"
    let result = json_decode(system(cmd))
  endif

Vim8からjson_decodeが使えるようになってので返ってきたjsonデータに実行します。
これでVimの値としてのデータアクセス可能です。

エラー処理

次にレスポンスコードが200以外はエラーを投げるように実装しておきます。
echoerrを利用することでエラー発生場所を表示できます。

autoload/show_weather.vim
...
    if result['cod'] == 200
      echo result['weather']
      echo result['main']
    else
      echoerr result
    endif

成功時はレスポンスのjsonから天気と気温などを表示するようにしておきました。雑。

最終的に出来上がったのはこんな感じです。

autoload/show_weather.vim
function! show_weather#ShowWeather() abort
  if executable('curl')
    let cmd = "curl -s 'http://api.openweathermap.org/data/2.5/weather?units=metric&q=tokyo,jp&APPID=".g:open_weather_appid."'"
    let result = json_decode(system(cmd))

    if result['cod'] == 200
      echo result['weather']
      echo result['main']
    else
      echoerr result
    endif
  else
    echoerr 'error! curl not available.'
  endif
endfunction

実行コマンド

最後にplugin配下のファイルに実行コマンド書いておきます。

plugin/show_weather.vim
command! ShowWeather call show_weather#ShowWeather()

これでVimから:ShowWeatherを実行すると天気データの入ったデータが表示されるようになりました。

[{'id': 804, 'description': 'overcast clouds', 'icon': '04n', 'main': 'Clouds'}]
{'pressure': 1007, 'temp': 16.3, 'temp_min': 12.78, 'feels_like': 14.93, 'temp_max': 18.33, 'humidity': 79}

まとめ

今回のソースは一応githubにあげておきました。
https://github.com/sakupa802/vim-show-weather

VimScriptの基本は理解出来たので、次回はもっと実用的なレベルのものを公開できればと思ってます。
Vimって素晴らしい。

参考

https://knowledge.sakura.ad.jp/23436/
http://secret-garden.hatenablog.com/entry/2016/10/11/000000

11
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
4