Liu Yubao wrote:
Hi,

I want a function Highlight(from_row, from_col, width, height, color) where
from_row > 0 && from_col > 0 && width >=0 && height >=0 and zero means maximum.
The highlight will stay until I call something to cancel it.

With this function, I can place a rule at column 80 with Highlight(1, 80, 1, 0, 
green),
highlight all xml blocks like View Source Chart extension for Firefox (http://jennifermadden.com/scripts/ViewRenderedSource.html), make a table with colorized
border and so on.

I would greatly appreciate your help!



(untested)

        function Highlight(from_row, from_col, width, height, highlightGroup)   
                let row_before = a:from_row - 1
                let row_after = a:from_row + a:height
                let col_before = a:from_col - 1
                let col_after = a:from_col + a:width
                if   (row_before < 0)
                \ || (row_before > (row_after - 2))
                \ || (col_before < 0)
                \ || (col_before > (col_after - 2))
                        echoerr 'function Highlight: invalid arguments ('
                        \ . a:from_row . ', '
                        \ . a:from_col . ', '
                        \ . a:width . ', '
                        \ . a:height . ', '
                        \ . a:highlightGroup . ')'
                else
                        exe 'match' a:highlightGroup '/'
                        \       . '\%>' . row_before . 'l^.*$'
                        \       . '\&'
                        \       . '\%<' . row_after  . 'l^.*$'
                        \       . '\&'
                        \       . '\%>' . col_before . 'v.*$'
                        \       . '\&'
                        \       . '^.*\%>' . col_after . 'v.'
                        \       . '/'
                endif
        endfunction

Notes:
- The last argument is not a color but a highlight group, as in

        :exe 'call Highlight(1,80,1,' . (line('$') - 1) . ', Todo)'

- Arguments which would not result in a nonempty highlight will be rejected (except that there is no check that lines are long enough or numerous enough to reach into the highlight area). In particular, height or width cannot be zero. (Use some arbitrary high value like 999999 for maximum height or width.)
- To remove the highlight, use

        :match none

- The use of continuation lines assumes that 'nocompatible' has been set, either explicitly, or implicitly due to the existence of a user vimrc.

see
        :help :match
        :help ordinary-atom
        :help pattern


Best regards,
Tony.

Reply via email to