unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Arjan Bos <Arjan.Bos@nospam.ISeeYou.nl>
Subject: Re: font-lock function matcher sample
Date: Tue, 03 Aug 2004 20:09:42 +0200	[thread overview]
Message-ID: <ceokv1$ib$1@reader08.wxs.nl> (raw)
In-Reply-To: <jwv4qntcnqn.fsf-monnier+gnu.emacs.help@gnu.org>

Stefan Monnier wrote:
>>What I have currently is working correctly.  First I find a word.  If word,
>>limit and point are the same as the previous time the function was called,
>>I return `nil'.
> 
> 
> The fact that you need to compare with last search indicates that there's
> something fishy with your code.
> 
> 
>>       (while (and (re-search-forward beatnik-font-matcher-regexp limit t)
>>		  (not (= (beatnik-last-word-score) score-match))))
> 
> 
> Here you lose the information about whether the search succeeded or failed
> which the source of your problems.  I.e. if the search fails immediately
> (typically you've hit LIMIT) then (= (beatnik-last-word-score) score-match)
> will be true and your code will screw up.
> 
> How about the code below instead:
> 
> (defun beatnik-=-matcher-p (limit score-match)
>    "Tries to find a word whose scrable score matches score-match.
> If such a word was found, t is returned."
>    (let (found)
>      (while (and (setq found (re-search-forward
>                               beatnik-font-matcher-regexp limit t))
>                  (/= (beatnik-last-word-score) score-match)))
>      found))
> 

This is almost what I intended. This sets `found' to `t' even when the 
asked for scrabble score via (beatnik-last-word-score) isn't found. But 
when I move the `and' into the `setq' it doesn't work, meaning that it 
locks itself fontifying.

(defun beatnik-=-matcher-p (limit score-match)
   "Tries to find a word whose scrable score matches score-match.
If such a word was found, t is returned."
   (let (found)
     ;; doesn't work:
     (while (and (setq found (re-search-forward
			     beatnik-font-matcher-regexp limit t))
		      (/= (beatnik-last-word-score) score-match)))
     found))

So, unfortunately, I'm back to my kludge with the global variable that 
keeps track of the last matched match.

> 
>>Stephan and Alan, many thanks for helping me out.  Maybe your comments
>>should find their way to the doc-string or the info pages?
> 
> 
> From what I can see, your problems had nothing to do understanding how
> font-lock works.  But if you have a concrete suggestion for how to change
> the docstring, patches are welcome.
> 

Well, what I really missed was a sample function. I think the one you 
posted could well serve as a template. The info page 
((elisp)Search-based Fontification.) had the needed input and output for 
the defun, but the doc-string for `font-lock-defaults' had neither. 
Maybe it could point to the info page?
Although the info page contains all the needed info, I can understand it 
now in hindsight, but the thing about `match-data' /was/ quite confusing 
for me.


And the biggest nuisance in figuring this out was that emacs crashed a 
lot when getting things wrong in the defun. It gave lots of `malloc' 
errors and `double free' errors. Which is, of course, not a pretty way 
to cancel fontifying ;-) .

-- 
--
If you really want to contact me, then replace the "I see you" text by 
its three letter accronym, hetnet.

Fabricate Diem PVNC, Motto of the Night Watch -- Terry Pratchett

  reply	other threads:[~2004-08-03 18:09 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
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 [this message]
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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to='ceokv1$ib$1@reader08.wxs.nl' \
    --to=arjan.bos@nospam.iseeyou.nl \
    --cc=Arjan.Bos@ISeeYou.nl \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).