Comment Boxes In Emacs

Here’s a neat little hack that can save a bit of time. Emacs has the comment-box command that will put a mode-specific comment box around a region. For example, suppose you have the Elisp

A function to do something very profound.
It is called from the top level.
(defun be-profound ()
...)

and that you select the first two lines and call comment-box. You get

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; A function to do something very profound. ;
; It is called from the top level.          ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun be-profound ()
...)

Similarly if you have some C code like

Here is the the main function
that does something or another

int main ( int argc, char **argv)

and select the first two lines and call comment-box you get

/**********************************/
/* Here is the the main function  */
/* that does something or another */
/**********************************/

int main ( int argc, char **argv)

Back when I was writing a lot of C code I used to put a comment box like that in front of every function. I did it by hand so it was sort of pain. I wish I’d had, or known about, something like comment-box to help me out (that was when I was using Vim).

This entry was posted in Programming and tagged . Bookmark the permalink.

3 Responses to Comment Boxes In Emacs

  1. Paul says:

    Have a look at the mother of all comment boxing

    https://github.com/lewang/rebox2

    Multiple styles, lots of features… Includes rounded boxes, my favorite:

    # ,——————–
    # | This is a comment.
    # `——————–

  2. Vinh Nguyen says:

    Thanks! Never knew this function existed, and I’m tired of doing similar things by hand.

    Now all we need is an “uncomment-box” function so we can modify the comment box and get the modified box to look pretty. Or we need a function that will revise the prettiness of a comment box after it has been modified.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>