昨日のコードにもバグがあった...というか全然意図したとおりに動いてなかったorz


昨日のコードにもバグがあった...というか全然意図したとおりに動いてなかった。。。
ので、間違ったコードを残しても害悪でしかないので記事自体を削除。

こんどこそ、unite.vimで指定したファイル、拡張子のリストを出すsource... になってると思う....

mapとかfilterが渡した引数自体を変えてしまうってのに何度か引っかかった。
はまるってほどではないけど、間違いやすいですね。

let s:save_cpo = &cpo
set cpo&vim

let s:True = 1
let s:False = 0

let s:is_first_lauch = s:True
let s:output_filenames_cache = []

if !exists( "g:project_unite_source_filter_extentions" )
	let g:project_unite_source_filter_extentions = []
endif

if !exists( "g:project_unite_source_dirnames" )
	let g:project_unite_source_dirnames = []
endif

function! unite#sources#project#define()"{{{
  return s:source
endfunction"}}}

let s:source = {
\   'name': 'project',
\ }

function! s:format_for_word( index, max_digit, path )
	let format = '%' . a:max_digit . 'd: %s'
	return printf(format, a:index + 1, matchstr(a:path, '\w*\..*$'))
endfunction

function! s:source.gather_candidates(args, context)
	let l:extentions = g:project_unite_source_filter_extentions
	let l:dirnames = g:project_unite_source_dirnames

	let l:output_filenames = []

	if s:is_first_lauch == s:True
		" don't use cache
		for s:dir in l:dirnames
			let l:filenames = split( glob( s:dir . "**/*" ), "\n" )
			for l:ext in l:extentions
				let l:file_filter_pattern = '.*\.' . l:ext . "$"
				let s:outputs = filter( copy(l:filenames), 'matchstr( v:val, l:file_filter_pattern ) != ""' )
				let l:output_filenames = extend( l:output_filenames, s:outputs )
			endfor
		endfor

		let format = '%' . strlen(len(l:output_filenames)) . 'd: %s'
		let s:output_filenames_cache = copy(l:output_filenames)
		let s:is_first_lauch = s:False
		return map( l:output_filenames, '{
					\ "word": s:format_for_word( v:key, strlen(len(l:output_filenames)), v:val ),
					\ "kind": "file",
					\ "action__path": v:val,
					\ }')
	else
		" use cache
		let format = '%' . strlen(len(s:output_filenames_cache)) . 'd: %s'
		return map( copy(s:output_filenames_cache), '{
					\ "word": s:format_for_word( v:key, strlen(len(l:output_filenames)), v:val ),
					\ "kind": "file",
					\ "action__path": v:val,
					\ }')
	endif
endfunction

let &cpo = s:save_cpo
unlet s:save_cpo