help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fontifying of user defined variables


From: rgb
Subject: Re: fontifying of user defined variables
Date: 14 Sep 2006 07:47:16 -0700
User-agent: G2/1.0

Dieter Wilhelm wrote:
> dieter@duenenhof-wilhelm.de writes:
>
> >
> > I'd like to highlight user specified variables in a major mode for a
> > simple macro language (Ansys parametric design language or APDL).
> > All number variables are specified with the following assignment:
> >
> > VARIABEL=VALUE
> >
> > I'd like to fontify in a certain face any variable VARIABLE which
> > was defined in such a way and appears after the definition anywhere
> > in the code.  Could you please outline the method or point to a lisp
> > file where such a "dynamic" highlighting is accomplished.
>
> Well, had a look at C/C++ mode and not even there I could find above
> functionality.  So I guess it's too awkward to program this otherwise,
> I think, very helpful stuff.
>
> The only idea I've so far is for a function which parses now and then
> the buffer and adds or removes font lock keywords to the
> font-lock-keywords variable according to the current variable
> definitions in the buffer.

That could turn out to be very inefficient and therefore
very slow if you work on large files.  Otherwise, except for
one (possibly major) point it seems a workable idea.

The potential problem is that variables will be highlighted no matter
where they appear.  Even before the point they are defined.

The only way around that problem (that I know of) is to supply a
function rather than a regexp to font-lock-keywords.  The
function would then be responsible for identifying variable names
to be highlighted.

You can always supply a function anywhere a regexp is
allowed in the font-lock setup variables.  The function must take
1 argument (search-limit).  font-lock expects the function  to
act like re-search-forward in that it must return t or nil if it finds
a match and it must use set-match-data to mark the location
of the match and it must leave point somewhere after the match.
The big difference, of course, is that your function
must know what to look for (because it's only argument is
search-limit) whereas re-search-forward gets a regexp argument
that tells it what to look for.

Your periodic scanning function would still maintain a list of
variable names to highlight but wouldn't update font-lock-keywords.
Instead, it saves them to some other variable of your choosing in
a list that includes a marker to where the definition was detected.

The function you provide to font-lock-keywords would then
find keywords (variable names) only if they occured after the
point where they were defined.  Your function (like
re-search-forward) is expected to operate on the region between
point and search-limit.  So if you don't find a keyword in that range
you return nil.  If you find one you set match-data and return t.
When you return t you can expect that your function will be called
again by font-lock to look for more keywords.  point will be
whereever you left it when you were called last so you need to be
sure to leave point after any keyword you find or you'll loop finding
it over and over.

Good luck



reply via email to

[Prev in Thread] Current Thread [Next in Thread]