On Thu, Mar 25, 2010 at 6:21 PM, S. McKay Curtis <
smcurtis@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.
>

Thanks a lot - that looks really nice and I am going to use it. Just one
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)
>

As a suggestion:

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

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


> 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.
>
>
>
Would it be possible to regenerate the file R-function-names.txt after a new
library is loaded (should not be to difficult by redefining the function
library() and require()) and to re-read the file into emacs and use the new
list for syntax highlighting (I don't know if this is possible - I know
nothing about lisp - I know, I should...)?
This would be a really flexible and dynamic approach.

Thanks again,

Rainer

>
>
>
> On Sat, Nov 21, 2009 at 9:48 AM, S. McKay Curtis
> <smcurtis@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@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@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@stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/ess-help
> >>
> >
>
> ______________________________________________
> ESS-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/ess-help
>



-- 
NEW GERMAN FAX NUMBER!!!

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

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

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

Skype:          RMkrug
Google:         R.M.Krug@gmail.com

	[[alternative HTML version deleted]]

