From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stephen Berman Newsgroups: gmane.emacs.devel Subject: Re: Conditionalized font-locking? Date: Thu, 10 May 2012 17:22:06 +0200 Message-ID: <87ipg49i3l.fsf@escher.home> References: <871umw74yr.fsf@escher.home> <87ipg858fd.fsf@escher.home> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1336663355 12957 80.91.229.3 (10 May 2012 15:22:35 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 10 May 2012 15:22:35 +0000 (UTC) Cc: emacs-devel@gnu.org To: "Stefan Monnier" Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu May 10 17:22:32 2012 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SSVCE-0000yl-15 for ged-emacs-devel@m.gmane.org; Thu, 10 May 2012 17:22:26 +0200 Original-Received: from localhost ([::1]:60160 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSVCB-0001uE-5J for ged-emacs-devel@m.gmane.org; Thu, 10 May 2012 11:22:23 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:44450) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSVC2-0001tF-Mi for emacs-devel@gnu.org; Thu, 10 May 2012 11:22:21 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SSVC0-0001Wj-JW for emacs-devel@gnu.org; Thu, 10 May 2012 11:22:14 -0400 Original-Received: from mailout-de.gmx.net ([213.165.64.23]:40197) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1SSVC0-0001Vi-9U for emacs-devel@gnu.org; Thu, 10 May 2012 11:22:12 -0400 Original-Received: (qmail invoked by alias); 10 May 2012 15:22:07 -0000 Original-Received: from i59F57B4B.versanet.de (EHLO escher.home) [89.245.123.75] by mail.gmx.net (mp035) with SMTP; 10 May 2012 17:22:07 +0200 X-Authenticated: #20778731 X-Provags-ID: V01U2FsdGVkX18N8m9oykINqikkpsNVT25zg5mvDK2tR2bHEB/iDf zHwqdPgnqGCJwI In-Reply-To: <87ipg858fd.fsf@escher.home> (Stephen Berman's message of "7 May 2012 17:15:34 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.1.50 (gnu/linux) X-Y-GMX-Trusted: 0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 213.165.64.23 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:150422 Archived-At: On 7 May 2012 17:15:34 +0200 "Stephen Berman" wrote: > On Mon, 07 May 2012 11:05:27 -0400 Stefan Monnier wrote: > >>> (defun srb-number-matcher (lim) >>> "Search for odd numbers within LIM for font-locking." >>> (re-search-forward "\\<\\([0-9]+\\)\\>" lim t) >>> (let ((num (match-string-no-properties 1))) >>> (when (eq (logand (string-to-number num) 1) 1) num))) >> >> If the re-search-forward fails, this function will mis-behave. > > D'oh! Unfortunately, I'm still not getting what I want. It's not unlikely I'm again overlooking something obvious (though I've looked long and hard, and tried lots of variations), so if you or someone else can point it out, I'd be grateful. Again, what I want is to fontify date-time strings with a special face if the date in the string is earlier than the current date; moreover, the time string is optional. The following code (which is close to what I'm actually using but with a simpler regexp and filtering condition) seems to work at first: --8<---------------cut here---------------start------------->8--- (defvar srb-font-lock-keywords (list '(srb-date-time-matcher (1 font-lock-warning-face t t)) '(srb-date-time-matcher (2 font-lock-warning-face t t)))) (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))))) (define-derived-mode srb-mode nil "SRB" () "Mode for testing font-locking." (set (make-local-variable 'font-lock-defaults) '(srb-font-lock-keywords t))) --8<---------------cut here---------------end--------------->8--- However, if the buffer contains a string that matches the regexp but fails the second when-condition, then not only is it -- correctly -- not fontified but subsequent matching dates that pass the when-condition are also -- incorrectly -- not fontified. Here's a test file showing the problem: --8<---------------cut here---------------start------------->8--- 2012-04-01 10:00 2012-05-02 2013-05-01 10:00 2012-03-10 10:00 2012-04-01 10:00 2012-05-02 2012-03-10 10:00 2012-04-01 10:00 2012-05-02 2012-03-10 10:00 Local Variables: mode: srb End: --8<---------------cut here---------------end--------------->8--- When I evaluate the code above and then visit this file, I see only the first two dates in font-lock-warning-face. However, if I modify the buffer at one of the dates after the 2013 date that fails the when-condition, e.g. by typing a space, then that date becomes fontified (and maybe the following one too, this seems to vary). (With my actual code and data, some but not all dates after a correctly non-fontified date are correctly fontified, though I've found no pattern to explain this; but I hope a solution to the above case will also take care of this wrinkle.) I suspect the problem has to do with this (info (elisp)Search-based Fontification): 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. I tried resetting point on failure, but that either didn't have an effect or it resulted in all dates being fontified. So how can I get all and only dates that satisfy the when-condition fontified? Steve Berman