unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Glenn Morris <rgm+news@stanford.edu>
To: help-gnu-emacs@gnu.org
Subject: Re: Multi-line font-lock parser
Date: Mon, 17 Aug 2009 18:29:39 -0700	[thread overview]
Message-ID: <fo1vn96ha4.fsf@xoc2.stanford.edu> (raw)
In-Reply-To: 87tz0fwk7h.fsf@iki.fi

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)))


      parent reply	other threads:[~2009-08-18  1:29 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-10 19:19 Multi-line font-lock parser Teemu Likonen
2009-08-10 21:32 ` Thierry Volpiatto
     [not found] ` <mailman.4341.1249940353.2239.help-gnu-emacs@gnu.org>
2009-08-11  4:28   ` Teemu Likonen
2009-08-11  5:48     ` Thierry Volpiatto
     [not found]     ` <mailman.4364.1249970086.2239.help-gnu-emacs@gnu.org>
2009-08-11  6:18       ` Teemu Likonen
2009-08-11  7:55         ` Thierry Volpiatto
2009-08-18  1:29 ` Glenn Morris [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fo1vn96ha4.fsf@xoc2.stanford.edu \
    --to=rgm+news@stanford.edu \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).