5

Indentation in ESS is occasionally a little kooky, and I'm trying to figure out if the kookiness is a bug or a feature. How does one get proper indentation of parentheses after an "=" sign in ESS (say, in R-mode)?

As an example, I'd expect indentation to look like this for the "labels" parameter:

somevar <- factor(x = 1:3,
                  labels = c("a",
                             "b",
                             "c"))

Instead, I get:

somevar <- factor(x = 1:3,
                  labels = c("a",
                    "b",
                    "c"))

This version (note the extra parentheses wrapped around the c(...) expression) seems to work just fine, however:

somevar <- factor(x = 1:3,
                  labels = (c("a",
                              "b",
                              "c")))

This oddity only seems to occur if it's after the "=". How do I get the desired behavior? Am I missing some setting somewhere? (I hope so: wading through the source code for ess-indent-line is not an explosion of joy.)

NOTE: I'd originally asked a version of this question on Stack Overflow about half a year ago -- it got a little interest but zero answers.

Dan
  • 32,584
  • 6
  • 98
  • 168

1 Answers1

8

Set ess-arg-function-offset to nil:

(setf ess-arg-function-offset nil)

For me this indents your examples the way that you want them to be. Read the documentation of the variable to find out why (and note that c is a function call).

Simply setting it once in an init file does not work. A simple way for it to register is to set it in ess-mode-hook:

(add-hook 'ess-mode-hook (lambda () (setq ess-arg-function-offset nil)))

This is because of the way that those style variables are set from a list of possible default styles (listed in ess-style-alist) for each ess buffer at their creation. This hook adjusts ess-arg-function-offset after the fact, which is why it works when a single setting in init.el doesn't work. You can define your own style and give it a name, and use it instead of one of the builtin ones, similar to what you can do with CC mode.

krupped
  • 131
  • 1
  • Beautiful, thanks! Oddly enough, simply setting it once in an init file does not seem to work -- I had to set it in `ess-mode-hook` for it to register. – Dan Oct 03 '14 at 00:31
  • @Dan, this is because of the way that those style variables are set from a list of possible default styles (listed in `ess-style-alist`) for each ess buffer at their creation. Your hook adjusts `ess-arg-function-offset` after the fact, which is why it works when a single setting in `init.el` doesn't work. You can define your own style and give it a name, and use it instead of one of the builtin ones, similar to what you can do with `cc-mode`. – krupped Oct 03 '14 at 01:43