[ESS] font-lock with Sweave/noweb

Claudia Beleites cbeleites at units.it
Thu May 8 19:50:38 CEST 2008


Hello,

I just started to use Sweave and therefore tried ESS. Nice - I really enjoy 
it.
Anyways, I was looking for a way to have a stronger visualization of which 
parts of the buffer are code an which are doc chunks.

So I introduced some changes to my noweb-font-lock-mode.el:
2 new faces are for highlighting doc and code chunk start lines with yellow 
and blue background.
The font-lock-keywords are changed so that the complete line (including 
newline character) is highlighted for the start of code (i.e. << >>=) and doc 
(i.e. @) chunks, but not for reused code chunks (<<label>>). The chunk start 
indicators need to start now at the first character of the line - as Sweave 
and noweb want it. Further characters after those tags are highlighted as 
comments.

The diff is below - I hope it is useful for you as well. 
However, I'm not familiar with elisp, so use the code carefully. And I'd 
appreciate suggestions (about better programming). 
I think it should be possible to extend this to use different background for 
the complete chunks (which would have been my ultimate goal :-) ) but I'm not 
sure how the regexps should look (and I read that the font-lock-keywords are 
not thought for multiline font locking). 

Cheers,

Claudia



diff -ur noweb-font-lock-mode.el.orig noweb-font-lock-mode.el
--- noweb-font-lock-mode.el.orig        2008-05-08 17:58:07.000000000 +0200
+++ noweb-font-lock-mode.el     2008-05-08 19:15:07.000000000 +0200
@@ -5,6 +5,8 @@
 ;; Copyright (C) 2002 by A.J. Rossini <rossini at u.washington.edu>
 ;; Copyright (C) 2003--2004 A.J. Rossini, Rich M. Heiberger, Martin
 ;;     Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
+;; 2008 C. Beleites <cbeleites at units.it> highlight first lines of
+;;      code & doc chunks

 ;; Maintainers: ESS-core <ESS-core at stat.math.ethz.ch>

@@ -125,6 +127,19 @@
 ;; (AJR) the next two lines were originally font-lock-warning-face
 ;; methods; XEmacs 20.4 doesn't define this, sigh...  -- KLUDGE --.

+;; (C. Beleites) 2 new faces for highlighting the start lines of chunks
+(defface noweb-font-lock-code-chunk-face
+  '((t (:background "#FFFFA0")))
+  "Face to highlight noweb code chunks")
+(defface noweb-font-lock-doc-chunk-face
+  '((t (:background "#B0C0FF")))
+  "Face to highlight noweb doc chunks")
+
+(defvar noweb-font-lock-code-chunk-face 'noweb-font-lock-code-chunk-face
+  "Face to use to highlight the first line of each code chunk")
+(defvar noweb-font-lock-doc-chunk-face 'noweb-font-lock-doc-chunk-face
+  "Face to use to highlight the first line of each doc chunk (except the very 
first at the beginning of the file")
+
 (defvar noweb-font-lock-doc-start-face font-lock-reference-face
   "Face to use to highlight the `@' at the start of each doc chunk")

@@ -253,17 +268,37 @@
 ;                   (3 noweb-font-lock-brackets-face prepend))
 ;                  ("^@[ \t\n]+"
 ;                   (0 noweb-font-lock-doc-start-face prepend )))))
+
+;; C. Beleites: changed keywords for highlighting the first lines
+;;       (append font-lock-keywords
+;;               '(("\\(\\[\\[\\)\\([^]]*\\]*\\)\\(\\]\\]\\|\\$\\)"
+;;                  (1 font-lock-reference-face prepend )
+;;                  (2 font-lock-keyword-face prepend)
+;;                  (3 font-lock-reference-face prepend))
+;;                 ("^[ \t\n]*\\(<<\\)\\([^>]*\\)\\(>>=?\\)"
+;;                  (1 font-lock-reference-face  prepend )
+;;                  (2 font-lock-keyword-face prepend)
+;;                  (3 font-lock-reference-face prepend))
+;;                 ("^@[ \t\n]+"
+;;                  (0 font-lock-reference-face prepend )))))
          (append font-lock-keywords
                  '(("\\(\\[\\[\\)\\([^]]*\\]*\\)\\(\\]\\]\\|\\$\\)"
                     (1 font-lock-reference-face prepend )
                     (2 font-lock-keyword-face prepend)
                     (3 font-lock-reference-face prepend))
-                   ("^[ \t\n]*\\(<<\\)\\([^>]*\\)\\(>>=?\\)"
+                   ("^[ \t\n]*<<\\([^>]+\\)>>"        ;; reuse code chunk
+                    (1 font-lock-keyword-face  prepend ))
+                   ("^\\(<<\\)\\([^>]*\\)\\(>>=\\)\\(\\(?:[ \t][^\n]*\\)?\\)
[\n]"        ;; code chunk start
+                    (0 noweb-font-lock-code-chunk-face prepend )
                     (1 font-lock-reference-face  prepend )
                     (2 font-lock-keyword-face prepend)
-                    (3 font-lock-reference-face prepend))
-                   ("^@[ \t\n]+"
-                    (0 font-lock-reference-face prepend )))))
+                    (3 font-lock-reference-face prepend)
+                    (4 font-lock-comment-face prepend))
+                   ("^@\\(\\(?:[ \t][^\n]*\\)?\\)
[\n]"                                    ;; doc chunk start
+                    (0 font-lock-reference-face prepend )
+                    (0 noweb-font-lock-doc-chunk-face prepend )
+                    (1 font-lock-comment-face prepend)
+                    ))))




-- 
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 2
I-34127 Trieste

phone: +39 (0 40) 5 88-34 47
email: cbeleites at units.it




More information about the ESS-help mailing list