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

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

Re: font-lock-defaults doesn't work??


From: Tim X
Subject: Re: font-lock-defaults doesn't work??
Date: Sun, 15 Apr 2007 13:54:58 +1000
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.95 (gnu/linux)

"Peter Tury" <tury.peter@gmail.com> writes:

> Tim X:
>> You are also re-inventing a wheel to some extent. Use define-derived-mode
>
> If I remember well, define-derived-mode is for very simple new major
> modes what practically doesn't do more than fontification, new key
> bindings and so. However, I plan to add (much?) more staff later. What
> I sent above is the minimal extract from the current code what can be
> used to check if it works in the same way for you, than for me.
>

I think you might be thinking of one of the other facilities, like generic mode
or mmm mode. 

There are a number of conventions and setup requirements for a new mode that
are common to almost all modes. the define-derived-mode sets things up to make
this easy. You define the font-lock keywords, syntax table, indent function and
whatever else you want to do. some people just start with deriving from
fundamental mode and then incrementally build up the functionality they need. 

> Have you tried it? Does it work well for you even without the
> "secretly must-to-have parts"? Anyway it looks strange for me if
> fontification works (almost) well, but syntax parsing doesn't -- I
> think they should be in sync. in any (normal) cases...
>

I'm not an expert in this area, but I have created a couple of my own modes. In
the most recent one I've been working on, comment characters are either /*
....*/ or -- for a single line comment. All I needed to do to get these lines
fontified in the comment font was to modify the syntax table to set the comment
characters. I also modified the syntax table to change some of the definitions
of what are considered to be word characters, symbol characters etc, but this
was done to get syntax parsing to work (I do this to determine whether I'm
inside a procedures argument list and indent the arguments on subsequent lines
to the opening (. ). It has nothing to do with font-locking directly. 

To define the font-lock stuff, I just define variables representing regular
expressions for the different font-lock types (keyword, built-in, warning,
etc). I use regexp-opt to get an optimised regexp for the block of words and
then set them via the facilities provided in define-derived-mode. 

I then define an indent function and a calculate-indent-size function, set the
appropriate local variables and the basics of my mode (font-locking and
indentation) are done. Now I'm enhancing it further by adding align rules to
see if I get nicer alignment of some definitions. After that, I plan to add
some abbrev stuff and possibly some skeleton/tempo templates to create
'electric' operations and add imenu support. 

Nearly all of this has been straight-forward and based on information I got
from the emacs wikki site. The trick I found was to do things incrementally. I
actually started by getting indentation to work the way I wanted. I then did
the font-lock stuff and am now adding other bits.

>> from memory, I don't htink this bit is correct and you shouldn't need it. See
>> the example on the eamcs wiki. Comment font locking is driven by the comment
>> characters defined in the syntax table. You shouldn't need to also set them
>> explicitly in the font-lock stuff.
>
> I thought the other way (according to emacs help, what I showed
> previously): I planned to use _only_ font-lock-defaults and thought it
> sets the syntax table for me. I need syntax properties as well anyway,
> so I don't plan to deal too much with the syntax-table: only comment
> delimiters seems to be in the right place there in my case...
>
>> have a look at a fairly simple mode and see how they do the font-lock stuff.
>
> Yes, I searched for "official" settings in my elisp directory, but I
> afraid all of what I checked are old, so maybe they don't use new(?)
> possibilities. Thus I tried what I could understand from the actual
> emacs documentation...
>
>> I found sql.el quite useful (part of emacs). Again, look at the emacs wikki,
>> there is some really useful ifo there.
>
> I'll check sql.el also, thanks for the tip. What emacswiki page you
> think?
>

See http://www.emacswiki.org/cgi-bin/wiki/CreateNewMajorMode

and in particular, follow the link concerning derived mode.
-- 
tcross (at) rapttech dot com dot au


reply via email to

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