CUSTOM_COMMAND_SIG(cro_comment_toggle){
View_Summary view = get_active_view(app, AccessOpen);
Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessOpen);
if(cro_selection_active) {
int min_line, max_line;
if (view.cursor.line > view.mark.line) {
min_line = view.mark.line;
max_line = view.cursor.line;
if (view.cursor.character == 1) max_line--;
} else {
max_line = view.mark.line;
min_line = view.cursor.line;
if (view.mark.character == 1) max_line--;
}
bool is_commenting = false;
for(int line_number=min_line; line_number<=max_line; line_number++) {
int pos = buffer_get_line_start(app, &buffer, line_number);
char line_start[2];
if (buffer_read_range(app, &buffer, pos, pos+2, line_start)) {
if (!(line_start[0]=='/' && line_start[1]=='/')) {is_commenting=true; break; }
}
}
for(int line_number=min_line; line_number<=max_line; line_number++) {
int pos = buffer_get_line_start(app, &buffer, line_number);
if (is_commenting) {
buffer_replace_range(app, &buffer, pos, pos, "//", 2);
} else {
buffer_replace_range(app, &buffer, pos, pos+2, 0, 0);
}
}
if (view.cursor.line > view.mark.line) {
int min_pos = buffer_get_line_start(app, &buffer, min_line);
int max_pos = buffer_get_line_end (app, &buffer, max_line);
view_set_cursor(app, &view, seek_pos(min_pos), true);
view_set_mark (app, &view, seek_pos(max_pos));
view_set_highlight(app, &view, min_pos, max_pos, true);
} else {
int min_pos = buffer_get_line_start(app, &buffer, min_line);
int max_pos = buffer_get_line_end (app, &buffer, max_line);
view_set_mark (app, &view, seek_pos(max_pos));
view_set_cursor(app, &view, seek_pos(min_pos), true);
view_set_highlight(app, &view, min_pos, max_pos, true);
}
} else {
auto pos = seek_line_beginning(app, &buffer, view.cursor.pos);
char line_start[2];
buffer_read_range(app, &buffer, pos, pos+2, line_start);
if (line_start[0]=='/' && line_start[1]=='/') {
buffer_replace_range(app, &buffer, pos, pos+2, 0, 0);
} else {
buffer_replace_range(app, &buffer, pos, pos, "//", 2);
}
}
}