help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Multi-line font-lock parser


From: Glenn Morris
Subject: Re: Multi-line font-lock parser
Date: Mon, 17 Aug 2009 18:29:39 -0700
User-agent: Gnus (www.gnus.org), GNU Emacs (www.gnu.org/software/emacs/)

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


reply via email to

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