[Top][All Lists]

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

Re: Conditionalized font-locking?

From: Davis Herring
Subject: Re: Conditionalized font-locking?
Date: Thu, 10 May 2012 09:30:17 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20110717 Lanikai/3.1.11

> (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.

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 product is sold by volume, not by mass.  If it appears too dense or
too sparse, it is because mass-energy conversion has occurred during

reply via email to

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