16
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NeovimのFloating Windowを使う

Last updated at Posted at 2019-07-28

はじめに

ヘルプを見ながらFloating Windowを出してみたので残しておきます。
NVIM v0.3.8ではnvim_create_buf()やオプションが存在しないのでnightlyを使用してください。
:h api-floatwinでFloating Windowの項目が開けます。

サンプル解説

ヘルプを開くとサンプルが載っているので1つずつ見ていきます。

let buf = nvim_create_buf(v:false, v:true)
call nvim_buf_set_lines(buf, 0, -1, v:true, ["test", "text"])
let opts = {'relative': 'cursor', 'width': 10, 'height': 2, 'col': 0, 'row': 1, 'anchor': 'NW', 'style': 'minimal'} 
let win = nvim_open_win(buf, 0, opts)
 " optional: change highlight, otherwise Pmenu is used
call nvim_win_set_option(win, 'winhl', 'Normal:MyHighlight')

"ウィンドウを閉じる。サンプルにはない。
call nvim_win_close(win, v:true)

nvim_create_buf({listed}, {scratch})

空のバッファを作る。

{listed}

オンの場合、バッファリストに載るようになる。

{scratch}

オンの場合、使い捨て用のスクラッチバッファを作成する。(バッファが変更されたことを意味する'modified'がオンにならない。)

nvim_buf_set_lines({buffer}, {start}, {end}, {strict_indexing}, {replacement})

バッファの内容を書き換える。
setline()の豪華版。

{buffer}

バッファハンドル。0の場合カレントバッファ。

{start} {end}

書き換える行数の始まりと終わり。同じ行を選ぶと、置換ではなく新しい行が作られる。
-1は末尾の下を表す。

{strict_indexing}

オンの場合、行数指定がバッファの範囲外のときエラーになる。 1

{replacement}

バッファに書き込みたいテキストの配列。

nvim_open_win({buffer}, {enter}, {config})

{buffer}

バッファハンドル。

{enter}

オンの場合、開いたウィンドウにフォーカスが行く。

config

辞書を渡して設定する。

relative

設定するとFloating Windowになる。
値にeditor win cursorと表示場所の基準を決められる。

win

relativewinを指定した場合、ウィンドウIDを指定できる。
デフォルトはカレントウィンドウ。

anchor

ウィンドウの原点をどこにするか決める。デフォルトは北西(NW)で、右クリックメニューのように左上が角になるように表示される。
値にはNW NE SW SEを設定する。

height width

ウィンドウの高さと幅。

row col

基準からの表示位置。

focusable

ウィンドウにフォーカスを移すことができるかを決める。
デフォルトはオン。

external

GUIのときサポートされていればexternal windowとして表示する(?)。
relativeと同時に設定できない。
neovim-qtではサポートされていなくて詳細不明。

style

現在は有効な値はminimalのみ。
'number' 'relativenumber' 'cursorline' 'cursorcolumn' 'spell' 'list' が無効になる。

nvim_win_set_option({window}, {name}, {value})

windowハンドルを指定してウィンドウオプションを指定できる。
'nil'を渡すとオプションを削除できる。

{window}

ウィンドウハンドル

{name}

オプション名

{value}

オプションの値

nvim_win_close({window}, {force})

:closeと同じ。

{window}

windowハンドル

{force}

オンにすると:close!と同じ。

実際に使ったもの

badapple.gif

VimでBad Apple!!のpopup windowをFloating Windowに書き換えてみました。

リポジトリはこちら

  1. E5555: API call: Index out of bounds

16
10
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
16
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?