You could try this. It basically works but I've only tested it very briefly so you should check the logic and see if it covers everything and test it well. One thing it doesn't cope with is a comment on the same line as the { or }
Probably if you want to allow trailing comments you could replace the line with the original line with the appropriate number of characters removed from the start using substr() but you also have to change the code that looks for {} on a line by themselves (maybe use search() with XC to look for non blanks that aren't in comments - or search for {} preceded only by whitespace)
say() is a useful debugging function. Another thing you can do is record a macro and inspect the code that gets generated - but note that if you do a cursor_right, you'll see a call to cursor_right() which is not the same as calling the core right() function as I do below. Also, I'd recommend having backup history enabled so you can recover from any disasters and save the file before beautifying.
_command void move_paren() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
int col1, col2, line1, line2;
int max = 0;
typeless pos1, pos2;
_str line;
_save_pos2(pos1);
top();
for (;;) {
if (++max > 10) // increase to 100000 or so some time
break; // don't loop forever!
right();
if (search('{','@xcs') != 0)
break;
_save_pos2(pos2);
get_line(line);
if (strip(line) != '{') {
// there are other non whitespace characters on this line
continue;
}
col1 = p_col;
line1 = p_line;
if (up() != 0)
continue; // this is the first line
_begin_line(); // go to col 1
//say(p_col);
// search for first non whitespace
if (search('[~ \t]','r@') != 0)
break;
//say('k' :+ p_col);
if (p_line >= line1) {
_restore_pos2(pos2);
continue;
}
col2 = p_col + 2; // the column we want { to be on
if (col2 >= col1) {
_restore_pos2(pos2);
continue;
}
if (down() != 0)
break;
// double check this line has only {
get_line(line);
if (strip(line) != '{') {
// there are other non whitespace characters on this line
continue;
}
// check what happens if col2 is 1
replace_line(field('',col2-1) :+ '{');
p_line = line1; // make sure we're on the correct line
_begin_line(); // go to col 1
// find the { again
if (search('{','@xcs') != 0)
break;
_save_pos2(pos2);
if (find_matching_paren() != 0)
break;
get_line(line);
if (strip(line) != '}') {
// there are other non whitespace characters on this line
continue;
}
// check what happens if col2 is 1
replace_line(field('',col2-1) :+ '}');
_restore_pos2(pos2); // go back to {
// and do it again
}
_restore_pos2(pos1);
}
Graeme