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

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

Re: font-lock function matcher sample


From: Alan Mackenzie
Subject: Re: font-lock function matcher sample
Date: Sun, 25 Jul 2004 08:26:29 +0000
User-agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.0.35 (i686))

Arjan Bos <address@hidden> wrote on Sat, 17 Jul 2004
23:08:59 +0200:
> Hi,

> Could anyone of you please post a sample for a font-lock function matcher?

The font-lock function matcher wasn't (and possibly still isn't) fully
documented in the elisp manual.  The following may be helpful:

     When FUNCTION is called, it receives one argument, the limit of the
     search.  Its search should start at point and not extend beyond the
     limit.  FUNCTION should return non-`nil' if it succeeds, and set the
     match data to describe the match that was found.  FUNCTION will be
     called repeatedly with the same limit, and with point where the
     previous invocation left it, until it fails.  It need not reset
     point to a sensible value on failure.

> I'm looking for a way to fontify words accoriding to their scrabble
> score. So if a word would score 6 points in american scrabble, it
> should get a certain font-locking.

> Currently, I have the following elisp, but it does not work.

Have you discovered the joys of edebug, yet?  If not, I thoroughly
recommend you to invest a few hours learning it.  For example, put point
inside `scrabble-6-matcher' and do C-u C-M-x to instrument it for edebug.
Then call the function directly with M-: (scrabble-6-matcher 200).
Enjoy!

A hint: if you're going to be using edebug within "live" font-lock
routines, do this first:  M-: (setq font-lock-support-mode nil).
Otherwise the jit-lock timer routine will kick in 3 seconds after you've
starting looking at your function, destroying your concentration and
peace of mind.

> I've been looking at some samples from the emacs lisp directories, but
> failed to work out the pattern. Also the info node on font-locking and
> the various font-locking doc-strings were helpful, but not helpful
> enough.  My elisp swings between locking emacs (presumably on
> font-locking) and no font-locking at all. (this of course when I
> twiddle around with it)

> (defvar scrabble-font-lock-keywords
>    (list '(scrabble-6-matcher (1 font-lock-warning-face))
> ))

> (defun scrabble-6-matcher (limit)
>    "returns t when the scrabble score of a word is 6."
>    (if (and (re-search-forward "\\([a-z]\\)*" limit t)
>         (< (scrabble-last-word-score) 5))
>        (progn
>       (set-match-data
>        (list
>         (match-beginning 1) (match-end 1)
>         (match-beginning 1) (match-end 1)
>         nil nil)))
>      ;; else
>      (set-match-data
>       (list
>        (match-beginning 1) (match-end 1)
>        nil nil
>        (match-beginning 1) (match-end 1)))
>      t))

> (defun scrabble-last-word-score ()
>    ""
>    5)

The "no font-locking at all" is probably because you've caused an error
inside an after-change-functions thing, namely the font-locking.  In this
case, Emacs Lisp _removes_ the guilty hook from after-change-functions,
since the only alternative would be totally hanging the system.

As for locking the system, I suspect you've got into an infinite loop
with your regular expression: (re-search-forward "\\([a-z]\\)*" limit t).
This regexp will match any sequence of lower case letters, including an
empty one.  ;-(.  Probably you really want something more like
"\\([a-z]\\)+".  Then ask yourself whether you really want the "+"
_outside_ the grouping parentheses.  ;-)

Have fun!

> Arjan

-- 
Alan Mackenzie (Munich, Germany)
Email: address@hidden; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").



reply via email to

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