smartinput の <BS> や <CR> の汎用性を高める


この投稿は、変にややこしいやり方を使ってたこの投稿を改善したものです;-)

smartinput は括弧のペアを入力してくれるプラグインとして知られてますが、ペアになった括弧をまとめて削除するような機能もあります。しかし、<BS><CR>に別のプラグインの機能をマッピングしていて忘れ去られていることが多いように思います。(※当社比)

この問題に対処するためにsmartinputが提供する機能を含んだ<BS><CR>に対して代わりの名前を付けて、呼び出しやすくします。

まずは、 smartinput 側の設定です。

call smartinput#map_to_trigger('i', '<Plug>(smartinput_BS)',
      \                        '<BS>',
      \                        '<BS>')
call smartinput#map_to_trigger('i', '<Plug>(smartinput_C-h)',
      \                        '<BS>',
      \                        '<C-h>')
call smartinput#map_to_trigger('i', '<Plug>(smartinput_CR)',
      \                        '<Enter>',
      \                        '<Enter>')

これで、smartinputが提供する機能を含んだ<BS><C-h><CR>を、<Plug>(smartinput_BS)<Plug>(smartinput_C-h)<Plug>(smartinput_CR)で使えるようになりました。

例としてこれらを、 neocomplcache の一般的な設定に当てはめて利用してみましょう。

" <BS> でポップアップを閉じて文字を削除
imap <expr> <BS>
      \ neocomplcache#smart_close_popup() . "\<Plug>(smartinput_BS)"

" <C-h> でポップアップを閉じて文字を削除
imap <expr> <C-h>
      \ neocomplcache#smart_close_popup() . "\<Plug>(smartinput_C-h)"


" <CR> でポップアップ中の候補を選択し改行する
imap <expr> <CR>
      \ neocomplete#smart_close_popup() . "\<Plug>(smartinput_CR)"

" <CR> でポップアップ中の候補を選択するだけで、改行はしないバージョン
" ポップアップがないときには改行する
imap <expr> <CR> pumvisible() ?
      \ neocomplcache#close_popup() : "\<Plug>(smartinput_CR)"

<BS><CR><C-h>の代わりに、新たに作ったマッピングを利用しています。また、inoremapimapに変更しているのがポイントです。