From a09b1a4e05074b0a737cbd7dea80d4b1d1582707 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 30 Nov 2018 14:32:28 +0100 Subject: [PATCH] Move caret commands to components.caretcommands --- qutebrowser/api/apitypes.py | 2 +- qutebrowser/app.py | 2 +- qutebrowser/browser/commands.py | 169 ------------------- qutebrowser/components/caretcommands.py | 205 ++++++++++++++++++++++++ 4 files changed, 207 insertions(+), 171 deletions(-) create mode 100644 qutebrowser/components/caretcommands.py diff --git a/qutebrowser/api/apitypes.py b/qutebrowser/api/apitypes.py index 444e49558..8ec7be547 100644 --- a/qutebrowser/api/apitypes.py +++ b/qutebrowser/api/apitypes.py @@ -20,4 +20,4 @@ """A single tab.""" # pylint: disable=unused-import -from qutebrowser.browser.browsertab import AbstractTab as Tab +from qutebrowser.browser.browsertab import WebTabError, AbstractTab as Tab diff --git a/qutebrowser/app.py b/qutebrowser/app.py index e950ff2ec..0bb7e14cb 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -77,7 +77,7 @@ from qutebrowser.utils import (log, version, message, utils, urlutils, objreg, usertypes, standarddir, error, qtutils) # pylint: disable=unused-import # We import those to run the cmdutils.register decorators. -from qutebrowser.components import scrollcommands +from qutebrowser.components import scrollcommands, caretcommands from qutebrowser.mainwindow.statusbar import command from qutebrowser.misc import utilcmds # pylint: enable=unused-import diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 791e45a1a..f1bb8fb35 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1348,18 +1348,6 @@ class CommandDispatcher: except KeyError: raise cmdutils.CommandError("Bookmark '{}' not found!".format(url)) - @cmdutils.register(instance='command-dispatcher', scope='window') - def follow_selected(self, *, tab=False): - """Follow the selected text. - - Args: - tab: Load the selected link in a new tab. - """ - try: - self._current_widget().caret.follow_selected(tab=tab) - except browsertab.WebTabError as e: - raise cmdutils.CommandError(str(e)) - @cmdutils.register(instance='command-dispatcher', name='inspector', scope='window') def toggle_inspector(self): @@ -1800,163 +1788,6 @@ class CommandDispatcher: tab.search.prev_result() tab.search.prev_result(result_cb=cb) - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_next_line(self, count=1): - """Move the cursor or selection to the next line. - - Args: - count: How many lines to move. - """ - self._current_widget().caret.move_to_next_line(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_prev_line(self, count=1): - """Move the cursor or selection to the prev line. - - Args: - count: How many lines to move. - """ - self._current_widget().caret.move_to_prev_line(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_next_char(self, count=1): - """Move the cursor or selection to the next char. - - Args: - count: How many lines to move. - """ - self._current_widget().caret.move_to_next_char(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_prev_char(self, count=1): - """Move the cursor or selection to the previous char. - - Args: - count: How many chars to move. - """ - self._current_widget().caret.move_to_prev_char(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_end_of_word(self, count=1): - """Move the cursor or selection to the end of the word. - - Args: - count: How many words to move. - """ - self._current_widget().caret.move_to_end_of_word(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_next_word(self, count=1): - """Move the cursor or selection to the next word. - - Args: - count: How many words to move. - """ - self._current_widget().caret.move_to_next_word(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_prev_word(self, count=1): - """Move the cursor or selection to the previous word. - - Args: - count: How many words to move. - """ - self._current_widget().caret.move_to_prev_word(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - def move_to_start_of_line(self): - """Move the cursor or selection to the start of the line.""" - self._current_widget().caret.move_to_start_of_line() - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - def move_to_end_of_line(self): - """Move the cursor or selection to the end of line.""" - self._current_widget().caret.move_to_end_of_line() - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_start_of_next_block(self, count=1): - """Move the cursor or selection to the start of next block. - - Args: - count: How many blocks to move. - """ - self._current_widget().caret.move_to_start_of_next_block(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_start_of_prev_block(self, count=1): - """Move the cursor or selection to the start of previous block. - - Args: - count: How many blocks to move. - """ - self._current_widget().caret.move_to_start_of_prev_block(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_end_of_next_block(self, count=1): - """Move the cursor or selection to the end of next block. - - Args: - count: How many blocks to move. - """ - self._current_widget().caret.move_to_end_of_next_block(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - @cmdutils.argument('count', value=cmdutils.Value.count) - def move_to_end_of_prev_block(self, count=1): - """Move the cursor or selection to the end of previous block. - - Args: - count: How many blocks to move. - """ - self._current_widget().caret.move_to_end_of_prev_block(count) - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - def move_to_start_of_document(self): - """Move the cursor or selection to the start of the document.""" - self._current_widget().caret.move_to_start_of_document() - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - def move_to_end_of_document(self): - """Move the cursor or selection to the end of the document.""" - self._current_widget().caret.move_to_end_of_document() - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - def toggle_selection(self): - """Toggle caret selection mode.""" - self._current_widget().caret.toggle_selection() - - @cmdutils.register(instance='command-dispatcher', modes=[KeyMode.caret], - scope='window') - def drop_selection(self): - """Drop selection and keep selection mode enabled.""" - self._current_widget().caret.drop_selection() - @cmdutils.register(instance='command-dispatcher', scope='window', debug=True) @cmdutils.argument('count', value=cmdutils.Value.count) diff --git a/qutebrowser/components/caretcommands.py b/qutebrowser/components/caretcommands.py new file mode 100644 index 000000000..ae0828ef5 --- /dev/null +++ b/qutebrowser/components/caretcommands.py @@ -0,0 +1,205 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2018 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Commands related to caret browsing.""" + + +from qutebrowser.api import cmdutils, apitypes + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_next_line(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the next line. + + Args: + count: How many lines to move. + """ + tab.caret.move_to_next_line(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_prev_line(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the prev line. + + Args: + count: How many lines to move. + """ + tab.caret.move_to_prev_line(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_next_char(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the next char. + + Args: + count: How many lines to move. + """ + tab.caret.move_to_next_char(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_prev_char(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the previous char. + + Args: + count: How many chars to move. + """ + tab.caret.move_to_prev_char(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_end_of_word(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the end of the word. + + Args: + count: How many words to move. + """ + tab.caret.move_to_end_of_word(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_next_word(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the next word. + + Args: + count: How many words to move. + """ + tab.caret.move_to_next_word(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_prev_word(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the previous word. + + Args: + count: How many words to move. + """ + tab.caret.move_to_prev_word(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +def move_to_start_of_line(tab: apitypes.Tab): + """Move the cursor or selection to the start of the line.""" + tab.caret.move_to_start_of_line() + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +def move_to_end_of_line(tab: apitypes.Tab): + """Move the cursor or selection to the end of line.""" + tab.caret.move_to_end_of_line() + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_start_of_next_block(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the start of next block. + + Args: + count: How many blocks to move. + """ + tab.caret.move_to_start_of_next_block(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_start_of_prev_block(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the start of previous block. + + Args: + count: How many blocks to move. + """ + tab.caret.move_to_start_of_prev_block(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_end_of_next_block(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the end of next block. + + Args: + count: How many blocks to move. + """ + tab.caret.move_to_end_of_next_block(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +@cmdutils.argument('tab', value=cmdutils.Value.tab) +@cmdutils.argument('count', value=cmdutils.Value.count) +def move_to_end_of_prev_block(tab: apitypes.Tab, count: int = 1): + """Move the cursor or selection to the end of previous block. + + Args: + count: How many blocks to move. + """ + tab.caret.move_to_end_of_prev_block(count) + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +def move_to_start_of_document(tab: apitypes.Tab): + """Move the cursor or selection to the start of the document.""" + tab.caret.move_to_start_of_document() + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +def move_to_end_of_document(tab: apitypes.Tab): + """Move the cursor or selection to the end of the document.""" + tab.caret.move_to_end_of_document() + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +def toggle_selection(tab: apitypes.Tab): + """Toggle caret selection mode.""" + tab.caret.toggle_selection() + + +@cmdutils.register(modes=[cmdutils.KeyMode.caret]) +def drop_selection(tab: apitypes.Tab): + """Drop selection and keep selection mode enabled.""" + tab.caret.drop_selection() + + +@cmdutils.register() +@cmdutils.argument('tab_obj', value=cmdutils.Value.tab) +def follow_selected(tab_obj: apitypes.Tab, *, tab=False): + """Follow the selected text. + + Args: + tab: Load the selected link in a new tab. + """ + try: + tab_obj.caret.follow_selected(tab=tab) + except apitypes.WebTabError as e: + raise cmdutils.CommandError(str(e))