[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Conditionalized font-locking?
From: |
Stephen Berman |
Subject: |
Re: Conditionalized font-locking? |
Date: |
Thu, 10 May 2012 20:18:50 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.1.50 (gnu/linux) |
On Thu, 10 May 2012 09:30:17 -0600 Davis Herring <address@hidden> wrote:
>> (defun srb-date-time-matcher (lim)
>> "Search for date-time strings within LIM for font-locking."
>> (when (re-search-forward (concat "^\\(?1:[0-9][0-9][0-9][0-9]-"
>> "[0-9][0-9]-[0-9][0-9]\\)"
>> " \\(?2:[0-9]?[0-9]:[0-9][0-9]\\)?")
>> lim t)
>> (let* ((date (match-string-no-properties 1))
>> (time (match-string-no-properties 2))
>> ;; days-between needs a non-empty time string.
>> (date-time (concat date " " (or time "00:00"))))
>> (when (< (days-between date-time (current-time-string)) 0)
>> (concat date " " time)))))
>> [...]
>> Fontification will call FUNCTION repeatedly with the same limit,
>> and with point where the previous invocation left it, until
>> FUNCTION fails. On failure, FUNCTION need not reset point in any
>> particular way.
>
> If any of your searches turns up a date you don't like, your function
> fails (returns nil), so font-lock gives up. You need to (unfortunately)
> write much the same loop that font-lock is wrapping around your function
> so that it finds the first date that it cares about rather than
> returning nil.
Thank you, that was the part I didn't quite understand.
> The easy way to do this if you don't have too many dates being fontified
> at one go is just to recursively call yourself when you don't like your
> current match:
>
> (or (< ...) (srb-date-time-matcher lim))
>
> Note that you needn't return any particular non-nil value on success;
> it's the match-data that font-lock uses.
This works perfectly, thanks very much!
Steve Berman