We need to comment our C++ code using javadoc formatted doxygen comments and I'm looking for something in emacs that can keep up the javadoc style as I type.
So if I start writing a comment like this:
/**
* This function does the following:
When I hit "enter", I'd like the the cursor to auto indent and insert a "* " so I can continue typing without formatting manually. So when I hit "return" the comment should now look like this (without me typing "[TAB]* "):
/**
* This function does the following:
*
Found the answer here: http://www.frankmeffert.de/2010/09/emacs-doxygen-doxymacs/ I made minor tweaks to work for C and C++ modes and add an extra space after each "*"
(defun my-javadoc-return ()
"Advanced C-m for Javadoc multiline comments.
Inserts `*' at the beggining of the new line if
unless return was pressed outside the comment"
(interactive)
(setq last (point))
(setq is-inside
(if (search-backward "*/" nil t)
;; there are some comment endings - search forward
(search-forward "/*" last t)
;; it's the only comment - search backward
(goto-char last)
(search-backward "/*" nil t)
)
)
;; go to last char position
(goto-char last)
;; the point is inside some comment, insert `* '
(if is-inside
(progn
(insert "\n* ")
(indent-for-tab-command))
;; else insert only new-line
(insert "\n")))
(add-hook 'c-mode-common-hook (lambda ()
(local-set-key "\r" 'my-javadoc-return)))
There's a variable c-block-comment-prefix
that controls the prefix of continued lines within /*...*/
-style comments.
With it being set to
(setq c-block-comment-prefix "* ")
and your point inside full — i.e. closed — comment block (|
being the point)
1. /|* */
2. /*| */
3. /* |*/
4. /* *|/
when you press M-j (c-indent-new-comment-line
command), you end up with the following:
/*
* */
Works both for 23 and 24 Emacsen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With