From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "rgb" Newsgroups: gmane.emacs.help Subject: Re: fontifying of user defined variables Date: 14 Sep 2006 07:47:16 -0700 Organization: http://groups.google.com Message-ID: <1158245236.678432.310180@m73g2000cwd.googlegroups.com> References: <10129949.484411158055226421.JavaMail.servlet@kundenserver> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: sea.gmane.org 1158248474 25922 80.91.229.2 (14 Sep 2006 15:41:14 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 14 Sep 2006 15:41:14 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Sep 14 17:41:12 2006 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1GNtKO-0002up-K3 for geh-help-gnu-emacs@m.gmane.org; Thu, 14 Sep 2006 17:40:37 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1GNtKO-0007Nx-2u for geh-help-gnu-emacs@m.gmane.org; Thu, 14 Sep 2006 11:40:36 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!m73g2000cwd.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 66 Original-NNTP-Posting-Host: 63.91.129.20 Original-X-Trace: posting.google.com 1158245242 2048 127.0.0.1 (14 Sep 2006 14:47:22 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Thu, 14 Sep 2006 14:47:22 +0000 (UTC) User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322),gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: m73g2000cwd.googlegroups.com; posting-host=63.91.129.20; posting-account=C7LM4w0AAAD23IRuMuUUJVCLQTuHhTK8 Original-Xref: shelby.stanford.edu gnu.emacs.help:141743 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:37366 Archived-At: 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