Made a comment Toggle command

Im continuing the work of porting over all my emacs custom config stuff over to 4coder:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
        }
    }
}


Anyway, still having fun, almost got everything how I want it. 4coder is awesome :)