+u32 str_lev_distance( const char *s1, const char *s2 )
+{
+ u32 m = strlen( s1 ),
+ n = strlen( s2 );
+
+ if( m==0 ) return n;
+ if( n==0 ) return m;
+
+ assert( n+1 <= 256 );
+
+ u32 costs[ 256 ];
+
+ for( u32 k=0; k<=n; k++ )
+ costs[k] = k;
+
+ u32 i = 0;
+ for( u32 i=0; i<m; i++ )
+ {
+ costs[0] = i+1;
+
+ u32 corner = i;
+
+ for( u32 j=0; j<n; j++ )
+ {
+ u32 upper = costs[j+1];
+
+ if( s1[i] == s2[j] )
+ costs[ j+1 ] = corner;
+ else
+ {
+ u32 t = (upper < corner)? upper: corner;
+ costs[j+1] = ((costs[j] < t)? costs[j]: t) + 1;
+ }
+
+ corner = upper;
+ }
+ }
+
+ return costs[n];
+}
+
+u32 str_lcs( const char *s1, const char *s2 )
+{
+ u32 m = VG_MIN( 31, strlen( s1 ) ),
+ n = VG_MIN( 31, strlen( s2 ) );
+
+ int suff[32][32],
+ result = 0;
+
+ for( int i=0; i<=m; i++ )
+ {
+ for( int j=0; j<=n; j++ )
+ {
+ if( i == 0 || j == 0 )
+ suff[i][j] = 0;
+ else if( s1[i-1] == s2[j-1] )
+ {
+ suff[i][j] = suff[i-1][j-1] + 1;
+ result = VG_MAX( result, suff[i][j] );
+ }
+ else
+ suff[i][j] = 0;
+ }
+ }
+
+ return result;
+}
+
+/* str must not fuckoff ever! */
+VG_STATIC void console_suggest_score_text( const char *input, const char *str )
+{
+ /* calc score */
+ u32 score = str_lcs( str, input );
+
+ if( score < 1 )
+ return;
+
+ int best_pos = vg_console.suggestion_count;
+ for( int j=best_pos-1; j>=0; j -- )
+ if( score > vg_console.suggestions[j].lev_score )
+ best_pos = j;
+
+ /* insert if good score */
+ if( best_pos < vg_list_size( vg_console.suggestions ) )
+ {
+ int start = VG_MIN( vg_console.suggestion_count,
+ vg_list_size( vg_console.suggestions )-1 );
+ for( int j=start; j>best_pos; j -- )
+ vg_console.suggestions[j] = vg_console.suggestions[j-1];
+
+ vg_console.suggestions[ best_pos ].str = str;
+ vg_console.suggestions[ best_pos ].len = strlen( str );
+ vg_console.suggestions[ best_pos ].lev_score = score;
+
+ if( vg_console.suggestion_count <
+ vg_list_size( vg_console.suggestions ) )
+ vg_console.suggestion_count ++;
+ }
+}
+
+VG_STATIC void console_update_suggestions(void)
+{
+ vg_console.suggestion_count = 0;
+ vg_console.suggestion_select = -1;
+ vg_console.suggestion_maxlen = 0;
+
+ /* find current term */
+ int start_index = 0;
+
+ for( int i=0; 1; i++ )
+ {
+ if( !vg_console.input[i] )
+ break;
+
+ if( isspace( vg_console.input[i] ) )
+ {
+ start_index = i;
+
+ /* TODO: not just functions
+ */
+
+ return;
+ }
+ }
+
+ vg_console.suggestion_pastepos = start_index;
+ const char *input_ref = &vg_console.input[ start_index ];
+
+ /* Score all our commands and cvars */
+ for( int i=0; i<vg_console.convar_count; i++ )
+ {
+ vg_convar *cvar = &vg_console.convars[i];
+ console_suggest_score_text( input_ref, cvar->name );
+ }
+
+ for( int i=0; i<vg_console.function_count; i++ )
+ {
+ vg_cmd *cmd = &vg_console.functions[i];
+ console_suggest_score_text( input_ref, cmd->name );
+ }
+
+ /* some post processing */
+ for( int i=0; i<vg_console.suggestion_count; i++ )
+ {
+ vg_console.suggestion_maxlen = VG_MAX( vg_console.suggestion_maxlen,
+ vg_console.suggestions[i].len );
+
+ if( vg_console.suggestions[i].lev_score <
+ vg_console.suggestions[0].lev_score/2 )
+ {
+ vg_console.suggestion_count = i;
+ return;
+ }
+ }
+}
+