From 5b95abce92042f4c3157a8f6e27d9922abbc879e Mon Sep 17 00:00:00 2001 From: Qball Date: Mon, 20 Jan 2025 14:34:53 +0100 Subject: [PATCH] [Lexer] Allow for optional imports. Adds the `?import` syntax. fixes: #2078 --- doc/rofi-theme.5.markdown | 8 ++++++++ lexer/theme-lexer.l | 24 +++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/doc/rofi-theme.5.markdown b/doc/rofi-theme.5.markdown index dd6c2b4f..2fc60df3 100644 --- a/doc/rofi-theme.5.markdown +++ b/doc/rofi-theme.5.markdown @@ -1658,6 +1658,14 @@ If a filename is provided, it will try to resolve it in the following order: A name is resolved (if it has no valid extension) as a filename by appending the `.rasi` and the `.rasinc` extension. It will first look for files with `.rasi`, then for files with `.rasinc`. +If you want to do an optional import, e.g. no error when the file does not exists, you can do: + +```css +?import "myfile" +``` + +This still throws an error on syntax error, but won't abort parsing if file does not exists. + ## Examples Several examples are installed together with **rofi**. These can be found in diff --git a/lexer/theme-lexer.l b/lexer/theme-lexer.l index 5af651ca..bf5b5bd4 100644 --- a/lexer/theme-lexer.l +++ b/lexer/theme-lexer.l @@ -51,6 +51,8 @@ int last_state = 0; extern int rofi_is_in_dmenu_mode; +gboolean import_optional = FALSE; + const char *rasi_theme_file_extensions[] = {".rasi", ".rasinc", NULL}; /** * Type of Object to parse. @@ -289,6 +291,7 @@ C_COMMENT_OPEN "/*" INCLUDE "@import" +OPT_INCLUDE "?import" THEME "@theme" DEFAULT (?i:\"default\"?) @@ -378,6 +381,12 @@ if ( queue == NULL ) { */ {INCLUDE} { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); + import_optional = FALSE; + BEGIN(INCLUDE); +} +{OPT_INCLUDE} { + g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); + import_optional = TRUE; BEGIN(INCLUDE); } {THEME} { @@ -437,10 +446,15 @@ if ( queue == NULL ) { yylloc->first_column = yylloc->last_column = 1; yylloc->filename = current->filename; } else { - char *str = g_markup_printf_escaped ( "Failed to open theme: %s\nError: %s", - filename, strerror ( errno ) ); - rofi_add_warning_message ( g_string_new ( str ) ); - g_free ( str ); + if ( !import_optional ) { + char *str = g_markup_printf_escaped ( "Failed to open theme: %s\nError: %s", + filename, strerror ( errno ) ); + rofi_add_warning_message ( g_string_new ( str ) ); + g_free ( str ); + } else { + g_warning("Trying to parse optional theme: '%s', Error: %s", + filename, strerror(errno)); + } g_free(filename); } // Pop out of include. */ @@ -844,7 +858,7 @@ if ( queue == NULL ) { /** * Media defaults. */ -{WHITESPACE}+ ; // ignore all whitespace +{WHITESPACE}+ ; // ignore all whitespace . { yytext[yyleng-1] = '\0';