Implement searching

This commit is contained in:
Florian Bruhin 2014-01-29 21:06:56 +01:00
parent c0f01d9219
commit 18c68ce74e
6 changed files with 71 additions and 2 deletions

View File

@ -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]

View File

@ -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

View File

@ -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 = ''

View File

@ -36,6 +36,7 @@ default_config = {
'u': 'undo',
'gg': 'scroll_perc_y 0',
'G': 'scroll_perc_y',
'n': 'nextsearch',
},
'colors': {
'completion.fg': '#333333',

View File

@ -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

View File

@ -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."""