Trying to figure out how to implement command (keep getting crash)

I'm still very new to 4coder and I'm currently trying to learn and implement some functionality. I have some basic modal functionality, with the ability to switch between a command mode and insert mode. Recently, I have been trying to implement the ability, when in command mode, to move the cursor to the first non-space, non-tab character on a line and then switch back over to my insert mode to start typing. I try and do this by first seeking to beginning of the line and then reading the buffer from the current cursor position to see where to finally place the cursor. Here is my current code:

 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
CUSTOM_COMMAND_SIG( jason_enter_normal_mode )
{
    
    jason_set_current_keymap( app, mapid_command);
    
    Theme_Color colors[] = {
        { Stag_Cursor, 0xffff5533 },
        { Stag_At_Cursor, 0xff00aacc },
        { Stag_Margin_Active, 0xffff5533 },
    };

    set_theme_colors( app, colors, ArrayCount( colors ) );
};


void SwitchModeTo(struct Application_Links* app, Custom_Command_Function* SwitchModeTo)
{
    SwitchModeTo(app);
};

CUSTOM_COMMAND_SIG(jason_seek_beginning_line_and_switch_to_insert_mode)
{
    exec_command(app, seek_beginning_of_line);
    View_Summary view = get_active_view(app, AccessAll);
    Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessProtected);
    char *ParsingRegion = (char *)malloc(sizeof(buffer.size));
    buffer_read_range(app, &buffer, 0, buffer.size, ParsingRegion);

    for(int charIndex{view.cursor.pos}; charIndex < buffer.size; ++charIndex)
    {
        if(ParsingRegion[charIndex] == '\t' || ParsingRegion[charIndex] == ' ')
        {
            //Do nothing
        }
        else
        {
            Buffer_Seek bufferSeek{};
            bufferSeek.type = buffer_seek_pos;
            bufferSeek.pos = charIndex;
            view_set_cursor(app, &view, bufferSeek, true);
            break;
        }
    };

	free(ParsingRegion);
	ParsingRegion = nullptr;

    SwitchModeTo(app, jason_enter_insert_mode);
};


This code does 'work' in a sense as my cursor will move to correct spot and I can sometimes even type a couple letters but eventually the program will just crash. Anyone able to see where my issue is? Am I misusing the API?

Edited by Jason on Reason: Initial post
You should try attaching a debugger to 4coder and see where it crashes. In your custom layer or in 4coder core ?

1
char *ParsingRegion = (char *)malloc(sizeof(buffer.size));

Shouldn't that be
1
char *ParsingRegion = (char *)malloc(buffer.size);


You're creating a copy of the whole buffer just to seek the beginning of the line. Can't you just allocate some space that would cover any indentation you have, like 64 bytes or so ?

1
2
3
4
5
6
7
char ParsingRegion[64];
uint32_t start = view.cursor.pos;
uint32_t end = start + 64;
if ( end > buffer.size ){
    end = buffer.size;
}
buffer_read_range(app, &buffer, start, end, ParsingRegion);
Oh wow, even after looking over it a few times I missed that part. I'm so used to using my own malloc that I wasn't even thinking when i used sizeof. Well that seemed to fix it. Thanks Simon.

Edited by Jason on