diff --git a/src/calibre/srv/code.py b/src/calibre/srv/code.py index 96b29fec05..d8027f56ee 100644 --- a/src/calibre/srv/code.py +++ b/src/calibre/srv/code.py @@ -142,6 +142,7 @@ def basic_interface_data(ctx, rd): 'gui_pubdate_display_format': tweaks['gui_pubdate_display_format'], 'gui_timestamp_display_format': tweaks['gui_timestamp_display_format'], 'gui_last_modified_display_format': tweaks['gui_last_modified_display_format'], + 'completion_mode': tweaks['completion_mode'], 'use_roman_numerals_for_series_number': get_use_roman(), 'translations': get_translations(), 'icon_map': icon_map(), diff --git a/src/pyj/book_list/edit_metadata.pyj b/src/pyj/book_list/edit_metadata.pyj index 41e6281769..e2a4d61aac 100644 --- a/src/pyj/book_list/edit_metadata.pyj +++ b/src/pyj/book_list/edit_metadata.pyj @@ -148,6 +148,14 @@ def show_completions(container_id, div, field, prefix, names): break +def query_contains(haystack, needle): + return haystack.toLowerCase().indexOf(needle) is not -1 + + +def query_startswitch(haystack, needle): + return haystack.toLowerCase().indexOf(needle) is 0 + + def update_completions(container_id, ok, field, names): c = document.getElementById(container_id) if not c: @@ -172,12 +180,13 @@ def update_completions(container_id, ok, field, names): prefix = val[-1] if val.length else '' if prefix is update_completions.prefix: return - pl = prefix.toLowerCase().strip() - if pl: - if update_completions.prefix and pl.startswith(update_completions.prefix.toLowerCase()): - matching_names = [x for x in update_completions.names if x.toLowerCase().startswith(pl)] - else: - matching_names = [x for x in names if x.toLowerCase().startswith(pl)] + needle = prefix.toLowerCase().strip() + + if needle: + interface_data = get_interface_data() + universe = update_completions.names if update_completions.prefix and needle.startswith(update_completions.prefix.toLowerCase()) else names + q = query_contains if interface_data.completion_mode is 'contains' else query_startswitch + matching_names = [x for x in universe if q(x, needle)] else: matching_names = [] update_completions.prefix = prefix