Monday, May 5, 2014

Re: disable syntax highlighting for a section


com! -bang -range -nargs=? NOSyntax :call <sid>NoSyn(<q-bang>, <q-args>, <q-line1>, <q-line2>)
com! ResetNOSyntax :unlet! b:no_syn_lines|call <sid>NoSynAucommand(0)|syn clear NoSyntax

fun! <sid>ClearNoSyntaxHi()
" reset syntax highlighting
syn clear NoSyntax
endfu

fun! <sid>NoSynInit()
if !exists("b:no_syn_lines")
let b:no_syn_lines={}
endif
if !exists('#NoSyntax')
call <sid>NoSynAucommand(1)
endif
if !exists("b:changedtick_no_syntax")
let b:changedtick_no_syntax = b:changedtick
endif
endfu

fun! <sid>NoSyn(bang, args, sline, eline) range
call <sid>NoSynInit()
if a:bang=='!'
for [key,val] in items(b:no_syn_lines)
if <sid>LineIsInRange(a:sline, val) && <sid>LineIsInRange(a:eline, val)
call remove(b:no_syn_lines, key)
endif
endfor
call <sid>ClearNoSyntaxHi()
else
if empty(a:args)
let pat=[a:sline, a:eline]
else
let pat=split(a:args, '-')
endif
" make sure, list has 2 items
let pat=(len(pat) == 1 ? [pat[0],pat[0]] : pat)
" line already has no syntax elements
for [key,lines] in items(b:no_syn_lines)
if <sid>LineIsInRange(pat[0], lines) &&
\ <sid>LineIsInRange(pat[1], lines)
continue
elseif (<sid>LineIsInRange(pat[0], lines) &&
\ !<sid>LineIsInRange(pat[1], lines)) ||
\ (!<sid>LineIsInRange(pat[0], lines) &&
\ <sid>LineIsInRange(pat[1], lines))
call remove(b:no_syn_lines, key)
let s:reset_syntax_hi=1
endif
endfor
let b:no_syn_lines[<sid>MaxItem(b:no_syn_lines)+1]=[pat[0], pat[1]]
let b:no_syn_end=line('$')
endif
call <sid>SetupSyn()
endfu

fun! <sid>SetupSyn()
call <sid>NoSynInit()
for lines in values(b:no_syn_lines)
exe printf('syn match NoSyntax /^\%%>%dl\%%<%dl.*$/', lines[0]-1, lines[1]+1)
endfor
endfu

fun! <sid>MaxItem(dict)
if empty(keys(a:dict))
return 0
else
return max(keys(a:dict))
endif
endfu

fun! <sid>LineIsInRange(line, pos)
return a:pos[0] <= a:line && a:pos[1] >= a:line
endfu

fun! <sid>NoSynAucommand(enable)
if a:enable
aug NoSyntax
au!
au TextChanged * :call <sid>UpdateNoSyn()
aug end
else
aug NoSyntax
au!
aug end
aug! NoSyntax
endif
endfu

fun! <sid>UpdateNoSyn()
if b:changedtick != b:changedtick_no_syntax
let b:changedtick_no_syntax = b:changedtick
if !exists("b:no_syn_end")
let b:no_syn_end=line('$')
return
elseif b:no_syn_end != line('$')
let diff = line('$') - b:no_syn_end
for line in values(b:no_syn_lines)
if line[0] >= getpos("'[")[1]
let line[0]+=diff
endif
if line[1] >= getpos("'[")[1]
let line[1]+=diff
endif
endfor
call <sid>ClearNoSyntaxHi()
call <sid>SetupSyn()
let b:no_syn_end=line('$')
endif
endif
endfu


Am 2014-05-02 23:46, schrieb shawn wilson:
> Sometimes (especially in documentation) syntax highlighting really
> messes up. However, I don't really want to highlight documentation
> anyway, so can I disable it per line?
>
> So, basically, what I want is to be able to select a block and say,
> don't do anything from within this block (and maybe have it set a mark
> or something so that I can insert stuff in the middle and it won't
> mess with new text)?

No. There is really no builtin way to disable syntax linewise. It
doesn't really
make sense anyhow, as the syntax engine often needs to match across
lines and
therefore depends on it.

As for your original problem, read the help at :h :syn-sync and see if
you can
manually resync the syntax engine, if you notice wrong behaviour. I
assume,
you only experience this for a specific filetype, so it might be a good
idea to
report those bugs to the maintainer of that specific syntax script. Look
into the
$VIMRUNTIME/syntax/<language>.vim script for his contact data.

Nevertheless, you can trick the syntax engine to stop highlighting
specific
lines. This is rather hacky approach, but it should work. You would add
some
specific syntax matches, that link to a not-defined syntax group and
use a pattern with the \%l line matching.

E.g. to stop syntax highlighting in the lines 75-80 in the current
buffer,
issue: :syn match NoSyntax /\%>74l.*\%<81l/
This should clear syntax highlighting (except for keywords). I'll attach
a
plugin like script, to test this out. Drawback is, it needs to manually
update
the regions since those ranges given in the syn-match command will be
fixed and not
updated when lines changed. For that reason, that script tries to update
the
regions on TextChanged autocommands. Not sure, how useful this is.

Best,
Christian

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment