diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 7a8825720..7a3bbe159 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1009,40 +1009,27 @@ class CommandDispatcher: raise cmdexc.CommandError(e) self._open(url, tab, bg, window) - @cmdutils.register(instance='command-dispatcher', scope='window') - @cmdutils.argument('index', completion=miscmodels.buffer) - @cmdutils.argument('count', count=True) - def buffer(self, index=None, count=None): - """Select tab by index or url/title best match. - - Focuses window if necessary when index is given. If both index and - count are given, use count. + def _resolve_buffer_index(self, index): + """Resolves a buffer index to the tabbedbrowser and tab. Args: - index: The [win_id/]index of the tab to focus. Or a substring + index: The [win_id/]index of the tab to be selected. Or a substring in which case the closest match will be focused. - count: The tab index to focus, starting with 1. """ - if count is not None: - index_parts = [count] - elif index is None: - raise cmdexc.CommandError("buffer: Either a count or the argument " - "index must be specified.") - else: - index_parts = index.split('/', 1) + index_parts = index.split('/', 1) - try: - for part in index_parts: - int(part) - except ValueError: - model = miscmodels.buffer() - model.set_pattern(index) - if model.count() > 0: - index = model.data(model.first_item()) - index_parts = index.split('/', 1) - else: - raise cmdexc.CommandError( - "No matching tab for: {}".format(index)) + try: + for part in index_parts: + int(part) + except ValueError: + model = miscmodels.buffer() + model.set_pattern(index) + if model.count() > 0: + index = model.data(model.first_item()) + index_parts = index.split('/', 1) + else: + raise cmdexc.CommandError( + "No matching tab for: {}".format(index)) if len(index_parts) == 2: win_id = int(index_parts[0]) @@ -1066,10 +1053,35 @@ class CommandDispatcher: raise cmdexc.CommandError( "There's no tab with index {}!".format(idx)) - window = objreg.window_registry[win_id] + return (tabbed_browser, tabbed_browser.widget(idx-1)) + + @cmdutils.register(instance='command-dispatcher', scope='window') + @cmdutils.argument('index', completion=miscmodels.buffer) + @cmdutils.argument('count', count=True) + def buffer(self, index=None, count=None): + """Select tab by index or url/title best match. + + Focuses window if necessary when index is given. If both index and + count are given, use count. + + Args: + index: The [win_id/]index of the tab to focus. Or a substring + in which case the closest match will be focused. + count: The tab index to focus, starting with 1. + """ + if count is None and index is None: + raise cmdexc.CommandError("buffer: Either a count or the argument " + "index must be specified.") + + if count is not None: + index = str(count) + + tabbed_browser, tab = self._resolve_buffer_index(index) + + window = tabbed_browser.window() window.activateWindow() window.raise_() - tabbed_browser.setCurrentIndex(idx-1) + tabbed_browser.setCurrentWidget(tab) @cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.argument('index', choices=['last'])