4coder»Forums
Simon Anciaux
1337 posts
API documentation as a text file
Edited by Simon Anciaux on Reason: Updated code
I made a parser to convert 4coder html documentation to a simple text file that you can open and search directly in 4coder. Here is the result if anyone is interested.
4coder_api.txt
And two custom functions, one that opens the file, and one that search for the word under the cursor.
 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// You need to set this to point to the location of 4coder_api.txt.
#define DOCUMENTATION_TXT_FILE "y:/4coder_txt_doc/4coder_api.txt"

CUSTOM_COMMAND_SIG( documentation_open ) {
    
    change_active_panel( app );
    View_Summary view = app->get_active_view( app, AccessAll );
    view_open_file( app, &view, literal( DOCUMENTATION_TXT_FILE ), false );
    refresh_view( app, &view );
    Buffer_Summary buffer = app->get_buffer( app, view.buffer_id, AccessAll );
    app->buffer_set_setting( app, &buffer, BufferSetting_ReadOnly, true );
}

CUSTOM_COMMAND_SIG( documentation_seek_word_under_cursor ) {
    
    Buffer_Summary buffer = app->get_buffer_by_name( app, literal( DOCUMENTATION_TXT_FILE ), AccessAll );
    
    if ( !buffer.exists ) {
        buffer = app->create_buffer( app, literal( DOCUMENTATION_TXT_FILE ), false );
    }
    
    if ( buffer.exists ) {
        
        app->buffer_set_setting( app, &buffer, BufferSetting_ReadOnly, true );
        
        View_Summary view = app->get_active_view( app, AccessAll );
        Buffer_Summary editBuffer = app->get_buffer( app, view.buffer_id, AccessAll );
        int startPosition = view.cursor.pos;
        int linePosition = view.cursor.character;
        
        int min, max;
        seek_beginning_of_line( app );
        refresh_view( app, &view );
        min = view.cursor.pos;
        
        seek_end_of_line( app );
        refresh_view( app, &view );
        max = view.cursor.pos;
        
        app->view_set_cursor( app, &view, seek_pos( startPosition ), true );
        
        char* str = ( ( char* ) app->memory ) + 4;
        
        int length = max - min;
        app->buffer_read_range( app, &editBuffer, min, max, str );
        
        int start = linePosition - 1;
        
        while ( start >= 0 && char_is_alpha_numeric( str[ start ] ) ) {
            start--;
        }
        
        start++;
        
        int end = linePosition - 1;
        
        while ( end < length && char_is_alpha_numeric( str[ end ] ) ) {
            end++;
        }
        
        str[ end ] = ' ';
        end++;
        
        if ( end - start >= 2 ) {
            
            char* doc = str + start - 4;
            doc[ 0 ] = 'd';
            doc[ 1 ] = 'o';
            doc[ 2 ] = 'c';
            doc[ 3 ] = '_';
            
            int result = 0;
            buffer_seek_string_insensitive_forward( app, &buffer, 0, buffer.size, doc, 4 + end - start, &result );
            
            if ( result != 0 && result != buffer.size ) {
                
                change_active_panel( app );
                view = app->get_active_view( app, AccessAll );
                app->view_set_buffer( app, &view, buffer.buffer_id, 0 );
                app->view_set_cursor( app, &view, seek_pos( result ), true );
                center_view( app );
                change_active_panel( app );
            }
        }
    }
}

A question for Allen, how can I open a file as read only ?

Also I noticed some errors in the html file:
- In the "4coder systems" section (id='section_4coder_systems') the "div" enclosing "Coming Soon" is not closed, it's missing the '/';
- Same problem for the "String intro" section (id='section_string_library'), the "div" enclosing "Coming Soon" is not closed, it's missing the '/';
- Right after the "4.3 String Function Descriptions" title there is a "ul" tag alone;
- 4.3.31: A "len" parameter is mentioned when the prototype doesn't contain one;
- In the string library part, "See Also" links don't work, they are missing the additional _str before _doc;

Allen Webster
476 posts / 6 projects
Heyo
API documentation as a text file
Awesome thanks!

You can make a read only buffer by buffer_set_setting and using the BufferSetting_ReadOnly setting.
Simon Anciaux
1337 posts
API documentation as a text file
Thanks, I updated the original post to open the file as read only.