Author Topic: Reformat C comments for new indentation level  (Read 2467 times)


  • Senior Community Member
  • Posts: 296
  • Hero Points: 13
Reformat C comments for new indentation level
« on: October 21, 2011, 02:12:52 pm »
#include ""

typeless _width;

//Some of this macro code was originally written with the help of a slickedit
//employee back in the days of visual slick edit for windows (i.e. versions 1-3
//or so). I don't recall who it was. This macro was written before slickedit
//reformatted comments as I typed them and I still use it when I re-indent code.

//The purpose of this macro is to reflow a comment at a new indentation level or
//to just reformat the comment per the document width of 80 using a tab indent
//setting of 4.

//To use this macro you select the comment using line mode making sure your
//cursor is at the new indentation location. Then execute the macro. The comment
//will be reformatted to the new width with the C/C++ "/*" " *" and "*/" to
//open/close the comment.

//Some improvements would be to obtain the document width and tab indent
//settings programatically. In addition, the comment style could be obtained and
//used (this is fixed for the old C style comment format).

_command reflow_c_comment() name_info(','EDITORCTL_ARG2|MARK_ARG2|MACRO_ARG2)
   if (!select_active()) {
      message('There needs to be an active selection');
      return (0);

   if (lowcase(_select_type('')):!='line') {


   //Determine the column of the opening of the comment.  This will be
   //used to indent the new comment.


   //Need to replace / * and * / before we replace the * otherwise
   //replacing the * will get rid of the / * and * / and cause problems
   replace('/\*','','PNIrm*');     //Get rid of '/*' in the line
   replace('\*/','','PNIrm*');     //Get rid of '*/' in the line
   replace('\*','','PNIrm*');      //Get rid of '*' in the line
   parse old_p_margins with left_ma right_ma para;
//set the margins to reformat the comment with the text not going past column
//80 and leaving room for my tab indent of 4. We will reformat the text in
//columns 1..n and then will re-indent the text later.
   p_margins=1' '81-new_left_ma+1-4' 'para;


   //Create the spacesstring which will be used to indent the newly formatted
   stringcounter = 1;
   while (stringcounter < new_left_ma)
   spacesstring = spacesstring' ';

   filter_put_string(spacesstring'/* 'text);
   while (p_line+1<line) {
      filter_put_string(spacesstring' * 'text);
   insert_line(spacesstring' */');


  • Guest
Re: Reformat C comments for new indentation level
« Reply #1 on: October 24, 2011, 02:04:48 pm »
Thank you for your submission. It is in the running for the iPad!

The winner will be announced via our Facebook page, Twitter page, and on this forum under the main SlickEdit October Macros Challenge Rules & How to Enter post,7327.0.html on November 7, 2011.


  • Guest
Re: Reformat C comments for new indentation level
« Reply #2 on: November 01, 2011, 03:14:31 pm »
We'll be announcing the winner of the iPad 2 on November 7th. Until then, your submission has earned a SlickEdit t-shirt! Email your size and address to to receive one.