From bea02304ee93a220f443a69def2825112a87cdd9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 2 Jan 2014 10:24:32 +0530 Subject: [PATCH] Add an option to control how completion works when editing authors/tags/series/etc. By default, when completing items, calibre will show you all the candidates that start with the text you have already typed. You can instead have it show all candidates that contain the text you have already typed. To do this, go to Preferences->Tweaks->Completion mode. For example, if you type asi it will now match both Asimov and Quasimodo, whereas the default behavior would match only Asimov. Fixes #1264304 [[Enhancement] search within string when tagging](https://bugs.launchpad.net/calibre/+bug/1264304) --- resources/default_tweaks.py | 8 ++++++++ src/calibre/gui2/complete2.py | 9 ++++++--- src/calibre/utils/icu.py | 3 +++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/resources/default_tweaks.py b/resources/default_tweaks.py index 993c8f74f9..a3008d7a5d 100644 --- a/resources/default_tweaks.py +++ b/resources/default_tweaks.py @@ -523,6 +523,14 @@ default_tweak_format = None # your library and your personal editing style. preselect_first_completion = False +#: Completion mode when editing authors/tags/series/etc. +# By default, when completing items, calibre will show you all the candidates +# that start with the text you have already typed. You can instead have it show +# all candidates that contain the text you have already typed. To do this, set +# completion_mode to 'contains'. For example, if you type asi it will match both +# Asimov and Quasimodo, whereas the default behavior would match only Asimov. +completion_mode = 'prefix' + #: Recognize numbers inside text when sorting # This means that when sorting on text fields like title the text "Book 2" # will sort before the text "Book 100". If you want this behavior, set diff --git a/src/calibre/gui2/complete2.py b/src/calibre/gui2/complete2.py index 3214cb5fff..25c7ff3fc2 100644 --- a/src/calibre/gui2/complete2.py +++ b/src/calibre/gui2/complete2.py @@ -14,11 +14,14 @@ from PyQt4.Qt import (QLineEdit, QAbstractListModel, Qt, pyqtSignal, QObject, QApplication, QListView, QPoint, QModelIndex, QFont, QFontInfo) from calibre.constants import isosx, get_osx_version -from calibre.utils.icu import sort_key, primary_startswith +from calibre.utils.icu import sort_key, primary_startswith, primary_icu_find from calibre.gui2 import NONE from calibre.gui2.widgets import EnComboBox, LineEditECM from calibre.utils.config import tweaks +def containsq(x, prefix): + return primary_icu_find(prefix, x)[0] != -1 + class CompleteModel(QAbstractListModel): # {{{ def __init__(self, parent=None): @@ -45,8 +48,8 @@ class CompleteModel(QAbstractListModel): # {{{ return subset = prefix.startswith(old_prefix) universe = self.current_items if subset else self.all_items - self.current_items = tuple(x for x in universe if primary_startswith(x, - prefix)) + func = primary_startswith if tweaks['completion_mode'] == 'prefix' else containsq + self.current_items = tuple(x for x in universe if func(x, prefix)) self.reset() def rowCount(self, *args): diff --git a/src/calibre/utils/icu.py b/src/calibre/utils/icu.py index 27168bc618..9e0df01b85 100644 --- a/src/calibre/utils/icu.py +++ b/src/calibre/utils/icu.py @@ -247,6 +247,9 @@ def primary_find(pat, src): if _icu_not_ok: from calibre.utils.filenames import ascii_text return py_find(ascii_text(pat), ascii_text(src)) + return primary_icu_find(pat, src) + +def primary_icu_find(pat, src): try: return icu_find(_primary_collator, pat, src) except AttributeError: