17

In many languages, the line comment starts with a single symbol, for example # in Python and R.

I find that in Emacs, when writing such line comments, I have to repeat the comment symbol twice to make the correct indentation.

See the following example:

(setq x-select-enable-clipboard t)
                                        ;using a single comment symbol indents wrongly
;; repeating the comment symbol indents fine
(setq-default c-basic-offset 4)

With a single ; at the beginning of the line cannot get the correct indentation. How to get the correct setting? Thanks!

EDIT:

I found the solution myself. In ESS's document:

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-fancy-comments nil) to your .emacs file.

So I put this in my .emacs:

(setq ess-fancy-comments nil) ; this is for ESS

I think for Python mode, it has a similar variable.

Drew
  • 29,895
  • 7
  • 74
  • 104
Yin Zhu
  • 16,980
  • 13
  • 75
  • 117
  • 2
    This is not a bug. The elisp convention is that ; goes to a comment column, ;; is indented as code, ;;; is at column 0, and ;;;; is at column 0 for major sections. See https://www.gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.html – Krazy Glew Jul 07 '17 at 21:54
  • 1
    ^ dings someone for adding information in a comment ... wow – Jim Balter Oct 26 '18 at 00:21

2 Answers2

14

Your example use Emacs Lisp, in this language the standard convention is that a single ; is indented to the right, whereas two ;; is indented like code would be indented at that point. I strongly recommend that you stick to this convention, otherwise your code would stand out as being different. And three ;;; is indented to the left. Four ;;;; is left indented, and used for major sections. (See https://www.gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.html)

For Ruby, comments always indent as code, as far as I know.

Krazy Glew
  • 7,210
  • 2
  • 49
  • 62
Lindydancer
  • 25,428
  • 4
  • 49
  • 68
3

The major mode should take care of this properly. If not, consider filing an enhancement request or bug report to the maintainers. Of course, "properly" might be in the eye of the beholder. You can try to make your preferences known, however. And check whether the major-mode code might already have user options for this.

Beyond that, the function that is the value of variable comment-indent-function governs this. Normally, this is set by the major mode. You can set it to any function you want (e.g. on the mode hook, so that your definition overrides the one provided by the major-mode code).

It accepts no arguments, and it returns the column you want the comment to be indented to.

Here is code that indents a comment to column 0, for example:

(defun foo () (setq comment-indent-function (lambda () 0)))
(add-hook 'SOME-MODE-HOOK 'foo 'APPEND)

For Emacs-Lisp mode, for example, you would use (add-hook 'emacs-lisp-mode-hook 'foo 'APPEND).

Drew
  • 29,895
  • 7
  • 74
  • 104
  • 2
    This answer should be rejected: while correct on its face, the behavior is not a bug, and this answer betrays ignorance of emacs conventions such as https://www.gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.html – Krazy Glew Jul 07 '17 at 21:51
  • @KrazyGlew: There is nothing in this answer that says the behavior is a bug or that betrays ignorance of the commenting conventions you cite. You might want to read it again. And you might want to read the question again. The question is not asking for commenting conventions, GNU or otherwise. OP asks how to change a behavior that s?he does not want - behavior that is "wrong" from the OP's point of view. – Drew Jul 07 '17 at 23:41
  • 1
    I agree that there was nothing in the OP question that said that the behavior was a bug. However, in the accepted answer (your answer) you say "...consider filing an enhancement request or bug report..." - sounds like you are suggesting it is a bug to me. As for "betraying ignorance" it is hard to prove lack of knowledge, but certainly the answer does not display that knowledge. – Krazy Glew Jul 08 '17 at 00:10
  • As for OP not asking what comment conventions exist - again, yes, but many years of experience as "programming help desk" taught me the importance of addressing the underlying user problem, not the possibly confused top level question. Think about what your answer accomplishes: The OP is obviously newby-ish. If he follows your advice, the amount of code that does not conform to emacs conventions has increased. If this user ever writes code that hev wants to share with the world, that is bad. – Krazy Glew Jul 08 '17 at 00:14
  • Plus, you may have sent the user off on an "educational" exercise, customizing comment-indent-function. Good learning experience, but not necessarily the best use of OP time. – Krazy Glew Jul 08 '17 at 00:15
  • now, I do owe you an apology: I was a bit grumpy, and I used harsh (to Americans) language "betrays ignorance". Better wording might have been "this answer does not mention relevant emacs conventions like http://gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.h‌​tml that usually make it unnecessary for single-; comments to be indented at column 0 or as code." If you had mentioned the conventions, and then said "But if you (the OP) really want to indent single-; comments as code or at column 0, then ..." (the rest of your answer) I would have no problem with it. – Krazy Glew Jul 08 '17 at 00:23
  • I could make this change to your answer if you want - I will leave it up to you. – Krazy Glew Jul 08 '17 at 00:25
  • 1
    @KrazyGlew Silly squabble here. Both this answer and mentioning the conventions provide useful information, and SO answers are for posterity, not just the specific person who asked. – Jim Balter Oct 26 '18 at 00:32