emacs使用右对齐边框制作方块注释

时间:2013-08-13 14:14:19

标签: emacs code-formatting ascii-art

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

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

  1. 以一种格式将一些文本转换为块注释
  2. 编辑块注释中的文本并让Emacs正确重排
  3. 评论格式示例:

    #*****************************************************************************#
    #* 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 个答案:

答案 0 :(得分:3)

古老而多功能的图书馆“rebox2”可以做到这一点。

要安装,请将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将自动重排该框。 (如果emacs没有自动执行此操作,您可能需要执行M-q请求重排。)

答案 1 :(得分:2)

M-x自定义变量RET注释样式RET

然后从value-menue

中选择“box”

答案 2 :(得分:1)

您可以使用yasnippet进行插入,使用overwrite-mode进行编辑。

如果你想要自动换行,你也可以杀死矩形 C-x r k , 切换到临时缓冲区 C-x b , yank矩形 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获得这两个包。

答案 3 :(得分:0)

对(1)的部分回答(但我希望听到更好的回答):

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

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

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

  • 确保set-fill-columnset-fill-prefix正确无误(见上文)
  • 删除所有右侧边框标记
  • 使用M-q
  • 重拨段落
  • 最后,手动添加顶部,底部和右侧边框。