Emacs makes comments in a square block with a right aligned border

I need to write code comments with aligned boundaries on all four sides due to the standard coding standard.

The easiest way in Emacs:

  • Convert text to block comment in this format
  • Edit the text in the block comment and return it to Emacs correctly.

Comment format examples:

#*****************************************************************************# #* 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 alone does not save the border of the right hand.

+4
source share
4 answers

The reliable and universal rebox2 "library can do this.

To install, download https://raw.github.com/lewang/rebox2/master/rebox2.el to the lisp directory and add the following to your .emacs file:

 (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 (Mq) a box repeatedly. Having "11" in this loop ; will allow you to easily "unbox" a comment block, eg 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))) 

Now:

  • To convert text to a block comment in this format, you simply select the text and do Mq
  • To edit the text in a block comment, you can simply edit the text directly, and emacs will automatically update the field. (You may need Mq request payment if emacs does not do this automatically).
+3
source

Mx customize-RET comment RET comments RET

Then select "box" from value-menue

+2
source

You can use yasnippet for insertion and overwrite-mode for editing.

If you need word wrap, you can also kill the rectangle Cx rk , switch to the temporary buffer Cx b , yank the rectangle Cx ry . Edit your heart content there. After that, kill the rectangle from the temporary buffer and paste it into your source.

Here is a fragment of the beginning / end of the block:

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

Here's a snippet of a block line:

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

Note that I am using the auto-yasnippet aya-tab-position package variable here. To use it in a fragment, you need to expand the fragment with aya-open-line . You can get both packages from MELPA.

+1
source

Partial answer to (1) (but I would like to hear the best):

  • Enter your comment text as plain text
  • Mx set-fill-column 76
  • Place the cursor at the beginning of the comment paragraph and manually enter "# *" (note area)
  • With the cursor in the first letter of the pair comment, run Mx set-fill-prefix
  • Now use Mq to pay a pair
  • This sorts the word wrap and puts the left border in place.
  • Finally, manually add the top, bottom, and right borders.

See http://www.gnu.org/software/emacs/manual/html_node/emacs/Fill-Prefix.html

The same approach can be used for some of (2):

  • Make sure set-fill-column and set-fill-prefix are correct (see above)
  • Delete all right hand border markers
  • Reflow the para with Mq
  • Finally, manually add the top, bottom, and right borders.
0
source

Source: https://habr.com/ru/post/1496821/


All Articles