When I'm writing a comment and press M-j
to continue the comment on the next line (using single-line comments), if I press it multiple times say because I want to leave the next comment line "empty" for separation, the cursor ends up appearing right after the comment delimiter. Instead, I always want the cursor to be separated from the comment delimiter by one space. I'd like this to be the case in every mode, globally if possible.
As an example, if I have this:
;; comment|
If I press M-j
I get this:
;; comment
;; |
It's correctly separated from the comment delimiter by one space.
If without doing anything else I press M-j
again, I get this:
;; comment
;;
;;|
So from then on there is no space added between the comment delimiter and the cursor. I want there to always be a space of separation.
By comparison, in c++-mode I always get the space.
I'd like to know if there's a way to get this in other modes (or even all other modes) before I resort to hacking it in by wrapping the function myself.
EDIT: I ended up advising comment-indent-new-line
to add one space if there aren't any spaces following the comment-start:
(define-advice comment-indent-new-line
(:after (&optional soft) at-least-one-space)
"Ensure that at least one space is added after the comment-start."
(let ((start (regexp-quote comment-start)))
(when (and (nth 4 (syntax-ppss))
(looking-back start (+ (point) (length start)))
(not (looking-back " " (+ (point) 1))))
(insert " "))))
M-j
, it continues the comment with the same indentation, which I like. It seems like this would force there to be a single space? I ended up creating my own advice which adds one space if there aren't any spaces after the comment-start, which I think is more flexible. – Jorge Israel Peña Jun 05 '16 at 21:46M-j
it does add a space? I think what happens is it takes on the same indentation as the previous comment, which was one space, but when you pressM-j
again without typing anything, that's considered a comment with no spaces behind it/no indentation, so the third comment doesn't have any indentation. – Jorge Israel Peña Jun 07 '16 at 19:21M-j
with only whitespace on the first line you'll also get that result. – Qudit Jun 08 '16 at 03:25