Skip to content

Instantly share code, notes, and snippets.

@junegunn
Last active May 10, 2023 15:50
Show Gist options
  • Star 78 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save junegunn/5dff641d68d20ba309ce to your computer and use it in GitHub Desktop.
Save junegunn/5dff641d68d20ba309ce to your computer and use it in GitHub Desktop.
Plugin completion using VimAwesome API
" ----------------------------------------------------------------------------
" vimawesome.com
" ----------------------------------------------------------------------------
function! VimAwesomeComplete() abort
let prefix = matchstr(strpart(getline('.'), 0, col('.') - 1), '[.a-zA-Z0-9_/-]*$')
echohl WarningMsg
echo 'Downloading plugin list from VimAwesome'
echohl None
ruby << EOF
require 'json'
require 'open-uri'
query = VIM::evaluate('prefix').gsub('/', '%20')
items = 1.upto(max_pages = 3).map do |page|
Thread.new do
url = "http://vimawesome.com/api/plugins?page=#{page}&query=#{query}"
data = open(url).read
json = JSON.parse(data, symbolize_names: true)
json[:plugins].map do |info|
pair = info.values_at :github_owner, :github_repo_name
next if pair.any? { |e| e.nil? || e.empty? }
{word: pair.join('/'),
menu: info[:category].to_s,
info: info.values_at(:short_desc, :author).compact.join($/)}
end.compact
end
end.each(&:join).map(&:value).inject(:+)
VIM::command("let cands = #{JSON.dump items}")
EOF
if !empty(cands)
inoremap <buffer> <c-v> <c-n>
augroup _VimAwesomeComplete
autocmd!
autocmd CursorMovedI,InsertLeave * iunmap <buffer> <c-v>
\| autocmd! _VimAwesomeComplete
augroup END
call complete(col('.') - strchars(prefix), cands)
endif
return ''
endfunction
augroup VimAwesomeComplete
autocmd!
autocmd FileType vim inoremap <c-x><c-v> <c-r>=VimAwesomeComplete()<cr>
augroup END
@junegunn
Copy link
Author

@nightire
Copy link

Love you man~

@starcraftman
Copy link

Very cool. You at all interested in putting that into plug.vim to toggle on? Though it might be a bit more useful as a perhaps a search command in left pane? If not, we can at least put this in the wiki faq I guess.

@junegunn
Copy link
Author

@starcraftman Well, I'd like to avoid making vim-plug dependent on external services, and the result of VimAwesome API doesn't seem flawless; missing plugins, duplicate results, etc. Moreover, I'm not convinced if the site is being actively maintained: vim-awesome/vim-awesome#61

Yeah, we should put this on the wiki page (a link on "Home" page or maybe a separate page?) since this compensates for the missing :PluginSearch from Vundle.

@starcraftman
Copy link

@junegunn Ah damn, seems their main dev(s) got busy. Should probably give em a hand, do love that site. The duping problem is a real bummer.

Separately, have you ruled out ever adding PlugSearch or just didn't because vim.org wasn't great to search against?

@junegunn
Copy link
Author

@starcraftman In my opinion, vim.org no longer functions as the central repository. I don't even publish my plugins there anymore. Regarding the command, I know that some people want PluginSearch equivalent in vim-plug, but I personally don't see the point of it. Don't we all have web browsers? :) Also I don't think it has to be a part of vim-plug, it can be a separate plugin.

@junegunn
Copy link
Author

Yeah, I think it should be a plugin of its own, doesn't have to be limited to a specific plugin manager.

@starcraftman
Copy link

@junegunn I guess your right. Separate plugin makes most sense, searching isn't really part of plugin management.

Do hope VimAwesome isn't abandoned. Only just realized Gundo hadn't gotten a patch in a year. Sad when good software falls by wayside. :/

@mhinz
Copy link

mhinz commented May 16, 2015

@junegunn Neat! :-)

@starcraftman If it's still working for you, by all means, keep it. Otherwise I enjoy using https://github.com/mbbill/undotree

@jonafato
Copy link

VimAwesome isn't dead, though the main developer (@divad12) does seem to be busy. There was a bit of activity a couple of months ago, and he was quick to respond to pull requests. I can't speak for the project or its maintainers, but I'd guess that pull requests would be well received based on my limited experience with it.

@craigp
Copy link

craigp commented May 18, 2015

This is epic

@unbalancedparentheses
Copy link

Great

@mirzalazuardi
Copy link

where should i put the file (vimawesome.vim) in case to combine with my vundle ?
is there a README.md for installing this ?

u're awesome dude!!

@junegunn
Copy link
Author

@mirzalazuardi Place the file in ~/.vim/plugin directory or simply paste the code onto your .vimrc.

@lalitmee
Copy link

@junegunn, when I tried using this, it was failing for me with some Open.uri from kernel is deprecated. I did a little bit of searching and fixed it by only using URI.open(URL) instead of open(URL).

Here is the updated code:

" ----------------------------------------------------------------------------
" vimawesome.com
" ----------------------------------------------------------------------------
function! VimAwesomeComplete() abort
  let prefix = matchstr(strpart(getline('.'), 0, col('.') - 1), '[.a-zA-Z0-9_/-]*$')
  echohl WarningMsg
  echo 'Downloading plugin list from VimAwesome'
  echohl None
ruby << EOF
  require 'json'
  require 'open-uri'

  query = VIM::evaluate('prefix').gsub('/', '%20')
  items = 1.upto(max_pages = 3).map do |page|
    Thread.new do
      url  = "http://vimawesome.com/api/plugins?page=#{page}&query=#{query}"
      data = URI.open(url).read
      json = JSON.parse(data, symbolize_names: true)
      json[:plugins].map do |info|
        pair = info.values_at :github_owner, :github_repo_name
        next if pair.any? { |e| e.nil? || e.empty? }
        {word: pair.join('/'),
         menu: info[:category].to_s,
         info: info.values_at(:short_desc, :author).compact.join($/)}
      end.compact
    end
  end.each(&:join).map(&:value).inject(:+)
  VIM::command("let cands = #{JSON.dump items}")
EOF
  if !empty(cands)
    inoremap <buffer> <c-v> <c-n>
    augroup _VimAwesomeComplete
      autocmd!
      autocmd CursorMovedI,InsertLeave * iunmap <buffer> <c-v>
            \| autocmd! _VimAwesomeComplete
    augroup END

    call complete(col('.') - strchars(prefix), cands)
  endif
  return ''
endfunction

augroup VimAwesomeComplete
  autocmd!
  autocmd FileType vim inoremap <c-x><c-v> <c-r>=VimAwesomeComplete()<cr>
augroup END

@hungpham3112
Copy link

I'm using your script and gvim on Windows shows message:

[vim-plug] vim - Invalid argument: vim - (implicit vim-scripts expansion is deprecated)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment