Skip to content

Commit

Permalink
[Script] Fix keep-selection add keep-filter
Browse files Browse the repository at this point in the history
Fixes: #1962
  • Loading branch information
DaveDavenport committed May 12, 2024
1 parent 9580c4a commit dee97eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions doc/rofi-script.5.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ The following extra options exists:
- **keep-selection**: If set, the selection is not moved to the first entry,
but the current position is maintained. The filter is cleared.

- **keep-filter**: If set, the filter is not cleared.

- **new-selection**: If `keep-selection` is set, this allows you to override
the selected entry (absolute position).

Expand Down
18 changes: 14 additions & 4 deletions source/modes/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ typedef struct {
char delim;
/** no custom */
gboolean no_custom;
/** keep filter */
gboolean keep_filter;

gboolean use_hot_keys;
} ScriptModePrivateData;
Expand Down Expand Up @@ -157,6 +159,8 @@ static void parse_header_entry(Mode *sw, char *line, ssize_t length) {
pd->use_hot_keys = (strcasecmp(value, "true") == 0);
} else if (strcasecmp(line, "keep-selection") == 0) {
pd->keep_selection = (strcasecmp(value, "true") == 0);
} else if (strcasecmp(line, "keep-filter") == 0) {
pd->keep_filter = (strcasecmp(value, "true") == 0);
} else if (strcasecmp(line, "new-selection") == 0) {
pd->new_selection = (int64_t)g_ascii_strtoll(value, NULL, 0);
} else if (strcasecmp(line, "data") == 0) {
Expand Down Expand Up @@ -184,6 +188,7 @@ static DmenuScriptEntry *execute_executor(Mode *sw, char *arg,
// Reset these between runs.
pd->new_selection = -1;
pd->keep_selection = 0;
pd->keep_filter = 0;
// Environment
char **env = g_get_environ();

Expand Down Expand Up @@ -320,6 +325,9 @@ static ModeMode script_mode_result(Mode *sw, int mretv, char **input,
ModeMode retv = MODE_EXIT;
DmenuScriptEntry *new_list = NULL;
unsigned int new_length = 0;
// store them as they might be different on next executor and reset.
gboolean keep_filter = rmpd->keep_filter;
gboolean keep_selection = rmpd->keep_selection;

if ((mretv & MENU_CUSTOM_COMMAND)) {
if (rmpd->use_hot_keys) {
Expand Down Expand Up @@ -370,20 +378,22 @@ static ModeMode script_mode_result(Mode *sw, int mretv, char **input,

rmpd->cmd_list = new_list;
rmpd->cmd_list_length = new_length;
if (rmpd->keep_selection) {
if (keep_selection) {
if (rmpd->new_selection >= 0 &&
rmpd->new_selection < rmpd->cmd_list_length) {
rofi_view_set_selected_line(rofi_view_get_active(),
rmpd->new_selection);
} else {
rofi_view_set_selected_line(rofi_view_get_active(), selected_line);
}
} else {
rofi_view_set_selected_line(rofi_view_get_active(), 0);
}
if (keep_filter == FALSE) {
g_free(*input);
*input = NULL;
retv = RELOAD_DIALOG;
} else {
retv = RESET_DIALOG;
}
retv = RELOAD_DIALOG;
}
return retv;
}
Expand Down

0 comments on commit dee97eb

Please sign in to comment.