From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Glenn Morris Newsgroups: gmane.emacs.help Subject: Re: Multi-line font-lock parser Date: Mon, 17 Aug 2009 18:29:39 -0700 Organization: None Message-ID: References: <87tz0fwk7h.fsf@iki.fi> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1250559654 6701 80.91.229.12 (18 Aug 2009 01:40:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 18 Aug 2009 01:40:54 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Aug 18 03:40:47 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MdDgs-0004L2-U7 for geh-help-gnu-emacs@m.gmane.org; Tue, 18 Aug 2009 03:40:47 +0200 Original-Received: from localhost ([127.0.0.1]:44011 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MdDgs-0000l2-7u for geh-help-gnu-emacs@m.gmane.org; Mon, 17 Aug 2009 21:40:46 -0400 Original-Path: news.stanford.edu!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 34 Original-NNTP-Posting-Host: xoc2.stanford.edu Original-X-Trace: news.stanford.edu 1250558979 10060 171.64.109.31 (18 Aug 2009 01:29:39 GMT) Original-X-Complaints-To: news@news.stanford.edu X-Spook: ammo Soviet hostage Abu Nidal primers conspiracy X-Ran: }y*x3Gs{NL;GG'_>wss],~Yb{-'I':[EoU'k82fx$y[WsII%)~lAWuG#uTA5}eNaDeM6YD X-Hue: black X-Attribution: GM Mail-Copies-To: nobody User-Agent: Gnus (www.gnus.org), GNU Emacs (www.gnu.org/software/emacs/) Cancel-Lock: sha1:jxmnx9ufOUh4ZN8Tiyhd21pjdqM= Original-Xref: news.stanford.edu gnu.emacs.help:172062 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:67237 Archived-At: Teemu Likonen wrote: > I'd like to write font-lock code which highlights the first line that > (1) is non-empty and (2) does not start with a "#" comment character. > This requires some multi-line parsing so plain regular expressions won't > suffice. The following is probably dumb, but may give you the idea. (defun my-font-lock-example (limit) ;; We must move point, set match data, and return non-nil if there ;; is something to highlight. We must only return nil when there ;; are no more matches before LIMIT. (when (and (re-search-forward "^[^#\n].*" limit t) (not (save-excursion (save-match-data (re-search-backward "^[^#\n]" nil t 2))))) ;; Need to rescan if insert text anywhere before current match and ;; the start of the buffer. If delete the current match, need to ;; rescan up to the next match or the end. Yuck. (put-text-property (point-min) (save-excursion (save-match-data (or (re-search-forward "^[^#\n].*" nil t) (point-max)))) 'font-lock-multiline t) t)) (setq foo-font-lock-keywords '((my-font-lock-example . font-lock-warning-face) ("FOO" . font-lock-constant-face))) (define-derived-mode foo-mode fundamental-mode "foo" "foo" (set (make-local-variable 'font-lock-defaults) '(foo-font-lock-keywords t)))