Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -pin-last option to keep the last-used entry first #651

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Settings config = {
.fixed_num_lines = TRUE,
/** Do not use history */
.disable_history = FALSE,
/** Always place the last-executed item in first place */
.pin_last = TRUE,
/** Sort the displayed list */
.sort = FALSE,
/** Use levenshtein sorting when matching */
Expand Down
6 changes: 6 additions & 0 deletions doc/rofi.1
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,12 @@ Disable history
Enable, disable sorting\. This setting can be changed at runtime (see \fB\-kb\-toggle\-sort\fR)\.
.
.P
\fB\-pin\-last\fR to enable \fB\-no\-pin\-last\fR to disable
.
.P
Always place the last-executed item in first place
.
.P
\fB\-levenshtein\-sort\fR to enable \fB\-no\-levenshtein\-sort\fR to disable
.
.P
Expand Down
5 changes: 5 additions & 0 deletions doc/rofi.1.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ Disable history
Enable, disable sorting.
This setting can be changed at runtime (see `-kb-toggle-sort`).

`-pin-last` to enable
`-no-pin-last` to disable

Always place the last-executed item in first place

`-levenshtein-sort` to enable
`-no-levenshtein-sort` to disable

Expand Down
2 changes: 2 additions & 0 deletions doc/test_xr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ rofi.window-command: xkill -id {window}
! rofi.drun-match-fields: name,generic,exec,categories
! "Disable history in run/ssh" Set from: File
rofi.disable-history: false
! "Always place the last-executed item in first place" Set from: Default
! rofi.pin-last: true
! "Use sorting" Set from: Default
! rofi.sort: false
! "Use levenshtein sorting also for fuzzy matching" Set from: File
Expand Down
1 change: 1 addition & 0 deletions include/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*
* This uses the following options from the #config object:
* * #Settings::disable_history
* * #Settings::pin_last
*
* @{
*/
Expand Down
4 changes: 4 additions & 0 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include <glib.h>

#include "rofi-types.h"

/**
* Enumeration indicating the matching method to use.
*
Expand Down Expand Up @@ -102,6 +104,8 @@ typedef struct
unsigned int fixed_num_lines;
/** Do not use history */
unsigned int disable_history;
/** Always place the last-executed item in first place */
unsigned int pin_last;
/** Toggle to enable sorting. */
unsigned int sort;
/** Desktop entries to match in drun */
Expand Down
17 changes: 16 additions & 1 deletion source/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ static void __history_write_element_list ( FILE *fd, _element **list, unsigned i
return;
}
// Sort the list before writing out.
g_qsort_with_data ( list, length, sizeof ( _element* ), __element_sort_func, NULL );
if ( config.pin_last ) {
// Leave the pinned element at the top.
g_qsort_with_data ( list + 1, length - 1, sizeof ( _element* ), __element_sort_func, NULL );
} else {
g_qsort_with_data ( list, length, sizeof ( _element* ), __element_sort_func, NULL );
}

// Get minimum index.
int min_value = list[length - 1]->index;
Expand Down Expand Up @@ -214,11 +219,21 @@ void history_set ( const char *filename, const char *entry )
// set # hits
list[length]->index = 1;

// save position
curr = length;

length++;
list[length] = NULL;
}
}

if ( config.pin_last && curr != 0 ) {
// Move the updated item to the top of the list.
_element *tmp = list[curr];
memmove ( list + 1, list, curr * sizeof ( _element* ) );
list[0] = tmp;
}

fd = fopen ( filename, "w" );
if ( fd == NULL ) {
g_warning ( "Failed to open file: %s", g_strerror ( errno ) );
Expand Down
2 changes: 2 additions & 0 deletions source/xrmoptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ static XrmOption xrmOptions[] = {
"Desktop entry fields to match in drun", CONFIG_DEFAULT },
{ xrm_Boolean, "disable-history", { .num = &config.disable_history }, NULL,
"Disable history in run/ssh", CONFIG_DEFAULT },
{ xrm_Boolean, "pin-last", { .num = &config.pin_last }, NULL,
"Always place the last-executed item in first place", CONFIG_DEFAULT },
{ xrm_Boolean, "sort", { .num = &config.sort }, NULL,
"Use sorting", CONFIG_DEFAULT },
{ xrm_Boolean, "levenshtein-sort", { .num = &config.levenshtein_sort }, NULL,
Expand Down
13 changes: 13 additions & 0 deletions test/history-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <assert.h>
#include <glib.h>
#include <history.h>
#include <settings.h>
#include <string.h>

static int test = 0;
Expand All @@ -45,6 +46,7 @@ const char *file = "text";
static void history_test ( void )
{
unlink ( file );
config.pin_last = 0;

// Empty list.
unsigned int length = 0;
Expand Down Expand Up @@ -114,6 +116,17 @@ static void history_test ( void )

g_strfreev ( retv );

// Pinning the last entry
config.pin_last = 1;
for ( unsigned int in = 0; in < 10; in++ ) {
history_set ( file, "popular" );
}
history_set ( file, "last" );

retv = history_get_list ( file, &length );
TASSERT ( g_strcmp0 ( retv[0], "last" ) == 0 );
g_strfreev ( retv );

unlink ( file );
}

Expand Down