Wikia

Vim Tips Wiki

Use balloonexpr effectively

Talk0
1,613pages on
this wiki
Revision as of 02:12, May 16, 2012 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Tip 1149 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Salman Halim · version 7.0


Vim allows the use of an option call 'balloonexpr', which basically calls a function and displays the return value in a tooltip whenever the mouse cursor is at rest over a piece of text for a short period. Thus, we have:

" Returns either the contents of a fold or spelling suggestions.
function! BalloonExpr()
  let foldStart = foldclosed(v:beval_lnum )
  let foldEnd = foldclosedend(v:beval_lnum)
  let lines = []
  if foldStart < 0
    " We're not in a fold.
    " If 'spell' is on and the word pointed to is incorrectly spelled,
    " the tool tip will contain a few suggestions.
    let lines = spellsuggest( spellbadword( v:beval_text )[ 0 ], 5, 0 )
  else
    let numLines = foldEnd - foldStart + 1
    " Up to 31 lines get shown okay; beyond that, only 30 lines are shown with
    " ellipsis in between to indicate too much. The reason why 31 get shown ok
    " is that 30 lines plus one of ellipsis is 31 anyway.
    if ( numLines > 31 )
      let lines = getline( foldStart, foldStart + 14 )
      let lines += [ '-- Snipped ' . ( numLines - 30 ) . ' lines --' ]
      let lines += getline( foldEnd - 14, foldEnd )
    else
      let lines = getline( foldStart, foldEnd )
    endif
  endif
  return join( lines, has( "balloon_multiline" ) ? "\n" : " " )
endfunction
set balloonexpr=BalloonExpr()

Now, if the mouse is hovering on a fold, this will show the contents of the fold in the tooltip. If the number of folded lines is too big, it will display the first and last 15 lines only, with a comment in between indicating the number of lines that it ate.

If, however, the cursor is on a mispelled word (only works if 'spell' is set), then the tooltip will instead display a small set of suggestions for correction.

CommentsEdit

Around Wikia's network

Random Wiki