diff --git a/data/gtk/ui/Preferences.blp b/data/gtk/ui/Preferences.blp index eb81f10..01da697 100644 --- a/data/gtk/ui/Preferences.blp +++ b/data/gtk/ui/Preferences.blp @@ -23,6 +23,15 @@ template $LexiPreferences : Adw.PreferencesDialog { title: _("Use Debug logging profile"); } } + + + Adw.PreferencesGroup { + title: _("Search"); + + Adw.SwitchRow use_accent_insensitive_search_row { + title: _("Ignore accents when searching for words"); + } + } } Adw.PreferencesPage { diff --git a/data/io.github.dzheremi2.lexi.gschema.xml.in b/data/io.github.dzheremi2.lexi.gschema.xml.in index 08a1c6d..dc919ee 100644 --- a/data/io.github.dzheremi2.lexi.gschema.xml.in +++ b/data/io.github.dzheremi2.lexi.gschema.xml.in @@ -7,6 +7,9 @@ false + + true + diff --git a/lexi/ui/Preferences.py b/lexi/ui/Preferences.py index f912369..d526d84 100644 --- a/lexi/ui/Preferences.py +++ b/lexi/ui/Preferences.py @@ -21,6 +21,7 @@ class LexiPreferences(Adw.PreferencesDialog): available_word_types_scrolled_window: Gtk.ScrolledWindow = gtc() available_word_types_list_box: Gtk.ListBox = gtc() use_debug_log_switch_row: Adw.SwitchRow = gtc() + use_accent_insensitive_search_switch_row: Adw.SwitchRow = gtc() opened: bool = False @@ -41,6 +42,12 @@ def __init__(self) -> None: "active", Gio.SettingsBindFlags.DEFAULT, ) + shared.schema.bind( + "accent-insensitive-search", + self.use_accent_insensitive_search_switch_row, + "active", + Gio.SettingsBindFlags.DEFAULT, + ) self.use_debug_log_switch_row.connect( "notify::active", self.__set_use_debug_log diff --git a/lexi/utils/sort_filter.py b/lexi/utils/sort_filter.py index 2a39390..9d6ae34 100644 --- a/lexi/utils/sort_filter.py +++ b/lexi/utils/sort_filter.py @@ -6,6 +6,7 @@ from lexi.logging.logger import logger from lexi.ui.WordRow import WordRow +import unicodedata # pylint: disable=no-else-return def sort_words(row1: WordRow, row2: WordRow) -> int: @@ -60,6 +61,11 @@ def sort_words(row1: WordRow, row2: WordRow) -> int: else: return 0 +def remove_accents(text: str) -> str: + if shared.schema.get_boolean("accent-insensitive-search"): + text = unicodedata.normalize("NFD", text) + return ''.join(character for character in text if not unicodedata.combining(character)) + return text def filter_words(row: WordRow) -> bool: """ @@ -75,19 +81,19 @@ def filter_words(row: WordRow) -> bool: bool True if the word matches both the text and type filters, False otherwise """ - text: str = shared.win.lexicon_search_entry.get_text().lower() + text: str = remove_accents(shared.win.lexicon_search_entry.get_text().lower().strip()) fits_in_filter = set(shared.config["enabled-types"]).issubset(set(row.word.types)) if not text.startswith("#"): try: matches_text = ( text == "" - or text in row.word.word.lower().replace("&rtl", "") - or ( - text in row.word.translations[0].lower().replace("&rtl", "") - if row.word.translations - else "" - ) + or text in remove_accents(row.word.word.lower().replace("&rtl", "")) ) + if not matches_text and row.word.translations: + for translation in row.word.translations: + if text in remove_accents(translation.lower().replace("&rtl", "")): + matches_text = True + break logger.debug( "Word ā€œ%sā€, is shown: %s", row.word.word,