mirror of
https://github.com/davatorium/rofi.git
synced 2024-11-18 13:54:36 -05:00
Keep the line matched (if possible) when filtering.
This commit is contained in:
parent
13203c9de7
commit
d2aad68bce
3 changed files with 59 additions and 50 deletions
|
@ -95,7 +95,7 @@ typedef int ( *menu_match_cb )( char **tokens, const char *input, int case_sensi
|
||||||
* @returns The command issued (see MenuReturn)
|
* @returns The command issued (see MenuReturn)
|
||||||
*/
|
*/
|
||||||
MenuReturn menu ( Switcher *sw, char **input, char *prompt,
|
MenuReturn menu ( Switcher *sw, char **input, char *prompt,
|
||||||
int *selected_line,
|
unsigned int *selected_line,
|
||||||
unsigned int *next_pos, const char *message ) __attribute__ ( ( nonnull ( 1, 2, 3, 4 ) ) );
|
unsigned int *next_pos, const char *message ) __attribute__ ( ( nonnull ( 1, 2, 3, 4 ) ) );
|
||||||
/**
|
/**
|
||||||
* @param sig The caught signal
|
* @param sig The caught signal
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include "rofi.h"
|
#include "rofi.h"
|
||||||
#include "dialogs/dmenu.h"
|
#include "dialogs/dmenu.h"
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
@ -44,7 +45,7 @@ struct range_pair
|
||||||
typedef struct _DmenuModePrivateData
|
typedef struct _DmenuModePrivateData
|
||||||
{
|
{
|
||||||
char *prompt;
|
char *prompt;
|
||||||
int selected_line;
|
unsigned int selected_line;
|
||||||
char *message;
|
char *message;
|
||||||
char *format;
|
char *format;
|
||||||
struct range_pair * urgent_list;
|
struct range_pair * urgent_list;
|
||||||
|
@ -225,13 +226,13 @@ static void dmenu_mode_init ( Switcher *sw )
|
||||||
DmenuModePrivateData *pd = (DmenuModePrivateData *) sw->private_data;
|
DmenuModePrivateData *pd = (DmenuModePrivateData *) sw->private_data;
|
||||||
|
|
||||||
pd->prompt = "dmenu ";
|
pd->prompt = "dmenu ";
|
||||||
pd->selected_line = -1;
|
pd->selected_line = UINT32_MAX;
|
||||||
|
|
||||||
find_arg_str ( "-mesg", &( pd->message ) );
|
find_arg_str ( "-mesg", &( pd->message ) );
|
||||||
|
|
||||||
// Check prompt
|
// Check prompt
|
||||||
find_arg_str ( "-p", &( pd->prompt ) );
|
find_arg_str ( "-p", &( pd->prompt ) );
|
||||||
find_arg_int ( "-selected-row", &( pd->selected_line ) );
|
find_arg_uint ( "-selected-row", &( pd->selected_line ) );
|
||||||
// By default we print the unescaped line back.
|
// By default we print the unescaped line back.
|
||||||
pd->format = "s";
|
pd->format = "s";
|
||||||
|
|
||||||
|
@ -291,7 +292,7 @@ int dmenu_switcher_dialog ( void )
|
||||||
int retv = FALSE;
|
int retv = FALSE;
|
||||||
int restart = FALSE;
|
int restart = FALSE;
|
||||||
unsigned int cmd_list_length = 0;
|
unsigned int cmd_list_length = 0;
|
||||||
char **cmd_list = dmenu_mode.get_data ( &( cmd_list_length ), &dmenu_mode );
|
char **cmd_list = dmenu_mode.get_data ( &( cmd_list_length ), &dmenu_mode );
|
||||||
|
|
||||||
|
|
||||||
int only_selected = FALSE;
|
int only_selected = FALSE;
|
||||||
|
@ -368,14 +369,14 @@ int dmenu_switcher_dialog ( void )
|
||||||
retv = TRUE;
|
retv = TRUE;
|
||||||
}
|
}
|
||||||
// Quick switch with entry selected.
|
// Quick switch with entry selected.
|
||||||
else if ( ( mretv & MENU_QUICK_SWITCH ) && pd->selected_line >= 0 ) {
|
else if ( ( mretv & MENU_QUICK_SWITCH ) && pd->selected_line < UINT32_MAX ) {
|
||||||
dmenu_output_formatted_line ( pd->format, cmd_list[pd->selected_line], pd->selected_line, input );
|
dmenu_output_formatted_line ( pd->format, cmd_list[pd->selected_line], pd->selected_line, input );
|
||||||
|
|
||||||
restart = FALSE;
|
restart = FALSE;
|
||||||
retv = 10 + ( mretv & MENU_LOWER_MASK );
|
retv = 10 + ( mretv & MENU_LOWER_MASK );
|
||||||
}
|
}
|
||||||
// Quick switch without entry selected.
|
// Quick switch without entry selected.
|
||||||
else if ( ( mretv & MENU_QUICK_SWITCH ) && pd->selected_line == -1 ) {
|
else if ( ( mretv & MENU_QUICK_SWITCH ) && pd->selected_line == UINT32_MAX ) {
|
||||||
dmenu_output_formatted_line ( pd->format, input, -1, input );
|
dmenu_output_formatted_line ( pd->format, input, -1, input );
|
||||||
|
|
||||||
restart = FALSE;
|
restart = FALSE;
|
||||||
|
|
|
@ -233,52 +233,52 @@ static Window create_window ( Display *display )
|
||||||
|
|
||||||
typedef struct MenuState
|
typedef struct MenuState
|
||||||
{
|
{
|
||||||
Switcher *sw;
|
Switcher *sw;
|
||||||
unsigned int menu_lines;
|
unsigned int menu_lines;
|
||||||
unsigned int max_elements;
|
unsigned int max_elements;
|
||||||
unsigned int max_rows;
|
unsigned int max_rows;
|
||||||
unsigned int columns;
|
unsigned int columns;
|
||||||
|
|
||||||
// window width,height
|
// window width,height
|
||||||
unsigned int w, h;
|
unsigned int w, h;
|
||||||
int x, y;
|
int x, y;
|
||||||
unsigned int element_width;
|
unsigned int element_width;
|
||||||
int top_offset;
|
int top_offset;
|
||||||
|
|
||||||
// Update/Refilter list.
|
// Update/Refilter list.
|
||||||
int update;
|
int update;
|
||||||
int refilter;
|
int refilter;
|
||||||
int rchanged;
|
int rchanged;
|
||||||
int cur_page;
|
int cur_page;
|
||||||
|
|
||||||
// Entries
|
// Entries
|
||||||
textbox *text;
|
textbox *text;
|
||||||
textbox *prompt_tb;
|
textbox *prompt_tb;
|
||||||
textbox *message_tb;
|
textbox *message_tb;
|
||||||
textbox *case_indicator;
|
textbox *case_indicator;
|
||||||
textbox **boxes;
|
textbox **boxes;
|
||||||
scrollbar *scrollbar;
|
scrollbar *scrollbar;
|
||||||
int *distance;
|
int *distance;
|
||||||
int *line_map;
|
unsigned int *line_map;
|
||||||
|
|
||||||
unsigned int num_lines;
|
unsigned int num_lines;
|
||||||
|
|
||||||
// Selected element.
|
// Selected element.
|
||||||
unsigned int selected;
|
unsigned int selected;
|
||||||
unsigned int filtered_lines;
|
unsigned int filtered_lines;
|
||||||
// Last offset in paginating.
|
// Last offset in paginating.
|
||||||
unsigned int last_offset;
|
unsigned int last_offset;
|
||||||
|
|
||||||
KeySym prev_key;
|
KeySym prev_key;
|
||||||
Time last_button_press;
|
Time last_button_press;
|
||||||
|
|
||||||
int quit;
|
int quit;
|
||||||
int skip_absorb;
|
int skip_absorb;
|
||||||
// Return state
|
// Return state
|
||||||
int *selected_line;
|
unsigned int *selected_line;
|
||||||
MenuReturn retv;
|
MenuReturn retv;
|
||||||
char **lines;
|
char **lines;
|
||||||
int line_height;
|
int line_height;
|
||||||
}MenuState;
|
}MenuState;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -704,6 +704,7 @@ static void menu_mouse_navigation ( MenuState *state, XButtonEvent *xbe )
|
||||||
static void menu_refilter ( MenuState *state, char **lines, menu_match_cb mmc, void *mmc_data,
|
static void menu_refilter ( MenuState *state, char **lines, menu_match_cb mmc, void *mmc_data,
|
||||||
int sorting, int case_sensitive )
|
int sorting, int case_sensitive )
|
||||||
{
|
{
|
||||||
|
unsigned int sl = state->line_map[state->selected];
|
||||||
if ( strlen ( state->text->text ) > 0 ) {
|
if ( strlen ( state->text->text ) > 0 ) {
|
||||||
unsigned int j = 0;
|
unsigned int j = 0;
|
||||||
char **tokens = tokenize ( state->text->text, case_sensitive );
|
char **tokens = tokenize ( state->text->text, case_sensitive );
|
||||||
|
@ -746,6 +747,12 @@ static void menu_refilter ( MenuState *state, char **lines, menu_match_cb mmc, v
|
||||||
scrollbar_set_max_value ( state->scrollbar, state->filtered_lines );
|
scrollbar_set_max_value ( state->scrollbar, state->filtered_lines );
|
||||||
state->refilter = FALSE;
|
state->refilter = FALSE;
|
||||||
state->rchanged = TRUE;
|
state->rchanged = TRUE;
|
||||||
|
for ( unsigned int i = 0; i < state->filtered_lines; i++ ) {
|
||||||
|
if ( state->line_map[i] == sl ) {
|
||||||
|
state->selected = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -902,12 +909,12 @@ static void menu_paste ( MenuState *state, XSelectionEvent *xse )
|
||||||
}
|
}
|
||||||
|
|
||||||
MenuReturn menu ( Switcher *sw, char **input, char *prompt,
|
MenuReturn menu ( Switcher *sw, char **input, char *prompt,
|
||||||
int *selected_line,
|
unsigned int *selected_line,
|
||||||
unsigned int *next_pos, const char *message )
|
unsigned int *next_pos, const char *message )
|
||||||
{
|
{
|
||||||
int shift = FALSE;
|
int shift = FALSE;
|
||||||
MenuState state = {
|
MenuState state = {
|
||||||
.sw = sw,
|
.sw = sw,
|
||||||
.selected_line = selected_line,
|
.selected_line = selected_line,
|
||||||
.retv = MENU_CANCEL,
|
.retv = MENU_CANCEL,
|
||||||
.prev_key = 0,
|
.prev_key = 0,
|
||||||
|
@ -1041,7 +1048,7 @@ MenuReturn menu ( Switcher *sw, char **input, char *prompt,
|
||||||
|
|
||||||
scrollbar_set_max_value ( state.scrollbar, state.num_lines );
|
scrollbar_set_max_value ( state.scrollbar, state.num_lines );
|
||||||
// filtered list
|
// filtered list
|
||||||
state.line_map = g_malloc0_n ( state.num_lines, sizeof ( int ) );
|
state.line_map = g_malloc0_n ( state.num_lines, sizeof ( unsigned int ) );
|
||||||
if ( config.levenshtein_sort ) {
|
if ( config.levenshtein_sort ) {
|
||||||
state.distance = (int *) g_malloc0_n ( state.num_lines, sizeof ( int ) );
|
state.distance = (int *) g_malloc0_n ( state.num_lines, sizeof ( int ) );
|
||||||
}
|
}
|
||||||
|
@ -1086,9 +1093,10 @@ MenuReturn menu ( Switcher *sw, char **input, char *prompt,
|
||||||
state.quit = FALSE;
|
state.quit = FALSE;
|
||||||
menu_refilter ( &state, state.lines, sw->token_match, sw, config.levenshtein_sort, config.case_sensitive );
|
menu_refilter ( &state, state.lines, sw->token_match, sw, config.levenshtein_sort, config.case_sensitive );
|
||||||
|
|
||||||
for ( unsigned int i = 0; ( *( state.selected_line ) ) >= 0 && !state.selected && i < state.filtered_lines; i++ ) {
|
for ( unsigned int i = 0; ( *( state.selected_line ) ) < UINT32_MAX && !state.selected && i < state.filtered_lines; i++ ) {
|
||||||
if ( state.line_map[i] == *( state.selected_line ) ) {
|
if ( state.line_map[i] == *( state.selected_line ) ) {
|
||||||
state.selected = i;
|
state.selected = i;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1299,7 +1307,7 @@ MenuReturn menu ( Switcher *sw, char **input, char *prompt,
|
||||||
void error_dialog ( const char *msg, int markup )
|
void error_dialog ( const char *msg, int markup )
|
||||||
{
|
{
|
||||||
MenuState state = {
|
MenuState state = {
|
||||||
.sw = NULL,
|
.sw = NULL,
|
||||||
.selected_line = NULL,
|
.selected_line = NULL,
|
||||||
.retv = MENU_CANCEL,
|
.retv = MENU_CANCEL,
|
||||||
.prev_key = 0,
|
.prev_key = 0,
|
||||||
|
@ -2064,9 +2072,9 @@ int main ( int argc, char *argv[] )
|
||||||
|
|
||||||
SwitcherMode switcher_run ( char **input, Switcher *sw )
|
SwitcherMode switcher_run ( char **input, Switcher *sw )
|
||||||
{
|
{
|
||||||
char *prompt = g_strdup_printf ( "%s:", sw->name );
|
char *prompt = g_strdup_printf ( "%s:", sw->name );
|
||||||
int selected_line = -1;
|
unsigned int selected_line = UINT32_MAX;
|
||||||
int mretv = menu ( sw, input, prompt, &selected_line, NULL, NULL );
|
int mretv = menu ( sw, input, prompt, &selected_line, NULL, NULL );
|
||||||
|
|
||||||
g_free ( prompt );
|
g_free ( prompt );
|
||||||
return sw->result ( mretv, input, selected_line, sw );
|
return sw->result ( mretv, input, selected_line, sw );
|
||||||
|
|
Loading…
Reference in a new issue