[ESS] Customizing the Syntax Highlighting in R
Erik Iverson
eriki at ccbr.umn.edu
Thu Mar 25 18:32:31 CET 2010
Could you be so kind as to put a screenshot up of the results of this?
S. McKay Curtis 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.
>
> 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
> <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
More information about the ESS-help
mailing list