0

It used to work in c-mode, but I cannot get it back, and now I miss it in rustic, too: If in a block comment, I want Emacs to insert "\n * " after I hit return:

/**
 *<-- [if I hit return here]
 * blah blah blah<--- [or here]
 * [I want to have this " * " automatically inserted
1
  • The question isn't too clear to me. But maybe this is about electric-indent-mode? Does it help to turn that off? (It was turned on by default gratuitously starting with Emacs 24.4.)
    – Drew
    Jul 14, 2022 at 14:32

1 Answer 1

1

When in a *.c file, you can do M-x local-set-key RET RET c-indent-new-comment-line RET. Now pressing ENTER does what you want.

I forced the style to "gnu", and set c-block-comment-prefix to *. Then c-indent-new-comment-line on the command block you posted, does what you want.

(add-hook 'c-mode-hook
      (defun c-mode-hook ()
        (c-set-style "gnu" t)
        (setcdr (assoc 'c-block-comment-prefix
               (cdr (assoc "gnu" c-style-alist)))
            "*")))

Play with the value of c-block-comment-prefix of your specific c-default-style in c-style-alist.

You need to bind the obsolete variable c-comment-continuation-stars, or play around with ``c-block-comment-prefix`.

Look at c-block-comment-prefix

(defcustom-c-stylevar c-block-comment-prefix
  (if (boundp 'c-comment-continuation-stars)
      (symbol-value 'c-comment-continuation-stars)
    "* ")
  "Specifies the line prefix of continued C-style block comments.
You should set this variable to the literal string that gets inserted
at the front of continued block style comment lines.  This should
either be the empty string, or some characters without preceding
spaces.  To adjust the alignment under the comment starter, put an
appropriate value on the `c' syntactic symbol (see the
`c-offsets-alist' variable)

The definition of that var is preceded by

(make-obsolete-variable 'c-comment-continuation-stars
            'c-block-comment-prefix "21.1")

;; Although c-comment-continuation-stars is obsolete, we look at it in
;; some places in CC Mode anyway, so make the compiler ignore it
;; during our compilation.
;; [This is unclean; better to use `symbol-value'. --ttn]
;;(cc-bytecomp-obsolete-var c-comment-continuation-stars)
;;(cc-bytecomp-defvar c-comment-continuation-stars)

c-indent-new-comment-line is an interactive compiled Lisp function in cc-cmds.el.

(c-indent-new-comment-line &optional SOFT ALLOW-AUTO-FILL)

Break line at point and indent, continuing comment or macro if within one. If inside a comment and comment-multi-line is non-nil, the indentation and line prefix are preserved (see the c-comment-prefix-regexp and c-block-comment-prefix variables for details). If inside a single line comment and comment-multi-line is nil, a new comment of the same type is started on the next line and indented as appropriate for comments. If inside a macro, a line continuation backslash is inserted and aligned as appropriate, and the new line is indented according to c-syntactic-indentation.

If a fill prefix is specified, it overrides all the above.

3
  • Hm, not with my Emacs. fill-prefix is nil, comment-multi-line is t. c-block-comment-prefix was "set-from-style", tried to set it to "* ", no change. If I do M-x c-indent-new-comment-line in a block comment, I get a new line without prefix and the cursor stays at the same column...
    – Markus
    Jul 14, 2022 at 17:49
  • I have updated my earlier answer. See emacs.stackexchange.com/a/72593/31220. I have also made a note of the locations that I traversed to arrive that response.
    – user31220
    Jul 15, 2022 at 2:51
  • Thanks a lot for your explanations. While it did not really solve my problem, it pointed me in the right direction. The function I need to call to get what I want is default-indent-new-line. I mapped [RET] to this function and now got what I want.
    – Markus
    Jul 15, 2022 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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