On Sun, November 8, 2009 4:11 pm, [email protected] wrote:
>  is it possible to make a visual selection permament
>  that way, that only a specific command will un-visual
>  the block?
>
>  Currently the visual block is unhighlighted after one has
>  -- for example -- entered the wrong :s//-command.
>
>  Would be nice, if the block remains highlighted after
>  such an error... ;)

,----
| fu! <SID>MatchVisSel()
|     let linehl="Visual"     " define text-highlight for line
|
|     let b:vstart = getpos("'<")[1:2]
|     let b:vend   = getpos("'>")[1:2]
|
|     if b:vend[1] < 0
|        let b:vend[1] = col("'>")
|     endif
|
|     if b:vstart[0] == 0
|        let b:vstart[0] +=1
|     endif
|
|     if b:vstart[1] == 0
|        let b:vstart[1] +=1
|     endif
|
|
|     if visualmode() =~# 'V'
|       " Linewise selection
|       let regex = '\%>'.(b:vstart[0]-1).'l\_.*\%<' . (b:vend[0]+1) . 'l'
|     elseif visualmode() =~# 'v'
|       " Now we need to match something like this:
|       "\%(\%(\%>15l\)\&\%(\%>7c\)\)\_.*\%(\%(\%<18l\)\&\%(\%<7c\)\)
|       let regex = '\%(\%(\%>' . (b:vstart[0]-1) . 'l\)\&\%(\%>' .
(b:vstart[1]-1) . 'c\)\)\_.*\%(\%(\%<' . (b:vend[0]+1) . 'l\)\&\(\%<' .
(b:vend[1]+1) . 'c\)\)'
|     else
|       " Now we need to match something like this:
|       " \%(\%(\%>9l\)\&\%(\%<15l\)\)\@<=\%26c.*
|       let regex = '\%(\%(\%>' . (b:vstart[0]-1) . 'l\)\&\%(\%<' .
(b:vend[0]+1) . 'l\)\)\@<=\%>' . (b:vstart[1]-1) . 'c.*\%<' .
(b:vend[1]+1) . 'c'
|     endif
|     exe "2match " . linehl . ' /' . regex . '/'
| endfu
|
| fu! <SID>Init(enable)
|     if a:enable
|       augroup VisualMatch
|           au!
|           au InsertLeave,CursorHold <buffer> :call <SID>MatchVisSel()
|       augroup END
|       call <SID>MatchVisSel()
|     else
|       augroup VisualMatch
|           au!
|       augroup END
|       augroup! VisualMatch
|       2match none
|     endif
| endfu
|
| com! EnableHilightVisualSelection :call <SID>Init(1)
| com! DisableHilightVisualSelection :call <SID>Init(0)
`----

Beware of line wrappings in my mail, please.

Newer versions of Vim would probably use matchadd(), but my current
version (stock 7.1 does not have it, so I used :2match

Notice however, that these RE can make Vim quite slow. Don't know if
this is caused by the autocommands firing or too slow RE parsing within
vim. Also I am not sure, if the RE, always match for all corner cases of
visual selection areas. I tested it with some simple cases, but there
may exist some edge cases, which possibly could not be matched. (e.g.
the line number of '< might be larger thane '> (the same holds for the
column number), though I did not observe this effect)

regards,
Christian
-- 
:wq!


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to