unite.vim で vim-pukiwiki の検索をする その 4

  1. unite.vim で vim-pukiwiki の検索をする その 1 - vim 初心者の作業メモ
  2. unite.vim で vim-pukiwiki の検索をする その 2 - vim 初心者の作業メモ
  3. unite.vim で vim-pukiwiki の検索をする その 3 - vim 初心者の作業メモ
  4. unite.vim で vim-pukiwiki の検索をする その 4 - vim 初心者の作業メモ

unite-pukiwiki/history を作る.

作るもの

「最終更新」のリストを目指していたが、これを作るには PukiWiki に接続しないといけないので手間がかかる.
そこで,内部に持っている表示履歴を unite.vim で表示するようにした.
以下のコマンドで, 表示したページの履歴があらわれ, 移動 action で該当のページを開く.

:Unite pukiwiki/history

やらないといけないこと

kind を何にするか
  • 実行するのは :PukiWiki コマンドだけど command になるのは何か美しくない
    • openable なのかな. open するのは "ファイル" ということではなさげなにおいもしなくもないけど...
      • "Defines an interface for files" 無理か.
  • can open なのか can be open(ed) なのか.

openable An interface that can open. This doesn't require any keys,
but a kind that inherits this requires open action.

openable *unite-action-openable*
Defines an interface for files that can be open. This requires an inheriting
kind to define open action.
tabopen Open the file in a new tab
tabdrop Open the file by ":tab drop" command
split Open the file, splitting horizontally
vsplit Open the file, splitting vertically
left Open the file in the left, splitting vertically
right Open the file in the right, splitting vertically
above Open the file in the top, splitting horizontally
below Open the file in the bottom, splitting horizontally
persist_open Open the file in alternate window. unite window isn't closed.

  • ref.vim なんかは独自の kind を作っている様子
action は何にするか.
  • ページを開く.
    • オプションとしては読み込み専用で開く、があってもいいのかな.
  • 履歴を削除する(未)

とりあえず open するだけの source を作る

正しいのか分からないけど kind=command を選択した.
    • openable でも試したけど動かなかった.
let s:uni_puki = {
	\ 'alias_table' : { 'open' : 'open_page' },
	\ 'default_kind' : 'openable',
 ...
    • command だとうごいた.
let s:uni_puki = {
	\ 'alias_table' : { 'execute' : 'open_page' },
	\ 'default_kind' : 'command',
 ...
ソース

意外に簡単につくれて書くことがあまりない. 動作を理解するところが一番大変だったかな.

" source を用意
let s:uni_puki = {
	\ 'name': 'pukiwiki/history',
	\ 'default_action' : 'open_page',
	\ 'action_table' : {},
	\ 'alias_table' : { 'execute' : 'open_page' },
	\ 'default_kind' : 'command',
	\}
" これだと動かない
"	\ 'alias_table' : { 'open' : 'open_page' },
"	\ 'default_kind' : 'openable',
 

function! s:uni_puki.gather_candidates(args, context) "{{{
" 候補は history のリスト
" history の要素はリストで [site, page, others]

	let history = pukiwiki#get_history_list()

	" copy しないとリストが壊れる.
    return map(copy(history), "{
	\ 'word' :  v:val[0] . ' -- ' . v:val[1], 
	\ 'action__command' : 'PukiWiki ' . v:val[0] . ' ' . v:val[1],
	\ 'source' : 'pukiwiki/history',
	\ 'pukiwiki_history' : v:val,
	\}")
endfunction "}}}

let s:uni_puki.action_table.open_page = {
	\ 'description' : 'open the selected page',
	\ 'is_quit' : 1,
	\ 'is_selectable' : 0,
	\}

" open_page の action の定義
function! s:uni_puki.action_table.open_page.func(candidates) "{{{
	" pukiwiki#PukiWiki() でページを開く
	" ページ名に空白が含まれたときに上の action__command の設定では動作しない.
	let history = a:candidates.pukiwiki_history
	call pukiwiki#PukiWiki(history[0], history[1])
"	let command = a:candidates.action__command
"	let type = get(a:candidates, 'action__type', ':')
"	execute type . comand
endfunction "}}}

function! unite#sources#pukiwiki#define() "{{{
	" 登録. g:pukiwiki_config が定義されていない場合には
	" 動作していないはずなので登録しない
	if !exists('g:pukiwiki_config')
		return {}
	endif
	return s:uni_puki
endfunction "}}}
added :Unite pukiwiki/history · syngan/vim-pukiwiki@2d6607f · GitHub

note

  • やっと「unite.vim の強力さ」が理解できてきた
    • vim 力のない人に伝わっているものなのでしょうか.
  • kind=command で空白を含む文字列をコマンドの引数として渡す時にどうすればいいか.
    • map 関数を使うのが間違い?
      • val[1] は空白を含むことがあるので ''(single-quote) とか ""(double-quote) で囲みたい
      • 解決策がわからなかったので command ではなく, 関数呼ぶようにした.
    return map(copy(history), "{
    \ 'word' :  v:val[0] . ' -- ' . v:val[1],
    \ 'action__command' : 'PukiWiki ' . v:val[0] . ' ' . v:val[1],
    \ 'source' : 'pukiwiki/history',
    \ 'pukiwiki_history' : v:val,
    \}")
  • ":Unite hoge/tako" という source は ${runtime が通っているところ}/autoload/unite/sources/hoge*.vim に書けばいい. hoge.vim でもいい.

Note: To optimize load source files, if "buffer/rec" source name is detected,
unite.vim loads "autoload/unite/sources/buffer*.vim" files. The source file
name must have source name prefix(Ex: buffer).

  • 全然関係お話だけど, :h の候補に "unite.txt@ja" とかでて面倒なのですが, どうやればきえますか.
  • vital.vim に getopt チックなの用意されないのかな