One thing I often do here is to add links to documentation when writing :he foo
, and I usually link to http://vimhelp.appspot.com. The problem is, that site's search is about slightly worse than the Stack Exchange sites' search. http://vimdoc.sourceforge.net, on the other hand, has excellent search functionality, searching the tags similar to the way Vim does.
What I'd like is a function which can convert something like :H magic
to http://vimhelp.appspot.com/pattern.txt.html#%2Fmagic
, that is:
- find the actual text of the help tag (
:he subst
can go to the help for:substitute
) - URL encode special characters in the tag (
:
,/
, etc.) - copy the URL to the clipboard or some other register
- Bonus: if run without args, pick the argument I used for my last call to
:he
, or the last tag I jumped to in the help.
I've come close:
function! Help2url (word)
python <<EOF
import vim
from urllib import quote_plus
topmatch = vim.eval ('taglist(a:word)[0]')
filename = topmatch['filename'].split ('/')[-1]
tag = quote_plus (topmatch['name'])
url = 'http://vimhelp.appspot.com/{:s}.html#{:s}'.format (filename, tag)
EOF
endfunction
This still needs to:
- use the help system's tags.
- copy to the clipboard
- work out the order that Vim uses.
The last point needs explaining. taglist('magic')
returns:
[{'cmd': '/*''magic''*', 'static': 0, 'name': '''magic''', 'kind': '', 'filename
': '/usr/share/vim/vim74/doc/options.txt'}, {'cmd': '/*''nomagic''*', 'static':
0, 'name': '''nomagic''', 'kind': '', 'filename': '/usr/share/vim/vim74/doc/opti
ons.txt'}, {'cmd': '/*\/magic*', 'static': 0, 'name': '/magic', 'kind': '', 'fil
ename': '/usr/share/vim/vim74/doc/pattern.txt'}, {'cmd': '/*:smagic*', 'static':
0, 'name': ':smagic', 'kind': '', 'filename': '/usr/share/vim/vim74/doc/change.
txt'}, {'cmd': '/*:snomagic*', 'static': 0, 'name': ':snomagic', 'kind': '', 'fi
lename': '/usr/share/vim/vim74/doc/change.txt'}]
The first match is 'magic'
, whereas :he magic
goes to /magic
. So, simply taking the first element of the list returned from taglist()
doesn't seem to cut it.
Solutions not using Python are welcome. I'm just more comfortable with it.