Fix for complete issue #273.

- Add complete handler.
This commit is contained in:
Dave Davenport 2015-11-24 13:59:35 +01:00
parent bdeb57a2c4
commit fa2bcd778c
9 changed files with 55 additions and 3 deletions

View File

@ -34,6 +34,8 @@ typedef enum
typedef void ( *switcher_free )( Switcher *data );
typedef char * ( *get_display_value )( unsigned int selected_line, const Switcher *data, int *state, int get_entry );
typedef char * ( *get_completion )( const Switcher *sw, unsigned int selected_line );
/**
* State returned by the rofi window.
*/
@ -289,6 +291,8 @@ struct _Switcher
int ( *is_not_ascii )( const struct _Switcher *sw, unsigned int index );
get_completion get_completion;
// Pointer to private data.
void *private_data;

View File

@ -226,6 +226,28 @@ static int combi_is_not_ascii ( const Switcher *sw, unsigned int index )
}
return FALSE;
}
static char * combi_get_completion ( const Switcher *sw, unsigned int index )
{
CombiModePrivateData *pd = sw->private_data;
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
if ( index >= pd->starts[i] && index < ( pd->starts[i] + pd->lengths[i] ) ) {
char * str = NULL;
if ( pd->switchers[i]->get_completion != NULL ) {
str = pd->switchers[i]->get_completion ( pd->switchers[i], index - pd->starts[i] );
}
else {
int state;
str = pd->switchers[i]->mgrv ( index - pd->starts[i], (void *) pd->switchers[i], &state, TRUE );
}
char *retv = g_strdup_printf ( "!%c %s", pd->switchers[i]->name[0], str );
g_free ( str );
return retv;
}
}
// Should never get here.
g_error ( "Failure, could not resolve sub-switcher." );
return NULL;
}
Switcher combi_mode =
{
@ -238,6 +260,7 @@ Switcher combi_mode =
.result = combi_mode_result,
.destroy = combi_mode_destroy,
.token_match = combi_mode_match,
.get_completion = combi_get_completion,
.mgrv = combi_mgrv,
.is_not_ascii = combi_is_not_ascii,
.private_data = NULL,

View File

@ -303,6 +303,7 @@ Switcher dmenu_mode =
.destroy = dmenu_mode_free,
.token_match = dmenu_token_match,
.mgrv = get_display_data,
.get_completion = NULL,
.is_not_ascii = dmenu_is_not_ascii,
.private_data = NULL,
.free = NULL

View File

@ -113,7 +113,7 @@ static void exec_cmd_entry ( DRunModeEntry *e )
str[i++] = ' ';
}
// We might have hit '\0' in prev. loop, break out of for then.
if ( str[i] == '\0') {
if ( str[i] == '\0' ) {
break;
}
}
@ -167,7 +167,7 @@ static void get_apps_dir ( DRunModePrivateData *pd, const char *bp )
}
if ( g_key_file_has_key ( kf, "Desktop Entry", "Exec", NULL ) ) {
size_t nl = ( ( pd->cmd_list_length ) + 1 );
pd->entry_list = g_realloc ( pd->entry_list, nl * sizeof ( *( pd->entry_list ) ) );
pd->entry_list = g_realloc ( pd->entry_list, nl * sizeof ( *( pd->entry_list ) ) );
pd->entry_list[pd->cmd_list_length].terminal = FALSE;
if ( g_key_file_has_key ( kf, "Desktop Entry", "Name", NULL ) ) {
gchar *n = g_key_file_get_locale_string ( kf, "Desktop Entry", "Name", NULL, NULL );
@ -296,6 +296,18 @@ static char *mgrv ( unsigned int selected_line, const Switcher *sw, int *state,
dr->generic_name );
}
}
static char *drun_get_completion ( const Switcher *sw, unsigned int index )
{
DRunModePrivateData *pd = (DRunModePrivateData *) sw->private_data;
/* Free temp storage. */
DRunModeEntry *dr = &( pd->entry_list[index] );
if ( dr->generic_name == NULL ) {
return g_strdup ( dr->name );
}
else {
return g_strdup_printf ( "%s", dr->name );
}
}
static int drun_token_match ( const Switcher *data,
char **tokens,
@ -342,6 +354,7 @@ Switcher drun_mode =
.result = drun_mode_result,
.destroy = drun_mode_destroy,
.token_match = drun_token_match,
.get_completion = drun_get_completion,
.mgrv = mgrv,
.is_not_ascii = drun_is_not_ascii,
.private_data = NULL,

View File

@ -354,6 +354,7 @@ Switcher run_mode =
.destroy = run_mode_destroy,
.token_match = run_token_match,
.mgrv = mgrv,
.get_completion = NULL,
.is_not_ascii = run_is_not_ascii,
.private_data = NULL,
.free = NULL

View File

@ -199,6 +199,7 @@ Switcher *script_switcher_parse_setup ( const char *str )
sw->result = script_mode_result;
sw->destroy = script_mode_destroy;
sw->token_match = script_token_match;
sw->get_completion = NULL,
sw->mgrv = mgrv;
sw->is_not_ascii = script_is_not_ascii;

View File

@ -405,6 +405,7 @@ Switcher ssh_mode =
.destroy = ssh_mode_destroy,
.token_match = ssh_token_match,
.mgrv = mgrv,
.get_completion = NULL,
.is_not_ascii = ssh_is_not_ascii,
.private_data = NULL,
.free = NULL

View File

@ -594,6 +594,7 @@ Switcher window_mode =
.destroy = window_mode_destroy,
.token_match = window_match,
.mgrv = mgrv,
.get_completion = NULL,
.is_not_ascii = window_is_not_ascii,
.private_data = NULL,
.free = NULL
@ -610,6 +611,7 @@ Switcher window_mode_cd =
.destroy = window_mode_destroy,
.token_match = window_match,
.mgrv = mgrv,
.get_completion = NULL,
.is_not_ascii = window_is_not_ascii,
.private_data = NULL,
.free = NULL

View File

@ -644,7 +644,13 @@ static int menu_keyboard_navigation ( MenuState *state, KeySym key, unsigned int
// If a valid item is selected, return that..
if ( state->selected < state->filtered_lines ) {
int st;
char * str = state->sw->mgrv ( state->line_map[state->selected], state->sw, &st, TRUE );
char *str = NULL;
if ( state->sw->get_completion ) {
str = state->sw->get_completion ( state->sw, state->line_map[state->selected] );
}
else {
str = state->sw->mgrv ( state->line_map[state->selected], state->sw, &st, TRUE );
}
textbox_text ( state->text, str );
g_free ( str );
textbox_cursor_end ( state->text );