chainsawriot

Home | About | Archive

Fixing a sore point of ESS: `ess-indent-with-fancy-comments`

Posted on Apr 4, 2025 by Chung-hong Chan

In a previous post, I mentioned a bug-ish thing of Emacs Speaks statistics (ESS) of flymake integration.

Here is another. ESS has a pretty weird default behavior that I am kind of ashamed of. Its name is “fancy comments”. First, it explains why all of my R code is commented with two hashes. Here is the excerpt from the ESS manual.

Comments are also handled specially by ESS, using an idea borrowed from the Emacs-Lisp indentation style. By default, comments beginning with ‘###’ are aligned to the beginning of the line. Comments beginning with ‘##’ are aligned to the current level of indentation for the block containing the comment. Finally, comments beginning with ‘#’ are aligned to a column on the right (the 40th column by default, but this value is controlled by the variable comment-column,) or just after the expression on the line containing the comment if it extends beyond the indentation column. You turn off the default behavior by adding the line (setq ess-indent-with-fancy-comments nil) to your .emacs file.

In my opinion, this default is kind of ridiculous. Therefore, if you write a comment with one hash and then press Return, that comment will be aligned to the 40th column by default (or: adding 40 spaces before it). My question is: Who would want that?

Perhaps someone would want that. But I actually don’t. After many years of having this weird default, I should change that. In the manual, it says changing one variable ess-indent-with-fancy-comments would work. The reality is, it does not work. It is not as simple and therefore the manual is incorrect. The usual trick of writing a function to change a variable and attach it to the mode hook isn’t working too. As of writing, this is still an open issue.

The problem is, that variable is actually in the ess-style-alist (association list, more or less like the R list or Python dictionary). Which style to use is defined by the variable ess-style. The default ess-style is RRR. Therefore, one has to modify ess-indent-with-fancy-comments of the RRR key.

(defun dont-like-fancy ()
  "switch off fancy comments in the default RRR ess-style"
(setcdr (assoc 'ess-indent-with-fancy-comments (cdr (assoc 'RRR ess-style-alist))) nil))
(add-hook 'ess-mode-hook 'dont-like-fancy)
(add-hook 'ess-r-mode-hook 'dont-like-fancy)

This method is key of hacky, because according to the ESS manual the style RRR should be fixed. The recommended way is to use the OWN style and then customize ess-own-style-list. Actually, by default it switches off ess-indent-with-fancy-comments. This should be the speedier (and officially sanctioned) way.

(setq ess-style 'OWN)
(setf (cdr (assoc 'ess-indent-with-fancy-comments ess-own-style-list)) nil)

I think from now on, I will write comment with just one hash.


Powered by Jekyll and profdr theme