[ESS] Customizing the Syntax Highlighting in R

Martin Maechler maechler at stat.math.ethz.ch
Fri Mar 26 18:51:18 CET 2010


>>>>> "RMK" == Rainer M Krug <r.m.krug at gmail.com>
>>>>>     on Fri, 26 Mar 2010 13:33:15 +0100 writes:

    RMK> On Thu, Mar 25, 2010 at 6:21 PM, S. McKay Curtis <
    RMK> smcurtis at stat.washington.edu> wrote:

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

    RMK> Thanks a lot - that looks really nice and I am going to use it. Just one
    RMK> suggestion and one question below:


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

    RMK> As a suggestion:

    RMK> if you use
    RMK> obj <- do.call("c", sapply( grep("package:", search(), value=TRUE),
    RMK> objects, all.names=TRUE))
    RMK> instead of
    RMK> obj <- do.call("c", sapply(c("package:base",
    RMK> "package:stats", "package:utils"), objects, all.names=TRUE))

    RMK> then all function names of all LOADED packages will be saved. This slows
    RMK> down the loading time of emacs, but is useful when often working with
    RMK> certain packages.

(well, it's a matter of taste ... I would *not* want that)


    >> 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.
    >> 
    >> 
    >> 
    RMK> Would it be possible to regenerate the file R-function-names.txt after a new
    RMK> library is loaded 

it's a *package* [!!!!]  which you load ...

please do read the result of

	install.packages("fortunes")
	library(package = fortunes)
	fortune(58)
	fortune(161)
	
Martin Maechler

    RMK> (should not be to difficult by redefining the function
    RMK> library() and require()) and to re-read the file into emacs and use the new
    RMK> list for syntax highlighting (I don't know if this is possible - I know
    RMK> nothing about lisp - I know, I should...)?
    RMK> This would be a really flexible and dynamic approach.

    RMK> Thanks again,

    RMK> Rainer

    >> 
    >> 
    >> 
    >> On Sat, Nov 21, 2009 at 9:48 AM, S. McKay Curtis
    >> <smcurtis at stat.washington.edu> wrote:
    >> > Hi Paul,
    >> >
    >> > This hack worked for me
    >> >
    >> > http://old.nabble.com/Font-locking-td22839833.html
    >> >
    >> > although I admit it's not very pretty.
    >> >
    >> > McKay
    >> >
    >> > On Fri, Nov 20, 2009 at 7:17 PM, Paul Heinrich Dietrich
    >> > <paul.heinrich.dietrich at gmail.com> wrote:
    >> >>
    >> >> I'm coming over to Emacs after using gvim, in which I built a R syntax
    >> >> highlighting file with thousands of keywords, because I routinely use
    >> >> numerous R packages.  In the default for Emacs + ESS, I see highlighting
    >> for
    >> >> only a few things, such as <-, TRUE, if, and for.  For example, I would
    >> like
    >> >> to add the following R functions as keywords, such as length, round,
    >> nrow,
    >> >> names, and read.csv in the colour red.  Thank you for any suggestions.
    >> >>
    >> >>
    >> >> Stephen Eglen wrote:
    >> >>>
    >> >>> I don't think there is an easy way to do this -- there is the variable
    >> >>>
    >> >>> inferior-ess-R-font-lock-keywords
    >> >>>
    >> >>> but that is not easy for a beginner to edit.  Could you give an example
    >> >>> of what you would like highlighted and in what colour?
    >> >>>
    >> >>> ______________________________________________
    >> >>> ESS-help at stat.math.ethz.ch mailing list
    >> >>> https://stat.ethz.ch/mailman/listinfo/ess-help
    >> >>>
    >> >>>
    >> >>
    >> >> --
    >> >> View this message in context:
    >> http://old.nabble.com/Customizing-the-Syntax-Highlighting-in-R-tp26443563p26453433.html
    >> >> Sent from the ESS - Help mailing list archive at Nabble.com.
    >> >>
    >> >> ______________________________________________
    >> >> ESS-help at stat.math.ethz.ch mailing list
    >> >> https://stat.ethz.ch/mailman/listinfo/ess-help
    >> >>
    >> >
    >> 
    >> ______________________________________________
    >> ESS-help at stat.math.ethz.ch mailing list
    >> https://stat.ethz.ch/mailman/listinfo/ess-help
    >> 



    RMK> -- 
    RMK> NEW GERMAN FAX NUMBER!!!

    RMK> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology,
    RMK> UCT), Dipl. Phys. (Germany)

    RMK> Centre of Excellence for Invasion Biology
    RMK> Natural Sciences Building
    RMK> Office Suite 2039
    RMK> Stellenbosch University
    RMK> Main Campus, Merriman Avenue
    RMK> Stellenbosch
    RMK> South Africa

    RMK> Cell:           +27 - (0)83 9479 042
    RMK> Fax:            +27 - (0)86 516 2782
    RMK> Fax:            +49 - (0)321 2125 2244
    RMK> email:          Rainer at krugs.de

    RMK> Skype:          RMkrug
    RMK> Google:         R.M.Krug at gmail.com

    RMK> [[alternative HTML version deleted]]

    RMK> ______________________________________________
    RMK> ESS-help at stat.math.ethz.ch mailing list
    RMK> https://stat.ethz.ch/mailman/listinfo/ess-help



More information about the ESS-help mailing list