Highlighting text between tags

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Highlighting text between tags

J.A.J. Pater
Dear Vim Wizards,

I've a text which is tagged with an XML/HTML like syntax, like this:

Lorem ipsum <A> dolor sit amet, consectetur adipisicing </A> elit, sed
<B> do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis </B> nostrud exercitation ullamco <D>
laboris nisi ut aliquip <E> ex ea </E> commodo consequat. Duis aute </
D> irure dolor in reprehenderit ...

Now I want vim to highlight the text between <A> and </A> with red,
<B> and </B> with yellow, <D> and </D> with blue and between <E> and </
E> with green.

How would I go about writing a script and/or a syntax file for this?
I've read a little bit around, but I just can't figure this one out.

Thanks in advance!

Adriaan.

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

Stahlman Family


Adriaan wrote:

> Dear Vim Wizards,
>
> I've a text which is tagged with an XML/HTML like syntax, like this:
>
> Lorem ipsum <A> dolor sit amet, consectetur adipisicing </A> elit, sed
> <B> do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
> enim ad minim veniam, quis </B> nostrud exercitation ullamco <D>
> laboris nisi ut aliquip <E> ex ea </E> commodo consequat. Duis aute </
> D> irure dolor in reprehenderit ...
>
> Now I want vim to highlight the text between <A> and </A> with red,
> <B> and </B> with yellow, <D> and </D> with blue and between <E> and </
> E> with green.

Adriaan,
Before writing your own, you might have a look at the following plugin:

Txtfmt (The Vim Highlighter)
http://www.vim.org/scripts/script.php?script_id=2208

Txtfmt will give you the highlight regions you mentioned and a lot more:
e.g., up to 8 foreground and 8 background colors (independently
configurable), and all combinations of bold, underline and italic.
(There's also a configuration that supports standout, reverse and
undercurl, but it's currently mutually exclusive with use of background
colors). Moreover, each of the "tags" used by Txtfmt are only a single
character wide, are hidden by the syntax (for mostly WYSIWYG
highlighting), and can be inserted with very intuitive mappings.

Hope it helps...
Brett Stahlman

>
> How would I go about writing a script and/or a syntax file for this?
> I've read a little bit around, but I just can't figure this one out.
>
> Thanks in advance!
>
> Adriaan.
>

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

J.A.J. Pater
> Adriaan wrote:

> > I've a text which is tagged with an XML/HTML like syntax, like this:
> >
> > Lorem ipsum <A> dolor sit amet, consectetur adipisicing </A> elit, sed
> > <B> do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
> > enim ad minim veniam, quis </B> nostrud exercitation ullamco <D>
> > laboris nisi ut aliquip <E> ex ea </E> commodo consequat. Duis aute </
> > D> irure dolor in reprehenderit ...
>
> > Now I want vim to highlight the text between <A> and </A> with red,
> > <B> and </B> with yellow, <D> and </D> with blue and between <E> and </
> > E> with green.
>
> Adriaan,
> Before writing your own, you might have a look at the following plugin:
>
> Txtfmt (The Vim Highlighter)http://www.vim.org/scripts/script.php?script_id=2208


> Hope it helps...
> Brett Stahlman

Thanks Brett,

I know about The Vim Highlighter.
But these files contain about 40-100 times "<A> blah blah </A>" idem
for <B> etc.
So I would have to re-tag these files to get them highlighted, which
is just to much work.

Well, I could replace "<A>" by "<A> + token Txtfmt uses" to get
highlighting.

I looked at writing my onw syntax file, which I found rather complex.
And I found the match command which I couldn't get to do what I
wanted, probably because I'm no good in regular expressions.

It should not be too complicated though...

Thanks anyway!

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

J.A.J. Pater
OK I've got it.

I created an syntax file like this:

:syntax region aComments start=/<A>/ end=/<\/A>/"
:highlight aComments ctermfg=red guifg=red

Sorry for the noise.

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

Andrei Kulakov
On 06/11/2010 10:18 AM, Adriaan wrote:

> OK I've got it.
>
> I created an syntax file like this:
>
> :syntax region aComments start=/<A>/ end=/<\/A>/"
> :highlight aComments ctermfg=red guifg=red
>
> Sorry for the noise.
>
>  

That's ok - I was just about to search help for this
very command when I saw this email in my mail folder! So, thanks.

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

Stahlman Family
In reply to this post by J.A.J. Pater


On Jun 11, 9:01 am, Adriaan <[hidden email]> wrote:

> > Adriaan wrote:
> > > I've a text which is tagged with an XML/HTML like syntax, like this:
>
> > > Lorem ipsum <A> dolor sit amet, consectetur adipisicing </A> elit, sed
> > > <B> do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
> > > enim ad minim veniam, quis </B> nostrud exercitation ullamco <D>
> > > laboris nisi ut aliquip <E> ex ea </E> commodo consequat. Duis aute </
> > > D> irure dolor in reprehenderit ...
>
> > > Now I want vim to highlight the text between <A> and </A> with red,
> > > <B> and </B> with yellow, <D> and </D> with blue and between <E> and </
> > > E> with green.
>
> > Adriaan,
> > Before writing your own, you might have a look at the following plugin:
>
> > Txtfmt (The Vim Highlighter)http://www.vim.org/scripts/script.php?script_id=2208
> > Hope it helps...
> > Brett Stahlman
>
> Thanks Brett,
>
> I know about The Vim Highlighter.
> But these files contain about 40-100 times "<A> blah blah </A>" idem
> for <B> etc.
> So I would have to re-tag these files to get them highlighted, which
> is just to much work.
>
> Well, I could replace "<A>" by "<A> + token Txtfmt uses" to get
> highlighting.

Yes. It would actually be quite simple. Far be it from me, however, to
discourage someone from writing his own Vim script. Still, if you did
want the added flexibility of Txtfmt in your files (e.g., being able
to highlight a piece of text with bold, italic, blue fg, and red bg
all at the same time), there are at least two simple ways to convert
your existing files...

-- Method #1 --
:%s/<A>/\=nr2char(0xE005)/g
:%s/<B>/\=nr2char(0xE007)/g
:%s/<D>/\=nr2char(0xE002)/g
:%s/<E>/\=nr2char(0xE003)/g
:%s/<\/[ABDE]>/\=nr2char(0xE000)/g

-- Method #2 --
:set ft=txtfmt
:%s/<A>/\=Txtfmt_GetTokStr("cred")/g
:%s/<B>/\=Txtfmt_GetTokStr("cyellow")/g
:%s/<D>/\=Txtfmt_GetTokStr("cblue")/g
:%s/<E>/\=Txtfmt_GetTokStr("cgreen")/g
:%s/<\/[ABDE]>/\=Txtfmt_GetTokStr("c-")/g

The latter method uses a function defined by the Txtfmt plugin; hence,
you would need to set filetype to txtfmt first...

Brett Stahlman

>
> I looked at writing my onw syntax file, which I found rather complex.
> And I found the match command which I couldn't get to do what I
> wanted, probably because I'm no good in regular expressions.
>
> It should not be too complicated though...
>
> Thanks anyway!- Hide quoted text -
>
> - Show quoted text -

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

Andrei Kulakov
On 06/11/2010 11:10 AM, Brett Stahlman wrote:
>
> Yes. It would actually be quite simple. Far be it from me, however, to
> discourage someone from writing his own Vim script. Still, if you did
> want the added flexibility of Txtfmt in your files (e.g., being able
> to highlight a piece of text with bold, italic, blue fg, and red bg
> all at the same time), there are at least two simple ways to convert
> your existing files...


Since txtfmt came up.. I like the idea but I was running into a problem
with it: in gvim, when you highlight some text and when cursor is before
the highlighted area, you can sometimes (i.e. very often) see the
unicode character used to start the highlight, which is supposed to be
invisible. You can sort of see a part of it overlaying the next
character, or something like that (it was a few months since I tried
it.) Is this a known issue with txtfmt and gvim or is there a fix or I
need to use some specific fonts in gui?

Thanks! -ak

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

Stahlman Family


On Jun 11, 10:10 am, AK <[hidden email]> wrote:

> On 06/11/2010 11:10 AM, Brett Stahlman wrote:
>
>
>
> > Yes. It would actually be quite simple. Far be it from me, however, to
> > discourage someone from writing his own Vim script. Still, if you did
> > want the added flexibility of Txtfmt in your files (e.g., being able
> > to highlight a piece of text with bold, italic, blue fg, and red bg
> > all at the same time), there are at least two simple ways to convert
> > your existing files...
>
> Since txtfmt came up.. I like the idea but I was running into a problem
> with it: in gvim, when you highlight some text and when cursor is before
> the highlighted area, you can sometimes (i.e. very often) see the
> unicode character used to start the highlight, which is supposed to be
> invisible. You can sort of see a part of it overlaying the next
> character, or something like that (it was a few months since I tried
> it.) Is this a known issue with txtfmt and gvim or is there a fix or I
> need to use some specific fonts in gui?

AK,
Which OS/GUI/encoding were you using?
Thanks,
Brett Stahlman


>
> Thanks! -ak

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

Andrei Kulakov
On 06/11/2010 12:38 PM, Brett Stahlman wrote:

>
>> Since txtfmt came up.. I like the idea but I was running into a problem
>> with it: in gvim, when you highlight some text and when cursor is before
>> the highlighted area, you can sometimes (i.e. very often) see the
>> unicode character used to start the highlight, which is supposed to be
>> invisible. You can sort of see a part of it overlaying the next
>> character, or something like that (it was a few months since I tried
>> it.) Is this a known issue with txtfmt and gvim or is there a fix or I
>> need to use some specific fonts in gui?
>>    
> AK,
> Which OS/GUI/encoding were you using?
> Thanks,
> Brett Stahlman
>
>  
Ubuntu 9.10, default gvim (gtk), default encoding (I guess utf-8?).

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

Stahlman Family
In reply to this post by Stahlman Family


...[snip]...

>
> > Well, I could replace "<A>" by "<A> + token Txtfmt uses" to get
> > highlighting.
>
> Yes. It would actually be quite simple. Far be it from me, however, to
> discourage someone from writing his own Vim script. Still, if you did
> want the added flexibility of Txtfmt in your files (e.g., being able
> to highlight a piece of text with bold, italic, blue fg, and red bg
> all at the same time), there are at least two simple ways to convert
> your existing files...
>
> -- Method #1 --
> :%s/<A>/\=nr2char(0xE005)/g
> :%s/<B>/\=nr2char(0xE007)/g
> :%s/<D>/\=nr2char(0xE002)/g
> :%s/<E>/\=nr2char(0xE003)/g
> :%s/<\/[ABDE]>/\=nr2char(0xE000)/g

I should have pointed out that the method above assumes that the
Txtfmt tokens are located at 0xE000 (start of Unicode's "Private Use
Area"), which is the default location for a utf-8 encoding. For a
latin1 encoding, on the other hand, the default location is decimal
180. You can determine the token locations used in a Txtfmt buffer by
running :ShowTokenMap (or by setting the start token explicitly with
the txtfmtTokrange option).

Method #2 (below) is preferable to the method outlined above because
it ensures that the correct characters are used, without requiring you
to know or care what they are...

Brett Stahlman

>
> -- Method #2 --
> :set ft=txtfmt
> :%s/<A>/\=Txtfmt_GetTokStr("cred")/g
> :%s/<B>/\=Txtfmt_GetTokStr("cyellow")/g
> :%s/<D>/\=Txtfmt_GetTokStr("cblue")/g
> :%s/<E>/\=Txtfmt_GetTokStr("cgreen")/g
> :%s/<\/[ABDE]>/\=Txtfmt_GetTokStr("c-")/g
>
> The latter method uses a function defined by the Txtfmt plugin; hence,
> you would need to set filetype to txtfmt first...
>
> Brett Stahlman
>
>
>
>
>
> > I looked at writing my onw syntax file, which I found rather complex.
> > And I found the match command which I couldn't get to do what I
> > wanted, probably because I'm no good in regular expressions.
>
> > It should not be too complicated though...
>
> > Thanks anyway!- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

Stahlman Family
In reply to this post by Andrei Kulakov


On Jun 11, 11:44 am, AK <[hidden email]> wrote:

> On 06/11/2010 12:38 PM, Brett Stahlman wrote:
>
>
>
>
>
> >> Since txtfmt came up.. I like the idea but I was running into a problem
> >> with it: in gvim, when you highlight some text and when cursor is before
> >> the highlighted area, you can sometimes (i.e. very often) see the
> >> unicode character used to start the highlight, which is supposed to be
> >> invisible. You can sort of see a part of it overlaying the next
> >> character, or something like that (it was a few months since I tried
> >> it.) Is this a known issue with txtfmt and gvim or is there a fix or I
> >> need to use some specific fonts in gui?
>
> > AK,
> > Which OS/GUI/encoding were you using?
> > Thanks,
> > Brett Stahlman
>
> Ubuntu 9.10, default gvim (gtk), default encoding (I guess utf-8?).- Hide quoted text -

Thanks. I was actually already planning to install Ubuntu on my home
PC. I'll look into it...

Brett Stahlman

>
> - Show quoted text -

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Highlighting text between tags

Andrei Kulakov
On 06/11/2010 01:25 PM, Brett Stahlman wrote:
> Thanks. I was actually already planning to install Ubuntu on my home
> PC. I'll look into it...
>
> Brett Stahlman
>  

Thanks!

--
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