all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alan Mackenzie <acm@muc.de>
Subject: Re: font-lock function matcher sample
Date: Sun, 25 Jul 2004 08:26:29 +0000	[thread overview]
Message-ID: <lrqvdc.i9.ln@acm.acm> (raw)
In-Reply-To: cdc4gs$obm$1@reader13.wxs.nl

Arjan Bos <Arjan.Bos@nospam.iseeyou.nl> 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: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

  reply	other threads:[~2004-07-25  8:26 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-17 21:08 font-lock function matcher sample Arjan Bos
2004-07-25  8:26 ` Alan Mackenzie [this message]
2004-07-25 19:55   ` Arjan Bos
2004-07-26 20:59     ` Stefan Monnier
2004-07-26 21:00     ` Stefan Monnier
2004-07-27 18:12       ` Arjan Bos
2004-07-27 18:36         ` Stefan Monnier
2004-08-03 18:09           ` Arjan Bos
2004-08-03 18:39             ` Stefan Monnier
2004-08-04 18:56               ` Arjan Bos
2004-08-04 19:48                 ` Stefan Monnier
2004-08-04 20:00                   ` Arjan Bos
2004-08-04 20:08                     ` Stefan Monnier
2004-08-05 19:16                       ` Arjan Bos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=lrqvdc.i9.ln@acm.acm \
    --to=acm@muc.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.