sponsor Vim development Vim logo Vim Book Ad

basic Tip #276: Function signature previewer

 tip karma   Rating 5/2, Viewed by 1542 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   July 6, 2002 18:23      complexity:   basic
author:   Georgi Slavchev goyko@gbg.bg      as of Vim:   6.0

Have you ever tried to call a function which parameters you have forgotten?
Especially those long named and with long parameter list GTK+ functions
like gtk_menu_item_image_from_stock_new(..........) !!!
By accident I saw a function in Vim help. It's name was PreviewWord and it allowed
one to jump in the preview window to the tag for the word cursor is on.
I _slightly_ modified this function not to need tags file, but to search included files instead.
I wrote another function, which uses the above said one, which triggers PreviewWord
when you open the parenthesis after a function name.
Here it is:
" Note:
" This is literally stolen from Vim help. The only changes are:
" (1) if w != ""               becomes       if w =~ "\k"
" (2) exe "silent! ptag " . w  becomes       exe "silent! psearch " . w
" * The first change prevents PreviewWord of searching while cursor is on some
"   non-keyword characters, e.g. braces, asterisks, etc.
function! PreviewWord()
if &previewwindow " don't do this in the preview window
return
endif
let w = expand("<cword>") " get the word under cursor
if w =~ "\k" " if there is one ":ptag" to it

" Delete any existing highlight before showing another tag
silent! wincmd P " jump to preview window
if &previewwindow " if we really get there...
match none " delete existing highlight
wincmd p " back to old window
endif

" Try displaying a matching tag for the word under the cursor
let v:errmsg = ""
exe "silent! psearch " . w
if v:errmsg =~ "tag not found"
return
endif

silent! wincmd P " jump to preview window
if &previewwindow " if we really get there...
if has("folding")
silent! .foldopen " don't want a closed fold
endif
call search("$", "b") " to end of previous line
let w = substitute(w, '\', '\\\', "")
call search('\<\V' . w . '\>') " position cursor on match
" Add a match highlight to the word at this position
hi previewWord term=bold ctermbg=green guibg=green
exe 'match previewWord "\%' . line(".") . 'l\%' . col(".") . 'c\k*"'
wincmd p " back to old window
endif
endif
endfunction
au! CursorHold *.[ch] nested call PreviewWord()

" Note:
" When you open a parenthesis after a function name, and at the
" line end, that function's definition is previewed through PreviewWord().
" This is inspired from Delphi's CodeInsight technology.
" Something similar (PreviewClassMembers) could be written for
" the C++ users, for previewing the class members when you type
" a dot after an object name.
" If somebody decides to write it, please, mail it to me.
function! PreviewFunctionSignature()
let CharOnCursor = strpart( getline('.'), col('.')-2, 1)
if col(".") == col("$")
call PreviewWord()
endif
return "("
endfunction
inoremap <buffer> ( <C-R>=PreviewFunctionSignature()<LF>

 rate this tip  Life Changing Helpful Unfulfilling 

<< Some useful mappings for TeX | Function signature previewer >>

Additional Notes

realblades@yahoo.com, August 16, 2002 3:39
If it's installed neatly and has a manual page, you can search it by hitting K while on the word (function, command, filename...  anything with a manpage).
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo