2

I know how to set coloring for an interactive R session in ESS + Emacs for all numbers. Is there a way to drill down further and set different colors for negative numbers?

  • 1
    highlight-numbers may be relevant. – PythonNut Feb 7 '15 at 23:48
  • I saw that, but isn't it for source code? I want to see a matrix that is the output of computations with the numbers highlighted differently (light/dark of same color, say) according to >, < 0... – learner Feb 8 '15 at 14:10
2

highlight-numbers mode will provide color for all numbers in a buffer, and you can use it in your R process buffer. It only supports a single highlight, so you can't use a different color for positive and negative numbers. However, you can tweak it to highlight only negative numbers, leaving the other numbers to be colored according to ESS's font-locking.

The following code colors negative numbers red in an ESS R process buffer. There are a few edge cases where it doesn't apply, if that bothers you you may need to further refine the rx expression to more accurately catch everything you want. You do need to install highlight-numbers mode before this will work!

(add-hook 'inferior-ess-r-mode-hook 'highlight-numbers-mode)

(eval-after-load "highlight-numbers.el"
  (progn (puthash 'inferior-ess-r-mode
                  (rx (and
                       not-wordchar
                       "-"
                       symbol-start
                       (+ digit)
                       (? (and "." (* digit)))
                       (? (and (any "eE")
                               (? (any "-+"))
                               (+ digit)))))
                  highlight-numbers-modelist)
         (set-face-attribute 'highlight-numbers-number nil
                             :foreground "red" )))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.