Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Monday, November 8, 2010

How to make a Unite plugin

Unite

The hottest Vim plugin these days is unite.vim.

:Unite file command opens a unite dialog that you can specify file names with inputting text.

1

You can search not only files but also other resources like buffers you've opened. Unite also allows users to create your own "resource" and to define corresponding actions.

Here I'll make a demonstrative unite plugin unite-colorscheme.

colorscheme

Vim, particularly GUI version of Vim implementations like MacVim, has colorscheme that allows you to change the appearance very much. You may change the colorscheme of the Vim just by :colorscheme ujihisa, but it's not trivial to find which colorschemes you already have. (*1)

2

unite-colorscheme.vim

https://github.com/ujihisa/unite-colorscheme

unite-colorscheme consists of the following two files.

autoload/unite/sources/colorscheme.vim:

let s:unite_source = {
      \ 'name': 'colorscheme',
      \ }

function! s:unite_source.gather_candidates(args, context)
  " [(name, dir)]
  " e.g. [('adaryn', '/Users/ujihisa/.vimbundles/ColorSamplerPack/colors'), ...]
  let colorlist = map(split(globpath(&runtimepath, 'colors/*.vim'), '\n'),
      \'[fnamemodify(v:val, ":t:r"), fnamemodify(v:val, ":h")]')

  return map(colorlist, '{
        \ "word": v:val[0],
        \ "source": "colorscheme",
        \ "kind": "colorscheme",
        \ "action__path": printf("%s/%s.vim", v:val[1], v:val[0]),
        \ "action__directory": v:val[1],
        \ }')
endfunction

function! unite#sources#colorscheme#define()
  return s:unite_source
endfunction

And autoload/unite/kinds/colorscheme.vim:

let s:kind = {
      \ 'name': 'colorscheme',
      \ 'default_action': 'execute',
      \ 'action_table': {},
      \ 'parents': [],
      \ }
let s:kind.action_table.execute = {
      \ 'is_selectable': 1,
      \ }
function! s:kind.action_table.execute.func(candidates)
  if len(a:candidates) != 1
    echo "candidates must be only one"
    return
  endif
  execute "colorscheme" a:candidates[0].word
endfunction

function! unite#kinds#colorscheme#define()
  return s:kind
endfunction

After you installed the plugin, you can search by :Unite colorscheme and set the colorscheme on the Vim just by selecting one colorscheme from the choice.

3

References and footnote

No comments:

Post a Comment

Followers