0

This is my simple C code block:

int main() { // the enty function
        in x = 100; // my new variable
        for(int i=1; i<2012; ++i) { // loop a lot
                x -= i;
        }
} /* end of all things */

I want to indent this block into:

int main() {                        // the enty function
        int x = 100;                // my new variable
        for(int i=1; i<2012; ++i) { // loop a lot
                x -= i;
        }
}                                   /* end of all things */

But after I select this region and M-x align, it becomes:

int main() {                    // the enty function
        in      x  = 100;       // my new variable
        for(int i=1; i<2012; ++i) {     // loop a lot
                x -= i;
        }
}                               /* end of all things */

If align cannot handle all comments to the same column, that's fine, but I don't understand why this line of the code:

            in x = 100; // my new variable

becomes:

            in      x  = 100;       // my new variable

See, it adds multiple spaces around x. Why?

FYI: I did this in Emacs both with my .emacs.d and without my .emacs.d(emacs -q)

2

align is aligning those // comments to the value of comment-column in that buffer, which will be column 32 by default. Your for statement extends beyond that column, so it is out of alignment.

See C-h f comment-set-column. Use that, and then M-x align again.

See, it adds multiple spaces around x. Why?

Because it has aligned the = characters in the two assignments.

Note that alignment is flexible and complex (and sometimes may seem overzealous, as with your assignment statements). Different modes can have different rules, using different values, so the same rules won't apply in all buffers. See C-h v align-rules-list for more (I believe the rules you're asking about here are c++-comment and c-assignment respectively).

  • Thanks, I noticed comment-column problem years ago, I though it would be dynamically, like 4->8->12->16, or something like this. I'll check comment-set-column and align-rules-list for more info. – CodyChan Jul 11 '18 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.