Shlomi Fish
Shlomi Fish

Reputation: 4510

Vim Syntax: match only at the start of region - not subsequent ones

I have the following Vim syntax file:

" Vim syntax file
" Language: ScreenplayText - the textual source of ScreenplayXML
" Maintainer: Shlomi Fish <[email protected]>
" Home: http://search.cpan.org/dist/XML-Grammar-Screenplay/
"
" Author: Shlomi Fish
" Filenames: *.screenplay-text.txt
" Last Change: Thu Jul  3 00:59:42 IDT 2008
" Version: 0.0.1

" Thanks to Andy Wokula for his help on:
" https://groups.google.com/group/vim_use/browse_thread/thread/6c0906617d67864e/a21938c5df1d15cb?show_docid=a21938c5df1d15cb

" Quit if syntax file is already loaded
if version < 600
    syntax clear
elseif exists("b:current_syntax")
    finish
endif

syntax sync minlines=50

" syntax match screenplayTextComment /<!--\_.\{-0,}-->/
" syntax match screenplayTextDescription /^ *\[\_.\{-0,}\]/
syntax region screenplayTextComment start="<!--" end="-->"
syntax region screenplayTextDescription start="^ *\[" end="]"

" syntax region screenplayTextSaying start=/\(^\s*\n\)\@<=\_^\(+\{2,\}\|[^[:+]*\):/ end=/^\s*$/ contains=screenplayTextAddress,screenplayTextInnerDesc
syntax region screenplayTextSaying start=/\(^\s*\n\)\@<=\_^\(+\{2,\}\|[^[:+]*\):/ end=/^\s*$/ contains=screenplayTextAddress

" syntax match screenplayTextAddress /\%^\(+\{2,\}\|[^[:+]*\):/ contained nextgroup=screenplayTextInnerDesc
syntax match screenplayTextAddress /[^:]\+:/ contained nextgroup=screenplayTextSayingAfterAddress

syntax region screenplayTextSayingAfterAddress contained
" syntax match screenplayTextInnerDesc /\[\_.\{-0,}\]/ contained nextgroup=screenplayTextInnerDesc


" For debugging - remove.
" hi def link screenplayTextSaying Statement

hi def link screenplayTextComment Comment
hi def link screenplayTextDescription PreProc
hi def link screenplayTextInnerDesc PreProc
hi def screenplayTextAddress      term=bold cterm=bold gui=bold

It is intended for XML-Grammar-Screenplay, but the screenplayTextAddress still highlights stuff in subsequent lines with ":", like in:

Kate: Of course! See, my obsession with the Bible continued throughout my
life, and later on I became a scholar: first as a married woman who just
hanged around universities and learned things from the male professors
and students, later on as someone who helped some professors with their
research and eventually got credited, and as time progressed,
I got a B.A., and then a Ph.D., and am now a professor.

The second line with the ":" colon still gets highlighted. How do I prevent it from being highlighted?

Thanks for any insights.

Regards,

-- Shlomi Fish

P.S.: here is the repository for the vim-screenplay-text highlighting (a Mercurial one on bitbucket) and here is the screenplay I use to test it (in its GitHub git repository).

Upvotes: 3

Views: 903

Answers (2)

Billy Chan
Billy Chan

Reputation: 24815

It seems the only method to recognize the first line of paragraph is that it follows a blank line.

" Define blank line at first
syntax match EmptyLines "\(^\s*\n\)\+" nextgroup=FistLine, Description

" Then define Fist line
syntax region FirstLine start=+^[^\<\[]+ oneline contains=Address

" Then define Address
syntax match Address "^[^<\[]+:" contained

" Description
syntax match Description "\[[^\[]+\]"

" Comment
" Comment match code

" For the rest of text, just give them a general format

I did not test the code, just provide a thought. Please try out by yourself. Hope this helps.

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172570

The screenplayTextSaying group contains two branches; I'd separate them into two separate regions and then use the matchgroup to do the highlighting directly on the region start of the address ending with the colon. See :help :syn-matchgroup

syntax region screenplayTextSaying start=/\(^\s*\n\)\@<=\_^+\{2,\}:/ end=/^\s*$/
syntax region screenplayTextSaying end=/^\s*$/ matchgroup=screenplayTextAddress start=/\(^\s*\n\)\@<=\_^[^[:+]*:/
"syntax match screenplayTextAddress /[^:]\+:/ Not needed any more!

Upvotes: 2

Related Questions