From a84f04a14b4524a0a1872eea5b000192b9d3207a Mon Sep 17 00:00:00 2001 From: J0hannes101 <93882518+J0hannes101@users.noreply.github.com> Date: Fri, 21 Feb 2025 16:28:21 +0100 Subject: [PATCH] [DRUN] Add option to exclude categories from drun #2102 (#2104) * Add option to exclude categories from drun * Fix memory leak when both -drun-categories and -drun-exclude-categories are used * update docs --- config/config.c | 2 ++ doc/rofi.1.markdown | 4 ++++ include/settings.h | 2 ++ source/modes/drun.c | 19 +++++++++++++++++++ source/xrmoptions.c | 6 ++++++ 5 files changed, 33 insertions(+) diff --git a/config/config.c b/config/config.c index 2d2347a5..1b9bccac 100644 --- a/config/config.c +++ b/config/config.c @@ -128,6 +128,8 @@ Settings config = { .drun_match_fields = "name,generic,exec,categories,keywords", /** Only show entries in this category */ .drun_categories = NULL, + /** Exclude entries in this category */ + .drun_exclude_categories = NULL, /** Desktop entry show actions */ .drun_show_actions = FALSE, /** Desktop format display */ diff --git a/doc/rofi.1.markdown b/doc/rofi.1.markdown index 28be15fb..3847cde8 100644 --- a/doc/rofi.1.markdown +++ b/doc/rofi.1.markdown @@ -374,6 +374,10 @@ Tokenize the input. Only show desktop files that are present in the listed categories. +`-drun-exclude-categories` *category1*,*category2* + +Exclude desktop files that are present in the listed categories. + `-drun-match-fields` *field1*,*field2*,... When using `drun`, match only with the specified Desktop entry fields. diff --git a/include/settings.h b/include/settings.h index a958b478..971841f4 100644 --- a/include/settings.h +++ b/include/settings.h @@ -119,6 +119,8 @@ typedef struct { char *drun_match_fields; /** Only show entries in this category */ char *drun_categories; + /** Exclude entries in this category */ + char *drun_exclude_categories; /** Desktop entry show actions */ unsigned int drun_show_actions; /** Desktop format display */ diff --git a/source/modes/drun.c b/source/modes/drun.c index 02454bc5..4b146fb9 100644 --- a/source/modes/drun.c +++ b/source/modes/drun.c @@ -215,6 +215,7 @@ struct _DRunModePrivateData { unsigned int expected_line_height; char **show_categories; + char **exclude_categories; // Theme const gchar *icon_theme; @@ -722,6 +723,19 @@ static void read_desktop_file(DRunModePrivateData *pd, const char *root, } } + if (pd->exclude_categories) { + if (categories == NULL) { + categories = g_key_file_get_locale_string_list( + kf, DRUN_GROUP_NAME, "Categories", NULL, NULL, NULL); + } + if (rofi_strv_contains((const char *const *)categories, + (const char *const *)pd->exclude_categories)) { + g_strfreev(categories); + g_key_file_free(kf); + return; + } + } + size_t nl = ((pd->cmd_list_length) + 1); if (nl >= pd->cmd_list_length_actual) { pd->cmd_list_length_actual += 256; @@ -1320,6 +1334,10 @@ static int drun_mode_init(Mode *sw) { pd->show_categories = g_strsplit(config.drun_categories, ",", 0); } + if (config.drun_exclude_categories && *(config.drun_exclude_categories)) { + pd->exclude_categories = g_strsplit(config.drun_exclude_categories, ",", 0); + } + drun_mode_parse_entry_fields(); drun_mode_parse_display_format(); get_apps(pd); @@ -1473,6 +1491,7 @@ static void drun_mode_destroy(Mode *sw) { g_strfreev(rmpd->current_desktop_list); g_strfreev(rmpd->show_categories); + g_strfreev(rmpd->exclude_categories); g_free(rmpd); mode_set_private_data(sw, NULL); } diff --git a/source/xrmoptions.c b/source/xrmoptions.c index e4cbe4af..692124f9 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -241,6 +241,12 @@ static XrmOption xrmOptions[] = { NULL, "Only show Desktop entry from these categories", CONFIG_DEFAULT}, + {xrm_String, + "drun-exclude-categories", + {.str = &config.drun_exclude_categories}, + NULL, + "Exclude Desktop entries from these categories", + CONFIG_DEFAULT}, {xrm_Boolean, "drun-show-actions", {.num = &config.drun_show_actions},