[ESS] Customizing the Syntax Highlighting in RS. McKay Curtis smcurtis at stat.washington.edu
Thu Mar 25 18:21:25 CET 2010
* Previous message: [ESS] Emacs not recognizing the "*.r" and "*.q" files.
* Next message: [ESS] Customizing the Syntax Highlighting in R
* Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
I've finally figured out a less hackish way of customizing the syntax
highlighting in R. Unfortunately, Emacs's regular expression
implementation does not have "lookahead," so more words are
highlighted than I would prefer, specifically, options in functions
that happen to have the same name as an R function are also
highlighted. One way to avoid this is to include the opening
parenthesis in the regular expression for the syntax highlighting, but
the solution then becomes a problem --- having that opening
parenthesis highlighted is a minor annoyance. So if any one has any
bright ideas on how to fix that I would appreciate hearing them.
Anyway, here's how to customize the syntax highlighting for R:
1) Run the following lines of code in R. The R code will create a
text file with the names of all the functions in the base, stats, and
utils R packages. If you would like more syntax highlighting for
functions in other commonly used packages, you can add other packages
by modifying the code. The code stores the text file in the
".emacs.d" directory, but you can modify the code to store this file
wherever you prefer. (Truth be told, I don't really know what the
.emacs.d directory is really supposed to be used for... It just
magically appeared one day, so I use it.)
## Get R function names from packages base, stat, and utils
## and store them in a file for ESS to use for font locking / syntax
highlighting
obj <- do.call("c", sapply(c("package:base", "package:stats",
"package:utils"), objects, all.names=TRUE))
re <- "(^[^.[:alpha:][:digit:]]|<-|__)" # to remove "weird" functions
obj <- obj[-grep(re, obj)]
fpath <- file.path(Sys.getenv("HOME"), ".emacs.d", "R-function-names.txt")
write.table(obj, fpath, quote=FALSE, row.names=FALSE, col.names=FALSE)
2) Add the following lines of code to your .emacs file:
;; Read a whole file into list of lines
;; Author: Xah Lee
;; see http://xahlee.org/emacs/elisp_process_lines.html
(defun read-lines (file)
"Return a list of lines in FILE."
(with-temp-buffer
(insert-file-contents file)
(split-string
(buffer-string) "\n" t)
)
)
(add-hook 'ess-mode-hook
'(lambda()
(setq ess-my-extra-R-function-keywords
(read-lines "~/.emacs.d/R-function-names.txt"))
(setq ess-R-mode-font-lock-keywords
(append ess-R-mode-font-lock-keywords
(list (cons (concat "\\<" (regexp-opt
ess-my-extra-R-function-keywords 'enc-paren) "\\>")
'font-lock-function-name-face))))))
If you don't like the particular color, you can change the "
'font-lock-function-name-face " to something else (see the code in
ess-custom.el file).
3) Enjoy the dazzling display of colors in R.
On Sat, Nov 21, 2009 at 9:48 AM, S. McKay Curtis
来源:https://stat.ethz.ch/pipermail/ess-help/2010-March/005974.html