From 18c68ce74e1775b4b9097884c98175969f23b196 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 29 Jan 2014 21:06:56 +0100 Subject: [PATCH] Implement searching --- qutebrowser/app.py | 7 +++++ qutebrowser/commands/__init__.py | 7 +++++ qutebrowser/commands/utils.py | 38 ++++++++++++++++++++++++ qutebrowser/utils/config.py | 1 + qutebrowser/widgets/browser.py | 8 +++++ qutebrowser/widgets/statusbar/command.py | 12 ++++++-- 6 files changed, 71 insertions(+), 2 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index bb8b24838..16bd0bda7 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -53,6 +53,7 @@ class QuteBrowser(QApplication): config.init(confdir) self.commandparser = cmdutils.CommandParser() + self.searchparser = cmdutils.SearchParser() self.keyparser = KeyParser(self.mainwindow) self._init_cmds() self.mainwindow = MainWindow() @@ -61,9 +62,14 @@ class QuteBrowser(QApplication): self.mainwindow.tabs.keypress.connect(self.keyparser.handle) self.keyparser.set_cmd_text.connect(self.mainwindow.status.cmd.set_cmd) self.mainwindow.status.cmd.got_cmd.connect(self.commandparser.run) + self.mainwindow.status.cmd.got_search.connect(self.searchparser.search) + self.mainwindow.status.cmd.got_search_rev.connect( + self.searchparser.search_rev) self.mainwindow.status.cmd.returnPressed.connect( self.mainwindow.tabs.setFocus) self.commandparser.error.connect(self.mainwindow.status.disp_error) + self.searchparser.do_search.connect( + self.mainwindow.tabs.cur_search) self.keyparser.commandparser.error.connect( self.mainwindow.status.disp_error) self.keyparser.keystring_updated.connect( @@ -178,6 +184,7 @@ class QuteBrowser(QApplication): 'scroll_perc_y': self.mainwindow.tabs.cur_scroll_percent_y, 'undo': self.mainwindow.tabs.undo_close, 'pyeval': self.pyeval, + 'nextsearch': self.searchparser.nextsearch, } handler = handlers[cmd] diff --git a/qutebrowser/commands/__init__.py b/qutebrowser/commands/__init__.py index 5a5f870b2..730a75f6d 100644 --- a/qutebrowser/commands/__init__.py +++ b/qutebrowser/commands/__init__.py @@ -145,3 +145,10 @@ class PyEval(Command): nargs = 1 split_args = False + + +class NextSearch(Command): + """Jump to the next search term.""" + + nargs = 0 + hide = True diff --git a/qutebrowser/commands/utils.py b/qutebrowser/commands/utils.py index f87cf7fa1..436c2300c 100644 --- a/qutebrowser/commands/utils.py +++ b/qutebrowser/commands/utils.py @@ -5,6 +5,7 @@ import inspect import shlex from PyQt5.QtCore import QObject, pyqtSignal +from PyQt5.QtWebKitWidgets import QWebPage import qutebrowser.commands from qutebrowser.commands.exceptions import (ArgumentCountError, @@ -29,6 +30,43 @@ def register_all(): cmd_dict[n] = obj +class SearchParser(QObject): + """Parse qutebrowser searches.""" + text = None + flags = 0 + do_search = pyqtSignal(str, 'QWebPage::FindFlags') + + def search(self, text): + """Search for a text on a website. + + text -- The text to search for. + """ + self._search(text) + + def search_rev(self, text): + """Search for a text on a website in reverse direction. + + text -- The text to search for. + """ + self._search(text, rev=True) + + def _search(self, text, rev=False): + """Search for a text on the current page. + + text -- The text to search for. + rev -- Search direction. + """ + self.text = text + if rev: + self.flags = QWebPage.FindBackward + self.do_search.emit(self.text, self.flags) + + def nextsearch(self): + """Continue the search to the next term.""" + if self.text is not None: + self.do_search.emit(self.text, self.flags) + + class CommandParser(QObject): """Parse qutebrowser commandline commands.""" text = '' diff --git a/qutebrowser/utils/config.py b/qutebrowser/utils/config.py index e85151f2c..7afbc4b73 100644 --- a/qutebrowser/utils/config.py +++ b/qutebrowser/utils/config.py @@ -36,6 +36,7 @@ default_config = { 'u': 'undo', 'gg': 'scroll_perc_y 0', 'G': 'scroll_perc_y', + 'n': 'nextsearch', }, 'colors': { 'completion.fg': '#333333', diff --git a/qutebrowser/widgets/browser.py b/qutebrowser/widgets/browser.py index e90186a75..52d1710c1 100644 --- a/qutebrowser/widgets/browser.py +++ b/qutebrowser/widgets/browser.py @@ -145,6 +145,14 @@ class TabbedBrowser(TabWidget): # FIXME display warning if end of history self.currentWidget().forward() + def cur_search(self, text, flags): + """Search for text in the current page. + + text -- The text to search for. + flags -- The QWebPage::FindFlags. + """ + self.currentWidget().findText(text, flags) + def cur_scroll(self, dx, dy, count=None): """Scroll the current tab by count * dx/dy diff --git a/qutebrowser/widgets/statusbar/command.py b/qutebrowser/widgets/statusbar/command.py index 18ca7af92..64a4c3600 100644 --- a/qutebrowser/widgets/statusbar/command.py +++ b/qutebrowser/widgets/statusbar/command.py @@ -12,6 +12,9 @@ class Command(QLineEdit): """The commandline part of the statusbar.""" # Emitted when a command is triggered by the user got_cmd = pyqtSignal(str) + # Emitted for searches triggered by the user + got_search = pyqtSignal(str) + got_search_rev = pyqtSignal(str) statusbar = None # The status bar object esc_pressed = pyqtSignal() # Emitted when escape is pressed tab_pressed = pyqtSignal(bool) # Emitted when tab is pressed (arg: shift) @@ -47,13 +50,18 @@ class Command(QLineEdit): def process_cmdline(self): """Handle the command in the status bar.""" + signals = { + ':': self.got_cmd, + '/': self.got_search, + '?': self.got_search_rev, + } self._histbrowse_stop() text = self.text() if not self.history or text != self.history[-1]: self.history.append(text) self.setText('') - if text[0] == ':': - self.got_cmd.emit(text.lstrip(':')) + if text[0] in signals: + signals[text[0]].emit(text.lstrip(text[0])) def set_cmd(self, text): """Preset the statusbar to some text."""