You are not logged in.

#1 2012-06-21 15:52:47

ANOKNUSA
Member
Registered: 2010-10-22
Posts: 2,141

Vim, Code Folding: Highlighting?

Howdy-ha, everyone.  Here's a question: Both Geany and Kate place code folds at branching and looping statements, and I know Vim is capable of doing the same.  However, the one thing these two graphical editors do that I'd like to see in Vim is to highlight the complete block of code when the mouse pointer is placed over the fold marker.  Here's an example in Kate (please ignore the grotesque colors tongue):
tZWZjag

The advantage of this is that it helps to catch logical errors without needing to sroll back-and-forth through an entire file, and reduces the necessity of checking line-by-line.  Is such a thing possible in Vim (say, when the cursor is placed over the fold marker)? My preliminary searches make me think so, but I haven't found anything concrete; searches like "vim fold highlight" and "highlight branching" and such don't turn up much other than a) regular syntax highlighting or b) changing the colors of the fold markers themselves.  Thanks for any tips or pointers.

Offline

#2 2012-06-21 18:45:21

Wibjarm
Member
Registered: 2012-05-04
Posts: 43

Re: Vim, Code Folding: Highlighting?

Well, it isn't automatic on folding, but the matchit.vim plugin (which is helpfully already included with vim since 6.0 in the macros/ directory) makes % jump across matching control structures in addition to things like parentheses, so you can use that and either do v% yourself or remap % as v% to highlight blocks of code nicely.  I had thought that there was a foldenter event that you could hook an autocmd up to and fire the v% whenever you entered the fold, but I can't find it anywhere in the documentation, so maybe I just imagined that it exists.  Also, I have to say, thanks for asking about this, I wouldn't have gone looking for something like matchit otherwise, but I'm definitely liking it now that I know about it.

Last edited by Wibjarm (2012-06-21 18:56:07)

Offline

#3 2012-06-21 19:40:11

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Vim, Code Folding: Highlighting?

I think you would need a way to continuously report the current mouse position to vim? I know vim can handle mouse-clicks, but I'm not so sure about continuous mouse position.

However, thanks to recent posts about how to integrate vim with gdb, I wonder whether something could be done with python. It might take a little work, but if you can get python to continuously track the mouse, and embed that in a vim plugin, you could get some interesting results.

Offline

#4 2012-06-21 20:13:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,557
Website

Re: Vim, Code Folding: Highlighting?

It should be easy to get this behavior when the cursor is over the fold marker, but I don't know about the mouse.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2012-06-21 21:47:49

ANOKNUSA
Member
Registered: 2010-10-22
Posts: 2,141

Re: Vim, Code Folding: Highlighting?

Thanks for the replies, folks, but I should have clarified: I don't care about mouse support.  It's finicky enough in Vim as it is, and I only use it for selection/cursor placement (I've actually been using Kate in Vim-compatability mode, which actually isn't too bad).  I meant the actual input cursor; sorry about the confusion.

I should also add that the reason I'm looking at this as a feature is because I'm as noob as they come when it comes to coding, and this feature helps me quickly find my little screw-ups (and move on to bigger, better screw-ups) rather than spend time looking for them. So digging deep into Vim's code/feature set might be beyond me at the moment. hmm

EDIT - @ Wibjarm: Matchit may actually be helpful; I'll play around with it and see how it works.  Thanks!

Last edited by ANOKNUSA (2012-06-21 22:01:48)

Offline

#6 2012-06-21 22:12:17

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Vim, Code Folding: Highlighting?

ANOKNUSA wrote:

I should also add that the reason I'm looking at this as a feature is because I'm as noob as they come when it comes to coding, and this feature helps me quickly find my little screw-ups (and move on to bigger, better screw-ups) rather than spend time looking for them.

In your posts, you give the impression of being more knowledgeable than that. Maybe you're being modest.

Anyway, I always find vim's syntax highlighting useful for noticing errors. Big give-away: when brackets start with the red background, it tells me to look for mismatched brackets wink. Also, I find this advice useful: solve errors from top to bottom (first to last). Due to cascade effects, later errors will often disappear after resolving the earlier ones.

Sorry to get off-topic, it just seems like you have an X-Y problem, you know? - maybe learning more tricks for solving compile-time errors will obviate the requirement to highlight blocks. smile.

Offline

#7 2012-06-21 22:25:11

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,557
Website

Re: Vim, Code Folding: Highlighting?

Toward the end of matchparen.vim comment lines 140 and 141 as seen below and directly following them add the one uncommented line:

"    exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
"	  \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
exe '3match MatchParen /\%>' . c_lnum . 'l\%<' . m_lnum . 'l/'

edit: or the vanilla matchparen may be just what you want if you're content with highlighting the braces themselves, not the block of code.  I actually don't like either, but with matchparen already doing most of the work, this one-line addition is pretty simple.

Last edited by Trilby (2012-06-21 22:31:24)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2012-06-21 23:21:02

bohoomil
Banned
Registered: 2010-09-04
Posts: 2,377
Website

Re: Vim, Code Folding: Highlighting?

That's what I use: it doesn't colorize the folded block, but draws a line on the left side of the editor that marks the unfolded piece of code. The parameter is set in .vimrc with 'set foldcolumn=1':

set foldmethod=marker
set foldnestmax=10
set foldcolumn=1

-- and the piece in the colorscheme file (again, as present in my setup):

hi FoldColumn      ctermfg=14   ctermbg=none
hi Folded          ctermfg=14   ctermbg=0

The result is more or less what you want, though achieved a bit differently.


:: Registered Linux User No. 223384

:: github
:: infinality-bundle+fonts: good looking fonts made easy

Offline

#9 2012-06-22 01:10:44

acewin
Member
Registered: 2009-06-29
Posts: 13

Re: Vim, Code Folding: Highlighting?

ANOKNUSA wrote:

I should also add that the reason I'm looking at this as a feature is because I'm as noob as they come when it comes to coding, and this feature helps me quickly find my little screw-ups (and move on to bigger, better screw-ups) rather than spend time looking for them.

Does not do exactly what you are looking for, but does this help?

https://github.com/nathanaelkane/vim-indent-guides

Offline

Board footer

Powered by FluxBB