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: Arjan Bos
Subject: Re: font-lock function matcher sample
Date: Tue, 03 Aug 2004 20:09:42 +0200
User-agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7) Gecko/20040616

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 via email to

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