perogiex
perogiex

Reputation: 515

Formatting of javadoc style comments in emacs

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:
 * 

Upvotes: 5

Views: 1792

Answers (3)

perogiex
perogiex

Reputation: 515

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)))

Upvotes: 3

immerrr
immerrr

Reputation: 1273

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.

Upvotes: 3

Stefan
Stefan

Reputation: 28541

IIUC, hitting M-j instead of RET should give you the behavior you want.

Upvotes: 2

Related Questions