Tip #277: Function signature previewer
tip karma |
Rating 19/6, Viewed by 1161
|
created: |
|
July 6, 2002 18:28 |
|
complexity: |
|
basic |
author: |
|
Georgi Slavchev [email protected] |
|
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>
<<Function signature previewer |
all the right moves >>
Additional Notes
|