Friday, November 16, 2007

[Emacs Tips] 注释

M-x comment-dwim (Do What I Mean) 是最聪明的一种注释方式。如果已经选定了区域,那么就是注释或者反注释;如果没有选定区域,那么就是在这行末尾添加注释。

默认的键绑定是 M-;

注释还可以选择不同的模式,不同注释符号,在不同的编程语言模式下都不同。

控制注释符号的是变量 comment-start 和 comment-end,用 C-h v 查看,用例如 (setq comment-start "//") 修改。

变量 comment-style 控制注释的样式。可以用例如 (setq comment-style 'box) 修改。
以下是各种注释风格的示例。

int main()
{
/* this is a comment line. */
/* comment style 'plain */

/* this is a comment line. */
/* comment style 'indent */

/* this is a comment line. */
/* comment style 'aligned */

/* this is a comment line.
* comment style 'multi-line */

/*
* this is a comment line.
* comment style 'extra-line
*/

/***************************/
/* this is a comment line. */
/* comment style 'box */
/***************************/

/****************************
* this is a comment line. *
* comment style 'box-multi *
****************************/
}
再提供一个直接注释一行的函数。有时候比 comment-dwim 更有效。有选中区域的话就是注释或反注释这个区域;反之注释或者反注释光标所在的行。
(defun huangq-comment-dwim (&optional n)
"Comment do-what-i-mean"
(interactive "p")
(apply 'comment-or-uncomment-region
(if (and mark-active transient-mark-mode)
(list (region-beginning) (region-end))
(if (> n 0)
(list (line-beginning-position) (line-end-position n))
(list (line-beginning-position n) (line-end-position))))))

No comments: