4

由于外部强制执行的编码标准,我需要编写所有四个边对齐边框的代码注释。

在 Emacs 中,最简单的方法是:

  1. 将一些文本转换成这种格式的块注释
  2. 编辑块注释中的文本并让 Emacs 正确重排它

评论格式示例:

#*****************************************************************************#
#* This is a block comment. It has right-aligned borders.                    *#
#*                                                                           *#
#* It can have separate paragraphs. Text in a long paragraph flows from one  *#
#* line to the next with one space of internal padding.                      *#
#*                                                                           *#
#* The right hand border is at char 78                                       *#
#*****************************************************************************#
sub MySub
{
  #***************************************************************************#
  #* The left hand edge of the comment is aligned with the current code      *#
  #* block, but the right hand border is still at char 78                    *#
  #***************************************************************************# 
  my $var = 3;
}

auto-fill-mode本身不保留右侧边界。

4

4 回答 4

3

古老而多功能的库“ rebo​​x2 ”可以做到这一点。

要安装,请将https://raw.github.com/lewang/rebox2/master/rebox2.el下载到您的 site-lisp 目录中,并将以下内容添加到您的.emacs文件中:

(require 'rebox2)

; The following template defines the specific style required here,
; which does not correspond to any built-in rebox2 style.
;
; "75" means that the style is registered as x75, where "x" depends
; on the current langauge mode. The "?" char is switched for the language
; specific comment char
;
; "999" is the weighting used for recognising this comment style.
; This value works for me.
(rebox-register-template
 75
 999
 '("?*************?"
   "?* box123456 *?"
   "?*************?"))

(add-hook 'perl-mode-hook (lambda ()
; The "style loop" specifies a list of box styles which rebox will cycle
; through if you refill (M-q) a box repeatedly. Having "11" in this loop
; will allow you to easily "unbox" a comment block, e.g. for "uncomment-region"
                (set (make-local-variable 'rebox-style-loop) '(75 11))
; The "min-fill-column" setting ensures that the box is not made narrower
; when the text is short
                (set (make-local-variable 'rebox-min-fill-column) 79)
                (rebox-mode 1)))

现在:

  1. 要将某些文本转换为这种格式的块注释,您只需选择文本并执行M-q
  2. 要编辑块注释中的文本,您可以直接编辑文本,emacs 会自动重排该框。M-q(如果 emacs 没有自动执行,您可能需要请求重排。)
于 2013-08-13T15:27:59.220 回答
2

Mx 自定义变量 RET 注释样式 RET

然后从 value-menue 中选择“box”

于 2013-08-13T14:52:02.903 回答
1

您可以yasnippet用于插入和overwrite-mode编辑。

如果你想要自动换行,你也可以 kill rectangle C-x r k,切换到临时缓冲区C-x b, yank rectangle C-x r y。随心所欲地编辑那里。然后,从临时缓冲区中删除矩形并粘贴到您的源中。

这是块开始/结束片段:

# -*- mode: snippet -*-
# name: block comment
# key: bb
# --
`(concat "#" (make-string (- 80 aya-tab-position) ?*) "#")`
$0
`(concat "#" (make-string (- 80 aya-tab-position) ?*) "#")`

这是块行代码段:

# -*- mode: snippet -*-
# name: block comment line
# key: bl
# --
`"#* "`$1${1:$(make-string (- 78 aya-tab-position (length text)) ? )}#

请注意,我在这里使用auto-yasnippet包变量aya-tab-position。要在片段中使用它,您必须使用aya-open-line. 您可以从 MELPA 获得这两个软件包。

于 2013-08-13T14:49:10.077 回答
0

(1)的部分答案(但我很想听到更好的答案):

  • 将注释段落输入为纯文本
  • M-x set-fill-column 76
  • 将光标放在评论段落的开头并手动输入开头的“#*”(注意空格)
  • 将光标放在注释段落的第一个字母处,运行M-x set-fill-prefix
  • 现在使用M-q回流 para
  • 这将对自动换行进行排序并将左侧边框放置到位。
  • 最后,手动添加顶部、底部和右侧边框。

请参阅http://www.gnu.org/software/emacs/manual/html_node/emacs/Fill-Prefix.html

相同的方法可用于(2)中的一些:

  • 确保set-fill-columnset-fill-prefix是正确的(见上文)
  • 删除所有右侧边框标记
  • 使用回流参数M-q
  • 最后,手动添加顶部、底部和右侧边框。
于 2013-08-13T14:51:26.870 回答