【问题标题】:Formatting of javadoc style comments in emacs在 emacs 中格式化 javadoc 样式注释
【发布时间】:2013-10-24 12:19:45
【问题描述】:

我们需要使用 javadoc 格式的 doxygen cmets 来注释我们的 C++ 代码,我正在 emacs 中寻找能够在我键入时保持 javadoc 风格的东西。

所以如果我开始写这样的评论:

/**
 * This function does the following:

当我点击“输入”时,我希望光标自动缩进并插入一个“*”,这样我就可以继续输入而无需手动格式化。 所以当我点击“返回”时,评论现在应该是这样的(不用我输入“[TAB]*”):

/**
 * This function does the following:
 * 

【问题讨论】:

标签: c++ emacs


【解决方案1】:

在这里找到答案:http://www.frankmeffert.de/2010/09/emacs-doxygen-doxymacs/ 我对 C 和 C++ 模式进行了微调,并在每个“*”之后添加了一个额外的空格

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

【讨论】:

  • 附带说明:符号 tnil 会自行评估,您无需引用它们。
  • (if <foo> 't 'nil) 只是写<foo> 的一种迂回方式。如果你大声读出来,你就会明白为什么:“如果 为真,则为真,如果为假,则为假”。
【解决方案2】:

有一个变量c-block-comment-prefix 控制/*...*/-style cmets 中续行的前缀。

设置为

(setq c-block-comment-prefix "* ")

你的观点在完整——即封闭——评论区(| 是重点)

1. /|* */
2. /*| */
3. /* |*/
4. /* *|/

当你按下 M-jc-indent-new-comment-line 命令)时,你会得到以下结果:

/*
 * */

适用于 23 和 24 Emacsen。

【讨论】:

    【解决方案3】:

    IIUC,点击 M-j 而不是 RET 应该会给你想要的行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 2010-09-11
      • 2014-12-11
      • 2020-01-20
      • 2012-06-09
      • 2017-03-22
      • 1970-01-01
      • 2017-04-28
      相关资源
      最近更新 更多