diff --git a/src/calibre/gui2/tweak_book/completion/basic.py b/src/calibre/gui2/tweak_book/completion/basic.py index 824e88db53..b0d4df6079 100644 --- a/src/calibre/gui2/tweak_book/completion/basic.py +++ b/src/calibre/gui2/tweak_book/completion/basic.py @@ -77,6 +77,7 @@ def complete_names(names_data, data_conn): names_cache['stylesheet'] = frozenset(n for n in all_names if n.mime_type in OEB_STYLES) names_cache['image'] = frozenset(n for n in all_names if n.mime_type.startswith('image/')) names_cache['font'] = frozenset(n for n in all_names if n.mime_type in OEB_FONTS) + names_cache['css_resource'] = names_cache['image'] | names_cache['font'] names_cache['descriptions'] = d = {} for x, desc in {'text_link':_('Text'), 'stylesheet':_('Stylesheet'), 'image':_('Image'), 'font':_('Font')}.iteritems(): for n in names_cache[x]: diff --git a/src/calibre/gui2/tweak_book/editor/smarts/css.py b/src/calibre/gui2/tweak_book/editor/smarts/css.py index e7cd826d2c..c0c99ddba0 100644 --- a/src/calibre/gui2/tweak_book/editor/smarts/css.py +++ b/src/calibre/gui2/tweak_book/editor/smarts/css.py @@ -6,8 +6,11 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2014, Kovid Goyal ' +import re + from PyQt5.Qt import Qt +from calibre.gui2.tweak_book import current_container from calibre.gui2.tweak_book.editor.smarts import NullSmarts from calibre.gui2.tweak_book.editor.smarts.utils import ( no_modifiers, get_leading_whitespace_on_block, get_text_before_cursor, @@ -33,6 +36,12 @@ def find_rule(raw, rule_address): class Smarts(NullSmarts): + def __init__(self, *args, **kwargs): + if not hasattr(Smarts, 'regexps_compiled'): + Smarts.regexps_compiled = True + Smarts.complete_attr_pat = re.compile(r'''url\s*\(\s*['"]{0,1}([^)]*)$''') + NullSmarts.__init__(self, *args, **kwargs) + def handle_key_press(self, ev, editor): key = ev.key() @@ -65,4 +74,14 @@ class Smarts(NullSmarts): return False - + def get_completion_data(self, editor, ev=None): + c = editor.textCursor() + c.movePosition(c.StartOfLine, c.KeepAnchor) + text = c.selectedText() + m = self.complete_attr_pat.search(text) + if m is None: + return + query = m.group(1) or '' + doc_name = editor.completion_doc_name + if doc_name: + return 'complete_names', ('css_resource', doc_name, current_container().root), query