From 96cd34d24f7116c0fd37bf8571ee3ae79f9ad227 Mon Sep 17 00:00:00 2001 From: nick87720z Date: Tue, 8 Sep 2020 20:30:03 +0500 Subject: [PATCH] Fix and optimize 20200908 (#1184) * combi: Reuse own get_num_entries in self-init * combi_mode_parse_switchers: Remove duplicated mode pointer * combi_mode_parse_switchers: Regroup mode search lines * combi_mode_result: Simplify switcher search quit * mode_result: Check for mode switch actions before hook These are over-mode actions, which should not be handled in mode hooks at all. --- source/dialogs/combi.c | 62 ++++++++++++++++---------------------- source/dialogs/drun.c | 11 +------ source/dialogs/help-keys.c | 13 +------- source/dialogs/run.c | 11 +------ source/dialogs/script.c | 23 +------------- source/dialogs/ssh.c | 11 +------ source/dialogs/window.c | 11 +------ source/mode.c | 11 +++++++ 8 files changed, 43 insertions(+), 110 deletions(-) diff --git a/source/dialogs/combi.c b/source/dialogs/combi.c index eb39fa26..86d92ba5 100644 --- a/source/dialogs/combi.c +++ b/source/dialogs/combi.c @@ -76,27 +76,37 @@ static void combi_mode_parse_switchers ( Mode *sw ) sizeof ( CombiMode ) * ( pd->num_switchers + 1 ) ); Mode *mode = rofi_collect_modi_search ( token ); - if ( mode ) { + if ( mode != NULL ) { pd->switchers[pd->num_switchers].disable = FALSE; pd->switchers[pd->num_switchers++].mode = mode; + continue; } - else { - // If not build in, use custom switchers. - Mode *sw = script_switcher_parse_setup ( token ); - if ( sw != NULL ) { - pd->switchers[pd->num_switchers].disable = FALSE; - pd->switchers[pd->num_switchers++].mode = sw; - } - else { - // Report error, don't continue. - g_warning ( "Invalid script switcher: %s", token ); - token = NULL; - } + // If not build in, use custom switchers. + mode = script_switcher_parse_setup ( token ); + if ( mode != NULL ) { + pd->switchers[pd->num_switchers].disable = FALSE; + pd->switchers[pd->num_switchers++].mode = mode; + continue; } + // Report error, don't continue. + g_warning ( "Invalid script switcher: %s", token ); + token = NULL; } // Free string that was modified by strtok_r g_free ( switcher_str ); } +static unsigned int combi_mode_get_num_entries ( const Mode *sw ) +{ + const CombiModePrivateData *pd = (const CombiModePrivateData *) mode_get_private_data ( sw ); + unsigned int length = 0; + for ( unsigned int i = 0; i < pd->num_switchers; i++ ) { + unsigned int entries = mode_get_num_entries ( pd->switchers[i].mode ); + pd->starts[i] = length; + pd->lengths[i] = entries; + length += entries; + } + return length; +} static int combi_mode_init ( Mode *sw ) { @@ -112,29 +122,11 @@ static int combi_mode_init ( Mode *sw ) } } if ( pd->cmd_list_length == 0 ) { - pd->cmd_list_length = 0; - for ( unsigned int i = 0; i < pd->num_switchers; i++ ) { - unsigned int length = mode_get_num_entries ( pd->switchers[i].mode ); - pd->starts[i] = pd->cmd_list_length; - pd->lengths[i] = length; - pd->cmd_list_length += length; - } + pd->cmd_list_length = combi_mode_get_num_entries ( sw ); } } return TRUE; } -static unsigned int combi_mode_get_num_entries ( const Mode *sw ) -{ - const CombiModePrivateData *pd = (const CombiModePrivateData *) mode_get_private_data ( sw ); - unsigned int length = 0; - for ( unsigned int i = 0; i < pd->num_switchers; i++ ) { - unsigned int entries = mode_get_num_entries ( pd->switchers[i].mode ); - pd->starts[i] = length; - pd->lengths[i] = entries; - length += entries; - } - return length; -} static void combi_mode_destroy ( Mode *sw ) { CombiModePrivateData *pd = (CombiModePrivateData *) mode_get_private_data ( sw ); @@ -163,11 +155,12 @@ static ModeMode combi_mode_result ( Mode *sw, int mretv, char **input, unsigned } ssize_t bang_len = g_utf8_pointer_to_offset ( input[0], eob ) - 1; if ( bang_len > 0 ) { - for ( unsigned i = 0; switcher == -1 && i < pd->num_switchers; i++ ) { + for ( unsigned i = 0; i < pd->num_switchers; i++ ) { const char *mode_name = mode_get_name ( pd->switchers[i].mode ); size_t mode_name_len = g_utf8_strlen ( mode_name, -1 ); if ( (size_t) bang_len <= mode_name_len && utf8_strncmp ( &input[0][1], mode_name, bang_len ) == 0 ) { switcher = i; + break; } } } @@ -180,9 +173,6 @@ static ModeMode combi_mode_result ( Mode *sw, int mretv, char **input, unsigned return MODE_EXIT; } } - if ( mretv & MENU_QUICK_SWITCH ) { - return mretv & MENU_LOWER_MASK; - } for ( unsigned i = 0; i < pd->num_switchers; i++ ) { if ( selected_line >= pd->starts[i] && diff --git a/source/dialogs/drun.c b/source/dialogs/drun.c index 96bf3c06..cb7bd2c2 100644 --- a/source/dialogs/drun.c +++ b/source/dialogs/drun.c @@ -1012,16 +1012,7 @@ static ModeMode drun_mode_result ( Mode *sw, int mretv, char **input, unsigned i DRunModePrivateData *rmpd = (DRunModePrivateData *) mode_get_private_data ( sw ); ModeMode retv = MODE_EXIT; - if ( mretv & MENU_NEXT ) { - retv = NEXT_DIALOG; - } - else if ( mretv & MENU_PREVIOUS ) { - retv = PREVIOUS_DIALOG; - } - else if ( mretv & MENU_QUICK_SWITCH ) { - retv = ( mretv & MENU_LOWER_MASK ); - } - else if ( ( mretv & MENU_OK ) ) { + if ( ( mretv & MENU_OK ) ) { switch ( rmpd->entry_list[selected_line].type ) { case DRUN_DESKTOP_ENTRY_TYPE_APPLICATION: diff --git a/source/dialogs/help-keys.c b/source/dialogs/help-keys.c index 0439a54c..e5cedb14 100644 --- a/source/dialogs/help-keys.c +++ b/source/dialogs/help-keys.c @@ -71,18 +71,7 @@ static ModeMode help_keys_mode_result ( G_GNUC_UNUSED Mode *sw, G_GNUC_UNUSED char **input, G_GNUC_UNUSED unsigned int selected_line ) { - ModeMode retv = MODE_EXIT; - - if ( mretv & MENU_NEXT ) { - retv = NEXT_DIALOG; - } - else if ( mretv & MENU_PREVIOUS ) { - retv = PREVIOUS_DIALOG; - } - else if ( mretv & MENU_QUICK_SWITCH ) { - retv = ( mretv & MENU_LOWER_MASK ); - } - return retv; + return MODE_EXIT; } static void help_keys_mode_destroy ( Mode *sw ) { diff --git a/source/dialogs/run.c b/source/dialogs/run.c index 30caa8c2..0d223052 100644 --- a/source/dialogs/run.c +++ b/source/dialogs/run.c @@ -370,16 +370,7 @@ static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned in gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION ); - if ( mretv & MENU_NEXT ) { - retv = NEXT_DIALOG; - } - else if ( mretv & MENU_PREVIOUS ) { - retv = PREVIOUS_DIALOG; - } - else if ( mretv & MENU_QUICK_SWITCH ) { - retv = ( mretv & MENU_LOWER_MASK ); - } - else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) { + if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) { exec_cmd ( rmpd->cmd_list[selected_line], run_in_term ); } else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) { diff --git a/source/dialogs/script.c b/source/dialogs/script.c index 45ca3cec..3f02e170 100644 --- a/source/dialogs/script.c +++ b/source/dialogs/script.c @@ -278,28 +278,7 @@ static ModeMode script_mode_result ( Mode *sw, int mretv, char **input, unsigned DmenuScriptEntry *new_list = NULL; unsigned int new_length = 0; - if ( ( mretv & MENU_NEXT ) ) { - retv = NEXT_DIALOG; - } - else if ( ( mretv & MENU_PREVIOUS ) ) { - retv = PREVIOUS_DIALOG; - } - else if ( ( mretv & MENU_QUICK_SWITCH ) ) { - //retv = 1+( mretv & MENU_LOWER_MASK ); - script_mode_reset_highlight ( sw ); - if ( selected_line != UINT32_MAX ) { - new_list = execute_executor ( sw, rmpd->cmd_list[selected_line].entry, &new_length, 10 + ( mretv & MENU_LOWER_MASK ), &( rmpd->cmd_list[selected_line] ) ); - } - else { - if ( rmpd->no_custom == FALSE ) { - new_list = execute_executor ( sw, *input, &new_length, 10 + ( mretv & MENU_LOWER_MASK ), NULL ); - } - else { - return RELOAD_DIALOG; - } - } - } - else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line].entry != NULL ) { + if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line].entry != NULL ) { if ( rmpd->cmd_list[selected_line].nonselectable ) { return RELOAD_DIALOG; } diff --git a/source/dialogs/ssh.c b/source/dialogs/ssh.c index 20ae008a..fd590551 100644 --- a/source/dialogs/ssh.c +++ b/source/dialogs/ssh.c @@ -593,16 +593,7 @@ static ModeMode ssh_mode_result ( Mode *sw, int mretv, char **input, unsigned in { ModeMode retv = MODE_EXIT; SSHModePrivateData *rmpd = (SSHModePrivateData *) mode_get_private_data ( sw ); - if ( mretv & MENU_NEXT ) { - retv = NEXT_DIALOG; - } - else if ( mretv & MENU_PREVIOUS ) { - retv = PREVIOUS_DIALOG; - } - else if ( mretv & MENU_QUICK_SWITCH ) { - retv = ( mretv & MENU_LOWER_MASK ); - } - else if ( ( mretv & MENU_OK ) && rmpd->hosts_list[selected_line].hostname != NULL ) { + if ( ( mretv & MENU_OK ) && rmpd->hosts_list[selected_line].hostname != NULL ) { exec_ssh ( &( rmpd->hosts_list[selected_line] ) ); } else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) { diff --git a/source/dialogs/window.c b/source/dialogs/window.c index 59cdb01f..1060068e 100644 --- a/source/dialogs/window.c +++ b/source/dialogs/window.c @@ -650,16 +650,7 @@ static ModeMode window_mode_result ( Mode *sw, int mretv, G_GNUC_UNUSED char **i { ModeModePrivateData *rmpd = (ModeModePrivateData *) mode_get_private_data ( sw ); ModeMode retv = MODE_EXIT; - if ( mretv & MENU_NEXT ) { - retv = NEXT_DIALOG; - } - else if ( mretv & MENU_PREVIOUS ) { - retv = PREVIOUS_DIALOG; - } - else if ( ( mretv & MENU_QUICK_SWITCH ) == MENU_QUICK_SWITCH ) { - retv = ( mretv & MENU_LOWER_MASK ); - } - else if ( ( mretv & ( MENU_OK ) ) ) { + if ( ( mretv & ( MENU_OK ) ) ) { if ( mretv & MENU_CUSTOM_ACTION ) { act_on_window ( rmpd->ids->array[selected_line] ); } diff --git a/source/mode.c b/source/mode.c index 987f2823..6b7b2f48 100644 --- a/source/mode.c +++ b/source/mode.c @@ -96,9 +96,20 @@ char * mode_get_completion ( const Mode *mode, unsigned int selected_line ) ModeMode mode_result ( Mode *mode, int menu_retv, char **input, unsigned int selected_line ) { + if ( menu_retv & MENU_NEXT ) { + return NEXT_DIALOG; + } + else if ( menu_retv & MENU_PREVIOUS ) { + return PREVIOUS_DIALOG; + } + else if ( menu_retv & MENU_QUICK_SWITCH ) { + return ( menu_retv & MENU_LOWER_MASK ); + } + g_assert ( mode != NULL ); g_assert ( mode->_result != NULL ); g_assert ( input != NULL ); + return mode->_result ( mode, menu_retv, input, selected_line ); }