Answers:
下面的代码通过我在c-mode
缓冲区中的简短测试可以正常工作:
/*
,请点击M-j
,为其的默认绑定indent-new-comment-line
(以及c-indent-new-comment-line
in 的默认绑定c-mode
)。如果是第一个注释行,*/
则将自动插入结束字符。M-j
通过插入更多带有*
前缀的注释行可以增加击中次数。这是c-indent-new-comment-line
/ indent-new-comment-line
函数的内置行为。查看多行注释文档。*
每个注释行上的注释与注释之间至少有一个空格。(defun my-prettify-c-block-comment (orig-fun &rest args)
(let* ((first-comment-line (looking-back "/\\*\\s-*.*"))
(star-col-num (when first-comment-line
(save-excursion
(re-search-backward "/\\*")
(1+ (current-column))))))
(apply orig-fun args)
(when first-comment-line
(save-excursion
(newline)
(dotimes (cnt star-col-num)
(insert " "))
(move-to-column star-col-num)
(insert "*/"))
(move-to-column star-col-num) ; comment this line if using bsd style
(insert "*") ; comment this line if using bsd style
))
;; Ensure one space between the asterisk and the comment
(when (not (looking-back " "))
(insert " ")))
(advice-add 'c-indent-new-comment-line :around #'my-prettify-c-block-comment)
;; (advice-remove 'c-indent-new-comment-line #'my-prettify-c-block-comment)
例如,评估上面的代码后,我得到下面的打字:/*
M-j
First comment line
M-j
Second comment line
。▮表示键入结束时的光标位置。
/*
* First comment line
* Second comment line▮
*/
测试偏移注释块..
将光标放在分号后,键入:/*
M-j
Test offset comment
给出以下内容。▮表示键入结束时的光标位置。
#include<stdio.h>
main() {
printf("Hello World"); /*
* Test offset comment▮
*/
}
(setq c-default-style "bsd" c-basic-offset 4)
我的init.el
,出现这种情况:i.imgur.com/KMLx6Ll.gif任何想法?
(move-to-column star-col-num) (insert "*")
从上述解决方案中删除将为您解决此问题。我没有用C语言编写代码,因此我没有研究"bsd"
样式设置了哪些变量。